Repository navigation
Fixes some bugs in WindmillMapViaMultimap. #40750
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
Open
acrites
wants to merge
4
commits into
apache:master
Choose a base branch
from
acrites:windmill-map-via-multimap-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3abd877
Fix WindmillMapViaMultimap.computeIfAbsent return value and add unit …
acrites 612960c
Clean up tests.
acrites 1669ffd
Fixes extra copy of Iterator
acrites 557716c
Update MapState.computeIfAbsent javadoc and WindmillMapViaMultimap to…
acrites File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
289 changes: 289 additions & 0 deletions
289
...va/org/apache/beam/runners/dataflow/worker/windmill/state/WindmillMapViaMultimapTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.runners.dataflow.worker.windmill.state; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyBoolean; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.AbstractMap; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.apache.beam.runners.core.StateNamespace; | ||
| import org.apache.beam.runners.core.StateNamespaceForTest; | ||
| import org.apache.beam.runners.core.StateTag; | ||
| import org.apache.beam.runners.core.StateTags; | ||
| import org.apache.beam.runners.dataflow.options.DataflowWorkerHarnessOptions; | ||
| import org.apache.beam.runners.dataflow.worker.WindmillComputationKey; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.Coder.Context; | ||
| import org.apache.beam.sdk.coders.NullableCoder; | ||
| import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
| import org.apache.beam.sdk.coders.VarIntCoder; | ||
| import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
| import org.apache.beam.sdk.state.MapState; | ||
| import org.apache.beam.sdk.state.ReadableState; | ||
| import org.apache.beam.sdk.util.ByteStringOutputStream; | ||
| import org.apache.beam.vendor.grpc.v1p69p0.com.google.protobuf.ByteString; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Supplier; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.Futures; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.SettableFuture; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.Timeout; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| /** Unit tests for {@link WindmillMapViaMultimap}. */ | ||
| @RunWith(JUnit4.class) | ||
| public class WindmillMapViaMultimapTest { | ||
| @Rule public transient Timeout globalTimeout = Timeout.seconds(600); | ||
|
|
||
| private static final String STATE_FAMILY = "family"; | ||
| private static final StateNamespace NAMESPACE = new StateNamespaceForTest("ns"); | ||
|
|
||
| @Mock private WindmillStateReader mockReader; | ||
| @Mock private Supplier<Closeable> readStateSupplier; | ||
|
|
||
| private WindmillStateCache cacheViaMultimap; | ||
| private WindmillStateInternals<String> underTestMapViaMultimap; | ||
|
|
||
| private static ByteString key(StateNamespace namespace, String addrId) { | ||
| return ByteString.copyFromUtf8(namespace.stringKey() + "+u" + addrId); | ||
| } | ||
|
|
||
| private static <T> ByteString encodeWithCoder(T value, Coder<T> coder) { | ||
| ByteStringOutputStream out = new ByteStringOutputStream(); | ||
| try { | ||
| coder.encode(value, out, Context.OUTER); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| return out.toByteString(); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| MockitoAnnotations.initMocks(this); | ||
| DataflowWorkerHarnessOptions options = | ||
| PipelineOptionsFactory.as(DataflowWorkerHarnessOptions.class); | ||
| cacheViaMultimap = | ||
| WindmillStateCache.builder() | ||
| .setSizeMb(options.getWorkerCacheMb()) | ||
| .setSupportMapViaMultimap(true) | ||
| .build(); | ||
|
|
||
| WindmillTagEncoding tagEncoding = WindmillTagEncodingV1.instance(); | ||
| underTestMapViaMultimap = | ||
| new WindmillStateInternals<>( | ||
| "key", | ||
| STATE_FAMILY, | ||
| mockReader, | ||
| false, | ||
| cacheViaMultimap | ||
| .forComputation("comp") | ||
| .forKey( | ||
| WindmillComputationKey.create( | ||
| "comp", ByteString.copyFrom("key", StandardCharsets.UTF_8), 123), | ||
| 17L, | ||
| 1L) | ||
| .forFamily(STATE_FAMILY), | ||
| tagEncoding, | ||
| readStateSupplier); | ||
|
|
||
| when(mockReader.multimapFetchAllFuture(anyBoolean(), any(), any(), any())) | ||
| .thenReturn(Futures.immediateFuture(Collections.emptyList())); | ||
| when(mockReader.multimapFetchSingleEntryFuture(any(), any(), any(), any())) | ||
| .thenReturn(Futures.immediateFuture(Collections.emptyList())); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that {@link WindmillMapViaMultimap#computeIfAbsent} returns the computed value on | ||
| * insertion, conforming to the {@link MapState#computeIfAbsent} specification. | ||
| */ | ||
| @Test | ||
| public void testComputeIfAbsentReturnsComputedValueOnInsert() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| final String key = "tag1"; | ||
| SettableFuture<Iterable<Integer>> future = SettableFuture.create(); | ||
| when(mockReader.multimapFetchSingleEntryFuture( | ||
| encodeWithCoder(key, StringUtf8Coder.of()), | ||
| key(NAMESPACE, "map"), | ||
| STATE_FAMILY, | ||
| VarIntCoder.of())) | ||
| .thenReturn(future); | ||
| future.set(Collections.emptyList()); | ||
|
|
||
| ReadableState<Integer> result = mapState.computeIfAbsent(key, k -> 42); | ||
| assertEquals( | ||
| "computeIfAbsent returns computed value when key was absent and computed value is inserted", | ||
| Integer.valueOf(42), | ||
| result.read()); | ||
| assertEquals(Integer.valueOf(42), mapState.get(key).read()); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that {@link WindmillMapViaMultimap} throws {@link IllegalArgumentException} if multiple | ||
| * values exist for a key, enforcing at-most-one-value map invariants. | ||
| */ | ||
| @Test(expected = IllegalArgumentException.class) | ||
| public void testGetThrowsOnMultiValuedKey() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| final String key = "tag1"; | ||
| SettableFuture<Iterable<Integer>> future = SettableFuture.create(); | ||
| when(mockReader.multimapFetchSingleEntryFuture( | ||
| encodeWithCoder(key, StringUtf8Coder.of()), | ||
| key(NAMESPACE, "map"), | ||
| STATE_FAMILY, | ||
| VarIntCoder.of())) | ||
| .thenReturn(future); | ||
| // Storage returns multiple values for key | ||
| future.set(Arrays.asList(10, 20)); | ||
|
|
||
| mapState.get(key).read(); | ||
| } | ||
|
|
||
| /** Tests basic put, get, overwrite, and remove on {@link WindmillMapViaMultimap}. */ | ||
| @Test | ||
| public void testBasicPutGetOverwriteAndRemove() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| mapState.put("a", 1); | ||
| assertEquals(Integer.valueOf(1), mapState.get("a").read()); | ||
|
|
||
| mapState.put("a", 2); | ||
| assertEquals(Integer.valueOf(2), mapState.get("a").read()); | ||
|
|
||
| mapState.remove("a"); | ||
| assertNull(mapState.get("a").read()); | ||
| } | ||
|
|
||
| /** Tests keys, values, and entries on {@link WindmillMapViaMultimap}. */ | ||
| @Test | ||
| public void testKeysValuesAndEntries() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| mapState.put("k1", 10); | ||
| mapState.put("k2", 20); | ||
|
|
||
| assertThat(mapState.keys().read(), Matchers.containsInAnyOrder("k1", "k2")); | ||
| assertThat(mapState.values().read(), Matchers.containsInAnyOrder(10, 20)); | ||
| assertThat( | ||
| mapState.entries().read(), | ||
| Matchers.containsInAnyOrder( | ||
| new AbstractMap.SimpleEntry<>("k1", 10), new AbstractMap.SimpleEntry<>("k2", 20))); | ||
| } | ||
|
|
||
| /** Tests clear() and isEmpty() on {@link WindmillMapViaMultimap}. */ | ||
| @Test | ||
| public void testClearAndIsEmpty() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| assertTrue(mapState.isEmpty().read()); | ||
|
|
||
| mapState.put("key1", 10); | ||
| assertFalse(mapState.isEmpty().read()); | ||
|
|
||
| mapState.clear(); | ||
| assertTrue(mapState.isEmpty().read()); | ||
| } | ||
|
|
||
| /** Tests that computeIfAbsent on an existing key returns the existing value. */ | ||
| @Test | ||
| public void testComputeIfAbsentExistingKeyReturnsExistingValue() { | ||
| StateTag<MapState<String, Integer>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), VarIntCoder.of()); | ||
| MapState<String, Integer> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| mapState.put("key1", 10); | ||
| AtomicBoolean called = new AtomicBoolean(false); | ||
| ReadableState<Integer> result = | ||
| mapState.computeIfAbsent( | ||
| "key1", | ||
| k -> { | ||
| called.set(true); | ||
| return 99; | ||
| }); | ||
|
|
||
| assertEquals(Integer.valueOf(10), result.read()); | ||
| assertFalse("Mapping function should not be called for existing key", called.get()); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that computeIfAbsent computes and inserts a new value when the key is currently mapped to | ||
| * null, returning the computed value. | ||
| */ | ||
| @Test | ||
| public void testComputeIfAbsentWhenMappedToNull() { | ||
| StateTag<MapState<String, String>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), NullableCoder.of(StringUtf8Coder.of())); | ||
| MapState<String, String> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| mapState.put("key1", null); | ||
| AtomicBoolean called = new AtomicBoolean(false); | ||
| ReadableState<String> result = | ||
| mapState.computeIfAbsent( | ||
| "key1", | ||
| k -> { | ||
| called.set(true); | ||
| return "computed"; | ||
| }); | ||
|
|
||
| assertEquals("computed", result.read()); | ||
| assertTrue("Mapping function should be called when key is mapped to null", called.get()); | ||
| assertEquals("computed", mapState.get("key1").read()); | ||
| } | ||
|
|
||
| /** Tests that computeIfAbsent returns null if the computed value is null. */ | ||
| @Test | ||
| public void testComputeIfAbsentReturnsNullWhenComputedValueIsNull() { | ||
| StateTag<MapState<String, String>> addr = | ||
| StateTags.map("map", StringUtf8Coder.of(), NullableCoder.of(StringUtf8Coder.of())); | ||
| MapState<String, String> mapState = underTestMapViaMultimap.state(NAMESPACE, addr); | ||
|
|
||
| ReadableState<String> result = mapState.computeIfAbsent("key1", k -> null); | ||
|
|
||
| assertNull(result.read()); | ||
| assertNull(mapState.get("key1").read()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, there is some difficulty here because null is a valid value for some coders within Beam (https://beam.apache.org/releases/javadoc/2.0.0/org/apache/beam/sdk/coders/NullableCoder.html for example)
So if the iterable exists it seems we should just return the value as it was.
However when evaluating mappingFunction above we need to decide how we interpret null return values:
I think either can be valid interpretation, maybe we should see what the existing implementations do and update the comment to match that behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some other implementations I found were
InMemoryMap:Here is the Spark runner's implementation (note it's return value matches the original version of
MapState.computeIfAbsent()):It looks like the Flink runner has the same implementation as the Spark runner:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In all of these cases the behavior seems to be "if the value is currently mapped to null, apply the mapping function and overwrite it with whatever the mapping function returns". I think they all return the previous value that was stored in the map. So it seems like
WindmillMapViaMultimapis the only class not implementingcomputeIfAbsentbased on the original comments.