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()));
}
}
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);
}
}
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());
}
}
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);
}
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());
}
}
Aggregations