Search in sources :

Example 6 with SlotInfo

use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.

the class AbstractDispatcherMetaServerController method getMetaServer.

private MetaServer getMetaServer(String clusterId, ForwardInfo forwardInfo, String uri) {
    int slotId = slotManager.getSlotIdByKey(clusterId);
    SlotInfo slotInfo = slotManager.getSlotInfo(slotId);
    if (forwardInfo != null && forwardInfo.getType() == ForwardType.MOVING) {
        if (!(slotInfo.getSlotState() == SLOT_STATE.MOVING && slotInfo.getToServerId() == currentMetaServer.getServerId())) {
            throw new MovingTargetException(forwardInfo, currentMetaServer.getServerId(), slotInfo, clusterId, slotId);
        }
        logger.info("[getMetaServer][use current server]");
        return currentMetaServer;
    }
    if (forwardInfo != null && forwardInfo.getType() == ForwardType.MULTICASTING) {
        logger.info("[multicast message][do now]");
        return currentMetaServer;
    }
    META_SERVER_SERVICE service = META_SERVER_SERVICE.fromPath(uri);
    Integer serverId = slotManager.getServerIdByKey(clusterId);
    if (serverId == null) {
        throw new IllegalStateException("clusterId:" + clusterId + ", unfound server");
    }
    if (service.getForwardType() == ForwardType.MULTICASTING) {
        logger.info("[getMetaServer][multi casting]{}, {}, {}", clusterId, forwardInfo, uri);
        Set<MetaServer> allServers = servers.allClusterServers();
        MetaServer current = servers.getClusterServer(serverId);
        allServers.remove(current);
        return MultiMetaServer.newProxy(current, new LinkedList<>(allServers));
    } else if (service.getForwardType() == ForwardType.FORWARD) {
        return servers.getClusterServer(serverId);
    } else {
        throw new IllegalStateException("service type can not be:" + service.getForwardType());
    }
}
Also used : MovingTargetException(com.ctrip.xpipe.redis.meta.server.rest.exception.MovingTargetException) MultiMetaServer(com.ctrip.xpipe.redis.meta.server.impl.MultiMetaServer) MetaServer(com.ctrip.xpipe.redis.meta.server.MetaServer) SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo) META_SERVER_SERVICE(com.ctrip.xpipe.redis.core.metaserver.META_SERVER_SERVICE)

Example 7 with SlotInfo

use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.

the class MoveSlotFromLiving method doExecute.

@Override
protected void doExecute() throws Exception {
    try {
        logger.info("[doExecute]{},{}->{}", slot, from, to);
        // change slot info to moving
        SlotInfo slotInfo = new SlotInfo(from.getServerId());
        slotInfo.moveingSlot(to.getServerId());
        setSlotInfo(slotInfo);
        CommandFuture<Void> importFuture = to.importSlot(slot);
        if (importFuture.await(taskMaxWaitMilli, TimeUnit.MILLISECONDS)) {
            if (!importFuture.isSuccess()) {
                logger.info("[doExecute][import fail]", importFuture.cause());
                setFailAndlRollback(new Exception("import fail", importFuture.cause()));
                return;
            }
        } else {
            // timeout
            setFailAndlRollback(new TimeoutException("import time out " + to + "," + taskMaxWaitMilli));
            return;
        }
        CommandFuture<Void> exportFuture = from.exportSlot(slot);
        if (exportFuture.await(taskMaxWaitMilli, TimeUnit.MILLISECONDS)) {
            if (!exportFuture.isSuccess()) {
                logger.info("[doExecute][export fail]", exportFuture.cause());
                setFailAndlRollback(new Exception("export fail", exportFuture.cause()));
                return;
            }
        } else {
            setFailAndlRollback(new TimeoutException("export time out " + from + "," + taskMaxWaitMilli));
            return;
        }
        setSuccess();
    } catch (Throwable th) {
        setFailAndlRollback(th);
    }
}
Also used : SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with SlotInfo

use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.

the class MoveSlotFromLiving method setFailAndlRollback.

private void setFailAndlRollback(Throwable th) throws ShardingException {
    setSlotInfo(new SlotInfo(from.getServerId()));
    getFrom().addSlot(slot);
    getTo().deleteSlot(slot);
    future().setFailure(th);
}
Also used : SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)

Example 9 with SlotInfo

use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.

the class DefaultSlotManager method getSlotsByServerId.

@Override
public Set<Integer> getSlotsByServerId(int serverId, boolean includeMoving) {
    try {
        lock.readLock().lock();
        if (includeMoving) {
            return serverMap.get(serverId);
        } else {
            Set<Integer> slots = new HashSet<>(serverMap.get(serverId));
            Set<Integer> movingSlots = new HashSet<>();
            for (Integer slotId : slots) {
                SlotInfo slotInfo = slotsMap.get(slotId);
                if (slotInfo.getSlotState() == SLOT_STATE.MOVING) {
                    movingSlots.add(slotId);
                }
            }
            slots.removeAll(movingSlots);
            return slots;
        }
    } finally {
        lock.readLock().unlock();
    }
}
Also used : SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo) HashSet(java.util.HashSet)

Example 10 with SlotInfo

use of com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo in project x-pipe by ctripcorp.

the class DefaultSlotManager method getSlotServerId.

@Override
public Integer getSlotServerId(int slotId) {
    try {
        lock.readLock().lock();
        SlotInfo slotInfo = slotsMap.get(slotId);
        if (slotInfo == null) {
            return null;
        }
        return slotInfo.getServerId();
    } finally {
        lock.readLock().unlock();
    }
}
Also used : SlotInfo(com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)

Aggregations

SlotInfo (com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)13 HashSet (java.util.HashSet)3 ClusterException (com.ctrip.xpipe.redis.meta.server.cluster.ClusterException)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 META_SERVER_SERVICE (com.ctrip.xpipe.redis.core.metaserver.META_SERVER_SERVICE)1 MetaServer (com.ctrip.xpipe.redis.meta.server.MetaServer)1 ClusterServer (com.ctrip.xpipe.redis.meta.server.cluster.ClusterServer)1 MultiMetaServer (com.ctrip.xpipe.redis.meta.server.impl.MultiMetaServer)1 MovingTargetException (com.ctrip.xpipe.redis.meta.server.rest.exception.MovingTargetException)1 TimeoutException (java.util.concurrent.TimeoutException)1