ganesh-skumar commented on code in PR #41615:
URL: https://github.com/apache/beam/pull/41615#discussion_r4680982946
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/BatchElements.java:
##########
@@ -469,25 +547,41 @@ public void setup() {
public void startBundle() {
batch = new ArrayList<>();
runningBatchSize = 0;
+ runningBatchWeight = 0L;
targetBatchSize = estimator.nextBatchSize();
}
@ProcessElement
public void processElement(@Element T element, OutputReceiver<List<T>>
receiver) {
- int elementSize = 1;
- if (runningBatchSize + elementSize > targetBatchSize) {
- if (runningBatchSize > 0 && !batch.isEmpty()) {
- try (BatchElements.BatchSizeEstimator.Stopwatch sw =
- estimator.recordTime(runningBatchSize)) {
- receiver.output(batch); // emit full batch downstream
- }
+ int elementSize;
+ long elementWeight;
+ if (maxBatchWeight == null) {
+ elementSize = elementSizeFn != null ? elementSizeFn.apply(element) : 1;
+ elementWeight = 0L;
+ } else {
+ elementSize = 1;
+ elementWeight = elementSizeFn != null ? elementSizeFn.apply(element) :
1L;
+ }
+
+ boolean wouldExceedSize = runningBatchSize + (long) elementSize >
targetBatchSize;
+ boolean wouldExceedWeight =
+ maxBatchWeight != null && runningBatchWeight + elementWeight >
maxBatchWeight;
+
+ // Never emit an empty batch: an element that exceeds the limits on its
own
+ // is emitted in a batch by itself.
+ if (!batch.isEmpty() && (wouldExceedSize || wouldExceedWeight)) {
+ try (BatchElements.BatchSizeEstimator.Stopwatch sw =
+ estimator.recordTime(runningBatchSize)) {
+ receiver.output(batch); // emit full batch downstream
}
batch = new ArrayList<>();
runningBatchSize = 0;
+ runningBatchWeight = 0L;
targetBatchSize = estimator.nextBatchSize();
}
batch.add(element);
runningBatchSize += elementSize;
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]