Search in sources :

Example 1 with ScheduledExecutorService

use of com.datatorrent.common.util.ScheduledExecutorService in project apex-core by apache.

the class GenericNodeTest method testCheckpointDistance.

private void testCheckpointDistance(int dagCheckPoint, int opCheckPoint) throws InterruptedException {
    int windowWidth = 50;
    long sleeptime = 25L;
    int maxWindows = 60;
    // Adding some extra time for the windows to finish
    long maxSleep = windowWidth * maxWindows + 5000;
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, "default");
    final WindowGenerator windowGenerator = new WindowGenerator(executorService, 1024);
    windowGenerator.setWindowWidth(windowWidth);
    windowGenerator.setFirstWindow(executorService.getCurrentTimeMillis());
    windowGenerator.setCheckpointCount(dagCheckPoint, 0);
    //GenericOperator go = new GenericOperator();
    CheckpointDistanceOperator go = new CheckpointDistanceOperator();
    go.maxWindows = maxWindows;
    List<Integer> checkpoints = new ArrayList<>();
    int window = 0;
    while (window < maxWindows) {
        window = (int) Math.ceil((double) (window + 1) / dagCheckPoint) * dagCheckPoint;
        window = (int) Math.ceil((double) window / opCheckPoint) * opCheckPoint;
        checkpoints.add(window);
    }
    final StreamContext stcontext = new StreamContext("s1");
    DefaultAttributeMap attrMap = new DefaultAttributeMap();
    attrMap.put(Context.DAGContext.CHECKPOINT_WINDOW_COUNT, dagCheckPoint);
    attrMap.put(Context.OperatorContext.CHECKPOINT_WINDOW_COUNT, opCheckPoint);
    final OperatorContext context = new com.datatorrent.stram.engine.OperatorContext(0, "operator", attrMap, null);
    final GenericNode gn = new GenericNode(go, context);
    gn.setId(1);
    //DefaultReservoir reservoir1 = new DefaultReservoir("ip1Res", 1024);
    //DefaultReservoir reservoir2 = new DefaultReservoir("ip2Res", 1024);
    //gn.connectInputPort("ip1", reservoir1);
    //gn.connectInputPort("ip2", reservoir2);
    gn.connectInputPort("ip1", windowGenerator.acquireReservoir("ip1", 1024));
    gn.connectInputPort("ip2", windowGenerator.acquireReservoir("ip2", 1024));
    gn.connectOutputPort("op", Sink.BLACKHOLE);
    final AtomicBoolean ab = new AtomicBoolean(false);
    Thread t = new Thread() {

        @Override
        public void run() {
            gn.setup(context);
            windowGenerator.activate(stcontext);
            gn.activate();
            ab.set(true);
            gn.run();
            windowGenerator.deactivate();
            gn.deactivate();
            gn.teardown();
        }
    };
    t.start();
    long interval = 0;
    do {
        Thread.sleep(sleeptime);
        interval += sleeptime;
    } while ((go.numWindows < maxWindows) && (interval < maxSleep));
    Assert.assertEquals("Number distances", maxWindows, go.numWindows);
    int chkindex = 0;
    int nextCheckpoint = checkpoints.get(chkindex++);
    for (int i = 0; i < maxWindows; ++i) {
        if ((i + 1) > nextCheckpoint) {
            nextCheckpoint = checkpoints.get(chkindex++);
        }
        Assert.assertEquals("Windows from checkpoint for " + i, nextCheckpoint - i, (int) go.distances.get(i));
    }
    gn.shutdown();
    t.join();
}
Also used : ScheduledExecutorService(com.datatorrent.common.util.ScheduledExecutorService) ScheduledThreadPoolExecutor(com.datatorrent.common.util.ScheduledThreadPoolExecutor) ArrayList(java.util.ArrayList) Checkpoint(com.datatorrent.stram.api.Checkpoint) DefaultAttributeMap(com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

DefaultAttributeMap (com.datatorrent.api.Attribute.AttributeMap.DefaultAttributeMap)1 ScheduledExecutorService (com.datatorrent.common.util.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (com.datatorrent.common.util.ScheduledThreadPoolExecutor)1 Checkpoint (com.datatorrent.stram.api.Checkpoint)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1