@Override public Boolean call() throws Exception { return endpointStrategy.pick() == null; } }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
@Override protected Discoverable pick(ServiceDiscovered serviceDiscovered) { Discoverable lastPick = this.lastPick; if (lastPick == null || !isValid(lastPick, serviceDiscovered)) { this.lastPick = lastPick = picker.pick(); } return lastPick; }
@Override public Discoverable pick() { Discoverable lastPick = this.lastPick; if (lastPick == null || !isValid(lastPick)) { this.lastPick = lastPick = picker.pick(); } return lastPick; }
@Override protected InetSocketAddress getExploreServiceAddress() { Discoverable discoverable = endpointStrategySupplier.get().pick(3L, TimeUnit.SECONDS); if (discoverable != null) { return discoverable.getSocketAddress(); } throw new ServiceUnavailableException(Service.EXPLORE_HTTP_USER_SERVICE); }
@Override protected InetSocketAddress getExploreServiceAddress() { Discoverable discoverable = endpointStrategySupplier.get().pick(3L, TimeUnit.SECONDS); if (discoverable != null) { return discoverable.getSocketAddress(); } throw new ServiceUnavailableException(Service.EXPLORE_HTTP_USER_SERVICE); }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
/** * Discover the service address, then append the base path and specified resource to get the URL. * * @param resource the resource to use * @return the resolved URL * @throws ServiceUnavailableException if the service could not be discovered */ public URL resolve(String resource) { Discoverable discoverable = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS); if (discoverable == null) { throw new ServiceUnavailableException(discoverableServiceName); } InetSocketAddress address = discoverable.getSocketAddress(); String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME; String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource); try { return new URL(urlStr); } catch (MalformedURLException e) { // shouldn't happen. If it does, it means there is some bug in the service announcer throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, urlStr), e); } }
private URL resolve(String path) throws MalformedURLException { @SuppressWarnings("ConstantConditions") InetSocketAddress socketAddress = endpointStrategy.pick(1, TimeUnit.SECONDS).getSocketAddress(); return new URL(String.format("http://%s:%d%s%s", socketAddress.getHostName(), socketAddress.getPort(), Constants.Gateway.API_VERSION_3, path)); }
/** * generic method to discover a thrift service and start up the * thrift transport and protocol layer. */ public static TProtocol getThriftProtocol(String serviceName, EndpointStrategy endpointStrategy) throws ServerException { Discoverable endpoint = endpointStrategy.pick(); if (endpoint == null) { String message = String.format("Service '%s' is not registered in discovery service.", serviceName); LOG.error(message); throw new ServerException(message); } TTransport transport = new TFramedTransport( new TSocket(endpoint.getSocketAddress().getHostName(), endpoint.getSocketAddress().getPort())); try { transport.open(); } catch (TTransportException e) { String message = String.format("Unable to connect to thrift service %s at %s. Reason: %s", serviceName, endpoint.getSocketAddress(), e.getMessage()); LOG.error(message); throw new ServerException(message, e); } // now try to connect the thrift client return new TBinaryProtocol(transport); } }
/** * generic method to discover a thrift service and start up the * thrift transport and protocol layer. */ public static TProtocol getThriftProtocol(String serviceName, EndpointStrategy endpointStrategy) throws ServerException { Discoverable endpoint = endpointStrategy.pick(); if (endpoint == null) { String message = String.format("Service '%s' is not registered in discovery service.", serviceName); LOG.error(message); throw new ServerException(message); } TTransport transport = new TFramedTransport( new TSocket(endpoint.getSocketAddress().getHostName(), endpoint.getSocketAddress().getPort())); try { transport.open(); } catch (TTransportException e) { String message = String.format("Unable to connect to thrift service %s at %s. Reason: %s", serviceName, endpoint.getSocketAddress(), e.getMessage()); LOG.error(message); throw new ServerException(message, e); } // now try to connect the thrift client return new TBinaryProtocol(transport); } }
/** * Finds the {@link Discoverable} for the given {@link HttpRequest} to route to. */ private Discoverable getDiscoverable(HttpRequest httpRequest) { EndpointStrategy strategy = serviceLookup.getDiscoverable(httpRequest); if (strategy == null) { throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No endpoint strategy found for request " + getRequestLine(httpRequest)); } // Do a non-blocking pick first. If the service has been discovered before, this should return an endpoint // immediately. Discoverable discoverable = strategy.pick(); if (discoverable != null) { return discoverable; } // Do a blocking pick for up to 1 second. It is for the case where a service is being discovered for the first time, // in which population of the cache might take time. discoverable = strategy.pick(1, TimeUnit.SECONDS); if (discoverable == null) { throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No discoverable found for request " + getRequestLine(httpRequest)); } return discoverable; }
private static void waitForService(String name) { EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryService.discover(name)); Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", name); }
private static void waitForService(DiscoveryServiceClient discoveryService, String discoverableName) throws InterruptedException { EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryService.discover(discoverableName)); Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", discoverableName); }
private static void waitForService(String service) { EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(service)); Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", service); }
private static void waitForService(String service) { EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(service)); Preconditions.checkNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS), "%s service is not up after 5 seconds", service); }
if (endpointStrategy.pick(300L, TimeUnit.MILLISECONDS) == null) { LOG.trace("Discoverable endpoint {} not found", discoverableName); throw new ServiceUnavailableException(programId.toString(),
if (strategy.pick(300L, TimeUnit.MILLISECONDS) == null) { LOG.trace("Discoverable endpoint {} not found", discoverableName); throw new ServiceUnavailableException(programId.toString(),
@BeforeClass public static void setup() { artifactRepository = getInjector().getInstance(ArtifactRepository.class); systemArtifactsDir = getInjector().getInstance(CConfiguration.class).get(Constants.AppFabric.SYSTEM_ARTIFACTS_DIR); DiscoveryServiceClient discoveryClient = getInjector().getInstance(DiscoveryServiceClient.class); EndpointStrategy endpointStrategy = new RandomEndpointStrategy( () -> discoveryClient.discover(Constants.Service.METADATA_SERVICE)); Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS); Assert.assertNotNull(discoverable); String host = discoverable.getSocketAddress().getHostName(); int port = discoverable.getSocketAddress().getPort(); ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(host).setPort(port).build(); ClientConfig clientConfig = ClientConfig.builder().setConnectionConfig(connectionConfig).build(); metadataClient = new MetadataClient(clientConfig); }
@Test public void startStopServer() throws Exception { Injector injector = AppFabricTestHelper.getInjector(); AppFabricServer server = injector.getInstance(AppFabricServer.class); DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class); Service.State state = server.startAndWait(); Assert.assertTrue(state == Service.State.RUNNING); final EndpointStrategy endpointStrategy = new RandomEndpointStrategy( () -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP)); Assert.assertNotNull(endpointStrategy.pick(5, TimeUnit.SECONDS)); state = server.stopAndWait(); Assert.assertTrue(state == Service.State.TERMINATED); Tasks.waitFor(true, new Callable<Boolean>() { @Override public Boolean call() throws Exception { return endpointStrategy.pick() == null; } }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); }
private static ClientConfig getClientConfig(DiscoveryServiceClient discoveryClient, String service) { EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(service)); Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS); Assert.assertNotNull(discoverable); int port = discoverable.getSocketAddress().getPort(); ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(hostname).setPort(port).build(); return ClientConfig.builder().setConnectionConfig(connectionConfig).build(); }