Search in sources :

Example 1 with ServiceInstances

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);
    }
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) ServiceInstances(co.cask.cdap.proto.ServiceInstances) ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with ServiceInstances

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());
}
Also used : ServiceInstances(co.cask.cdap.proto.ServiceInstances) HttpResponse(org.apache.http.HttpResponse) Id(co.cask.cdap.proto.Id) ProgramId(co.cask.cdap.proto.id.ProgramId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) StreamId(co.cask.cdap.proto.id.StreamId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) ConcurrencyConstraint(co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint) Test(org.junit.Test)

Aggregations

Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)2 ProtoConstraint (co.cask.cdap.proto.ProtoConstraint)2 ServiceInstances (co.cask.cdap.proto.ServiceInstances)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 ServiceSpecification (co.cask.cdap.api.service.ServiceSpecification)1 ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ConcurrencyConstraint (co.cask.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint)1 Id (co.cask.cdap.proto.Id)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 StreamId (co.cask.cdap.proto.id.StreamId)1 IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 HttpResponse (org.apache.http.HttpResponse)1 Test (org.junit.Test)1