Search in sources :

Example 6 with ServiceSpecification

use of io.cdap.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, Collections.emptySet(), Collections.emptyList()));
    }
    ResourceSpecification resourceSpec = handlerSpec.getResourceSpecification();
    return new ServiceSpecification(className, twillSpec.getName(), twillSpec.getName(), handlers, new Resources(resourceSpec.getMemorySize(), resourceSpec.getVirtualCores()), resourceSpec.getInstances(), Collections.emptyMap());
}
Also used : ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) ResourceSpecification(org.apache.twill.api.ResourceSpecification) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillSpecification(org.apache.twill.api.TwillSpecification) TypeToken(com.google.common.reflect.TypeToken) Resources(io.cdap.cdap.api.Resources)

Example 7 with ServiceSpecification

use of io.cdap.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, Plugin> plugins = deserializeMap(jsonObj.get("plugins"), context, Plugin.class);
    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();
    Map<String, String> properties = deserializeMap(jsonObj.get("properties"), context, String.class);
    return new ServiceSpecification(className, name, description, handlers, resources, instances, plugins, properties);
}
Also used : ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) Resources(io.cdap.cdap.api.Resources) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 8 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by caskdata.

the class ServiceProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    int instanceId = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCE_ID, "-1"));
    Preconditions.checkArgument(instanceId >= 0, "Missing instance Id");
    int instanceCount = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCES, "0"));
    Preconditions.checkArgument(instanceCount > 0, "Invalid or missing instance count");
    RunId runId = ProgramRunners.getRunId(options);
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    ProgramType programType = program.getType();
    Preconditions.checkNotNull(programType, "Missing processor type.");
    Preconditions.checkArgument(programType == ProgramType.SERVICE, "Only Service process type is supported.");
    ServiceSpecification spec = appSpec.getServices().get(program.getName());
    String host = options.getArguments().getOption(ProgramOptionConstants.HOST);
    Preconditions.checkArgument(host != null, "No hostname is provided");
    // Setup dataset framework context, if required
    if (datasetFramework instanceof ProgramContextAware) {
        ProgramId programId = program.getId();
        ((ProgramContextAware) datasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
    }
    final PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
    try {
        RetryStrategy retryStrategy = SystemArguments.getRetryStrategy(options.getUserArguments().asMap(), program.getType(), cConf);
        ArtifactManager artifactManager = artifactManagerFactory.create(program.getId().getNamespaceId(), retryStrategy);
        ServiceHttpServer component = new ServiceHttpServer(host, program, options, cConf, spec, instanceId, instanceCount, serviceAnnouncer, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, pluginInstantiator, secureStore, secureStoreManager, messagingService, artifactManager, metadataReader, metadataPublisher, namespaceQueryAdmin, pluginFinder, fieldLineageWriter, transactionRunner, preferencesFetcher, remoteClientFactory, contextAccessEnforcer);
        // Add a service listener to make sure the plugin instantiator is closed when the http server is finished.
        component.addListener(createRuntimeServiceListener(Collections.singleton(pluginInstantiator)), Threads.SAME_THREAD_EXECUTOR);
        ProgramController controller = new ServiceProgramControllerAdapter(component, program.getId().run(runId));
        component.start();
        return controller;
    } catch (Throwable t) {
        Closeables.closeQuietly(pluginInstantiator);
        throw t;
    }
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) ProgramController(io.cdap.cdap.app.runtime.ProgramController) ProgramId(io.cdap.cdap.proto.id.ProgramId) BasicProgramContext(io.cdap.cdap.internal.app.runtime.BasicProgramContext) ServiceHttpServer(io.cdap.cdap.internal.app.services.ServiceHttpServer) ArtifactManager(io.cdap.cdap.api.artifact.ArtifactManager) PluginInstantiator(io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator) ProgramType(io.cdap.cdap.proto.ProgramType) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) RunId(org.apache.twill.api.RunId) ProgramContextAware(io.cdap.cdap.data.ProgramContextAware) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy)

Example 9 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by cdapio.

the class DefaultAppConfigurer method addService.

@Override
public void addService(Service service) {
    Preconditions.checkArgument(service != null, "Service cannot be null.");
    // check that a system service is only used in system namespace
    if (!deployNamespace.equals(Id.Namespace.fromEntityId(NamespaceId.SYSTEM))) {
        TypeToken<?> type = TypeToken.of(service.getClass()).resolveType(Service.class.getTypeParameters()[0]);
        if (SystemServiceConfigurer.class.isAssignableFrom(type.getRawType())) {
            throw new IllegalArgumentException(String.format("Invalid service '%s'. Services can only use a SystemServiceConfigurer if the application is " + "deployed in the system namespace.", service.getClass().getSimpleName()));
        }
    }
    DefaultSystemTableConfigurer systemTableConfigurer = new DefaultSystemTableConfigurer();
    DefaultServiceConfigurer configurer = new DefaultServiceConfigurer(service, deployNamespace, artifactId, pluginFinder, pluginInstantiator, systemTableConfigurer, runtimeInfo, getFeatureFlagsProvider());
    service.configure(configurer);
    ServiceSpecification spec = configurer.createSpecification();
    addDatasetsAndPlugins(configurer);
    addSystemTableSpecs(systemTableConfigurer.getTableSpecs());
    services.put(spec.getName(), spec);
}
Also used : ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) DefaultSystemTableConfigurer(io.cdap.cdap.internal.app.services.DefaultSystemTableConfigurer) DefaultServiceConfigurer(io.cdap.cdap.internal.app.services.DefaultServiceConfigurer)

Example 10 with ServiceSpecification

use of io.cdap.cdap.api.service.ServiceSpecification in project cdap by cdapio.

the class ServiceClient method getEndpoints.

/**
 * Gets a list of {@link ServiceHttpEndpoint} that a {@link Service} exposes.
 *
 * @param service ID of the service
 * @return A list of {@link ServiceHttpEndpoint}
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or service could not be found
 */
public List<ServiceHttpEndpoint> getEndpoints(ServiceId service) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    ServiceSpecification specification = get(service);
    ImmutableList.Builder<ServiceHttpEndpoint> builder = new ImmutableList.Builder<>();
    for (HttpServiceHandlerSpecification handlerSpecification : specification.getHandlers().values()) {
        builder.addAll(handlerSpecification.getEndpoints());
    }
    return builder.build();
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) ImmutableList(com.google.common.collect.ImmutableList) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Aggregations

ServiceSpecification (io.cdap.cdap.api.service.ServiceSpecification)34 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)16 HttpServiceHandlerSpecification (io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)10 JsonObject (com.google.gson.JsonObject)6 ProgramType (io.cdap.cdap.proto.ProgramType)6 ProgramId (io.cdap.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6 Resources (io.cdap.cdap.api.Resources)4 MapReduceSpecification (io.cdap.cdap.api.mapreduce.MapReduceSpecification)4 Plugin (io.cdap.cdap.api.plugin.Plugin)4 ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)4 SparkSpecification (io.cdap.cdap.api.spark.SparkSpecification)4 ForwardingApplicationSpecification (io.cdap.cdap.internal.app.ForwardingApplicationSpecification)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 HttpResponse (io.cdap.common.http.HttpResponse)4 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (com.google.common.reflect.TypeToken)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 AppWithServices (io.cdap.cdap.AppWithServices)2