use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getServiceAvailability.
/**
* Return the availability (i.e. discoverable registration) status of a service.
*/
@GET
@Path("/apps/{app-name}/versions/{app-version}/services/{service-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-name") String serviceName) throws Exception {
ServiceId serviceId = new ApplicationId(namespaceId, appName, appVersion).service(serviceName);
ProgramStatus status = lifecycleService.getProgramStatus(serviceId);
if (status == ProgramStatus.STOPPED) {
responder.sendString(HttpResponseStatus.SERVICE_UNAVAILABLE, "Service is stopped. Please start it.");
} else {
// Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
String serviceDiscoverableName = ServiceDiscoverable.getName(serviceId);
EndpointStrategy endpointStrategy = new RandomEndpointStrategy(discoveryServiceClient.discover(serviceDiscoverableName));
if (endpointStrategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
LOG.trace("Discoverable endpoint {} not found", serviceDiscoverableName);
responder.sendString(HttpResponseStatus.SERVICE_UNAVAILABLE, "Service is running but not accepting requests at this time.");
} else {
responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
}
}
use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.
the class SetProgramInstancesCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
ApplicationId appId = cliConfig.getCurrentNamespace().app(programIdParts[0]);
int numInstances = arguments.getInt(ArgumentName.NUM_INSTANCES.toString());
switch(elementType) {
case FLOWLET:
if (programIdParts.length < 3) {
throw new CommandInputError(this);
}
String flowId = programIdParts[1];
String flowletName = programIdParts[2];
FlowletId flowletId = appId.flow(flowId).flowlet(flowletName);
programClient.setFlowletInstances(flowletId, numInstances);
output.printf("Successfully set flowlet '%s' of flow '%s' of app '%s' to %d instances\n", flowId, flowletId, appId.getEntityName(), numInstances);
break;
case WORKER:
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String workerName = programIdParts[1];
ProgramId workerId = appId.worker(workerName);
programClient.setWorkerInstances(workerId, numInstances);
output.printf("Successfully set worker '%s' of app '%s' to %d instances\n", workerName, appId.getEntityName(), numInstances);
break;
case SERVICE:
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String serviceName = programIdParts[1];
ServiceId service = appId.service(serviceName);
programClient.setServiceInstances(service, numInstances);
output.printf("Successfully set service '%s' of app '%s' to %d instances\n", serviceName, appId.getEntityName(), numInstances);
break;
default:
// TODO: remove this
throw new IllegalArgumentException("Unrecognized program element type for scaling: " + elementType);
}
}
use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.
the class DeleteRouteConfigCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
ServiceId serviceId = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
String appName = serviceId.getApplication();
String serviceName = serviceId.getProgram();
serviceClient.deleteRouteConfig(serviceId);
output.printf("Successfully deleted route configuration of %s '%s' of application '%s'\n", ElementType.SERVICE.getName(), serviceName, appName);
}
use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.
the class HttpMethodPrefixCompleter method complete.
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN);
ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(SERVICE_ID));
if (programIdArgument != null) {
ServiceId service;
if (arguments.get(APP_VERSION) == null) {
service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId()).service(programIdArgument.getProgramId());
} else {
service = cliConfig.getCurrentNamespace().app(programIdArgument.getAppId(), arguments.get(APP_VERSION)).service(programIdArgument.getProgramId());
}
completer.setEndpoints(getMethods(service));
} else {
completer.setEndpoints(Collections.<String>emptyList());
}
return super.complete(buffer, cursor, candidates);
}
use of co.cask.cdap.proto.id.ServiceId in project cdap by caskdata.
the class PreferencesClientTestRun method testProgramAPI.
@Test
public void testProgramAPI() throws Exception {
Map<String, String> propMap = Maps.newHashMap();
propMap.put("key", "instance");
File jarFile = createAppJarFile(AppReturnsArgs.class);
appClient.deploy(NamespaceId.DEFAULT, jarFile);
ApplicationId app = NamespaceId.DEFAULT.app(AppReturnsArgs.NAME);
ServiceId service = app.service(AppReturnsArgs.SERVICE);
try {
client.setInstancePreferences(propMap);
Map<String, String> setMap = Maps.newHashMap();
setMap.put("saved", "args");
programClient.setRuntimeArgs(service, setMap);
assertEquals(setMap, programClient.getRuntimeArgs(service));
propMap.put("run", "value");
propMap.put("logical.start.time", "1234567890000");
propMap.putAll(setMap);
programClient.start(service, false, propMap);
assertProgramRunning(programClient, service);
URL serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
HttpResponse response = getServiceResponse(serviceURL);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
Map<String, String> responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
assertEquals(propMap, responseMap);
programClient.stop(service);
assertProgramStopped(programClient, service);
long minStartTime = System.currentTimeMillis();
client.deleteInstancePreferences();
programClient.start(service);
assertProgramRunning(programClient, service);
propMap.remove("key");
propMap.remove("run");
propMap.remove("logical.start.time");
serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
response = getServiceResponse(serviceURL);
responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
long actualStartTime = Long.parseLong(responseMap.remove("logical.start.time"));
Assert.assertTrue(actualStartTime >= minStartTime);
assertEquals(propMap, responseMap);
programClient.stop(service);
assertProgramStopped(programClient, service);
propMap.clear();
minStartTime = System.currentTimeMillis();
programClient.setRuntimeArgs(service, propMap);
programClient.start(service);
assertProgramRunning(programClient, service);
serviceURL = new URL(serviceClient.getServiceURL(service), AppReturnsArgs.ENDPOINT);
response = getServiceResponse(serviceURL);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
responseMap = GSON.fromJson(response.getResponseBodyAsString(), STRING_MAP_TYPE);
actualStartTime = Long.parseLong(responseMap.remove("logical.start.time"));
Assert.assertTrue(actualStartTime >= minStartTime);
assertEquals(propMap, responseMap);
} finally {
programClient.stop(service);
assertProgramStopped(programClient, service);
appClient.delete(app);
}
}
Aggregations