You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/28 23:53:16 UTC
[GitHub] [beam] nehsyc opened a new pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
nehsyc opened a new pull request #12726:
URL: https://github.com/apache/beam/pull/12726
Add an option in `GroupIntoBatches` interface to allow users to set a timeout for batching. This is useful when there can be incomplete batches, e.g., very few input elements or unbounded input in a global window that might arrive at arbitrary time intervals.
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [x] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
- [x] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
- [ ] 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/#make-reviewers-job-easier).
Post-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
--- | --- | --- | --- | --- | --- | ---
Go | [](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
Java | [](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
Python | [](https://ci-beam.apache.org/job/beam_PostCommit_Python2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python35/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python38/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python2_PVR_Flink_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python35_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
XLang | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Direct/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
Pre-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
--- |Java | Python | Go | Website | Whitespace
--- | --- | --- | --- | --- | ---
Non-portable | [](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocker_Cron/lastCompletedBuild/) <br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocs_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Whitespace_Cron/lastCompletedBuild/)
Portable | --- | [](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | --- | ---
See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------



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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] ajamato commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
ajamato commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-686754624
No need for my review. Thanks for making the change
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487443128
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-691330023
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r482259978
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +184,95 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached, window expiration or buffering time
+ // limit being reached. Reset the buffering timer anyway since the state is empty now. It'll
+ // be reset again if a new element arrives before the expiration time set here.
+ if (maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
Review comment:
I thought we have exposed `clear` API to java timer but it turns out we haven't. Extending timer sounds good to me before we have clear API.
Another problem is when the bufferTimer is fired, we still reset the bufferTimer in the `flushBatch`, which is not necessary. It seems like we can pass `/*bufferingTimer=*/=null` when `onBufferingTimer ` invokes `fluchBatch`,
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-691755419
Please fix spotlessCheck and squash all commits into me. Then I can merge it. Thanks!
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz merged pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz merged pull request #12726:
URL: https://github.com/apache/beam/pull/12726
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-692291612
Run Java PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487443109
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487443109
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
Good catch! Corrected.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
Added a check. But we still need additional checks on null value. See my reply below.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Changed it to null.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-683928743
Run Java PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-683197740
R: @lukecwik @boyuanzz
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-691330023
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-683197987
cc: @ajamato
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r480286341
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +184,95 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached, window expiration or buffering time
+ // limit being reached. Reset the buffering timer anyway since the state is empty now. It'll
+ // be reset again if a new element arrives before the expiration time set here.
+ if (maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
Review comment:
It seems like we want to clear the timer when the flush is triggered by batch size/end of window. I think we can just clear the timer when `flushBatch` is called by window timer expiration and `num >= batchSize`.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-692200394
> Please fix spotlessCheck and squash all commits into me. Then I can merge it. Thanks!
Done. Thanks!
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-692205395
Run Dataflow ValidatesRunner
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-685998086
Run Spotless PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487313819
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487313819
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r481510949
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +184,95 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached, window expiration or buffering time
+ // limit being reached. Reset the buffering timer anyway since the state is empty now. It'll
+ // be reset again if a new element arrives before the expiration time set here.
+ if (maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
Review comment:
By clearing the timer, do you mean to extend the deadline like what I am doing now or set it to something else?
I feel it's a little bit cleaner to put it in FlushBatch since we always want to reset the buffering time when a batch is flushed, whatever the reason is. wdyt?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487443109
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
We still need the check on non-null value here, if not non-zero, since we should not set the timer in the case where the buffering duration is not set (i.e., when only `ofSize()` is called).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-692328746
The test failure is not related.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-691709190
Run Spotless PreCommit
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r482473106
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +184,95 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached, window expiration or buffering time
+ // limit being reached. Reset the buffering timer anyway since the state is empty now. It'll
+ // be reset again if a new element arrives before the expiration time set here.
+ if (maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
Review comment:
Sounds good. Uploaded a commit to address 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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] boyuanzz commented on a change in pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
boyuanzz commented on a change in pull request #12726:
URL: https://github.com/apache/beam/pull/12726#discussion_r487313819
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
Review comment:
Can we use `null` here?
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
Review comment:
We want to pass `bufferingTimer` to null when `flushBatch`is called here since there is no need to clear the bufferTimer when it's fired.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
If we `checkArgument(duration != Duration.ZERO)` when `withMaxBufferingDuration ()`, then we can drop the check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -66,25 +68,49 @@
* }
* }}));
* </pre>
+ *
+ * <p>Example 2 (batch unbounded input in a global window):
+ *
+ * <pre>{@code
+ * PCollection<KV<String, String>> unboundedInput = ...;
+ * long batchSize = 100L;
+ * Duration maxBufferingDuration = Duration.standardSeconds(10);
+ * PCollection<KV<String, Iterable<String>>> batched = unboundedInput
+ * .apply(Window.<KV<String, String>>into(new GlobalWindows())
+ * .triggering(Repeatedly.forever(AfterPane.elementCountAtLeast(1)))
+ * .discardingFiredPanes())
+ * .apply(GroupIntoBatches.<String, String>ofSize(batchSize)
+ * .withMaxBufferingDuration(maxBufferingDuration));
+ * }</pre>
*/
public class GroupIntoBatches<K, InputT>
extends PTransform<PCollection<KV<K, InputT>>, PCollection<KV<K, Iterable<InputT>>>> {
private final long batchSize;
+ private final Duration maxBufferingDuration;
- private GroupIntoBatches(long batchSize) {
+ private GroupIntoBatches(long batchSize, Duration maxBufferingDuration) {
this.batchSize = batchSize;
+ this.maxBufferingDuration = maxBufferingDuration;
}
public static <K, InputT> GroupIntoBatches<K, InputT> ofSize(long batchSize) {
- return new GroupIntoBatches<>(batchSize);
+ return new GroupIntoBatches<>(batchSize, Duration.ZERO);
}
/** Returns the size of the batch. */
public long getBatchSize() {
return batchSize;
}
+ /**
+ * Set a time limit (in processing time) on how long an incomplete batch of elements is allowed to
+ * be buffered. Once a batch is flushed to output, the timer is reset.
+ */
+ public GroupIntoBatches<K, InputT> withMaxBufferingDuration(Duration duration) {
+ return new GroupIntoBatches<>(batchSize, duration);
Review comment:
If `Duration.ZERO` is not preferred, we can have a check here.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
}
private void flushBatch(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
ValueState<K> key,
BagState<InputT> batch,
- CombiningState<Long, long[], Long> numElementsInBatch) {
+ CombiningState<Long, long[], Long> numElementsInBatch,
+ @Nullable Timer bufferingTimer) {
Iterable<InputT> values = batch.read();
- // when the timer fires, batch state might be empty
+ // When the timer fires, batch state might be empty
if (!Iterables.isEmpty(values)) {
receiver.output(KV.of(key.read(), values));
}
batch.clear();
LOG.debug("*** BATCH *** clear");
numElementsInBatch.clear();
+ // We might reach here due to batch size being reached or window expiration. Reset the
+ // buffering timer (if not null) since the state is empty now. It'll be extended again if a
+ // new element arrives prior to the expiration time set here.
+ // TODO(BEAM-10887): Use clear() when it's available.
+ if (bufferingTimer != null && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
Review comment:
Same above.
##########
File path: sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/GroupIntoBatches.java
##########
@@ -150,70 +185,96 @@ public long apply(long left, long right) {
});
this.keySpec = StateSpecs.value(inputKeyCoder);
- // prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
+ // Prefetch every 20% of batchSize elements. Do not prefetch if batchSize is too little
this.prefetchFrequency = ((batchSize / 5) <= 1) ? Long.MAX_VALUE : (batchSize / 5);
}
@ProcessElement
public void processElement(
- @TimerId(END_OF_WINDOW_ID) Timer timer,
+ @TimerId(END_OF_WINDOW_ID) Timer windowTimer,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
@StateId(KEY_ID) ValueState<K> key,
@Element KV<K, InputT> element,
BoundedWindow window,
OutputReceiver<KV<K, Iterable<InputT>>> receiver) {
- Instant windowExpires = window.maxTimestamp().plus(allowedLateness);
-
- LOG.debug(
- "*** SET TIMER *** to point in time {} for window {}",
- windowExpires.toString(),
- window.toString());
- timer.set(windowExpires);
+ Instant windowEnds = window.maxTimestamp().plus(allowedLateness);
+ LOG.debug("*** SET TIMER *** to point in time {} for window {}", windowEnds, window);
+ windowTimer.set(windowEnds);
key.write(element.getKey());
+ LOG.debug("*** BATCH *** Add element for window {} ", window);
batch.add(element.getValue());
- LOG.debug("*** BATCH *** Add element for window {} ", window.toString());
- // blind add is supported with combiningState
+ // Blind add is supported with combiningState
numElementsInBatch.add(1L);
+
Long num = numElementsInBatch.read();
+ if (num == 1 && maxBufferingDuration.isLongerThan(Duration.ZERO)) {
+ // This is the first element in batch. Start counting buffering time if a limit was set.
+ bufferingTimer.offset(maxBufferingDuration).setRelative();
+ }
if (num % prefetchFrequency == 0) {
- // prefetch data and modify batch state (readLater() modifies this)
+ // Prefetch data and modify batch state (readLater() modifies this)
batch.readLater();
}
if (num >= batchSize) {
LOG.debug("*** END OF BATCH *** for window {}", window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
}
}
+ @OnTimer(END_OF_BUFFERING_ID)
+ public void onBufferingTimer(
+ OutputReceiver<KV<K, Iterable<InputT>>> receiver,
+ @Timestamp Instant timestamp,
+ @StateId(KEY_ID) ValueState<K> key,
+ @StateId(BATCH_ID) BagState<InputT> batch,
+ @StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer) {
+ LOG.debug(
+ "*** END OF BUFFERING *** for timer timestamp {} with buffering duration {}",
+ timestamp,
+ maxBufferingDuration);
+ flushBatch(receiver, key, batch, numElementsInBatch, bufferingTimer);
+ }
+
@OnTimer(END_OF_WINDOW_ID)
- public void onTimerCallback(
+ public void onWindowTimer(
OutputReceiver<KV<K, Iterable<InputT>>> receiver,
@Timestamp Instant timestamp,
@StateId(KEY_ID) ValueState<K> key,
@StateId(BATCH_ID) BagState<InputT> batch,
@StateId(NUM_ELEMENTS_IN_BATCH_ID) CombiningState<Long, long[], Long> numElementsInBatch,
+ @TimerId(END_OF_BUFFERING_ID) Timer bufferingTimer,
BoundedWindow window) {
LOG.debug(
"*** END OF WINDOW *** for timer timestamp {} in windows {}",
timestamp,
window.toString());
- flushBatch(receiver, key, batch, numElementsInBatch);
+ flushBatch(receiver, key, batch, numElementsInBatch, null);
Review comment:
Instead, we want to pass actual bufferingTimer here since we want to clear it.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] nehsyc commented on pull request #12726: [BEAM-10475] Add max buffering duration to GroupIntoBatches (Java)
Posted by GitBox <gi...@apache.org>.
nehsyc commented on pull request #12726:
URL: https://github.com/apache/beam/pull/12726#issuecomment-691330023
Added a TODO for clear(). @boyuanzz
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org