/** * Generate schemas for all input and output types with the given {@link SchemaGenerator}. * @param generator The {@link SchemaGenerator} for generating type schema. */ public void generateSchema(SchemaGenerator generator) throws UnsupportedTypeException { if (inputs == null && outputs == null && inputTypes != null && outputTypes != null) { // Generate both inputs and outputs before making this visible Map<String, Set<Schema>> inputs = generateSchema(generator, inputTypes); Map<String, Set<Schema>> outputs = generateSchema(generator, outputTypes); this.inputs = inputs; this.outputs = outputs; } }
@Override public MoreFlowlet add(String name, Flowlet flowlet, int instances) { Preconditions.checkArgument(flowlet != null, UserMessages.getMessage(UserErrors.INVALID_FLOWLET_NULL)); FlowletDefinition flowletDef = new FlowletDefinition(name, flowlet, instances); String flowletName = flowletDef.getFlowletSpec().getName(); Preconditions.checkArgument(instances > 0, String.format(UserMessages.getMessage(UserErrors.INVALID_INSTANCES), flowletName, instances)); Preconditions.checkArgument(!flowlets.containsKey(flowletName), UserMessages.getMessage(UserErrors.INVALID_FLOWLET_EXISTS), flowletName); flowlets.put(flowletName, flowletDef); return this; }
private SchemaCache createSchemaCache(Program program) throws Exception { ImmutableSet.Builder<Schema> schemas = ImmutableSet.builder(); for (FlowletDefinition flowletDef : program.getSpecification().getFlowlets().values()) { schemas.addAll(Iterables.concat(flowletDef.getInputs().values())); schemas.addAll(Iterables.concat(flowletDef.getOutputs().values())); } return new SchemaCache(schemas.build(), program.getClassLoader()); }
@Override public ProgramController run(Program program, ProgramOptions options) { // Extract and verify parameters FlowSpecification flowSpec = program.getSpecification(); ProgramType processorType = program.getType(); Preconditions.checkNotNull(processorType, "Missing processor type."); Preconditions.checkArgument(processorType == ProgramType.FLOW, "Only FLOW process type is supported."); Preconditions.checkNotNull(flowSpec, "Missing FlowSpecification for %s", program.getName()); for (FlowletDefinition flowletDefinition : flowSpec.getFlowlets().values()) { int maxInstances = flowletDefinition.getFlowletSpec().getMaxInstances(); Preconditions.checkArgument(flowletDefinition.getInstances() <= maxInstances, "Flowlet %s can have a maximum of %s instances", flowletDefinition.getFlowletSpec().getName(), maxInstances); } try { // Launch flowlet program runners RunId runId = RunIds.generate(); programOptions.put(runId, options); Multimap<String, QueueName> consumerQueues = FlowUtils.configureQueue(program, flowSpec, queueAdmin); final Table<String, Integer, ProgramController> flowlets = createFlowlets(program, runId, flowSpec); return new FlowProgramController(flowlets, runId, program, flowSpec, consumerQueues, discoveryServiceClient); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public MoreConnect to(String flowlet) { Preconditions.checkArgument(flowlet != null, UserMessages.getMessage(UserErrors.INVALID_FLOWLET_NULL)); Preconditions.checkArgument(flowlets.containsKey(flowlet), UserMessages.getMessage(UserErrors.INVALID_FLOWLET_NAME), flowlet); FlowletConnection.Type sourceType; String sourceName; sourceType = FlowletConnection.Type.FLOWLET; sourceName = fromFlowlet.getFlowletSpec().getName(); connections.add(new FlowletConnection(sourceType, sourceName, flowlet)); return this; }
int instanceCount = entry.getValue().getInstances(); for (int instanceId = 0; instanceId < instanceCount; instanceId++) { flowlets.put(entry.getKey(), instanceId,
FlowletDefinition(String flowletName, Flowlet flowlet, int instances) { FlowletSpecification flowletSpec = flowlet.configure(); this.instances = instances; Map<String, Set<Type>> inputTypes = Maps.newHashMap(); Map<String, Set<Type>> outputTypes = Maps.newHashMap(); Map<String, String> properties = Maps.newHashMap(flowletSpec.getProperties()); Reflections.visit(flowlet, TypeToken.of(flowlet.getClass()), new PropertyFieldExtractor(properties), new OutputEmitterFieldExtractor(outputTypes), new ProcessMethodExtractor(inputTypes)); this.inputTypes = immutableCopyOf(inputTypes); this.outputTypes = immutableCopyOf(outputTypes); this.flowletSpec = new DefaultFlowletSpecification(flowlet.getClass().getName(), flowletName == null ? flowletSpec.getName() : flowletName, flowletSpec.getDescription(), flowletSpec.getFailurePolicy(), properties, flowletSpec.getResources(), flowletSpec.getMaxInstances()); }
@Override protected ProgramController launch(Program program, ProgramOptions options, File hConfFile, File cConfFile, ApplicationLauncher launcher) { // Extract and verify parameters FlowSpecification flowSpec = program.getSpecification(); ProgramType processorType = program.getType(); Preconditions.checkNotNull(processorType, "Missing processor type."); Preconditions.checkArgument(processorType == ProgramType.FLOW, "Only FLOW process type is supported."); try { Preconditions.checkNotNull(flowSpec, "Missing FlowSpecification for %s", program.getName()); for (FlowletDefinition flowletDefinition : flowSpec.getFlowlets().values()) { int maxInstances = flowletDefinition.getFlowletSpec().getMaxInstances(); Preconditions.checkArgument(flowletDefinition.getInstances() <= maxInstances, "Flowlet %s can have a maximum of %s instances", flowletDefinition.getFlowletSpec().getName(), maxInstances); } LOG.info("Configuring flowlets queues"); Multimap<String, QueueName> flowletQueues = FlowUtils.configureQueue(program, flowSpec, queueAdmin); // Launch flowlet program runners LOG.info("Launching distributed flow: " + program.getName() + ":" + flowSpec.getName()); TwillController controller = launcher.launch(new FlowTwillApplication(program, flowSpec, hConfFile, cConfFile, eventHandler)); DistributedFlowletInstanceUpdater instanceUpdater = new DistributedFlowletInstanceUpdater(program, controller, queueAdmin, flowletQueues); return new FlowTwillProgramController(program.getName(), controller, instanceUpdater).startListen(); } catch (Exception e) { throw Throwables.propagate(e); } }
void update(String flowletId, int newInstanceCount, int oldInstanceCount) throws Exception { FlowletDefinition flowletDefinition = program.getSpecification().getFlowlets().get(flowletId); int maxInstances = flowletDefinition.getFlowletSpec().getMaxInstances(); Preconditions.checkArgument(newInstanceCount <= maxInstances, "Flowlet %s can have a maximum of %s instances", flowletId, maxInstances); waitForInstances(flowletId, oldInstanceCount); twillController.sendCommand(flowletId, ProgramCommands.SUSPEND).get(); FlowUtils.reconfigure(consumerQueues.get(flowletId), FlowUtils.generateConsumerGroupId(program, flowletId), newInstanceCount, queueAdmin); twillController.changeInstances(flowletId, newInstanceCount).get(); twillController.sendCommand(flowletId, ProgramCommands.RESUME).get(); }
String flowletId = entry.getKey(); long groupId = FlowUtils.generateConsumerGroupId(program, flowletId); int instances = entry.getValue().getInstances();
for (Map.Entry<String, FlowletDefinition> entry : spec.getFlowlets().entrySet()) { FlowletDefinition flowletDefinition = entry.getValue(); FlowletSpecification flowletSpec = flowletDefinition.getFlowletSpec(); ResourceSpecification resourceSpec = ResourceSpecification.Builder.with() .setVirtualCores(flowletSpec.getResources().getVirtualCores()) .setMemory(flowletSpec.getResources().getMemoryMB(), ResourceSpecification.SizeUnit.MEGA) .setInstances(flowletDefinition.getInstances()) .build();
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); options.getUserArguments(), flowletDef.getFlowletSpec(), metricsCollectionService, dataFabricFacade, serviceAnnouncer); new PropertyFieldSetter(flowletDef.getFlowletSpec().getProperties()), new MetricsFieldSetter(flowletContext.getMetrics()), new OutputEmitterFieldSetter(outputEmitterFactory(flowletContext, flowletName, Service serviceHook = createServiceHook(flowletName, consumerSuppliers, controllerRef); FlowletProcessDriver driver = new FlowletProcessDriver(flowlet, flowletContext, processSpecs, createCallback(flowlet, flowletDef.getFlowletSpec()), dataFabricFacade, serviceHook);
final Node sourceNode = new Node(connection.getSourceType(), source); Set<QueueSpecification> queueSpec = generateQueueSpecification(flow, connection, flowlets.get(target).getInputs(), flowlets.get(source).getOutputs());
public void toJson(FlowSpecification flowSpec, Appendable appendable) throws IOException { Preconditions.checkState(schemaGenerator != null, "No schema generator is configured. Fail to serialize to json"); try { for (FlowletDefinition flowletDef : flowSpec.getFlowlets().values()) { flowletDef.generateSchema(schemaGenerator); } gson.toJson(flowSpec, FlowSpecification.class, appendable); } catch (UnsupportedTypeException e) { throw new IOException(e); } }