Search in sources :

Example 1 with MetaServer

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

the class DispatchMoving method exportServer.

protected MetaServer exportServer(Object key) {
    SlotInfo slotInfo = slotManager.getSlotInfoByKey(key);
    if (slotInfo.getSlotState() == SLOT_STATE.MOVING) {
        MetaServer clusterServer = clusterServers.getClusterServer(slotInfo.getToServerId());
        logger.info("[exportServer]{}, {}", key, clusterServer);
        return clusterServer;
    }
    return null;
}
Also used : MetaServer(com.ctrip.xpipe.redis.meta.server.MetaServer)

Example 2 with MetaServer

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

the class MultiMetaServer method invoke.

@Override
public Object invoke(Object proxy, final Method method, final Object[] rawArgs) throws Throwable {
    for (final MetaServer metaServer : otherServers) {
        final Object[] args = copy(rawArgs);
        executors.execute(new AbstractExceptionLogTask() {

            @Override
            protected void doRun() throws Exception {
                method.invoke(metaServer, args);
            }
        });
    }
    final Object[] args = copy(rawArgs);
    return method.invoke(dstServer, args);
}
Also used : AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) MetaServer(com.ctrip.xpipe.redis.meta.server.MetaServer)

Example 3 with MetaServer

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

the class MultiMetaServerTest method testMultiProxy.

@Test
public void testMultiProxy() {
    int serversCount = 10;
    List<MetaServer> servers = new LinkedList<>();
    for (int i = 0; i < serversCount - 1; i++) {
        servers.add(mock(MetaServer.class));
    }
    MetaServer metaServer = MultiMetaServer.newProxy(mock(MetaServer.class), servers);
    final ForwardInfo forwardInfo = new ForwardInfo();
    metaServer.clusterDeleted(getClusterId(), forwardInfo);
    for (MetaServer mockServer : servers) {
        verify(mockServer).clusterDeleted(eq(getClusterId()), argThat(new BaseMatcher<ForwardInfo>() {

            @Override
            public boolean matches(Object item) {
                // should be cloned
                if (item == forwardInfo) {
                    return false;
                }
                return true;
            }

            @Override
            public void describeTo(Description description) {
            }
        }));
    }
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) MetaServer(com.ctrip.xpipe.redis.meta.server.MetaServer) ForwardInfo(com.ctrip.xpipe.redis.meta.server.rest.ForwardInfo) LinkedList(java.util.LinkedList) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Example 4 with MetaServer

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

the class DispatchMoving method tryMovingDispatch.

@Around("pointcutMovingMethod() && args(clusterId,..,forwardInfo)")
public Object tryMovingDispatch(ProceedingJoinPoint joinpoint, String clusterId, ForwardInfo forwardInfo) throws Throwable {
    String targetMethodName = joinpoint.getSignature().getName();
    logger.info("[tryMovingDispatch]{}, {}", targetMethodName, clusterId);
    if (forwardInfo != null && forwardInfo.getType() == ForwardType.MOVING) {
        logger.info("[tryMovingDispatch][isMoving][self process]{}, {}", targetMethodName, clusterId);
        return joinpoint.proceed();
    }
    MetaServer exportServer = exportServer(clusterId);
    if (exportServer == currentClusterServer) {
        throw new IllegalStateException("export server should not be current " + exportServer);
    }
    if (exportServer != null) {
        Object[] args = joinpoint.getArgs();
        if (forwardInfo == null) {
            forwardInfo = new ForwardInfo();
            replace(args, forwardInfo);
        }
        forwardInfo.setType(ForwardType.MOVING);
        Method method = ObjectUtils.getMethod(targetMethodName, MetaServer.class);
        if (method == null) {
            // impossible to happen
            throw new IllegalStateException("can not find method " + targetMethodName);
        }
        return method.invoke(exportServer, args);
    }
    return joinpoint.proceed();
}
Also used : MetaServer(com.ctrip.xpipe.redis.meta.server.MetaServer) Method(java.lang.reflect.Method) ForwardInfo(com.ctrip.xpipe.redis.meta.server.rest.ForwardInfo) Around(org.aspectj.lang.annotation.Around)

Example 5 with MetaServer

use of com.ctrip.xpipe.redis.meta.server.MetaServer 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)

Aggregations

MetaServer (com.ctrip.xpipe.redis.meta.server.MetaServer)6 MultiMetaServer (com.ctrip.xpipe.redis.meta.server.impl.MultiMetaServer)2 ForwardInfo (com.ctrip.xpipe.redis.meta.server.rest.ForwardInfo)2 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 META_SERVER_SERVICE (com.ctrip.xpipe.redis.core.metaserver.META_SERVER_SERVICE)1 AbstractMetaServerTest (com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)1 SlotInfo (com.ctrip.xpipe.redis.meta.server.cluster.SlotInfo)1 MovingTargetException (com.ctrip.xpipe.redis.meta.server.rest.exception.MovingTargetException)1 UnfoundAliveSererException (com.ctrip.xpipe.redis.meta.server.rest.exception.UnfoundAliveSererException)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 Around (org.aspectj.lang.annotation.Around)1 BaseMatcher (org.hamcrest.BaseMatcher)1 Description (org.hamcrest.Description)1 Test (org.junit.Test)1 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)1