Search in sources :

Example 1 with CommandActiveConsumerChange

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange in project incubator-pulsar by apache.

the class Commands method newActiveConsumerChange.

public static ByteBuf newActiveConsumerChange(long consumerId, boolean isActive) {
    CommandActiveConsumerChange.Builder changeBuilder = CommandActiveConsumerChange.newBuilder().setConsumerId(consumerId).setIsActive(isActive);
    CommandActiveConsumerChange change = changeBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.ACTIVE_CONSUMER_CHANGE).setActiveConsumerChange(change));
    changeBuilder.recycle();
    change.recycle();
    return res;
}
Also used : CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with CommandActiveConsumerChange

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange in project incubator-pulsar by apache.

the class PersistentDispatcherFailoverConsumerTest method testAddRemoveConsumer.

@Test
public void testAddRemoveConsumer() throws Exception {
    log.info("--- Starting PersistentDispatcherFailoverConsumerTest::testAddConsumer ---");
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
    int partitionIndex = 0;
    PersistentDispatcherSingleActiveConsumer pdfc = new PersistentDispatcherSingleActiveConsumer(cursorMock, SubType.Failover, partitionIndex, topic);
    // 1. Verify no consumers connected
    assertFalse(pdfc.isConsumerConnected());
    // 2. Add consumer
    Consumer consumer1 = spy(new Consumer(sub, SubType.Exclusive, topic.getName(), 1, /* consumer id */
    0, "Cons1", /* consumer name */
    50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
    InitialPosition.Latest));
    pdfc.addConsumer(consumer1);
    List<Consumer> consumers = pdfc.getConsumers();
    assertTrue(consumers.get(0).consumerName() == consumer1.consumerName());
    assertEquals(1, consumers.size());
    CommandActiveConsumerChange change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, true);
    verify(consumer1, times(1)).notifyActiveConsumerChange(same(consumer1));
    // 3. Add again, duplicate allowed
    pdfc.addConsumer(consumer1);
    consumers = pdfc.getConsumers();
    assertTrue(consumers.get(0).consumerName() == consumer1.consumerName());
    assertEquals(2, consumers.size());
    // 4. Verify active consumer
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer1.consumerName());
    // get the notified with who is the leader
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, true);
    verify(consumer1, times(2)).notifyActiveConsumerChange(same(consumer1));
    // 5. Add another consumer which does not change active consumer
    Consumer consumer2 = spy(new Consumer(sub, SubType.Exclusive, topic.getName(), 2, /* consumer id */
    0, "Cons2", /* consumer name */
    50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
    InitialPosition.Latest));
    pdfc.addConsumer(consumer2);
    consumers = pdfc.getConsumers();
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer1.consumerName());
    assertEquals(3, consumers.size());
    // get notified with who is the leader
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 2, false);
    verify(consumer1, times(2)).notifyActiveConsumerChange(same(consumer1));
    verify(consumer2, times(1)).notifyActiveConsumerChange(same(consumer1));
    // 6. Add a consumer which changes active consumer
    Consumer consumer0 = spy(new Consumer(sub, SubType.Exclusive, topic.getName(), 0, /* consumer id */
    0, "Cons0", /* consumer name */
    50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
    InitialPosition.Latest));
    pdfc.addConsumer(consumer0);
    consumers = pdfc.getConsumers();
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer0.consumerName());
    assertEquals(4, consumers.size());
    // all consumers will receive notifications
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 0, true);
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, false);
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, false);
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 2, false);
    verify(consumer0, times(1)).notifyActiveConsumerChange(same(consumer0));
    verify(consumer1, times(2)).notifyActiveConsumerChange(same(consumer1));
    verify(consumer1, times(2)).notifyActiveConsumerChange(same(consumer0));
    verify(consumer2, times(1)).notifyActiveConsumerChange(same(consumer1));
    verify(consumer2, times(1)).notifyActiveConsumerChange(same(consumer0));
    // 7. Remove last consumer
    pdfc.removeConsumer(consumer2);
    consumers = pdfc.getConsumers();
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer0.consumerName());
    assertEquals(3, consumers.size());
    // not consumer group changes
    assertNull(consumerChanges.poll());
    // 8. Verify if we cannot unsubscribe when more than one consumer is connected
    assertFalse(pdfc.canUnsubscribe(consumer0));
    // 9. Remove active consumer
    pdfc.removeConsumer(consumer0);
    consumers = pdfc.getConsumers();
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer1.consumerName());
    assertEquals(2, consumers.size());
    // the remaining consumers will receive notifications
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, true);
    change = consumerChanges.take();
    verifyActiveConsumerChange(change, 1, true);
    // 10. Attempt to remove already removed consumer
    String cause = "";
    try {
        pdfc.removeConsumer(consumer0);
    } catch (Exception e) {
        cause = e.getMessage();
    }
    assertEquals(cause, "Consumer was not connected");
    // 11. Remove active consumer
    pdfc.removeConsumer(consumer1);
    consumers = pdfc.getConsumers();
    assertTrue(pdfc.getActiveConsumer().consumerName() == consumer1.consumerName());
    assertEquals(1, consumers.size());
    // not consumer group changes
    assertNull(consumerChanges.poll());
    // 11. With only one consumer, unsubscribe is allowed
    assertTrue(pdfc.canUnsubscribe(consumer1));
}
Also used : CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Test(org.testng.annotations.Test)

Example 3 with CommandActiveConsumerChange

use of org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange in project incubator-pulsar by apache.

the class PersistentDispatcherFailoverConsumerTest method testConsumerGroupChangesWithOldNewConsumers.

@Test
public void testConsumerGroupChangesWithOldNewConsumers() throws Exception {
    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
    int partitionIndex = 0;
    PersistentDispatcherSingleActiveConsumer pdfc = new PersistentDispatcherSingleActiveConsumer(cursorMock, SubType.Failover, partitionIndex, topic);
    // 1. Verify no consumers connected
    assertFalse(pdfc.isConsumerConnected());
    // 2. Add old consumer
    Consumer consumer1 = new Consumer(sub, SubType.Exclusive, topic.getName(), 1, /* consumer id */
    0, "Cons1", /* consumer name */
    50000, serverCnxWithOldVersion, "myrole-1", Collections.emptyMap(), false, InitialPosition.Latest);
    pdfc.addConsumer(consumer1);
    List<Consumer> consumers = pdfc.getConsumers();
    assertTrue(consumers.get(0).consumerName() == consumer1.consumerName());
    assertEquals(1, consumers.size());
    assertNull(consumerChanges.poll());
    verify(channelCtx, times(0)).write(any());
    // 3. Add new consumer
    Consumer consumer2 = new Consumer(sub, SubType.Exclusive, topic.getName(), 2, /* consumer id */
    0, "Cons2", /* consumer name */
    50000, serverCnx, "myrole-1", Collections.emptyMap(), false, InitialPosition.Latest);
    pdfc.addConsumer(consumer2);
    consumers = pdfc.getConsumers();
    assertTrue(consumers.get(0).consumerName() == consumer1.consumerName());
    assertEquals(2, consumers.size());
    CommandActiveConsumerChange change = consumerChanges.take();
    verifyActiveConsumerChange(change, 2, false);
    verify(channelCtx, times(1)).writeAndFlush(any(), any());
}
Also used : CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) PersistentDispatcherSingleActiveConsumer(org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer) Test(org.testng.annotations.Test)

Aggregations

CommandActiveConsumerChange (org.apache.pulsar.common.api.proto.PulsarApi.CommandActiveConsumerChange)3 PersistentDispatcherSingleActiveConsumer (org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)2 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2 Test (org.testng.annotations.Test)2 ByteBuf (io.netty.buffer.ByteBuf)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1