Skip to content
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

[fix][broker] Fix the publish latency spike from the contention of MessageDeduplication #20647

Merged
merged 6 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import com.google.common.annotations.VisibleForTesting;
import io.netty.buffer.ByteBuf;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback;
import org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback;
Expand Down Expand Up @@ -126,7 +126,7 @@ public MessageDupUnknownException() {
private final int maxNumberOfProducers;

// Map used to track the inactive producer along with the timestamp of their last activity
private final Map<String, Long> inactiveProducers = new HashMap<>();
private final Map<String, Long> inactiveProducers = new ConcurrentHashMap<>();

private final String replicatorPrefix;

Expand Down Expand Up @@ -436,15 +436,15 @@ private boolean isDeduplicationEnabled() {
/**
* Topic will call this method whenever a producer connects.
*/
public synchronized void producerAdded(String producerName) {
public void producerAdded(String producerName) {
// Producer is no-longer inactive
inactiveProducers.remove(producerName);
}

/**
* Topic will call this method whenever a producer disconnects.
*/
public synchronized void producerRemoved(String producerName) {
public void producerRemoved(String producerName) {
// Producer is no-longer active
inactiveProducers.put(producerName, System.currentTimeMillis());
}
Expand All @@ -456,7 +456,7 @@ public synchronized void purgeInactiveProducers() {
long minimumActiveTimestamp = System.currentTimeMillis() - TimeUnit.MINUTES
.toMillis(pulsar.getConfiguration().getBrokerDeduplicationProducerInactivityTimeoutMinutes());

Iterator<java.util.Map.Entry<String, Long>> mapIterator = inactiveProducers.entrySet().iterator();
Iterator<Map.Entry<String, Long>> mapIterator = inactiveProducers.entrySet().iterator();
boolean hasInactive = false;
while (mapIterator.hasNext()) {
java.util.Map.Entry<String, Long> entry = mapIterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.netty.channel.EventLoopGroup;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.mledger.ManagedCursor;
import org.apache.bookkeeper.mledger.ManagedLedger;
Expand Down Expand Up @@ -174,7 +175,7 @@ public void testInactiveProducerRemove() throws Exception {

Field field = MessageDeduplication.class.getDeclaredField("inactiveProducers");
field.setAccessible(true);
Map<String, Long> inactiveProducers = (Map<String, Long>) field.get(messageDeduplication);
Map<String, Long> inactiveProducers = (ConcurrentHashMap<String, Long>) field.get(messageDeduplication);

String producerName1 = "test1";
when(publishContext.getHighestSequenceId()).thenReturn(2L);
Expand Down