Java Code Examples for org.apache.flink.runtime.checkpoint.OperatorSubtaskState#getManagedOperatorState()

The following examples show how to use org.apache.flink.runtime.checkpoint.OperatorSubtaskState#getManagedOperatorState() . 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: AbstractStreamOperatorTestHarness.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Takes the different {@link OperatorSubtaskState} created by calling {@link #snapshot(long, long)}
 * on different instances of {@link AbstractStreamOperatorTestHarness} (each one representing one subtask)
 * and repacks them into a single {@link OperatorSubtaskState} so that the parallelism of the test
 * can change arbitrarily (i.e. be able to scale both up and down).
 *
 * <p>After repacking the partial states, remember to use
 * {@link #repartitionOperatorState(OperatorSubtaskState, int, int, int, int)} to reshape the state handles
 * to include only those key-group states in the local key-group range and the operator states that would
 * be assigned to the local subtask. Bear in mind that for parallelism greater than one, you
 * have to use the constructor {@link #AbstractStreamOperatorTestHarness(StreamOperator, int, int, int)}.
 *
 * <p><b>NOTE: </b> each of the {@code handles} in the argument list is assumed to be from a single task of a single
 * operator (i.e. chain length of one).
 *
 * <p>For an example of how to use it, have a look at
 * {@link AbstractStreamOperatorTest#testStateAndTimerStateShufflingScalingDown()}.
 *
 * @param handles the different states to be merged.
 * @return the resulting state, or {@code null} if no partial states are specified.
 */
public static OperatorSubtaskState repackageState(OperatorSubtaskState... handles) throws Exception {

	if (handles.length < 1) {
		return null;
	} else if (handles.length == 1) {
		return handles[0];
	}

	List<OperatorStateHandle> mergedManagedOperatorState = new ArrayList<>(handles.length);
	List<OperatorStateHandle> mergedRawOperatorState = new ArrayList<>(handles.length);

	List<KeyedStateHandle> mergedManagedKeyedState = new ArrayList<>(handles.length);
	List<KeyedStateHandle> mergedRawKeyedState = new ArrayList<>(handles.length);

	for (OperatorSubtaskState handle : handles) {

		Collection<OperatorStateHandle> managedOperatorState = handle.getManagedOperatorState();
		Collection<OperatorStateHandle> rawOperatorState = handle.getRawOperatorState();
		Collection<KeyedStateHandle> managedKeyedState = handle.getManagedKeyedState();
		Collection<KeyedStateHandle> rawKeyedState = handle.getRawKeyedState();

		mergedManagedOperatorState.addAll(managedOperatorState);
		mergedRawOperatorState.addAll(rawOperatorState);
		mergedManagedKeyedState.addAll(managedKeyedState);
		mergedRawKeyedState.addAll(rawKeyedState);
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(mergedManagedOperatorState),
		new StateObjectCollection<>(mergedRawOperatorState),
		new StateObjectCollection<>(mergedManagedKeyedState),
		new StateObjectCollection<>(mergedRawKeyedState));
}
 
Example 2
Source File: MetadataSerializer.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
/**
 * 解析 operatorSubtaskState 的 ManagedOperatorState
 *
 * @param operatorSubtaskState operatorSubtaskState
 */
private static void parseManagedOperatorState(OperatorSubtaskState operatorSubtaskState) {
    // 遍历当前 subtask 的 OperatorState
    for (OperatorStateHandle operatorStateHandle : operatorSubtaskState.getManagedOperatorState()) {
        StreamStateHandle delegateStateHandle = operatorStateHandle.getDelegateStateHandle();
        if (delegateStateHandle instanceof FileStateHandle) {
            Path filePath = ((FileStateHandle) delegateStateHandle).getFilePath();
            System.out.println(filePath.getPath());
        }
    }
}
 
Example 3
Source File: ListStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadListOperatorState() throws Exception {
	try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement(1, 0);
		testHarness.processElement(2, 0);
		testHarness.processElement(3, 0);

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		ListStateInputFormat<Integer> format = new ListStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		List<Integer> results = new ArrayList<>();

		while (!format.reachedEnd()) {
			results.add(format.nextRecord(0));
		}

		results.sort(Comparator.naturalOrder());

		Assert.assertEquals(
			"Failed to read correct list state from state backend",
			Arrays.asList(1, 2, 3),
			results);
	}
}
 
Example 4
Source File: BroadcastStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBroadcastState() throws Exception {
	try (TwoInputStreamOperatorTestHarness<Void, Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement2(new StreamRecord<>(1));
		testHarness.processElement2(new StreamRecord<>(2));
		testHarness.processElement2(new StreamRecord<>(3));

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		BroadcastStateInputFormat<Integer, Integer> format = new BroadcastStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		Map<Integer, Integer> results = new HashMap<>(3);

		while (!format.reachedEnd()) {
			Tuple2<Integer, Integer> entry = format.nextRecord(null);
			results.put(entry.f0, entry.f1);
		}

		Map<Integer, Integer> expected = new HashMap<>(3);
		expected.put(1, 1);
		expected.put(2, 2);
		expected.put(3, 3);

		Assert.assertEquals("Failed to read correct list state from state backend", expected, results);
	}
}
 
Example 5
Source File: UnionStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadUnionOperatorState() throws Exception {
	try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement(1, 0);
		testHarness.processElement(2, 0);
		testHarness.processElement(3, 0);

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		UnionStateInputFormat<Integer> format = new UnionStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		List<Integer> results = new ArrayList<>();

		while (!format.reachedEnd()) {
			results.add(format.nextRecord(0));
		}

		results.sort(Comparator.naturalOrder());

		Assert.assertEquals("Failed to read correct list state from state backend", Arrays.asList(1, 2, 3), results);
	}
}
 
Example 6
Source File: AbstractStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Takes the different {@link OperatorSubtaskState} created by calling {@link #snapshot(long, long)}
 * on different instances of {@link AbstractStreamOperatorTestHarness} (each one representing one subtask)
 * and repacks them into a single {@link OperatorSubtaskState} so that the parallelism of the test
 * can change arbitrarily (i.e. be able to scale both up and down).
 *
 * <p>After repacking the partial states, remember to use
 * {@link #repartitionOperatorState(OperatorSubtaskState, int, int, int, int)} to reshape the state handles
 * to include only those key-group states in the local key-group range and the operator states that would
 * be assigned to the local subtask. Bear in mind that for parallelism greater than one, you
 * have to use the constructor {@link #AbstractStreamOperatorTestHarness(StreamOperator, int, int, int)}.
 *
 * <p><b>NOTE: </b> each of the {@code handles} in the argument list is assumed to be from a single task of a single
 * operator (i.e. chain length of one).
 *
 * <p>For an example of how to use it, have a look at
 * {@link AbstractStreamOperatorTest#testStateAndTimerStateShufflingScalingDown()}.
 *
 * @param handles the different states to be merged.
 * @return the resulting state, or {@code null} if no partial states are specified.
 */
public static OperatorSubtaskState repackageState(OperatorSubtaskState... handles) throws Exception {

	if (handles.length < 1) {
		return null;
	} else if (handles.length == 1) {
		return handles[0];
	}

	List<OperatorStateHandle> mergedManagedOperatorState = new ArrayList<>(handles.length);
	List<OperatorStateHandle> mergedRawOperatorState = new ArrayList<>(handles.length);

	List<KeyedStateHandle> mergedManagedKeyedState = new ArrayList<>(handles.length);
	List<KeyedStateHandle> mergedRawKeyedState = new ArrayList<>(handles.length);

	for (OperatorSubtaskState handle : handles) {

		Collection<OperatorStateHandle> managedOperatorState = handle.getManagedOperatorState();
		Collection<OperatorStateHandle> rawOperatorState = handle.getRawOperatorState();
		Collection<KeyedStateHandle> managedKeyedState = handle.getManagedKeyedState();
		Collection<KeyedStateHandle> rawKeyedState = handle.getRawKeyedState();

		mergedManagedOperatorState.addAll(managedOperatorState);
		mergedRawOperatorState.addAll(rawOperatorState);
		mergedManagedKeyedState.addAll(managedKeyedState);
		mergedRawKeyedState.addAll(rawKeyedState);
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(mergedManagedOperatorState),
		new StateObjectCollection<>(mergedRawOperatorState),
		new StateObjectCollection<>(mergedManagedKeyedState),
		new StateObjectCollection<>(mergedRawKeyedState));
}
 
Example 7
Source File: MetadataSerializer.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
/**
 * 解析 operatorSubtaskState 的 ManagedOperatorState
 *
 * @param operatorSubtaskState operatorSubtaskState
 */
private static void parseManagedOperatorState(OperatorSubtaskState operatorSubtaskState) {
    // 遍历当前 subtask 的 OperatorState
    for (OperatorStateHandle operatorStateHandle : operatorSubtaskState.getManagedOperatorState()) {
        StreamStateHandle delegateStateHandle = operatorStateHandle.getDelegateStateHandle();
        if (delegateStateHandle instanceof FileStateHandle) {
            Path filePath = ((FileStateHandle) delegateStateHandle).getFilePath();
            System.out.println(filePath.getPath());
        }
    }
}
 
Example 8
Source File: ListStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadListOperatorState() throws Exception {
	try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement(1, 0);
		testHarness.processElement(2, 0);
		testHarness.processElement(3, 0);

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		ListStateInputFormat<Integer> format = new ListStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		List<Integer> results = new ArrayList<>();

		while (!format.reachedEnd()) {
			results.add(format.nextRecord(0));
		}

		results.sort(Comparator.naturalOrder());

		Assert.assertEquals(
			"Failed to read correct list state from state backend",
			Arrays.asList(1, 2, 3),
			results);
	}
}
 
Example 9
Source File: BroadcastStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBroadcastState() throws Exception {
	try (TwoInputStreamOperatorTestHarness<Void, Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement2(new StreamRecord<>(1));
		testHarness.processElement2(new StreamRecord<>(2));
		testHarness.processElement2(new StreamRecord<>(3));

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		BroadcastStateInputFormat<Integer, Integer> format = new BroadcastStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		Map<Integer, Integer> results = new HashMap<>(3);

		while (!format.reachedEnd()) {
			Tuple2<Integer, Integer> entry = format.nextRecord(null);
			results.put(entry.f0, entry.f1);
		}

		Map<Integer, Integer> expected = new HashMap<>(3);
		expected.put(1, 1);
		expected.put(2, 2);
		expected.put(3, 3);

		Assert.assertEquals("Failed to read correct list state from state backend", expected, results);
	}
}
 
Example 10
Source File: UnionStateInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadUnionOperatorState() throws Exception {
	try (OneInputStreamOperatorTestHarness<Integer, Void> testHarness = getTestHarness()) {
		testHarness.open();

		testHarness.processElement(1, 0);
		testHarness.processElement(2, 0);
		testHarness.processElement(3, 0);

		OperatorSubtaskState subtaskState = testHarness.snapshot(0, 0);
		OperatorState state = new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4);
		state.putState(0, subtaskState);

		OperatorStateInputSplit split = new OperatorStateInputSplit(subtaskState.getManagedOperatorState(), 0);

		UnionStateInputFormat<Integer> format = new UnionStateInputFormat<>(state, descriptor);

		format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
		format.open(split);

		List<Integer> results = new ArrayList<>();

		while (!format.reachedEnd()) {
			results.add(format.nextRecord(0));
		}

		results.sort(Comparator.naturalOrder());

		Assert.assertEquals("Failed to read correct list state from state backend", Arrays.asList(1, 2, 3), results);
	}
}
 
Example 11
Source File: AbstractStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Takes the different {@link OperatorSubtaskState} created by calling {@link #snapshot(long, long)}
 * on different instances of {@link AbstractStreamOperatorTestHarness} (each one representing one subtask)
 * and repacks them into a single {@link OperatorSubtaskState} so that the parallelism of the test
 * can change arbitrarily (i.e. be able to scale both up and down).
 *
 * <p>After repacking the partial states, remember to use
 * {@link #repartitionOperatorState(OperatorSubtaskState, int, int, int, int)} to reshape the state handles
 * to include only those key-group states in the local key-group range and the operator states that would
 * be assigned to the local subtask. Bear in mind that for parallelism greater than one, you
 * have to use the constructor {@link #AbstractStreamOperatorTestHarness(StreamOperator, int, int, int)}.
 *
 * <p><b>NOTE: </b> each of the {@code handles} in the argument list is assumed to be from a single task of a single
 * operator (i.e. chain length of one).
 *
 * <p>For an example of how to use it, have a look at
 * {@link AbstractStreamOperatorTest#testStateAndTimerStateShufflingScalingDown()}.
 *
 * @param handles the different states to be merged.
 * @return the resulting state, or {@code null} if no partial states are specified.
 */
public static OperatorSubtaskState repackageState(OperatorSubtaskState... handles) throws Exception {

	if (handles.length < 1) {
		return null;
	} else if (handles.length == 1) {
		return handles[0];
	}

	List<OperatorStateHandle> mergedManagedOperatorState = new ArrayList<>(handles.length);
	List<OperatorStateHandle> mergedRawOperatorState = new ArrayList<>(handles.length);

	List<KeyedStateHandle> mergedManagedKeyedState = new ArrayList<>(handles.length);
	List<KeyedStateHandle> mergedRawKeyedState = new ArrayList<>(handles.length);

	for (OperatorSubtaskState handle : handles) {

		Collection<OperatorStateHandle> managedOperatorState = handle.getManagedOperatorState();
		Collection<OperatorStateHandle> rawOperatorState = handle.getRawOperatorState();
		Collection<KeyedStateHandle> managedKeyedState = handle.getManagedKeyedState();
		Collection<KeyedStateHandle> rawKeyedState = handle.getRawKeyedState();

		mergedManagedOperatorState.addAll(managedOperatorState);
		mergedRawOperatorState.addAll(rawOperatorState);
		mergedManagedKeyedState.addAll(managedKeyedState);
		mergedRawKeyedState.addAll(rawKeyedState);
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(mergedManagedOperatorState),
		new StateObjectCollection<>(mergedRawOperatorState),
		new StateObjectCollection<>(mergedManagedKeyedState),
		new StateObjectCollection<>(mergedRawKeyedState));
}
 
Example 12
Source File: AbstractStreamOperatorTestHarness.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the reshaped the state handles to include only those key-group states in the local key-group range
 * and the operator states that would be assigned to the local subtask.
 */
public static OperatorSubtaskState repartitionOperatorState(
	final OperatorSubtaskState operatorStateHandles,
	final int numKeyGroups,
	final int oldParallelism,
	final int newParallelism,
	final int subtaskIndex) {

	Preconditions.checkNotNull(operatorStateHandles, "the previous operatorStateHandles should not be null.");

	// create a new OperatorStateHandles that only contains the state for our key-groups

	List<KeyGroupRange> keyGroupPartitions = StateAssignmentOperation.createKeyGroupPartitions(
		numKeyGroups,
		newParallelism);

	KeyGroupRange localKeyGroupRange = keyGroupPartitions.get(subtaskIndex);

	List<KeyedStateHandle> localManagedKeyGroupState = StateAssignmentOperation.getKeyedStateHandles(
		operatorStateHandles.getManagedKeyedState(),
		localKeyGroupRange);

	List<KeyedStateHandle> localRawKeyGroupState = StateAssignmentOperation.getKeyedStateHandles(
		operatorStateHandles.getRawKeyedState(),
		localKeyGroupRange);

	StateObjectCollection<OperatorStateHandle> managedOperatorStates = operatorStateHandles.getManagedOperatorState();
	Collection<OperatorStateHandle> localManagedOperatorState;

	if (!managedOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> managedOperatorState =
			managedOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localManagedOperatorState = operatorStateRepartitioner.repartitionState(
			managedOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localManagedOperatorState = Collections.emptyList();
	}

	StateObjectCollection<OperatorStateHandle> rawOperatorStates = operatorStateHandles.getRawOperatorState();
	Collection<OperatorStateHandle> localRawOperatorState;

	if (!rawOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> rawOperatorState =
			rawOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localRawOperatorState = operatorStateRepartitioner.repartitionState(
			rawOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localRawOperatorState = Collections.emptyList();
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(nullToEmptyCollection(localManagedOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localManagedKeyGroupState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawKeyGroupState)));
}
 
Example 13
Source File: AbstractStreamOperatorTestHarness.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the reshaped the state handles to include only those key-group states in the local key-group range
 * and the operator states that would be assigned to the local subtask.
 */
public static OperatorSubtaskState repartitionOperatorState(
	final OperatorSubtaskState operatorStateHandles,
	final int numKeyGroups,
	final int oldParallelism,
	final int newParallelism,
	final int subtaskIndex) {

	Preconditions.checkNotNull(operatorStateHandles, "the previous operatorStateHandles should not be null.");

	// create a new OperatorStateHandles that only contains the state for our key-groups

	List<KeyGroupRange> keyGroupPartitions = StateAssignmentOperation.createKeyGroupPartitions(
		numKeyGroups,
		newParallelism);

	KeyGroupRange localKeyGroupRange = keyGroupPartitions.get(subtaskIndex);

	List<KeyedStateHandle> localManagedKeyGroupState = StateAssignmentOperation.getKeyedStateHandles(
		operatorStateHandles.getManagedKeyedState(),
		localKeyGroupRange);

	List<KeyedStateHandle> localRawKeyGroupState = StateAssignmentOperation.getKeyedStateHandles(
		operatorStateHandles.getRawKeyedState(),
		localKeyGroupRange);

	StateObjectCollection<OperatorStateHandle> managedOperatorStates = operatorStateHandles.getManagedOperatorState();
	Collection<OperatorStateHandle> localManagedOperatorState;

	if (!managedOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> managedOperatorState =
			managedOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localManagedOperatorState = operatorStateRepartitioner.repartitionState(
			managedOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localManagedOperatorState = Collections.emptyList();
	}

	StateObjectCollection<OperatorStateHandle> rawOperatorStates = operatorStateHandles.getRawOperatorState();
	Collection<OperatorStateHandle> localRawOperatorState;

	if (!rawOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> rawOperatorState =
			rawOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localRawOperatorState = operatorStateRepartitioner.repartitionState(
			rawOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localRawOperatorState = Collections.emptyList();
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(nullToEmptyCollection(localManagedOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localManagedKeyGroupState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawKeyGroupState)));
}
 
Example 14
Source File: AbstractStreamOperatorTestHarness.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the reshaped the state handles to include only those key-group states in the local key-group range
 * and the operator states that would be assigned to the local subtask.
 */
public static OperatorSubtaskState repartitionOperatorState(
	final OperatorSubtaskState operatorStateHandles,
	final int numKeyGroups,
	final int oldParallelism,
	final int newParallelism,
	final int subtaskIndex) {

	Preconditions.checkNotNull(operatorStateHandles, "the previous operatorStateHandles should not be null.");

	// create a new OperatorStateHandles that only contains the state for our key-groups

	List<KeyGroupRange> keyGroupPartitions = StateAssignmentOperation.createKeyGroupPartitions(
		numKeyGroups,
		newParallelism);

	KeyGroupRange localKeyGroupRange = keyGroupPartitions.get(subtaskIndex);

	List<KeyedStateHandle> localManagedKeyGroupState = new ArrayList<>();
	StateAssignmentOperation.extractIntersectingState(
		operatorStateHandles.getManagedKeyedState(),
		localKeyGroupRange,
		localManagedKeyGroupState);

	List<KeyedStateHandle> localRawKeyGroupState = new ArrayList<>();
	StateAssignmentOperation.extractIntersectingState(
		operatorStateHandles.getRawKeyedState(),
		localKeyGroupRange,
		localRawKeyGroupState);

	StateObjectCollection<OperatorStateHandle> managedOperatorStates = operatorStateHandles.getManagedOperatorState();
	Collection<OperatorStateHandle> localManagedOperatorState;

	if (!managedOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> managedOperatorState =
			managedOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localManagedOperatorState = operatorStateRepartitioner.repartitionState(
			managedOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localManagedOperatorState = Collections.emptyList();
	}

	StateObjectCollection<OperatorStateHandle> rawOperatorStates = operatorStateHandles.getRawOperatorState();
	Collection<OperatorStateHandle> localRawOperatorState;

	if (!rawOperatorStates.isEmpty()) {
		List<List<OperatorStateHandle>> rawOperatorState =
			rawOperatorStates.stream().map(Collections::singletonList).collect(Collectors.toList());

		localRawOperatorState = operatorStateRepartitioner.repartitionState(
			rawOperatorState,
			oldParallelism,
			newParallelism).get(subtaskIndex);
	} else {
		localRawOperatorState = Collections.emptyList();
	}

	return new OperatorSubtaskState(
		new StateObjectCollection<>(nullToEmptyCollection(localManagedOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawOperatorState)),
		new StateObjectCollection<>(nullToEmptyCollection(localManagedKeyGroupState)),
		new StateObjectCollection<>(nullToEmptyCollection(localRawKeyGroupState)));
}