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());
}
}
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);
}
}
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);
}
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();
}
}
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();
}
}
Aggregations