Search in sources :

Example 1 with TransactionControlFilter

use of io.servicecomb.loadbalance.filter.TransactionControlFilter in project java-chassis by ServiceComb.

the class TestLoadBalancer method testGetAllServers.

@Test
public void testGetAllServers() {
    List<Server> servers = new ArrayList<Server>();
    Server server = Mockito.mock(Server.class);
    servers.add(server);
    Mockito.when(serverList.getInitialListOfServers()).thenReturn(servers);
    TransactionControlFilter filter = Mockito.mock(TransactionControlFilter.class);
    Mockito.when(filter.getFilteredListOfServers(servers)).thenReturn(servers);
    Assert.assertEquals(servers, loadBalancer.getAllServers());
}
Also used : Server(com.netflix.loadbalancer.Server) ArrayList(java.util.ArrayList) TransactionControlFilter(io.servicecomb.loadbalance.filter.TransactionControlFilter) SimpleTransactionControlFilter(io.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) Test(org.junit.Test)

Example 2 with TransactionControlFilter

use of io.servicecomb.loadbalance.filter.TransactionControlFilter in project java-chassis by ServiceComb.

the class LoadbalanceHandler method setTransactionControlFilter.

protected void setTransactionControlFilter(LoadBalancer lb, Invocation invocation) {
    final String filterName = TransactionControlFilter.class.getName();
    String policyClsName = Configuration.INSTANCE.getFlowsplitFilterPolicy(invocation.getMicroserviceName());
    if (policyClsName.isEmpty()) {
        lb.removeFilter(filterName);
        return;
    }
    if (lb.containsFilter(filterName)) {
        return;
    }
    try {
        Class<?> policyCls = Class.forName(policyClsName);
        if (!TransactionControlFilter.class.isAssignableFrom(policyCls)) {
            String errMsg = String.format("Define instance filter %s in yaml, but not extends abstract class TransactionControlFilter.", policyClsName);
            LOGGER.error(errMsg);
            throw new Error(errMsg);
        }
        TransactionControlFilter transactionControlFilter = (TransactionControlFilter) policyCls.newInstance();
        transactionControlFilter.setInvocation(invocation);
        transactionControlFilter.setLoadBalancerStats(lb.getLoadBalancerStats());
        lb.putFilter(filterName, transactionControlFilter);
    } catch (Throwable e) {
        String errMsg = "Fail to create instance of class: " + policyClsName;
        LOGGER.error(errMsg);
        throw new Error(errMsg, e);
    }
}
Also used : TransactionControlFilter(io.servicecomb.loadbalance.filter.TransactionControlFilter)

Example 3 with TransactionControlFilter

use of io.servicecomb.loadbalance.filter.TransactionControlFilter in project java-chassis by ServiceComb.

the class TestLoadBalancer method testFilter.

@Test
public void testFilter() {
    Assert.assertEquals(0, loadBalancer.getFilterSize());
    TransactionControlFilter filter = new SimpleTransactionControlFilter();
    loadBalancer.putFilter("test", filter);
    Assert.assertEquals(1, loadBalancer.getFilterSize());
    Assert.assertTrue(loadBalancer.containsFilter("test"));
    loadBalancer.removeFilter("test");
    Assert.assertEquals(0, loadBalancer.getFilterSize());
    Assert.assertFalse(loadBalancer.containsFilter("test"));
}
Also used : TransactionControlFilter(io.servicecomb.loadbalance.filter.TransactionControlFilter) SimpleTransactionControlFilter(io.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) SimpleTransactionControlFilter(io.servicecomb.loadbalance.filter.SimpleTransactionControlFilter) Test(org.junit.Test)

Aggregations

TransactionControlFilter (io.servicecomb.loadbalance.filter.TransactionControlFilter)3 SimpleTransactionControlFilter (io.servicecomb.loadbalance.filter.SimpleTransactionControlFilter)2 Test (org.junit.Test)2 Server (com.netflix.loadbalancer.Server)1 ArrayList (java.util.ArrayList)1