解决OSCache集群情况下缓存不同步的问题

问题描述:oscache集群环境下,如果网络发生故障,会导致集群变成多个小的子群甚至单节点,这期间如果其中一节点做了更新缓存的操作,当网络恢复后,集群中的cache状态会不一致,导致用户看到不同的view。
解决思路:获取网络恢复后子群合并时产生的通知,并进行更新。最简单有效的就是清空所有节点的缓存。
jar版本:oscache2.4.1 jgroups2.12.1

oscache在集群环境下的配置需要打开cache.event.listeners属性,里面包含JavaGroupsBroadcastingListener。
JavaGroupsBroadcastingListener使用了JGroups的NotificationBus进行通信,而NotificationBus实现了receiver接口,其中的ViewAccept就是在view变化时被调用的。
我们需要做的是重写JavaGroupsBroadcastingListener的初始化方法。
重写NotificationBus的viewAccept方法。
修改oscache.properties里面的cache.event.listeners.
红色是添加或修改的,上代码:

MySelfJavaGroupsBroadcastingListener类:

public synchronized void initialize(Cache cache, Config config) throws InitializationException {
super.initialize(cache, config);

String properties = config.getProperty(CHANNEL_PROPERTIES);
String multicastIP = config.getProperty(MULTICAST_IP_PROPERTY);

if ((properties == null) && (multicastIP == null)) {
multicastIP = DEFAULT_MULTICAST_IP;
}

if (properties == null) {
properties = DEFAULT_CHANNEL_PROPERTIES_PRE + multicastIP.trim() + DEFAULT_CHANNEL_PROPERTIES_POST;
} else {
properties = properties.trim();
}

if (log.isInfoEnabled()) {
log.info("Starting a new JavaGroups broadcasting listener with properties=" + properties);
}

try {
[color=red] bus = new MySelfNotificationBus(BUS_NAME, properties);[/color] bus.start();
bus.getChannel().setOpt(Channel.LOCAL, new Boolean(false));
bus.setConsumer(this);
log.info("JavaGroups clustering support started successfully");
} catch (Exception e) {
throw new InitializationException("Initialization failed: " + e);
}
}


MySelfNotificationBus类:

[color=red]public class MySelfNotificationBus extends NotificationBus {

public MySelfNotificationBus (String bus_name, String properties) throws Exception {
super(bus_name,properties);
}

public synchronized void viewAccepted(View new_view) {
super.viewAccepted(new_view);
final NotificationBus bus = this;
if(new_view instanceof MergeView){
new Thread() {
public void run() {
bus.notifyConsumer(new ClusterNotification(ClusterNotification.FLUSH_CACHE,new Date()));
}
}.start();
}
}[/color]

oscache.properties:

cache.event.listeners=[color=red]x.x.x.MySelfJavaGroupsBroadcastingListener[/color], \
com.opensymphony.oscache.extra.CacheEntryEventListenerImpl, \
com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl, \
com.opensymphony.oscache.extra.ScopeEventListenerImpl
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值