KKcorps commented on a change in pull request #7026: URL: https://github.com/apache/incubator-pinot/pull/7026#discussion_r648040234
########## File path: pinot-plugins/pinot-stream-ingestion/pinot-pulsar/src/main/java/org/apache/pinot/plugin/stream/pulsar/PulsarStreamLevelConsumerManager.java ########## @@ -0,0 +1,171 @@ +/** + * 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. + */ +package org.apache.pinot.plugin.stream.pulsar; + +import com.google.common.util.concurrent.Uninterruptibles; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.tuple.ImmutableTriple; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.PulsarClientException; +import org.apache.pulsar.client.api.Reader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class PulsarStreamLevelConsumerManager { + private static final Logger LOGGER = LoggerFactory.getLogger(PulsarStreamLevelConsumerManager.class); + private static final Long IN_USE = -1L; + private static final long CONSUMER_SHUTDOWN_DELAY_MILLIS = TimeUnit.SECONDS.toMillis(60); // One minute + private static final Map<ImmutableTriple<String, String, String>, Reader<byte[]>> CONSUMER_FOR_CONFIG_KEY = + new HashMap<>(); + private static final IdentityHashMap<Reader<byte[]>, Long> CONSUMER_RELEASE_TIME = new IdentityHashMap<>(); + protected static PulsarClient _pulsarClient; + protected static Reader<byte[]> _reader; + + public static Reader<byte[]> acquirePulsarConsumerForConfig(PulsarConfig pulsarStreamLevelStreamConfig) { + final ImmutableTriple<String, String, String> configKey = + new ImmutableTriple<>(pulsarStreamLevelStreamConfig.getPulsarTopicName(), + pulsarStreamLevelStreamConfig.getSubscriberId(), pulsarStreamLevelStreamConfig.getBootstrapServers()); + + synchronized (PulsarStreamLevelConsumerManager.class) { + // If we have the consumer and it's not already acquired, return it, otherwise error out if it's already acquired + if (CONSUMER_FOR_CONFIG_KEY.containsKey(configKey)) { + Reader<byte[]> pulsarConsumer = CONSUMER_FOR_CONFIG_KEY.get(configKey); + if (CONSUMER_RELEASE_TIME.get(pulsarConsumer).equals(IN_USE)) { + throw new RuntimeException("Consumer " + pulsarConsumer + " already in use!"); + } else { + LOGGER.info("Reusing pulsar consumer with id {}", pulsarConsumer); + CONSUMER_RELEASE_TIME.put(pulsarConsumer, IN_USE); + return pulsarConsumer; + } + } + + LOGGER.info("Creating new pulsar consumer and iterator for topic {}", + pulsarStreamLevelStreamConfig.getPulsarTopicName()); + + // Create the consumer + + Properties consumerProp = new Properties(); Review comment: Not needed with Reader interface. It was only needed for Consumer interface which is not being used now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org