acrites commented on code in PR #41692:
URL: https://github.com/apache/beam/pull/41692#discussion_r4744724563
##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/data/PCollectionConsumerRegistry.java:
##########
@@ -517,80 +523,165 @@ private static class ByteSizeObserver extends
ElementByteSizeObserver {
protected void reportElementSize(long elementSize) {
observedSize += elementSize;
}
+
+ @Override
+ public void reset() {
+ super.reset();
+ observedSize = 0;
+ }
}
- final Distribution distribution;
- ByteSizeObserver byteCountObserver;
+ private final Distribution distribution;
+ private final @Nullable Random randomOverride;
+ private ByteSizeObserver reusableObserver = new ByteSizeObserver();
+ private @Nullable ByteSizeObserver pendingLazyObserver = null;
public SampleByteSizeDistribution(Distribution distribution) {
+ this(distribution, null);
+ }
+
+ @VisibleForTesting
+ SampleByteSizeDistribution(Distribution distribution, @Nullable Random
randomGenerator) {
this.distribution = distribution;
- this.byteCountObserver = null;
+ this.randomOverride = randomGenerator;
+ }
+
+ private Random random() {
+ return randomOverride != null ? randomOverride :
ThreadLocalRandom.current();
}
public void tryUpdate(T value, Coder<T> coder) throws Exception {
+ // Clear any pending lazy observer from a prior element that exited
abnormally.
+ if (pendingLazyObserver != null) {
+ pendingLazyObserver = null;
+ }
if (shouldSampleElement()) {
- // First try using byte size observer
- byteCountObserver = new ByteSizeObserver();
- coder.registerByteSizeObserver(value, byteCountObserver);
+ recordSample(value, coder);
+ }
+ }
- if (!byteCountObserver.getIsLazy()) {
- byteCountObserver.advance();
- this.distribution.update(byteCountObserver.observedSize);
+ private void recordSample(T value, Coder<T> coder) throws Exception {
+ ByteSizeObserver observer = reusableObserver;
+ observer.reset();
+ try {
+ coder.registerByteSizeObserver(value, observer);
+ } catch (Exception | Error e) {
+ if (observer.getIsLazy()) {
Review Comment:
Good call, done!
--
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]