Search in sources :

Example 1 with ServiceInstances

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

Example 2 with ServiceInstances

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

Aggregations

ServiceInstances (io.cdap.cdap.proto.ServiceInstances)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)1 Id (io.cdap.cdap.common.id.Id)1 ConcurrencyConstraint (io.cdap.cdap.internal.app.runtime.schedule.constraint.ConcurrencyConstraint)1 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)1 ProtoConstraint (io.cdap.cdap.proto.ProtoConstraint)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 ProfileId (io.cdap.cdap.proto.id.ProfileId)1 ServiceId (io.cdap.cdap.proto.id.ServiceId)1 HttpResponse (io.cdap.common.http.HttpResponse)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Test (org.junit.Test)1