use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.
the class DefaultSlotManager method refresh.
@Override
public void refresh(int... slotIds) throws ClusterException {
String slotsPath = MetaZkConfig.getMetaServerSlotsPath();
CuratorFramework client = zkClient.get();
Map<Integer, SlotInfo> slotsInfo = new HashMap<>();
for (int slotId : slotIds) {
byte[] slotContent = new byte[0];
try {
slotContent = client.getData().forPath(slotsPath + "/" + String.valueOf(slotId));
} catch (Exception e) {
throw new ClusterException("get data for:" + slotId, e);
}
SlotInfo slotInfo = Codec.DEFAULT.decode(slotContent, SlotInfo.class);
slotsInfo.put(slotId, slotInfo);
}
try {
lock.writeLock().lock();
for (Entry<Integer, SlotInfo> entry : slotsInfo.entrySet()) {
Integer slotId = entry.getKey();
SlotInfo slotInfo = entry.getValue();
SlotInfo oldSlotInfo = slotsMap.get(slotId);
slotsMap.put(slotId, slotInfo);
if (oldSlotInfo != null) {
Set<Integer> oldServerSlot = serverMap.get(oldSlotInfo.getServerId());
if (oldServerSlot != null) {
oldServerSlot.remove(slotId);
}
}
getOrCreateServerMap(serverMap, slotInfo.getServerId()).add(slotId);
}
} finally {
lock.writeLock().unlock();
}
}
use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.
the class DefaultSlotManager method getServerIdByKey.
@Override
public Integer getServerIdByKey(Object key) {
int slotId = getSlotIdByKey(key);
try {
lock.readLock().lock();
SlotInfo slotInfo = slotsMap.get(slotId);
if (slotInfo == null) {
return null;
}
return slotInfo.getServerId();
} finally {
lock.readLock().unlock();
}
}
use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.
the class DefaultSlotManager method doRefresh.
private void doRefresh() throws ClusterException {
logger.debug("[doRefresh]");
Map<Integer, SlotInfo> slotsMap = new ConcurrentHashMap<>();
Map<Integer, Set<Integer>> serverMap = new ConcurrentHashMap<>();
try {
CuratorFramework client = zkClient.get();
String slotsPath = MetaZkConfig.getMetaServerSlotsPath();
for (String slotPath : client.getChildren().forPath(slotsPath)) {
int slot = Integer.parseInt(slotPath);
byte[] slotContent = client.getData().forPath(slotsPath + "/" + slotPath);
SlotInfo slotInfo = Codec.DEFAULT.decode(slotContent, SlotInfo.class);
Set<Integer> serverSlots = getOrCreateServerMap(serverMap, slotInfo.getServerId());
serverSlots.add(slot);
slotsMap.put(slot, slotInfo);
}
} catch (Exception e) {
throw new ClusterException("doRefersh", e);
}
try {
lock.writeLock().lock();
this.slotsMap = slotsMap;
this.serverMap = serverMap;
} finally {
lock.writeLock().unlock();
}
}
Aggregations