Search in sources :

Example 1 with ServiceSpecification

use of co.cask.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ServiceSpecificationCodec method decodeOldSpec.

private ServiceSpecification decodeOldSpec(JsonObject json) {
    String className = json.get("classname").getAsString();
    TwillSpecification twillSpec = twillSpecificationAdapter.fromJson(json.get("spec").getAsString()).getTwillSpecification();
    Map<String, HttpServiceHandlerSpecification> handlers = Maps.newHashMap();
    RuntimeSpecification handlerSpec = twillSpec.getRunnables().get(twillSpec.getName());
    Map<String, String> configs = handlerSpec.getRunnableSpecification().getConfigs();
    // Get the class names of all handlers. It is stored in the handler runnable spec configs
    List<String> handlerClasses = GSON.fromJson(configs.get("service.runnable.handlers"), new TypeToken<List<String>>() {
    }.getType());
    List<JsonObject> handlerSpecs = GSON.fromJson(configs.get("service.runnable.handler.spec"), new TypeToken<List<JsonObject>>() {
    }.getType());
    for (int i = 0; i < handlerClasses.size(); i++) {
        String handlerClass = handlerClasses.get(i);
        JsonObject spec = handlerSpecs.get(i);
        Map<String, String> properties = GSON.fromJson(spec.get("properties"), new TypeToken<Map<String, String>>() {
        }.getType());
        // Reconstruct the HttpServiceSpecification. However there is no way to determine the datasets or endpoints
        // as it is not recorded in old spec. It's ok since the spec is only used to load data from MDS during redeploy.
        handlers.put(spec.get("name").getAsString(), new HttpServiceHandlerSpecification(handlerClass, spec.get("name").getAsString(), spec.get("description").getAsString(), properties, ImmutableSet.<String>of(), ImmutableList.<ServiceHttpEndpoint>of()));
    }
    ResourceSpecification resourceSpec = handlerSpec.getResourceSpecification();
    return new ServiceSpecification(className, twillSpec.getName(), twillSpec.getName(), handlers, new Resources(resourceSpec.getMemorySize(), resourceSpec.getVirtualCores()), resourceSpec.getInstances());
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) ResourceSpecification(org.apache.twill.api.ResourceSpecification) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillSpecification(org.apache.twill.api.TwillSpecification) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) TypeToken(com.google.common.reflect.TypeToken) Resources(co.cask.cdap.api.Resources)

Example 2 with ServiceSpecification

use of co.cask.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ServiceSpecificationCodec method deserialize.

@Override
public ServiceSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = (JsonObject) json;
    if (isOldSpec(jsonObj)) {
        return decodeOldSpec(jsonObj);
    }
    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, HttpServiceHandlerSpecification> handlers = deserializeMap(jsonObj.get("handlers"), context, HttpServiceHandlerSpecification.class);
    Resources resources = context.deserialize(jsonObj.get("resources"), Resources.class);
    int instances = jsonObj.get("instances").getAsInt();
    return new ServiceSpecification(className, name, description, handlers, resources, instances);
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) Resources(co.cask.cdap.api.Resources) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint)

Example 3 with ServiceSpecification

use of co.cask.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class DistributedServiceProgramRunner method setupLaunchConfig.

@Override
protected void setupLaunchConfig(LaunchConfig launchConfig, Program program, ProgramOptions options, CConfiguration cConf, Configuration hConf, File tempDir) {
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    ServiceSpecification serviceSpec = appSpec.getServices().get(program.getName());
    // Add a runnable for the service handler
    Resources resources = SystemArguments.getResources(options.getUserArguments(), serviceSpec.getResources());
    launchConfig.addRunnable(serviceSpec.getName(), new ServiceTwillRunnable(serviceSpec.getName()), resources, serviceSpec.getInstances());
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) Resources(co.cask.cdap.api.Resources)

Example 4 with ServiceSpecification

use of co.cask.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class GenerateClientUsageExample method serviceClient.

public void serviceClient() throws Exception {
    // Construct the client used to interact with CDAP
    ServiceClient serviceClient = new ServiceClient(clientConfig);
    // Fetch service information using the service in the PurchaseApp example
    ServiceSpecification serviceSpec = serviceClient.get(NamespaceId.DEFAULT.app("PurchaseApp").service("CatalogLookup"));
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) ServiceClient(co.cask.cdap.client.ServiceClient)

Example 5 with ServiceSpecification

use of co.cask.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getServiceInstances.

/**
   * Return the number of instances of a service.
   */
@GET
@Path("/apps/{app-id}/services/{service-id}/instances")
public void getServiceInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
    try {
        ProgramId programId = new ProgramId(namespaceId, appId, ProgramType.SERVICE, serviceId);
        if (!store.programExists(programId)) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Service not found");
            return;
        }
        ServiceSpecification specification = (ServiceSpecification) lifecycleService.getProgramSpecification(programId);
        if (specification == null) {
            responder.sendStatus(HttpResponseStatus.NOT_FOUND);
            return;
        }
        int instances = specification.getInstances();
        responder.sendJson(HttpResponseStatus.OK, new ServiceInstances(instances, getInstanceCount(programId, serviceId)));
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    }
}
Also used : ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) ServiceInstances(co.cask.cdap.proto.ServiceInstances) ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ServiceSpecification (co.cask.cdap.api.service.ServiceSpecification)17 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)7 HttpServiceHandlerSpecification (co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)5 ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Resources (co.cask.cdap.api.Resources)3 ProgramType (co.cask.cdap.proto.ProgramType)3 JsonObject (com.google.gson.JsonObject)3 Test (org.junit.Test)3 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)2 MapReduceSpecification (co.cask.cdap.api.mapreduce.MapReduceSpecification)2 SparkSpecification (co.cask.cdap.api.spark.SparkSpecification)2 ApplicationId (co.cask.cdap.proto.id.ApplicationId)2 RunId (org.apache.twill.api.RunId)2 AppWithServices (co.cask.cdap.AppWithServices)1 TxRunnable (co.cask.cdap.api.TxRunnable)1 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)1 DatasetContext (co.cask.cdap.api.data.DatasetContext)1 StreamSpecification (co.cask.cdap.api.data.stream.StreamSpecification)1 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1