Search in sources :

Example 1 with RouteStore

use of co.cask.cdap.route.store.RouteStore in project cdap by caskdata.

the class UserServiceEndpointStrategyTest method testStrategy.

@Test
public void testStrategy() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    Map<String, Integer> routeToVersion = ImmutableMap.of("2", 100);
    Map<ProgramId, RouteConfig> routeMap = ImmutableMap.of(serviceId, new RouteConfig(routeToVersion));
    RouteStore configStore = new InMemoryRouteStore(routeMap);
    UserServiceEndpointStrategy strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("2", Bytes.toString(picked.getPayload()));
    }
    // Switch config to choose version 3 always
    routeToVersion = ImmutableMap.of("3", 100);
    configStore.store(serviceId, new RouteConfig(routeToVersion));
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("3", Bytes.toString(picked.getPayload()));
    }
    // Switch config to choose verion 1 and 4 - 50% each
    routeToVersion = ImmutableMap.of("1", 50, "4", 50);
    configStore.store(serviceId, new RouteConfig(routeToVersion));
    Map<String, Integer> resultMap = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        Discoverable picked = strategy.pick();
        String version = Bytes.toString(picked.getPayload());
        if (resultMap.containsKey(version)) {
            resultMap.put(version, resultMap.get(version) + 1);
        } else {
            resultMap.put(version, 1);
        }
    }
    Assert.assertEquals(2, resultMap.size());
    double requestsToOne = resultMap.get("1");
    double requestsToTwo = resultMap.get("4");
    double requestRatio = requestsToOne / requestsToTwo;
    // Request Ratio should be close to 1.0 since we expect 50% of requests to go to each of these versions
    Assert.assertTrue(String.format("RequestRatio was %f and 1 got %f and 4 got %f", requestRatio, requestsToOne, requestsToTwo), requestRatio >= 0.7);
    Assert.assertTrue(String.format("RequestRatio was %f and 1 got %f and 4 got %f", requestRatio, requestsToOne, requestsToTwo), requestRatio <= 1.3);
    // Set the payload filter
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, null, "1");
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("1", Bytes.toString(picked.getPayload()));
    }
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RouteStore(co.cask.cdap.route.store.RouteStore) RouteConfig(co.cask.cdap.route.store.RouteConfig) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 2 with RouteStore

use of co.cask.cdap.route.store.RouteStore in project cdap by caskdata.

the class RouterMain method init.

@Override
public void init(String[] args) {
    LOG.info("Initializing Router...");
    try {
        // Load configuration
        cConf = CConfiguration.create();
        if (cConf.getBoolean(Constants.Security.ENABLED)) {
            int foundPaths = RouterAuditLookUp.getInstance().getNumberOfPaths();
            if (cConf.getBoolean(Constants.Router.ROUTER_AUDIT_PATH_CHECK_ENABLED) && foundPaths != ExceptedNumberOfAuditPolicyPaths.EXPECTED_PATH_NUMBER) {
                LOG.error("Failed to start the router due to the incorrect number of paths with AuditPolicy. " + "Expected: {}, found: {}", ExceptedNumberOfAuditPolicyPaths.EXPECTED_PATH_NUMBER, foundPaths);
                System.exit(1);
            }
            // Enable Kerberos login
            SecurityUtil.enableKerberosLogin(cConf);
        }
        // Initialize ZK client
        String zookeeper = cConf.get(Constants.Zookeeper.QUORUM);
        if (zookeeper == null) {
            LOG.error("No ZooKeeper quorum provided.");
            System.exit(1);
        }
        Injector injector = createGuiceInjector(cConf);
        zkClientService = injector.getInstance(ZKClientService.class);
        // Get the Router
        router = injector.getInstance(NettyRouter.class);
        // Get RouteStore so that we can close it when shutting down
        routeStore = injector.getInstance(RouteStore.class);
        LOG.info("Router initialized.");
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
        throw Throwables.propagate(t);
    }
}
Also used : ZKClientService(org.apache.twill.zookeeper.ZKClientService) Injector(com.google.inject.Injector) RouteStore(co.cask.cdap.route.store.RouteStore) ZKRouteStore(co.cask.cdap.route.store.ZKRouteStore)

Example 3 with RouteStore

use of co.cask.cdap.route.store.RouteStore in project cdap by caskdata.

the class UserServiceEndpointStrategyTest method testFallback.

@Test
public void testFallback() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    Map<ProgramId, RouteConfig> routeConfigMap = new HashMap<>();
    routeConfigMap.put(serviceId, new RouteConfig(Collections.<String, Integer>emptyMap()));
    RouteStore configStore = new InMemoryRouteStore(routeConfigMap);
    UserServiceEndpointStrategy strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.SMALLEST, null);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("0", Bytes.toString(picked.getPayload()));
    }
    // Remove "0", so the smallest version should now be "1"
    candidates.remove(0);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("1", Bytes.toString(picked.getPayload()));
    }
    // Test greatest strategy
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.LARGEST, null);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("4", Bytes.toString(picked.getPayload()));
    }
    // Remove "4", so the largest version should now be "3"
    candidates.remove(candidates.size() - 1);
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        Assert.assertEquals("3", Bytes.toString(picked.getPayload()));
    }
    // Test random strategy - remaining versions are 1, 2, 3
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.RANDOM, null);
    Set<String> pickedVersions = new HashSet<>();
    for (int i = 0; i < 1000; i++) {
        Discoverable picked = strategy.pick();
        pickedVersions.add(Bytes.toString(picked.getPayload()));
    }
    // There is a good probability that more than one version has been picked since its random
    Assert.assertTrue(pickedVersions.size() > 1);
    // Test drop strategy
    strategy = new UserServiceEndpointStrategy(serviceDiscovered, configStore, serviceId, RouteFallbackStrategy.DROP, null);
    for (int i = 0; i < 1000; i++) {
        Assert.assertNull(strategy.pick());
    }
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RouteStore(co.cask.cdap.route.store.RouteStore) RouteConfig(co.cask.cdap.route.store.RouteConfig) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with RouteStore

use of co.cask.cdap.route.store.RouteStore in project cdap by caskdata.

the class UserServiceEndpointStrategyTest method testVersionStrategy.

@Test
public void testVersionStrategy() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    RouteStore configStore = new InMemoryRouteStore(Collections.<ProgramId, RouteConfig>emptyMap());
    versionStrategyCheck(serviceDiscovered, configStore, serviceId, false);
    candidates.remove(2);
    versionStrategyCheck(serviceDiscovered, configStore, serviceId, true);
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) ArrayList(java.util.ArrayList) RouteStore(co.cask.cdap.route.store.RouteStore) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 5 with RouteStore

use of co.cask.cdap.route.store.RouteStore in project cdap by caskdata.

the class RouterResource method before.

@Override
protected void before() throws Throwable {
    CConfiguration cConf = CConfiguration.create();
    Injector injector = Guice.createInjector(new SecurityModules().getInMemoryModules(), new DiscoveryRuntimeModule().getInMemoryModules(), new AppFabricTestModule(cConf));
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    AccessTokenTransformer accessTokenTransformer = new MockAccessTokenTransfomer();
    RouteStore routeStore = injector.getInstance(RouteStore.class);
    SConfiguration sConf = injector.getInstance(SConfiguration.class);
    cConf.set(Constants.Router.ADDRESS, hostname);
    cConf.setInt(Constants.Router.ROUTER_PORT, 0);
    for (Map.Entry<String, String> entry : additionalConfig.entrySet()) {
        cConf.set(entry.getKey(), entry.getValue());
    }
    router = new NettyRouter(cConf, sConf, InetAddresses.forString(hostname), new RouterServiceLookup(cConf, (DiscoveryServiceClient) discoveryService, new RouterPathLookup(), routeStore), new MockTokenValidator("failme"), accessTokenTransformer, discoveryServiceClient);
    router.startAndWait();
    for (Map.Entry<Integer, String> entry : router.getServiceLookup().getServiceMap().entrySet()) {
        serviceMap.put(entry.getValue(), entry.getKey());
    }
}
Also used : DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RouteStore(co.cask.cdap.route.store.RouteStore) CConfiguration(co.cask.cdap.common.conf.CConfiguration) SecurityModules(co.cask.cdap.security.guice.SecurityModules) AccessTokenTransformer(co.cask.cdap.security.auth.AccessTokenTransformer) Injector(com.google.inject.Injector) SConfiguration(co.cask.cdap.common.conf.SConfiguration) AppFabricTestModule(co.cask.cdap.internal.guice.AppFabricTestModule) DiscoveryRuntimeModule(co.cask.cdap.common.guice.DiscoveryRuntimeModule) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RouteStore (co.cask.cdap.route.store.RouteStore)6 ServiceDiscoverable (co.cask.cdap.common.service.ServiceDiscoverable)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3 Injector (com.google.inject.Injector)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Discoverable (org.apache.twill.discovery.Discoverable)3 Test (org.junit.Test)3 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 SConfiguration (co.cask.cdap.common.conf.SConfiguration)2 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)2 AppFabricTestModule (co.cask.cdap.internal.guice.AppFabricTestModule)2 RouteConfig (co.cask.cdap.route.store.RouteConfig)2 AccessTokenTransformer (co.cask.cdap.security.auth.AccessTokenTransformer)2 SecurityModules (co.cask.cdap.security.guice.SecurityModules)2 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)2 ZKRouteStore (co.cask.cdap.route.store.ZKRouteStore)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1