Skip to content
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 @@ -155,8 +155,6 @@ public synchronized void addConsumer(Consumer consumer) throws BrokerServiceExce
throw new ConsumerBusyException("Subscription reached max consumers limit");
}

consumers.add(consumer);

if (subscriptionType == SubType.Exclusive
&& consumer.getKeySharedMeta() != null
&& consumer.getKeySharedMeta().getHashRangesList() != null
Expand All @@ -168,6 +166,8 @@ public synchronized void addConsumer(Consumer consumer) throws BrokerServiceExce
isKeyHashRangeFiltered = false;
}

consumers.add(consumer);

if (!pickAndScheduleActiveConsumer()) {
// the active consumer is not changed
Consumer currentActiveConsumer = ACTIVE_CONSUMER_UPDATER.get(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public NonPersistentStickyKeyDispatcherMultipleConsumers(NonPersistentTopic topi
@Override
public synchronized void addConsumer(Consumer consumer) throws BrokerServiceException {
super.addConsumer(consumer);
selector.addConsumer(consumer);
try {
selector.addConsumer(consumer);
} catch (BrokerServiceException e) {
consumerSet.removeAll(consumer);
consumerList.remove(consumer);
throw e;
}
}

@Override
Expand Down Expand Up @@ -99,4 +105,4 @@ public void sendMessages(List<Entry> entries) {
TOTAL_AVAILABLE_PERMITS_UPDATER.addAndGet(this, -sendMessageInfo.getTotalMessages());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ public class PersistentStickyKeyDispatcherMultipleConsumers extends PersistentDi
@Override
public synchronized void addConsumer(Consumer consumer) throws BrokerServiceException {
super.addConsumer(consumer);
selector.addConsumer(consumer);
try {
selector.addConsumer(consumer);
} catch (BrokerServiceException e) {
consumerSet.removeAll(consumer);
consumerList.remove(consumer);
throw e;
}

// If this was the 1st consumer, or if all the messages are already acked, then we
// don't need to do anything special
Expand Down Expand Up @@ -294,4 +300,4 @@ protected Set<? extends Position> asyncReplayEntries(Set<? extends Position> pos

private static final Logger log = LoggerFactory.getLogger(PersistentStickyKeyDispatcherMultipleConsumers.class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public void testRemoveFirstConsumer() throws Exception {
@Test
public void testHashRangeConflict() throws PulsarClientException {
this.conf.setSubscriptionKeySharedEnable(true);
final String topic = "testHashRangeConflict-" + UUID.randomUUID().toString();
final String topic = "persistent://public/default/testHashRangeConflict-" + UUID.randomUUID().toString();
final String sub = "test";

Consumer<String> consumer1 = createFixedHashRangesConsumer(topic, sub, Range.of(0,99), Range.of(400, 65535));
Expand All @@ -667,6 +667,10 @@ public void testHashRangeConflict() throws PulsarClientException {
Consumer<String> consumer2 = createFixedHashRangesConsumer(topic, sub, Range.of(100,399));
Assert.assertTrue(consumer2.isConnected());

PersistentStickyKeyDispatcherMultipleConsumers dispatcher = (PersistentStickyKeyDispatcherMultipleConsumers) pulsar
.getBrokerService().getTopicReference(topic).get().getSubscription(sub).getDispatcher();
Assert.assertEquals(dispatcher.getConsumers().size(), 2);

try {
createFixedHashRangesConsumer(topic, sub, Range.of(0, 65535));
Assert.fail("Should failed with conflict range.");
Expand All @@ -679,7 +683,9 @@ public void testHashRangeConflict() throws PulsarClientException {
} catch (PulsarClientException.ConsumerAssignException ignore) {
}

Assert.assertEquals(dispatcher.getConsumers().size(), 2);
consumer1.close();
Assert.assertEquals(dispatcher.getConsumers().size(), 1);

try {
createFixedHashRangesConsumer(topic, sub, Range.of(0, 65535));
Expand All @@ -705,9 +711,11 @@ public void testHashRangeConflict() throws PulsarClientException {
Consumer<String> consumer4 = createFixedHashRangesConsumer(topic, sub, Range.of(50,99));
Assert.assertTrue(consumer4.isConnected());

Assert.assertEquals(dispatcher.getConsumers().size(), 3);
consumer2.close();
consumer3.close();
consumer4.close();
Assert.assertFalse(dispatcher.isConsumerConnected());
}

private Consumer<String> createFixedHashRangesConsumer(String topic, String subscription, Range... ranges) throws PulsarClientException {
Expand Down