use of com.datatorrent.stram.engine.GenericTestOperator in project apex-core by apache.
the class PhysicalPlanTest method testContainersForSlidingWindow.
@Test
public void testContainersForSlidingWindow() {
LogicalPlan dag = new LogicalPlan();
dag.setAttribute(OperatorContext.STORAGE_AGENT, new StramTestSupport.MemoryStorageAgent());
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3 = dag.addOperator("o3", GenericTestOperator.class);
dag.setOperatorAttribute(o1, OperatorContext.APPLICATION_WINDOW_COUNT, 4);
dag.setOperatorAttribute(o1, OperatorContext.SLIDE_BY_WINDOW_COUNT, 2);
dag.getOperatorMeta("o1").getMeta(o1.outport1).getUnifierMeta().getAttributes().put(OperatorContext.MEMORY_MB, 2000);
dag.getOperatorMeta("o1").getMeta(o1.outport2).getUnifierMeta().getAttributes().put(OperatorContext.MEMORY_MB, 4000);
dag.addStream("o1.outport1", o1.outport1, o2.inport1);
dag.addStream("o1.outport2", o1.outport2, o2.inport2);
dag.addStream("o2.outport1", o2.outport1, o3.inport1);
PhysicalPlan plan = new PhysicalPlan(dag, new TestPlanContext());
Assert.assertEquals("number of containers", 5, plan.getContainers().size());
boolean sawOutput1Slider = false;
boolean sawOutput2Slider = false;
for (PTContainer container : plan.getContainers()) {
Assert.assertEquals("number of operators in each container is 1", container.operators.size(), 1);
if (container.operators.get(0).isUnifier()) {
String name = container.operators.get(0).getName();
if (name.equals("o1.outport1#slider")) {
sawOutput1Slider = true;
Assert.assertEquals("container memory is 2512", container.getRequiredMemoryMB(), 2512);
} else if (name.equals("o1.outport2#slider")) {
sawOutput2Slider = true;
Assert.assertEquals("container memory is 2512", container.getRequiredMemoryMB(), 4512);
}
}
}
Assert.assertEquals("Found output1 slider", true, sawOutput1Slider);
Assert.assertEquals("Found output2 slider", true, sawOutput2Slider);
}
use of com.datatorrent.stram.engine.GenericTestOperator in project apex-core by apache.
the class PhysicalPlanTest method testNodeLocality.
@Test
public void testNodeLocality() {
LogicalPlan dag = new LogicalPlan();
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator partitioned = dag.addOperator("partitioned", GenericTestOperator.class);
dag.getMeta(partitioned).getAttributes().put(OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(2));
GenericTestOperator partitionedParallel = dag.addOperator("partitionedParallel", GenericTestOperator.class);
dag.addStream("o1_outport1", o1.outport1, partitioned.inport1).setLocality(null);
dag.addStream("partitioned_outport1", partitioned.outport1, partitionedParallel.inport2).setLocality(Locality.NODE_LOCAL);
dag.setInputPortAttribute(partitionedParallel.inport2, PortContext.PARTITION_PARALLEL, true);
GenericTestOperator single = dag.addOperator("single", GenericTestOperator.class);
dag.addStream("partitionedParallel_outport1", partitionedParallel.outport1, single.inport1);
int maxContainers = 6;
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());
PTContainer container1 = plan.getContainers().get(0);
Assert.assertEquals("number operators " + container1, 1, container1.getOperators().size());
Assert.assertEquals("operators " + container1, dag.getMeta(o1), container1.getOperators().get(0).getOperatorMeta());
for (int i = 1; i < 3; i++) {
PTContainer c = plan.getContainers().get(i);
Assert.assertEquals("number operators " + c, 1, c.getOperators().size());
Set<OperatorMeta> expectedLogical = Sets.newHashSet(dag.getMeta(partitioned));
Set<OperatorMeta> actualLogical = Sets.newHashSet();
for (PTOperator p : c.getOperators()) {
actualLogical.add(p.getOperatorMeta());
}
Assert.assertEquals("operators " + c, expectedLogical, actualLogical);
}
// in-node parallel partition
for (int i = 3; i < 5; i++) {
PTContainer c = plan.getContainers().get(i);
Assert.assertEquals("number operators " + c, 1, c.getOperators().size());
Set<OperatorMeta> expectedLogical = Sets.newHashSet(dag.getMeta(partitionedParallel));
Set<OperatorMeta> actualLogical = Sets.newHashSet();
for (PTOperator p : c.getOperators()) {
actualLogical.add(p.getOperatorMeta());
Assert.assertEquals("nodeLocal " + p.getNodeLocalOperators(), 2, p.getNodeLocalOperators().getOperatorSet().size());
}
Assert.assertEquals("operators " + c, expectedLogical, actualLogical);
}
}
use of com.datatorrent.stram.engine.GenericTestOperator in project apex-core by apache.
the class StramLocalClusterTest method testRecovery.
@Test
@SuppressWarnings("SleepWhileInLoop")
public void testRecovery() throws Exception {
AsyncFSStorageAgent agent = new AsyncFSStorageAgent(testMeta.getPath(), null);
agent.setSyncCheckpoint(true);
dag.setAttribute(OperatorContext.STORAGE_AGENT, agent);
TestGeneratorInputOperator node1 = dag.addOperator("o1", TestGeneratorInputOperator.class);
// data will be added externally from test
node1.setMaxTuples(0);
GenericTestOperator node2 = dag.addOperator("o2", GenericTestOperator.class);
dag.addStream("o1o2", node1.outport, node2.inport1);
dag.validate();
dag.getAttributes().put(LogicalPlan.CHECKPOINT_WINDOW_COUNT, 2);
final ManualScheduledExecutorService wclock = new ManualScheduledExecutorService(1);
MockComponentFactory mcf = new MockComponentFactory() {
@Override
public WindowGenerator setupWindowGenerator() {
WindowGenerator wingen = StramTestSupport.setupWindowGenerator(wclock);
wingen.setCheckpointCount(2, 0);
return wingen;
}
};
StramLocalCluster localCluster = new StramLocalCluster(dag, mcf);
localCluster.setPerContainerBufferServer(true);
// driven by test
localCluster.setHeartbeatMonitoringEnabled(false);
localCluster.runAsync();
PTOperator ptNode1 = localCluster.findByLogicalNode(dag.getMeta(node1));
PTOperator ptNode2 = localCluster.findByLogicalNode(dag.getMeta(node2));
LocalStreamingContainer c0 = StramTestSupport.waitForActivation(localCluster, ptNode1);
Map<Integer, Node<?>> nodeMap = c0.getNodes();
Assert.assertEquals("number operators", 1, nodeMap.size());
TestGeneratorInputOperator n1 = (TestGeneratorInputOperator) nodeMap.get(ptNode1.getId()).getOperator();
Assert.assertNotNull(n1);
LocalStreamingContainer c2 = StramTestSupport.waitForActivation(localCluster, ptNode2);
Map<Integer, Node<?>> c2NodeMap = c2.getNodes();
Assert.assertEquals("number operators downstream", 1, c2NodeMap.size());
GenericTestOperator n2 = (GenericTestOperator) c2NodeMap.get(localCluster.findByLogicalNode(dag.getMeta(node2)).getId()).getOperator();
Assert.assertNotNull(n2);
// input data
String tuple1 = "tuple1";
n1.addTuple(tuple1);
OperatorContext n1Context = c0.getNodeContext(ptNode1.getId());
Assert.assertEquals("initial window id", -1, n1Context.getLastProcessedWindowId());
// checkpoint window
wclock.tick(1);
wclock.tick(1);
Assert.assertEquals("current window", 2, wclock.getCurrentTimeMillis());
OperatorContext o2Context = c2.getNodeContext(ptNode2.getId());
Assert.assertNotNull("context ", o2Context);
StramTestSupport.waitForWindowComplete(o2Context, 1);
Assert.assertEquals("o2 received ", tuple1, n2.inport1Tuple);
wclock.tick(1);
Assert.assertEquals("current window", 3, wclock.getCurrentTimeMillis());
// checkpoint between window 1 and 2
StramTestSupport.waitForWindowComplete(o2Context, 2);
// propagate checkpoints to master
c0.triggerHeartbeat();
// wait for heartbeat cycle to complete
c0.waitForHeartbeat(5000);
Assert.assertEquals("checkpoint " + ptNode1, 1, ptNode1.getRecentCheckpoint().windowId);
c2.triggerHeartbeat();
// Thread.yield();
// yield without using yield for heartbeat cycle
Thread.sleep(1);
c2.waitForHeartbeat(5000);
Assert.assertEquals("checkpoint " + ptNode2, 1, ptNode2.getRecentCheckpoint().windowId);
Assert.assertEquals("checkpoints " + ptNode1, Arrays.asList(new Checkpoint[] { new Checkpoint(1L, 0, 0) }), ptNode1.checkpoints);
Assert.assertEquals("checkpoints " + ptNode2, Arrays.asList(new Checkpoint[] { new Checkpoint(1L, 0, 0) }), ptNode2.checkpoints);
//
// simulate container failure (operator o1)
//
localCluster.failContainer(c0);
// replacement container starts empty
// operators will deploy after downstream operator was removed
LocalStreamingContainer c0Replaced = StramTestSupport.waitForActivation(localCluster, ptNode1);
c0Replaced.triggerHeartbeat();
// next heartbeat after setup
c0Replaced.waitForHeartbeat(5000);
Assert.assertNotSame("old container", c0, c0Replaced);
Assert.assertNotSame("old container", c0.getContainerId(), c0Replaced.getContainerId());
// verify change in downstream container
LOG.debug("triggering c2 heartbeat processing");
StreamingContainerAgent c2Agent = localCluster.getContainerAgent(c2);
// wait for downstream re-deploy to complete
long startTms = System.currentTimeMillis();
while (c2Agent.hasPendingWork() && StramTestSupport.DEFAULT_TIMEOUT_MILLIS > System.currentTimeMillis() - startTms) {
Thread.sleep(200);
c2.triggerHeartbeat();
LOG.debug("Waiting for {} to complete pending work.", c2.getContainerId());
}
Assert.assertEquals(c2.getContainerId() + " operators after redeploy " + c2.getNodes(), 1, c2.getNodes().size());
// verify downstream operator re-deployed in existing container
Assert.assertEquals("active " + ptNode2, c2, StramTestSupport.waitForActivation(localCluster, ptNode2));
GenericTestOperator o2Recovered = (GenericTestOperator) c2NodeMap.get(localCluster.findByLogicalNode(dag.getMeta(node2)).getId()).getOperator();
Assert.assertNotNull("redeployed " + ptNode2, o2Recovered);
Assert.assertNotSame("new instance " + ptNode2, n2, o2Recovered);
Assert.assertEquals("restored state " + ptNode2, tuple1, o2Recovered.inport1Tuple);
TestGeneratorInputOperator o1Recovered = (TestGeneratorInputOperator) c0Replaced.getNodes().get(ptNode1.getId()).getOperator();
Assert.assertNotNull(o1Recovered);
OperatorContext o1RecoveredContext = c0Replaced.getNodeContext(ptNode1.getId());
Assert.assertNotNull("active " + ptNode1, o1RecoveredContext);
wclock.tick(1);
Assert.assertEquals("current window", 4, wclock.getCurrentTimeMillis());
// refresh context after operator re-deploy
o2Context = c2.getNodeContext(ptNode2.getId());
Assert.assertNotNull("active " + ptNode2, o2Context);
StramTestSupport.waitForWindowComplete(o1RecoveredContext, 3);
StramTestSupport.waitForWindowComplete(o2Context, 3);
// checkpoint window
wclock.tick(1);
Assert.assertEquals("current window", 5, wclock.getCurrentTimeMillis());
String tuple2 = "tuple2";
o1Recovered.addTuple(tuple2);
StramTestSupport.waitForWindowComplete(o1RecoveredContext, 4);
StramTestSupport.waitForWindowComplete(o2Context, 4);
// check data flow after recovery
Assert.assertEquals("retrieved tuple (after recovery) " + ptNode2, tuple2, o2Recovered.inport1Tuple);
// propagate checkpoints to master
c0Replaced.triggerHeartbeat();
c0Replaced.waitForHeartbeat(5000);
c2.triggerHeartbeat();
c2.waitForHeartbeat(5000);
// purge checkpoints
// checkpoint purging
localCluster.dnmgr.monitorHeartbeat(false);
Assert.assertEquals("checkpoints " + ptNode1, Arrays.asList(new Checkpoint[] { new Checkpoint(3L, 0, 0) }), ptNode1.checkpoints);
Assert.assertEquals("checkpoints " + ptNode2, Arrays.asList(new Checkpoint[] { new Checkpoint(3L, 0, 0) }), ptNode2.checkpoints);
localCluster.shutdown();
}
use of com.datatorrent.stram.engine.GenericTestOperator in project apex-core by apache.
the class PhysicalPlanTest method testRepartitioningScaleUp.
@Test
public void testRepartitioningScaleUp() {
LogicalPlan dag = new LogicalPlan();
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator mergeNode = dag.addOperator("mergeNode", GenericTestOperator.class);
dag.addStream("o1.outport1", o1.outport1, o2.inport1, o2.inport2);
dag.addStream("mergeStream", o2.outport1, mergeNode.inport1);
OperatorMeta o2Meta = dag.getMeta(o2);
o2Meta.getAttributes().put(OperatorContext.STATS_LISTENERS, Lists.newArrayList((StatsListener) new PartitionLoadWatch(0, 5)));
o2Meta.getAttributes().put(OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(1));
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of operators", 3, plan.getAllOperators().size());
Assert.assertEquals("number of save requests", 3, ctx.backupRequests);
List<PTOperator> o2Partitions = plan.getOperators(o2Meta);
Assert.assertEquals("partition count " + o2Meta, 1, o2Partitions.size());
PTOperator o2p1 = o2Partitions.get(0);
Assert.assertEquals("stats handlers " + o2p1, 1, o2p1.statsListeners.size());
StatsListener sl = o2p1.statsListeners.get(0);
Assert.assertTrue("stats handlers " + o2p1.statsListeners, sl instanceof PartitionLoadWatch);
// no delay
((PartitionLoadWatch) sl).evalIntervalMillis = -1;
setThroughput(o2p1, 10);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("partitioning triggered", 1, ctx.events.size());
ctx.backupRequests = 0;
ctx.events.remove(0).run();
o2Partitions = plan.getOperators(o2Meta);
Assert.assertEquals("partition count " + o2Partitions, 2, o2Partitions.size());
o2p1 = o2Partitions.get(0);
Assert.assertEquals("sinks " + o2p1.getOutputs(), 1, o2p1.getOutputs().size());
PTOperator o2p2 = o2Partitions.get(1);
Assert.assertEquals("sinks " + o2p2.getOutputs(), 1, o2p2.getOutputs().size());
Set<PTOperator> expUndeploy = Sets.newHashSet(plan.getOperators(dag.getMeta(mergeNode)));
expUndeploy.add(o2p1);
expUndeploy.addAll(plan.getOperators(dag.getMeta(mergeNode)).get(0).upstreamMerge.values());
// verify load update generates expected events per configuration
setThroughput(o2p1, 0);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("load min", 0, ctx.events.size());
setThroughput(o2p1, 3);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("load within range", 0, ctx.events.size());
setThroughput(o2p1, 10);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("load exceeds max", 1, ctx.events.size());
ctx.backupRequests = 0;
ctx.events.remove(0).run();
Assert.assertEquals("new partitions", 3, plan.getOperators(o2Meta).size());
Assert.assertTrue("", plan.getOperators(o2Meta).contains(o2p2));
for (PTOperator partition : plan.getOperators(o2Meta)) {
Assert.assertNotNull("container null " + partition, partition.getContainer());
Assert.assertEquals("outputs " + partition, 1, partition.getOutputs().size());
Assert.assertEquals("downstream operators " + partition.getOutputs().get(0).sinks, 1, partition.getOutputs().get(0).sinks.size());
}
Assert.assertEquals("" + ctx.undeploy, expUndeploy, ctx.undeploy);
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(dag.getMeta(mergeNode)));
expDeploy.addAll(plan.getOperators(o2Meta));
expDeploy.remove(o2p2);
expDeploy.addAll(plan.getOperators(dag.getMeta(mergeNode)).get(0).upstreamMerge.values());
Assert.assertEquals("" + ctx.deploy, expDeploy, ctx.deploy);
Assert.assertEquals("Count of storage requests", 2, ctx.backupRequests);
// partitioning skipped on insufficient head room
o2p1 = plan.getOperators(o2Meta).get(0);
plan.setAvailableResources(0);
setThroughput(o2p1, 10);
plan.onStatusUpdate(o2p1);
Assert.assertEquals("not repartitioned", 1, ctx.events.size());
ctx.events.remove(0).run();
Assert.assertEquals("partition count unchanged", 3, plan.getOperators(o2Meta).size());
}
use of com.datatorrent.stram.engine.GenericTestOperator in project apex-core by apache.
the class PhysicalPlanTest method testRepartitioningScaleDown.
@Test
public void testRepartitioningScaleDown() {
LogicalPlan dag = new LogicalPlan();
GenericTestOperator o1 = dag.addOperator("o1", GenericTestOperator.class);
GenericTestOperator o2 = dag.addOperator("o2", GenericTestOperator.class);
GenericTestOperator o3parallel = dag.addOperator("o3parallel", GenericTestOperator.class);
OperatorMeta o3Meta = dag.getMeta(o3parallel);
GenericTestOperator mergeNode = dag.addOperator("mergeNode", GenericTestOperator.class);
dag.addStream("o1.outport1", o1.outport1, o2.inport1, o2.inport2);
dag.addStream("o2.outport1", o2.outport1, o3parallel.inport1).setLocality(Locality.CONTAINER_LOCAL);
dag.setInputPortAttribute(o3parallel.inport1, PortContext.PARTITION_PARALLEL, true);
dag.addStream("o3parallel_outport1", o3parallel.outport1, mergeNode.inport1);
dag.getAttributes().put(LogicalPlan.CONTAINERS_MAX_COUNT, 2);
OperatorMeta node2Meta = dag.getMeta(o2);
node2Meta.getAttributes().put(OperatorContext.STATS_LISTENERS, Lists.newArrayList((StatsListener) new PartitionLoadWatch(3, 5)));
node2Meta.getAttributes().put(OperatorContext.PARTITIONER, new StatelessPartitioner<GenericTestOperator>(8));
TestPlanContext ctx = new TestPlanContext();
dag.setAttribute(OperatorContext.STORAGE_AGENT, ctx);
PhysicalPlan plan = new PhysicalPlan(dag, ctx);
Assert.assertEquals("number of containers", 2, plan.getContainers().size());
Assert.assertEquals("Count of storage requests", plan.getAllOperators().size(), ctx.backupRequests);
List<PTOperator> n2Instances = plan.getOperators(node2Meta);
Assert.assertEquals("partition instances " + n2Instances, 8, n2Instances.size());
PTOperator po = n2Instances.get(0);
Collection<PTOperator> unifiers = plan.getMergeOperators(node2Meta);
Assert.assertEquals("unifiers " + node2Meta, 0, unifiers.size());
Collection<PTOperator> o3unifiers = plan.getOperators(dag.getMeta(mergeNode)).get(0).upstreamMerge.values();
Assert.assertEquals("unifiers " + o3Meta, 1, o3unifiers.size());
PTOperator o3unifier = o3unifiers.iterator().next();
Assert.assertEquals("unifier inputs " + o3unifier, 8, o3unifier.getInputs().size());
Set<PTOperator> expUndeploy = Sets.newHashSet(plan.getOperators(dag.getMeta(mergeNode)));
expUndeploy.addAll(n2Instances);
expUndeploy.addAll(plan.getOperators(o3Meta));
expUndeploy.addAll(o3unifiers);
// verify load update generates expected events per configuration
Assert.assertEquals("stats handlers " + po, 1, po.statsListeners.size());
StatsListener l = po.statsListeners.get(0);
Assert.assertTrue("stats handlers " + po.statsListeners, l instanceof PartitionLoadWatch);
// no delay
((PartitionLoadWatch) l).evalIntervalMillis = -1;
setThroughput(po, 5);
plan.onStatusUpdate(po);
Assert.assertEquals("load upper bound", 0, ctx.events.size());
setThroughput(po, 3);
plan.onStatusUpdate(po);
Assert.assertEquals("load lower bound", 0, ctx.events.size());
setThroughput(po, 2);
plan.onStatusUpdate(po);
Assert.assertEquals("load below min", 1, ctx.events.size());
ctx.backupRequests = 0;
ctx.events.remove(0).run();
// expect operators unchanged
Assert.assertEquals("partitions unchanged", Sets.newHashSet(n2Instances), Sets.newHashSet(plan.getOperators(node2Meta)));
for (PTOperator o : n2Instances) {
setThroughput(o, 2);
plan.onStatusUpdate(o);
}
Assert.assertEquals("load below min", 1, ctx.events.size());
ctx.events.remove(0).run();
Assert.assertEquals("partitions merged", 4, plan.getOperators(node2Meta).size());
Assert.assertEquals("unifier inputs after scale down " + o3unifier, 4, o3unifier.getInputs().size());
for (PTOperator p : plan.getOperators(o3Meta)) {
Assert.assertEquals("outputs " + p.getOutputs(), 1, p.getOutputs().size());
}
for (PTOperator p : plan.getOperators(node2Meta)) {
PartitionKeys pks = p.getPartitionKeys().values().iterator().next();
Assert.assertEquals("partition mask " + p, 3, pks.mask);
Assert.assertEquals("inputs " + p, 2, p.getInputs().size());
boolean portConnected = false;
for (PTInput input : p.getInputs()) {
if (GenericTestOperator.IPORT1.equals(input.portName)) {
portConnected = true;
Assert.assertEquals("partition mask " + input, pks, input.partitions);
}
}
Assert.assertTrue("connected " + GenericTestOperator.IPORT1, portConnected);
}
Assert.assertEquals("" + ctx.undeploy, expUndeploy, ctx.undeploy);
o3unifiers = plan.getOperators(dag.getMeta(mergeNode)).get(0).upstreamMerge.values();
Set<PTOperator> expDeploy = Sets.newHashSet(plan.getOperators(dag.getMeta(mergeNode)));
expDeploy.addAll(plan.getOperators(node2Meta));
expDeploy.addAll(plan.getOperators(o3Meta));
expDeploy.addAll(o3unifiers);
Assert.assertEquals("" + ctx.deploy, expDeploy, ctx.deploy);
for (PTOperator oper : ctx.deploy) {
Assert.assertNotNull("container " + oper, oper.getContainer());
}
Assert.assertEquals("Count of storage requests", 8, ctx.backupRequests);
}
Aggregations