mattday9 commented on code in PR #25681:
URL: https://github.com/apache/pulsar/pull/25681#discussion_r3205356384
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java:
##########
@@ -122,21 +122,29 @@ private static long trimLowerBit(long timestamp, int
bits) {
}
@Override
- public boolean addMessage(long ledgerId, long entryId, long deliverAt) {
+ public synchronized boolean addMessage(long ledgerId, long entryId, long
deliverAt) {
if (deliverAt < 0 || deliverAt <= getCutoffTime()) {
messagesHaveFixedDelay = false;
return false;
}
- log.debug()
- .attr("ledgerId", ledgerId)
- .attr("entryId", entryId)
- .attr("deliveryInMs", () -> deliverAt - clock.millis())
- .log("Add message");
- long timestamp = trimLowerBit(deliverAt,
timestampPrecisionBitCnt);
- delayedMessageMap.computeIfAbsent(timestamp, k -> new TreeMap<>())
- .computeIfAbsent(ledgerId, k -> new Roaring64Bitmap())
- .add(entryId);
- delayedMessagesCount.incrementAndGet();
+
+ log.debug()
+ .attr("ledgerId", ledgerId)
+ .attr("entryId", entryId)
+ .attr("deliveryInMs", () -> deliverAt - clock.millis())
+ .log("Add message");
+ long timestamp = trimLowerBit(deliverAt, timestampPrecisionBitCnt);
+
+ Roaring64Bitmap bitmap = delayedMessageMap.computeIfAbsent(timestamp,
k -> new TreeMap<>())
+ .computeIfAbsent(ledgerId, k -> new Roaring64Bitmap());
+ // Roaring64Bitmap does not store duplicates, so track if it a new
element
+ // so we can keep delayedMessagesCount in sync
+ boolean isNew = !bitmap.contains(entryId);
Review Comment:
nit: unnecessary to declare new variable here that's only being used once.
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java:
##########
@@ -274,4 +279,111 @@ public void
testDelaySequence(InMemoryDelayedDeliveryTracker tracker) throws Exc
tracker.close();
}
+ @Test(dataProvider = "delayedTracker")
+ public void
testAddMultipleMessagesSameWindow(InMemoryDelayedDeliveryTracker tracker)
throws Exception {
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+
+ clockTime.set(60);
+
+ tracker.getScheduledMessages(10);
+ }
Review Comment:
I don't see any assertions - pass through execution coverage only.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java:
##########
@@ -122,21 +122,29 @@ private static long trimLowerBit(long timestamp, int
bits) {
}
@Override
- public boolean addMessage(long ledgerId, long entryId, long deliverAt) {
+ public synchronized boolean addMessage(long ledgerId, long entryId, long
deliverAt) {
if (deliverAt < 0 || deliverAt <= getCutoffTime()) {
messagesHaveFixedDelay = false;
return false;
}
- log.debug()
- .attr("ledgerId", ledgerId)
- .attr("entryId", entryId)
- .attr("deliveryInMs", () -> deliverAt - clock.millis())
- .log("Add message");
- long timestamp = trimLowerBit(deliverAt,
timestampPrecisionBitCnt);
- delayedMessageMap.computeIfAbsent(timestamp, k -> new TreeMap<>())
- .computeIfAbsent(ledgerId, k -> new Roaring64Bitmap())
- .add(entryId);
- delayedMessagesCount.incrementAndGet();
+
+ log.debug()
+ .attr("ledgerId", ledgerId)
+ .attr("entryId", entryId)
+ .attr("deliveryInMs", () -> deliverAt - clock.millis())
+ .log("Add message");
+ long timestamp = trimLowerBit(deliverAt, timestampPrecisionBitCnt);
+
+ Roaring64Bitmap bitmap = delayedMessageMap.computeIfAbsent(timestamp,
k -> new TreeMap<>())
+ .computeIfAbsent(ledgerId, k -> new Roaring64Bitmap());
+ // Roaring64Bitmap does not store duplicates, so track if it a new
element
+ // so we can keep delayedMessagesCount in sync
+ boolean isNew = !bitmap.contains(entryId);
+
+ if (isNew) {
+ bitmap.add(entryId);
Review Comment:
Nit: This is ok and its readable but could collapse under the same lambda
expression above for conciseness. Something like:
```
delayedMessageMap.computeIfAbsent(timestamp, k -> new TreeMap<>())
.compute(ledgerId, (k, bitmap) -> {
if (bitmap == null) {
bitmap = new Roaring64Bitmap();
}
if (!bitmap.contains(entryId)) {
bitmap.add(entryId);
delayedMessagesCount.incrementAndGet();
}
return bitmap;
});
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java:
##########
@@ -274,4 +279,111 @@ public void
testDelaySequence(InMemoryDelayedDeliveryTracker tracker) throws Exc
tracker.close();
}
+ @Test(dataProvider = "delayedTracker")
+ public void
testAddMultipleMessagesSameWindow(InMemoryDelayedDeliveryTracker tracker)
throws Exception {
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+
+ clockTime.set(60);
+
+ tracker.getScheduledMessages(10);
+ }
+
+ @Test(dataProvider = "delayedTracker")
+ public void testRaceConditionInUpdateTimer(InMemoryDelayedDeliveryTracker
tracker) throws Exception {
+ final int numThreads = 16;
+ final int operationsPerThread = 1000;
+ final CountDownLatch startLatch = new CountDownLatch(1);
+ final CountDownLatch doneLatch = new CountDownLatch(numThreads);
+ final AtomicInteger errors = new AtomicInteger(0);
+ final AtomicReference<Exception> firstException = new
AtomicReference<>();
+
+ @Cleanup("shutdown")
+ ExecutorService executorService = Executors.newFixedThreadPool(32);
+
+ for (int i = 0; i < 2; i++) {
+ executorService.submit(() -> {
+ try {
+ startLatch.await();
+ for (int j = 0; j < operationsPerThread; j++) {
+ tracker.clear();
+ Thread.sleep(1);
+ }
+ } catch (Exception e) {
+ errors.incrementAndGet();
+ firstException.compareAndSet(null, e);
+ e.printStackTrace();
+ } finally {
+ doneLatch.countDown();
+ }
+ });
+ }
+
+ for (int i = 0; i < 5; i++) {
+ executorService.submit(() -> {
+ try {
+ startLatch.await();
+ for (int j = 0; j < operationsPerThread; j++) {
+ tracker.addMessage(1, 1, 10);
+ Thread.sleep(1);
+ }
+ } catch (Exception e) {
+ errors.incrementAndGet();
+ firstException.compareAndSet(null, e);
+ e.printStackTrace();
+ } finally {
+ doneLatch.countDown();
+ }
+ });
+ }
+
+ for (int i = 0; i < 5; i++) {
Review Comment:
Magic number - use descriptive variables
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java:
##########
@@ -274,4 +279,111 @@ public void
testDelaySequence(InMemoryDelayedDeliveryTracker tracker) throws Exc
tracker.close();
}
+ @Test(dataProvider = "delayedTracker")
+ public void
testAddMultipleMessagesSameWindow(InMemoryDelayedDeliveryTracker tracker)
throws Exception {
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+
+ clockTime.set(60);
+
+ tracker.getScheduledMessages(10);
+ }
+
+ @Test(dataProvider = "delayedTracker")
+ public void testRaceConditionInUpdateTimer(InMemoryDelayedDeliveryTracker
tracker) throws Exception {
+ final int numThreads = 16;
+ final int operationsPerThread = 1000;
+ final CountDownLatch startLatch = new CountDownLatch(1);
+ final CountDownLatch doneLatch = new CountDownLatch(numThreads);
+ final AtomicInteger errors = new AtomicInteger(0);
+ final AtomicReference<Exception> firstException = new
AtomicReference<>();
+
+ @Cleanup("shutdown")
+ ExecutorService executorService = Executors.newFixedThreadPool(32);
+
+ for (int i = 0; i < 2; i++) {
+ executorService.submit(() -> {
+ try {
+ startLatch.await();
+ for (int j = 0; j < operationsPerThread; j++) {
+ tracker.clear();
+ Thread.sleep(1);
+ }
+ } catch (Exception e) {
+ errors.incrementAndGet();
+ firstException.compareAndSet(null, e);
+ e.printStackTrace();
Review Comment:
printStackTrace() is an anti-pattern. Better off asserting and/or logging
the exception.
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java:
##########
@@ -274,4 +279,111 @@ public void
testDelaySequence(InMemoryDelayedDeliveryTracker tracker) throws Exc
tracker.close();
}
+ @Test(dataProvider = "delayedTracker")
+ public void
testAddMultipleMessagesSameWindow(InMemoryDelayedDeliveryTracker tracker)
throws Exception {
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+ tracker.addMessage(1, 1, 50);
+
+ clockTime.set(60);
+
+ tracker.getScheduledMessages(10);
+ }
+
+ @Test(dataProvider = "delayedTracker")
+ public void testRaceConditionInUpdateTimer(InMemoryDelayedDeliveryTracker
tracker) throws Exception {
+ final int numThreads = 16;
Review Comment:
I'm seeing 17 threads so 1 of these may not complete when
[reaching](https://github.com/apache/pulsar/pull/25681/changes#diff-985bb3c41e26dba336cbf9d90c2b5185d3b15c665755b4c0b90fb64dd443e884R378)
line 378 assert
--
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]