Preconditions.checkArgument(processorType == ProgramType.SERVICE, "Only Service process type is supported.");
String processorName = program.getName();
Preconditions.checkNotNull(processorName, "Missing processor name.");
ServiceSpecification serviceSpec = appSpec.getServices().get(processorName);
RuntimeSpecification runnableSpec = serviceSpec.getRunnables().get(runnableName);
Preconditions.checkNotNull(runnableSpec, "RuntimeSpecification missing for Runnable \"%s\"", runnableName);
Class<?> clz = null;
String classStr = runnableSpec.getRunnableSpecification().getClassName();
// special case for handling http service
if (classStr.equals(HttpServiceTwillRunnable.class.getName())) {
clz = HttpServiceTwillRunnable.class;
} else {
clz = Class.forName(runnableSpec.getRunnableSpecification().getClassName(),
true, program.getClassLoader());
}
Preconditions.checkArgument(TwillRunnable.class.isAssignableFrom(clz), "%s is not a TwillRunnable.", clz);
Class<? extends TwillRunnable> runnableClass = (Class<? extends TwillRunnable>) clz;
RunId twillRunId = RunIds.generate();
final String[] argArray = RuntimeArguments.toPosixArray(options.getUserArguments());
DiscoveryService dService = new DiscoveryService() {
@Override
public Cancellable register(final Discoverable discoverable) {
discoverables.add(discoverable);
return dsService.register(new Discoverable() {
@Override
public String getName() {
return String.format("service.%s.%s.%s", program.getAccountId(),
program.getApplicationId(), program.getName());
}
@Override
public InetSocketAddress getSocketAddress() {
return discoverable.getSocketAddress();
}
});
}
};
twillContext = new InMemoryTwillContext(twillRunId, runId, InetAddress.getLocalHost(), new String[0], argArray,
runnableSpec.getRunnableSpecification(), instanceId,
runnableSpec.getResourceSpecification().getVirtualCores(),
runnableSpec.getResourceSpecification().getMemorySize(),
discoveryServiceClient, dService, instanceCount, electionRegistry);
TypeToken<? extends TwillRunnable> runnableType = TypeToken.of(runnableClass);
TwillRunnable runnable = null;
if (runnableClass.isAssignableFrom(HttpServiceTwillRunnable.class)) {
// Special case for running HTTP services
runnable = new HttpServiceTwillRunnable(program, runId, cConfiguration, runnableName, metricsCollectionService,
discoveryServiceClient, datasetFramework,
transactionSystemClient);
} else if (runnableClass.isAssignableFrom(ServiceWorkerTwillRunnable.class)) {
runnable = new ServiceWorkerTwillRunnable(program, runId, runnableName, program.getClassLoader(),
cConfiguration, metricsCollectionService, datasetFramework,
transactionSystemClient, discoveryServiceClient);
} else {
runnable = new InstantiatorFactory(false).get(runnableType).create();
}
InMemoryRunnableDriver driver = new
InMemoryRunnableDriver(runnable, twillContext, new UserServiceLoggingContext(program.getAccountId(),
program.getApplicationId(),
processorName,
runnableName));
//Injecting Metrics
Reflections.visit(runnable, runnableType,
new MetricsFieldSetter(new ServiceRunnableMetrics(metricsCollectionService,
program.getApplicationId(),
serviceSpec.getName(), runnableName,
twillContext.getInstanceId())),
new PropertyFieldSetter(runnableSpec.getRunnableSpecification().getConfigs()));
ProgramController controller = new InMemoryRunnableProgramController(program.getName(), runnableName,
twillContext, driver,