Search in sources :

Example 31 with OperatorStateHandle

use of org.apache.flink.runtime.state.OperatorStateHandle in project flink by apache.

the class TaskStateSnapshotTest method hasState.

@Test
public void hasState() {
    Random random = new Random(0x42);
    TaskStateSnapshot taskStateSnapshot = new TaskStateSnapshot();
    Assert.assertFalse(taskStateSnapshot.hasState());
    OperatorSubtaskState emptyOperatorSubtaskState = OperatorSubtaskState.builder().build();
    Assert.assertFalse(emptyOperatorSubtaskState.hasState());
    taskStateSnapshot.putSubtaskStateByOperatorID(new OperatorID(), emptyOperatorSubtaskState);
    Assert.assertFalse(taskStateSnapshot.hasState());
    OperatorStateHandle stateHandle = StateHandleDummyUtil.createNewOperatorStateHandle(2, random);
    OperatorSubtaskState nonEmptyOperatorSubtaskState = OperatorSubtaskState.builder().setManagedOperatorState(stateHandle).build();
    Assert.assertTrue(nonEmptyOperatorSubtaskState.hasState());
    taskStateSnapshot.putSubtaskStateByOperatorID(new OperatorID(), nonEmptyOperatorSubtaskState);
    Assert.assertTrue(taskStateSnapshot.hasState());
}
Also used : Random(java.util.Random) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) Test(org.junit.Test)

Example 32 with OperatorStateHandle

use of org.apache.flink.runtime.state.OperatorStateHandle in project flink by apache.

the class TaskStateSnapshotTest method getStateSize.

@Test
public void getStateSize() {
    Random random = new Random(0x42);
    TaskStateSnapshot taskStateSnapshot = new TaskStateSnapshot();
    Assert.assertEquals(0, taskStateSnapshot.getStateSize());
    OperatorSubtaskState emptyOperatorSubtaskState = OperatorSubtaskState.builder().build();
    Assert.assertFalse(emptyOperatorSubtaskState.hasState());
    taskStateSnapshot.putSubtaskStateByOperatorID(new OperatorID(), emptyOperatorSubtaskState);
    Assert.assertEquals(0, taskStateSnapshot.getStateSize());
    OperatorStateHandle stateHandle_1 = StateHandleDummyUtil.createNewOperatorStateHandle(2, random);
    OperatorSubtaskState nonEmptyOperatorSubtaskState_1 = OperatorSubtaskState.builder().setManagedOperatorState(stateHandle_1).build();
    OperatorStateHandle stateHandle_2 = StateHandleDummyUtil.createNewOperatorStateHandle(2, random);
    OperatorSubtaskState nonEmptyOperatorSubtaskState_2 = OperatorSubtaskState.builder().setRawOperatorState(stateHandle_2).build();
    taskStateSnapshot.putSubtaskStateByOperatorID(new OperatorID(), nonEmptyOperatorSubtaskState_1);
    taskStateSnapshot.putSubtaskStateByOperatorID(new OperatorID(), nonEmptyOperatorSubtaskState_2);
    long totalSize = stateHandle_1.getStateSize() + stateHandle_2.getStateSize();
    Assert.assertEquals(totalSize, taskStateSnapshot.getStateSize());
}
Also used : Random(java.util.Random) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) Test(org.junit.Test)

Example 33 with OperatorStateHandle

use of org.apache.flink.runtime.state.OperatorStateHandle in project flink by apache.

the class RoundRobinOperatorStateRepartitioner method groupByStateMode.

/**
 * Group by the different named states.
 */
@SuppressWarnings("unchecked, rawtype")
private GroupByStateNameResults groupByStateMode(List<List<OperatorStateHandle>> previousParallelSubtaskStates) {
    // Reorganize: group by (State Name -> StreamStateHandle + StateMetaInfo)
    EnumMap<OperatorStateHandle.Mode, Map<String, List<Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo>>>> nameToStateByMode = new EnumMap<>(OperatorStateHandle.Mode.class);
    for (OperatorStateHandle.Mode mode : OperatorStateHandle.Mode.values()) {
        nameToStateByMode.put(mode, new HashMap<>());
    }
    for (List<OperatorStateHandle> previousParallelSubtaskState : previousParallelSubtaskStates) {
        for (OperatorStateHandle operatorStateHandle : previousParallelSubtaskState) {
            if (operatorStateHandle == null) {
                continue;
            }
            final Set<Map.Entry<String, OperatorStateHandle.StateMetaInfo>> partitionOffsetEntries = operatorStateHandle.getStateNameToPartitionOffsets().entrySet();
            for (Map.Entry<String, OperatorStateHandle.StateMetaInfo> e : partitionOffsetEntries) {
                OperatorStateHandle.StateMetaInfo metaInfo = e.getValue();
                Map<String, List<Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo>>> nameToState = nameToStateByMode.get(metaInfo.getDistributionMode());
                List<Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo>> stateLocations = nameToState.computeIfAbsent(e.getKey(), k -> new ArrayList<>(previousParallelSubtaskStates.size() * partitionOffsetEntries.size()));
                stateLocations.add(Tuple2.of(operatorStateHandle.getDelegateStateHandle(), e.getValue()));
            }
        }
    }
    return new GroupByStateNameResults(nameToStateByMode);
}
Also used : OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) Tuple2(org.apache.flink.api.java.tuple.Tuple2) ArrayList(java.util.ArrayList) List(java.util.List) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 34 with OperatorStateHandle

use of org.apache.flink.runtime.state.OperatorStateHandle in project flink by apache.

the class RoundRobinOperatorStateRepartitioner method collectStates.

/**
 * Collect the states from given parallelSubtaskStates with the specific {@code mode}.
 */
private Map<String, StateEntry> collectStates(List<List<OperatorStateHandle>> parallelSubtaskStates, OperatorStateHandle.Mode mode) {
    Map<String, StateEntry> states = new HashMap<>(parallelSubtaskStates.size());
    for (int i = 0; i < parallelSubtaskStates.size(); ++i) {
        final int subtaskIndex = i;
        List<OperatorStateHandle> subTaskState = parallelSubtaskStates.get(i);
        for (OperatorStateHandle operatorStateHandle : subTaskState) {
            if (operatorStateHandle == null) {
                continue;
            }
            final Set<Map.Entry<String, OperatorStateHandle.StateMetaInfo>> partitionOffsetEntries = operatorStateHandle.getStateNameToPartitionOffsets().entrySet();
            partitionOffsetEntries.stream().filter(entry -> entry.getValue().getDistributionMode().equals(mode)).forEach(entry -> {
                StateEntry stateEntry = states.computeIfAbsent(entry.getKey(), k -> new StateEntry(parallelSubtaskStates.size() * partitionOffsetEntries.size(), parallelSubtaskStates.size()));
                stateEntry.addEntry(subtaskIndex, Tuple2.of(operatorStateHandle.getDelegateStateHandle(), entry.getValue()));
            });
        }
    }
    return states;
}
Also used : Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) EnumMap(java.util.EnumMap) Set(java.util.Set) HashMap(java.util.HashMap) OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) Preconditions(org.apache.flink.util.Preconditions) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Internal(org.apache.flink.annotation.Internal) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) BitSet(java.util.BitSet) HashMap(java.util.HashMap) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle)

Example 35 with OperatorStateHandle

use of org.apache.flink.runtime.state.OperatorStateHandle in project flink by apache.

the class RoundRobinOperatorStateRepartitioner method repartitionBroadcastState.

/**
 * Repartition BROADCAST state.
 */
private void repartitionBroadcastState(Map<String, List<Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo>>> broadcastState, List<Map<StreamStateHandle, OperatorStateHandle>> mergeMapList) {
    int newParallelism = mergeMapList.size();
    for (int i = 0; i < newParallelism; ++i) {
        final Map<StreamStateHandle, OperatorStateHandle> mergeMap = mergeMapList.get(i);
        // for each name, pick the i-th entry
        for (Map.Entry<String, List<Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo>>> e : broadcastState.entrySet()) {
            int previousParallelism = e.getValue().size();
            Tuple2<StreamStateHandle, OperatorStateHandle.StateMetaInfo> handleWithMetaInfo = e.getValue().get(i % previousParallelism);
            OperatorStateHandle operatorStateHandle = mergeMap.get(handleWithMetaInfo.f0);
            if (operatorStateHandle == null) {
                operatorStateHandle = new OperatorStreamStateHandle(new HashMap<>(broadcastState.size()), handleWithMetaInfo.f0);
                mergeMap.put(handleWithMetaInfo.f0, operatorStateHandle);
            }
            operatorStateHandle.getStateNameToPartitionOffsets().put(e.getKey(), handleWithMetaInfo.f1);
        }
    }
}
Also used : OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

OperatorStateHandle (org.apache.flink.runtime.state.OperatorStateHandle)64 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)22 Test (org.junit.Test)21 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)20 OperatorStreamStateHandle (org.apache.flink.runtime.state.OperatorStreamStateHandle)19 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)17 List (java.util.List)15 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)15 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)15 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)14 Map (java.util.Map)13 Collection (java.util.Collection)10 StateObjectCollection (org.apache.flink.runtime.checkpoint.StateObjectCollection)10 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)10 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)9 EnumMap (java.util.EnumMap)8 JobID (org.apache.flink.api.common.JobID)8 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)8