You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "capistrant (via GitHub)" <gi...@apache.org> on 2025/03/27 19:13:04 UTC
[PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
capistrant opened a new pull request, #17841:
URL: https://github.com/apache/druid/pull/17841
<!-- Thanks for trying to help us make Apache Druid be the best it can be! Please fill out as much of the following information as is possible (where relevant, and remove it when irrelevant) to help make the intention and scope of this PR clear in order to ease review. -->
<!-- Please read the doc for contribution (https://github.com/apache/druid/blob/master/CONTRIBUTING.md) before making this PR. Also, once you open a PR, please _avoid using force pushes and rebasing_ since these make it difficult for reviewers to see what you've changed in response to their reviews. See [the 'If your pull request shows conflicts with master' section](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#if-your-pull-request-shows-conflicts-with-master) for more details. -->
<!-- Replace XXXX with the id of the issue fixed in this PR. Remove this section if there is no corresponding issue. Don't reference the issue in the title of this pull-request. -->
<!-- If you are a committer, follow the PR action item checklist for committers:
https://github.com/apache/druid/blob/master/dev/committer-instructions.md#pr-and-issue-action-item-checklist-for-committers. -->
### Description
When getting a new Druid development environment setup, I ran into some stubborn unit tests that kept flaking as I tried to get a clean local build. After some analysis I identified some quick (and perhaps a little bit dirty?) changes to the tests in question to avoid what I perceive to be issues related to concurrency. I am definitely a little bit perplexed on how CI is so much more stable than my local when it come to running these tests. I'm hard pressed to get a single clean run on either of the Test classes until I modified.
<!-- Describe the goal of this PR, what problem are you fixing. If there is a corresponding issue (referenced above), it's not necessary to repeat the description here, however, you may choose to keep one summary sentence. -->
<!-- Describe your patch: what did you change in code? How did you fix the problem? -->
<!-- If there are several relatively logically separate changes in this PR, create a mini-section for each of them. For example: -->
#### Fix test flakes in `SegmentBootstrapperTest`
##### Make TestSegmentCacheManager thread safe
`SegmentCacheManager` interface asserts that implementations must be thread safe. Using `ArrayList` with no further protections to ensure thread safety in the `TestSegmentCacheManager` was violating that assertion and leading to concurrency related problems running unit tests who depend on this implementation.
Using the hammer approach of switching to `CopyOnWriteArrayList` allows us to easily meet thread safety requirements and not have to change any implementation details of `TestSegmentCacheManager`. Since this implementation is strictly for unit testing, I think this tradeoff is ok.
##### Modify assertions in `SegmentBootstrapperTest` to ignore ordering of lists
The order of segments added to these unsorted collections is non-deterministic if there are multiple bootstrap threads working behind the scenes during test execution. Modifying the asserts to confirm collection sizes are equal and checking that all items in each are in the other is one way to confirm that the segment set in the test results matches expectations. There are definitely other ways to go about this assertion if we think mine is too ugly.
#### Fix test flakes in `KinesisIndexTaskTest`
I found that, at least on my local, some test cases constantly run into a situation where the test starts waiting for the task to be in reading state when it is already moved on to publishing by the time the wait begins. The simplest way I could see to fix this was to allow those waits to wait for state to be reading OR publishing. I left the old implementation that only checks for read state because there was an additional use of that method that was not associated with this test startup race condition I was facing.
<!--
In each section, please describe design decisions made, including:
- Choice of algorithms
- Behavioral aspects. What configuration values are acceptable? How are corner cases and error conditions handled, such as when there are insufficient resources?
- Class organization and design (how the logic is split between classes, inheritance, composition, design patterns)
- Method organization and design (how the logic is split between methods, parameters and return types)
- Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
-->
<!-- It's good to describe an alternative design (or mention an alternative name) for every design (or naming) decision point and compare the alternatives with the designs that you've implemented (or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
<!-- If there was a discussion of the design of the feature implemented in this PR elsewhere (e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list), link to that discussion from this PR description and explain what have changed in your final design compared to your original proposal or the consensus version in the end of the discussion. If something hasn't changed since the original discussion, you can omit a detailed discussion of those aspects of the design here, perhaps apart from brief mentioning for the sake of readability of this PR description. -->
<!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
#### Release note
<!-- Give your best effort to summarize your changes in a couple of sentences aimed toward Druid users.
If your change doesn't have end user impact, you can skip this section.
For tips about how to write a good release note, see [Release notes](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#release-notes).
-->
N/A
<hr>
##### Key changed/added classes in this PR
* TestSegmentCacheManager
<hr>
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
This PR has:
- [ ] been self-reviewed.
- [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
- [ ] added documentation for new or modified features or behaviors.
- [ ] a release note entry in the PR description.
- [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
- [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
- [ ] added integration tests.
- [ ] been tested in a test Druid cluster.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017568316
##########
extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisIndexTaskTest.java:
##########
@@ -2461,6 +2461,20 @@ private boolean isTaskReading(KinesisIndexTask task)
return task.getRunner().getStatus() == SeekableStreamIndexTaskRunner.Status.READING;
}
+ /**
+ * Return true if specified task is in READING OR PUBLISHING state
+ * This helper method allows UTs to wait until the task associated with the test has started before continuing on with
+ * the test. Checking for PUBLISHING state in addition to READING prevents a race condition where a task could go
+ * publishing before the wait for READING state had started.
+ * @param task {@link KinesisIndexTask} having its state checked
+ * @return true if task is in READING or PUBLISHING state, otherwise false
+ */
+ private boolean isTaskReadingOrPublishing(KinesisIndexTask task)
Review Comment:
If there are race conditions, do we really need to wait for READING state at all?
I wonder if simply waiting on PUBLISHING state would make the tests easier to understand.
##########
server/src/test/java/org/apache/druid/server/coordination/SegmentBootstrapperTest.java:
##########
@@ -150,11 +150,18 @@ public void testStartStop() throws Exception
Assert.assertEquals(2L, segmentManager.getDataSourceCounts().get("test_two" + i).longValue());
}
- Assert.assertEquals(ImmutableList.copyOf(segments), segmentAnnouncer.getObservedSegments());
-
final ImmutableList<DataSegment> expectedBootstrapSegments = ImmutableList.copyOf(segments);
- Assert.assertEquals(expectedBootstrapSegments, cacheManager.getObservedBootstrapSegments());
- Assert.assertEquals(expectedBootstrapSegments, cacheManager.getObservedBootstrapSegmentsLoadedIntoPageCache());
+
+ // The following verbose assert - seen throughout this test class - confirms list item equality irrespective of item order.
+ Assert.assertTrue(expectedBootstrapSegments.size() == segmentAnnouncer.getObservedSegments().size() &&
+ expectedBootstrapSegments.containsAll(segmentAnnouncer.getObservedSegments()) &&
+ segmentAnnouncer.getObservedSegments().containsAll(expectedBootstrapSegments));
Review Comment:
Some suggestions:
- Maybe add a private method `verifyContentsAreSame()` or something in this class to do this check,
and avoid the verbosity.
- A simple set-based comparison would be neater than a two way contains all check.
- Try to avoid asserting multiple conditions in a single assert statement
```java
private static <T> void verifyContentsAreSame(List<T> listA, List<T> listB) {
Assert.assertEquals(listA.size(), listB.size());
Assert.assertEquals(Set.copyOf(listA), Set.copyOf(listB));
}
```
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "capistrant (via GitHub)" <gi...@apache.org>.
capistrant commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017671407
##########
extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisIndexTaskTest.java:
##########
@@ -2461,6 +2461,20 @@ private boolean isTaskReading(KinesisIndexTask task)
return task.getRunner().getStatus() == SeekableStreamIndexTaskRunner.Status.READING;
}
+ /**
+ * Return true if specified task is in READING OR PUBLISHING state
+ * This helper method allows UTs to wait until the task associated with the test has started before continuing on with
+ * the test. Checking for PUBLISHING state in addition to READING prevents a race condition where a task could go
+ * publishing before the wait for READING state had started.
+ * @param task {@link KinesisIndexTask} having its state checked
+ * @return true if task is in READING or PUBLISHING state, otherwise false
+ */
+ private boolean isTaskReadingOrPublishing(KinesisIndexTask task)
Review Comment:
The three instances where I moved the test to `isTaskReadingOrPublishing` seem to me to not care at all about the details of if it is waiting for reading, publishing, or either. so I believe it is safe to change this new method simply to `isTaskPublishing` and wait on that if it makes things less confusing. Will put it through the paces locally and then push a change if I confirm
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017568316
##########
extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisIndexTaskTest.java:
##########
@@ -2461,6 +2461,20 @@ private boolean isTaskReading(KinesisIndexTask task)
return task.getRunner().getStatus() == SeekableStreamIndexTaskRunner.Status.READING;
}
+ /**
+ * Return true if specified task is in READING OR PUBLISHING state
+ * This helper method allows UTs to wait until the task associated with the test has started before continuing on with
+ * the test. Checking for PUBLISHING state in addition to READING prevents a race condition where a task could go
+ * publishing before the wait for READING state had started.
+ * @param task {@link KinesisIndexTask} having its state checked
+ * @return true if task is in READING or PUBLISHING state, otherwise false
+ */
+ private boolean isTaskReadingOrPublishing(KinesisIndexTask task)
Review Comment:
If there are race conditions, do we really need to wait for READING state at all?
I wonder if simply waiting on PUBLISHING state would make the tests easier to follow.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "capistrant (via GitHub)" <gi...@apache.org>.
capistrant commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017585521
##########
server/src/test/java/org/apache/druid/server/coordination/SegmentBootstrapperTest.java:
##########
@@ -150,11 +150,18 @@ public void testStartStop() throws Exception
Assert.assertEquals(2L, segmentManager.getDataSourceCounts().get("test_two" + i).longValue());
}
- Assert.assertEquals(ImmutableList.copyOf(segments), segmentAnnouncer.getObservedSegments());
-
final ImmutableList<DataSegment> expectedBootstrapSegments = ImmutableList.copyOf(segments);
- Assert.assertEquals(expectedBootstrapSegments, cacheManager.getObservedBootstrapSegments());
- Assert.assertEquals(expectedBootstrapSegments, cacheManager.getObservedBootstrapSegmentsLoadedIntoPageCache());
+
+ // The following verbose assert - seen throughout this test class - confirms list item equality irrespective of item order.
+ Assert.assertTrue(expectedBootstrapSegments.size() == segmentAnnouncer.getObservedSegments().size() &&
+ expectedBootstrapSegments.containsAll(segmentAnnouncer.getObservedSegments()) &&
+ segmentAnnouncer.getObservedSegments().containsAll(expectedBootstrapSegments));
Review Comment:
cool, I appreciate the ideas on cleaning up, I'll do some refactoring. And thanks for the tip on staying away from multi-condition asserts, I'll keep that in mind for future.
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "capistrant (via GitHub)" <gi...@apache.org>.
capistrant commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017586557
##########
extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisIndexTaskTest.java:
##########
@@ -2461,6 +2461,20 @@ private boolean isTaskReading(KinesisIndexTask task)
return task.getRunner().getStatus() == SeekableStreamIndexTaskRunner.Status.READING;
}
+ /**
+ * Return true if specified task is in READING OR PUBLISHING state
+ * This helper method allows UTs to wait until the task associated with the test has started before continuing on with
+ * the test. Checking for PUBLISHING state in addition to READING prevents a race condition where a task could go
+ * publishing before the wait for READING state had started.
+ * @param task {@link KinesisIndexTask} having its state checked
+ * @return true if task is in READING or PUBLISHING state, otherwise false
+ */
+ private boolean isTaskReadingOrPublishing(KinesisIndexTask task)
Review Comment:
Fair point, I'll investigate
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "kfaraz (via GitHub)" <gi...@apache.org>.
kfaraz merged PR #17841:
URL: https://github.com/apache/druid/pull/17841
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Fix flaky unit tests in SegmentBootstrapperTest and KinesisIndexTaskTest (druid)
Posted by "capistrant (via GitHub)" <gi...@apache.org>.
capistrant commented on code in PR #17841:
URL: https://github.com/apache/druid/pull/17841#discussion_r2017671407
##########
extensions-core/kinesis-indexing-service/src/test/java/org/apache/druid/indexing/kinesis/KinesisIndexTaskTest.java:
##########
@@ -2461,6 +2461,20 @@ private boolean isTaskReading(KinesisIndexTask task)
return task.getRunner().getStatus() == SeekableStreamIndexTaskRunner.Status.READING;
}
+ /**
+ * Return true if specified task is in READING OR PUBLISHING state
+ * This helper method allows UTs to wait until the task associated with the test has started before continuing on with
+ * the test. Checking for PUBLISHING state in addition to READING prevents a race condition where a task could go
+ * publishing before the wait for READING state had started.
+ * @param task {@link KinesisIndexTask} having its state checked
+ * @return true if task is in READING or PUBLISHING state, otherwise false
+ */
+ private boolean isTaskReadingOrPublishing(KinesisIndexTask task)
Review Comment:
The three instances where I moved the test to `isTaskReadingOrPublishing` seem to me to not care at all about the details of if it is reading, publishing, or both. so I believe it is safe to change this new method simply to `isTaskPublishing` and wait on that if it makes things less confusing. Will put it through the paces locally and then push a change if I confirm
--
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: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org