Search in sources :

Example 21 with StateNamespace

use of org.apache.beam.runners.core.StateNamespace in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testCommitWithEmptyNewAndFullUnderlyingIsNotEmpty.

@Test
public void testCommitWithEmptyNewAndFullUnderlyingIsNotEmpty() {
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, underlying);
    StateNamespace namespace = new StateNamespaceForTest("foo");
    StateTag<BagState<String>> bagTag = StateTags.bag("foo", StringUtf8Coder.of());
    BagState<String> stringBag = underlying.state(namespace, bagTag);
    assertThat(stringBag.read(), emptyIterable());
    stringBag.add("bar");
    stringBag.add("baz");
    internals.commit();
    assertThat(internals.isEmpty(), is(false));
}
Also used : StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) BagState(org.apache.beam.sdk.state.BagState) StateNamespace(org.apache.beam.runners.core.StateNamespace) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 22 with StateNamespace

use of org.apache.beam.runners.core.StateNamespace in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testCommitWithUnderlying.

@Test
public void testCommitWithUnderlying() {
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, underlying);
    StateNamespace namespace = new StateNamespaceForTest("foo");
    StateTag<BagState<String>> bagTag = StateTags.bag("foo", StringUtf8Coder.of());
    BagState<String> stringBag = underlying.state(namespace, bagTag);
    assertThat(stringBag.read(), emptyIterable());
    stringBag.add("bar");
    stringBag.add("baz");
    internals.commit();
    BagState<String> reReadStringBag = internals.state(namespace, bagTag);
    assertThat(reReadStringBag.read(), containsInAnyOrder("baz", "bar"));
    reReadStringBag.add("spam");
    BagState<String> underlyingState = underlying.state(namespace, bagTag);
    assertThat(underlyingState.read(), containsInAnyOrder("spam", "bar", "baz"));
    assertThat(underlyingState, is(theInstance(stringBag)));
    assertThat(internals.isEmpty(), is(false));
}
Also used : StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) BagState(org.apache.beam.sdk.state.BagState) StateNamespace(org.apache.beam.runners.core.StateNamespace) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 23 with StateNamespace

use of org.apache.beam.runners.core.StateNamespace in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testBagStateWithUnderlying.

@Test
public void testBagStateWithUnderlying() {
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    StateNamespace namespace = new StateNamespaceForTest("foo");
    StateTag<BagState<Integer>> valueTag = StateTags.bag("foo", VarIntCoder.of());
    BagState<Integer> underlyingValue = underlying.state(namespace, valueTag);
    assertThat(underlyingValue.read(), emptyIterable());
    underlyingValue.add(1);
    assertThat(underlyingValue.read(), containsInAnyOrder(1));
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, underlying);
    BagState<Integer> copyOnAccessState = internals.state(namespace, valueTag);
    assertThat(copyOnAccessState.read(), containsInAnyOrder(1));
    copyOnAccessState.add(4);
    assertThat(copyOnAccessState.read(), containsInAnyOrder(4, 1));
    assertThat(underlyingValue.read(), containsInAnyOrder(1));
    BagState<Integer> reReadUnderlyingValue = underlying.state(namespace, valueTag);
    assertThat(Lists.newArrayList(underlyingValue.read()), equalTo(Lists.newArrayList(reReadUnderlyingValue.read())));
}
Also used : StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) BagState(org.apache.beam.sdk.state.BagState) StateNamespace(org.apache.beam.runners.core.StateNamespace) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 24 with StateNamespace

use of org.apache.beam.runners.core.StateNamespace in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testWatermarkHoldStateWithUnderlying.

@Test
public void testWatermarkHoldStateWithUnderlying() {
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    TimestampCombiner timestampCombiner = TimestampCombiner.EARLIEST;
    StateNamespace namespace = new StateNamespaceForTest("foo");
    StateTag<WatermarkHoldState> stateTag = StateTags.watermarkStateInternal("wmstate", timestampCombiner);
    WatermarkHoldState underlyingValue = underlying.state(namespace, stateTag);
    assertThat(underlyingValue.read(), nullValue());
    underlyingValue.add(new Instant(250L));
    assertThat(underlyingValue.read(), equalTo(new Instant(250L)));
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, underlying);
    WatermarkHoldState copyOnAccessState = internals.state(namespace, stateTag);
    assertThat(copyOnAccessState.read(), equalTo(new Instant(250L)));
    copyOnAccessState.add(new Instant(100L));
    assertThat(copyOnAccessState.read(), equalTo(new Instant(100L)));
    assertThat(underlyingValue.read(), equalTo(new Instant(250L)));
    copyOnAccessState.add(new Instant(500L));
    assertThat(copyOnAccessState.read(), equalTo(new Instant(100L)));
    WatermarkHoldState reReadUnderlyingValue = underlying.state(namespace, stateTag);
    assertThat(underlyingValue.read(), equalTo(reReadUnderlyingValue.read()));
}
Also used : TimestampCombiner(org.apache.beam.sdk.transforms.windowing.TimestampCombiner) Instant(org.joda.time.Instant) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) StateNamespace(org.apache.beam.runners.core.StateNamespace) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 25 with StateNamespace

use of org.apache.beam.runners.core.StateNamespace in project beam by apache.

the class CopyOnAccessInMemoryStateInternalsTest method testGetWithEmpty.

@Test
public void testGetWithEmpty() {
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    StateNamespace namespace = new StateNamespaceForTest("foo");
    StateTag<BagState<String>> bagTag = StateTags.bag("foo", StringUtf8Coder.of());
    BagState<String> stringBag = internals.state(namespace, bagTag);
    assertThat(stringBag.read(), emptyIterable());
    stringBag.add("bar");
    stringBag.add("baz");
    assertThat(stringBag.read(), containsInAnyOrder("baz", "bar"));
    BagState<String> reReadStringBag = internals.state(namespace, bagTag);
    assertThat(reReadStringBag.read(), containsInAnyOrder("baz", "bar"));
}
Also used : StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) BagState(org.apache.beam.sdk.state.BagState) StateNamespace(org.apache.beam.runners.core.StateNamespace) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Aggregations

StateNamespace (org.apache.beam.runners.core.StateNamespace)43 Test (org.junit.Test)30 Instant (org.joda.time.Instant)20 StateNamespaceForTest (org.apache.beam.runners.core.StateNamespaceForTest)15 TimerInternals (org.apache.beam.runners.core.TimerInternals)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 SamzaPipelineOptions (org.apache.beam.runners.samza.SamzaPipelineOptions)11 ByteArray (org.apache.beam.runners.samza.runtime.SamzaStoreStateInternals.ByteArray)11 StateValue (org.apache.beam.runners.samza.runtime.SamzaStoreStateInternals.StateValue)11 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)11 BagState (org.apache.beam.sdk.state.BagState)10 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)5 ValueState (org.apache.beam.sdk.state.ValueState)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteBuffer (java.nio.ByteBuffer)3 StateNamespaces (org.apache.beam.runners.core.StateNamespaces)3 WindowNamespace (org.apache.beam.runners.core.StateNamespaces.WindowNamespace)3 SerializablePipelineOptions (org.apache.beam.runners.core.construction.SerializablePipelineOptions)3 StreamRecordStripper.stripStreamRecordFromWindowedValue (org.apache.beam.runners.flink.translation.wrappers.streaming.StreamRecordStripper.stripStreamRecordFromWindowedValue)3 Coder (org.apache.beam.sdk.coders.Coder)3