Skip to content
Merged
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 @@ -18,16 +18,19 @@
*/
package org.apache.pulsar.broker.service;

import com.google.common.collect.Lists;
import org.apache.pulsar.broker.service.BrokerServiceException.ConsumerAssignException;
import org.apache.pulsar.common.util.Murmur3_32Hash;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.pulsar.broker.service.BrokerServiceException.ConsumerAssignException;
import org.apache.pulsar.common.util.Murmur3_32Hash;

/**
* This is a consumer selector based fixed hash range.
*
Expand All @@ -39,7 +42,7 @@ public class ConsistentHashingStickyKeyConsumerSelector implements StickyKeyCons
private final ReadWriteLock rwLock = new ReentrantReadWriteLock();

// Consistent-Hash ring
private final NavigableMap<Integer, Consumer> hashRing;
private final NavigableMap<Integer, List<Consumer>> hashRing;

private final int numberOfPoints;

Expand All @@ -57,7 +60,17 @@ public void addConsumer(Consumer consumer) throws ConsumerAssignException {
for (int i = 0; i < numberOfPoints; i++) {
String key = consumer.consumerName() + i;
int hash = Murmur3_32Hash.getInstance().makeHash(key.getBytes());
hashRing.put(hash, consumer);
hashRing.compute(hash, (k, v) -> {
if (v == null) {
return Lists.newArrayList(consumer);
} else {
if (!v.contains(consumer)) {
v.add(consumer);
v.sort(Comparator.comparing(Consumer::consumerName, String::compareTo));
}
return v;
}
});
}
} finally {
rwLock.writeLock().unlock();
Expand All @@ -72,7 +85,17 @@ public void removeConsumer(Consumer consumer) {
for (int i = 0; i < numberOfPoints; i++) {
String key = consumer.consumerName() + i;
int hash = Murmur3_32Hash.getInstance().makeHash(key.getBytes());
hashRing.remove(hash, consumer);
hashRing.compute(hash, (k, v) -> {
if (v == null) {
return null;
} else {
v.removeIf(c -> c.consumerName().equals(consumer.consumerName()));
if (v.isEmpty()) {
v = null;
}
return v;
}
});
}
} finally {
rwLock.writeLock().unlock();
Expand All @@ -89,18 +112,21 @@ public Consumer select(byte[] stickyKey) {
return null;
}

Map.Entry<Integer, Consumer> ceilingEntry = hashRing.ceilingEntry(hash);
List<Consumer> consumerList;
Map.Entry<Integer, List<Consumer>> ceilingEntry = hashRing.ceilingEntry(hash);
if (ceilingEntry != null) {
return ceilingEntry.getValue();
consumerList = ceilingEntry.getValue();
} else {
return hashRing.firstEntry().getValue();
consumerList = hashRing.firstEntry().getValue();
}

return consumerList.get(hash % consumerList.size());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ltamber I have a question about this line of code in #23315 (comment) . Are you available to answer that question? Thanks /cc @codelipenghui

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change already, #23327

} finally {
rwLock.readLock().unlock();
}
}

Map<Integer, Consumer> getRangeConsumer() {
Map<Integer, List<Consumer>> getRangeConsumer() {
return Collections.unmodifiableMap(hashRing);
}
}