use of com.datatorrent.stram.plan.TestPlanContext in project apex-core by apache.
the class PhysicalPlanTest method testSingleFinalUnifierInputOverride.
@Test
public void testSingleFinalUnifierInputOverride() {
LogicalPlan dag = new LogicalPlan();
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
dag.setOperatorAttribute(o1, OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(3));
OperatorMeta o1Meta = dag.getMeta(o1);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
dag.setOperatorAttribute(o2, OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(2));
dag.setInputPortAttribute(o2.inport1, PortContext.UNIFIER_SINGLE_FINAL, true);
OperatorMeta o2Meta = dag.getMeta(o2);
dag.addStream("o1.outport1", o1.outport1, o2.inport1);
dag.setAttribute(LogicalPlan.CONTAINERS_MAX_COUNT, 10);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 6, plan.getContainers().size());
Assert.assertEquals("o1 merge unifiers", 1, plan.getMergeOperators(o1Meta).size());
dag.setOutputPortAttribute(o1.outport1, PortContext.UNIFIER_SINGLE_FINAL, false);
ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 6, plan.getContainers().size());
Assert.assertEquals("o1 merge unifiers", 1, plan.getMergeOperators(o1Meta).size());
dag.setOutputPortAttribute(o1.outport1, PortContext.UNIFIER_SINGLE_FINAL, true);
dag.setInputPortAttribute(o2.inport1, PortContext.UNIFIER_SINGLE_FINAL, false);
ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 5, plan.getContainers().size());
Set<String> expectedNames = Sets.newHashSet(o1Meta.getMeta(o1.outport1).getUnifierMeta().getName(), o2Meta.getName());
for (int i = 3; i < 5; ++i) {
PTContainer container = plan.getContainers().get(i);
Assert.assertEquals("o2 container size", 2, container.getOperators().size());
Set<String> names = Sets.newHashSet();
for (PTOperator operator : container.getOperators()) {
names.add(operator.getOperatorMeta().getName());
}
Assert.assertEquals("o2 container operators", expectedNames, names);
}
}
use of com.datatorrent.stram.plan.TestPlanContext in project apex-core by apache.
the class LogicalPlanModificationTest method testAddOperator.
@Test
public void testAddOperator() {
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
dag.addStream("o1.outport1", o1.outport1, o2.inport1);
dag.addStream("o2.outport1", o2.outport1, o3.inport1);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
ctx.deploy.clear();
ctx.undeploy.clear();
Assert.assertEquals("containers", 3, plan.getContainers().size());
PlanModifier pm = new PlanModifier(plan);
GenericTestOperator added1 = new GenericTestOperator();
pm.addOperator("added1", added1);
pm.addStream("added1.outport1", added1.outport1, o3.inport2);
Assert.assertEquals("undeploy " + ctx.undeploy, 0, ctx.undeploy.size());
Assert.assertEquals("deploy " + ctx.deploy, 0, ctx.deploy.size());
pm.applyChanges(ctx);
Assert.assertEquals("containers post change", 4, plan.getContainers().size());
Assert.assertEquals("undeploy " + ctx.undeploy, 1, ctx.undeploy.size());
Assert.assertEquals("deploy " + ctx.deploy, 2, ctx.deploy.size());
}
use of com.datatorrent.stram.plan.TestPlanContext in project apex-core by apache.
the class PhysicalPlanTest method testMxNPartitioning.
/**
* MxN partitioning. When source and sink of a stream are partitioned, a
* separate unifier is created container local with each downstream partition.
*/
@Test
public void testMxNPartitioning() {
LogicalPlan dag = new LogicalPlan();
TestGeneratorInputOperator o1 = dag.addOperator("o1", TestGeneratorInputOperator.class);
TestPartitioner<TestGeneratorInputOperator> o1Partitioner = new TestPartitioner<>();
o1Partitioner.setPartitionCount(2);
dag.setOperatorAttribute(o1, OperatorContext.PARTITIONER, o1Partitioner);
dag.setOperatorAttribute(o1, OperatorContext.STATS_LISTENERS, Lists.newArrayList((StatsListener) new PartitioningTest.PartitionLoadWatch()));
OperatorMeta o1Meta = dag.getMeta(o1);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
dag.setOperatorAttribute(o2, OperatorContext.PARTITIONER, new StatelessPartitioner<TestGeneratorInputOperator>(3));
dag.setOperatorAttribute(o2, OperatorContext.STATS_LISTENERS, Arrays.asList(new StatsListener[] { new PartitioningTest.PartitionLoadWatch() }));
OperatorMeta o2Meta = dag.getMeta(o2);
dag.addStream("o1.outport1", o1.outport, o2.inport1);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 5, plan.getContainers().size());
List<PTOperator> inputOperators = new ArrayList<>();
for (int i = 0; i < 2; i++) {
PTContainer container = plan.getContainers().get(i);
Assert.assertEquals("number operators " + container, 1, container.getOperators().size());
Assert.assertEquals("operators " + container, o1Meta.getName(), container.getOperators().get(0).getOperatorMeta().getName());
inputOperators.add(container.getOperators().get(0));
}
for (int i = 2; i < 5; i++) {
PTContainer container = plan.getContainers().get(i);
Assert.assertEquals("number operators " + container, 2, container.getOperators().size());
Assert.assertEquals("operators " + container, o2Meta.getName(), container.getOperators().get(0).getOperatorMeta().getName());
Set<String> expectedLogicalNames = Sets.newHashSet(o1Meta.getMeta(o1.outport).getUnifierMeta().getName(), o2Meta.getName());
Map<String, PTOperator> actualOperators = new HashMap<>();
for (PTOperator p : container.getOperators()) {
actualOperators.put(p.getOperatorMeta().getName(), p);
}
Assert.assertEquals("", expectedLogicalNames, actualOperators.keySet());
PTOperator pUnifier = actualOperators.get(o1Meta.getMeta(o1.outport).getUnifierMeta().getName());
Assert.assertNotNull("" + pUnifier, pUnifier.getContainer());
Assert.assertTrue("" + pUnifier, pUnifier.isUnifier());
// input from each upstream partition
Assert.assertEquals("" + pUnifier, 2, pUnifier.getInputs().size());
int numberPartitionKeys = (i == 2) ? 2 : 1;
for (int inputIndex = 0; inputIndex < pUnifier.getInputs().size(); inputIndex++) {
PTInput input = pUnifier.getInputs().get(inputIndex);
Assert.assertEquals("" + pUnifier, "outport", input.source.portName);
Assert.assertEquals("" + pUnifier, inputOperators.get(inputIndex), input.source.source);
Assert.assertEquals("partition keys " + input.partitions, numberPartitionKeys, input.partitions.partitions.size());
}
// output to single downstream partition
Assert.assertEquals("" + pUnifier, 1, pUnifier.getOutputs().size());
Assert.assertTrue("" + actualOperators.get(o2Meta.getName()).getOperatorMeta().getOperator(), actualOperators.get(o2Meta.getName()).getOperatorMeta().getOperator() instanceof GenericTestOperator);
PTOperator p = actualOperators.get(o2Meta.getName());
Assert.assertEquals("partition inputs " + p.getInputs(), 1, p.getInputs().size());
Assert.assertEquals("partition inputs " + p.getInputs(), pUnifier, p.getInputs().get(0).source.source);
Assert.assertEquals("input partition keys " + p.getInputs(), null, p.getInputs().get(0).partitions);
Assert.assertTrue("partitioned unifier container local " + p.getInputs().get(0).source, p.getInputs().get(0).source.isDownStreamInline());
}
// scale down N from 3 to 2 and then from 2 to 1
for (int i = 0; i < 2; i++) {
List<PTOperator> ptos = plan.getOperators(o2Meta);
Set<PTOperator> expUndeploy = Sets.newHashSet(ptos);
for (PTOperator ptOperator : ptos) {
expUndeploy.addAll(ptOperator.upstreamMerge.values());
PartitioningTest.PartitionLoadWatch.put(ptOperator, -1);
plan.onStatusUpdate(ptOperator);
}
ctx.backupRequests = 0;
ctx.events.remove(0).run();
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(o2Meta));
// Either unifiers for each partition or single unifier for single partition is expected to be deployed
expDeploy.addAll(plan.getMergeOperators(o1Meta));
for (PTOperator ptOperator : plan.getOperators(o2Meta)) {
expDeploy.addAll(ptOperator.upstreamMerge.values());
}
Assert.assertEquals("number of containers", 4 - i, plan.getContainers().size());
Assert.assertEquals("number of operators", 2 - i, plan.getOperators(o2Meta).size());
Assert.assertEquals("undeployed operators " + ctx.undeploy, expUndeploy, ctx.undeploy);
Assert.assertEquals("deployed operators " + ctx.deploy, expDeploy, ctx.deploy);
}
// scale up N from 1 to 2 and then from 2 to 3
for (int i = 0; i < 2; i++) {
List<PTOperator> unChangedOps = new LinkedList<>(plan.getOperators(o2Meta));
PTOperator o2p1 = unChangedOps.remove(0);
Set<PTOperator> expUndeploy = Sets.newHashSet(o2p1);
// Either single unifier for one partition or merged unifiers for each partition is expected to be undeployed
expUndeploy.addAll(plan.getMergeOperators(o1Meta));
expUndeploy.addAll(o2p1.upstreamMerge.values());
List<PTOperator> nOps = new LinkedList<>();
for (Iterator<PTOperator> iterator = unChangedOps.iterator(); iterator.hasNext(); ) {
PTOperator ptOperator = iterator.next();
nOps.addAll(ptOperator.upstreamMerge.values());
}
unChangedOps.addAll(nOps);
PartitioningTest.PartitionLoadWatch.put(o2p1, 1);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("repartition event", 1, ctx.events.size());
ctx.backupRequests = 0;
ctx.events.remove(0).run();
Assert.assertEquals("N partitions after scale up " + o2Meta, 2 + i, plan.getOperators(o2Meta).size());
Assert.assertTrue("no unifiers", plan.getMergeOperators(o1Meta).isEmpty());
for (PTOperator o : plan.getOperators(o2Meta)) {
Assert.assertNotNull(o.container);
PTOperator unifier = o.upstreamMerge.values().iterator().next();
Assert.assertNotNull(unifier.container);
Assert.assertSame("unifier in same container", o.container, unifier.container);
Assert.assertEquals("container operators " + o.container, Sets.newHashSet(o.container.getOperators()), Sets.newHashSet(o, unifier));
}
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(o2Meta));
for (PTOperator ptOperator : plan.getOperators(o2Meta)) {
expDeploy.addAll(ptOperator.upstreamMerge.values());
}
expDeploy.removeAll(unChangedOps);
Assert.assertEquals("number of containers", 4 + i, plan.getContainers().size());
Assert.assertEquals("undeployed operators" + ctx.undeploy, expUndeploy, ctx.undeploy);
Assert.assertEquals("deployed operators" + ctx.deploy, expDeploy, ctx.deploy);
}
// scale down M to 1
{
Set<PTOperator> expUndeploy = Sets.newHashSet();
Set<PTOperator> expDeploy = Sets.newHashSet();
for (PTOperator o2p : plan.getOperators(o2Meta)) {
expUndeploy.addAll(o2p.upstreamMerge.values());
expUndeploy.add(o2p);
expDeploy.add(o2p);
}
for (PTOperator o1p : plan.getOperators(o1Meta)) {
expUndeploy.add(o1p);
PartitioningTest.PartitionLoadWatch.put(o1p, -1);
plan.onStatusUpdate(o1p);
}
Assert.assertEquals("repartition event", 1, ctx.events.size());
ctx.events.remove(0).run();
Assert.assertEquals("M partitions after scale down " + o1Meta, 1, plan.getOperators(o1Meta).size());
expUndeploy.removeAll(plan.getOperators(o1Meta));
for (PTOperator o2p : plan.getOperators(o2Meta)) {
Assert.assertTrue("merge unifier " + o2p + " " + o2p.upstreamMerge, o2p.upstreamMerge.isEmpty());
}
Assert.assertEquals("undeploy", expUndeploy, ctx.undeploy);
Assert.assertEquals("deploy", expDeploy, ctx.deploy);
}
// scale up M to 2
Assert.assertEquals("M partitions " + o1Meta, 1, plan.getOperators(o1Meta).size());
{
Set<PTOperator> expUndeploy = Sets.newHashSet();
Set<PTOperator> expDeploy = Sets.newHashSet();
for (PTOperator o1p : plan.getOperators(o1Meta)) {
PartitioningTest.PartitionLoadWatch.put(o1p, 1);
plan.onStatusUpdate(o1p);
}
Assert.assertEquals("repartition event", 1, ctx.events.size());
o1Partitioner.extraPartitions.add(new DefaultPartition<>(o1));
ctx.events.remove(0).run();
o1Partitioner.extraPartitions.clear();
List<PTOperator> o1Partitions = plan.getOperators(o1Meta);
List<PTOperator> o2Partitions = plan.getOperators(o2Meta);
Assert.assertEquals("M partitions after scale up " + o1Meta, 2, o1Partitions.size());
// previous partition unchanged
expDeploy.add(o1Partitions.get(1));
for (PTOperator o1p : o1Partitions) {
Assert.assertEquals("outputs " + o1p, 1, o1p.getOutputs().size());
Assert.assertEquals("sinks " + o1p, o2Partitions.size(), o1p.getOutputs().get(0).sinks.size());
}
for (PTOperator o2p : plan.getOperators(o2Meta)) {
expUndeploy.add(o2p);
expDeploy.add(o2p);
Assert.assertEquals("merge unifier " + o2p + " " + o2p.upstreamMerge, 1, o2p.upstreamMerge.size());
expDeploy.addAll(o2p.upstreamMerge.values());
}
Assert.assertEquals("undeploy", expUndeploy, ctx.undeploy);
Assert.assertEquals("deploy", expDeploy, ctx.deploy);
}
}
use of com.datatorrent.stram.plan.TestPlanContext in project apex-core by apache.
the class LogicalPlanModificationTest method testAddStream.
@Test
public void testAddStream() {
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
List<PTOperator> o1Instances = plan.getOperators(dag.getMeta(o1));
Assert.assertEquals("o1Instances " + o1Instances, 1, o1Instances.size());
PTOperator o1p1 = o1Instances.get(0);
OperatorMeta om2 = dag.getMeta(o2);
List<PTOperator> o2Instances = plan.getOperators(om2);
Assert.assertEquals("o2Instances " + o2Instances, 1, o2Instances.size());
PTOperator o2p1 = o2Instances.get(0);
Assert.assertEquals("outputs " + o1p1, 0, o1p1.getOutputs().size());
Assert.assertEquals("inputs " + o2p1, 0, o2p1.getInputs().size());
PlanModifier pm = new PlanModifier(plan);
pm.addStream("o1.outport1", o1.outport1, o2.inport1);
pm.addStream("o1.outport1", o1.outport1, o2.inport2);
pm.applyChanges(ctx);
Assert.assertEquals("undeploy " + ctx.undeploy, 2, ctx.undeploy.size());
Assert.assertEquals("deploy " + ctx.deploy, 2, ctx.deploy.size());
Assert.assertEquals("outputs " + o1p1, 1, o1p1.getOutputs().size());
Assert.assertEquals("inputs " + o2p1, 2, o2p1.getInputs().size());
Set<String> portNames = Sets.newHashSet();
for (PTOperator.PTInput in : o2p1.getInputs()) {
portNames.add(in.portName);
}
Set<String> expPortNames = Sets.newHashSet(GenericTestOperator.IPORT1, GenericTestOperator.IPORT2);
Assert.assertEquals("input port names " + o2p1.getInputs(), expPortNames, portNames);
}
use of com.datatorrent.stram.plan.TestPlanContext in project apex-core by apache.
the class PhysicalPlanTest method testAugmentedDynamicPartitioning.
/**
* Test covering scenario when only new partitions are added during dynamic partitioning and there
* are no changes to existing partitions and partition mapping
*/
@Test
public void testAugmentedDynamicPartitioning() {
LogicalPlan dag = new LogicalPlan();
TestGeneratorInputOperator o1 = dag.addOperator("o1", TestGeneratorInputOperator.class);
dag.setOperatorAttribute(o1, OperatorContext.PARTITIONER, new TestAugmentingPartitioner<TestGeneratorInputOperator>(3));
dag.setOperatorAttribute(o1, OperatorContext.STATS_LISTENERS, Lists.newArrayList((StatsListener) new PartitioningTest.PartitionLoadWatch()));
OperatorMeta o1Meta = dag.getMeta(o1);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
OperatorMeta o2Meta = dag.getMeta(o2);
dag.addStream("o1.outport1", o1.outport, o2.inport1);
int maxContainers = 10;
dag.setAttribute(LogicalPlan.CONTAINERS_MAX_COUNT, maxContainers);
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 4, plan.getContainers().size());
List<PTOperator> o1ops = plan.getOperators(o1Meta);
Assert.assertEquals("number of o1 operators", 3, o1ops.size());
List<PTOperator> o2ops = plan.getOperators(o2Meta);
Assert.assertEquals("number of o2 operators", 1, o2ops.size());
Set<PTOperator> expUndeploy = Sets.newLinkedHashSet();
expUndeploy.addAll(plan.getOperators(o2Meta));
expUndeploy.add(plan.getOperators(o2Meta).get(0).upstreamMerge.values().iterator().next());
for (int i = 0; i < 2; ++i) {
PartitioningTest.PartitionLoadWatch.put(o1ops.get(i), 1);
plan.onStatusUpdate(o1ops.get(i));
}
ctx.backupRequests = 0;
ctx.events.remove(0).run();
Assert.assertEquals("number of containers", 6, plan.getContainers().size());
Assert.assertEquals("undeployed opertors", expUndeploy, ctx.undeploy);
}
Aggregations