Search in sources :

Example 6 with ServiceId

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.");
        }
    }
}
Also used : RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(co.cask.cdap.common.discovery.EndpointStrategy) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ServiceId(co.cask.cdap.proto.id.ServiceId) ProgramStatus(co.cask.cdap.proto.ProgramStatus) BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) RandomEndpointStrategy(co.cask.cdap.common.discovery.RandomEndpointStrategy) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with ServiceId

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);
    }
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) FlowletId(co.cask.cdap.proto.id.FlowletId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramId(co.cask.cdap.proto.id.ProgramId) ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 8 with ServiceId

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);
}
Also used : ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 9 with ServiceId

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);
}
Also used : ProgramIdArgument(co.cask.cdap.cli.ProgramIdArgument) ServiceId(co.cask.cdap.proto.id.ServiceId)

Example 10 with ServiceId

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);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ApplicationId(co.cask.cdap.proto.id.ApplicationId) File(java.io.File) URL(java.net.URL) ServiceId(co.cask.cdap.proto.id.ServiceId) Test(org.junit.Test)

Aggregations

ServiceId (co.cask.cdap.proto.id.ServiceId)17 Test (org.junit.Test)5 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ProgramIdArgument (co.cask.cdap.cli.ProgramIdArgument)2 CommandInputError (co.cask.cdap.cli.exception.CommandInputError)2 HttpResponse (co.cask.common.http.HttpResponse)2 URL (java.net.URL)2 ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 EndpointStrategy (co.cask.cdap.common.discovery.EndpointStrategy)1 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)1 BatchProgramStatus (co.cask.cdap.proto.BatchProgramStatus)1 ProgramStatus (co.cask.cdap.proto.ProgramStatus)1 FlowletId (co.cask.cdap.proto.id.FlowletId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 HttpMethod (co.cask.common.http.HttpMethod)1 HttpRequest (co.cask.common.http.HttpRequest)1 TypeToken (com.google.common.reflect.TypeToken)1 File (java.io.File)1