private static String getZKPath(ProgramId serviceId) { return String.format("/routestore/%s", ServiceDiscoverable.getName(serviceId)); }
private static String getZKPath(ProgramId serviceId) { return String.format("/routestore/%s", ServiceDiscoverable.getName(serviceId)); }
@Inject SparkServiceAnnouncer(CConfiguration cConf, ZKClient zKClient, ProgramId programId) { // Use the ZK path that points to the Twill application of the Spark client. String ns = String.format("%s/%s", cConf.get(Constants.CFG_TWILL_ZK_NAMESPACE), ServiceDiscoverable.getName(programId)); this.zkClient = ZKClients.namespace(zKClient, ns); }
@Inject SparkServiceAnnouncer(CConfiguration cConf, ZKClient zKClient, ProgramId programId) { // Use the ZK path that points to the Twill application of the Spark client. String ns = String.format("%s/%s", cConf.get(Constants.CFG_TWILL_ZK_NAMESPACE), ServiceDiscoverable.getName(programId)); this.zkClient = ZKClients.namespace(zKClient, ns); }
@Inject SparkServiceAnnouncer(CConfiguration cConf, ZKClient zKClient, ProgramId programId) { // Use the ZK path that points to the Twill application of the Spark client. String ns = String.format("%s/%s", cConf.get(Constants.CFG_TWILL_ZK_NAMESPACE), ServiceDiscoverable.getName(programId)); this.zkClient = ZKClients.namespace(zKClient, ns); }
@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Transactionals.execute(transactional, context -> { getRouteTable(context).write(ServiceDiscoverable.getName(serviceId), GSON.toJson(routeConfig.getRoutes())); }); }
@Override public void store(final ProgramId serviceId, final RouteConfig routeConfig) { Transactionals.execute(transactional, context -> { getRouteTable(context).write(ServiceDiscoverable.getName(serviceId), GSON.toJson(routeConfig.getRoutes())); }); }
@Override public RouteConfig fetch(final ProgramId serviceId) { return Transactionals.execute(transactional, context -> { byte[] value = getRouteTable(context).read(ServiceDiscoverable.getName(serviceId)); if (value == null) { return new RouteConfig(Collections.emptyMap()); } return new RouteConfig(GSON.fromJson(Bytes.toString(value), MAP_STRING_INTEGER_TYPE)); }); }
@Override public RouteConfig fetch(final ProgramId serviceId) { return Transactionals.execute(transactional, context -> { byte[] value = getRouteTable(context).read(ServiceDiscoverable.getName(serviceId)); if (value == null) { return new RouteConfig(Collections.emptyMap()); } return new RouteConfig(GSON.fromJson(Bytes.toString(value), MAP_STRING_INTEGER_TYPE)); }); }
@Override public URL getServiceURL(long timeout, TimeUnit timeoutUnit) { return ServiceDiscoverable.createServiceBaseURL( new RandomEndpointStrategy(() -> discoveryServiceClient.discover(ServiceDiscoverable.getName(programId))) .pick(timeout, timeoutUnit), programId); }
@Override public URL getServiceURL(long timeout, TimeUnit timeoutUnit) { ServiceDiscovered discovered = discoveryServiceClient.discover(ServiceDiscoverable.getName(programId)); return ServiceDiscoverable.createServiceBaseURL(new RandomEndpointStrategy(discovered).pick(timeout, timeoutUnit), programId); }
@Override public URL getServiceURL(long timeout, TimeUnit timeoutUnit) { return ServiceDiscoverable.createServiceBaseURL( new RandomEndpointStrategy(() -> discoveryServiceClient.discover(ServiceDiscoverable.getName(programId))) .pick(timeout, timeoutUnit), programId); } }
@Override public URL getServiceURL(long timeout, TimeUnit timeoutUnit) { ServiceDiscovered discovered = discoveryServiceClient.discover(ServiceDiscoverable.getName(programId)); return ServiceDiscoverable.createServiceBaseURL(new RandomEndpointStrategy(discovered).pick(timeout, timeoutUnit), programId); } }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { Transactionals.execute(transactional, context -> { byte[] key = Bytes.toBytes(ServiceDiscoverable.getName(serviceId)); KeyValueTable kvTable = getRouteTable(context); if (kvTable.read(key) == null) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } kvTable.delete(key); }, NotFoundException.class); }
public static String getName(ProgramId programId) { return getName(programId.getNamespace(), programId.getApplication(), programId.getType(), programId.getProgram()); }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { Transactionals.execute(transactional, context -> { byte[] key = Bytes.toBytes(ServiceDiscoverable.getName(serviceId)); KeyValueTable kvTable = getRouteTable(context); if (kvTable.read(key) == null) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } kvTable.delete(key); }, NotFoundException.class); }
public static String getName(ProgramId programId) { return getName(programId.getNamespace(), programId.getApplication(), programId.getType(), programId.getProgram()); }
/** * Starts the {@link NettyHttpService} and announces this runnable as well. */ @Override public void startUp() throws Exception { // All handlers of a Service run in the same Twill runnable and each Netty thread gets its own // instance of a handler (and handlerContext). Creating the logging context here ensures that the logs // during startup/shutdown and in each thread created are published. LoggingContextAccessor.setLoggingContext(getLoggingContext()); delegatorContexts.addAll(createDelegatorContexts()); service = createNettyHttpService(delegatorContexts); LOG.debug("Starting HTTP server for Service {}", program.getId()); ProgramId programId = program.getId(); service.start(); // announce the twill runnable InetSocketAddress bindAddress = service.getBindAddress(); int port = bindAddress.getPort(); // Announce the service with its version as the payload cancelDiscovery = serviceAnnouncer.announce(ServiceDiscoverable.getName(programId), port, Bytes.toBytes(programId.getVersion())); LOG.info("Announced HTTP Service for Service {} at {}", programId, bindAddress); // Create a Timer thread to periodically collect handler that are no longer in used and call destroy on it timer = new Timer("http-handler-gc", true); long cleanupPeriod = DEFAULT_HANDLER_CLEANUP_PERIOD_MILLIS; String cleanupPeriodProperty = System.getProperty(HANDLER_CLEANUP_PERIOD_MILLIS); if (cleanupPeriodProperty != null) { cleanupPeriod = Long.parseLong(cleanupPeriodProperty); } timer.scheduleAtFixedRate(createHandlerDestroyTask(), cleanupPeriod, cleanupPeriod); }
String discoverableName = ServiceDiscoverable.getName(programId);
@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)); } }