use of io.cdap.cdap.proto.ServiceInstances in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getServiceInstances.
/**
* Return the number of instances of a service.
*/
@GET
@Path("/apps/{app-id}/services/{service-id}/instances")
public void getServiceInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
try {
ProgramId programId = validateAndGetNamespace(namespaceId).app(appId).service(serviceId);
lifecycleService.ensureProgramExists(programId);
int instances = store.getServiceInstances(programId);
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(new ServiceInstances(instances, getInstanceCount(programId, serviceId))));
} catch (SecurityException e) {
responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
}
}
use of io.cdap.cdap.proto.ServiceInstances in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testServices.
@Test
public void testServices() throws Exception {
deploy(AppWithServices.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
Id.Service service1 = Id.Service.from(Id.Namespace.from(TEST_NAMESPACE1), AppWithServices.NAME, AppWithServices.SERVICE_NAME);
final Id.Service service2 = Id.Service.from(Id.Namespace.from(TEST_NAMESPACE2), AppWithServices.NAME, AppWithServices.SERVICE_NAME);
HttpResponse activeResponse = getServiceAvailability(service1);
// Service is not valid, so it should return 404
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), activeResponse.getResponseCode());
activeResponse = getServiceAvailability(service2);
// Service has not been started, so it should return 503
Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE.code(), activeResponse.getResponseCode());
// start service in wrong namespace
startProgram(service1, 404);
startProgram(service2);
waitState(service2, RUNNING);
Tasks.waitFor(200, () -> getServiceAvailability(service2).getResponseCode(), 2, TimeUnit.SECONDS, 10, TimeUnit.MILLISECONDS);
// verify instances
try {
getServiceInstances(service1);
Assert.fail("Should not find service in " + TEST_NAMESPACE1);
} catch (AssertionError expected) {
// expected
}
ServiceInstances instances = getServiceInstances(service2);
Assert.assertEquals(1, instances.getRequested());
Assert.assertEquals(1, instances.getProvisioned());
// request 2 additional instances
int code = setServiceInstances(service1, 3);
Assert.assertEquals(404, code);
code = setServiceInstances(service2, 3);
Assert.assertEquals(200, code);
// verify that additional instances were provisioned
instances = getServiceInstances(service2);
Assert.assertEquals(3, instances.getRequested());
Assert.assertEquals(3, instances.getProvisioned());
// verify that endpoints are not available in the wrong namespace
HttpResponse response = callService(service1, HttpMethod.POST, "multi");
code = response.getResponseCode();
Assert.assertEquals(404, code);
response = callService(service1, HttpMethod.GET, "multi/ping");
code = response.getResponseCode();
Assert.assertEquals(404, code);
// stop service
stopProgram(service1, 404);
stopProgram(service2);
waitState(service2, STOPPED);
activeResponse = getServiceAvailability(service2);
// Service has been stopped, so it should return 503
Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE.code(), activeResponse.getResponseCode());
}
Aggregations