Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate byte size via sampling in StateBackedIterable if size is not cheap to calculate #33780

Merged
merged 9 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
import org.apache.beam.fn.harness.Cache;
import org.apache.beam.fn.harness.Caches;
Expand Down Expand Up @@ -106,6 +108,11 @@ private static class WrappedObservingIterator<T> extends ElementByteSizeObservab
private boolean observerNeedsAdvance = false;
private boolean exceptionLogged = false;

// Lowest sampling probability: 0.001%.
private static final int SAMPLING_TOKEN_UPPER_BOUND = 1000000;
private static final int SAMPLING_CUTOFF = 10;
private int samplingToken = 0;

static <T> WrappedObservingIterator<T> create(
Iterator<T> iterator, org.apache.beam.sdk.coders.Coder<T> elementCoder) {
WrappedObservingIterator<T> result = new WrappedObservingIterator<>(iterator, elementCoder);
Expand All @@ -125,6 +132,18 @@ private WrappedObservingIterator(
this.elementCoder = elementCoder;
}

private boolean sampleElement() {
// Sampling probability decreases as the element count is increasing.
// We unconditionally sample the first samplingCutoff elements. For the
// next samplingCutoff elements, the sampling probability drops from 100%
// to 50%. The probability of sampling the Nth element is:
// min(1, samplingCutoff / N), with an additional lower bound of
// samplingCutoff / samplingTokenUpperBound. This algorithm may be refined
// later.
samplingToken = Math.min(samplingToken + 1, SAMPLING_TOKEN_UPPER_BOUND);
return ThreadLocalRandom.current().nextInt(samplingToken) < SAMPLING_CUTOFF;
}

@Override
public boolean hasNext() {
if (observerNeedsAdvance) {
Expand All @@ -138,15 +157,20 @@ public boolean hasNext() {
public T next() {
T value = wrappedIterator.next();
try {
elementCoder.registerByteSizeObserver(value, observerProxy);
if (observerProxy.getIsLazy()) {
// The observer will only be notified of bytes as the result
// is used. We defer advancing the observer until hasNext in an
// attempt to capture those bytes.
observerNeedsAdvance = true;
} else {
observerNeedsAdvance = false;
observerProxy.advance();
boolean cheap = elementCoder.isRegisterByteSizeObserverCheap(value);
if (cheap || sampleElement()) {
observerProxy.setScalingFactor(
cheap ? 1.0 : Math.max(samplingToken, SAMPLING_CUTOFF) / (double) SAMPLING_CUTOFF);
elementCoder.registerByteSizeObserver(value, observerProxy);
if (observerProxy.getIsLazy()) {
// The observer will only be notified of bytes as the result
// is used. We defer advancing the observer until hasNext in an
// attempt to capture those bytes.
observerNeedsAdvance = true;
} else {
observerNeedsAdvance = false;
observerProxy.advance();
}
}
} catch (Exception e) {
if (!exceptionLogged) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ public void testByteObservingStateBackedIterable() throws Exception {
.sum();
observer.advance();
// 5 comes from size and hasNext (see IterableLikeCoder)
assertEquals(iterateBytes + 5, observer.total);
// observer receives scaled, StringUtf8Coder is not cheap so sampling may produce value that
// is off
assertEquals((float) iterateBytes + 5, (float) observer.total, 3);
}
}

Expand Down
Loading