forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][Java Client] Fix thread safety issue of
LastCumulativeAck
(ap…
…ache#16072) ### Motivation There were several issues caused by the thread safe issue of `LastCumulativeAck`, see: - apache#10586 - apache#12343 The root cause is that `LastCumulativeAck` could be accessed by different threads, especially in `flushAsync` method. But the fields are accessed directly and no thread safety can be guaranteed. In addition, the current `LastCumulativeAck` class was added in apache#8996 to hold two object references, but this modification is wrong. Before apache#8996, there are two CAS operations in `doCumulativeAck` method in case it's called concurretly. Though the composite CAS operation is not atomic. However, after apache#8996, only CAS operation was performed but it's compared with a `LastCumulativeAck` object, not the two fields (`messageId` and `bitSetRecyclable`). There is another issue that it uses a flag `cumulativeAckFlushRequired` to mark if `lastCumulativeAck` should flush. However, if `flushAsync` was called concurrently, both would send ACK commands to broker. ### Modifications To solve the thread safety issue, this PR move the `LastCumulativeAck` out of the `PersistentAcknowledgmentsGroupingTracker` to disable directly access to the internal fields. Then, the following synchronized methods were added to guarantee the thread safety: - `update`: Guarantee the safe write operations. It also recycles the `BitSetRecyclable` object before assigning new values and indicates itself can be flushed. - `flush`: If it can be flushed, return a thread local `LastCumulativeAck` instance that contains the message ID and the bit set. The bit set is deep copied to avoid the original reference being recycled in another `update` call. In addition, since the `messageId` field is volatile, the `getMessageId` method can always retrieve the latest reference. `LastCumulativeAckTest` is added to verify the sematics above. Based on the new design, we can only maintain a `LastCumulativeAck` field in `PersistentAcknowledgmentsGroupingTracker` and call the related methods in `doCumulativeAck` and `flushAsync`. It also fixes the problem that two concurrent `flushAsync` calls might send the same ACK command twice.
- Loading branch information
1 parent
c8f03e8
commit 936d6fd
Showing
2 changed files
with
159 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
86 changes: 86 additions & 0 deletions
86
pulsar-client/src/test/java/org/apache/pulsar/client/impl/LastCumulativeAckTest.java
This file contains 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,86 @@ | ||
/** | ||
* 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.pulsar.client.impl; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertNotSame; | ||
import static org.testng.Assert.assertNull; | ||
import static org.testng.Assert.assertSame; | ||
import static org.testng.Assert.assertTrue; | ||
import org.apache.pulsar.common.util.collections.BitSetRecyclable; | ||
import org.testng.annotations.Test; | ||
|
||
public class LastCumulativeAckTest { | ||
|
||
@Test | ||
public void testUpdate() { | ||
final LastCumulativeAck lastCumulativeAck = new LastCumulativeAck(); | ||
assertFalse(lastCumulativeAck.isFlushRequired()); | ||
assertEquals(lastCumulativeAck.getMessageId(), LastCumulativeAck.DEFAULT_MESSAGE_ID); | ||
assertNull(lastCumulativeAck.getBitSetRecyclable()); | ||
|
||
final MessageIdImpl messageId1 = new MessageIdImpl(0L, 1L, 10); | ||
final BitSetRecyclable bitSetRecyclable1 = BitSetRecyclable.create(); | ||
bitSetRecyclable1.set(0, 3); | ||
lastCumulativeAck.update(messageId1, bitSetRecyclable1); | ||
assertTrue(lastCumulativeAck.isFlushRequired()); | ||
assertSame(lastCumulativeAck.getMessageId(), messageId1); | ||
assertSame(lastCumulativeAck.getBitSetRecyclable(), bitSetRecyclable1); | ||
|
||
final MessageIdImpl messageId2 = new MessageIdImpl(0L, 2L, 8); | ||
lastCumulativeAck.update(messageId2, bitSetRecyclable1); | ||
// bitSetRecyclable1 is not recycled | ||
assertEquals(bitSetRecyclable1.toString(), "{0, 1, 2}"); | ||
|
||
final BitSetRecyclable bitSetRecyclable2 = BitSetRecyclable.create(); | ||
bitSetRecyclable2.set(0, 2); | ||
|
||
// `update()` only accepts a newer message ID, so this call here has no side effect | ||
lastCumulativeAck.update(messageId2, bitSetRecyclable2); | ||
assertSame(lastCumulativeAck.getBitSetRecyclable(), bitSetRecyclable1); | ||
|
||
final MessageIdImpl messageId3 = new MessageIdImpl(0L, 3L, 9); | ||
lastCumulativeAck.update(messageId3, bitSetRecyclable2); | ||
// bitSetRecyclable1 is recycled because it's replaced in `update` | ||
assertEquals(bitSetRecyclable1.toString(), "{}"); | ||
assertSame(lastCumulativeAck.getMessageId(), messageId3); | ||
assertSame(lastCumulativeAck.getBitSetRecyclable(), bitSetRecyclable2); | ||
bitSetRecyclable2.recycle(); | ||
} | ||
|
||
@Test | ||
public void testFlush() { | ||
final LastCumulativeAck lastCumulativeAck = new LastCumulativeAck(); | ||
assertNull(lastCumulativeAck.flush()); | ||
|
||
final MessageIdImpl messageId = new MessageIdImpl(0L, 1L, 3); | ||
final BitSetRecyclable bitSetRecyclable = BitSetRecyclable.create(); | ||
bitSetRecyclable.set(0, 3); | ||
lastCumulativeAck.update(messageId, bitSetRecyclable); | ||
assertTrue(lastCumulativeAck.isFlushRequired()); | ||
|
||
final LastCumulativeAck lastCumulativeAckToFlush = lastCumulativeAck.flush(); | ||
assertFalse(lastCumulativeAck.isFlushRequired()); | ||
assertSame(lastCumulativeAckToFlush.getMessageId(), messageId); | ||
assertNotSame(lastCumulativeAckToFlush.getBitSetRecyclable(), bitSetRecyclable); | ||
assertEquals(lastCumulativeAckToFlush.getBitSetRecyclable(), bitSetRecyclable); | ||
} | ||
|
||
} |