Search in sources :

Example 1 with Instances

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

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 2 with Instances

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

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 3 with Instances

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

the class MonitorClient method setSystemServiceInstances.

/**
 * Sets the number of instances the system service is running on.
 *
 * @param serviceName name of the system service
 * @param instances number of instances the system service is running on
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the system service with the specified name was not found
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void setSystemServiceInstances(String serviceName, int instances) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(new String(response.getResponseBody()));
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Instances(io.cdap.cdap.proto.Instances) SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL)

Example 4 with Instances

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

the class MonitorHandlerAuthorizationTest method testSetServiceInstanceAuthorization.

@Test
public void testSetServiceInstanceAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(StandardPermission.UPDATE));
    Instances instances = new Instances(1);
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "system/services/service/instances", Unpooled.copiedBuffer(GSON.toJson(instances), StandardCharsets.UTF_8));
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.setServiceInstance(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.setServiceInstance(request, responder, SERVICE_NAME);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) Instances(io.cdap.cdap.proto.Instances) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 5 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)7 HttpResponse (io.cdap.common.http.HttpResponse)4 NotFoundException (io.cdap.cdap.common.NotFoundException)3 ServiceInstances (io.cdap.cdap.proto.ServiceInstances)3 HttpRequest (io.cdap.common.http.HttpRequest)3 URL (java.net.URL)3 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)2 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)2 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)1 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)1 BatchRunnableInstances (io.cdap.cdap.proto.BatchRunnableInstances)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 ServiceId (io.cdap.cdap.proto.id.ServiceId)1 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)1