private static String getZKPath(ProgramId serviceId) { return String.format("/routestore/%s", ServiceDiscoverable.getName(serviceId)); }
@Override public URL getServiceURL(long timeout, TimeUnit timeoutUnit) { return ServiceDiscoverable.createServiceBaseURL( new RandomEndpointStrategy(() -> discoveryServiceClient.discover(ServiceDiscoverable.getName(programId))) .pick(timeout, timeoutUnit), programId); } }
private boolean isUserServiceType(String uriPart) { for (ProgramType type : ServiceDiscoverable.getUserServiceTypes()) { if (type.getCategoryName().equals(uriPart)) { return true; } } return false; }
if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) { throw new BadRequestException("Only service or spark is support for service availability check"); 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)); } }
/** * Creates a base {@link URL} for calling user service based on the given {@link Discoverable}. * * @return a {@link URL} or {@code null} if the discoverable is {@code null} or failed to construct a URL. */ @Nullable public static URL createServiceBaseURL(@Nullable Discoverable discoverable, ProgramId programId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); boolean ssl = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()); return createServiceBaseURL(address.getHostName(), address.getPort(), ssl, programId); }
private EndpointStrategy discover(RouteDestination routeDestination) { LOG.debug("Looking up service name {}", routeDestination); // If its a user service, then use UserServiceEndpointStrategy Strategy String serviceName = routeDestination.getServiceName(); return ServiceDiscoverable.isUserService(serviceName) ? new UserServiceEndpointStrategy(() -> discoveryServiceClient.discover(serviceName), routeStore, ServiceDiscoverable.getId(serviceName), fallbackStrategy, routeDestination.getVersion()) : new RandomEndpointStrategy(() -> discoveryServiceClient.discover(serviceName)); } }
if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) { throw new BadRequestException("Only service or spark is support for service availability check"); String discoverableName = ServiceDiscoverable.getName(programId);
/** * Creates a base {@link URL} for calling user service based on the given {@link Discoverable}. * * @return a {@link URL} or {@code null} if the discoverable is {@code null} or failed to construct a URL. */ @Nullable public static URL createServiceBaseURL(@Nullable Discoverable discoverable, ProgramId programId) { if (discoverable == null) { return null; } InetSocketAddress address = discoverable.getSocketAddress(); boolean ssl = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()); return createServiceBaseURL(address.getHostName(), address.getPort(), ssl, programId); }
private static String getZKPath(ProgramId serviceId) { return String.format("/routestore/%s", ServiceDiscoverable.getName(serviceId)); }
@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) { try { Tasks.waitFor(true, () -> { try { checkAvailability(); return true; } catch (ServiceUnavailableException e) { return false; } }, timeout, timeoutUnit); ConnectionConfig connectionConfig = clientConfig.getConnectionConfig(); return ServiceDiscoverable.createServiceBaseURL(connectionConfig.getHostname(), connectionConfig.getPort(), connectionConfig.isSSLEnabled(), programId); } catch (TimeoutException e) { return null; } catch (Exception e) { LOG.warn("Exception raised when waiting for Spark service to be available", e); return null; } }
@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 URL getServiceURL(long timeout, TimeUnit timeoutUnit) { ServiceDiscovered discovered = discoveryServiceClient.discover(ServiceDiscoverable.getName(programId)); return ServiceDiscoverable.createServiceBaseURL(new RandomEndpointStrategy(discovered).pick(timeout, timeoutUnit), programId); }
@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 URL getServiceURL(long timeout, TimeUnit timeoutUnit) { ServiceDiscovered discovered = discoveryServiceClient.discover(ServiceDiscoverable.getName(programId)); return ServiceDiscoverable.createServiceBaseURL(new RandomEndpointStrategy(discovered).pick(timeout, timeoutUnit), programId); } }
@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)); }); }