Search in sources :

Example 36 with TimerData

use of org.apache.beam.runners.core.TimerInternals.TimerData in project beam by apache.

the class EvaluationContextTest method extractFiredTimersExtractsTimers.

@Test
public void extractFiredTimersExtractsTimers() {
    TransformResult<?> holdResult = StepTransformResult.withHold(impulseProducer, new Instant(0)).build();
    context.handleResult(null, ImmutableList.of(), holdResult);
    StructuralKey<?> key = StructuralKey.of("foo".length(), VarIntCoder.of());
    TimerData toFire = TimerData.of(StateNamespaces.global(), new Instant(100L), new Instant(100L), TimeDomain.EVENT_TIME);
    TransformResult<?> timerResult = StepTransformResult.withoutHold(downstreamProducer).withState(CopyOnAccessInMemoryStateInternals.withUnderlying(key, null)).withTimerUpdate(TimerUpdate.builder(key).setTimer(toFire).build()).build();
    // haven't added any timers, must be empty
    assertThat(context.extractFiredTimers(), emptyIterable());
    context.handleResult(context.createKeyedBundle(key, impulse).commit(Instant.now()), ImmutableList.of(), timerResult);
    // timer hasn't fired
    assertThat(context.extractFiredTimers(), emptyIterable());
    TransformResult<?> advanceResult = StepTransformResult.withoutHold(impulseProducer).build();
    // Should cause the downstream timer to fire
    context.handleResult(null, ImmutableList.of(), advanceResult);
    Collection<FiredTimers<AppliedPTransform<?, ?, ?>>> fired = context.extractFiredTimers();
    assertThat(Iterables.getOnlyElement(fired).getKey(), equalTo(key));
    FiredTimers<AppliedPTransform<?, ?, ?>> firedForKey = Iterables.getOnlyElement(fired);
    // Contains exclusively the fired timer
    assertThat(firedForKey.getTimers(), contains(toFire));
    // Don't reextract timers
    assertThat(context.extractFiredTimers(), emptyIterable());
}
Also used : FiredTimers(org.apache.beam.runners.direct.WatermarkManager.FiredTimers) AppliedPTransform(org.apache.beam.sdk.runners.AppliedPTransform) Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Example 37 with TimerData

use of org.apache.beam.runners.core.TimerInternals.TimerData in project beam by apache.

the class InMemoryTimerInternalsTest method testFiringProcessingTimeTimers.

@Test
public void testFiringProcessingTimeTimers() throws Exception {
    InMemoryTimerInternals underTest = new InMemoryTimerInternals();
    TimerData processingTime1 = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.PROCESSING_TIME);
    TimerData processingTime2 = TimerData.of(NS1, new Instant(29), new Instant(29), TimeDomain.PROCESSING_TIME);
    underTest.setTimer(processingTime1);
    underTest.setTimer(processingTime2);
    underTest.advanceProcessingTime(new Instant(20));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime1));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    // Advancing just a little shouldn't refire
    underTest.advanceProcessingTime(new Instant(21));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    // Adding the timer and advancing a little should fire again
    underTest.setTimer(processingTime1);
    underTest.advanceProcessingTime(new Instant(21));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime1));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    // And advancing the rest of the way should still have the other timer
    underTest.advanceProcessingTime(new Instant(30));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime2));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
}
Also used : Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Example 38 with TimerData

use of org.apache.beam.runners.core.TimerInternals.TimerData in project beam by apache.

the class InMemoryTimerInternalsTest method testDeduplicate.

@Test
public void testDeduplicate() throws Exception {
    InMemoryTimerInternals underTest = new InMemoryTimerInternals();
    TimerData eventTime = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.EVENT_TIME);
    TimerData processingTime = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.PROCESSING_TIME);
    underTest.setTimer(eventTime);
    underTest.setTimer(eventTime);
    underTest.setTimer(processingTime);
    underTest.setTimer(processingTime);
    underTest.advanceProcessingTime(new Instant(20));
    underTest.advanceInputWatermark(new Instant(20));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTime));
    assertThat(underTest.removeNextEventTimer(), nullValue());
}
Also used : Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Example 39 with TimerData

use of org.apache.beam.runners.core.TimerInternals.TimerData in project beam by apache.

the class InMemoryTimerInternalsTest method testFiringEventTimers.

@Test
public void testFiringEventTimers() throws Exception {
    InMemoryTimerInternals underTest = new InMemoryTimerInternals();
    TimerData eventTimer1 = TimerData.of(ID1, NS1, new Instant(19), new Instant(19), TimeDomain.EVENT_TIME);
    TimerData eventTimer2 = TimerData.of(ID2, NS1, new Instant(29), new Instant(29), TimeDomain.EVENT_TIME);
    underTest.setTimer(eventTimer1);
    underTest.setTimer(eventTimer2);
    underTest.advanceInputWatermark(new Instant(20));
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTimer1));
    assertThat(underTest.removeNextEventTimer(), nullValue());
    // Advancing just a little shouldn't refire
    underTest.advanceInputWatermark(new Instant(21));
    assertThat(underTest.removeNextEventTimer(), nullValue());
    // Adding the timer and advancing a little should refire
    underTest.setTimer(eventTimer1);
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTimer1));
    assertThat(underTest.removeNextEventTimer(), nullValue());
    // And advancing the rest of the way should still have the other timer
    underTest.advanceInputWatermark(new Instant(30));
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTimer2));
    assertThat(underTest.removeNextEventTimer(), nullValue());
}
Also used : Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Example 40 with TimerData

use of org.apache.beam.runners.core.TimerInternals.TimerData in project beam by apache.

the class InMemoryTimerInternalsTest method testTimerOrdering.

@Test
public void testTimerOrdering() throws Exception {
    InMemoryTimerInternals underTest = new InMemoryTimerInternals();
    TimerData eventTime1 = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.EVENT_TIME);
    TimerData processingTime1 = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.PROCESSING_TIME);
    TimerData synchronizedProcessingTime1 = TimerData.of(NS1, new Instant(19), new Instant(19), TimeDomain.SYNCHRONIZED_PROCESSING_TIME);
    TimerData eventTime2 = TimerData.of(NS1, new Instant(29), new Instant(29), TimeDomain.EVENT_TIME);
    TimerData processingTime2 = TimerData.of(NS1, new Instant(29), new Instant(29), TimeDomain.PROCESSING_TIME);
    TimerData synchronizedProcessingTime2 = TimerData.of(NS1, new Instant(29), new Instant(29), TimeDomain.SYNCHRONIZED_PROCESSING_TIME);
    underTest.setTimer(processingTime1);
    underTest.setTimer(eventTime1);
    underTest.setTimer(synchronizedProcessingTime1);
    underTest.setTimer(processingTime2);
    underTest.setTimer(eventTime2);
    underTest.setTimer(synchronizedProcessingTime2);
    assertThat(underTest.removeNextEventTimer(), nullValue());
    underTest.advanceInputWatermark(new Instant(30));
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTime1));
    assertThat(underTest.removeNextEventTimer(), equalTo(eventTime2));
    assertThat(underTest.removeNextEventTimer(), nullValue());
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    underTest.advanceProcessingTime(new Instant(30));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime1));
    assertThat(underTest.removeNextProcessingTimer(), equalTo(processingTime2));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
    assertThat(underTest.removeNextSynchronizedProcessingTimer(), nullValue());
    underTest.advanceSynchronizedProcessingTime(new Instant(30));
    assertThat(underTest.removeNextSynchronizedProcessingTimer(), equalTo(synchronizedProcessingTime1));
    assertThat(underTest.removeNextSynchronizedProcessingTimer(), equalTo(synchronizedProcessingTime2));
    assertThat(underTest.removeNextProcessingTimer(), nullValue());
}
Also used : Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Aggregations

TimerData (org.apache.beam.runners.core.TimerInternals.TimerData)43 Instant (org.joda.time.Instant)37 Test (org.junit.Test)35 TimerUpdate (org.apache.beam.runners.direct.WatermarkManager.TimerUpdate)13 TimerUpdateBuilder (org.apache.beam.runners.direct.WatermarkManager.TimerUpdate.TimerUpdateBuilder)8 FiredTimers (org.apache.beam.runners.direct.WatermarkManager.FiredTimers)7 AppliedPTransform (org.apache.beam.sdk.runners.AppliedPTransform)6 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)5 ArrayList (java.util.ArrayList)4 StateNamespace (org.apache.beam.runners.core.StateNamespace)3 WindowNamespace (org.apache.beam.runners.core.StateNamespaces.WindowNamespace)3 TimerDataCoder (org.apache.beam.runners.core.TimerInternals.TimerDataCoder)3 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 TransformWatermarks (org.apache.beam.runners.direct.WatermarkManager.TransformWatermarks)2 Coder (org.apache.beam.sdk.coders.Coder)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 Closeable (java.io.Closeable)1 Collection (java.util.Collection)1