Search in sources :

Example 11 with WatermarkHoldState

use of org.apache.beam.sdk.state.WatermarkHoldState in project beam by apache.

the class ApexStateInternalsTest method testMergeLatestWatermarkIntoSource.

@Test
public void testMergeLatestWatermarkIntoSource() throws Exception {
    WatermarkHoldState value1 = underTest.state(NAMESPACE_1, WATERMARK_LATEST_ADDR);
    WatermarkHoldState value2 = underTest.state(NAMESPACE_2, WATERMARK_LATEST_ADDR);
    WatermarkHoldState value3 = underTest.state(NAMESPACE_3, WATERMARK_LATEST_ADDR);
    value1.add(new Instant(3000));
    value2.add(new Instant(5000));
    value1.add(new Instant(4000));
    value2.add(new Instant(2000));
    // Merging clears the old values and updates the result value.
    StateMerging.mergeWatermarks(Arrays.asList(value1, value2), value3, WINDOW_1);
    // Merging clears the old values and updates the result value.
    assertThat(value3.read(), Matchers.equalTo(new Instant(5000)));
    assertThat(value1.read(), Matchers.equalTo(null));
    assertThat(value2.read(), Matchers.equalTo(null));
}
Also used : Instant(org.joda.time.Instant) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 12 with WatermarkHoldState

use of org.apache.beam.sdk.state.WatermarkHoldState in project beam by apache.

the class ApexStateInternalsTest method testWatermarkLatestState.

@Test
public void testWatermarkLatestState() throws Exception {
    WatermarkHoldState value = underTest.state(NAMESPACE_1, WATERMARK_LATEST_ADDR);
    // State instances are cached, but depend on the namespace.
    assertEquals(value, underTest.state(NAMESPACE_1, WATERMARK_LATEST_ADDR));
    assertFalse(value.equals(underTest.state(NAMESPACE_2, WATERMARK_LATEST_ADDR)));
    assertThat(value.read(), Matchers.nullValue());
    value.add(new Instant(2000));
    assertThat(value.read(), Matchers.equalTo(new Instant(2000)));
    value.add(new Instant(3000));
    assertThat(value.read(), Matchers.equalTo(new Instant(3000)));
    value.add(new Instant(1000));
    assertThat(value.read(), Matchers.equalTo(new Instant(3000)));
    value.clear();
    assertThat(value.read(), Matchers.equalTo(null));
    assertEquals(underTest.state(NAMESPACE_1, WATERMARK_LATEST_ADDR), value);
}
Also used : Instant(org.joda.time.Instant) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 13 with WatermarkHoldState

use of org.apache.beam.sdk.state.WatermarkHoldState in project beam by apache.

the class ApexStateInternalsTest method testWatermarkEndOfWindowState.

@Test
public void testWatermarkEndOfWindowState() throws Exception {
    WatermarkHoldState value = underTest.state(NAMESPACE_1, WATERMARK_EOW_ADDR);
    // State instances are cached, but depend on the namespace.
    assertEquals(value, underTest.state(NAMESPACE_1, WATERMARK_EOW_ADDR));
    assertFalse(value.equals(underTest.state(NAMESPACE_2, WATERMARK_EOW_ADDR)));
    assertThat(value.read(), Matchers.nullValue());
    value.add(new Instant(2000));
    assertThat(value.read(), Matchers.equalTo(new Instant(2000)));
    value.clear();
    assertThat(value.read(), Matchers.equalTo(null));
    assertEquals(underTest.state(NAMESPACE_1, WATERMARK_EOW_ADDR), value);
}
Also used : Instant(org.joda.time.Instant) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 14 with WatermarkHoldState

use of org.apache.beam.sdk.state.WatermarkHoldState in project beam by apache.

the class ApexStateInternalsTest method testWatermarkEarliestState.

@Test
public void testWatermarkEarliestState() throws Exception {
    WatermarkHoldState value = underTest.state(NAMESPACE_1, WATERMARK_EARLIEST_ADDR);
    // State instances are cached, but depend on the namespace.
    assertEquals(value, underTest.state(NAMESPACE_1, WATERMARK_EARLIEST_ADDR));
    assertFalse(value.equals(underTest.state(NAMESPACE_2, WATERMARK_EARLIEST_ADDR)));
    assertThat(value.read(), Matchers.nullValue());
    value.add(new Instant(2000));
    assertThat(value.read(), Matchers.equalTo(new Instant(2000)));
    value.add(new Instant(3000));
    assertThat(value.read(), Matchers.equalTo(new Instant(2000)));
    value.add(new Instant(1000));
    assertThat(value.read(), Matchers.equalTo(new Instant(1000)));
    value.clear();
    assertThat(value.read(), Matchers.equalTo(null));
    assertEquals(underTest.state(NAMESPACE_1, WATERMARK_EARLIEST_ADDR), value);
}
Also used : Instant(org.joda.time.Instant) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 15 with WatermarkHoldState

use of org.apache.beam.sdk.state.WatermarkHoldState in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testGetEarliestWatermarkHoldWithEarliestInUnderlyingTable.

@Test
public void testGetEarliestWatermarkHoldWithEarliestInUnderlyingTable() {
    BoundedWindow first = new BoundedWindow() {

        @Override
        public Instant maxTimestamp() {
            return new Instant(2048L);
        }
    };
    BoundedWindow second = new BoundedWindow() {

        @Override
        public Instant maxTimestamp() {
            return new Instant(689743L);
        }
    };
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying("foo", null);
    StateTag<WatermarkHoldState> firstHoldAddress = StateTags.watermarkStateInternal("foo", TimestampCombiner.EARLIEST);
    WatermarkHoldState firstHold = underlying.state(StateNamespaces.window(null, first), firstHoldAddress);
    firstHold.add(new Instant(22L));
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying("foo", underlying.commit());
    StateTag<WatermarkHoldState> secondHoldAddress = StateTags.watermarkStateInternal("foo", TimestampCombiner.EARLIEST);
    WatermarkHoldState secondHold = internals.state(StateNamespaces.window(null, second), secondHoldAddress);
    secondHold.add(new Instant(244L));
    internals.commit();
    assertThat(internals.getEarliestWatermarkHold(), equalTo(new Instant(22L)));
}
Also used : Instant(org.joda.time.Instant) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Aggregations

WatermarkHoldState (org.apache.beam.sdk.state.WatermarkHoldState)25 Instant (org.joda.time.Instant)24 Test (org.junit.Test)22 StateNamespaceForTest (org.apache.beam.runners.core.StateNamespaceForTest)16 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)3 ReadableState (org.apache.beam.sdk.state.ReadableState)2 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 StateNamespace (org.apache.beam.runners.core.StateNamespace)1 TimestampCombiner (org.apache.beam.sdk.transforms.windowing.TimestampCombiner)1