You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "fredia (via GitHub)" <gi...@apache.org> on 2024/09/20 03:57:30 UTC

[PR] [FLINK-34975][state/forst] Support multiget when db all in local [flink]

fredia opened a new pull request, #25363:
URL: https://github.com/apache/flink/pull/25363

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   Support multiget when db all in local.
   
   
   ## Brief change log
   
   - Add `isRemoteDb` check
   - Modify `ForStGeneralMultiGetOperation`
   
   ## Verifying this change
   
   Please make sure both new and modified tests in this PR follow [the conventions for tests defined in our code quality guide](https://flink.apache.org/how-to-contribute/code-style-and-quality-common/#7-testing).
   
   
   This change is already covered by existing tests, such as *(please describe tests)*.
   - `ForStGeneralMultiGetOperationTest`
   - `ForStStateExecutorTest`
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget when db all in local [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #25363:
URL: https://github.com/apache/flink/pull/25363#issuecomment-2362714740

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "e003cb105f6d493f5bca7505b2828e043df28766",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "e003cb105f6d493f5bca7505b2828e043df28766",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * e003cb105f6d493f5bca7505b2828e043df28766 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget when db all in local [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r1772769493


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -40,32 +44,163 @@ public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final Runnable subProcessFinished;
 
+    private final Boolean isRemoteDb;
+
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, null, false, 1);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
-            Runnable subProcessFinished) {
+            Runnable subProcessFinished,
+            boolean isRemoteDb,
+            int readIoParallelism) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.isRemoteDb = isRemoteDb;
+        this.readIoParallelism = readIoParallelism;
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        if (isRemoteDb) {
+            processOneByOne(batchRequest, error, counter, future);

Review Comment:
   This branch has been removed now.



-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget for forstdb [flink]

Posted by "rkhachatryan (via GitHub)" <gi...@apache.org>.
rkhachatryan commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r3287549573


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -33,39 +37,167 @@
 public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final RocksDB db;
-
     private final List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest;
 
+    List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests;
+    List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests;
+
     private final Executor executor;
 
     private final Runnable subProcessFinished;
 
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, 1, null);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
+            int readIoParallelism,
             Runnable subProcessFinished) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.readIoParallelism = readIoParallelism;
+        this.splitRequests = new ArrayList<>();
+        this.mapCheckRequests = new ArrayList<>();
+        classifyAndSplitRequests(splitRequests, mapCheckRequests);
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        processOneByOne(mapCheckRequests, error, counter, future);
+        for (List<ForStDBGetRequest<?, ?, ?, ?>> getRequests : splitRequests) {
+            executor.execute(
+                    () -> {
+                        try {
+                            ReadOptions readOptions = new ReadOptions();

Review Comment:
   Should this object be closed?



-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget for forstdb [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia merged PR #25363:
URL: https://github.com/apache/flink/pull/25363


-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget when db all in local [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r1774522528


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -33,39 +37,169 @@
 public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final RocksDB db;
-
     private final List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest;
 
+    List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests;
+    List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests;
+
     private final Executor executor;
 
     private final Runnable subProcessFinished;
 
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, 1, null);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
+            int readIoParallelism,
             Runnable subProcessFinished) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.readIoParallelism = readIoParallelism;
+        this.splitRequests = new ArrayList<>();
+        this.mapCheckRequests = new ArrayList<>();
+        classifyAndSplitRequests(splitRequests, mapCheckRequests);
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        processOneByOne(mapCheckRequests, error, counter, future);
+        for (List<ForStDBGetRequest<?, ?, ?, ?>> getRequests : splitRequests) {
+            executor.execute(
+                    () -> {
+                        ReadOptions readOptions = new ReadOptions();
+                        readOptions.setReadaheadSize(0);
+                        List<byte[]> keys = new ArrayList<>(getRequests.size());
+                        List<ColumnFamilyHandle> columnFamilyHandles =
+                                new ArrayList<>(getRequests.size());
+
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    byte[] key = request.buildSerializedKey();
+                                    keys.add(key);
+                                    columnFamilyHandles.add(request.getColumnFamilyHandle());
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (IOException e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request,
+                                        "Error when execute ForStDb serialized get key",
+                                        e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }

Review Comment:
   Not well readable. How about use `try...finally...` and put `subProcessFinished.run();` within the `finally` clause?



##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -33,39 +37,169 @@
 public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final RocksDB db;
-
     private final List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest;
 
+    List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests;
+    List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests;
+
     private final Executor executor;
 
     private final Runnable subProcessFinished;
 
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, 1, null);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
+            int readIoParallelism,
             Runnable subProcessFinished) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.readIoParallelism = readIoParallelism;
+        this.splitRequests = new ArrayList<>();
+        this.mapCheckRequests = new ArrayList<>();
+        classifyAndSplitRequests(splitRequests, mapCheckRequests);
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        processOneByOne(mapCheckRequests, error, counter, future);
+        for (List<ForStDBGetRequest<?, ?, ?, ?>> getRequests : splitRequests) {
+            executor.execute(
+                    () -> {
+                        ReadOptions readOptions = new ReadOptions();
+                        readOptions.setReadaheadSize(0);
+                        List<byte[]> keys = new ArrayList<>(getRequests.size());
+                        List<ColumnFamilyHandle> columnFamilyHandles =
+                                new ArrayList<>(getRequests.size());
+
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    byte[] key = request.buildSerializedKey();
+                                    keys.add(key);
+                                    columnFamilyHandles.add(request.getColumnFamilyHandle());
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (IOException e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request,
+                                        "Error when execute ForStDb serialized get key",
+                                        e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        List<byte[]> values = null;
+                        try {
+                            values = db.multiGetAsList(readOptions, columnFamilyHandles, keys);
+                        } catch (Exception e) {
+                            error.set(e);
+                            future.completeExceptionally(e);
+                            for (int i = 0; i < getRequests.size(); i++) {
+                                completeExceptionallyRequest(
+                                        getRequests.get(i), "Error occurred when multiGet", e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    request.completeStateFuture(values.get(i));
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same "
+                                                    + "group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (Exception e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request, "Error when complete get future.", e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+
+                        if (counter.addAndGet(-getRequests.size()) == 0
+                                && !future.isCompletedExceptionally()) {
+                            future.complete(null);
+                        }
+                        if (subProcessFinished != null) {
+                            subProcessFinished.run();
+                        }
+                    });
+        }
+        return future;
+    }
+
+    private void completeExceptionallyRequest(
+            ForStDBGetRequest<?, ?, ?, ?> request, String message, Exception e) {
+        request.completeStateFutureExceptionally(message, e);
+    }
+
+    private void classifyAndSplitRequests(
+            List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests,
+            List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests) {
+        List<ForStDBGetRequest<?, ?, ?, ?>> getRequests = new ArrayList<>();
         for (int i = 0; i < batchRequest.size(); i++) {
             ForStDBGetRequest<?, ?, ?, ?> request = batchRequest.get(i);
+            if (request instanceof ForStDBMapCheckRequest) {
+                mapCheckRequests.add(request);
+            } else {
+                getRequests.add(request);

Review Comment:
   How about using round-robin to add request into `splitRequests`?



-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget for forstdb [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r1774571210


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -33,39 +37,169 @@
 public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final RocksDB db;
-
     private final List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest;
 
+    List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests;
+    List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests;
+
     private final Executor executor;
 
     private final Runnable subProcessFinished;
 
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, 1, null);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
+            int readIoParallelism,
             Runnable subProcessFinished) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.readIoParallelism = readIoParallelism;
+        this.splitRequests = new ArrayList<>();
+        this.mapCheckRequests = new ArrayList<>();
+        classifyAndSplitRequests(splitRequests, mapCheckRequests);
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        processOneByOne(mapCheckRequests, error, counter, future);
+        for (List<ForStDBGetRequest<?, ?, ?, ?>> getRequests : splitRequests) {
+            executor.execute(
+                    () -> {
+                        ReadOptions readOptions = new ReadOptions();
+                        readOptions.setReadaheadSize(0);
+                        List<byte[]> keys = new ArrayList<>(getRequests.size());
+                        List<ColumnFamilyHandle> columnFamilyHandles =
+                                new ArrayList<>(getRequests.size());
+
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    byte[] key = request.buildSerializedKey();
+                                    keys.add(key);
+                                    columnFamilyHandles.add(request.getColumnFamilyHandle());
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (IOException e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request,
+                                        "Error when execute ForStDb serialized get key",
+                                        e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        List<byte[]> values = null;
+                        try {
+                            values = db.multiGetAsList(readOptions, columnFamilyHandles, keys);
+                        } catch (Exception e) {
+                            error.set(e);
+                            future.completeExceptionally(e);
+                            for (int i = 0; i < getRequests.size(); i++) {
+                                completeExceptionallyRequest(
+                                        getRequests.get(i), "Error occurred when multiGet", e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    request.completeStateFuture(values.get(i));
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same "
+                                                    + "group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (Exception e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request, "Error when complete get future.", e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+
+                        if (counter.addAndGet(-getRequests.size()) == 0
+                                && !future.isCompletedExceptionally()) {
+                            future.complete(null);
+                        }
+                        if (subProcessFinished != null) {
+                            subProcessFinished.run();
+                        }
+                    });
+        }
+        return future;
+    }
+
+    private void completeExceptionallyRequest(
+            ForStDBGetRequest<?, ?, ?, ?> request, String message, Exception e) {
+        request.completeStateFutureExceptionally(message, e);
+    }
+
+    private void classifyAndSplitRequests(
+            List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests,
+            List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests) {
+        List<ForStDBGetRequest<?, ?, ?, ?>> getRequests = new ArrayList<>();
         for (int i = 0; i < batchRequest.size(); i++) {
             ForStDBGetRequest<?, ?, ?, ?> request = batchRequest.get(i);
+            if (request instanceof ForStDBMapCheckRequest) {
+                mapCheckRequests.add(request);
+            } else {
+                getRequests.add(request);

Review Comment:
   OK. Let's keep this



-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget when db all in local [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r1772552389


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -40,32 +44,163 @@ public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final Runnable subProcessFinished;
 
+    private final Boolean isRemoteDb;
+
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, null, false, 1);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
-            Runnable subProcessFinished) {
+            Runnable subProcessFinished,

Review Comment:
   How about making `subProcessFinished` as the last parameter? More align with other requests' constructor style.



##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -40,32 +44,163 @@ public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final Runnable subProcessFinished;
 
+    private final Boolean isRemoteDb;
+
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, null, false, 1);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
-            Runnable subProcessFinished) {
+            Runnable subProcessFinished,
+            boolean isRemoteDb,
+            int readIoParallelism) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.isRemoteDb = isRemoteDb;
+        this.readIoParallelism = readIoParallelism;
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        if (isRemoteDb) {
+            processOneByOne(batchRequest, error, counter, future);

Review Comment:
   ```suggestion
               return processOneByOne(batchRequest, error, counter);
   ```
   More readable?



-- 
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


Re: [PR] [FLINK-34975][state/forst] Support multiget for forstdb [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25363:
URL: https://github.com/apache/flink/pull/25363#discussion_r1774562437


##########
flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStGeneralMultiGetOperation.java:
##########
@@ -33,39 +37,169 @@
 public class ForStGeneralMultiGetOperation implements ForStDBOperation {
 
     private final RocksDB db;
-
     private final List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest;
 
+    List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests;
+    List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests;
+
     private final Executor executor;
 
     private final Runnable subProcessFinished;
 
+    private final int readIoParallelism;
+
     ForStGeneralMultiGetOperation(
             RocksDB db, List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest, Executor executor) {
-        this(db, batchRequest, executor, null);
+        this(db, batchRequest, executor, 1, null);
     }
 
     ForStGeneralMultiGetOperation(
             RocksDB db,
             List<ForStDBGetRequest<?, ?, ?, ?>> batchRequest,
             Executor executor,
+            int readIoParallelism,
             Runnable subProcessFinished) {
         this.db = db;
         this.batchRequest = batchRequest;
         this.executor = executor;
         this.subProcessFinished = subProcessFinished;
+        this.readIoParallelism = readIoParallelism;
+        this.splitRequests = new ArrayList<>();
+        this.mapCheckRequests = new ArrayList<>();
+        classifyAndSplitRequests(splitRequests, mapCheckRequests);
     }
 
     @Override
     public CompletableFuture<Void> process() {
-        // TODO: Use MultiGet to optimize this implement
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-
         AtomicReference<Exception> error = new AtomicReference<>();
         AtomicInteger counter = new AtomicInteger(batchRequest.size());
+
+        processOneByOne(mapCheckRequests, error, counter, future);
+        for (List<ForStDBGetRequest<?, ?, ?, ?>> getRequests : splitRequests) {
+            executor.execute(
+                    () -> {
+                        ReadOptions readOptions = new ReadOptions();
+                        readOptions.setReadaheadSize(0);
+                        List<byte[]> keys = new ArrayList<>(getRequests.size());
+                        List<ColumnFamilyHandle> columnFamilyHandles =
+                                new ArrayList<>(getRequests.size());
+
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    byte[] key = request.buildSerializedKey();
+                                    keys.add(key);
+                                    columnFamilyHandles.add(request.getColumnFamilyHandle());
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (IOException e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request,
+                                        "Error when execute ForStDb serialized get key",
+                                        e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        List<byte[]> values = null;
+                        try {
+                            values = db.multiGetAsList(readOptions, columnFamilyHandles, keys);
+                        } catch (Exception e) {
+                            error.set(e);
+                            future.completeExceptionally(e);
+                            for (int i = 0; i < getRequests.size(); i++) {
+                                completeExceptionallyRequest(
+                                        getRequests.get(i), "Error occurred when multiGet", e);
+                            }
+                        }
+                        if (error.get() != null) {
+                            if (subProcessFinished != null) {
+                                subProcessFinished.run();
+                            }
+                            return;
+                        }
+                        for (int i = 0; i < getRequests.size(); i++) {
+                            ForStDBGetRequest<?, ?, ?, ?> request = getRequests.get(i);
+                            try {
+                                if (error.get() == null) {
+                                    request.completeStateFuture(values.get(i));
+                                } else {
+                                    completeExceptionallyRequest(
+                                            request,
+                                            "Error already occurred in other state request of the same "
+                                                    + "group, failed the state request directly",
+                                            error.get());
+                                }
+                            } catch (Exception e) {
+                                error.set(e);
+                                completeExceptionallyRequest(
+                                        request, "Error when complete get future.", e);
+                                future.completeExceptionally(e);
+                            }
+                        }
+
+                        if (counter.addAndGet(-getRequests.size()) == 0
+                                && !future.isCompletedExceptionally()) {
+                            future.complete(null);
+                        }
+                        if (subProcessFinished != null) {
+                            subProcessFinished.run();
+                        }
+                    });
+        }
+        return future;
+    }
+
+    private void completeExceptionallyRequest(
+            ForStDBGetRequest<?, ?, ?, ?> request, String message, Exception e) {
+        request.completeStateFutureExceptionally(message, e);
+    }
+
+    private void classifyAndSplitRequests(
+            List<List<ForStDBGetRequest<?, ?, ?, ?>>> splitRequests,
+            List<ForStDBGetRequest<?, ?, ?, ?>> mapCheckRequests) {
+        List<ForStDBGetRequest<?, ?, ?, ?>> getRequests = new ArrayList<>();
         for (int i = 0; i < batchRequest.size(); i++) {
             ForStDBGetRequest<?, ?, ?, ?> request = batchRequest.get(i);
+            if (request instanceof ForStDBMapCheckRequest) {
+                mapCheckRequests.add(request);
+            } else {
+                getRequests.add(request);

Review Comment:
   I'm worried that this will break the locality of access, now adjacent requests are in a small batch.



-- 
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