reuvenlax commented on code in PR #41540:
URL: https://github.com/apache/beam/pull/41540#discussion_r4762824543


##########
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) {
+    if (values instanceof List) {
+      return Collections.unmodifiableList((List<V>) values);
+    }
+    if (values instanceof Collection) {
+      return Collections.unmodifiableCollection((Collection<V>) values);
+    }
+    return Iterables.unmodifiableIterable(values);
+  }
+
+  private static <V> long weighBulkRead(Map<Object, Iterable<V>> bulkRead) {
+    // CachedBulkRead + HashMap base (~96 B) + Node[] table (next power of 2 
>= size / 0.75).
+    int size = bulkRead.size();
+    int tableCapacity = size == 0 ? 0 : Integer.highestOneBit(Math.max(16, 
(size * 4) / 3) * 2 - 1);
+    long weight = 96L + (long) tableCapacity * Caches.REFERENCE_SIZE;
+    for (Map.Entry<Object, Iterable<V>> entry : bulkRead.entrySet()) {
+      // HashMap.Node overhead (~32 B) + structural key + unmodifiable values 
iterable.
+      weight = LongMath.saturatedAdd(weight, 32L);
+      weight = LongMath.saturatedAdd(weight, Caches.weigh(entry.getKey()));
+      weight = LongMath.saturatedAdd(weight, weighIterable(entry.getValue()));
+    }
+    return weight;
+  }
+
+  private static <V> long weighIterable(Iterable<V> iterable) {
+    if (iterable instanceof Collection) {

Review Comment:
   this should now be done as part of Caches.weigh (there's an o instanceof 
Weighted path in that function).



-- 
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]

Reply via email to