@Override public ServiceManager getServiceManager(String serviceName) { ProgramId programId = application.service(serviceName); return new DefaultServiceManager(programId, appFabricClient, discoveryServiceClient, this, metricsManager); }
@Override public ServiceManager getServiceManager(String serviceName) { ProgramId programId = application.service(serviceName); return new DefaultServiceManager(programId, appFabricClient, discoveryServiceClient, this, metricsManager); }
@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())); }
private void deleteRouteConfig(ApplicationId appId, ApplicationSpecification appSpec) { for (ServiceSpecification serviceSpec : appSpec.getServices().values()) { ProgramId serviceId = appId.service(serviceSpec.getName()); try { routeStore.delete(serviceId); } catch (NotFoundException ex) { // expected if a config has not been stored for that service. } } }
private void deleteRouteConfig(ApplicationId appId, ApplicationSpecification appSpec) { for (ServiceSpecification serviceSpec : appSpec.getServices().values()) { ProgramId serviceId = appId.service(serviceSpec.getName()); try { routeStore.delete(serviceId); } catch (NotFoundException ex) { // expected if a config has not been stored for that service. } } }
public void programClient() throws Exception { // Construct the client used to interact with CDAP ProgramClient programClient = new ProgramClient(clientConfig); // Start a service in the WordCount example programClient.start(NamespaceId.DEFAULT.app("WordCount").service("RetrieveCounts")); // Fetch live information from the HelloWorld example // Live info includes the address of an component’s container host and the container’s debug port, // formatted in JSON programClient.getLiveInfo(NamespaceId.DEFAULT.app("HelloWorld").service("greet")); // Fetch program logs in the WordCount example programClient.getProgramLogs(NamespaceId.DEFAULT.app("WordCount").service("RetrieveCounts"), 0, Long.MAX_VALUE); // Scale a service in the HelloWorld example programClient.setServiceInstances(NamespaceId.DEFAULT.app("HelloWorld").service("greet"), 3); // Stop a service in the HelloWorld example programClient.stop(NamespaceId.DEFAULT.app("HelloWorld").service("greet")); }
public void serviceClient() throws Exception { // Construct the client used to interact with CDAP ServiceClient serviceClient = new ServiceClient(clientConfig); // Fetch service information using the service in the PurchaseApp example ServiceSpecification serviceSpec = serviceClient.get( NamespaceId.DEFAULT.app("PurchaseApp").service("CatalogLookup")); }
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN); ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID)); if (programIdArgument != null) { ServiceId service; if (arguments.get(APP_VERSION) == null) { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service( programIdArgument.getProgramId()); } else { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service( programIdArgument.getProgramId()); } completer.setEndpoints(getEndpoints(service, arguments.get(METHOD))); } else { completer.setEndpoints(Collections.<String>emptyList()); } return super.complete(buffer, cursor, candidates); }
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN); ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID)); if (programIdArgument != null) { ServiceId service; if (arguments.get(APP_VERSION) == null) { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service( programIdArgument.getProgramId()); } else { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service( programIdArgument.getProgramId()); } completer.setEndpoints(getMethods(service)); } else { completer.setEndpoints(Collections.<String>emptyList()); } return super.complete(buffer, cursor, candidates); }
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN); ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID)); if (programIdArgument != null) { ServiceId service; if (arguments.get(APP_VERSION) == null) { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service( programIdArgument.getProgramId()); } else { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service( programIdArgument.getProgramId()); } completer.setEndpoints(getEndpoints(service, arguments.get(METHOD))); } else { completer.setEndpoints(Collections.<String>emptyList()); } return super.complete(buffer, cursor, candidates); }
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN); ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID)); if (programIdArgument != null) { ServiceId service; if (arguments.get(APP_VERSION) == null) { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service( programIdArgument.getProgramId()); } else { service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service( programIdArgument.getProgramId()); } completer.setEndpoints(getMethods(service)); } else { completer.setEndpoints(Collections.<String>emptyList()); } return super.complete(buffer, cursor, candidates); }
@Test public void test() { ProgramId serviceId = new NamespaceId("test_ns").app("my_app").service("myservice"); String twillAppName = TwillAppNames.toTwillAppName(serviceId); Assert.assertEquals("service.test_ns.my_app.myservice", twillAppName); Assert.assertEquals(serviceId, TwillAppNames.fromTwillAppName(twillAppName)); // parsing from twill app name can be optional (return null) Assert.assertNull(TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, false)); try { // if passing true, throws exception, when parsing is not possible TwillAppNames.fromTwillAppName(Constants.Service.MASTER_SERVICES, true); Assert.fail("Expected not being able to parse "); } catch (IllegalArgumentException e) { // expected Assert.assertTrue(e.getMessage().contains("does not match pattern for programs")); } } }
@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 testProgramId() throws Exception { ProgramId serviceId = new ApplicationId("ns", "app").service("s1"); String discoverableName = ServiceDiscoverable.getName(serviceId); Assert.assertEquals("service.ns.app.s1", discoverableName); Assert.assertTrue(ServiceDiscoverable.isUserService(discoverableName)); Assert.assertFalse(ServiceDiscoverable.isUserService("service1.")); Assert.assertEquals(serviceId, ServiceDiscoverable.getId(discoverableName)); } }
@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 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 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 testGetProgramProfile() { ProfileId profileId = NamespaceId.DEFAULT.profile("p"); Map<String, String> args = Collections.singletonMap(SystemArguments.PROFILE_NAME, profileId.getScopedName()); ApplicationId appId = NamespaceId.DEFAULT.app("a"); ProgramId mrId = appId.mr("mr"); ProgramId serviceId = appId.service("serv"); ProgramId sparkId = appId.spark("spark"); ProgramId workerId = appId.worker("worker"); ProgramId workflowID = appId.workflow("wf"); Assert.assertEquals(profileId, SystemArguments.getProfileIdForProgram(mrId, args)); Assert.assertEquals(ProfileId.NATIVE, SystemArguments.getProfileIdForProgram(serviceId, args)); Assert.assertEquals(profileId, SystemArguments.getProfileIdForProgram(sparkId, args)); Assert.assertEquals(ProfileId.NATIVE, SystemArguments.getProfileIdForProgram(workerId, args)); Assert.assertEquals(profileId, SystemArguments.getProfileIdForProgram(workflowID, args)); }
@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); } } } }