Search in sources :

Example 1 with Referer

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

the class FailoverHaStrategyTest method setUp.

@Before
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    super.setUp();
    loadBalance = mockery.mock(LoadBalance.class);
    final Referer<IWorld> referer1 = mockery.mock(Referer.class, "ref1");
    final Referer<IWorld> referer2 = mockery.mock(Referer.class, "ref2");
    referers = new ArrayList<Referer<IWorld>>();
    referers.add(referer1);
    referers.add(referer2);
    failoverHaStrategy = new FailoverHaStrategy<IWorld>() {

        @Override
        protected List<Referer<IWorld>> selectReferers(Request request, LoadBalance<IWorld> loadBalance) {
            return referers;
        }
    };
    URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.LOCALHOST, 0, IWorld.class.getName());
    url.addParameter(URLParamType.retries.getName(), String.valueOf(retries));
    failoverHaStrategy.setUrl(url);
}
Also used : IWorld(com.weibo.api.motan.protocol.example.IWorld) LoadBalance(com.weibo.api.motan.cluster.LoadBalance) Referer(com.weibo.api.motan.rpc.Referer) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) ArrayList(java.util.ArrayList) List(java.util.List) URL(com.weibo.api.motan.rpc.URL) Before(org.junit.Before)

Example 2 with Referer

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

the class ClusterSpiTest method initCluster.

@SuppressWarnings("unchecked")
private void initCluster(boolean throwException) {
    referers = new ArrayList<Referer<IHello>>();
    for (int i = 0; i < 10; i++) {
        referers.add(mockery.mock(Referer.class, "ref_" + i));
    }
    clusterSpi.setHaStrategy(new FailoverHaStrategy<IHello>());
    clusterSpi.setLoadBalance(new RandomLoadBalance<IHello>());
    URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
    url.addParameter(URLParamType.throwException.getName(), String.valueOf(throwException));
    url.addParameter(URLParamType.retries.getName(), "2");
    clusterSpi.setUrl(url);
    clusterSpi.onRefresh(referers);
    clusterSpi.init();
}
Also used : Referer(com.weibo.api.motan.rpc.Referer) RegistryService(com.weibo.api.motan.registry.RegistryService) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL)

Example 3 with Referer

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

the class ClusterSupport method doRefreshReferersByUrls.

private void doRefreshReferersByUrls(URL registryUrl, List<URL> serviceUrls) {
    List<Referer<T>> newReferers = new ArrayList<>();
    for (URL u : serviceUrls) {
        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 : Referer(com.weibo.api.motan.rpc.Referer) URL(com.weibo.api.motan.rpc.URL)

Example 4 with Referer

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

the class ClusterSupportTest method testRefreshReferers.

@Test
public void testRefreshReferers() {
    MotanSwitcherUtil.setSwitcherValue("feature.motan.partial.server", true);
    List<URL> copy = new ArrayList<URL>();
    clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 2)));
    clusterSupport.notify(registries.get(regProtocol2).getUrl(), null);
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
    clusterSupport.refreshReferers();
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
    clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 6)));
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
    Assert.assertEquals(getAvailableReferersCount(), 4);
    Referer referer1 = clusterSupport.getCluster().getReferers().get(0);
    Referer referer2 = clusterSupport.getCluster().getReferers().get(1);
    // 设置1节点不可用,未小于阈值,不触发refresh
    availableMap.put(referer1.getUrl().toString(), false);
    clusterSupport.refreshReferers();
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
    Assert.assertEquals(getAvailableReferersCount(), 3);
    // 设置2节点不可用,小于阈值,触发refresh
    availableMap.put(referer2.getUrl().toString(), false);
    clusterSupport.refreshReferers();
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 6);
    Assert.assertEquals(getAvailableReferersCount(), 4);
    // 设置1节点恢复,未大于阈值,不触发refresh
    availableMap.put(referer2.getUrl().toString(), true);
    clusterSupport.refreshReferers();
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 6);
    Assert.assertEquals(getAvailableReferersCount(), 5);
    // 设置0节点恢复,大于阈值,触发refresh
    availableMap.put(referer1.getUrl().toString(), true);
    clusterSupport.refreshReferers();
    Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
    Assert.assertEquals(getAvailableReferersCount(), 4);
}
Also used : ArrayList(java.util.ArrayList) Referer(com.weibo.api.motan.rpc.Referer) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 5 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"));
    final URL url = URL.valueOf("motan%3A%2F%2F10.209.128.244%3A8000%2Fcom.weibo.api.motan.protocol.example.IWorld%3Fprotocol%3Dmotan%26export%3Dmotan%3A8000%26application%3Dapi%26module%3Dtest%26check%3Dtrue%26refreshTimestamp%3D1373275099717%26methodconfig.world%28void%29.retries%3D1%26id%3Dmotan%26methodconfig.world%28java.lang.String%29.retries%3D1%26methodconfig.world%28java.lang.String%2Cboolean%29.retries%3D1%26nodeType%3Dservice%26group%3Dwangzhe-test-yf%26shareChannel%3Dtrue%26&");
    mockery.checking(new Expectations() {

        {
            for (Referer<IHello> ref : referers) {
                atLeast(0).of(ref).getServiceUrl();
                will(returnValue(url));
                atLeast(1).of(ref).destroy();
            }
        }
    });
    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 : Expectations(org.jmock.Expectations) 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)

Aggregations

Referer (com.weibo.api.motan.rpc.Referer)20 ArrayList (java.util.ArrayList)11 IHello (com.weibo.api.motan.protocol.example.IHello)9 URL (com.weibo.api.motan.rpc.URL)8 MockReferer (com.weibo.api.motan.mock.MockReferer)7 Expectations (org.jmock.Expectations)6 Request (com.weibo.api.motan.rpc.Request)5 Test (org.junit.Test)4 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)3 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 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 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 IWorld (com.weibo.api.motan.protocol.example.IWorld)1 RegistryService (com.weibo.api.motan.registry.RegistryService)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1