Search in sources :

Example 61 with StreamStateHandle

use of org.apache.flink.runtime.state.StreamStateHandle 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)

Example 62 with StreamStateHandle

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

the class FsCheckpointStateToolset method duplicate.

@Override
public List<StreamStateHandle> duplicate(List<StreamStateHandle> stateHandles) throws IOException {
    final List<CopyRequest> requests = new ArrayList<>();
    for (StreamStateHandle handle : stateHandles) {
        if (!(handle instanceof FileStateHandle)) {
            throw new IllegalArgumentException("We can duplicate only FileStateHandles.");
        }
        final Path srcPath = ((FileStateHandle) handle).getFilePath();
        requests.add(CopyRequest.of(srcPath, getNewDstPath(srcPath.getName())));
    }
    fs.duplicate(requests);
    return IntStream.range(0, stateHandles.size()).mapToObj(idx -> {
        final StreamStateHandle originalHandle = stateHandles.get(idx);
        final Path dst = requests.get(idx).getDestination();
        if (originalHandle instanceof RelativeFileStateHandle) {
            return new RelativeFileStateHandle(dst, dst.getName(), originalHandle.getStateSize());
        } else {
            return new FileStateHandle(dst, originalHandle.getStateSize());
        }
    }).collect(Collectors.toList());
}
Also used : Path(org.apache.flink.core.fs.Path) IntStream(java.util.stream.IntStream) List(java.util.List) CheckpointStateToolset(org.apache.flink.runtime.state.CheckpointStateToolset) Path(org.apache.flink.core.fs.Path) EntropyInjector(org.apache.flink.core.fs.EntropyInjector) DuplicatingFileSystem(org.apache.flink.core.fs.DuplicatingFileSystem) CopyRequest(org.apache.flink.core.fs.DuplicatingFileSystem.CopyRequest) IOException(java.io.IOException) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CopyRequest(org.apache.flink.core.fs.DuplicatingFileSystem.CopyRequest) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ArrayList(java.util.ArrayList)

Example 63 with StreamStateHandle

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

the class CheckpointTestUtils method randomlySetSubtaskState.

private static void randomlySetSubtaskState(OperatorState taskState, int[] subtasksToSet, Random random, String basePath) {
    boolean hasOperatorStateBackend = random.nextBoolean();
    boolean hasOperatorStateStream = random.nextBoolean();
    boolean hasKeyedBackend = random.nextInt(4) != 0;
    boolean hasKeyedStream = random.nextInt(4) != 0;
    boolean isIncremental = random.nextInt(3) == 0;
    for (int subtaskIdx : subtasksToSet) {
        StreamStateHandle operatorStateBackend = new ByteStreamStateHandle("b", ("Beautiful").getBytes(ConfigConstants.DEFAULT_CHARSET));
        StreamStateHandle operatorStateStream = new ByteStreamStateHandle("b", ("Beautiful").getBytes(ConfigConstants.DEFAULT_CHARSET));
        Map<String, OperatorStateHandle.StateMetaInfo> offsetsMap = new HashMap<>();
        offsetsMap.put("A", new OperatorStateHandle.StateMetaInfo(new long[] { 0, 10, 20 }, OperatorStateHandle.Mode.SPLIT_DISTRIBUTE));
        offsetsMap.put("B", new OperatorStateHandle.StateMetaInfo(new long[] { 30, 40, 50 }, OperatorStateHandle.Mode.SPLIT_DISTRIBUTE));
        offsetsMap.put("C", new OperatorStateHandle.StateMetaInfo(new long[] { 60, 70, 80 }, OperatorStateHandle.Mode.UNION));
        final OperatorSubtaskState.Builder state = OperatorSubtaskState.builder();
        if (hasOperatorStateBackend) {
            state.setManagedOperatorState(new OperatorStreamStateHandle(offsetsMap, operatorStateBackend));
        }
        if (hasOperatorStateStream) {
            state.setRawOperatorState(new OperatorStreamStateHandle(offsetsMap, operatorStateStream));
        }
        if (hasKeyedBackend) {
            final KeyedStateHandle stateHandle;
            if (isSavepoint(basePath)) {
                stateHandle = createDummyKeyGroupSavepointStateHandle(random, basePath);
            } else if (isIncremental) {
                stateHandle = createDummyIncrementalKeyedStateHandle(random);
            } else {
                stateHandle = createDummyKeyGroupStateHandle(random, null);
            }
            state.setRawKeyedState(stateHandle);
        }
        if (hasKeyedStream) {
            final KeyedStateHandle stateHandle;
            if (isSavepoint(basePath)) {
                stateHandle = createDummyKeyGroupSavepointStateHandle(random, basePath);
            } else {
                stateHandle = createDummyKeyGroupStateHandle(random, null);
            }
            state.setManagedKeyedState(stateHandle);
        }
        state.setInputChannelState((random.nextBoolean() && !isSavepoint(basePath)) ? singleton(createNewInputChannelStateHandle(random.nextInt(5), random)) : empty());
        state.setResultSubpartitionState((random.nextBoolean() && !isSavepoint(basePath)) ? singleton(createNewResultSubpartitionStateHandle(random.nextInt(5), random)) : empty());
        taskState.putState(subtaskIdx, state.build());
    }
}
Also used : HashMap(java.util.HashMap) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) FinishedOperatorSubtaskState(org.apache.flink.runtime.checkpoint.FinishedOperatorSubtaskState) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) OperatorStreamStateHandle(org.apache.flink.runtime.state.OperatorStreamStateHandle) OperatorStateHandle(org.apache.flink.runtime.state.OperatorStateHandle)

Example 64 with StreamStateHandle

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

the class MetadataV3SerializerTest method testSerializeKeyGroupsStateHandle.

@Test
public void testSerializeKeyGroupsStateHandle() throws IOException {
    KeyGroupRangeOffsets offsets = new KeyGroupRangeOffsets(0, 123);
    byte[] data = { 1, 2, 3, 4 };
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        MetadataV2V3SerializerBase.serializeStreamStateHandle(new KeyGroupsStateHandle(offsets, new ByteStreamStateHandle("test", data)), new DataOutputStream(out));
        try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
            StreamStateHandle handle = MetadataV2V3SerializerBase.deserializeStreamStateHandle(new DataInputStream(in), null);
            assertTrue(handle instanceof KeyGroupsStateHandle);
            assertEquals(offsets, ((KeyGroupsStateHandle) handle).getGroupRangeOffsets());
            byte[] deserialized = new byte[data.length];
            try (FSDataInputStream dataStream = handle.openInputStream()) {
                dataStream.read(deserialized);
                assertArrayEquals(data, deserialized);
            }
        }
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyGroupRangeOffsets(org.apache.flink.runtime.state.KeyGroupRangeOffsets) DataOutputStream(java.io.DataOutputStream) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) DataInputStream(java.io.DataInputStream) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) Test(org.junit.Test)

Example 65 with StreamStateHandle

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

the class GenericWriteAheadSink method notifyCheckpointComplete.

@Override
public void notifyCheckpointComplete(long checkpointId) throws Exception {
    super.notifyCheckpointComplete(checkpointId);
    synchronized (pendingCheckpoints) {
        Iterator<PendingCheckpoint> pendingCheckpointIt = pendingCheckpoints.iterator();
        while (pendingCheckpointIt.hasNext()) {
            PendingCheckpoint pendingCheckpoint = pendingCheckpointIt.next();
            long pastCheckpointId = pendingCheckpoint.checkpointId;
            int subtaskId = pendingCheckpoint.subtaskId;
            long timestamp = pendingCheckpoint.timestamp;
            StreamStateHandle streamHandle = pendingCheckpoint.stateHandle;
            if (pastCheckpointId <= checkpointId) {
                try {
                    if (!committer.isCheckpointCommitted(subtaskId, pastCheckpointId)) {
                        try (FSDataInputStream in = streamHandle.openInputStream()) {
                            boolean success = sendValues(new ReusingMutableToRegularIteratorWrapper<>(new InputViewIterator<>(new DataInputViewStreamWrapper(in), serializer), serializer), pastCheckpointId, timestamp);
                            if (success) {
                                // in case the checkpoint was successfully committed,
                                // discard its state from the backend and mark it for removal
                                // in case it failed, we retry on the next checkpoint
                                committer.commitCheckpoint(subtaskId, pastCheckpointId);
                                streamHandle.discardState();
                                pendingCheckpointIt.remove();
                            }
                        }
                    } else {
                        streamHandle.discardState();
                        pendingCheckpointIt.remove();
                    }
                } catch (Exception e) {
                    // we have to break here to prevent a new (later) checkpoint
                    // from being committed before this one
                    LOG.error("Could not commit checkpoint.", e);
                    break;
                }
            }
        }
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) IOException(java.io.IOException) InputViewIterator(org.apache.flink.runtime.io.disk.InputViewIterator)

Aggregations

StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)84 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)36 HashMap (java.util.HashMap)32 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 OperatorStateHandle (org.apache.flink.runtime.state.OperatorStateHandle)19 IOException (java.io.IOException)18 StateHandleID (org.apache.flink.runtime.state.StateHandleID)18 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)17 OperatorStreamStateHandle (org.apache.flink.runtime.state.OperatorStreamStateHandle)17 Map (java.util.Map)16 List (java.util.List)13 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)13 JobID (org.apache.flink.api.common.JobID)11 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)11 IncrementalRemoteKeyedStateHandle (org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle)11 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)10 CheckpointStateOutputStream (org.apache.flink.runtime.state.CheckpointStateOutputStream)10 Path (org.apache.flink.core.fs.Path)9 DeclineCheckpoint (org.apache.flink.runtime.messages.checkpoint.DeclineCheckpoint)9