ivankelly commented on a change in pull request #2219: Cpp client: add PatternMultiTopicsConsumerImpl to support regex subscribe URL: https://github.com/apache/incubator-pulsar/pull/2219#discussion_r207469293
########## File path: pulsar-client-cpp/lib/PatternMultiTopicsConsumerImpl.cc ########## @@ -0,0 +1,235 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "PatternMultiTopicsConsumerImpl.h" + +DECLARE_LOG_OBJECT() + +using namespace pulsar; + +PatternMultiTopicsConsumerImpl::PatternMultiTopicsConsumerImpl(ClientImplPtr client, + const std::string pattern, + const std::vector<std::string>& topics, + const std::string& subscriptionName, + const ConsumerConfiguration& conf, + const LookupServicePtr lookupServicePtr_) + : MultiTopicsConsumerImpl(client, topics, subscriptionName, TopicName::get(pattern), conf, + lookupServicePtr_), + patternString_(pattern), + pattern_(std::regex(pattern)), + autoDiscoveryTimer_(), + autoDiscoveryRunning_(false) {} + +const std::regex PatternMultiTopicsConsumerImpl::getPattern() { return pattern_; } + +void PatternMultiTopicsConsumerImpl::resetAutoDiscoveryTimer() { + autoDiscoveryRunning_ = false; + autoDiscoveryTimer_->expires_from_now(seconds(conf_.getPatternAutoDiscoveryPeriod())); + autoDiscoveryTimer_->async_wait( + boost::bind(&PatternMultiTopicsConsumerImpl::autoDiscoveryTimerTask, this, _1)); +} + +void PatternMultiTopicsConsumerImpl::autoDiscoveryTimerTask(const boost::system::error_code& err) { + if (err == boost::asio::error::operation_aborted) { + LOG_DEBUG(getName() << "Timer cancelled: " << err.message()); + return; + } else if (err) { + LOG_ERROR(getName() << "Timer error: " << err.message()); + return; + } + + if (state_ != Ready) { + LOG_ERROR("Error in autoDiscoveryTimerTask consumer state not ready: " << state_); + resetAutoDiscoveryTimer(); + return; + } + + if (autoDiscoveryRunning_) { + LOG_DEBUG("autoDiscoveryTimerTask still running, cancel this running. "); + return; + } + + autoDiscoveryRunning_ = true; + + // already get namespace from pattern. + assert(namespaceName_); + + lookupServicePtr_->getTopicsOfNamespaceAsync(namespaceName_) + .addListener(boost::bind(&PatternMultiTopicsConsumerImpl::timerGetTopicsOfNamespace, this, _1, _2)); +} + +void PatternMultiTopicsConsumerImpl::timerGetTopicsOfNamespace(const Result result, + const NamespaceTopicsPtr topics) { + if (result != ResultOk) { + LOG_ERROR("Error in Getting topicsOfNameSpace. result: " << result); + resetAutoDiscoveryTimer(); + return; + } + + NamespaceTopicsPtr newTopics = PatternMultiTopicsConsumerImpl::topicsPatternFilter(*topics, pattern_); + // get old topics in consumer: + NamespaceTopicsPtr oldTopics = boost::make_shared<std::vector<std::string>>(); + for (std::map<std::string, int>::iterator it = topicsPartitions_.begin(); it != topicsPartitions_.end(); + it++) { + oldTopics->push_back(it->first); + } + NamespaceTopicsPtr topicsAdded = topicsListsMinus(*newTopics, *oldTopics); + NamespaceTopicsPtr topicsRemoved = topicsListsMinus(*oldTopics, *newTopics); + + // callback method when removed topics all un-subscribed. + ResultCallback topicsRemovedCallback = [this](Result result) { + if (result != ResultOk) { + LOG_ERROR("Failed to unsubscribe topics: " << result); + } + resetAutoDiscoveryTimer(); + }; + + // callback method when added topics all subscribed. + ResultCallback topicsAddedCallback = [this, topicsRemoved, topicsRemovedCallback](Result result) { + if (result == ResultOk) { + // call to unsubscribe all removed topics. + onTopicsRemoved(topicsRemoved, topicsRemovedCallback); + } else { + resetAutoDiscoveryTimer(); + } + }; + + // call to subscribe new added topics, then in its callback do unsubscribe + onTopicsAdded(topicsListsMinus(*newTopics, *oldTopics), topicsAddedCallback); Review comment: You've already declare topicsAdded above, so it can just be passed in. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
