You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "rkhachatryan (via GitHub)" <gi...@apache.org> on 2023/06/19 21:52:01 UTC
[GitHub] [flink] rkhachatryan commented on a diff in pull request #22788: [FLINK-32345][state] Improve parallel download of RocksDB incremental state.
rkhachatryan commented on code in PR #22788:
URL: https://github.com/apache/flink/pull/22788#discussion_r1234494762
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/restore/RocksDBIncrementalRestoreOperation.java:
##########
@@ -284,19 +300,45 @@ private void cleanUpPathQuietly(@Nonnull Path path) {
private void restoreWithRescaling(Collection<KeyedStateHandle> restoreStateHandles)
throws Exception {
- // Prepare for restore with rescaling
- KeyedStateHandle initialHandle =
+ Preconditions.checkArgument(restoreStateHandles != null && !restoreStateHandles.isEmpty());
+
+ // Choose the best state handle for the initial DB
+ final KeyedStateHandle selectedInitialHandle =
RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
restoreStateHandles, keyGroupRange, overlapFractionThreshold);
- // Init base DB instance
- if (initialHandle != null) {
- restoreStateHandles.remove(initialHandle);
- initDBWithRescaling(initialHandle);
- } else {
- this.rocksHandle.openDB();
+ Preconditions.checkNotNull(selectedInitialHandle);
+
+ final Path absolutInstanceBasePath = instanceBasePath.getAbsoluteFile().toPath();
+
+ final List<StateHandleDownloadSpec> allDownloadSpecs =
+ new ArrayList<>(restoreStateHandles.size());
+
+ // Prepare and collect all the download request to pull remote state to a local directory
+ for (KeyedStateHandle stateHandle : restoreStateHandles) {
+ if (!(stateHandle instanceof IncrementalRemoteKeyedStateHandle)) {
+ throw unexpectedStateHandleException(
+ IncrementalRemoteKeyedStateHandle.class, stateHandle.getClass());
+ }
+ StateHandleDownloadSpec downloadRequest =
+ new StateHandleDownloadSpec(
+ (IncrementalRemoteKeyedStateHandle) stateHandle,
+ absolutInstanceBasePath.resolve(UUID.randomUUID().toString()));
+
+ if (stateHandle == selectedInitialHandle) {
+ // Insert the initial handle at the head of the list
+ allDownloadSpecs.add(0, downloadRequest);
+ } else {
+ allDownloadSpecs.add(downloadRequest);
+ }
Review Comment:
`add(0)`/`get(0)` seems unclear and fragile to me.
How about using map for `allDownloadSpecs` instead?
```
Map<KeyedStateHandle,StateHandleDownloadSpec> allDownloadSpecs = new HashMap<>();
...
allDownloadSpecs.put(
stateHandle,
new StateHandleDownloadSpec(
(IncrementalRemoteKeyedStateHandle) stateHandle,
absolutInstanceBasePath.resolve(UUID.randomUUID().toString())));
...
initBaseDBForRescaling(allDownloadSpecs.get(selectedInitialHandle));
```
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateDownloader.java:
##########
@@ -90,27 +80,62 @@ private void downloadDataForAllStateHandles(
} else {
throw new FlinkRuntimeException("Failed to download data for state handles.", e);
}
+ } finally {
+ // Unregister and close the internal closer. In a failure case, this should interrupt
+ // ongoing downloads.
+ closeableRegistry.unregisterCloseable(internalCloser);
+ IOUtils.closeQuietly(internalCloser);
Review Comment:
I think it should be
```
if (closeableRegistry.unregisterCloseable(internalCloser)) {
IOUtils.closeQuietly(internalCloser);
}
```
to prevent double close.
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/restore/RocksDBIncrementalRestoreOperation.java:
##########
@@ -306,24 +348,17 @@ private void restoreWithRescaling(Collection<KeyedStateHandle> restoreStateHandl
CompositeKeySerializationUtils.serializeKeyGroup(
keyGroupRange.getEndKeyGroup() + 1, stopKeyGroupPrefixBytes);
- for (KeyedStateHandle rawStateHandle : restoreStateHandles) {
+ // Insert all remaining state through creating temporary RocksDB instances
+ for (int stateIdx = 1; stateIdx < allDownloadSpecs.size(); ++stateIdx) {
- if (!(rawStateHandle instanceof IncrementalRemoteKeyedStateHandle)) {
- throw unexpectedStateHandleException(
- IncrementalRemoteKeyedStateHandle.class, rawStateHandle.getClass());
- }
+ final StateHandleDownloadSpec downloadRequest = allDownloadSpecs.get(stateIdx);
Review Comment:
This can be reverted back to foreach over `allDownloadSpecs.values()` once `allDownloadSpecs` is a map.
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateDownloader.java:
##########
@@ -90,27 +80,62 @@ private void downloadDataForAllStateHandles(
} else {
throw new FlinkRuntimeException("Failed to download data for state handles.", e);
}
+ } finally {
+ // Unregister and close the internal closer. In a failure case, this should interrupt
+ // ongoing downloads.
+ closeableRegistry.unregisterCloseable(internalCloser);
+ IOUtils.closeQuietly(internalCloser);
+ if (failureCleanupRequired) {
+ // Cleanup on exception: cancel all tasks and delete the created directories
+ futures.forEach(future -> future.cancel(true));
+ downloadRequests.stream()
+ .map(StateHandleDownloadSpec::getDownloadDestination)
+ .map(Path::toFile)
+ .forEach(FileUtils::deleteDirectoryQuietly);
+ }
}
}
- private List<Runnable> createDownloadRunnables(
- Map<StateHandleID, StreamStateHandle> stateHandleMap,
- Path restoreInstancePath,
+ /** Asynchronously runs the specified download requests on executorService. */
+ private Stream<CompletableFuture<Void>> transferAllStateDataToDirectoryAsync(
+ Collection<StateHandleDownloadSpec> handleWithPaths,
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());
+ return createDownloadRunnables(handleWithPaths, closeableRegistry)
+ .map(runnable -> CompletableFuture.runAsync(runnable, executorService));
Review Comment:
Should we now increase `state.backend.rocksdb.checkpoint.transfer.thread.num` ?
Or maybe use `ThreadPoolExecutor` with a bigger maximum size in `RocksDBStateDataTransfer`?
The default is `4`, which limits the benefits that this PR brings.
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateDownloader.java:
##########
@@ -90,27 +80,62 @@ private void downloadDataForAllStateHandles(
} else {
throw new FlinkRuntimeException("Failed to download data for state handles.", e);
}
+ } finally {
+ // Unregister and close the internal closer. In a failure case, this should interrupt
+ // ongoing downloads.
+ closeableRegistry.unregisterCloseable(internalCloser);
+ IOUtils.closeQuietly(internalCloser);
+ if (failureCleanupRequired) {
+ // Cleanup on exception: cancel all tasks and delete the created directories
+ futures.forEach(future -> future.cancel(true));
+ downloadRequests.stream()
+ .map(StateHandleDownloadSpec::getDownloadDestination)
+ .map(Path::toFile)
+ .forEach(FileUtils::deleteDirectoryQuietly);
+ }
}
}
- private List<Runnable> createDownloadRunnables(
- Map<StateHandleID, StreamStateHandle> stateHandleMap,
- Path restoreInstancePath,
+ /** Asynchronously runs the specified download requests on executorService. */
+ private Stream<CompletableFuture<Void>> transferAllStateDataToDirectoryAsync(
+ Collection<StateHandleDownloadSpec> handleWithPaths,
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());
+ return createDownloadRunnables(handleWithPaths, closeableRegistry)
+ .map(runnable -> CompletableFuture.runAsync(runnable, executorService));
+ }
- runnables.add(
- ThrowingRunnable.unchecked(
- () ->
- downloadDataForStateHandle(
- path, remoteFileHandle, closeableRegistry)));
- }
- return runnables;
+ /** Creates the {@link Runnable} instances for each files in the download requests. */
+ private Stream<Runnable> createDownloadRunnables(
+ Collection<StateHandleDownloadSpec> handleWithPaths,
+ CloseableRegistry closeableRegistry) {
+ return handleWithPaths.stream()
+ .flatMap(
+ downloadRequest ->
+ // Take all files from shared and private state.
+ Streams.concat(
+ downloadRequest.getStateHandle().getSharedState()
+ .entrySet().stream(),
+ downloadRequest.getStateHandle().getPrivateState()
+ .entrySet().stream())
+ .map(
+ // Create one runnable for each StreamStateHandle
+ entry -> {
+ StateHandleID stateHandleID = entry.getKey();
+ StreamStateHandle remoteFileHandle =
+ entry.getValue();
+ Path downloadDest =
+ downloadRequest
+ .getDownloadDestination()
+ .resolve(
+ stateHandleID
+ .toString());
+ return ThrowingRunnable.unchecked(
+ () ->
+ downloadDataForStateHandle(
+ downloadDest,
+ remoteFileHandle,
+ closeableRegistry));
Review Comment:
How about extracting a method `createDownloadRunnables(k, v, dst)`?
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateDownloader.java:
##########
@@ -90,27 +80,62 @@ private void downloadDataForAllStateHandles(
} else {
throw new FlinkRuntimeException("Failed to download data for state handles.", e);
}
+ } finally {
+ // Unregister and close the internal closer. In a failure case, this should interrupt
+ // ongoing downloads.
+ closeableRegistry.unregisterCloseable(internalCloser);
+ IOUtils.closeQuietly(internalCloser);
+ if (failureCleanupRequired) {
+ // Cleanup on exception: cancel all tasks and delete the created directories
+ futures.forEach(future -> future.cancel(true));
+ downloadRequests.stream()
+ .map(StateHandleDownloadSpec::getDownloadDestination)
+ .map(Path::toFile)
+ .forEach(FileUtils::deleteDirectoryQuietly);
+ }
}
}
- private List<Runnable> createDownloadRunnables(
- Map<StateHandleID, StreamStateHandle> stateHandleMap,
- Path restoreInstancePath,
+ /** Asynchronously runs the specified download requests on executorService. */
+ private Stream<CompletableFuture<Void>> transferAllStateDataToDirectoryAsync(
+ Collection<StateHandleDownloadSpec> handleWithPaths,
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());
+ return createDownloadRunnables(handleWithPaths, closeableRegistry)
+ .map(runnable -> CompletableFuture.runAsync(runnable, executorService));
+ }
- runnables.add(
- ThrowingRunnable.unchecked(
- () ->
- downloadDataForStateHandle(
- path, remoteFileHandle, closeableRegistry)));
- }
- return runnables;
+ /** Creates the {@link Runnable} instances for each files in the download requests. */
+ private Stream<Runnable> createDownloadRunnables(
Review Comment:
NIT: I'd inline this method (or the calling one).
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/restore/RocksDBIncrementalRestoreOperation.java:
##########
@@ -284,19 +300,45 @@ private void cleanUpPathQuietly(@Nonnull Path path) {
private void restoreWithRescaling(Collection<KeyedStateHandle> restoreStateHandles)
throws Exception {
- // Prepare for restore with rescaling
- KeyedStateHandle initialHandle =
+ Preconditions.checkArgument(restoreStateHandles != null && !restoreStateHandles.isEmpty());
+
+ // Choose the best state handle for the initial DB
+ final KeyedStateHandle selectedInitialHandle =
RocksDBIncrementalCheckpointUtils.chooseTheBestStateHandleForInitial(
restoreStateHandles, keyGroupRange, overlapFractionThreshold);
- // Init base DB instance
- if (initialHandle != null) {
- restoreStateHandles.remove(initialHandle);
- initDBWithRescaling(initialHandle);
- } else {
- this.rocksHandle.openDB();
+ Preconditions.checkNotNull(selectedInitialHandle);
+
+ final Path absolutInstanceBasePath = instanceBasePath.getAbsoluteFile().toPath();
+
+ final List<StateHandleDownloadSpec> allDownloadSpecs =
+ new ArrayList<>(restoreStateHandles.size());
+
+ // Prepare and collect all the download request to pull remote state to a local directory
+ for (KeyedStateHandle stateHandle : restoreStateHandles) {
+ if (!(stateHandle instanceof IncrementalRemoteKeyedStateHandle)) {
+ throw unexpectedStateHandleException(
+ IncrementalRemoteKeyedStateHandle.class, stateHandle.getClass());
+ }
+ StateHandleDownloadSpec downloadRequest =
+ new StateHandleDownloadSpec(
+ (IncrementalRemoteKeyedStateHandle) stateHandle,
+ absolutInstanceBasePath.resolve(UUID.randomUUID().toString()));
Review Comment:
FYI, @zoltar9264 is changing how the local path is derived in #22669.
##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/RocksDBStateDownloader.java:
##########
@@ -90,27 +80,62 @@ private void downloadDataForAllStateHandles(
} else {
throw new FlinkRuntimeException("Failed to download data for state handles.", e);
}
+ } finally {
+ // Unregister and close the internal closer. In a failure case, this should interrupt
+ // ongoing downloads.
+ closeableRegistry.unregisterCloseable(internalCloser);
+ IOUtils.closeQuietly(internalCloser);
+ if (failureCleanupRequired) {
+ // Cleanup on exception: cancel all tasks and delete the created directories
+ futures.forEach(future -> future.cancel(true));
+ downloadRequests.stream()
+ .map(StateHandleDownloadSpec::getDownloadDestination)
+ .map(Path::toFile)
+ .forEach(FileUtils::deleteDirectoryQuietly);
+ }
Review Comment:
Why can't we just move error handling logic to `catch` and remove `failureCleanupRequired`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org