use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by caskdata.
the class AuthenticationServiceMainTest method testBasicAuthenticationEnabled.
@Test
public void testBasicAuthenticationEnabled() throws IOException {
HttpResponse response = HttpRequests.execute(HttpRequest.get(getAuthenticationBaseURI().toURL()).build(), new HttpRequestConfig(0, 0, false));
Assert.assertEquals("basic realm=\"null\"", response.getHeaders().get("WWW-Authenticate").stream().findFirst().orElse(null));
Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, response.getResponseCode());
Injector injector = getServiceMainInstance(AuthenticationServiceMain.class).getInjector();
DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
Discoverable authenticationEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Service.EXTERNAL_AUTHENTICATION)).pick(5, TimeUnit.SECONDS);
Assert.assertNotNull(authenticationEndpoint);
}
use of io.cdap.cdap.common.discovery.RandomEndpointStrategy in project cdap by caskdata.
the class LogsServiceMainTest method doGet.
private HttpResponse doGet(String path, DiscoveryServiceClient discoveryServiceClient, String serviceName) throws IOException {
Discoverable discoverable = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(serviceName)).pick(10, TimeUnit.SECONDS);
Assert.assertNotNull(discoverable);
URL url = URIScheme.createURI(discoverable, path).toURL();
return HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
}
use of io.cdap.cdap.common.discovery.RandomEndpointStrategy 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.");
}
use of io.cdap.cdap.common.discovery.RandomEndpointStrategy 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.RandomEndpointStrategy 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();
}
}
Aggregations