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);
}
}
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);
}
}
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();
}
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);
}
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;
}
}
Aggregations