-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Switch to use ConcurrentMap for StringSetData #33057
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,13 @@ | |
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.beam.sdk.metrics.MetricName; | ||
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet; | ||
import org.junit.Assert; | ||
|
@@ -94,4 +100,42 @@ public void testReset() { | |
assertThat(stringSetCell.getCumulative(), equalTo(StringSetData.empty())); | ||
assertThat(stringSetCell.getDirty(), equalTo(new DirtyState())); | ||
} | ||
|
||
@Test(timeout = 5000) | ||
public void testStringSetCellConcurrentAddRetrieval() throws InterruptedException { | ||
StringSetCell cell = new StringSetCell(MetricName.named("namespace", "name")); | ||
AtomicBoolean finished = new AtomicBoolean(false); | ||
Thread increment = | ||
new Thread( | ||
() -> { | ||
for (long i = 0; !finished.get(); ++i) { | ||
cell.add(String.valueOf(i)); | ||
try { | ||
Thread.sleep(1); | ||
} catch (InterruptedException e) { | ||
break; | ||
} | ||
} | ||
}); | ||
increment.start(); | ||
Instant start = Instant.now(); | ||
try { | ||
while (true) { | ||
Set<String> s = cell.getCumulative().stringSet(); | ||
List<String> snapshot = new ArrayList<>(s); | ||
if (Instant.now().isAfter(start.plusSeconds(3)) && snapshot.size() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should assert that the contents of the snapshot are not corrupt. Maybe we keep track of the highest integer reached by the thread, and assert that at the end, the snapshot contains all of [0, N]. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, added assert. |
||
finished.compareAndSet(false, true); | ||
break; | ||
} | ||
} | ||
} finally { | ||
increment.interrupt(); | ||
increment.join(); | ||
} | ||
|
||
Set<String> s = cell.getCumulative().stringSet(); | ||
for (long i = 0; i < s.size(); ++i) { | ||
assertTrue(s.contains(String.valueOf(i))); | ||
} | ||
} | ||
} |
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.
1s sleep in a 3s test doesn't give much room to find races.
Does this test fail without your changes?
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.
Here it is 1 ms sleep. it does - #33057 (comment).
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.
My mistake. I was thinking Python where it's in seconds and not ms.