Java Code Examples for org.apache.flink.api.common.state.ListState#add()

The following examples show how to use org.apache.flink.api.common.state.ListState#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeavyDeploymentStressTestProgram.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeState(FunctionInitializationContext context) throws Exception {

	readyToFail = false;

	if (context.isRestored()) {
		isRunning = false;
	} else {
		isRunning = true;

		OperatorStateStore operatorStateStore = context.getOperatorStateStore();
		for (int i = 0; i < numListStates; ++i) {

			ListStateDescriptor<String> listStateDescriptor =
				new ListStateDescriptor<>("test-list-state-" + i, String.class);

			ListState<String> unionListState =
				operatorStateStore.getUnionListState(listStateDescriptor);

			for (int j = 0; j < numPartitionsPerListState; ++j) {
				unionListState.add(String.valueOf(j));
			}
		}
	}
}
 
Example 2
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * This test verifies that all ListState implementations are consistent in not allowing
 * adding {@code null}.
 */
@Test
public void testListStateAddNull() throws Exception {
	AbstractKeyedStateBackend<String> keyedBackend = createKeyedBackend(StringSerializer.INSTANCE);

	final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);

	try {
		ListState<Long> state =
			keyedBackend.getPartitionedState(
				VoidNamespace.INSTANCE,
				VoidNamespaceSerializer.INSTANCE,
				stateDescr);

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		expectedException.expect(NullPointerException.class);
		state.add(null);
	} finally {
		keyedBackend.close();
		keyedBackend.dispose();
	}
}
 
Example 3
Source File: Buckets.java    From flink with Apache License 2.0 6 votes vote down vote up
void snapshotState(
		final long checkpointId,
		final ListState<byte[]> bucketStatesContainer,
		final ListState<Long> partCounterStateContainer) throws Exception {

	Preconditions.checkState(
			fsWriter != null && bucketStateSerializer != null,
			"sink has not been initialized");

	LOG.info("Subtask {} checkpointing for checkpoint with id={} (max part counter={}).",
			subtaskIndex, checkpointId, maxPartCounter);

	bucketStatesContainer.clear();
	partCounterStateContainer.clear();

	snapshotActiveBuckets(checkpointId, bucketStatesContainer);
	partCounterStateContainer.add(maxPartCounter);
}
 
Example 4
Source File: Buckets.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
void snapshotState(
		final long checkpointId,
		final ListState<byte[]> bucketStatesContainer,
		final ListState<Long> partCounterStateContainer) throws Exception {

	Preconditions.checkState(
			fsWriter != null && bucketStateSerializer != null,
			"sink has not been initialized");

	LOG.info("Subtask {} checkpointing for checkpoint with id={} (max part counter={}).",
			subtaskIndex, checkpointId, maxPartCounter);

	bucketStatesContainer.clear();
	partCounterStateContainer.clear();

	snapshotActiveBuckets(checkpointId, bucketStatesContainer);
	partCounterStateContainer.add(maxPartCounter);
}
 
Example 5
Source File: FlinkBroadcastStateInternals.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Update map(namespce->T) from index 0. */
void updateMap(Map<String, T> map) throws Exception {
  if (indexInSubtaskGroup == 0) {
    ListState<Map<String, T>> state = flinkStateBackend.getUnionListState(flinkStateDescriptor);
    state.clear();
    if (map.size() > 0) {
      state.add(map);
    }
  } else {
    if (map.isEmpty()) {
      stateForNonZeroOperator.remove(name);
      // updateMap is always behind getMap,
      // getMap will clear map in BroadcastOperatorState,
      // we don't need clear here.
    } else {
      stateForNonZeroOperator.put(name, map);
    }
  }
}
 
Example 6
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplyToAllKeysLambdaFunction() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ListStateDescriptor<String> listStateDescriptor =
			new ListStateDescriptor<>("foo", StringSerializer.INSTANCE);

		ListState<String> listState =
			backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, listStateDescriptor);

		for (int i = 0; i < 100; ++i) {
			backend.setCurrentKey(i);
			listState.add("Hello" + i);
		}

		// valid state value via applyToAllKeys().
		backend.applyToAllKeys(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, listStateDescriptor,
			(Integer key, ListState<String> state) -> assertEquals("Hello" + key, state.get().iterator().next())
		);
	}
	finally {
		IOUtils.closeQuietly(backend);
		backend.dispose();
	}
}
 
Example 7
Source File: StateBackendMigrationTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testOperatorPartitionableListStateUpgrade(
		ListStateDescriptor<TestType> initialAccessDescriptor,
		ListStateDescriptor<TestType> newAccessDescriptorAfterRestore) throws Exception {

	CheckpointStreamFactory streamFactory = createStreamFactory();

	OperatorStateBackend backend = createOperatorStateBackend();

	try {
		ListState<TestType> state = backend.getListState(initialAccessDescriptor);

		state.add(new TestType("foo", 13));
		state.add(new TestType("bar", 278));

		OperatorStateHandle snapshot = runSnapshot(
			backend.snapshot(1L, 2L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()));
		backend.dispose();

		backend = restoreOperatorStateBackend(snapshot);

		state = backend.getListState(newAccessDescriptorAfterRestore);

		// make sure that reading and writing each state partition works with the new serializer
		Iterator<TestType> iterator = state.get().iterator();
		assertEquals(new TestType("foo", 13), iterator.next());
		assertEquals(new TestType("bar", 278), iterator.next());
		Assert.assertFalse(iterator.hasNext());
		state.add(new TestType("new-entry", 777));
	} finally {
		backend.dispose();
	}
}
 
Example 8
Source File: StreamingFunctionUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static boolean trySnapshotFunctionState(
		StateSnapshotContext context,
		OperatorStateBackend backend,
		Function userFunction) throws Exception {

	if (userFunction instanceof CheckpointedFunction) {
		((CheckpointedFunction) userFunction).snapshotState(context);

		return true;
	}

	if (userFunction instanceof ListCheckpointed) {
		@SuppressWarnings("unchecked")
		List<Serializable> partitionableState = ((ListCheckpointed<Serializable>) userFunction).
				snapshotState(context.getCheckpointId(), context.getCheckpointTimestamp());

		ListState<Serializable> listState = backend.
				getSerializableListState(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME);

		listState.clear();

		if (null != partitionableState) {
			try {
				for (Serializable statePartition : partitionableState) {
					listState.add(statePartition);
				}
			} catch (Exception e) {
				listState.clear();

				throw new Exception("Could not write partitionable state to operator " +
					"state backend.", e);
			}
		}

		return true;
	}

	return false;
}
 
Example 9
Source File: OneInputStreamTaskTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotState(StateSnapshotContext context) throws Exception {
	ListState<Integer> partitionableState =
		getOperatorStateBackend().getListState(TEST_DESCRIPTOR);
	partitionableState.clear();

	partitionableState.add(42);
	partitionableState.add(4711);

	++numberSnapshotCalls;
}
 
Example 10
Source File: StateBackendMigrationTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testOperatorUnionListStateUpgrade(
		ListStateDescriptor<TestType> initialAccessDescriptor,
		ListStateDescriptor<TestType> newAccessDescriptorAfterRestore) throws Exception {

	CheckpointStreamFactory streamFactory = createStreamFactory();

	OperatorStateBackend backend = createOperatorStateBackend();

	try {
		ListState<TestType> state = backend.getUnionListState(initialAccessDescriptor);

		state.add(new TestType("foo", 13));
		state.add(new TestType("bar", 278));

		OperatorStateHandle snapshot = runSnapshot(
			backend.snapshot(1L, 2L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()));
		backend.dispose();

		backend = restoreOperatorStateBackend(snapshot);

		state = backend.getUnionListState(newAccessDescriptorAfterRestore);

		// the state backend should have decided whether or not it needs to perform state migration;
		// make sure that reading and writing each state partition works with the new serializer
		Iterator<TestType> iterator = state.get().iterator();
		assertEquals(new TestType("foo", 13), iterator.next());
		assertEquals(new TestType("bar", 278), iterator.next());
		Assert.assertFalse(iterator.hasNext());
		state.add(new TestType("new-entry", 777));
	} finally {
		backend.dispose();
	}
}
 
Example 11
Source File: OperatorStateBackendTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testRegisterStatesWithoutTypeSerializer() throws Exception {
	// prepare an execution config with a non standard type registered
	final Class<?> registeredType = FutureTask.class;

	// validate the precondition of this test - if this condition fails, we need to pick a different
	// example serializer
	assertFalse(new KryoSerializer<>(File.class, new ExecutionConfig()).getKryo().getDefaultSerializer(registeredType)
			instanceof com.esotericsoftware.kryo.serializers.JavaSerializer);

	final ExecutionConfig cfg = new ExecutionConfig();
	cfg.registerTypeWithKryoSerializer(registeredType, com.esotericsoftware.kryo.serializers.JavaSerializer.class);

	final OperatorStateBackend operatorStateBackend =
		new DefaultOperatorStateBackendBuilder(
			classLoader,
			cfg,
			false,
			emptyStateHandles,
			new CloseableRegistry())
		.build();

	ListStateDescriptor<File> stateDescriptor = new ListStateDescriptor<>("test", File.class);
	ListStateDescriptor<String> stateDescriptor2 = new ListStateDescriptor<>("test2", String.class);

	ListState<File> listState = operatorStateBackend.getListState(stateDescriptor);
	assertNotNull(listState);

	ListState<String> listState2 = operatorStateBackend.getListState(stateDescriptor2);
	assertNotNull(listState2);

	assertEquals(2, operatorStateBackend.getRegisteredStateNames().size());

	// make sure that type registrations are forwarded
	TypeSerializer<?> serializer = ((PartitionableListState<?>) listState).getStateMetaInfo().getPartitionStateSerializer();
	assertTrue(serializer instanceof KryoSerializer);
	assertTrue(((KryoSerializer<?>) serializer).getKryo().getSerializer(registeredType)
			instanceof com.esotericsoftware.kryo.serializers.JavaSerializer);

	Iterator<String> it = listState2.get().iterator();
	assertFalse(it.hasNext());
	listState2.add("kevin");
	listState2.add("sunny");

	it = listState2.get().iterator();
	assertEquals("kevin", it.next());
	assertEquals("sunny", it.next());
	assertFalse(it.hasNext());
}
 
Example 12
Source File: OperatorStateBackendTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@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.getUserClassLoader(), 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.getUserClassLoader(), keyCopyCounter);
	TypeSerializer<Integer> valueSerializer = new VerifyingIntSerializer(env.getUserClassLoader(), 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);
}
 
Example 13
Source File: TtlListStateVerifier.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
void updateInternal(@Nonnull ListState<String> state, String update) throws Exception {
	state.add(update);
}
 
Example 14
Source File: OperatorStateBackendTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@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.getOperatorState(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) {
	}
}
 
Example 15
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testListStateRestoreWithWrongSerializers() throws Exception {
	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ListStateDescriptor<String> kvId = new ListStateDescriptor<>("id", String.class);
		ListState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.add("1");
		backend.setCurrentKey(2);
		state.add("2");

		// draw a snapshot
		KeyedStateHandle snapshot1 = runSnapshot(
			backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);

		backend.dispose();
		// restore the first snapshot and validate it
		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
		snapshot1.discardState();

		@SuppressWarnings("unchecked")
		TypeSerializer<String> fakeStringSerializer =
				(TypeSerializer<String>) (TypeSerializer<?>) FloatSerializer.INSTANCE;

		try {
			kvId = new ListStateDescriptor<>("id", fakeStringSerializer);

			state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

			state.get();

			fail("should recognize wrong serializers");
		} catch (StateMigrationException ignored) {
			// expected
		}
	} finally {
		backend.dispose();
	}
}
 
Example 16
Source File: StateBackendMigrationTestBase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void testKeyedListStateUpgrade(
		ListStateDescriptor<TestType> initialAccessDescriptor,
		ListStateDescriptor<TestType> newAccessDescriptorAfterRestore) throws Exception {

	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();

	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ListState<TestType> listState = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			CustomVoidNamespaceSerializer.INSTANCE,
			initialAccessDescriptor);

		backend.setCurrentKey(1);
		listState.add(new TestType("key-1", 1));
		listState.add(new TestType("key-1", 2));
		listState.add(new TestType("key-1", 3));

		backend.setCurrentKey(2);
		listState.add(new TestType("key-2", 1));

		backend.setCurrentKey(3);
		listState.add(new TestType("key-3", 1));
		listState.add(new TestType("key-3", 2));

		KeyedStateHandle snapshot = runSnapshot(
			backend.snapshot(1L, 2L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);
		backend.dispose();

		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot);

		listState = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			CustomVoidNamespaceSerializer.INSTANCE,
			newAccessDescriptorAfterRestore);

		// make sure that reading and writing each key state works with the new serializer
		backend.setCurrentKey(1);
		Iterator<TestType> iterable1 = listState.get().iterator();
		Assert.assertEquals(new TestType("key-1", 1), iterable1.next());
		Assert.assertEquals(new TestType("key-1", 2), iterable1.next());
		Assert.assertEquals(new TestType("key-1", 3), iterable1.next());
		Assert.assertFalse(iterable1.hasNext());
		listState.add(new TestType("new-key-1", 123));

		backend.setCurrentKey(2);
		Iterator<TestType> iterable2 = listState.get().iterator();
		Assert.assertEquals(new TestType("key-2", 1), iterable2.next());
		Assert.assertFalse(iterable2.hasNext());
		listState.add(new TestType("new-key-2", 456));

		backend.setCurrentKey(3);
		Iterator<TestType> iterable3 = listState.get().iterator();
		Assert.assertEquals(new TestType("key-3", 1), iterable3.next());
		Assert.assertEquals(new TestType("key-3", 2), iterable3.next());
		Assert.assertFalse(iterable3.hasNext());
		listState.add(new TestType("new-key-3", 777));

		// do another snapshot to verify the snapshot logic after migration
		snapshot = runSnapshot(
			backend.snapshot(2L, 3L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);
		snapshot.discardState();

	} finally {
		backend.dispose();
	}
}
 
Example 17
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testListStateRestoreWithWrongSerializers() throws Exception {
	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ListStateDescriptor<String> kvId = new ListStateDescriptor<>("id", String.class);
		ListState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.add("1");
		backend.setCurrentKey(2);
		state.add("2");

		// draw a snapshot
		KeyedStateHandle snapshot1 = runSnapshot(
			backend.snapshot(682375462378L, 2, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);

		backend.dispose();
		// restore the first snapshot and validate it
		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot1);
		snapshot1.discardState();

		@SuppressWarnings("unchecked")
		TypeSerializer<String> fakeStringSerializer =
				(TypeSerializer<String>) (TypeSerializer<?>) FloatSerializer.INSTANCE;

		try {
			kvId = new ListStateDescriptor<>("id", fakeStringSerializer);

			state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

			state.get();

			fail("should recognize wrong serializers");
		} catch (StateMigrationException ignored) {
			// expected
		}
	} finally {
		backend.dispose();
	}
}
 
Example 18
Source File: StateBackendMigrationTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testKeyedListStateUpgrade(
		ListStateDescriptor<TestType> initialAccessDescriptor,
		ListStateDescriptor<TestType> newAccessDescriptorAfterRestore) throws Exception {

	CheckpointStreamFactory streamFactory = createStreamFactory();
	SharedStateRegistry sharedStateRegistry = new SharedStateRegistry();

	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		ListState<TestType> listState = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			CustomVoidNamespaceSerializer.INSTANCE,
			initialAccessDescriptor);

		backend.setCurrentKey(1);
		listState.add(new TestType("key-1", 1));
		listState.add(new TestType("key-1", 2));
		listState.add(new TestType("key-1", 3));

		backend.setCurrentKey(2);
		listState.add(new TestType("key-2", 1));

		backend.setCurrentKey(3);
		listState.add(new TestType("key-3", 1));
		listState.add(new TestType("key-3", 2));

		KeyedStateHandle snapshot = runSnapshot(
			backend.snapshot(1L, 2L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);
		backend.dispose();

		backend = restoreKeyedBackend(IntSerializer.INSTANCE, snapshot);

		listState = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			CustomVoidNamespaceSerializer.INSTANCE,
			newAccessDescriptorAfterRestore);

		// make sure that reading and writing each key state works with the new serializer
		backend.setCurrentKey(1);
		Iterator<TestType> iterable1 = listState.get().iterator();
		assertEquals(new TestType("key-1", 1), iterable1.next());
		assertEquals(new TestType("key-1", 2), iterable1.next());
		assertEquals(new TestType("key-1", 3), iterable1.next());
		Assert.assertFalse(iterable1.hasNext());
		listState.add(new TestType("new-key-1", 123));

		backend.setCurrentKey(2);
		Iterator<TestType> iterable2 = listState.get().iterator();
		assertEquals(new TestType("key-2", 1), iterable2.next());
		Assert.assertFalse(iterable2.hasNext());
		listState.add(new TestType("new-key-2", 456));

		backend.setCurrentKey(3);
		Iterator<TestType> iterable3 = listState.get().iterator();
		assertEquals(new TestType("key-3", 1), iterable3.next());
		assertEquals(new TestType("key-3", 2), iterable3.next());
		Assert.assertFalse(iterable3.hasNext());
		listState.add(new TestType("new-key-3", 777));

		// do another snapshot to verify the snapshot logic after migration
		snapshot = runSnapshot(
			backend.snapshot(2L, 3L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation()),
			sharedStateRegistry);
		snapshot.discardState();

	} finally {
		backend.dispose();
	}
}
 
Example 19
Source File: TtlListStateVerifier.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
void updateInternal(@Nonnull ListState<String> state, String update) throws Exception {
	state.add(update);
}
 
Example 20
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testListStateAPIs() throws Exception {

	AbstractKeyedStateBackend<String> keyedBackend = createKeyedBackend(StringSerializer.INSTANCE);

	final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);

	try {
		ListState<Long> state =
			keyedBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, stateDescr);

		keyedBackend.setCurrentKey("def");
		assertNull(state.get());
		state.add(17L);
		state.add(11L);
		assertThat(state.get(), containsInAnyOrder(17L, 11L));
		// update(emptyList) should remain the value null
		state.update(Collections.emptyList());
		assertNull(state.get());
		state.update(Arrays.asList(10L, 16L));
		assertThat(state.get(), containsInAnyOrder(16L, 10L));
		assertThat(state.get(), containsInAnyOrder(16L, 10L));

		keyedBackend.setCurrentKey("abc");
		assertNull(state.get());

		keyedBackend.setCurrentKey("g");
		assertNull(state.get());
		assertNull(state.get());
		state.addAll(Collections.emptyList());
		assertNull(state.get());
		state.addAll(Arrays.asList(3L, 4L));
		assertThat(state.get(), containsInAnyOrder(3L, 4L));
		assertThat(state.get(), containsInAnyOrder(3L, 4L));
		state.addAll(new ArrayList<>());
		assertThat(state.get(), containsInAnyOrder(3L, 4L));
		state.addAll(Arrays.asList(5L, 6L));
		assertThat(state.get(), containsInAnyOrder(3L, 4L, 5L, 6L));
		state.addAll(new ArrayList<>());
		assertThat(state.get(), containsInAnyOrder(3L, 4L, 5L, 6L));

		assertThat(state.get(), containsInAnyOrder(3L, 4L, 5L, 6L));
		state.update(Arrays.asList(1L, 2L));
		assertThat(state.get(), containsInAnyOrder(1L, 2L));

		keyedBackend.setCurrentKey("def");
		assertThat(state.get(), containsInAnyOrder(10L, 16L));
		state.clear();
		assertNull(state.get());

		keyedBackend.setCurrentKey("g");
		state.add(3L);
		state.add(2L);
		state.add(1L);

		keyedBackend.setCurrentKey("def");
		assertNull(state.get());

		keyedBackend.setCurrentKey("g");
		assertThat(state.get(), containsInAnyOrder(1L, 2L, 3L, 2L, 1L));
		state.update(Arrays.asList(5L, 6L));
		assertThat(state.get(), containsInAnyOrder(5L, 6L));
		state.clear();

		// make sure all lists / maps are cleared
		assertThat("State backend is not empty.", keyedBackend.numKeyValueStateEntries(), is(0));
	} finally {
		keyedBackend.close();
		keyedBackend.dispose();
	}
}