use of com.datatorrent.stram.plan.logical.LogicalPlan 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);
}
use of com.datatorrent.stram.plan.logical.LogicalPlan in project apex-core by apache.
the class PhysicalPlanTest method testDefaultPartitioning.
@Test
public void testDefaultPartitioning() {
LogicalPlan dag = new LogicalPlan();
dag.setAttribute(OperatorContext.STORAGE_AGENT, new StramTestSupport.MemoryStorageAgent());
GenericTestOperator node1 = dag.addOperator("node1", GenericTestOperator.class);
GenericTestOperator node2 = dag.addOperator("node2", GenericTestOperator.class);
dag.addStream("node1.outport1", node1.outport1, node2.inport2, node2.inport1);
int initialPartitionCount = 5;
OperatorMeta node2Decl = dag.getMeta(node2);
node2Decl.getAttributes().put(OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(initialPartitionCount));
PhysicalPlan plan = new PhysicalPlan(dag, new TestPlanContext());
List<PTOperator> n2Instances = plan.getOperators(node2Decl);
Assert.assertEquals("partition instances " + n2Instances, initialPartitionCount, n2Instances.size());
List<Integer> assignedPartitionKeys = Lists.newArrayList();
for (int i = 0; i < n2Instances.size(); i++) {
PTOperator n2Partition = n2Instances.get(i);
Assert.assertNotNull("partition keys null: " + n2Partition, n2Partition.getPartitionKeys());
Map<InputPort<?>, PartitionKeys> pkeys = n2Partition.getPartitionKeys();
// one port partitioned
Assert.assertEquals("partition keys size: " + pkeys, 1, pkeys.size());
InputPort<?> expectedPort = node2.inport2;
Assert.assertEquals("partition port: " + pkeys, expectedPort, pkeys.keySet().iterator().next());
Assert.assertEquals("partition mask: " + pkeys, "111", Integer.toBinaryString(pkeys.get(expectedPort).mask));
Set<Integer> pks = pkeys.get(expectedPort).partitions;
Assert.assertTrue("number partition keys: " + pkeys, pks.size() == 1 || pks.size() == 2);
assignedPartitionKeys.addAll(pks);
}
int expectedMask = Integer.parseInt("111", 2);
Assert.assertEquals("assigned partitions ", expectedMask + 1, assignedPartitionKeys.size());
for (int i = 0; i <= expectedMask; i++) {
Assert.assertTrue("" + assignedPartitionKeys, assignedPartitionKeys.contains(i));
}
}
use of com.datatorrent.stram.plan.logical.LogicalPlan in project apex-core by apache.
the class PhysicalPlanTest method testParallelPartitioningValidation.
@Test
public void testParallelPartitioningValidation() {
LogicalPlan dag = new LogicalPlan();
dag.setAttribute(OperatorContext.STORAGE_AGENT, new TestPlanContext());
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
dag.setInputPortAttribute(o3.inport1, PortContext.PARTITION_PARALLEL, true);
dag.setInputPortAttribute(o3.inport2, PortContext.PARTITION_PARALLEL, true);
dag.addStream("o1Output1", o1.outport1, o3.inport1);
dag.addStream("o2Output1", o2.outport1, o3.inport2);
try {
new PhysicalPlan(dag, new TestPlanContext());
} catch (AssertionError ae) {
Assert.assertThat("Parallel partition needs common ancestor", ae.getMessage(), RegexMatcher.matches("operator cannot extend multiple partitions.*"));
}
GenericTestOperator commonAncestor = dag.addOperator("commonAncestor", GenericTestOperator.class);
dag.setInputPortAttribute(o1.inport1, PortContext.PARTITION_PARALLEL, true);
dag.setInputPortAttribute(o2.inport1, PortContext.PARTITION_PARALLEL, true);
dag.addStream("commonAncestor.outport1", commonAncestor.outport1, o1.inport1);
dag.addStream("commonAncestor.outport2", commonAncestor.outport2, o2.inport1);
new PhysicalPlan(dag, new TestPlanContext());
// two operators with multiple streams
dag = new LogicalPlan();
dag.setAttribute(OperatorContext.STORAGE_AGENT, new TestPlanContext());
o1 = dag.addOperator("o1", GenericTestOperator.class);
o2 = dag.addOperator("o2", GenericTestOperator.class);
dag.setInputPortAttribute(o2.inport1, PortContext.PARTITION_PARALLEL, true);
dag.setInputPortAttribute(o2.inport2, PortContext.PARTITION_PARALLEL, true);
dag.addStream("o1.outport1", o1.outport1, o2.inport1);
dag.addStream("o2.outport2", o1.outport2, o2.inport2);
new PhysicalPlan(dag, new TestPlanContext());
}
use of com.datatorrent.stram.plan.logical.LogicalPlan in project apex-core by apache.
the class PhysicalPlanTest method testSingleFinalMxNPartitioning.
/**
* 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 testSingleFinalMxNPartitioning() {
LogicalPlan dag = new LogicalPlan();
TestGeneratorInputOperator o1 = dag.addOperator("o1", TestGeneratorInputOperator.class);
dag.setOperatorAttribute(o1, OperatorContext.PARTITIONER, new StatelessPartitioner<TestGeneratorInputOperator>(2));
dag.setOperatorAttribute(o1, OperatorContext.STATS_LISTENERS, Lists.newArrayList((StatsListener) new PartitioningTest.PartitionLoadWatch()));
dag.setOutputPortAttribute(o1.outport, PortContext.UNIFIER_SINGLE_FINAL, true);
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);
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", 6, 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));
}
PTOperator inputUnifier = null;
{
PTContainer container = plan.getContainers().get(2);
Assert.assertEquals("number operators " + container, 1, container.getOperators().size());
PTOperator pUnifier = container.getOperators().get(0);
Assert.assertEquals("operators " + container, o1Meta.getMeta(o1.outport).getUnifierMeta().getName(), pUnifier.getOperatorMeta().getName());
Assert.assertTrue("single unifier " + pUnifier, pUnifier.isUnifier());
Assert.assertEquals("" + pUnifier, 2, pUnifier.getInputs().size());
for (int inputIndex = 0; inputIndex < pUnifier.getInputs().size(); inputIndex++) {
PTInput input = pUnifier.getInputs().get(inputIndex);
Assert.assertEquals("source port name " + pUnifier, "outport", input.source.portName);
Assert.assertEquals("" + pUnifier, inputOperators.get(inputIndex), input.source.source);
Assert.assertEquals("partition keys " + input.partitions, null, input.partitions);
}
Assert.assertEquals("number outputs " + pUnifier, 1, pUnifier.getOutputs().size());
PTOutput output = pUnifier.getOutputs().get(0);
Assert.assertEquals("number inputs " + output, 3, output.sinks.size());
for (int inputIndex = 0; inputIndex < output.sinks.size(); ++inputIndex) {
Assert.assertEquals("output sink " + output, o2Meta.getName(), output.sinks.get(inputIndex).target.getName());
Assert.assertEquals("destination port name " + output, GenericTestOperator.IPORT1, output.sinks.get(inputIndex).portName);
}
inputUnifier = pUnifier;
}
List<Integer> partitionKeySizes = new ArrayList<>();
for (int i = 3; i < 6; i++) {
PTContainer container = plan.getContainers().get(i);
Assert.assertEquals("number operators " + container, 1, container.getOperators().size());
Assert.assertEquals("operators " + container, o2Meta.getName(), container.getOperators().get(0).getOperatorMeta().getName());
PTOperator operator = container.getOperators().get(0);
Assert.assertEquals("operators " + container, o2Meta.getName(), operator.getOperatorMeta().getName());
Assert.assertEquals("number inputs " + operator, 1, operator.getInputs().size());
PTInput input = operator.getInputs().get(0);
Assert.assertEquals("" + operator, inputUnifier, input.source.source);
Assert.assertNotNull("input partitions " + operator, input.partitions);
partitionKeySizes.add(input.partitions.partitions.size());
}
Assert.assertEquals("input partition sizes count", 3, partitionKeySizes.size());
Collections.sort(partitionKeySizes);
Assert.assertEquals("input partition sizes", Arrays.asList(1, 1, 2), partitionKeySizes);
// 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());
expUndeploy.add(ptOperator);
PartitioningTest.PartitionLoadWatch.put(ptOperator, -1);
plan.onStatusUpdate(ptOperator);
}
ctx.backupRequests = 0;
ctx.events.remove(0).run();
Assert.assertEquals("single unifier ", 1, plan.getMergeOperators(o1Meta).size());
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(o2Meta));
// The unifier and o2 operators are expected to be deployed because of partition key changes
for (PTOperator ptOperator : plan.getOperators(o2Meta)) {
expDeploy.add(ptOperator);
}
// from 3 to 2 the containers decrease from 5 to 4, but from 2 to 1 the container remains same because single unifier are not inline with single operator partition
Assert.assertEquals("number of containers", 5 - 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);
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("single unifier ", 1, plan.getMergeOperators(o1Meta).size());
Assert.assertEquals("N partitions after scale up " + o2Meta, 2 + i, plan.getOperators(o2Meta).size());
for (PTOperator o : plan.getOperators(o2Meta)) {
Assert.assertNotNull(o.container);
Assert.assertEquals("number operators ", 1, o.container.getOperators().size());
}
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(o2Meta));
expDeploy.removeAll(unChangedOps);
Assert.assertEquals("number of containers", 5 + 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();
expUndeploy.addAll(plan.getMergeOperators(o1Meta));
for (PTOperator o2p : plan.getOperators(o2Meta)) {
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));
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)) {
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 up " + o1Meta, 2, plan.getOperators(o1Meta).size());
expDeploy.addAll(plan.getOperators(o1Meta));
expDeploy.addAll(plan.getMergeOperators(o1Meta));
for (PTOperator o2p : plan.getOperators(o2Meta)) {
expUndeploy.add(o2p);
expDeploy.add(o2p);
Assert.assertNotNull(o2p.container);
Assert.assertEquals("number operators ", 1, o2p.container.getOperators().size());
}
Assert.assertEquals("undeploy", expUndeploy, ctx.undeploy);
Assert.assertEquals("deploy", expDeploy, ctx.deploy);
}
}
use of com.datatorrent.stram.plan.logical.LogicalPlan in project apex-core by apache.
the class PhysicalPlanTest method testInline.
@Test
@SuppressWarnings("unchecked")
public void testInline() {
LogicalPlan dag = new LogicalPlan();
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
PartitioningTestOperator partOperator = dag.addOperator("partNode", PartitioningTestOperator.class);
partOperator.partitionKeys = new Integer[] { 0, 1 };
dag.getMeta(partOperator).getAttributes().put(OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(partOperator.partitionKeys.length));
dag.addStream("o1_outport1", o1.outport1, o2.inport1, o3.inport1, partOperator.inport1).setLocality(null);
// same container for o2 and o3
dag.addStream("o2_outport1", o2.outport1, o3.inport2).setLocality(Locality.CONTAINER_LOCAL);
dag.addStream("o3_outport1", o3.outport1, partOperator.inport2);
int maxContainers = 4;
dag.setAttribute(LogicalPlan.CONTAINERS_MAX_COUNT, maxContainers);
dag.setAttribute(OperatorContext.STORAGE_AGENT, new TestPlanContext());
PhysicalPlan plan = new PhysicalPlan(dag, new TestPlanContext());
Assert.assertEquals("number of containers", maxContainers, plan.getContainers().size());
Assert.assertEquals("operators container 0", 1, plan.getContainers().get(0).getOperators().size());
Assert.assertEquals("operators container 0", 1, plan.getContainers().get(0).getOperators().size());
Set<OperatorMeta> c2ExpNodes = Sets.newHashSet(dag.getMeta(o2), dag.getMeta(o3));
Set<OperatorMeta> c2ActNodes = new HashSet<>();
PTContainer c2 = plan.getContainers().get(1);
for (PTOperator pNode : c2.getOperators()) {
c2ActNodes.add(pNode.getOperatorMeta());
}
Assert.assertEquals("operators " + c2, c2ExpNodes, c2ActNodes);
// one container per partition
OperatorMeta partOperMeta = dag.getMeta(partOperator);
List<PTOperator> partitions = plan.getOperators(partOperMeta);
for (PTOperator partition : partitions) {
Assert.assertEquals("operators container" + partition, 1, partition.getContainer().getOperators().size());
}
}
Aggregations