LuciferYang commented on code in PR #13400:
URL: https://github.com/apache/gravitino/pull/13400#discussion_r4069472362
##########
core/src/test/java/org/apache/gravitino/cache/TestSegmentedLock.java:
##########
@@ -380,4 +381,58 @@ void testConcurrentGlobalClearing() {
});
});
}
+
+ @Test
+ @Timeout(30)
+ void testGlobalClearingWaitsForInFlightOperations() throws
InterruptedException {
+ SegmentedLock lock = new SegmentedLock(4);
+ CountDownLatch insideSegmentOp = new CountDownLatch(1);
+ CountDownLatch releaseSegmentOp = new CountDownLatch(1);
+ CountDownLatch globalActionDone = new CountDownLatch(1);
+ AtomicBoolean overlapped = new AtomicBoolean(false);
+
+ Thread segmentOpThread =
+ new Thread(
+ () ->
+ lock.withLock(
+ "key1",
+ () -> {
+ insideSegmentOp.countDown();
+ try {
+ releaseSegmentOp.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }));
+ segmentOpThread.start();
+ assertTrue(insideSegmentOp.await(5, TimeUnit.SECONDS), "segment operation
never started");
+
+ Thread globalThread =
+ new Thread(
+ () ->
+ lock.withGlobalLock(
+ () -> {
+ // The global action must never observe the in-flight
segment
+ // operation still holding its critical section.
+ if (releaseSegmentOp.getCount() > 0) {
+ overlapped.set(true);
+ }
Review Comment:
Fixed. The test now tracks a `segmentOpActive` flag set true at the first
statement and false at the last statement of the segment action, and the global
action asserts that flag is false. Because the write lock is granted only after
the read lock is released, a gate that let the global action run before the
segment action exited would observe `segmentOpActive == true` and fail the
test, instead of relying on the release-signal timing.
--
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]