Search in sources :

Example 11 with Referer

use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.

the class ClusterSpiTest method testCall.

public void testCall() {
    initCluster(true);
    final Request request = mockRequest();
    final Response response = mockery.mock(Response.class);
    final URL url = mockURL();
    mockery.checking(new Expectations() {

        {
            for (Referer<IHello> ref : referers) {
                atLeast(0).of(ref).call(request);
                will(returnValue(response));
                atLeast(0).of(ref).isAvailable();
                will(returnValue(true));
                atLeast(0).of(ref).getUrl();
                will(returnValue(url));
                atLeast(1).of(ref).destroy();
            }
        }
    });
    clusterSpi.call(request);
    clusterSpi.destroy();
    try {
        clusterSpi.call(request);
        fail("Should not run to here!");
    } catch (Exception e) {
        assertTrue(e instanceof MotanServiceException);
    }
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 12 with Referer

use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.

the class ClusterTest method setUp.

@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    super.setUp();
    HaStrategy<IHello> ha = new FailoverHaStrategy<IHello>();
    LoadBalance<IHello> lb = new RandomLoadBalance<IHello>();
    referers = new ArrayList<Referer<IHello>>();
    referers.add(mockery.mock(Referer.class, "ref1"));
    referers.add(mockery.mock(Referer.class, "ref2"));
    cluster.setUrl(new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName()));
    cluster.setHaStrategy(ha);
    cluster.setLoadBalance(lb);
    cluster.onRefresh(referers);
    cluster.init();
}
Also used : Referer(com.weibo.api.motan.rpc.Referer) FailoverHaStrategy(com.weibo.api.motan.cluster.ha.FailoverHaStrategy) IHello(com.weibo.api.motan.protocol.example.IHello) RandomLoadBalance(com.weibo.api.motan.cluster.loadbalance.RandomLoadBalance) URL(com.weibo.api.motan.rpc.URL)

Example 13 with Referer

use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.

the class RandomLoadBalanceTest method testSelectToHolder.

public void testSelectToHolder() {
    final Request request = mockery.mock(Request.class);
    mockery.checking(new Expectations() {

        {
            atLeast(1).of(request).getArguments();
            will(returnValue(new Object[] { 1, 2, 3 }));
            atLeast(0).of(request).getParamtersDesc();
            will(returnValue("void_"));
        }
    });
    for (int i = 0; i < 10; i++) {
        List<Referer<IHello>> refHolder = new ArrayList<Referer<IHello>>();
        randomLoadBalance.selectToHolder(request, refHolder);
        assertEquals(refHolder.size(), (referers.size() - falseCount));
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) ArrayList(java.util.ArrayList) Referer(com.weibo.api.motan.rpc.Referer) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 14 with Referer

use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.

the class ClusterSupport method notify.

/**
     * <pre>
     * 1 notify的执行需要串行
     * 2 notify通知都是全量通知,在设入新的referer后,cluster需要把不再使用的referer进行回收,避免资源泄漏;
     * 3 如果该registry对应的referer数量为0,而没有其他可用的referers,那就忽略该次通知;
     * 4 此处对protoco进行decorator处理,当前为增加filters
     * </pre>
     */
@Override
public synchronized void notify(URL registryUrl, List<URL> urls) {
    if (CollectionUtil.isEmpty(urls)) {
        onRegistryEmpty(registryUrl);
        LoggerUtil.warn("ClusterSupport config change notify, urls is empty: registry={} service={} urls=[]", registryUrl.getUri(), url.getIdentity());
        return;
    }
    LoggerUtil.info("ClusterSupport config change notify: registry={} service={} urls={}", registryUrl.getUri(), url.getIdentity(), getIdentities(urls));
    // 通知都是全量通知,在设入新的referer后,cluster内部需要把不再使用的referer进行回收,避免资源泄漏
    // ////////////////////////////////////////////////////////////////////////////////
    // 判断urls中是否包含权重信息,并通知loadbalance。
    processWeights(urls);
    List<Referer<T>> newReferers = new ArrayList<Referer<T>>();
    for (URL u : urls) {
        if (!u.canServe(url)) {
            continue;
        }
        Referer<T> referer = getExistingReferer(u, registryReferers.get(registryUrl));
        if (referer == null) {
            // careful u: serverURL, refererURL的配置会被serverURL的配置覆盖
            URL refererURL = u.createCopy();
            mergeClientConfigs(refererURL);
            referer = protocol.refer(interfaceClass, refererURL, u);
        }
        if (referer != null) {
            newReferers.add(referer);
        }
    }
    if (CollectionUtil.isEmpty(newReferers)) {
        onRegistryEmpty(registryUrl);
        return;
    }
    // 此处不销毁referers,由cluster进行销毁
    registryReferers.put(registryUrl, newReferers);
    refreshCluster();
}
Also used : ArrayList(java.util.ArrayList) Referer(com.weibo.api.motan.rpc.Referer) URL(com.weibo.api.motan.rpc.URL)

Example 15 with Referer

use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.

the class FailoverHaStrategy method call.

@Override
public Response call(Request request, LoadBalance<T> loadBalance) {
    List<Referer<T>> referers = selectReferers(request, loadBalance);
    if (referers.isEmpty()) {
        throw new MotanServiceException(String.format("FailoverHaStrategy No referers for request:%s, loadbalance:%s", request, loadBalance));
    }
    URL refUrl = referers.get(0).getUrl();
    // 先使用method的配置
    int tryCount = refUrl.getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.retries.getName(), URLParamType.retries.getIntValue());
    // 如果有问题,则设置为不重试
    if (tryCount < 0) {
        tryCount = 0;
    }
    for (int i = 0; i <= tryCount; i++) {
        Referer<T> refer = referers.get(i % referers.size());
        try {
            request.setRetries(i);
            return refer.call(request);
        } catch (RuntimeException e) {
            // 对于业务异常,直接抛出
            if (ExceptionUtil.isBizException(e)) {
                throw e;
            } else if (i >= tryCount) {
                throw e;
            }
            LoggerUtil.warn(String.format("FailoverHaStrategy Call false for request:%s error=%s", request, e.getMessage()));
        }
    }
    throw new MotanFrameworkException("FailoverHaStrategy.call should not come here!");
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL)

Aggregations

Referer (com.weibo.api.motan.rpc.Referer)24 URL (com.weibo.api.motan.rpc.URL)13 IHello (com.weibo.api.motan.protocol.example.IHello)12 Expectations (org.jmock.Expectations)12 ArrayList (java.util.ArrayList)11 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)8 MockReferer (com.weibo.api.motan.mock.MockReferer)7 Request (com.weibo.api.motan.rpc.Request)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 IWorld (com.weibo.api.motan.protocol.example.IWorld)4 Response (com.weibo.api.motan.rpc.Response)4 Test (org.junit.Test)4 RegistryService (com.weibo.api.motan.registry.RegistryService)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 LoadBalance (com.weibo.api.motan.cluster.LoadBalance)1 FailoverHaStrategy (com.weibo.api.motan.cluster.ha.FailoverHaStrategy)1 RandomLoadBalance (com.weibo.api.motan.cluster.loadbalance.RandomLoadBalance)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1