Search in sources :

Example 1 with SamzaSetState

use of org.apache.beam.runners.samza.state.SamzaSetState in project beam by apache.

the class SamzaStoreStateInternalsTest method testIteratorClosed.

@Test
public void testIteratorClosed() {
    final String stateId = "foo";
    DoFn<KV<String, Integer>, Set<Integer>> fn = new DoFn<KV<String, Integer>, Set<Integer>>() {

        @StateId(stateId)
        private final StateSpec<SetState<Integer>> setState = StateSpecs.set(VarIntCoder.of());

        @ProcessElement
        public void processElement(ProcessContext c, @StateId(stateId) SetState<Integer> setState) {
            SamzaSetState<Integer> state = (SamzaSetState<Integer>) setState;
            state.add(c.element().getValue());
            // the iterator for size needs to be closed
            int size = Iterators.size(state.readIterator().read());
            if (size > 1) {
                final Iterator<Integer> iterator = state.readIterator().read();
                assertTrue(iterator.hasNext());
                // this iterator should be closed too
                iterator.next();
            }
        }
    };
    pipeline.apply(Create.of(KV.of("hello", 97), KV.of("hello", 42), KV.of("hello", 42), KV.of("hello", 12))).apply(ParDo.of(fn));
    SamzaPipelineOptions options = PipelineOptionsFactory.create().as(SamzaPipelineOptions.class);
    options.setRunner(TestSamzaRunner.class);
    Map<String, String> configs = new HashMap<>(ConfigBuilder.localRunConfig());
    configs.put("stores.foo.factory", TestStorageEngine.class.getName());
    pipeline.getOptions().as(SamzaPipelineOptions.class).setConfigOverride(configs);
    pipeline.run();
    // The test code creates 7 underlying iterators, and 1 more is created during state.clear()
    // Verify all of them are closed
    assertEquals(8, TestStore.iterators.size());
    TestStore.iterators.forEach(iter -> assertTrue(iter.closed));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) KV(org.apache.beam.sdk.values.KV) StateSpec(org.apache.beam.sdk.state.StateSpec) DoFn(org.apache.beam.sdk.transforms.DoFn) SamzaSetState(org.apache.beam.runners.samza.state.SamzaSetState) SamzaPipelineOptions(org.apache.beam.runners.samza.SamzaPipelineOptions) SamzaSetState(org.apache.beam.runners.samza.state.SamzaSetState) SetState(org.apache.beam.sdk.state.SetState) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SamzaPipelineOptions (org.apache.beam.runners.samza.SamzaPipelineOptions)1 SamzaSetState (org.apache.beam.runners.samza.state.SamzaSetState)1 SetState (org.apache.beam.sdk.state.SetState)1 StateSpec (org.apache.beam.sdk.state.StateSpec)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 KV (org.apache.beam.sdk.values.KV)1 Test (org.junit.Test)1