Java Code Examples for org.apache.pulsar.broker.ServiceConfiguration#isSubscriptionKeySharedUseConsistentHashing()

The following examples show how to use org.apache.pulsar.broker.ServiceConfiguration#isSubscriptionKeySharedUseConsistentHashing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentStickyKeyDispatcherMultipleConsumers.java    From pulsar with Apache License 2.0 6 votes vote down vote up
PersistentStickyKeyDispatcherMultipleConsumers(PersistentTopic topic, ManagedCursor cursor,
        Subscription subscription, ServiceConfiguration conf, KeySharedMeta ksm) {
    super(topic, cursor, subscription);

    this.allowOutOfOrderDelivery = ksm.getAllowOutOfOrderDelivery();
    this.recentlyJoinedConsumers = allowOutOfOrderDelivery ? Collections.emptyMap() : new HashMap<>();

    switch (ksm.getKeySharedMode()) {
    case AUTO_SPLIT:
        if (conf.isSubscriptionKeySharedUseConsistentHashing()) {
            selector = new ConsistentHashingStickyKeyConsumerSelector(
                    conf.getSubscriptionKeySharedConsistentHashingReplicaPoints());
        } else {
            selector = new HashRangeAutoSplitStickyKeyConsumerSelector();
        }
        break;

    case STICKY:
        this.selector = new HashRangeExclusiveStickyKeyConsumerSelector();
        break;

    default:
        throw new IllegalArgumentException("Invalid key-shared mode: " + ksm.getKeySharedMode());
    }
}
 
Example 2
Source File: NonPersistentSubscription.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void addConsumer(Consumer consumer) throws BrokerServiceException {
    if (IS_FENCED_UPDATER.get(this) == TRUE) {
        log.warn("Attempting to add consumer {} on a fenced subscription", consumer);
        throw new SubscriptionFencedException("Subscription is fenced");
    }

    if (dispatcher == null || !dispatcher.isConsumerConnected()) {
        Dispatcher previousDispatcher = null;

        switch (consumer.subType()) {
        case Exclusive:
            if (dispatcher == null || dispatcher.getType() != SubType.Exclusive) {
                previousDispatcher = dispatcher;
                dispatcher = new NonPersistentDispatcherSingleActiveConsumer(SubType.Exclusive, 0, topic, this);
            }
            break;
        case Shared:
            if (dispatcher == null || dispatcher.getType() != SubType.Shared) {
                previousDispatcher = dispatcher;
                dispatcher = new NonPersistentDispatcherMultipleConsumers(topic, this);
            }
            break;
        case Failover:
            int partitionIndex = TopicName.getPartitionIndex(topicName);
            if (partitionIndex < 0) {
                // For non partition topics, assume index 0 to pick a predictable consumer
                partitionIndex = 0;
            }

            if (dispatcher == null || dispatcher.getType() != SubType.Failover) {
                previousDispatcher = dispatcher;
                dispatcher = new NonPersistentDispatcherSingleActiveConsumer(SubType.Failover, partitionIndex,
                        topic, this);
            }
            break;
        case Key_Shared:
            if (dispatcher == null || dispatcher.getType() != SubType.Key_Shared) {
                previousDispatcher = dispatcher;
                KeySharedMeta ksm = consumer.getKeySharedMeta() != null ? consumer.getKeySharedMeta() : KeySharedMeta.getDefaultInstance();

                switch (ksm.getKeySharedMode()) {
                    case STICKY:
                        dispatcher = new NonPersistentStickyKeyDispatcherMultipleConsumers(topic, this,
                                new HashRangeExclusiveStickyKeyConsumerSelector());
                        break;

                    case AUTO_SPLIT:
                    default:
                        StickyKeyConsumerSelector selector;
                        ServiceConfiguration conf = topic.getBrokerService().getPulsar().getConfiguration();
                        if (conf.isSubscriptionKeySharedUseConsistentHashing()) {
                            selector = new ConsistentHashingStickyKeyConsumerSelector(
                                    conf.getSubscriptionKeySharedConsistentHashingReplicaPoints());
                        } else {
                            selector = new HashRangeAutoSplitStickyKeyConsumerSelector();
                        }

                        dispatcher = new NonPersistentStickyKeyDispatcherMultipleConsumers(topic, this, selector);
                        break;
                }
            }
            break;
        default:
            throw new ServerMetadataException("Unsupported subscription type");
        }

        if (previousDispatcher != null) {
            previousDispatcher.close().thenRun(() -> {
                log.info("[{}][{}] Successfully closed previous dispatcher", topicName, subName);
            }).exceptionally(ex -> {
                log.error("[{}][{}] Failed to close previous dispatcher", topicName, subName, ex);
                return null;
            });
        }
    } else {
        if (consumer.subType() != dispatcher.getType()) {
            throw new SubscriptionBusyException("Subscription is of different type");
        }
    }

    dispatcher.addConsumer(consumer);
}