Search in sources :

Example 1 with StreamingModeExecutionState

use of org.apache.beam.runners.dataflow.worker.StreamingModeExecutionContext.StreamingModeExecutionState in project beam by apache.

the class StreamingModeExecutionContextTest method stateSamplingInStreaming.

@Test(timeout = 2000)
public void stateSamplingInStreaming() {
    // Test that when writing on one thread and reading from another, updates always eventually
    // reach the reading thread.
    StreamingModeExecutionState state = new StreamingModeExecutionState(NameContextsForTests.nameContextForTest(), "testState", null, NoopProfileScope.NOOP, null);
    ExecutionStateSampler sampler = ExecutionStateSampler.newForTest();
    try {
        sampler.start();
        ExecutionStateTracker tracker = new ExecutionStateTracker(sampler);
        Thread executionThread = new Thread();
        executionThread.setName("looping-thread-for-test");
        tracker.activate(executionThread);
        tracker.enterState(state);
        // Wait for the state to be incremented 3 times
        for (int i = 0; i < 3; i++) {
            CounterUpdate update = null;
            while (update == null) {
                update = state.extractUpdate(false);
            }
            long newValue = splitIntToLong(update.getInteger());
            assertThat(newValue, Matchers.greaterThan(0L));
        }
    } finally {
        sampler.stop();
    }
}
Also used : StreamingModeExecutionState(org.apache.beam.runners.dataflow.worker.StreamingModeExecutionContext.StreamingModeExecutionState) ExecutionStateTracker(org.apache.beam.runners.core.metrics.ExecutionStateTracker) DataflowExecutionStateTracker(org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowExecutionStateTracker) ExecutionStateSampler(org.apache.beam.runners.core.metrics.ExecutionStateSampler) CounterUpdate(com.google.api.services.dataflow.model.CounterUpdate) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 2 with StreamingModeExecutionState

use of org.apache.beam.runners.dataflow.worker.StreamingModeExecutionContext.StreamingModeExecutionState in project beam by apache.

the class StreamingModeExecutionContextTest method testAtomicExtractUpdate.

/**
 * Ensure that incrementing and extracting counter updates are correct under concurrent reader and
 * writer threads.
 */
@Test
public void testAtomicExtractUpdate() throws InterruptedException, ExecutionException {
    long numUpdates = 1_000_000;
    StreamingModeExecutionState state = new StreamingModeExecutionState(NameContextsForTests.nameContextForTest(), "testState", null, NoopProfileScope.NOOP, null);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    AtomicBoolean doneWriting = new AtomicBoolean(false);
    Callable<Long> reader = () -> {
        long count = 0;
        boolean isLastRead;
        do {
            isLastRead = doneWriting.get();
            CounterUpdate update = state.extractUpdate(false);
            if (update != null) {
                count += splitIntToLong(update.getInteger());
            }
        } while (!isLastRead);
        return count;
    };
    Runnable writer = () -> {
        for (int i = 0; i < numUpdates; i++) {
            state.takeSample(1L);
        }
        doneWriting.set(true);
    };
    // NB: Reader is invoked before writer to ensure they execute concurrently.
    List<Future<Long>> results = executor.invokeAll(Lists.newArrayList(reader, Executors.callable(writer, 0L)), 2, TimeUnit.SECONDS);
    long count = results.get(0).get();
    assertThat(count, equalTo(numUpdates));
}
Also used : StreamingModeExecutionState(org.apache.beam.runners.dataflow.worker.StreamingModeExecutionContext.StreamingModeExecutionState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) DataflowCounterUpdateExtractor.splitIntToLong(org.apache.beam.runners.dataflow.worker.counters.DataflowCounterUpdateExtractor.splitIntToLong) Future(java.util.concurrent.Future) CounterUpdate(com.google.api.services.dataflow.model.CounterUpdate) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Aggregations

CounterUpdate (com.google.api.services.dataflow.model.CounterUpdate)2 StateNamespaceForTest (org.apache.beam.runners.core.StateNamespaceForTest)2 StreamingModeExecutionState (org.apache.beam.runners.dataflow.worker.StreamingModeExecutionContext.StreamingModeExecutionState)2 Test (org.junit.Test)2 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ExecutionStateSampler (org.apache.beam.runners.core.metrics.ExecutionStateSampler)1 ExecutionStateTracker (org.apache.beam.runners.core.metrics.ExecutionStateTracker)1 DataflowExecutionStateTracker (org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowExecutionStateTracker)1 DataflowCounterUpdateExtractor.splitIntToLong (org.apache.beam.runners.dataflow.worker.counters.DataflowCounterUpdateExtractor.splitIntToLong)1