Java Code Examples for org.apache.flink.api.common.state.ListStateDescriptor#initializeSerializerUnlessSet()

The following examples show how to use org.apache.flink.api.common.state.ListStateDescriptor#initializeSerializerUnlessSet() . 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: DefaultKeyedStateStore.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		ListState<T> originalState = getPartitionedState(stateProperties);
		return new UserFacingListState<>(originalState);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example 2
Source File: DefaultKeyedStateStore.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		ListState<T> originalState = getPartitionedState(stateProperties);
		return new UserFacingListState<>(originalState);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example 3
Source File: DefaultKeyedStateStore.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		ListState<T> originalState = getPartitionedState(stateProperties);
		return new UserFacingListState<>(originalState);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example 4
Source File: DefaultOperatorStateBackend.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private <S> ListState<S> getListState(
		ListStateDescriptor<S> stateDescriptor,
		OperatorStateHandle.Mode mode) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> previous = (PartitionableListState<S>) accessedStatesByName.get(name);
	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				mode);
		return previous;
	}

	// end up here if its the first time access after execution for the
	// provided state name; check compatibility of restored state, if any
	// TODO with eager registration in place, these checks should be moved to restore()

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<S> partitionStateSerializer = Preconditions.checkNotNull(stateDescriptor.getElementSerializer());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> partitionableListState = (PartitionableListState<S>) registeredOperatorStates.get(name);

	if (null == partitionableListState) {
		// no restored state for the state name; simply create new state holder

		partitionableListState = new PartitionableListState<>(
			new RegisteredOperatorStateBackendMetaInfo<>(
				name,
				partitionStateSerializer,
				mode));

		registeredOperatorStates.put(name, partitionableListState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				partitionableListState.getStateMetaInfo().getName(),
				name,
				partitionableListState.getStateMetaInfo().getAssignmentMode(),
				mode);

		RegisteredOperatorStateBackendMetaInfo<S> restoredPartitionableListStateMetaInfo =
			partitionableListState.getStateMetaInfo();

		// check compatibility to determine if new serializers are incompatible
		TypeSerializer<S> newPartitionStateSerializer = partitionStateSerializer.duplicate();

		TypeSerializerSchemaCompatibility<S> stateCompatibility =
			restoredPartitionableListStateMetaInfo.updatePartitionStateSerializer(newPartitionStateSerializer);
		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new state typeSerializer for operator state must not be incompatible.");
		}

		partitionableListState.setStateMetaInfo(restoredPartitionableListStateMetaInfo);
	}

	accessedStatesByName.put(name, partitionableListState);
	return partitionableListState;
}
 
Example 5
Source File: HeapKeyedStateBackendSnapshotMigrationTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * [FLINK-5979]
 *
 * <p>This test takes a snapshot that was created with Flink 1.2 and tries to restore it in master to check
 * the backwards compatibility of the serialization format of {@link StateTable}s.
 */
@Test
public void testRestore1_2ToMaster() throws Exception {

	ClassLoader cl = getClass().getClassLoader();
	URL resource = cl.getResource("heap_keyed_statebackend_1_2.snapshot");

	Preconditions.checkNotNull(resource, "Binary snapshot resource not found!");

	final Integer namespace1 = 1;
	final Integer namespace2 = 2;
	final Integer namespace3 = 3;

	final KeyGroupsStateHandle stateHandle;
	try (BufferedInputStream bis = new BufferedInputStream((new FileInputStream(resource.getFile())))) {
		stateHandle = InstantiationUtil.deserializeObject(bis, Thread.currentThread().getContextClassLoader());
	}
	try (final HeapKeyedStateBackend<String> keyedBackend = createKeyedBackend(StateObjectCollection.singleton(stateHandle))) {
		final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);
		stateDescr.initializeSerializerUnlessSet(new ExecutionConfig());

		InternalListState<String, Integer, Long> state = keyedBackend.createInternalState(IntSerializer.INSTANCE, stateDescr);

		assertEquals(7, keyedBackend.numKeyValueStateEntries());

		keyedBackend.setCurrentKey("abc");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(33L, 55L), state.get());
		state.setCurrentNamespace(namespace2);
		assertEquals(asList(22L, 11L), state.get());
		state.setCurrentNamespace(namespace3);
		assertEquals(Collections.singletonList(44L), state.get());

		keyedBackend.setCurrentKey("def");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(11L, 44L), state.get());

		state.setCurrentNamespace(namespace3);
		assertEquals(asList(22L, 55L, 33L), state.get());

		keyedBackend.setCurrentKey("jkl");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(11L, 22L, 33L, 44L, 55L), state.get());

		keyedBackend.setCurrentKey("mno");
		state.setCurrentNamespace(namespace3);
		assertEquals(asList(11L, 22L, 33L, 44L, 55L), state.get());
	}
}
 
Example 6
Source File: StreamingRuntimeContext.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
	stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
	return keyedStateStore.getListState(stateProperties);
}
 
Example 7
Source File: DefaultOperatorStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <S> ListState<S> getListState(
		ListStateDescriptor<S> stateDescriptor,
		OperatorStateHandle.Mode mode) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> previous = (PartitionableListState<S>) accessedStatesByName.get(name);
	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				mode);
		return previous;
	}

	// end up here if its the first time access after execution for the
	// provided state name; check compatibility of restored state, if any
	// TODO with eager registration in place, these checks should be moved to restore()

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<S> partitionStateSerializer = Preconditions.checkNotNull(stateDescriptor.getElementSerializer());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> partitionableListState = (PartitionableListState<S>) registeredOperatorStates.get(name);

	if (null == partitionableListState) {
		// no restored state for the state name; simply create new state holder

		partitionableListState = new PartitionableListState<>(
			new RegisteredOperatorStateBackendMetaInfo<>(
				name,
				partitionStateSerializer,
				mode));

		registeredOperatorStates.put(name, partitionableListState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				partitionableListState.getStateMetaInfo().getName(),
				name,
				partitionableListState.getStateMetaInfo().getAssignmentMode(),
				mode);

		RegisteredOperatorStateBackendMetaInfo<S> restoredPartitionableListStateMetaInfo =
			partitionableListState.getStateMetaInfo();

		// check compatibility to determine if new serializers are incompatible
		TypeSerializer<S> newPartitionStateSerializer = partitionStateSerializer.duplicate();

		TypeSerializerSchemaCompatibility<S> stateCompatibility =
			restoredPartitionableListStateMetaInfo.updatePartitionStateSerializer(newPartitionStateSerializer);
		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new state typeSerializer for operator state must not be incompatible.");
		}

		partitionableListState.setStateMetaInfo(restoredPartitionableListStateMetaInfo);
	}

	accessedStatesByName.put(name, partitionableListState);
	return partitionableListState;
}
 
Example 8
Source File: HeapKeyedStateBackendSnapshotMigrationTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * [FLINK-5979]
 *
 * <p>This test takes a snapshot that was created with Flink 1.2 and tries to restore it in master to check
 * the backwards compatibility of the serialization format of {@link StateTable}s.
 */
@Test
public void testRestore1_2ToMaster() throws Exception {

	ClassLoader cl = getClass().getClassLoader();
	URL resource = cl.getResource("heap_keyed_statebackend_1_2.snapshot");

	Preconditions.checkNotNull(resource, "Binary snapshot resource not found!");

	final Integer namespace1 = 1;
	final Integer namespace2 = 2;
	final Integer namespace3 = 3;

	final KeyGroupsStateHandle stateHandle;
	try (BufferedInputStream bis = new BufferedInputStream((new FileInputStream(resource.getFile())))) {
		stateHandle = InstantiationUtil.deserializeObject(bis, Thread.currentThread().getContextClassLoader());
	}
	try (final HeapKeyedStateBackend<String> keyedBackend = createKeyedBackend(StateObjectCollection.singleton(stateHandle))) {
		final ListStateDescriptor<Long> stateDescr = new ListStateDescriptor<>("my-state", Long.class);
		stateDescr.initializeSerializerUnlessSet(new ExecutionConfig());

		InternalListState<String, Integer, Long> state = keyedBackend.createInternalState(IntSerializer.INSTANCE, stateDescr);

		assertEquals(7, keyedBackend.numKeyValueStateEntries());

		keyedBackend.setCurrentKey("abc");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(33L, 55L), state.get());
		state.setCurrentNamespace(namespace2);
		assertEquals(asList(22L, 11L), state.get());
		state.setCurrentNamespace(namespace3);
		assertEquals(Collections.singletonList(44L), state.get());

		keyedBackend.setCurrentKey("def");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(11L, 44L), state.get());

		state.setCurrentNamespace(namespace3);
		assertEquals(asList(22L, 55L, 33L), state.get());

		keyedBackend.setCurrentKey("jkl");
		state.setCurrentNamespace(namespace1);
		assertEquals(asList(11L, 22L, 33L, 44L, 55L), state.get());

		keyedBackend.setCurrentKey("mno");
		state.setCurrentNamespace(namespace3);
		assertEquals(asList(11L, 22L, 33L, 44L, 55L), state.get());
	}
}
 
Example 9
Source File: StreamingRuntimeContext.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
	stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
	return keyedStateStore.getListState(stateProperties);
}
 
Example 10
Source File: DefaultOperatorStateBackend.java    From flink with Apache License 2.0 4 votes vote down vote up
private <S> ListState<S> getListState(
		ListStateDescriptor<S> stateDescriptor,
		OperatorStateHandle.Mode mode) throws StateMigrationException {

	Preconditions.checkNotNull(stateDescriptor);
	String name = Preconditions.checkNotNull(stateDescriptor.getName());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> previous = (PartitionableListState<S>) accessedStatesByName.get(name);
	if (previous != null) {
		checkStateNameAndMode(
				previous.getStateMetaInfo().getName(),
				name,
				previous.getStateMetaInfo().getAssignmentMode(),
				mode);
		return previous;
	}

	// end up here if its the first time access after execution for the
	// provided state name; check compatibility of restored state, if any
	// TODO with eager registration in place, these checks should be moved to restore()

	stateDescriptor.initializeSerializerUnlessSet(getExecutionConfig());
	TypeSerializer<S> partitionStateSerializer = Preconditions.checkNotNull(stateDescriptor.getElementSerializer());

	@SuppressWarnings("unchecked")
	PartitionableListState<S> partitionableListState = (PartitionableListState<S>) registeredOperatorStates.get(name);

	if (null == partitionableListState) {
		// no restored state for the state name; simply create new state holder

		partitionableListState = new PartitionableListState<>(
			new RegisteredOperatorStateBackendMetaInfo<>(
				name,
				partitionStateSerializer,
				mode));

		registeredOperatorStates.put(name, partitionableListState);
	} else {
		// has restored state; check compatibility of new state access

		checkStateNameAndMode(
				partitionableListState.getStateMetaInfo().getName(),
				name,
				partitionableListState.getStateMetaInfo().getAssignmentMode(),
				mode);

		RegisteredOperatorStateBackendMetaInfo<S> restoredPartitionableListStateMetaInfo =
			partitionableListState.getStateMetaInfo();

		// check compatibility to determine if new serializers are incompatible
		TypeSerializer<S> newPartitionStateSerializer = partitionStateSerializer.duplicate();

		TypeSerializerSchemaCompatibility<S> stateCompatibility =
			restoredPartitionableListStateMetaInfo.updatePartitionStateSerializer(newPartitionStateSerializer);
		if (stateCompatibility.isIncompatible()) {
			throw new StateMigrationException("The new state typeSerializer for operator state must not be incompatible.");
		}

		partitionableListState.setStateMetaInfo(restoredPartitionableListStateMetaInfo);
	}

	accessedStatesByName.put(name, partitionableListState);
	return partitionableListState;
}
 
Example 11
Source File: StreamingRuntimeContext.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public <T> ListState<T> getListState(ListStateDescriptor<T> stateProperties) {
	KeyedStateStore keyedStateStore = checkPreconditionsAndGetKeyedStateStore(stateProperties);
	stateProperties.initializeSerializerUnlessSet(getExecutionConfig());
	return keyedStateStore.getListState(stateProperties);
}