use of co.cask.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 = new ProgramId(namespaceId, appId, ProgramType.SERVICE, serviceId);
if (!store.programExists(programId)) {
responder.sendString(HttpResponseStatus.NOT_FOUND, "Service not found");
return;
}
ServiceSpecification specification = (ServiceSpecification) lifecycleService.getProgramSpecification(programId);
if (specification == null) {
responder.sendStatus(HttpResponseStatus.NOT_FOUND);
return;
}
int instances = specification.getInstances();
responder.sendJson(HttpResponseStatus.OK, new ServiceInstances(instances, getInstanceCount(programId, serviceId)));
} catch (SecurityException e) {
responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
}
}
use of co.cask.cdap.proto.ServiceInstances in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testServices.
@Test
public void testServices() throws Exception {
HttpResponse response = deploy(AppWithServices.class, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Id.Service service1 = Id.Service.from(Id.Namespace.from(TEST_NAMESPACE1), APP_WITH_SERVICES_APP_ID, APP_WITH_SERVICES_SERVICE_NAME);
final Id.Service service2 = Id.Service.from(Id.Namespace.from(TEST_NAMESPACE2), APP_WITH_SERVICES_APP_ID, APP_WITH_SERVICES_SERVICE_NAME);
HttpResponse activeResponse = getServiceAvailability(service1);
// Service is not valid, so it should return 404
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.getCode(), activeResponse.getStatusLine().getStatusCode());
activeResponse = getServiceAvailability(service2);
// Service has not been started, so it should return 503
Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE.getCode(), activeResponse.getStatusLine().getStatusCode());
// start service in wrong namespace
startProgram(service1, 404);
startProgram(service2);
Tasks.waitFor(200, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return getServiceAvailability(service2).getStatusLine().getStatusCode();
}
}, 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
response = callService(service1, HttpMethod.POST, "multi");
code = response.getStatusLine().getStatusCode();
Assert.assertEquals(404, code);
response = callService(service1, HttpMethod.GET, "multi/ping");
code = response.getStatusLine().getStatusCode();
Assert.assertEquals(404, code);
// stop service
stopProgram(service1, 404);
stopProgram(service2);
activeResponse = getServiceAvailability(service2);
// Service has been stopped, so it should return 503
Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE.getCode(), activeResponse.getStatusLine().getStatusCode());
}
Aggregations