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