Search in sources :

Example 11 with StateHandleID

use of org.apache.flink.runtime.state.StateHandleID 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 12 with StateHandleID

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

the class RocksDBStateDownloader method createDownloadRunnables.

private List<Runnable> createDownloadRunnables(Map<StateHandleID, StreamStateHandle> stateHandleMap, Path restoreInstancePath, CloseableRegistry closeableRegistry) {
    List<Runnable> runnables = new ArrayList<>(stateHandleMap.size());
    for (Map.Entry<StateHandleID, StreamStateHandle> entry : stateHandleMap.entrySet()) {
        StateHandleID stateHandleID = entry.getKey();
        StreamStateHandle remoteFileHandle = entry.getValue();
        Path path = restoreInstancePath.resolve(stateHandleID.toString());
        runnables.add(ThrowingRunnable.unchecked(() -> downloadDataForStateHandle(path, remoteFileHandle, closeableRegistry)));
    }
    return runnables;
}
Also used : Path(java.nio.file.Path) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) StateHandleID(org.apache.flink.runtime.state.StateHandleID) ThrowingRunnable(org.apache.flink.util.function.ThrowingRunnable) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 13 with StateHandleID

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

the class RocksDBStateUploaderTest method testMultiThreadUploadCorrectly.

/**
 * Test that upload files with multi-thread correctly.
 */
@Test
public void testMultiThreadUploadCorrectly() throws Exception {
    File checkpointPrivateFolder = temporaryFolder.newFolder("private");
    org.apache.flink.core.fs.Path checkpointPrivateDirectory = org.apache.flink.core.fs.Path.fromLocalFile(checkpointPrivateFolder);
    File checkpointSharedFolder = temporaryFolder.newFolder("shared");
    org.apache.flink.core.fs.Path checkpointSharedDirectory = org.apache.flink.core.fs.Path.fromLocalFile(checkpointSharedFolder);
    FileSystem fileSystem = checkpointPrivateDirectory.getFileSystem();
    int fileStateSizeThreshold = 1024;
    int writeBufferSize = 4096;
    FsCheckpointStreamFactory checkpointStreamFactory = new FsCheckpointStreamFactory(fileSystem, checkpointPrivateDirectory, checkpointSharedDirectory, fileStateSizeThreshold, writeBufferSize);
    String localFolder = "local";
    temporaryFolder.newFolder(localFolder);
    int sstFileCount = 6;
    Map<StateHandleID, Path> sstFilePaths = generateRandomSstFiles(localFolder, sstFileCount, fileStateSizeThreshold);
    try (RocksDBStateUploader rocksDBStateUploader = new RocksDBStateUploader(5)) {
        Map<StateHandleID, StreamStateHandle> sstFiles = rocksDBStateUploader.uploadFilesToCheckpointFs(sstFilePaths, checkpointStreamFactory, CheckpointedStateScope.SHARED, new CloseableRegistry());
        for (Map.Entry<StateHandleID, Path> entry : sstFilePaths.entrySet()) {
            assertStateContentEqual(entry.getValue(), sstFiles.get(entry.getKey()).openInputStream());
        }
    }
}
Also used : Path(java.nio.file.Path) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) StateHandleID(org.apache.flink.runtime.state.StateHandleID) FileSystem(org.apache.flink.core.fs.FileSystem) FsCheckpointStreamFactory(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with StateHandleID

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

the class RocksDBStateUploaderTest method generateRandomSstFiles.

private Map<StateHandleID, Path> generateRandomSstFiles(String localFolder, int sstFileCount, int fileStateSizeThreshold) throws IOException {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    Map<StateHandleID, Path> sstFilePaths = new HashMap<>(sstFileCount);
    for (int i = 0; i < sstFileCount; ++i) {
        File file = temporaryFolder.newFile(String.format("%s/%d.sst", localFolder, i));
        generateRandomFileContent(file.getPath(), random.nextInt(1_000_000) + fileStateSizeThreshold);
        sstFilePaths.put(new StateHandleID(String.valueOf(i)), file.toPath());
    }
    return sstFilePaths;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) StateHandleID(org.apache.flink.runtime.state.StateHandleID) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) File(java.io.File)

Example 15 with StateHandleID

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

the class RocksIncrementalSnapshotStrategyTest method testCheckpointIsIncremental.

// Verify the next checkpoint is still incremental after a savepoint completed.
@Test
public void testCheckpointIsIncremental() throws Exception {
    try (CloseableRegistry closeableRegistry = new CloseableRegistry();
        RocksIncrementalSnapshotStrategy checkpointSnapshotStrategy = createSnapshotStrategy(closeableRegistry)) {
        FsCheckpointStreamFactory checkpointStreamFactory = createFsCheckpointStreamFactory();
        // make and notify checkpoint with id 1
        snapshot(1L, checkpointSnapshotStrategy, checkpointStreamFactory, closeableRegistry);
        checkpointSnapshotStrategy.notifyCheckpointComplete(1L);
        // notify savepoint with id 2
        checkpointSnapshotStrategy.notifyCheckpointComplete(2L);
        // make checkpoint with id 3
        IncrementalRemoteKeyedStateHandle incrementalRemoteKeyedStateHandle3 = snapshot(3L, checkpointSnapshotStrategy, checkpointStreamFactory, closeableRegistry);
        // If 3rd checkpoint's placeholderStateHandleCount > 0,it means 3rd checkpoint is
        // incremental.
        Map<StateHandleID, StreamStateHandle> sharedState3 = incrementalRemoteKeyedStateHandle3.getSharedState();
        long placeholderStateHandleCount = sharedState3.entrySet().stream().filter(e -> e.getValue() instanceof PlaceholderStreamStateHandle).count();
        Assert.assertTrue(placeholderStateHandleCount > 0);
    }
}
Also used : RocksDBResource(org.apache.flink.contrib.streaming.state.RocksDBResource) StateHandleID(org.apache.flink.runtime.state.StateHandleID) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) ResourceGuard(org.apache.flink.util.ResourceGuard) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) RocksDBOptions(org.apache.flink.contrib.streaming.state.RocksDBOptions) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) RocksDB(org.rocksdb.RocksDB) TestLocalRecoveryConfig(org.apache.flink.runtime.state.TestLocalRecoveryConfig) Map(java.util.Map) RocksDBException(org.rocksdb.RocksDBException) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) StateDescriptor(org.apache.flink.api.common.state.StateDescriptor) CompositeKeySerializationUtils(org.apache.flink.runtime.state.CompositeKeySerializationUtils) Test(org.junit.Test) IOException(java.io.IOException) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) UUID(java.util.UUID) ArrayListSerializer(org.apache.flink.runtime.state.ArrayListSerializer) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) File(java.io.File) PlaceholderStreamStateHandle(org.apache.flink.runtime.state.PlaceholderStreamStateHandle) Rule(org.junit.Rule) TreeMap(java.util.TreeMap) FsCheckpointStreamFactory(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) RegisteredKeyValueStateBackendMetaInfo(org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo) RocksDBKeyedStateBackend(org.apache.flink.contrib.streaming.state.RocksDBKeyedStateBackend) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) SortedMap(java.util.SortedMap) LocalFileSystem.getSharedInstance(org.apache.flink.core.fs.local.LocalFileSystem.getSharedInstance) RocksDBStateUploader(org.apache.flink.contrib.streaming.state.RocksDBStateUploader) Path.fromLocalFile(org.apache.flink.core.fs.Path.fromLocalFile) PlaceholderStreamStateHandle(org.apache.flink.runtime.state.PlaceholderStreamStateHandle) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) PlaceholderStreamStateHandle(org.apache.flink.runtime.state.PlaceholderStreamStateHandle) StateHandleID(org.apache.flink.runtime.state.StateHandleID) FsCheckpointStreamFactory(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) Test(org.junit.Test)

Aggregations

StateHandleID (org.apache.flink.runtime.state.StateHandleID)19 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)18 HashMap (java.util.HashMap)14 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)14 IncrementalRemoteKeyedStateHandle (org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle)10 OperatorStreamStateHandle (org.apache.flink.runtime.state.OperatorStreamStateHandle)8 Test (org.junit.Test)8 Map (java.util.Map)7 PlaceholderStreamStateHandle (org.apache.flink.runtime.state.PlaceholderStreamStateHandle)7 IOException (java.io.IOException)5 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)5 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)5 File (java.io.File)4 UUID (java.util.UUID)4 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)4 TestingStreamStateHandle (org.apache.flink.runtime.state.TestingStreamStateHandle)4 DeclineCheckpoint (org.apache.flink.runtime.messages.checkpoint.DeclineCheckpoint)3 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)3