You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by aljoscha <gi...@git.apache.org> on 2017/05/04 12:11:21 UTC

[GitHub] flink pull request #3746: [FLINK-4022] [kafka] Partition and topic discovery...

Github user aljoscha commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3746#discussion_r114757153
  
    --- Diff: flink-connectors/flink-connector-kafka-base/src/main/java/org/apache/flink/streaming/connectors/kafka/internals/AbstractPartitionDiscoverer.java ---
    @@ -0,0 +1,254 @@
    +/*
    + * 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.flink.streaming.connectors.kafka.internals;
    +
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.apache.flink.util.Preconditions.checkNotNull;
    +
    +/**
    + * Base class for all partition discoverers.
    + *
    + * <p>This partition discoverer base class implements the logic around bookkeeping
    + * discovered partitions, and using the information to determine whether or not there
    + * are new partitions that the consumer subtask should subscribe to.
    + *
    + * <p>Subclass implementations should simply implement the logic of using the version-specific
    + * Kafka clients to fetch topic and partition metadata.
    + *
    + * <p>Since Kafka clients are generally not thread-safe, partition discoverers should
    + * not be concurrently accessed. The only exception for this would be the {@link #wakeup()}
    + * call, which allows the discoverer to be interrupted during a {@link #discoverPartitions()} call.
    + */
    +public abstract class AbstractPartitionDiscoverer {
    +
    +	/** Describes whether we are discovering partitions for fixed topics or a topic pattern. */
    +	private final KafkaTopicsDescriptor topicsDescriptor;
    +
    +	/** Index of the consumer subtask that this partition discoverer belongs to. */
    +	private final int indexOfThisSubtask;
    +
    +	/** The total number of consumer subtasks. */
    +	private final int numParallelSubtasks;
    +
    +	/** Flag to determine whether or not the discoverer is closed. */
    +	private volatile boolean closed = true;
    +
    +	/**
    +	 * Flag to determine whether or not the discoverer had been woken up.
    +	 * When set to {@code true}, {@link #discoverPartitions()} would be interrupted as early as possible.
    +	 * Once interrupted, the flag is reset.
    +	 */
    +	private volatile boolean wakeup;
    +
    +	/**
    +	 * Map of topics to they're largest discovered partition id seen by this subtask.
    +	 * This state may be updated whenever {@link AbstractPartitionDiscoverer#discoverPartitions()} or
    +	 * {@link AbstractPartitionDiscoverer#setAndCheckDiscoveredPartition(KafkaTopicPartition)} is called.
    +	 *
    +	 * This is used to remove old partitions from the fetched partition lists. It is sufficient
    +	 * to keep track of only the largest partition id because Kafka partition numbers are only
    +	 * allowed to be increased and has incremental ids.
    +	 */
    +	private final Map<String, Integer> topicsToLargestDiscoveredPartitionId;
    +
    +	public AbstractPartitionDiscoverer(
    +			KafkaTopicsDescriptor topicsDescriptor,
    +			int indexOfThisSubtask,
    +			int numParallelSubtasks) {
    +
    +		this.topicsDescriptor = checkNotNull(topicsDescriptor);
    +		this.indexOfThisSubtask = indexOfThisSubtask;
    +		this.numParallelSubtasks = numParallelSubtasks;
    +		this.topicsToLargestDiscoveredPartitionId = new HashMap<>();
    +	}
    +
    +	/**
    +	 * Opens the partition discoverer, initializing all required Kafka connections.
    +	 *
    +	 * <p>NOTE: thread-safety is not guaranteed.
    +	 */
    +	public void open() throws Exception {
    +		closed = false;
    +		initializeConnections();
    +	}
    +
    +	/**
    +	 * Closes the partition discoverer, cleaning up all Kafka connections.
    +	 *
    +	 * <p>NOTE: thread-safety is not guaranteed.
    +	 */
    +	public void close() throws Exception {
    +		closed = true;
    +		closeConnections();
    +	}
    +
    +	/**
    +	 * Interrupt an in-progress discovery attempt by throwing a {@link WakeupException}.
    +	 * If no attempt is in progress, the immediate next attempt will throw a {@link WakeupException}.
    +	 *
    +	 * <p>This method can be called concurrently from a different thread.
    +	 */
    +	public void wakeup() {
    +		wakeup = true;
    +		wakeupConnections();
    +	}
    +
    +	/**
    +	 * Execute a partition discovery attempt for this subtask.
    +	 * This method lets the partition discoverer update what partitions it has discovered so far.
    +	 *
    +	 * @return List of discovered new partitions that this subtask should subscribe to.
    +	 */
    +	public List<KafkaTopicPartition> discoverPartitions() throws WakeupException, ClosedException {
    +		if (!closed && !wakeup) {
    +			try {
    +				List<KafkaTopicPartition> newDiscoveredPartitions;
    +
    +				// (1) get all possible partitions, based on whether we are subscribed to fixed topics or a topic patern
    --- End diff --
    
    Typo "patern"


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---