Search in sources :

Example 21 with Referer

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

the class ActiveWeightLoadBalanceTest method allAvailableCluster.

private void allAvailableCluster(int refererSize) {
    ActiveWeightLoadBalance balance = createBalance(refererSize, 0);
    Referer referer = balance.select(null);
    Assert.assertNotNull(referer);
    if (refererSize <= ActiveWeightLoadBalance.MAX_REFERER_COUNT) {
        Assert.assertEquals(referer.activeRefererCount(), lowActive);
    } else {
        Assert.assertTrue(refererSize - ActiveWeightLoadBalance.MAX_REFERER_COUNT + 1 >= referer.activeRefererCount());
    }
    List<Referer> referersHolder = new ArrayList<Referer>();
    balance.selectToHolder(null, referersHolder);
    if (refererSize <= ActiveWeightLoadBalance.MAX_REFERER_COUNT) {
        Assert.assertEquals(referersHolder.size(), refererSize);
        check(referersHolder);
    } else {
        Assert.assertEquals(referersHolder.size(), ActiveWeightLoadBalance.MAX_REFERER_COUNT);
        check(referersHolder);
    }
}
Also used : Referer(com.weibo.api.motan.rpc.Referer) MockReferer(com.weibo.api.motan.mock.MockReferer) ArrayList(java.util.ArrayList)

Example 22 with Referer

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

the class ActiveWeightLoadBalanceTest method main.

public static void main(String[] args) {
    List<MockReferer> referers = new ArrayList<MockReferer>();
    for (int i = 0; i < 20; i++) {
        MockReferer referer = new MockReferer();
        referer.active = 1 + i;
        referers.add(referer);
    }
    for (int j = 0; j < 20; j++) {
        List<Referer> copy = new ArrayList<Referer>(referers);
        Collections.shuffle(copy);
        String buffer = "[";
        for (Referer referer : copy) {
            buffer += referer.activeRefererCount() + ",";
        }
        Collections.sort(copy, new ActiveWeightLoadBalance.LowActivePriorityComparator());
        int prefix = -1;
        for (Referer aCopy : copy) {
            if (aCopy.activeRefererCount() <= prefix) {
                throw new RuntimeException("bug bug bug");
            }
            prefix = aCopy.activeRefererCount();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MockReferer(com.weibo.api.motan.mock.MockReferer) Referer(com.weibo.api.motan.rpc.Referer) MockReferer(com.weibo.api.motan.mock.MockReferer)

Example 23 with Referer

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

the class ConfigurableWeightLoadBalanceTest method testDoSelect.

@Test
public void testDoSelect() {
    int[] groupWeight = new int[] { 2, 3, 5 };
    Map<String, AtomicInteger> counter = generate(3, groupWeight, new int[] { 3, 4, 5 });
    for (int j = 0; j < 100; j++) {
        int size = 100;
        for (int i = 0; i < size; i++) {
            Referer referer = balance.doSelect(new DefaultRequest());
            String group = referer.getServiceUrl().getGroup();
            counter.get(group).incrementAndGet();
        }
        for (String key : counter.keySet()) {
            float total = size * (j + 1);
            float ratio = counter.get(key).get() * 10 / total;
            int weight = groupWeight[Integer.parseInt(key.substring("group".length()))];
            // 权重误差不超过阈值。
            Assert.assertTrue(Math.abs(weight - ratio) < 2);
        }
    }
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Referer(com.weibo.api.motan.rpc.Referer) MockReferer(com.weibo.api.motan.mock.MockReferer) Test(org.junit.Test)

Example 24 with Referer

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

the class RandomLoadBalanceTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    randomLoadBalance.onRefresh(referers);
    mockery.checking(new Expectations() {

        {
            int i = 0;
            for (Referer<IHello> ref : referers) {
                boolean rs = i++ >= falseCount;
                atLeast(0).of(ref).isAvailable();
                will(returnValue(rs));
            }
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Referer(com.weibo.api.motan.rpc.Referer)

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