Search in sources :

Example 1 with ClusterException

use of com.ctrip.xpipe.redis.meta.server.cluster.ClusterException 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();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ClusterException(com.ctrip.xpipe.redis.meta.server.cluster.ClusterException) HashMap(java.util.HashMap) SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo) ClusterException(com.ctrip.xpipe.redis.meta.server.cluster.ClusterException)

Example 2 with ClusterException

use of com.ctrip.xpipe.redis.meta.server.cluster.ClusterException 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();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ClusterException(com.ctrip.xpipe.redis.meta.server.cluster.ClusterException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ClusterException(com.ctrip.xpipe.redis.meta.server.cluster.ClusterException) SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)

Aggregations

ClusterException (com.ctrip.xpipe.redis.meta.server.cluster.ClusterException)2 SlotInfo (com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1