Search in sources :

Example 6 with Instances

use of io.cdap.cdap.proto.Instances in project cdap by cdapio.

the class ProgramClient method setWorkerInstances.

/**
 * Sets the number of instances that a worker will run on.
 *
 * @param instances number of instances for the worker to run on
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the application or worker could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void setWorkerInstances(ProgramId worker, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(worker.getNamespaceId(), String.format("apps/%s/workers/%s/instances", worker.getApplication(), worker.getProgram()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(worker);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Instances(io.cdap.cdap.proto.Instances) HttpResponse(io.cdap.common.http.HttpResponse) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 7 with Instances

use of io.cdap.cdap.proto.Instances in project cdap by cdapio.

the class ProgramClient method setServiceInstances.

/**
 * Sets the number of instances of a service.
 *
 * @param service the service
 * @param instances number of instances for the service
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the application or service could not be found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void setServiceInstances(ServiceId service, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(service.getNamespaceId(), String.format("apps/%s/services/%s/instances", service.getApplication(), service.getProgram()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(service);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Instances(io.cdap.cdap.proto.Instances) HttpResponse(io.cdap.common.http.HttpResponse) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 8 with Instances

use of io.cdap.cdap.proto.Instances in project cdap by cdapio.

the class ProgramLifecycleHttpHandlerTest method setServiceInstances.

private int setServiceInstances(Id.Service serviceId, int instances) throws Exception {
    String instanceUrl = String.format("apps/%s/services/%s/instances", serviceId.getApplicationId(), serviceId.getId());
    String versionedInstanceUrl = getVersionedAPIPath(instanceUrl, Constants.Gateway.API_VERSION_3_TOKEN, serviceId.getNamespaceId());
    String instancesBody = GSON.toJson(new Instances(instances));
    return doPut(versionedInstanceUrl, instancesBody).getResponseCode();
}
Also used : Instances(io.cdap.cdap.proto.Instances) ServiceInstances(io.cdap.cdap.proto.ServiceInstances)

Example 9 with Instances

use of io.cdap.cdap.proto.Instances in project cdap by cdapio.

the class ProgramLifecycleHttpHandlerTest method testBatchInstances.

@Test
public void testBatchInstances() throws Exception {
    String instancesUrl1 = getVersionedAPIPath("instances", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
    Assert.assertEquals(400, doPost(instancesUrl1, "").getResponseCode());
    // empty array is valid args
    Assert.assertEquals(200, doPost(instancesUrl1, "[]").getResponseCode());
    deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
    Gson gson = new Gson();
    // data requires appId, programId, and programType. Test missing fields/invalid programType
    List<Map<String, String>> request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service"));
    Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
    request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programId", AllProgramsApp.NoOpService.NAME));
    Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
    request = Arrays.asList(ImmutableMap.of("programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
    Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
    request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "XXX", "programId", AllProgramsApp.NoOpService.NAME));
    Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request)).getResponseCode());
    // Test malformed json
    request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
    Assert.assertEquals(400, doPost(instancesUrl1, gson.toJson(request) + "]]").getResponseCode());
    // Test missing app
    request = Collections.singletonList(ImmutableMap.of("appId", "NotExist", "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
    List<JsonObject> returnedBody = readResponse(doPost(instancesUrl1, gson.toJson(request)), LIST_OF_JSONOBJECT_TYPE);
    Assert.assertEquals(404, returnedBody.get(0).get("statusCode").getAsInt());
    // valid test
    request = Arrays.asList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME), ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Worker", "programId", AllProgramsApp.NoOpWorker.NAME));
    HttpResponse response = doPost(instancesUrl1, gson.toJson(request));
    verifyInitialBatchInstanceOutput(response);
    // start the service
    ServiceId serviceId = new ServiceId(TEST_NAMESPACE1, AllProgramsApp.NAME, AllProgramsApp.NoOpService.NAME);
    startProgram(serviceId);
    waitState(serviceId, RUNNING);
    request = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
    response = doPost(instancesUrl1, gson.toJson(request));
    returnedBody = readResponse(response, LIST_OF_JSONOBJECT_TYPE);
    Assert.assertEquals(1, returnedBody.get(0).get("provisioned").getAsInt());
    // Increase service instances to 2
    String setInstanceUrl = getVersionedAPIPath(String.format("apps/%s/services/%s/instances", AllProgramsApp.NAME, AllProgramsApp.NoOpService.NAME), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
    Assert.assertEquals(200, doPut(setInstanceUrl, gson.toJson(new Instances(2))).getResponseCode());
    // Validate there are 2 instances
    Tasks.waitFor(2, () -> {
        List<Map<String, String>> request1 = Collections.singletonList(ImmutableMap.of("appId", AllProgramsApp.NAME, "programType", "Service", "programId", AllProgramsApp.NoOpService.NAME));
        HttpResponse response1 = doPost(instancesUrl1, gson.toJson(request1));
        List<JsonObject> returnedBody1 = readResponse(response1, LIST_OF_JSONOBJECT_TYPE);
        return returnedBody1.get(0).get("provisioned").getAsInt();
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    stopProgram(serviceId);
    waitState(serviceId, STOPPED);
}
Also used : Instances(io.cdap.cdap.proto.Instances) ServiceInstances(io.cdap.cdap.proto.ServiceInstances) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) HttpResponse(io.cdap.common.http.HttpResponse) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Example 10 with Instances

use of io.cdap.cdap.proto.Instances in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getWorkerInstances.

/**
 * Returns number of instances of a worker.
 */
@GET
@Path("/apps/{app-id}/workers/{worker-id}/instances")
public void getWorkerInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("worker-id") String workerId) throws Exception {
    try {
        ProgramId programId = validateAndGetNamespace(namespaceId).app(appId).worker(workerId);
        lifecycleService.ensureProgramExists(programId);
        int count = store.getWorkerInstances(programId);
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(new Instances(count)));
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (Throwable e) {
        if (respondIfElementNotFound(e, responder)) {
            return;
        }
        throw e;
    }
}
Also used : BatchRunnableInstances(io.cdap.cdap.proto.BatchRunnableInstances) ServiceInstances(io.cdap.cdap.proto.ServiceInstances) Instances(io.cdap.cdap.proto.Instances) 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)

Aggregations

Instances (io.cdap.cdap.proto.Instances)14 HttpResponse (io.cdap.common.http.HttpResponse)8 NotFoundException (io.cdap.cdap.common.NotFoundException)6 ServiceInstances (io.cdap.cdap.proto.ServiceInstances)6 HttpRequest (io.cdap.common.http.HttpRequest)6 URL (java.net.URL)6 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)4 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)4 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)4 Test (org.junit.Test)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)2 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)2 BatchRunnableInstances (io.cdap.cdap.proto.BatchRunnableInstances)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 ServiceId (io.cdap.cdap.proto.id.ServiceId)2 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)2