String processorName = program.getName();
Preconditions.checkNotNull(processorName, "Missing processor name.");
FlowSpecification flowSpec = appSpec.getFlows().get(processorName);
FlowletDefinition flowletDef = flowSpec.getFlowlets().get(flowletName);
Preconditions.checkNotNull(flowletDef, "Definition missing for flowlet \"%s\"", flowletName);
boolean disableTransaction = program.getMainClass().isAnnotationPresent(DisableTransaction.class);
if (disableTransaction) {
LOG.info("Transaction is disable for flowlet {}.{}.{}",
program.getApplicationId(), program.getId().getId(), 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 flowlet context
flowletContext = new BasicFlowletContext(program, flowletName, instanceId,
runId, instanceCount,
flowletDef.getDatasets(),
options.getUserArguments(), flowletDef.getFlowletSpec(),
metricsCollectionService, discoveryServiceClient,
dsFramework, configuration);
// Creates tx related objects
DataFabricFacade dataFabricFacade = disableTransaction ?
dataFabricFacadeFactory.createNoTransaction(program, flowletContext.getDatasetInstantiator())
: dataFabricFacadeFactory.create(program, flowletContext.getDatasetInstantiator());
// Creates QueueSpecification
Table<Node, String, Set<QueueSpecification>> queueSpecs =
new SimpleQueueSpecificationGenerator(Id.Application.from(program.getAccountId(), program.getApplicationId()))
.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 cdap classloader. It is needed for the DatumWriterFactory be able
// to load cdap 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 DataSetFieldSetter(flowletContext),
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);
if (disableTransaction) {
LOG.info("Transaction disabled for flowlet {}", flowletContext);
}