KKcorps commented on a change in pull request #7026: URL: https://github.com/apache/incubator-pinot/pull/7026#discussion_r658105036
########## File path: pinot-plugins/pinot-stream-ingestion/pinot-pulsar/src/main/java/org/apache/pinot/plugin/stream/pulsar/PulsarPartitionLevelConsumer.java ########## @@ -0,0 +1,119 @@ +/** + * 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.collect.Iterables; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.pinot.spi.stream.MessageBatch; +import org.apache.pinot.spi.stream.PartitionGroupConsumer; +import org.apache.pinot.spi.stream.PartitionGroupConsumptionStatus; +import org.apache.pinot.spi.stream.StreamConfig; +import org.apache.pinot.spi.stream.StreamPartitionMsgOffset; +import org.apache.pulsar.client.api.Message; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.PulsarClientException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@link PartitionGroupConsumer} implementation for the Pulsar stream + */ +public class PulsarPartitionLevelConsumer extends PulsarPartitionLevelConnectionHandler implements PartitionGroupConsumer { + private static final Logger LOGGER = LoggerFactory.getLogger(PulsarPartitionLevelConsumer.class); + private final ExecutorService _executorService; + + public PulsarPartitionLevelConsumer(String clientId, StreamConfig streamConfig, + PartitionGroupConsumptionStatus partitionGroupConsumptionStatus) { + super(clientId, streamConfig, partitionGroupConsumptionStatus.getPartitionGroupId()); + _executorService = Executors.newSingleThreadExecutor(); + } + + /** + * Fetch records from the Pulsar stream between the start and end KinesisCheckpoint + * Used {@link org.apache.pulsar.client.api.Reader} to read the messaged from pulsar partitioned topic + * The reader seeks to the startMsgOffset and starts reading records in a loop until endMsgOffset or timeout is reached. + */ + @Override + public MessageBatch fetchMessages(StreamPartitionMsgOffset startMsgOffset, StreamPartitionMsgOffset endMsgOffset, + int timeoutMillis) { + final MessageId startMessageId = ((MessageIdStreamOffset) startMsgOffset).getMessageId(); + final MessageId endMessageId = + endMsgOffset == null ? MessageId.latest : ((MessageIdStreamOffset) endMsgOffset).getMessageId(); + + List<Message<byte[]>> messagesList = new ArrayList<>(); + Future<PulsarMessageBatch> pulsarResultFuture = + _executorService.submit(() -> fetchMessages(startMessageId, endMessageId, messagesList)); + + try { + return pulsarResultFuture.get(timeoutMillis, TimeUnit.MILLISECONDS); + } catch (TimeoutException e) { + // The fetchMessages has thrown an exception. Most common cause is the timeout. + // We return the records fetched till now along with the next start offset. + return new PulsarMessageBatch(buildOffsetFilteringIterable(messagesList, startMessageId, endMessageId)); Review comment: Yes. Does it look fine now? @mcvsubbu -- 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