You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ij...@apache.org on 2017/01/13 02:31:24 UTC

kafka git commit: MINOR: Minor improvements in consumer close timeout handling

Repository: kafka
Updated Branches:
  refs/heads/trunk 2b6ba55c7 -> 2eb32d6a7


MINOR: Minor improvements in consumer close timeout handling

Author: Jason Gustafson <ja...@confluent.io>

Reviewers: Rajini Sivaram <ra...@googlemail.com>, Ismael Juma <is...@juma.me.uk>

Closes #2348 from hachikuji/minor-cleanup-for-kip-102


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/2eb32d6a
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/2eb32d6a
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/2eb32d6a

Branch: refs/heads/trunk
Commit: 2eb32d6a70bcdef86bf87f9afd56b70de9df505e
Parents: 2b6ba55
Author: Jason Gustafson <ja...@confluent.io>
Authored: Fri Jan 13 02:31:19 2017 +0000
Committer: Ismael Juma <is...@juma.me.uk>
Committed: Fri Jan 13 02:31:19 2017 +0000

----------------------------------------------------------------------
 .../kafka/clients/consumer/KafkaConsumer.java   |  9 +++---
 .../consumer/internals/AbstractCoordinator.java | 34 ++++++++++----------
 .../consumer/internals/ConsumerCoordinator.java | 25 ++++++++++----
 .../internals/ConsumerNetworkClient.java        | 15 +++++++--
 .../internals/ConsumerNetworkClientTest.java    |  2 +-
 5 files changed, 52 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/2eb32d6a/clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java b/clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
index 9c1bc89..bbad999 100644
--- a/clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
+++ b/clients/src/main/java/org/apache/kafka/clients/consumer/KafkaConsumer.java
@@ -1514,12 +1514,11 @@ public class KafkaConsumer<K, V> implements Consumer<K, V> {
      * Tries to close the consumer cleanly within the specified timeout. This method waits up to
      * <code>timeout</code> for the consumer to complete pending commits and leave the group.
      * If auto-commit is enabled, this will commit the current offsets if possible within the
-     * timeout. If rebalance is in progress, auto-commits and pending asynchronous commits may
-     * be aborted if the coordinator is not known within the timeout. If the consumer is unable
-     * to complete commit and leave group requests before the timeout expires, the consumer is
-     * force closed. Note that {@link #wakeup()} cannot be used to interrupt close.
+     * timeout. If the consumer is unable to complete offset commits and gracefully leave the group
+     * before the timeout expires, the consumer is force closed. Note that {@link #wakeup()} cannot be
+     * used to interrupt close.
      *
-     * @param timeout The maximum time to wait for consumer to close gracefully. The value should be
+     * @param timeout The maximum time to wait for consumer to close gracefully. The value must be
      *                non-negative. Specifying a timeout of zero means do not wait for pending requests to complete.
      * @param timeUnit The time unit for the <code>timeout</code>
      * @throws InterruptException If the thread is interrupted before or while this function is called

http://git-wip-us.apache.org/repos/asf/kafka/blob/2eb32d6a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java
index 0bd93cb..7d77c0b 100644
--- a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java
+++ b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java
@@ -192,9 +192,15 @@ public abstract class AbstractCoordinator implements Closeable {
         ensureCoordinatorReady(0, Long.MAX_VALUE);
     }
 
-    protected synchronized long ensureCoordinatorReady(long now, long timeoutMs) {
+    /**
+     * Ensure that the coordinator is ready to receive requests.
+     * @param startTimeMs Current time in milliseconds
+     * @param timeoutMs Maximum time to wait to discover the coordinator
+     * @return true If coordinator discovery and initial connection succeeded, false otherwise
+     */
+    protected synchronized boolean ensureCoordinatorReady(long startTimeMs, long timeoutMs) {
         long remainingMs = timeoutMs;
-        long startTimeMs = now;
+
         while (coordinatorUnknown()) {
             RequestFuture<Void> future = lookupCoordinator();
             client.poll(future, remainingMs);
@@ -202,6 +208,9 @@ public abstract class AbstractCoordinator implements Closeable {
             if (future.failed()) {
                 if (future.isRetriable()) {
                     remainingMs = timeoutMs - (time.milliseconds() - startTimeMs);
+                    if (remainingMs <= 0)
+                        break;
+
                     client.awaitMetadataUpdate(remainingMs);
                 } else
                     throw future.exception();
@@ -211,11 +220,13 @@ public abstract class AbstractCoordinator implements Closeable {
                 coordinatorDead();
                 time.sleep(retryBackoffMs);
             }
+
             remainingMs = timeoutMs - (time.milliseconds() - startTimeMs);
             if (remainingMs <= 0)
                 break;
         }
-        return remainingMs;
+
+        return !coordinatorUnknown();
     }
 
     protected synchronized RequestFuture<Void> lookupCoordinator() {
@@ -647,21 +658,10 @@ public abstract class AbstractCoordinator implements Closeable {
         // interrupted using wakeup) and the leave group request which have been queued, but not
         // yet sent to the broker. Wait up to close timeout for these pending requests to be processed.
         // If coordinator is not known, requests are aborted.
-        long now = time.milliseconds();
-        long endTimeMs = now + timeoutMs;
         Node coordinator = coordinator();
-        while (coordinator != null && client.pendingRequestCount(coordinator) > 0) {
-            if (Thread.currentThread().isInterrupted())
-                throw new InterruptException("Consumer close was interrupted");
-            long remainingTimeMs = endTimeMs - now;
-            client.poll(remainingTimeMs > 0 ? remainingTimeMs : 0);
-            now = time.milliseconds();
-            if (client.pendingRequestCount(coordinator) > 0 && now >= endTimeMs) {
-                log.warn("Close timed out with {} pending requests to coordinator, terminating client connections for group {}.",
-                        client.pendingRequestCount(coordinator), groupId);
-                break;
-            }
-        }
+        if (coordinator != null && !client.awaitPendingRequests(coordinator, timeoutMs))
+            log.warn("Close timed out with {} pending requests to coordinator, terminating client connections for group {}.",
+                    client.pendingRequestCount(coordinator), groupId);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/kafka/blob/2eb32d6a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java
index c963cca..3d16736 100644
--- a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java
+++ b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinator.java
@@ -411,12 +411,12 @@ public final class ConsumerCoordinator extends AbstractCoordinator {
         try {
             maybeAutoCommitOffsetsSync(timeoutMs);
             now = time.milliseconds();
-            if (pendingAsyncCommits.get() > 0) {
+            if (pendingAsyncCommits.get() > 0 && endTimeMs > now) {
                 ensureCoordinatorReady(now, endTimeMs - now);
                 now = time.milliseconds();
             }
         } finally {
-            super.close(endTimeMs - now);
+            super.close(Math.max(0, endTimeMs - now));
         }
     }
 
@@ -497,18 +497,25 @@ public final class ConsumerCoordinator extends AbstractCoordinator {
      * @throws org.apache.kafka.common.errors.AuthorizationException if the consumer is not authorized to the group
      *             or to any of the specified partitions
      * @throws CommitFailedException if an unrecoverable error occurs before the commit can be completed
+     * @return If the offset commit was successfully sent and a successful response was received from
+     *         the coordinator
      */
-    public void commitOffsetsSync(Map<TopicPartition, OffsetAndMetadata> offsets, long timeoutMs) {
+    public boolean commitOffsetsSync(Map<TopicPartition, OffsetAndMetadata> offsets, long timeoutMs) {
         invokeCompletedOffsetCommitCallbacks();
 
         if (offsets.isEmpty())
-            return;
+            return true;
 
         long now = time.milliseconds();
         long startMs = now;
         long remainingMs = timeoutMs;
         do {
-            remainingMs = ensureCoordinatorReady(now, remainingMs);
+            if (coordinatorUnknown()) {
+                if (!ensureCoordinatorReady(now, remainingMs))
+                    return false;
+
+                remainingMs = timeoutMs - (time.milliseconds() - startMs);
+            }
 
             RequestFuture<Void> future = sendOffsetCommitRequest(offsets);
             client.poll(future, remainingMs);
@@ -516,7 +523,7 @@ public final class ConsumerCoordinator extends AbstractCoordinator {
             if (future.succeeded()) {
                 if (interceptors != null)
                     interceptors.onCommit(offsets);
-                return;
+                return true;
             }
 
             if (!future.isRetriable())
@@ -527,6 +534,8 @@ public final class ConsumerCoordinator extends AbstractCoordinator {
             now = time.milliseconds();
             remainingMs = timeoutMs - (now - startMs);
         } while (remainingMs > 0);
+
+        return false;
     }
 
     private void maybeAutoCommitOffsetsAsync(long now) {
@@ -563,8 +572,10 @@ public final class ConsumerCoordinator extends AbstractCoordinator {
     private void maybeAutoCommitOffsetsSync(long timeoutMs) {
         if (autoCommitEnabled) {
             try {
-                commitOffsetsSync(subscriptions.allConsumed(), timeoutMs);
+                if (!commitOffsetsSync(subscriptions.allConsumed(), timeoutMs))
+                    log.debug("Automatic commit of offsets {} for group {} timed out before completion", subscriptions.allConsumed(), groupId);
             } catch (WakeupException | InterruptException e) {
+                log.debug("Automatic commit of offsets {} for group {} was interrupted before completion", subscriptions.allConsumed(), groupId);
                 // rethrow wakeups since they are triggered by the user
                 throw e;
             } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/kafka/blob/2eb32d6a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClient.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClient.java b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClient.java
index e91a159..ea92ab7 100644
--- a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClient.java
+++ b/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClient.java
@@ -269,10 +269,19 @@ public class ConsumerNetworkClient implements Closeable {
     /**
      * Block until all pending requests from the given node have finished.
      * @param node The node to await requests from
+     * @param timeoutMs The maximum time in milliseconds to block
+     * @return true If all requests finished, false if the timeout expired first
      */
-    public void awaitPendingRequests(Node node) {
-        while (pendingRequestCount(node) > 0)
-            poll(retryBackoffMs);
+    public boolean awaitPendingRequests(Node node, long timeoutMs) {
+        long startMs = time.milliseconds();
+        long remainingMs = timeoutMs;
+
+        while (pendingRequestCount(node) > 0 && remainingMs > 0) {
+            poll(remainingMs);
+            remainingMs = timeoutMs - (time.milliseconds() - startMs);
+        }
+
+        return pendingRequestCount(node) == 0;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/kafka/blob/2eb32d6a/clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClientTest.java
----------------------------------------------------------------------
diff --git a/clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClientTest.java b/clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClientTest.java
index 2493ed8..3694714 100644
--- a/clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClientTest.java
+++ b/clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerNetworkClientTest.java
@@ -71,7 +71,7 @@ public class ConsumerNetworkClientTest {
         assertEquals(2, consumerClient.pendingRequestCount());
         assertEquals(2, consumerClient.pendingRequestCount(node));
 
-        consumerClient.awaitPendingRequests(node);
+        consumerClient.awaitPendingRequests(node, Long.MAX_VALUE);
         assertTrue(future1.succeeded());
         assertTrue(future2.succeeded());
     }