use of io.cdap.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class AppFabricServerTest method startStopServer.
@Test
public void startStopServer() throws Exception {
Injector injector = AppFabricTestHelper.getInjector();
try {
AppFabricServer server = injector.getInstance(AppFabricServer.class);
DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
Service.State state = server.startAndWait();
Assert.assertSame(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.assertSame(state, Service.State.TERMINATED);
Tasks.waitFor(true, () -> endpointStrategy.pick() == null, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
} finally {
AppFabricTestHelper.shutdown();
}
}
use of io.cdap.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class AppFabricServerTest method testSSL.
@Test
public void testSSL() throws IOException {
CConfiguration cConf = CConfiguration.create();
cConf.setBoolean(Constants.Security.SSL.INTERNAL_ENABLED, true);
SConfiguration sConf = SConfiguration.create();
final Injector injector = AppFabricTestHelper.getInjector(cConf, sConf);
try {
final DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
AppFabricServer appFabricServer = injector.getInstance(AppFabricServer.class);
appFabricServer.startAndWait();
Assert.assertTrue(appFabricServer.isRunning());
Supplier<EndpointStrategy> endpointStrategySupplier = Suppliers.memoize(() -> new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.APP_FABRIC_HTTP)))::get;
Discoverable discoverable = endpointStrategySupplier.get().pick(3, TimeUnit.SECONDS);
Assert.assertNotNull(discoverable);
Assert.assertTrue(URIScheme.HTTPS.isMatch(discoverable));
InetSocketAddress addr = discoverable.getSocketAddress();
// Since the server uses a self signed certificate we need a client that trusts all certificates
SSLSocket socket = (SSLSocket) LDAPLoginModule.TrustAllSSLSocketFactory.getDefault().createSocket(addr.getHostName(), addr.getPort());
// in millis
socket.setSoTimeout(5000);
// Would throw exception if the server does not support ssl.
// "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
socket.startHandshake();
appFabricServer.stopAndWait();
} finally {
AppFabricTestHelper.shutdown();
}
}
use of io.cdap.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class DefaultSecureStoreServiceTest method waitForService.
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);
}
use of io.cdap.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class AppFabricTestBase method getClientConfig.
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);
ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(discoverable.getSocketAddress().getHostName()).setPort(discoverable.getSocketAddress().getPort()).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
return ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
use of io.cdap.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getServiceAvailability.
/**
* Return the availability (i.e. discoverable registration) status of a service.
*/
@GET
@Path("/apps/{app-name}/versions/{app-version}/{service-type}/{program-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-type") String serviceType, @PathParam("program-name") String programName) throws Exception {
// Currently we only support services and sparks as the service-type
ProgramType programType = getProgramType(serviceType);
if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) {
throw new BadRequestException("Only service or spark is support for service availability check");
}
ProgramId programId = new ProgramId(new ApplicationId(namespaceId, appName, appVersion), programType, programName);
ProgramStatus status = lifecycleService.getProgramStatus(programId);
if (status == ProgramStatus.STOPPED) {
throw new ServiceUnavailableException(programId.toString(), "Service is stopped. Please start it.");
}
// Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
String discoverableName = ServiceDiscoverable.getName(programId);
// TODO: CDAP-12959 - Should use the UserServiceEndpointStrategy and discover based on the version
// and have appVersion nullable for the non versioned endpoint
EndpointStrategy strategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(discoverableName));
if (strategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
LOG.trace("Discoverable endpoint {} not found", discoverableName);
throw new ServiceUnavailableException(programId.toString(), "Service is running but not accepting requests at this time.");
}
responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
Aggregations