Search in sources :

Example 6 with StateChange

use of org.apache.flink.runtime.state.changelog.StateChange in project flink by apache.

the class StateChangelogStorageTest method extract.

private Map<Integer, List<byte[]>> extract(T handle, StateChangelogHandleReader<T> reader) throws Exception {
    Map<Integer, List<byte[]>> changes = new HashMap<>();
    try (CloseableIterator<StateChange> it = reader.getChanges(handle)) {
        while (it.hasNext()) {
            StateChange change = it.next();
            changes.computeIfAbsent(change.getKeyGroup(), k -> new ArrayList<>()).add(change.getChange());
        }
    }
    return changes;
}
Also used : StateChangelogStorage(org.apache.flink.runtime.state.changelog.StateChangelogStorage) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) ChangelogStateHandle(org.apache.flink.runtime.state.changelog.ChangelogStateHandle) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) StateChangelogHandleReader(org.apache.flink.runtime.state.changelog.StateChangelogHandleReader) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) Iterator(java.util.Iterator) StateChange(org.apache.flink.runtime.state.changelog.StateChange) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) CloseableIterator(org.apache.flink.util.CloseableIterator) List(java.util.List) Rule(org.junit.Rule) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) Assert.assertFalse(org.junit.Assert.assertFalse) Function.identity(java.util.function.Function.identity) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) SequenceNumber(org.apache.flink.runtime.state.changelog.SequenceNumber) StateChangelogWriter(org.apache.flink.runtime.state.changelog.StateChangelogWriter) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) StateChange(org.apache.flink.runtime.state.changelog.StateChange) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with StateChange

use of org.apache.flink.runtime.state.changelog.StateChange in project flink by apache.

the class StateChangeFormat method writeChangeSet.

private void writeChangeSet(DataOutputViewStreamWrapper output, List<StateChange> changes) throws IOException {
    // write in groups to output kg id only once
    Map<Integer, List<StateChange>> byKeyGroup = changes.stream().collect(Collectors.groupingBy(StateChange::getKeyGroup));
    // sort groups to output metadata first (see StateChangeLoggerImpl.COMMON_KEY_GROUP)
    Map<Integer, List<StateChange>> sorted = new TreeMap<>(byKeyGroup);
    output.writeInt(sorted.size());
    for (Map.Entry<Integer, List<StateChange>> entry : sorted.entrySet()) {
        output.writeInt(entry.getValue().size());
        output.writeInt(entry.getKey());
        for (StateChange stateChange : entry.getValue()) {
            output.writeInt(stateChange.getChange().length);
            output.write(stateChange.getChange());
        }
    }
}
Also used : StateChange(org.apache.flink.runtime.state.changelog.StateChange) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 8 with StateChange

use of org.apache.flink.runtime.state.changelog.StateChange in project flink by apache.

the class MetadataV2V3SerializerBase method deserializeKeyedStateHandle.

@VisibleForTesting
@Nullable
static KeyedStateHandle deserializeKeyedStateHandle(DataInputStream dis, @Nullable DeserializationContext context) throws IOException {
    final int type = dis.readByte();
    if (NULL_HANDLE == type) {
        return null;
    } else if (KEY_GROUPS_HANDLE == type || KEY_GROUPS_HANDLE_V2 == type || SAVEPOINT_KEY_GROUPS_HANDLE == type) {
        int startKeyGroup = dis.readInt();
        int numKeyGroups = dis.readInt();
        KeyGroupRange keyGroupRange = KeyGroupRange.of(startKeyGroup, startKeyGroup + numKeyGroups - 1);
        long[] offsets = new long[numKeyGroups];
        for (int i = 0; i < numKeyGroups; ++i) {
            offsets[i] = dis.readLong();
        }
        KeyGroupRangeOffsets keyGroupRangeOffsets = new KeyGroupRangeOffsets(keyGroupRange, offsets);
        StreamStateHandle stateHandle = deserializeStreamStateHandle(dis, context);
        if (SAVEPOINT_KEY_GROUPS_HANDLE == type) {
            return new KeyGroupsSavepointStateHandle(keyGroupRangeOffsets, stateHandle);
        } else {
            StateHandleID stateHandleID = KEY_GROUPS_HANDLE_V2 == type ? new StateHandleID(dis.readUTF()) : StateHandleID.randomStateHandleId();
            return KeyGroupsStateHandle.restore(keyGroupRangeOffsets, stateHandle, stateHandleID);
        }
    } else if (INCREMENTAL_KEY_GROUPS_HANDLE == type || INCREMENTAL_KEY_GROUPS_HANDLE_V2 == type) {
        return deserializeIncrementalStateHandle(dis, context, type);
    } else if (CHANGELOG_HANDLE == type) {
        int startKeyGroup = dis.readInt();
        int numKeyGroups = dis.readInt();
        KeyGroupRange keyGroupRange = KeyGroupRange.of(startKeyGroup, startKeyGroup + numKeyGroups - 1);
        long checkpointedSize = dis.readLong();
        int baseSize = dis.readInt();
        List<KeyedStateHandle> base = new ArrayList<>(baseSize);
        for (int i = 0; i < baseSize; i++) {
            KeyedStateHandle handle = deserializeKeyedStateHandle(dis, context);
            if (handle != null) {
                base.add(handle);
            } else {
                LOG.warn("Unexpected null keyed state handle of materialized part when deserializing changelog state-backend handle");
            }
        }
        int deltaSize = dis.readInt();
        List<ChangelogStateHandle> delta = new ArrayList<>(deltaSize);
        for (int i = 0; i < deltaSize; i++) {
            delta.add((ChangelogStateHandle) deserializeKeyedStateHandle(dis, context));
        }
        long materializationID = dis.readLong();
        StateHandleID stateHandleId = new StateHandleID(dis.readUTF());
        return ChangelogStateBackendHandleImpl.restore(base, delta, keyGroupRange, materializationID, checkpointedSize, stateHandleId);
    } else if (CHANGELOG_BYTE_INCREMENT_HANDLE == type) {
        int start = dis.readInt();
        int numKeyGroups = dis.readInt();
        KeyGroupRange keyGroupRange = KeyGroupRange.of(start, start + numKeyGroups - 1);
        long from = dis.readLong();
        long to = dis.readLong();
        int size = dis.readInt();
        List<StateChange> changes = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            int keyGroup = dis.readInt();
            int bytesSize = dis.readInt();
            byte[] bytes = new byte[bytesSize];
            IOUtils.readFully(dis, bytes, 0, bytesSize);
            changes.add(new StateChange(keyGroup, bytes));
        }
        StateHandleID stateHandleId = new StateHandleID(dis.readUTF());
        return InMemoryChangelogStateHandle.restore(changes, SequenceNumber.of(from), SequenceNumber.of(to), keyGroupRange, stateHandleId);
    } else if (CHANGELOG_FILE_INCREMENT_HANDLE == type) {
        int start = dis.readInt();
        int numKeyGroups = dis.readInt();
        KeyGroupRange keyGroupRange = KeyGroupRange.of(start, start + numKeyGroups - 1);
        int numHandles = dis.readInt();
        List<Tuple2<StreamStateHandle, Long>> streamHandleAndOffset = new ArrayList<>(numHandles);
        for (int i = 0; i < numHandles; i++) {
            long o = dis.readLong();
            StreamStateHandle h = deserializeStreamStateHandle(dis, context);
            streamHandleAndOffset.add(Tuple2.of(h, o));
        }
        long size = dis.readLong();
        long checkpointedSize = dis.readLong();
        StateHandleID stateHandleId = new StateHandleID(dis.readUTF());
        return ChangelogStateHandleStreamImpl.restore(streamHandleAndOffset, keyGroupRange, size, checkpointedSize, stateHandleId);
    } else {
        throw new IllegalStateException("Reading invalid KeyedStateHandle, type: " + type);
    }
}
Also used : KeyGroupRangeOffsets(org.apache.flink.runtime.state.KeyGroupRangeOffsets) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) ArrayList(java.util.ArrayList) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) KeyGroupsSavepointStateHandle(org.apache.flink.runtime.state.KeyGroupsSavepointStateHandle) OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) StateChange(org.apache.flink.runtime.state.changelog.StateChange) StateHandleID(org.apache.flink.runtime.state.StateHandleID) InMemoryChangelogStateHandle(org.apache.flink.runtime.state.changelog.inmemory.InMemoryChangelogStateHandle) ChangelogStateHandle(org.apache.flink.runtime.state.changelog.ChangelogStateHandle) List(java.util.List) ArrayList(java.util.ArrayList) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) Nullable(javax.annotation.Nullable)

Aggregations

StateChange (org.apache.flink.runtime.state.changelog.StateChange)8 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)2 IncrementalRemoteKeyedStateHandle (org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle)2 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)2 KeyGroupsSavepointStateHandle (org.apache.flink.runtime.state.KeyGroupsSavepointStateHandle)2 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)2 ChangelogStateHandle (org.apache.flink.runtime.state.changelog.ChangelogStateHandle)2 InMemoryChangelogStateHandle (org.apache.flink.runtime.state.changelog.inmemory.InMemoryChangelogStateHandle)2 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)2 CloseableIterator (org.apache.flink.util.CloseableIterator)2 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 TreeMap (java.util.TreeMap)1 Function.identity (java.util.function.Function.identity)1