github-actions[bot] commented on code in PR #41615:
URL: https://github.com/apache/beam/pull/41615#discussion_r4679990335
##########
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:
**🟡 Medium**: `runningBatchSize` is an `int`, while `wouldExceedSize` uses
`runningBatchSize + (long) elementSize` to guard against `int` overflow.
However, if a batch is not emitted (or if `elementSizeFn` returns a very large
integer or negative value), `runningBatchSize += elementSize` can still
overflow `int` or become negative, which then corrupts
`estimator.recordTime(runningBatchSize)` and future `wouldExceedSize` checks.
Consider validating that `elementSizeFn.apply(element) >= 0` (or
clamping/throwing) and using `Math.addExact` or saturating addition (e.g.,
`Ints.saturatedCast((long) runningBatchSize + elementSize)`). The same applies
to `batch.size += elementSize` in `WindowAwareBatchingDoFn` at line 679.
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/BatchElements.java:
##########
@@ -104,7 +135,46 @@ public static <T> BatchElements<T> withDefaults() {
}
public static <T> BatchElements<T> withConfig(BatchConfig config) {
- return new BatchElements<>(config);
+ return new BatchElements<>(config, null, null);
Review Comment:
**🟢 Low**: Consider validating that `config != null` in `withConfig` (or the
constructor) so null configurations fail fast at construction time rather than
throwing a `NullPointerException` later during pipeline execution.
--
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]