Preconditions.checkArgument(processorType == ProgramType.FLOW, "Only FLOW process type is supported.");
String processorName = program.getName();
Preconditions.checkNotNull(processorName, "Missing processor name.");
FlowletDefinition flowletDef = flowSpec.getFlowlets().get(flowletName);
Preconditions.checkNotNull(flowletDef, "Definition missing for flowlet \"%s\"", flowletName);
Class<?> clz = Class.forName(flowletDef.getFlowletSpec().getClassName(), true,
program.getClassLoader());
Preconditions.checkArgument(Flowlet.class.isAssignableFrom(clz), "%s is not a Flowlet.", clz);
Class<? extends Flowlet> flowletClass = (Class<? extends Flowlet>) clz;
// Creates tx related objects
DataFabricFacade dataFabricFacade = dataFabricFacadeFactory.create(program);
// Creates flowlet context
flowletContext = new BasicFlowletContext(program, flowletName, instanceId, runId, instanceCount,
options.getUserArguments(), flowletDef.getFlowletSpec(),
metricsCollectionService, dataFabricFacade, serviceAnnouncer);
// Creates QueueSpecification
Table<Node, String, Set<QueueSpecification>> queueSpecs = new SimpleQueueSpecificationGenerator().create(
flowSpec);
Flowlet flowlet = new InstantiatorFactory(false).get(TypeToken.of(flowletClass)).create();
TypeToken<? extends Flowlet> flowletType = TypeToken.of(flowletClass);
// Set the context classloader to the Tigon classloader. It is needed for the DatumWriterFactory be able
// to load Tigon classes
Thread.currentThread().setContextClassLoader(FlowletProgramRunner.class.getClassLoader());
// Inject DataSet, OutputEmitter, Metric fields
Reflections.visit(flowlet, TypeToken.of(flowlet.getClass()),
new PropertyFieldSetter(flowletDef.getFlowletSpec().getProperties()),
new MetricsFieldSetter(flowletContext.getMetrics()),
new OutputEmitterFieldSetter(outputEmitterFactory(flowletContext, flowletName,
dataFabricFacade, queueSpecs))
);
ImmutableList.Builder<ConsumerSupplier<?>> queueConsumerSupplierBuilder = ImmutableList.builder();
Collection<ProcessSpecification> processSpecs =
createProcessSpecification(flowletContext, flowletType,
processMethodFactory(flowlet),
processSpecificationFactory(flowletContext, dataFabricFacade, queueReaderFactory,
flowletName, queueSpecs, queueConsumerSupplierBuilder,
createSchemaCache(program)),
Lists.<ProcessSpecification>newLinkedList());
List<ConsumerSupplier<?>> consumerSuppliers = queueConsumerSupplierBuilder.build();
// Create the flowlet driver
AtomicReference<FlowletProgramController> controllerRef = new AtomicReference<FlowletProgramController>();
Service serviceHook = createServiceHook(flowletName, consumerSuppliers, controllerRef);
FlowletProcessDriver driver = new FlowletProcessDriver(flowlet, flowletContext, processSpecs,
createCallback(flowlet, flowletDef.getFlowletSpec()),
dataFabricFacade, serviceHook);
FlowletProgramController controller = new FlowletProgramController(program.getName(), flowletName,
flowletContext, driver, consumerSuppliers);
controllerRef.set(controller);