nlu90 commented on a change in pull request #12838: URL: https://github.com/apache/pulsar/pull/12838#discussion_r770139258
########## File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java ########## @@ -0,0 +1,271 @@ +/** + * 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.pulsar.client.impl; + +import io.netty.util.Timeout; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.api.Message; +import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.PulsarClientException; +import org.apache.pulsar.client.api.Reader; +import org.apache.pulsar.client.api.Schema; +import org.apache.pulsar.client.api.TableView; +import org.apache.pulsar.common.util.FutureUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +@Slf4j +public class TableViewImpl<T> implements TableView<T> { + + private final PulsarClientImpl client; + private final Schema<T> schema; + private final TableViewConfigurationData conf; + + private final ConcurrentMap<String, T> data; + + private final ConcurrentMap<String, Reader<T>> readers; + + private final List<BiConsumer<String, T>> listeners; + private final ReentrantLock listenersMutex; + + TableViewImpl(PulsarClientImpl client, Schema<T> schema, TableViewConfigurationData conf) { + this.client = client; + this.schema = schema; + this.conf = conf; + this.data = new ConcurrentHashMap<>(); + this.readers = new ConcurrentHashMap<>(); + this.listeners = new ArrayList<>(); + this.listenersMutex = new ReentrantLock(); + } + + CompletableFuture<TableView<T>> start() { + return client.getPartitionsForTopic(conf.getTopicName()) + .thenCompose(partitions -> { + Set<String> partitionsSet = new HashSet<>(partitions); + List<CompletableFuture<?>> futures = new ArrayList<>(); + + // Add new Partitions + partitions.forEach(partition -> { + if (!readers.containsKey(partition)) { + futures.add(newReader(partition)); + } + }); + + // Remove partitions that are not used anymore + readers.forEach((existingPartition, existingReader) -> { + if (!partitionsSet.contains(existingPartition)) { + futures.add(existingReader.closeAsync() + .thenRun(() -> readers.remove(existingPartition, existingReader))); + } + }); + + return FutureUtil.waitForAll(futures) + .thenRun(() -> schedulePartitionsCheck()); + }).thenApply(__ -> this); + } + + private void schedulePartitionsCheck() { + client.timer() + .newTimeout(this::checkForPartitionsChanges, conf.getAutoUpdatePartitionsSeconds(), TimeUnit.SECONDS); + } + + private void checkForPartitionsChanges(Timeout timeout) { + if (timeout.isCancelled()) { + return ; + } + + start().whenComplete((tw, ex) -> { + if (ex != null) { + log.warn("Failed to check for changes in number of partitions"); + } + }); + } + + @Override + public int size() { + return data.size(); + } + + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + + @Override + public boolean containsKey(String key) { + return data.containsKey(key); + } + + @Override + public T get(String key) { + return data.get(key); + } + + @Override + public Set<Map.Entry<String, T>> entrySet() { + return data.entrySet(); + } + + @Override + public Set<String> keySet() { + return data.keySet(); + } + + @Override + public Collection<T> values() { + return data.values(); + } + + @Override + public void forEach(BiConsumer<String, T> action) { + data.forEach(action); + } + + @Override + public void forEachAndListen(BiConsumer<String, T> action) { + // Ensure we iterate over all the existing entry _and_ start the listening from the exact next message + try { + listenersMutex.lock(); + + // Execute the action over existing entries + forEach(action); + + listeners.add(action); + } finally { + listenersMutex.unlock(); + } + } + + @Override + public CompletableFuture<Void> closeAsync() { + return FutureUtil.waitForAll( + readers.values().stream() + .map(Reader::closeAsync) + .collect(Collectors.toList()) + ); + } + + @Override + public void close() throws PulsarClientException { + try { + closeAsync().get(); + } catch (Exception e) { + throw PulsarClientException.unwrap(e); + } + } + + private void handleMessage(Message<T> msg) { + try { + if (msg.hasKey()) { + if (log.isDebugEnabled()) { + log.debug("Applying message. key={} value={}", msg.getKey(), msg.getValue()); Review comment: @codelipenghui The topic name is logged during the initial replay: https://github.com/apache/pulsar/pull/12838/files#diff-4c15ad9e44a19d82a9c40274cad3950b0af335cb40bd537b130300510a6857d8R251 -- 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. To unsubscribe, e-mail: commits-unsubscr...@pulsar.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org