reuvenlax commented on code in PR #41540:
URL: https://github.com/apache/beam/pull/41540#discussion_r4773412685
##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/state/MultimapSideInputTest.java:
##########
@@ -72,13 +92,478 @@ public void testGetWithBulkRead() throws Exception {
StringUtf8Coder.of(),
true);
assertArrayEquals(
- new String[] {"A1", "A2", "A3"},
Iterables.toArray(multimapSideInput.get(A), String.class));
+ new String[] {"A1", "A2", "A3"},
+ Iterables.toArray(multimapSideInput.get(Arrays.copyOf(A, A.length)),
String.class));
assertArrayEquals(
new String[] {"B1", "B2"}, Iterables.toArray(multimapSideInput.get(B),
String.class));
assertArrayEquals(
new String[] {}, Iterables.toArray(multimapSideInput.get(UNKNOWN),
String.class));
}
+ @Test
+ public void testGetWithBulkReadCached() throws Exception {
+ FakeBeamFnStateClient fakeBeamFnStateClient =
+ new FakeBeamFnStateClient(
+ ImmutableMap.of(
+ keysValuesStateKey(),
+ KV.of(
+ KvCoder.of(ByteArrayCoder.of(),
IterableCoder.of(StringUtf8Coder.of())),
+ asList(KV.of(A, asList("A1", "A2", "A3")), KV.of(B,
asList("B1", "B2"))))));
+
+ Cache<?, ?> cache = Caches.eternal();
+ {
+ // The first side input populates the bulk-read cache.
+ MultimapSideInput<byte[], String> multimapSideInput =
+ new MultimapSideInput<>(
+ cache,
+ fakeBeamFnStateClient,
+ "instructionId",
+ keysStateKey(),
+ ByteArrayCoder.of(),
+ StringUtf8Coder.of(),
+ true);
+ Iterable<String> aValues = multimapSideInput.get(A);
+ assertArrayEquals(new String[] {"A1", "A2", "A3"},
Iterables.toArray(aValues, String.class));
+ // Cached bulk-read iterables must be unmodifiable so callers cannot
corrupt shared entries.
+ if (aValues instanceof List) {
+ assertThrows(UnsupportedOperationException.class, () ->
((List<String>) aValues).clear());
+ }
+ Iterator<String> it = aValues.iterator();
+ it.next();
+ assertThrows(UnsupportedOperationException.class, it::remove);
+ assertArrayEquals(
+ new String[] {"B1", "B2"},
Iterables.toArray(multimapSideInput.get(B), String.class));
+ assertArrayEquals(
+ new String[] {}, Iterables.toArray(multimapSideInput.get(UNKNOWN),
String.class));
+ }
+
+ {
+ // The second side input must serve all bulk-read lookups purely from
the cache.
+ MultimapSideInput<byte[], String> multimapSideInput =
+ new MultimapSideInput<>(
+ cache,
+ requestBuilder -> {
+ throw new IllegalStateException("Unexpected call for test.");
+ },
+ "instructionId",
+ keysStateKey(),
+ ByteArrayCoder.of(),
+ StringUtf8Coder.of(),
+ true);
+ assertArrayEquals(
+ new String[] {"A1", "A2", "A3"},
+ Iterables.toArray(multimapSideInput.get(Arrays.copyOf(A, A.length)),
String.class));
+ assertArrayEquals(
+ new String[] {"B1", "B2"},
Iterables.toArray(multimapSideInput.get(B), String.class));
+ assertArrayEquals(
+ new String[] {}, Iterables.toArray(multimapSideInput.get(UNKNOWN),
String.class));
+ }
+ }
+
+ @Test
+ public void testGetWithBulkReadMoreThan100KeysCached() throws Exception {
Review Comment:
Made MultimapSideInput.BULK_READ_SIZE @VisibleForTesting package-private.
Now reference that constant
##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/MultimapSideInput.java:
##########
@@ -172,6 +166,71 @@ 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));
+ long entriesWeight = 0L;
+ while (bulkRead.size() < BULK_READ_SIZE && entries.hasNext()) {
+ KV<K, Iterable<V>> entry = entries.next();
+ Object structuralKey = keyCoder.structuralValue(entry.getKey());
+ Iterable<V> rawValues = entry.getValue();
+ // Weigh rawValues before wrapping with Iterables.unmodifiableIterable
so Caches.weigh()
+ // checks Weighted and hits its RandomAccess list fast paths rather
than falling back to
+ // jamm. Includes ~32B HashMap.Node + ~16B UnmodifiableIterable
wrapper overhead.
+ entriesWeight = LongMath.saturatedAdd(entriesWeight, 48L);
+ entriesWeight = LongMath.saturatedAdd(entriesWeight,
Caches.weigh(structuralKey));
+ entriesWeight = LongMath.saturatedAdd(entriesWeight,
Caches.weigh(rawValues));
+ bulkRead.put(structuralKey, Iterables.unmodifiableIterable(rawValues));
+ }
+ boolean complete = !entries.hasNext();
+ int size = bulkRead.size();
+ int tableCapacity =
+ size == 0 ? 0 : Integer.highestOneBit(Math.max(16, (size * 4) / 3) *
2 - 1);
+ // CachedBulkRead + HashMap base (~96 B) + Node[] table (next power of 2
>= size / 0.75).
Review Comment:
done or both
--
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]