Search in sources :

Example 1 with TimerDataCoder

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

the class ApexTimerInternalsTest method testSerialization.

@Test
public void testSerialization() {
    TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
    TimerData timerData = TimerData.of("arbitrary-id", StateNamespaces.global(), new Instant(0), TimeDomain.EVENT_TIME);
    String key = "key";
    ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
    timerInternals.setContext(key, StringUtf8Coder.of(), Instant.now(), null);
    timerInternals.setTimer(timerData);
    ApexTimerInternals<String> cloned;
    assertNotNull("Serialization", cloned = KryoCloneUtils.cloneObject(timerInternals));
    cloned.setContext(key, StringUtf8Coder.of(), Instant.now(), null);
    Map<?, Set<Slice>> timers = cloned.getTimerSet(TimeDomain.EVENT_TIME).getMap();
    assertEquals(1, timers.size());
}
Also used : Set(java.util.Set) TimerDataCoder(org.apache.beam.runners.core.TimerInternals.TimerDataCoder) Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Test(org.junit.Test)

Example 2 with TimerDataCoder

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

the class ApexTimerInternalsTest method testEventTimeTimers.

@Test
public void testEventTimeTimers() {
    final Map<String, Collection<TimerData>> firedTimers = new HashMap<>();
    TimerProcessor<String> timerProcessor = new TimerProcessor<String>() {

        @Override
        public void fireTimer(String key, Collection<TimerData> timerData) {
            firedTimers.put(key, timerData);
        }
    };
    TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
    String key1 = "key1";
    Instant instant0 = new Instant(0);
    Instant instant1 = new Instant(1);
    Instant instant2 = new Instant(2);
    ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
    timerInternals.setContext(key1, StringUtf8Coder.of(), Instant.now(), null);
    TimerData timerData0 = TimerData.of("timerData0", StateNamespaces.global(), instant0, TimeDomain.EVENT_TIME);
    timerInternals.setTimer(timerData0);
    TimerData timerData1 = TimerData.of("timerData1", StateNamespaces.global(), instant1, TimeDomain.EVENT_TIME);
    timerInternals.setTimer(timerData1);
    timerInternals.fireReadyTimers(instant0.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
    assertEquals(0, firedTimers.size());
    firedTimers.clear();
    timerInternals.fireReadyTimers(instant1.getMillis(), timerProcessor, TimeDomain.PROCESSING_TIME);
    assertEquals(0, firedTimers.size());
    timerInternals.fireReadyTimers(instant1.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
    assertEquals(1, firedTimers.size());
    assertEquals(Sets.newHashSet(timerData0), Sets.newHashSet(firedTimers.values().iterator().next()));
    firedTimers.clear();
    timerInternals.fireReadyTimers(instant2.getMillis(), timerProcessor, TimeDomain.EVENT_TIME);
    assertEquals(1, firedTimers.size());
    assertEquals(Sets.newHashSet(timerData1), Sets.newHashSet(firedTimers.values().iterator().next()));
    firedTimers.clear();
}
Also used : TimerDataCoder(org.apache.beam.runners.core.TimerInternals.TimerDataCoder) HashMap(java.util.HashMap) Instant(org.joda.time.Instant) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) Collection(java.util.Collection) TimerProcessor(org.apache.beam.runners.apex.translation.operators.ApexTimerInternals.TimerProcessor) Test(org.junit.Test)

Example 3 with TimerDataCoder

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

the class ApexTimerInternalsTest method testDeleteTimer.

@Test
public void testDeleteTimer() {
    TimerDataCoder timerDataCoder = TimerDataCoder.of(GlobalWindow.Coder.INSTANCE);
    String key1 = "key1";
    Instant instant0 = new Instant(0);
    Instant instant1 = new Instant(1);
    ApexTimerInternals<String> timerInternals = new ApexTimerInternals<>(timerDataCoder);
    timerInternals.setContext(key1, StringUtf8Coder.of(), Instant.now(), null);
    TimerData timerData0 = TimerData.of("timerData0", StateNamespaces.global(), instant0, TimeDomain.EVENT_TIME);
    timerInternals.setTimer(timerData0);
    TimerData timerData1 = TimerData.of("timerData1", StateNamespaces.global(), instant1, TimeDomain.EVENT_TIME);
    timerInternals.setTimer(timerData1);
    Map<?, Set<Slice>> timerMap = timerInternals.getTimerSet(TimeDomain.EVENT_TIME).getMap();
    assertEquals(1, timerMap.size());
    assertEquals(2, timerMap.values().iterator().next().size());
    timerInternals.deleteTimer(timerData0.getNamespace(), timerData0.getTimerId());
    assertEquals(1, timerMap.size());
    assertEquals(1, timerMap.values().iterator().next().size());
    timerInternals.deleteTimer(timerData1.getNamespace(), timerData1.getTimerId(), TimeDomain.PROCESSING_TIME);
    assertEquals(1, timerMap.size());
    assertEquals(1, timerMap.values().iterator().next().size());
    timerInternals.deleteTimer(timerData1.getNamespace(), timerData1.getTimerId(), TimeDomain.EVENT_TIME);
    assertEquals(0, timerMap.size());
}
Also used : Set(java.util.Set) TimerDataCoder(org.apache.beam.runners.core.TimerInternals.TimerDataCoder) 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)3 TimerDataCoder (org.apache.beam.runners.core.TimerInternals.TimerDataCoder)3 Instant (org.joda.time.Instant)3 Test (org.junit.Test)3 Set (java.util.Set)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 TimerProcessor (org.apache.beam.runners.apex.translation.operators.ApexTimerInternals.TimerProcessor)1