reuvenlax commented on code in PR #41540:
URL: https://github.com/apache/beam/pull/41540#discussion_r4762803582
##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/MultimapSideInput.java:
##########
@@ -47,13 +53,49 @@
public class MultimapSideInput<K, V> implements MultimapView<K, V> {
private static final int BULK_READ_SIZE = 100;
+ private static final int WEIGH_SAMPLE_LIMIT = 64;
+
+ private enum BulkReadCacheKey {
+ INSTANCE
+ }
+
+ /**
+ * Immutable, bounded snapshot of up to {@link #BULK_READ_SIZE} multimap
entries shared across
+ * bundles via the state cache.
+ */
+ private static final class CachedBulkRead<V> implements Weighted {
+ private final Map<Object, Iterable<V>> entries;
+ private final boolean complete;
+ private final long weight;
+
+ CachedBulkRead(Map<Object, Iterable<V>> entries, boolean complete, long
weight) {
+ this.entries = entries;
+ this.complete = complete;
+ this.weight = weight;
+ }
+
+ @Override
+ public long getWeight() {
+ return weight;
+ }
+
+ Iterable<V> get(Object structuralKey) {
+ Iterable<V> result = entries.get(structuralKey);
+ if (result == null && complete) {
+ // As we read the entire set of values, we don't have to do a lookup
to know
+ // this key doesn't exist. Missing keys are treated as empty iterables
in this multimap.
+ return Collections.emptyList();
+ }
+ return result;
+ }
+ }
private final Cache<?, ?> cache;
Review Comment:
final variables shouldn't need annotations.
bulkReadResult is volatile, and there's double-checked locking read. Adding
GuardedBy causes ErrorProne to to complain on the lock-free read, so added
comment instead.
##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/MultimapSideInput.java:
##########
@@ -172,6 +165,117 @@ public Iterable<V> get(K k) {
Caches.subCache(cache, "ValuesForKey", encodedKey), beamFnStateClient,
request, valueCoder);
}
+ private synchronized CachedBulkRead<V> loadOrGetBulkRead() {
+ CachedBulkRead<V> reader = this.bulkReadResult;
+ if (reader != null) {
+ return reader;
+ }
+
+ Cache<BulkReadCacheKey, CachedBulkRead<V>> bulkReadCache =
Caches.subCache(cache, "BulkRead");
+ reader = bulkReadCache.peek(BulkReadCacheKey.INSTANCE);
+ if (reader != null) {
+ this.bulkReadResult = reader;
+ return reader;
+ }
+
+ Map<Object, Iterable<V>> bulkRead = new HashMap<>();
+ StateKey bulkReadStateKey =
+ StateKey.newBuilder()
+ .setMultimapKeysValuesSideInput(
+ StateKey.MultimapKeysValuesSideInput.newBuilder()
+ .setTransformId(
+
keysRequest.getStateKey().getMultimapKeysSideInput().getTransformId())
+ .setSideInputId(
+
keysRequest.getStateKey().getMultimapKeysSideInput().getSideInputId())
+
.setWindow(keysRequest.getStateKey().getMultimapKeysSideInput().getWindow()))
+ .build();
+
+ StateRequest bulkReadRequest =
keysRequest.toBuilder().setStateKey(bulkReadStateKey).build();
+ try {
+ // Stream lazily via DataStreamDecoder over chunked state responses so
pages holding
+ // > BULK_READ_SIZE keys stop decoding after entry BULK_READ_SIZE while
still supporting
+ // individual KV entries that straddle chunk boundaries.
+ DataStreamDecoder<KV<K, Iterable<V>>> entries =
+ new DataStreamDecoder<>(
+ KvCoder.of(keyCoder, IterableCoder.of(valueCoder)),
+ new StateFetchingIterators.LazyBlockingStateFetchingIterator(
+ beamFnStateClient, bulkReadRequest));
+ while (bulkRead.size() < BULK_READ_SIZE && entries.hasNext()) {
+ KV<K, Iterable<V>> entry = entries.next();
+ bulkRead.put(
+ keyCoder.structuralValue(entry.getKey()),
toUnmodifiableIterable(entry.getValue()));
+ }
+ boolean complete = !entries.hasNext();
+ reader = new CachedBulkRead<>(bulkRead, complete,
weighBulkRead(bulkRead));
+ bulkReadCache.put(BulkReadCacheKey.INSTANCE, reader);
+ } catch (Exception exn) {
+ // Do not publish partial/failed bulk reads to the shared cache.
+ reader = new CachedBulkRead<>(bulkRead, false, 0L);
+ }
+ this.bulkReadResult = reader;
+ return reader;
+ }
+
+ private static <V> Iterable<V> toUnmodifiableIterable(Iterable<V> values) {
Review Comment:
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]