Search in sources :

Example 16 with MapStateDescriptor

use of org.apache.flink.api.common.state.MapStateDescriptor in project flink by apache.

the class SortingBoundedInputITCase method testBatchExecutionWithTimersTwoInput.

@Test
public void testBatchExecutionWithTimersTwoInput() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    // set parallelism to 1 to have consistent order of results
    env.setParallelism(1);
    Configuration config = new Configuration();
    config.set(ExecutionOptions.RUNTIME_MODE, RuntimeExecutionMode.BATCH);
    env.configure(config, this.getClass().getClassLoader());
    WatermarkStrategy<Tuple2<Integer, Integer>> watermarkStrategy = WatermarkStrategy.forGenerator(ctx -> GENERATE_WATERMARK_AFTER_4_14_TIMESTAMP).withTimestampAssigner((r, previousTimestamp) -> r.f1);
    SingleOutputStreamOperator<Integer> elements1 = env.fromElements(Tuple2.of(1, 3), Tuple2.of(1, 1), Tuple2.of(2, 1), Tuple2.of(1, 4), // late element
    Tuple2.of(2, 3), // late element
    Tuple2.of(1, 2), Tuple2.of(1, 13), Tuple2.of(1, 11), Tuple2.of(2, 14), // late element
    Tuple2.of(1, 11)).assignTimestampsAndWatermarks(watermarkStrategy).map(element -> element.f0);
    SingleOutputStreamOperator<Integer> elements2 = env.fromElements(Tuple2.of(1, 3), Tuple2.of(1, 1), Tuple2.of(2, 1), Tuple2.of(1, 4), // late element
    Tuple2.of(2, 3), // late element
    Tuple2.of(1, 2), Tuple2.of(1, 13), Tuple2.of(1, 11), Tuple2.of(2, 14), // late element
    Tuple2.of(1, 11)).assignTimestampsAndWatermarks(watermarkStrategy).map(element -> element.f0);
    OutputTag<Integer> lateElements = new OutputTag<>("late_elements", BasicTypeInfo.INT_TYPE_INFO);
    SingleOutputStreamOperator<Tuple3<Long, Integer, Integer>> sums = elements1.connect(elements2).keyBy(element -> element, element -> element).process(new KeyedCoProcessFunction<Integer, Integer, Integer, Tuple3<Long, Integer, Integer>>() {

        private MapState<Long, Integer> countState;

        private ValueState<Long> previousTimestampState;

        @Override
        public void open(Configuration parameters) {
            countState = getRuntimeContext().getMapState(new MapStateDescriptor<>("sum", BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO));
            previousTimestampState = getRuntimeContext().getState(new ValueStateDescriptor<>("previousTimestamp", BasicTypeInfo.LONG_TYPE_INFO));
        }

        @Override
        public void processElement1(Integer value, Context ctx, Collector<Tuple3<Long, Integer, Integer>> out) throws Exception {
            processElement(value, ctx);
        }

        @Override
        public void processElement2(Integer value, Context ctx, Collector<Tuple3<Long, Integer, Integer>> out) throws Exception {
            processElement(value, ctx);
        }

        private void processElement(Integer value, Context ctx) throws Exception {
            Long elementTimestamp = ctx.timestamp();
            long nextTen = ((elementTimestamp + 10) / 10) * 10;
            ctx.timerService().registerEventTimeTimer(nextTen);
            if (elementTimestamp < ctx.timerService().currentWatermark()) {
                ctx.output(lateElements, value);
            } else {
                Long previousTimestamp = Optional.ofNullable(previousTimestampState.value()).orElse(0L);
                assertThat(elementTimestamp, greaterThanOrEqualTo(previousTimestamp));
                previousTimestampState.update(elementTimestamp);
                Integer currentCount = Optional.ofNullable(countState.get(nextTen)).orElse(0);
                countState.put(nextTen, currentCount + 1);
            }
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<Tuple3<Long, Integer, Integer>> out) throws Exception {
            out.collect(Tuple3.of(timestamp, ctx.getCurrentKey(), countState.get(timestamp)));
            countState.remove(timestamp);
            // this would go in infinite loop if we did not quiesce the
            // timer service.
            ctx.timerService().registerEventTimeTimer(timestamp + 1);
        }
    });
    DataStream<Integer> lateStream = sums.getSideOutput(lateElements);
    List<Integer> lateRecordsCollected = CollectionUtil.iteratorToList(DataStreamUtils.collect(lateStream));
    List<Tuple3<Long, Integer, Integer>> sumsCollected = CollectionUtil.iteratorToList(DataStreamUtils.collect(sums));
    assertTrue(lateRecordsCollected.isEmpty());
    assertThat(sumsCollected, equalTo(Arrays.asList(Tuple3.of(10L, 1, 8), Tuple3.of(20L, 1, 6), Tuple3.of(10L, 2, 4), Tuple3.of(20L, 2, 2))));
}
Also used : Arrays(java.util.Arrays) Tuple3(org.apache.flink.api.java.tuple.Tuple3) WatermarkGenerator(org.apache.flink.api.common.eventtime.WatermarkGenerator) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) KeyedCoProcessFunction(org.apache.flink.streaming.api.functions.co.KeyedCoProcessFunction) Random(java.util.Random) BasicTypeInfo(org.apache.flink.api.common.typeinfo.BasicTypeInfo) Assert.assertThat(org.junit.Assert.assertThat) SplittableIterator(org.apache.flink.util.SplittableIterator) ChainingStrategy(org.apache.flink.streaming.api.operators.ChainingStrategy) ExecutionOptions(org.apache.flink.configuration.ExecutionOptions) WatermarkStatus(org.apache.flink.streaming.runtime.watermarkstatus.WatermarkStatus) AbstractTestBase(org.apache.flink.test.util.AbstractTestBase) BoundedMultiInput(org.apache.flink.streaming.api.operators.BoundedMultiInput) DataStreamUtils(org.apache.flink.streaming.api.datastream.DataStreamUtils) Set(java.util.Set) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) KeyedStream(org.apache.flink.streaming.api.datastream.KeyedStream) OutputTag(org.apache.flink.util.OutputTag) BoundedOneInput(org.apache.flink.streaming.api.operators.BoundedOneInput) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) Objects(java.util.Objects) MultipleInputStreamOperator(org.apache.flink.streaming.api.operators.MultipleInputStreamOperator) List(java.util.List) ValueState(org.apache.flink.api.common.state.ValueState) Watermark(org.apache.flink.api.common.eventtime.Watermark) Optional(java.util.Optional) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) StreamOperatorFactory(org.apache.flink.streaming.api.operators.StreamOperatorFactory) AbstractStreamOperatorV2(org.apache.flink.streaming.api.operators.AbstractStreamOperatorV2) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) DataStreamSource(org.apache.flink.streaming.api.datastream.DataStreamSource) KeyedProcessFunction(org.apache.flink.streaming.api.functions.KeyedProcessFunction) HashSet(java.util.HashSet) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Collector(org.apache.flink.util.Collector) TwoInputStreamOperator(org.apache.flink.streaming.api.operators.TwoInputStreamOperator) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Iterator(java.util.Iterator) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Configuration(org.apache.flink.configuration.Configuration) SingleOutputStreamOperator(org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator) KeyedMultipleInputTransformation(org.apache.flink.streaming.api.transformations.KeyedMultipleInputTransformation) Assert.assertTrue(org.junit.Assert.assertTrue) StreamOperatorParameters(org.apache.flink.streaming.api.operators.StreamOperatorParameters) Test(org.junit.Test) CollectionUtil(org.apache.flink.util.CollectionUtil) WatermarkOutput(org.apache.flink.api.common.eventtime.WatermarkOutput) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) DataStream(org.apache.flink.streaming.api.datastream.DataStream) StreamOperator(org.apache.flink.streaming.api.operators.StreamOperator) Consumer(java.util.function.Consumer) MapState(org.apache.flink.api.common.state.MapState) LatencyMarker(org.apache.flink.streaming.runtime.streamrecord.LatencyMarker) Assert(org.junit.Assert) RuntimeExecutionMode(org.apache.flink.api.common.RuntimeExecutionMode) Input(org.apache.flink.streaming.api.operators.Input) Configuration(org.apache.flink.configuration.Configuration) OutputTag(org.apache.flink.util.OutputTag) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Tuple3(org.apache.flink.api.java.tuple.Tuple3) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Test(org.junit.Test)

Example 17 with MapStateDescriptor

use of org.apache.flink.api.common.state.MapStateDescriptor in project flink by apache.

the class OperatorStateBackendTest method testCorrectClassLoaderUsedOnSnapshot.

@SuppressWarnings("unchecked")
@Test
public void testCorrectClassLoaderUsedOnSnapshot() throws Exception {
    AbstractStateBackend abstractStateBackend = new MemoryStateBackend(4096);
    final Environment env = createMockEnvironment();
    CloseableRegistry cancelStreamRegistry = new CloseableRegistry();
    OperatorStateBackend operatorStateBackend = abstractStateBackend.createOperatorStateBackend(env, "test-op-name", emptyStateHandles, cancelStreamRegistry);
    AtomicInteger copyCounter = new AtomicInteger(0);
    TypeSerializer<Integer> serializer = new VerifyingIntSerializer(env.getUserCodeClassLoader().asClassLoader(), copyCounter);
    // write some state
    ListStateDescriptor<Integer> stateDescriptor = new ListStateDescriptor<>("test", serializer);
    ListState<Integer> listState = operatorStateBackend.getListState(stateDescriptor);
    listState.add(42);
    AtomicInteger keyCopyCounter = new AtomicInteger(0);
    AtomicInteger valueCopyCounter = new AtomicInteger(0);
    TypeSerializer<Integer> keySerializer = new VerifyingIntSerializer(env.getUserCodeClassLoader().asClassLoader(), keyCopyCounter);
    TypeSerializer<Integer> valueSerializer = new VerifyingIntSerializer(env.getUserCodeClassLoader().asClassLoader(), valueCopyCounter);
    MapStateDescriptor<Integer, Integer> broadcastStateDesc = new MapStateDescriptor<>("test-broadcast", keySerializer, valueSerializer);
    BroadcastState<Integer, Integer> broadcastState = operatorStateBackend.getBroadcastState(broadcastStateDesc);
    broadcastState.put(1, 2);
    broadcastState.put(3, 4);
    broadcastState.put(5, 6);
    CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(4096);
    RunnableFuture<SnapshotResult<OperatorStateHandle>> runnableFuture = operatorStateBackend.snapshot(1, 1, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
    FutureUtils.runIfNotDoneAndGet(runnableFuture);
    // make sure that the copy method has been called
    assertTrue(copyCounter.get() > 0);
    assertTrue(keyCopyCounter.get() > 0);
    assertTrue(valueCopyCounter.get() > 0);
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Environment(org.apache.flink.runtime.execution.Environment) Test(org.junit.Test)

Example 18 with MapStateDescriptor

use of org.apache.flink.api.common.state.MapStateDescriptor in project flink by apache.

the class OperatorStateBackendTest method testSnapshotRestoreAsync.

@Test
public void testSnapshotRestoreAsync() throws Exception {
    OperatorStateBackend operatorStateBackend = new DefaultOperatorStateBackendBuilder(OperatorStateBackendTest.class.getClassLoader(), new ExecutionConfig(), true, emptyStateHandles, new CloseableRegistry()).build();
    ListStateDescriptor<MutableType> stateDescriptor1 = new ListStateDescriptor<>("test1", new JavaSerializer<MutableType>());
    ListStateDescriptor<MutableType> stateDescriptor2 = new ListStateDescriptor<>("test2", new JavaSerializer<MutableType>());
    ListStateDescriptor<MutableType> stateDescriptor3 = new ListStateDescriptor<>("test3", new JavaSerializer<MutableType>());
    MapStateDescriptor<MutableType, MutableType> broadcastStateDescriptor1 = new MapStateDescriptor<>("test4", new JavaSerializer<MutableType>(), new JavaSerializer<MutableType>());
    MapStateDescriptor<MutableType, MutableType> broadcastStateDescriptor2 = new MapStateDescriptor<>("test5", new JavaSerializer<MutableType>(), new JavaSerializer<MutableType>());
    MapStateDescriptor<MutableType, MutableType> broadcastStateDescriptor3 = new MapStateDescriptor<>("test6", new JavaSerializer<MutableType>(), new JavaSerializer<MutableType>());
    ListState<MutableType> listState1 = operatorStateBackend.getListState(stateDescriptor1);
    ListState<MutableType> listState2 = operatorStateBackend.getListState(stateDescriptor2);
    ListState<MutableType> listState3 = operatorStateBackend.getUnionListState(stateDescriptor3);
    BroadcastState<MutableType, MutableType> broadcastState1 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor1);
    BroadcastState<MutableType, MutableType> broadcastState2 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor2);
    BroadcastState<MutableType, MutableType> broadcastState3 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor3);
    listState1.add(MutableType.of(42));
    listState1.add(MutableType.of(4711));
    listState2.add(MutableType.of(7));
    listState2.add(MutableType.of(13));
    listState2.add(MutableType.of(23));
    listState3.add(MutableType.of(17));
    listState3.add(MutableType.of(18));
    listState3.add(MutableType.of(19));
    listState3.add(MutableType.of(20));
    broadcastState1.put(MutableType.of(1), MutableType.of(2));
    broadcastState1.put(MutableType.of(2), MutableType.of(5));
    broadcastState2.put(MutableType.of(2), MutableType.of(5));
    BlockerCheckpointStreamFactory streamFactory = new BlockerCheckpointStreamFactory(1024 * 1024);
    OneShotLatch waiterLatch = new OneShotLatch();
    OneShotLatch blockerLatch = new OneShotLatch();
    streamFactory.setWaiterLatch(waiterLatch);
    streamFactory.setBlockerLatch(blockerLatch);
    RunnableFuture<SnapshotResult<OperatorStateHandle>> runnableFuture = operatorStateBackend.snapshot(1, 1, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(runnableFuture);
    // wait until the async checkpoint is in the write code, then continue
    waiterLatch.await();
    // do some mutations to the state, to test if our snapshot will NOT reflect them
    listState1.add(MutableType.of(77));
    broadcastState1.put(MutableType.of(32), MutableType.of(97));
    int n = 0;
    for (MutableType mutableType : listState2.get()) {
        if (++n == 2) {
            // allow the write code to continue, so that we could do changes while state is
            // written in parallel.
            blockerLatch.trigger();
        }
        mutableType.setValue(mutableType.getValue() + 10);
    }
    listState3.clear();
    broadcastState2.clear();
    operatorStateBackend.getListState(new ListStateDescriptor<>("test4", new JavaSerializer<MutableType>()));
    // run the snapshot
    SnapshotResult<OperatorStateHandle> snapshotResult = runnableFuture.get();
    OperatorStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
    try {
        operatorStateBackend.close();
        operatorStateBackend.dispose();
        AbstractStateBackend abstractStateBackend = new MemoryStateBackend(4096);
        CloseableRegistry cancelStreamRegistry = new CloseableRegistry();
        operatorStateBackend = abstractStateBackend.createOperatorStateBackend(createMockEnvironment(), "testOperator", StateObjectCollection.singleton(stateHandle), cancelStreamRegistry);
        assertEquals(3, operatorStateBackend.getRegisteredStateNames().size());
        assertEquals(3, operatorStateBackend.getRegisteredBroadcastStateNames().size());
        listState1 = operatorStateBackend.getListState(stateDescriptor1);
        listState2 = operatorStateBackend.getListState(stateDescriptor2);
        listState3 = operatorStateBackend.getUnionListState(stateDescriptor3);
        broadcastState1 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor1);
        broadcastState2 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor2);
        broadcastState3 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor3);
        assertEquals(3, operatorStateBackend.getRegisteredStateNames().size());
        assertEquals(3, operatorStateBackend.getRegisteredBroadcastStateNames().size());
        Iterator<MutableType> it = listState1.get().iterator();
        assertEquals(42, it.next().value);
        assertEquals(4711, it.next().value);
        assertFalse(it.hasNext());
        it = listState2.get().iterator();
        assertEquals(7, it.next().value);
        assertEquals(13, it.next().value);
        assertEquals(23, it.next().value);
        assertFalse(it.hasNext());
        it = listState3.get().iterator();
        assertEquals(17, it.next().value);
        assertEquals(18, it.next().value);
        assertEquals(19, it.next().value);
        assertEquals(20, it.next().value);
        assertFalse(it.hasNext());
        Iterator<Map.Entry<MutableType, MutableType>> bIt = broadcastState1.iterator();
        assertTrue(bIt.hasNext());
        Map.Entry<MutableType, MutableType> entry = bIt.next();
        assertEquals(1, entry.getKey().value);
        assertEquals(2, entry.getValue().value);
        assertTrue(bIt.hasNext());
        entry = bIt.next();
        assertEquals(2, entry.getKey().value);
        assertEquals(5, entry.getValue().value);
        assertFalse(bIt.hasNext());
        bIt = broadcastState2.iterator();
        assertTrue(bIt.hasNext());
        entry = bIt.next();
        assertEquals(2, entry.getKey().value);
        assertEquals(5, entry.getValue().value);
        assertFalse(bIt.hasNext());
        bIt = broadcastState3.iterator();
        assertFalse(bIt.hasNext());
        operatorStateBackend.close();
        operatorStateBackend.dispose();
    } finally {
        stateHandle.discardState();
    }
    executorService.shutdown();
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ExecutorService(java.util.concurrent.ExecutorService) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with MapStateDescriptor

use of org.apache.flink.api.common.state.MapStateDescriptor in project flink by apache.

the class OperatorStateBackendTest method testSnapshotRestoreSync.

@Test
public void testSnapshotRestoreSync() throws Exception {
    AbstractStateBackend abstractStateBackend = new MemoryStateBackend(2 * 4096);
    OperatorStateBackend operatorStateBackend = abstractStateBackend.createOperatorStateBackend(createMockEnvironment(), "test-op-name", emptyStateHandles, new CloseableRegistry());
    ListStateDescriptor<Serializable> stateDescriptor1 = new ListStateDescriptor<>("test1", new JavaSerializer<>());
    ListStateDescriptor<Serializable> stateDescriptor2 = new ListStateDescriptor<>("test2", new JavaSerializer<>());
    ListStateDescriptor<Serializable> stateDescriptor3 = new ListStateDescriptor<>("test3", new JavaSerializer<>());
    MapStateDescriptor<Serializable, Serializable> broadcastStateDescriptor1 = new MapStateDescriptor<>("test4", new JavaSerializer<>(), new JavaSerializer<>());
    MapStateDescriptor<Serializable, Serializable> broadcastStateDescriptor2 = new MapStateDescriptor<>("test5", new JavaSerializer<>(), new JavaSerializer<>());
    MapStateDescriptor<Serializable, Serializable> broadcastStateDescriptor3 = new MapStateDescriptor<>("test6", new JavaSerializer<>(), new JavaSerializer<>());
    ListState<Serializable> listState1 = operatorStateBackend.getListState(stateDescriptor1);
    ListState<Serializable> listState2 = operatorStateBackend.getListState(stateDescriptor2);
    ListState<Serializable> listState3 = operatorStateBackend.getUnionListState(stateDescriptor3);
    BroadcastState<Serializable, Serializable> broadcastState1 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor1);
    BroadcastState<Serializable, Serializable> broadcastState2 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor2);
    BroadcastState<Serializable, Serializable> broadcastState3 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor3);
    listState1.add(42);
    listState1.add(4711);
    listState2.add(7);
    listState2.add(13);
    listState2.add(23);
    listState3.add(17);
    listState3.add(18);
    listState3.add(19);
    listState3.add(20);
    broadcastState1.put(1, 2);
    broadcastState1.put(2, 5);
    broadcastState2.put(2, 5);
    CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(2 * 4096);
    RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot = operatorStateBackend.snapshot(1L, 1L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
    SnapshotResult<OperatorStateHandle> snapshotResult = FutureUtils.runIfNotDoneAndGet(snapshot);
    OperatorStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
    try {
        operatorStateBackend.close();
        operatorStateBackend.dispose();
        operatorStateBackend = abstractStateBackend.createOperatorStateBackend(createMockEnvironment(), "testOperator", StateObjectCollection.singleton(stateHandle), new CloseableRegistry());
        assertEquals(3, operatorStateBackend.getRegisteredStateNames().size());
        assertEquals(3, operatorStateBackend.getRegisteredBroadcastStateNames().size());
        listState1 = operatorStateBackend.getListState(stateDescriptor1);
        listState2 = operatorStateBackend.getListState(stateDescriptor2);
        listState3 = operatorStateBackend.getUnionListState(stateDescriptor3);
        broadcastState1 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor1);
        broadcastState2 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor2);
        broadcastState3 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor3);
        assertEquals(3, operatorStateBackend.getRegisteredStateNames().size());
        assertEquals(3, operatorStateBackend.getRegisteredBroadcastStateNames().size());
        Iterator<Serializable> it = listState1.get().iterator();
        assertEquals(42, it.next());
        assertEquals(4711, it.next());
        assertFalse(it.hasNext());
        it = listState2.get().iterator();
        assertEquals(7, it.next());
        assertEquals(13, it.next());
        assertEquals(23, it.next());
        assertFalse(it.hasNext());
        it = listState3.get().iterator();
        assertEquals(17, it.next());
        assertEquals(18, it.next());
        assertEquals(19, it.next());
        assertEquals(20, it.next());
        assertFalse(it.hasNext());
        Iterator<Map.Entry<Serializable, Serializable>> bIt = broadcastState1.iterator();
        assertTrue(bIt.hasNext());
        Map.Entry<Serializable, Serializable> entry = bIt.next();
        assertEquals(1, entry.getKey());
        assertEquals(2, entry.getValue());
        assertTrue(bIt.hasNext());
        entry = bIt.next();
        assertEquals(2, entry.getKey());
        assertEquals(5, entry.getValue());
        assertFalse(bIt.hasNext());
        bIt = broadcastState2.iterator();
        assertTrue(bIt.hasNext());
        entry = bIt.next();
        assertEquals(2, entry.getKey());
        assertEquals(5, entry.getValue());
        assertFalse(bIt.hasNext());
        bIt = broadcastState3.iterator();
        assertFalse(bIt.hasNext());
        operatorStateBackend.close();
        operatorStateBackend.dispose();
    } finally {
        stateHandle.discardState();
    }
}
Also used : Serializable(java.io.Serializable) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 20 with MapStateDescriptor

use of org.apache.flink.api.common.state.MapStateDescriptor in project flink by apache.

the class OperatorStateBackendTest method testSnapshotAsyncClose.

@Test
public void testSnapshotAsyncClose() throws Exception {
    DefaultOperatorStateBackend operatorStateBackend = new DefaultOperatorStateBackendBuilder(OperatorStateBackendTest.class.getClassLoader(), new ExecutionConfig(), true, emptyStateHandles, new CloseableRegistry()).build();
    ListStateDescriptor<MutableType> stateDescriptor1 = new ListStateDescriptor<>("test1", new JavaSerializer<MutableType>());
    ListState<MutableType> listState1 = operatorStateBackend.getListState(stateDescriptor1);
    listState1.add(MutableType.of(42));
    listState1.add(MutableType.of(4711));
    MapStateDescriptor<MutableType, MutableType> broadcastStateDescriptor1 = new MapStateDescriptor<>("test4", new JavaSerializer<MutableType>(), new JavaSerializer<MutableType>());
    BroadcastState<MutableType, MutableType> broadcastState1 = operatorStateBackend.getBroadcastState(broadcastStateDescriptor1);
    broadcastState1.put(MutableType.of(1), MutableType.of(2));
    broadcastState1.put(MutableType.of(2), MutableType.of(5));
    BlockerCheckpointStreamFactory streamFactory = new BlockerCheckpointStreamFactory(1024 * 1024);
    OneShotLatch waiterLatch = new OneShotLatch();
    OneShotLatch blockerLatch = new OneShotLatch();
    streamFactory.setWaiterLatch(waiterLatch);
    streamFactory.setBlockerLatch(blockerLatch);
    RunnableFuture<SnapshotResult<OperatorStateHandle>> runnableFuture = operatorStateBackend.snapshot(1, 1, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(runnableFuture);
    // wait until the async checkpoint is in the write code, then continue
    waiterLatch.await();
    operatorStateBackend.close();
    blockerLatch.trigger();
    try {
        runnableFuture.get(60, TimeUnit.SECONDS);
        Assert.fail();
    } catch (CancellationException expected) {
    }
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Aggregations

MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)47 Test (org.junit.Test)28 List (java.util.List)15 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)14 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)13 ArrayList (java.util.ArrayList)12 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)12 HashMap (java.util.HashMap)9 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)9 Map (java.util.Map)8 Configuration (org.apache.flink.configuration.Configuration)8 BlockerCheckpointStreamFactory (org.apache.flink.runtime.util.BlockerCheckpointStreamFactory)8 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)7 MapState (org.apache.flink.api.common.state.MapState)6 RowData (org.apache.flink.table.data.RowData)6 Collector (org.apache.flink.util.Collector)6 ListTypeInfo (org.apache.flink.api.java.typeutils.ListTypeInfo)5 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)5 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)5 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)5