Search in sources :

Example 1 with FlowletDefinition

use of co.cask.cdap.api.flow.FlowletDefinition in project cdap by caskdata.

the class FlowProgramRunner method createFlowlets.

/**
   * Starts all flowlets in the flow program.
   * @param program Program to run
   * @param flowSpec The {@link FlowSpecification}.
   * @return A {@link Table} with row as flowlet id, column as instance id, cell as the {@link ProgramController}
   *         for the flowlet.
   */
private Table<String, Integer, ProgramController> createFlowlets(Program program, ProgramOptions options, FlowSpecification flowSpec) {
    Table<String, Integer, ProgramController> flowlets = HashBasedTable.create();
    try {
        for (Map.Entry<String, FlowletDefinition> entry : flowSpec.getFlowlets().entrySet()) {
            ProgramOptions flowletOptions = resolveFlowletOptions(options, entry.getKey());
            int instanceCount = entry.getValue().getInstances();
            for (int instanceId = 0; instanceId < instanceCount; instanceId++) {
                flowlets.put(entry.getKey(), instanceId, startFlowlet(program, createFlowletOptions(entry.getKey(), instanceId, instanceCount, flowletOptions)));
            }
        }
    } catch (Throwable t) {
        try {
            // Need to stop all started flowlets
            Futures.successfulAsList(Iterables.transform(flowlets.values(), new Function<ProgramController, ListenableFuture<?>>() {

                @Override
                public ListenableFuture<?> apply(ProgramController controller) {
                    return controller.stop();
                }
            })).get();
        } catch (Exception e) {
            LOG.error("Fail to stop all flowlets on failure.");
        }
        throw Throwables.propagate(t);
    }
    return flowlets;
}
Also used : FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) Function(com.google.common.base.Function) ProgramController(co.cask.cdap.app.runtime.ProgramController) AbstractProgramController(co.cask.cdap.internal.app.runtime.AbstractProgramController) HashMap(java.util.HashMap) Map(java.util.Map) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(co.cask.cdap.app.runtime.ProgramOptions) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with FlowletDefinition

use of co.cask.cdap.api.flow.FlowletDefinition in project cdap by caskdata.

the class FlowSpecificationCodec method deserialize.

@Override
public FlowSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String className = jsonObj.get("className").getAsString();
    String name = jsonObj.get("name").getAsString();
    String description = jsonObj.get("description").getAsString();
    Map<String, FlowletDefinition> flowlets = deserializeMap(jsonObj.get("flowlets"), context, FlowletDefinition.class);
    List<FlowletConnection> connections = deserializeList(jsonObj.get("connections"), context, FlowletConnection.class);
    return new DefaultFlowSpecification(className, name, description, flowlets, connections);
}
Also used : FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) FlowletConnection(co.cask.cdap.api.flow.FlowletConnection) JsonObject(com.google.gson.JsonObject) DefaultFlowSpecification(co.cask.cdap.internal.flow.DefaultFlowSpecification)

Example 3 with FlowletDefinition

use of co.cask.cdap.api.flow.FlowletDefinition in project cdap by caskdata.

the class FlowletProgramRunner method createSchemaCache.

private SchemaCache createSchemaCache(Program program) throws Exception {
    ImmutableSet.Builder<Schema> schemas = ImmutableSet.builder();
    for (FlowSpecification flowSpec : program.getApplicationSpecification().getFlows().values()) {
        for (FlowletDefinition flowletDef : flowSpec.getFlowlets().values()) {
            schemas.addAll(Iterables.concat(flowletDef.getInputs().values()));
            schemas.addAll(Iterables.concat(flowletDef.getOutputs().values()));
        }
    }
    // Temp fix for ENG-3949. Always add old stream event schema.
    // TODO: Remove it later. The right thing to do is to have schemas history being stored to support schema
    // evolution. By design, as long as the schema cache is populated with old schema, the type projection logic
    // in the decoder would handle it correctly.
    schemas.add(schemaGenerator.generate(StreamEventData.class));
    return new SchemaCache(schemas.build(), program.getClassLoader());
}
Also used : FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) ImmutableSet(com.google.common.collect.ImmutableSet) FlowSpecification(co.cask.cdap.api.flow.FlowSpecification) Schema(co.cask.cdap.api.data.schema.Schema) StreamEventData(co.cask.cdap.api.stream.StreamEventData)

Example 4 with FlowletDefinition

use of co.cask.cdap.api.flow.FlowletDefinition in project cdap by caskdata.

the class InMemoryFlowProgramRunner method createFlowlets.

/**
 * Starts all flowlets in the flow program.
 * @param program Program to run
 * @param flowSpec The {@link FlowSpecification}.
 * @return A {@link Table} with row as flowlet id, column as instance id, cell as the {@link ProgramController}
 *         for the flowlet.
 */
private Table<String, Integer, ProgramController> createFlowlets(Program program, ProgramOptions options, FlowSpecification flowSpec) {
    Table<String, Integer, ProgramController> flowlets = HashBasedTable.create();
    try {
        for (Map.Entry<String, FlowletDefinition> entry : flowSpec.getFlowlets().entrySet()) {
            ProgramOptions flowletOptions = resolveFlowletOptions(options, entry.getKey());
            int instanceCount = entry.getValue().getInstances();
            for (int instanceId = 0; instanceId < instanceCount; instanceId++) {
                flowlets.put(entry.getKey(), instanceId, startFlowlet(program, createFlowletOptions(instanceId, instanceCount, flowletOptions)));
            }
        }
    } catch (Throwable t) {
        try {
            // Need to stop all started flowlets
            Futures.successfulAsList(Iterables.transform(flowlets.values(), new Function<ProgramController, ListenableFuture<?>>() {

                @Override
                public ListenableFuture<?> apply(ProgramController controller) {
                    return controller.stop();
                }
            })).get();
        } catch (Exception e) {
            LOG.error("Fail to stop all flowlets on failure.");
        }
        throw Throwables.propagate(t);
    }
    return flowlets;
}
Also used : FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) Function(com.google.common.base.Function) ProgramController(co.cask.cdap.app.runtime.ProgramController) AbstractProgramController(co.cask.cdap.internal.app.runtime.AbstractProgramController) HashMap(java.util.HashMap) Map(java.util.Map) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(co.cask.cdap.app.runtime.ProgramOptions) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with FlowletDefinition

use of co.cask.cdap.api.flow.FlowletDefinition in project cdap by caskdata.

the class DefaultFlowConfigurer method addFlowlet.

@Override
public void addFlowlet(String name, Flowlet flowlet, int instances) {
    Preconditions.checkNotNull(flowlet, UserMessages.getMessage(UserErrors.INVALID_FLOWLET_NULL));
    DefaultFlowletConfigurer flowletConfigurer = new DefaultFlowletConfigurer(flowlet);
    flowlet.configure(flowletConfigurer);
    FlowletSpecification flowletSpecification = flowletConfigurer.createSpecification();
    Map<String, Set<Type>> inputTypes = new HashMap<>();
    Map<String, Set<Type>> outputTypes = new HashMap<>();
    Reflections.visit(flowlet, flowlet.getClass(), new OutputEmitterFieldExtractor(outputTypes), new ProcessMethodExtractor(inputTypes));
    FlowletDefinition flowletDef = new FlowletDefinition(name, inputTypes, outputTypes, flowletSpecification, 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);
    addStreams(flowletConfigurer.getStreams());
    addDatasetSpecs(flowletConfigurer.getDatasetSpecs());
    addDatasetModules(flowletConfigurer.getDatasetModules());
}
Also used : FlowletDefinition(co.cask.cdap.api.flow.FlowletDefinition) Set(java.util.Set) HashMap(java.util.HashMap) FlowletSpecification(co.cask.cdap.api.flow.flowlet.FlowletSpecification) ProcessMethodExtractor(co.cask.cdap.internal.specification.ProcessMethodExtractor) OutputEmitterFieldExtractor(co.cask.cdap.internal.specification.OutputEmitterFieldExtractor)

Aggregations

FlowletDefinition (co.cask.cdap.api.flow.FlowletDefinition)16 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)7 Map (java.util.Map)7 Set (java.util.Set)6 QueueSpecification (co.cask.cdap.app.queue.QueueSpecification)5 FlowletConnection (co.cask.cdap.api.flow.FlowletConnection)4 SimpleQueueSpecificationGenerator (co.cask.cdap.internal.app.queue.SimpleQueueSpecificationGenerator)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 HashMap (java.util.HashMap)3 Schema (co.cask.cdap.api.data.schema.Schema)2 FlowletSpecification (co.cask.cdap.api.flow.flowlet.FlowletSpecification)2 ProgramController (co.cask.cdap.app.runtime.ProgramController)2 ProgramOptions (co.cask.cdap.app.runtime.ProgramOptions)2 QueueName (co.cask.cdap.common.queue.QueueName)2 ConsumerGroupConfig (co.cask.cdap.data2.queue.ConsumerGroupConfig)2 ForwardingFlowSpecification (co.cask.cdap.internal.app.ForwardingFlowSpecification)2 AbstractProgramController (co.cask.cdap.internal.app.runtime.AbstractProgramController)2 SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)2