You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "scwhittle (via GitHub)" <gi...@apache.org> on 2023/04/20 12:53:34 UTC

[GitHub] [beam] scwhittle opened a new pull request, #26370: [WIP] Flush bag writes if buffer is over 10MB

scwhittle opened a new pull request, #26370:
URL: https://github.com/apache/beam/pull/26370

   fixes #26369
   
   This still needs tests, and should be changed to strictly respect a buffer limit by flushing before appending a new element if it would go over the limit. To do that efficiently we will probably want a special stream like a variant of BufferedElementCountingOutputStream.java that doesn't prepend counts
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] scwhittle commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "scwhittle (via GitHub)" <gi...@apache.org>.
scwhittle commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1182329325


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   The 10MB is a soft-limit as that seems large enough to get efficiency wins of batching over rpcs while keeping things more granular for paging in later. But for the dataflow streaming backend there is a hard-limit of 100MB.  Incoming elements can be up to that limit, so if those elements are directly written to a bag I want to ensure we don't batch beyond that.
   
   I will add some comments



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] Abacn commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "Abacn (via GitHub)" <gi...@apache.org>.
Abacn commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1536367579

   Already approved by @robertwb 


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1536368407

   Stopping reviewer notifications for this pull request: requested by reviewer


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] scwhittle commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "scwhittle (via GitHub)" <gi...@apache.org>.
scwhittle commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1182792600


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   Can you clarify? Here we don't have the encoded sizes until we actually encode, and for efficiency it is nice to encode with ByteStringOutputStream instead of creating a separate output stream for each value and concatenating to respect the limit.  Pulling off the prefix seemed like an ok solution to keep the performance of ByteStringOutputStream.  Were you thinking the performance hit of separate buffers was worth the simpler code?
   



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1517627667

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @robertwb for label java.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1536170054

   Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @Abacn for label java.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] robertwb commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1181805751


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/ByteStringOutputStream.java:
##########
@@ -153,6 +157,47 @@ public ByteString toByteStringAndReset() {
     return rval;
   }
 
+  /**
+   * Creates a byte string with the given size containing the prefix of the contents of this output
+   * stream.
+   */
+  public ByteString consumePrefixToByteString(int prefixSize) {
+    ByteString rval;
+    Preconditions.checkArgument(prefixSize <= size());
+    if (prefixSize == size()) {
+      return toByteStringAndReset();
+    }
+    int bytesFromBuffer = prefixSize - result.size();
+    if (bytesFromBuffer == 0) {
+      rval = result;
+      result = ByteString.EMPTY;
+      return rval;
+    }
+    if (bytesFromBuffer < 0) {
+      rval = result.substring(0, prefixSize);
+      result = result.substring(prefixSize);
+      return rval;
+    }
+    // Copy or reference the kept bytes for the rval.
+    if (shouldCopy(buffer.length, bytesFromBuffer)) {
+      byte[] bufferCopy = new byte[bytesFromBuffer];
+      System.arraycopy(buffer, 0, bufferCopy, 0, bytesFromBuffer);
+      rval = result.concat(UnsafeByteOperations.unsafeWrap(bufferCopy));

Review Comment:
   Any reason not to us ByteString.copyFrom here? 



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] robertwb commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1182890732


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   I was thinking of just having the loop
   
   ```
   for (T newValue : newValues) {
     valueCoder.encode(newValue, out);
     if (out.size() > BAG_APPEND_BATCHING_LIMIT) {
       [send out.toByteStringAndReset()]
     }
   }
   ...
   ```
   
   rather than introducing the (I think correct, but complex to reason about) `consumePrefixToByteString`. True, we will fail on a element of size 100-x MB after writing x MB, for x < 10, but the question is whether that corner case is worth the additional complexity. 



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] scwhittle commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "scwhittle (via GitHub)" <gi...@apache.org>.
scwhittle commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1538113497

   PTAL, I simplied by removing ByteStringOutputStream changes and instead just concatting if beneath the limit.


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] scwhittle commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "scwhittle (via GitHub)" <gi...@apache.org>.
scwhittle commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1189681296


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   Per offline discussion, keeping the existing behavior which ensures we don't increase the size of large elements through batching. This is important to respect runner limits such as in Dataflow. Input elements may be up to the limit and thus greedy batching could exceed the limit.



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] robertwb commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1182691120


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   OK, that's the same soft limit we use on the (push) data channel. Let's avoid introducing the complexity of consumePrefixToByteString and just send + create a new object when we hit this threshold. 



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1529645370

   Reminder, please take a look at this pr: @robertwb 


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] robertwb commented on a diff in pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "robertwb (via GitHub)" <gi...@apache.org>.
robertwb commented on code in PR #26370:
URL: https://github.com/apache/beam/pull/26370#discussion_r1181808904


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/BagUserState.java:
##########
@@ -130,13 +132,31 @@ public void asyncClose() throws Exception {
     if (!newValues.isEmpty()) {
       ByteStringOutputStream out = new ByteStringOutputStream();
       for (T newValue : newValues) {
-        // TODO: Replace with chunking output stream
+        int previousSize = out.size();
         valueCoder.encode(newValue, out);
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT && previousSize > 0) {
+          // Respect the limit by outputting the previous batch of elements.
+          beamFnStateClient.handle(
+              request
+                  .toBuilder()
+                  .setAppend(
+                      StateAppendRequest.newBuilder()
+                          .setData(out.consumePrefixToByteString(previousSize))));
+        }
+        if (out.size() > BAG_APPEND_BATCHING_LIMIT) {

Review Comment:
   This seems to imply 10MB is a soft limit. 
   
   If so, I think it'd be easier to simply emit once the limit is exceeded, rather than introduce the (more complicated to reason about, though I think it's correct) consumePrefixToByteString just to avoid the one extra element. 



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] Abacn commented on pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

Posted by "Abacn (via GitHub)" <gi...@apache.org>.
Abacn commented on PR #26370:
URL: https://github.com/apache/beam/pull/26370#issuecomment-1536366912

   stop reviewer notifications


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] robertwb merged pull request #26370: Ensure FnApi bag appends are not batched if it would exceed 10MB limit

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


-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org