hlteoh37 commented on code in PR #20245:
URL: https://github.com/apache/flink/pull/20245#discussion_r924493155


##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -344,69 +351,69 @@ public void write(InputT element, Context context) throws 
IOException, Interrupt
      * </ul>
      */
     private void nonBlockingFlush() throws InterruptedException {
-        while (!isInFlightRequestOrMessageLimitExceeded()
+        while (!rateLimitingStrategy.shouldBlock(createRequestInfo())
                 && (bufferedRequestEntries.size() >= getNextBatchSizeLimit()
                         || bufferedRequestEntriesTotalSizeInBytes >= 
maxBatchSizeInBytes)) {
             flush();
         }
     }
 
-    /**
-     * Determines if the sink should block and complete existing in flight 
requests before it may
-     * prudently create any new ones. This is exactly determined by if the 
number of requests
-     * currently in flight exceeds the maximum supported by the sink OR if the 
number of in flight
-     * messages exceeds the maximum determined to be appropriate by the rate 
limiting strategy.
-     */
-    private boolean isInFlightRequestOrMessageLimitExceeded() {
-        return inFlightRequestsCount >= maxInFlightRequests
-                || inFlightMessages >= rateLimitingStrategy.getRateLimit();
+    private RequestInfo createRequestInfo() {
+        int batchSize = getNextBatchSize();
+        long requestStartTime = System.currentTimeMillis();
+        return RequestInfo.builder()
+                .setBatchSize(batchSize)
+                .setRequestStartTime(requestStartTime)
+                .build();
     }
 
     /**
      * Persists buffered RequestsEntries into the destination by invoking 
{@code
      * submitRequestEntries} with batches according to the user specified 
buffering hints.
      *
-     * <p>The method blocks if too many async requests are in flight.
+     * <p>The method checks with the {@code rateLimitingStrategy} to see if it 
should block the
+     * request.
      */
     private void flush() throws InterruptedException {
-        while (isInFlightRequestOrMessageLimitExceeded()) {
+        RequestInfo requestInfo = createRequestInfo();
+        while (rateLimitingStrategy.shouldBlock(requestInfo)) {
             mailboxExecutor.yield();
+            requestInfo = createRequestInfo();
         }
 
-        List<RequestEntryT> batch = createNextAvailableBatch();
-        int batchSize = batch.size();
-
+        List<RequestEntryT> batch = createNextAvailableBatch(requestInfo);
         if (batch.size() == 0) {
             return;
         }
 
-        long timestampOfRequest = System.currentTimeMillis();
-        Consumer<List<RequestEntryT>> requestResult =
+        int batchSize = requestInfo.getBatchSize();

Review Comment:
   `batch.size()` is the better one to use, but we need to be consistent across 
calls to `registerInFlightRequest` and `registerCompletedRequest` calls.
   
   Should we create a new instance of `RequestInfo`?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to