@GET @Path("/routeconfig") public void getRouteConfig(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception { ProgramId programId = Ids.namespace(namespaceId).app(appId).service(serviceId); RouteConfig routeConfig = routeStore.fetch(programId); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(routeConfig.getRoutes())); }
@GET @Path("/routeconfig") public void getRouteConfig(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception { ProgramId programId = Ids.namespace(namespaceId).app(appId).service(serviceId); RouteConfig routeConfig = routeStore.fetch(programId); responder.sendJson(HttpResponseStatus.OK, GSON.toJson(routeConfig.getRoutes())); }
@Override public Boolean call() throws Exception { return routeStore.fetch(programId).getRoutes().isEmpty(); } }, 5, TimeUnit.SECONDS);
@Override public Boolean call() throws Exception { return routeStore.fetch(s1).getRoutes().isEmpty(); } }, 5L, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
@Override public Map<String, Integer> call() throws Exception { return readRouteStore.fetch(programId).getRoutes(); } }, 5L, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
@Override public Map<String, Integer> call() throws Exception { return readingRouteStore.fetch(s1).getRoutes(); } }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
RouteConfig routeConfig = routeStore.fetch(serviceId); if (!routeConfig.isValid()) { if (fallbackStrategy.equals(RouteFallbackStrategy.DROP)) {
@Test public void testAbsenceOfConfig() throws Exception { ApplicationId appId = new ApplicationId("n1", "a1"); final ProgramId programId = appId.service("testAbsenceOfConfig"); try (final RouteStore routeStore = new ZKRouteStore(zkClientService)) { Assert.assertNotNull(routeStore.fetch(programId)); Assert.assertTrue(routeStore.fetch(programId).getRoutes().isEmpty()); routeStore.store(programId, new RouteConfig(TEST_ROUTE_CONFIG)); Tasks.waitFor(false, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return routeStore.fetch(programId).getRoutes().isEmpty(); } }, 5, TimeUnit.SECONDS); Assert.assertEquals(TEST_ROUTE_CONFIG, routeStore.fetch(programId).getRoutes()); } }
@Test public void testRouteStorage() throws Exception { RouteStore routeStore = new LocalRouteStore(datasetFramework, new InMemoryTxSystemClient(txManager)); ApplicationId appId = new ApplicationId("n1", "a1"); ProgramId service1 = appId.service("s1"); RouteConfig routeConfig = new RouteConfig(ImmutableMap.of("v1", 100)); routeStore.store(service1, routeConfig); Assert.assertEquals(routeConfig.getRoutes(), routeStore.fetch(service1).getRoutes()); routeStore.delete(service1); Assert.assertNotNull(routeStore.fetch(service1)); try { routeStore.delete(service1); Assert.fail("Config should have been deleted and thus a NotFoundException must have been thrown."); } catch (NotFoundException e) { // expected } } }
@Test public void testCreateOfConfig() throws Exception { // This test first the config is missing and then created ApplicationId appId = new ApplicationId("n1", "a1"); final ProgramId programId = appId.service("testCreateOfConfig"); try (final RouteStore writeRouteStore = new ZKRouteStore(zkClientService)) { try (final RouteStore readRouteStore = new ZKRouteStore(zkClientService)) { // First it should be no route Assert.assertTrue(readRouteStore.fetch(programId).getRoutes().isEmpty()); // Write a new route config writeRouteStore.store(programId, new RouteConfig(TEST_ROUTE_CONFIG)); // Fetch it again and it should has the changes Tasks.waitFor(TEST_ROUTE_CONFIG, new Callable<Map<String, Integer>>() { @Override public Map<String, Integer> call() throws Exception { return readRouteStore.fetch(programId).getRoutes(); } }, 5L, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); } } }
@Test public void testStore() throws Exception { ApplicationId appId = new ApplicationId("n1", "a1"); final ProgramId s1 = appId.service("testStore"); try (final RouteStore routeStore = new ZKRouteStore(zkClientService)) { routeStore.store(s1, new RouteConfig(TEST_ROUTE_CONFIG)); Assert.assertEquals(TEST_ROUTE_CONFIG, routeStore.fetch(s1).getRoutes()); routeStore.delete(s1); Tasks.waitFor(true, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return routeStore.fetch(s1).getRoutes().isEmpty(); } }, 5L, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); } }
@Test public void testChangeConfig() throws Exception { ApplicationId appId = new ApplicationId("n1", "a1"); final ProgramId s1 = appId.service("testChangeConfig"); // Create two RouteStore, one for writing, one for reading try (RouteStore writingRouteStore = new ZKRouteStore(zkClientService)) { try (final RouteStore readingRouteStore = new ZKRouteStore(zkClientService)) { writingRouteStore.store(s1, new RouteConfig(TEST_ROUTE_CONFIG)); Assert.assertEquals(TEST_ROUTE_CONFIG, readingRouteStore.fetch(s1).getRoutes()); writingRouteStore.store(s1, new RouteConfig(TEST_ROUTE_CONFIG2)); Tasks.waitFor(TEST_ROUTE_CONFIG2, new Callable<Map<String, Integer>>() { @Override public Map<String, Integer> call() throws Exception { return readingRouteStore.fetch(s1).getRoutes(); } }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); } } } }