Search in sources :

Example 1 with DAG

use of com.datatorrent.api.DAG in project apex-core by apache.

the class OperatorContextTest method testInjectionOfOperatorName.

@Test
public void testInjectionOfOperatorName() throws Exception {
    StreamingApplication application = new StreamingApplication() {

        @Override
        public void populateDAG(DAG dag, Configuration conf) {
            dag.addOperator("input", new MockInputOperator());
        }
    };
    LocalMode lma = LocalMode.newInstance();
    lma.prepareDAG(application, new Configuration());
    LocalMode.Controller lc = lma.getController();
    lc.runAsync();
    latch.await();
    Assert.assertEquals("operator name", "input", operatorName);
    lc.shutdown();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) LocalMode(com.datatorrent.api.LocalMode) StreamingApplication(com.datatorrent.api.StreamingApplication) DAG(com.datatorrent.api.DAG) Test(org.junit.Test)

Example 2 with DAG

use of com.datatorrent.api.DAG in project apex-core by apache.

the class LogicalPlanConfiguration method populateDAG.

/**
   * Populate the logical plan structure from properties.
   * @param dag
   */
public void populateDAG(LogicalPlan dag) {
    Configuration pconf = new Configuration(conf);
    for (final String propertyName : this.properties.stringPropertyNames()) {
        String propertyValue = this.properties.getProperty(propertyName);
        pconf.setIfUnset(propertyName, propertyValue);
    }
    AppConf appConf = this.stramConf.getChild(WILDCARD, StramElement.APPLICATION);
    if (appConf == null) {
        LOG.warn("Application configuration not found. Probably an empty app.");
        return;
    }
    Map<String, OperatorConf> operators = appConf.getChildren(StramElement.OPERATOR);
    Map<OperatorConf, GenericOperator> nodeMap = Maps.newHashMapWithExpectedSize(operators.size());
    // add all operators first
    for (Map.Entry<String, OperatorConf> nodeConfEntry : operators.entrySet()) {
        OperatorConf nodeConf = nodeConfEntry.getValue();
        if (!WILDCARD.equals(nodeConf.id)) {
            Class<? extends GenericOperator> nodeClass = StramUtils.classForName(nodeConf.getClassNameReqd(), GenericOperator.class);
            String optJson = nodeConf.getProperties().get(nodeClass.getName());
            GenericOperator operator = null;
            try {
                if (optJson != null) {
                    // if there is a special key which is the class name, it means the operator is serialized in json format
                    ObjectMapper mapper = ObjectMapperFactory.getOperatorValueDeserializer();
                    operator = mapper.readValue("{\"" + nodeClass.getName() + "\":" + optJson + "}", nodeClass);
                    addOperator(dag, nodeConfEntry.getKey(), operator);
                } else {
                    operator = addOperator(dag, nodeConfEntry.getKey(), nodeClass);
                }
                setOperatorProperties(operator, nodeConf.getProperties());
            } catch (IOException e) {
                throw new IllegalArgumentException("Error setting operator properties " + e.getMessage(), e);
            }
            nodeMap.put(nodeConf, operator);
        }
    }
    Map<String, StreamConf> streams = appConf.getChildren(StramElement.STREAM);
    // wire operators
    for (Map.Entry<String, StreamConf> streamConfEntry : streams.entrySet()) {
        StreamConf streamConf = streamConfEntry.getValue();
        DAG.StreamMeta sd = dag.addStream(streamConfEntry.getKey());
        sd.setLocality(streamConf.getLocality());
        String schemaClassName = streamConf.properties.getProperty(STREAM_SCHEMA);
        Class<?> schemaClass = null;
        if (schemaClassName != null) {
            schemaClass = StramUtils.classForName(schemaClassName, Object.class);
        }
        if (streamConf.sourceNode != null) {
            String portName = null;
            for (Map.Entry<String, StreamConf> e : streamConf.sourceNode.outputs.entrySet()) {
                if (e.getValue() == streamConf) {
                    portName = e.getKey();
                }
            }
            GenericOperator sourceDecl = nodeMap.get(streamConf.sourceNode);
            Operators.PortMappingDescriptor sourcePortMap = new Operators.PortMappingDescriptor();
            Operators.describe(sourceDecl, sourcePortMap);
            sd.setSource(sourcePortMap.outputPorts.get(portName).component);
            if (schemaClass != null) {
                dag.setOutputPortAttribute(sourcePortMap.outputPorts.get(portName).component, PortContext.TUPLE_CLASS, schemaClass);
            }
        }
        for (OperatorConf targetNode : streamConf.targetNodes) {
            String portName = null;
            for (Map.Entry<String, StreamConf> e : targetNode.inputs.entrySet()) {
                if (e.getValue() == streamConf) {
                    portName = e.getKey();
                }
            }
            GenericOperator targetDecl = nodeMap.get(targetNode);
            Operators.PortMappingDescriptor targetPortMap = new Operators.PortMappingDescriptor();
            Operators.describe(targetDecl, targetPortMap);
            sd.addSink(targetPortMap.inputPorts.get(portName).component);
            if (schemaClass != null) {
                dag.setInputPortAttribute(targetPortMap.inputPorts.get(portName).component, PortContext.TUPLE_CLASS, schemaClass);
            }
        }
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) GenericOperator(com.datatorrent.api.DAG.GenericOperator) IOException(java.io.IOException) PRE_VALIDATE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.PRE_VALIDATE_DAG) POST_CONFIGURE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.POST_CONFIGURE_DAG) DAG(com.datatorrent.api.DAG) POST_VALIDATE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.POST_VALIDATE_DAG) PRE_POPULATE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.PRE_POPULATE_DAG) POST_POPULATE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.POST_POPULATE_DAG) PRE_CONFIGURE_DAG(org.apache.apex.api.plugin.DAGSetupEvent.Type.PRE_CONFIGURE_DAG) JSONObject(org.codehaus.jettison.json.JSONObject) Map(java.util.Map) BeanMap(org.apache.commons.beanutils.BeanMap) TreeMap(java.util.TreeMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 3 with DAG

use of com.datatorrent.api.DAG in project apex-core by apache.

the class LogicalPlanConfigurationTest method testOperatorLevelAttributes.

@Test
@SuppressWarnings({ "UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes" })
public void testOperatorLevelAttributes() {
    String appName = "app1";
    StreamingApplication app = new StreamingApplication() {

        @Override
        public void populateDAG(DAG dag, Configuration conf) {
            dag.addOperator("operator1", GenericTestOperator.class);
            dag.addOperator("operator2", GenericTestOperator.class);
        }
    };
    Properties props = new Properties();
    props.put(StreamingApplication.APEX_PREFIX + "application." + appName + ".class", app.getClass().getName());
    props.put(StreamingApplication.APEX_PREFIX + "operator.*." + OperatorContext.APPLICATION_WINDOW_COUNT.getName(), "2");
    props.put(StreamingApplication.APEX_PREFIX + "operator.*." + OperatorContext.STATS_LISTENERS.getName(), PartitionLoadWatch.class.getName());
    props.put(StreamingApplication.APEX_PREFIX + "application." + appName + ".operator.operator1." + OperatorContext.APPLICATION_WINDOW_COUNT.getName(), "20");
    LogicalPlanConfiguration dagBuilder = new LogicalPlanConfiguration(new Configuration(false));
    dagBuilder.addFromProperties(props, null);
    String appPath = app.getClass().getName().replace(".", "/") + ".class";
    LogicalPlan dag = new LogicalPlan();
    dagBuilder.prepareDAG(dag, app, appPath);
    Assert.assertEquals("", Integer.valueOf(20), dag.getOperatorMeta("operator1").getValue(OperatorContext.APPLICATION_WINDOW_COUNT));
    Assert.assertEquals("", Integer.valueOf(2), dag.getOperatorMeta("operator2").getValue(OperatorContext.APPLICATION_WINDOW_COUNT));
    Assert.assertEquals("", PartitionLoadWatch.class, dag.getOperatorMeta("operator2").getValue(OperatorContext.STATS_LISTENERS).toArray()[0].getClass());
}
Also used : PartitionLoadWatch(com.datatorrent.stram.PartitioningTest.PartitionLoadWatch) Configuration(org.apache.hadoop.conf.Configuration) Integer2String(com.datatorrent.api.StringCodec.Integer2String) StreamingApplication(com.datatorrent.api.StreamingApplication) DAG(com.datatorrent.api.DAG) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with DAG

use of com.datatorrent.api.DAG in project apex-core by apache.

the class LogicalPlanConfigurationTest method testApplicationLevelParameter.

@Test
public void testApplicationLevelParameter() {
    String appName = "app1";
    final GenericTestOperator operator1 = new GenericTestOperator();
    final GenericTestOperator operator2 = new GenericTestOperator();
    StreamingApplication app = new StreamingApplication() {

        @Override
        public void populateDAG(DAG dag, Configuration conf) {
            dag.addOperator("operator1", operator1);
            dag.addOperator("operator2", operator2);
        }
    };
    Properties props = new Properties();
    props.put(StreamingApplication.APEX_PREFIX + "application." + appName + ".class", app.getClass().getName());
    props.put(StreamingApplication.APEX_PREFIX + "operator.*.myStringProperty", "foo ${xyz} bar ${zzz} baz");
    props.put(StreamingApplication.APEX_PREFIX + "operator.*.booleanProperty", Boolean.TRUE.toString());
    props.put(StreamingApplication.APEX_PREFIX + "application." + appName + ".operator.operator1.myStringProperty", "apv1");
    LogicalPlanConfiguration dagBuilder = new LogicalPlanConfiguration(new Configuration(false));
    Configuration vars = new Configuration(false);
    vars.set("xyz", "123");
    vars.set("zzz", "456");
    dagBuilder.addFromProperties(props, vars);
    String appPath = app.getClass().getName().replace(".", "/") + ".class";
    LogicalPlan dag = new LogicalPlan();
    dagBuilder.prepareDAG(dag, app, appPath);
    Assert.assertEquals("apv1", operator1.getMyStringProperty());
    Assert.assertEquals("foo 123 bar 456 baz", operator2.getMyStringProperty());
    Assert.assertEquals(true, operator2.isBooleanProperty());
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) GenericTestOperator(com.datatorrent.stram.engine.GenericTestOperator) Integer2String(com.datatorrent.api.StringCodec.Integer2String) StreamingApplication(com.datatorrent.api.StreamingApplication) DAG(com.datatorrent.api.DAG) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with DAG

use of com.datatorrent.api.DAG in project apex-core by apache.

the class LogicalPlanConfigurationTest method testModuleUnifierLevelAttributes.

@Test
@SuppressWarnings({ "UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes" })
public void testModuleUnifierLevelAttributes() {
    class DummyOperator extends BaseOperator {

        int prop;

        public transient DefaultInputPort<Integer> input = new DefaultInputPort<Integer>() {

            @Override
            public void process(Integer tuple) {
                LOG.debug(tuple.intValue() + " processed");
                output.emit(tuple);
            }
        };

        public transient DefaultOutputPort<Integer> output = new DefaultOutputPort<>();
    }
    class DummyOutputOperator extends BaseOperator {

        int prop;

        public transient DefaultInputPort<Integer> input = new DefaultInputPort<Integer>() {

            @Override
            public void process(Integer tuple) {
                LOG.debug(tuple.intValue() + " processed");
            }
        };
    }
    class TestUnifierAttributeModule implements Module {

        public transient ProxyInputPort<Integer> moduleInput = new ProxyInputPort<>();

        public transient ProxyOutputPort<Integer> moduleOutput = new Module.ProxyOutputPort<>();

        @Override
        public void populateDAG(DAG dag, Configuration conf) {
            DummyOperator dummyOperator = dag.addOperator("DummyOperator", new DummyOperator());
            dag.setOperatorAttribute(dummyOperator, Context.OperatorContext.PARTITIONER, new StatelessPartitioner<DummyOperator>(3));
            dag.setUnifierAttribute(dummyOperator.output, OperatorContext.TIMEOUT_WINDOW_COUNT, 2);
            moduleInput.set(dummyOperator.input);
            moduleOutput.set(dummyOperator.output);
        }
    }
    StreamingApplication app = new StreamingApplication() {

        @Override
        public void populateDAG(DAG dag, Configuration conf) {
            Module m1 = dag.addModule("TestModule", new TestUnifierAttributeModule());
            DummyOutputOperator dummyOutputOperator = dag.addOperator("DummyOutputOperator", new DummyOutputOperator());
            dag.addStream("Module To Operator", ((TestUnifierAttributeModule) m1).moduleOutput, dummyOutputOperator.input);
        }
    };
    String appName = "UnifierApp";
    LogicalPlanConfiguration dagBuilder = new LogicalPlanConfiguration(new Configuration(false));
    LogicalPlan dag = new LogicalPlan();
    dag.setAttribute(Context.OperatorContext.STORAGE_AGENT, new MockStorageAgent());
    dagBuilder.prepareDAG(dag, app, appName);
    LogicalPlan.OperatorMeta ometa = dag.getOperatorMeta("TestModule$DummyOperator");
    LogicalPlan.OperatorMeta om = null;
    for (Map.Entry<LogicalPlan.OutputPortMeta, LogicalPlan.StreamMeta> entry : ometa.getOutputStreams().entrySet()) {
        if (entry.getKey().getPortName().equals("output")) {
            om = entry.getKey().getUnifierMeta();
        }
    }
    /*
     * Verify the attribute value after preparing DAG.
     */
    Assert.assertNotNull(om);
    Assert.assertEquals("", Integer.valueOf(2), om.getValue(Context.OperatorContext.TIMEOUT_WINDOW_COUNT));
    PhysicalPlan plan = new PhysicalPlan(dag, new TestPlanContext());
    List<PTContainer> containers = plan.getContainers();
    LogicalPlan.OperatorMeta operatorMeta = null;
    for (PTContainer container : containers) {
        List<PTOperator> operators = container.getOperators();
        for (PTOperator operator : operators) {
            if (operator.isUnifier()) {
                operatorMeta = operator.getOperatorMeta();
            }
        }
    }
    /*
     * Verify attribute after physical plan creation with partitioned operators.
     */
    Assert.assertEquals("", Integer.valueOf(2), operatorMeta.getValue(OperatorContext.TIMEOUT_WINDOW_COUNT));
}
Also used : BaseOperator(com.datatorrent.common.util.BaseOperator) Configuration(org.apache.hadoop.conf.Configuration) StreamingApplication(com.datatorrent.api.StreamingApplication) Integer2String(com.datatorrent.api.StringCodec.Integer2String) StreamMeta(com.datatorrent.stram.plan.logical.LogicalPlan.StreamMeta) OutputPortMeta(com.datatorrent.stram.plan.logical.LogicalPlan.OutputPortMeta) TestPlanContext(com.datatorrent.stram.plan.TestPlanContext) PTContainer(com.datatorrent.stram.plan.physical.PTContainer) DefaultOutputPort(com.datatorrent.api.DefaultOutputPort) PhysicalPlan(com.datatorrent.stram.plan.physical.PhysicalPlan) PTOperator(com.datatorrent.stram.plan.physical.PTOperator) DAG(com.datatorrent.api.DAG) Module(com.datatorrent.api.Module) Map(java.util.Map) AttributeMap(com.datatorrent.api.Attribute.AttributeMap) DefaultInputPort(com.datatorrent.api.DefaultInputPort) OperatorMeta(com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta) Test(org.junit.Test)

Aggregations

DAG (com.datatorrent.api.DAG)21 Test (org.junit.Test)15 Configuration (org.apache.hadoop.conf.Configuration)13 StreamingApplication (com.datatorrent.api.StreamingApplication)11 Properties (java.util.Properties)7 Pipeline (org.apache.beam.sdk.Pipeline)6 Integer2String (com.datatorrent.api.StringCodec.Integer2String)5 AttributeMap (com.datatorrent.api.Attribute.AttributeMap)4 File (java.io.File)4 Map (java.util.Map)4 ApexPipelineOptions (org.apache.beam.runners.apex.ApexPipelineOptions)4 Attribute (com.datatorrent.api.Attribute)3 GenericTestOperator (com.datatorrent.stram.engine.GenericTestOperator)3 ApexRunnerResult (org.apache.beam.runners.apex.ApexRunnerResult)3 OperatorMeta (com.datatorrent.stram.plan.logical.LogicalPlan.OperatorMeta)2 OutputPortMeta (com.datatorrent.stram.plan.logical.LogicalPlan.OutputPortMeta)2 StreamMeta (com.datatorrent.stram.plan.logical.LogicalPlan.StreamMeta)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JarFile (java.util.jar.JarFile)2