{"action":"created","comment":{"url":"https://api.github.com/repos/apache/storm/pulls/comments/53723835","id":53723835,"diff_hunk":"@@ -0,0 +1,457 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ *   or more contributor license agreements.  See the NOTICE file\n+ *   distributed with this work for additional information\n+ *   regarding copyright ownership.  The ASF licenses this file\n+ *   to you under the Apache License, Version 2.0 (the\n+ *   \"License\"); you may not use this file except in compliance\n+ *   with the License.  You may obtain a copy of the License at\n+ *\n+ *   http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ *   Unless required by applicable law or agreed to in writing, software\n+ *   distributed under the License is distributed on an \"AS IS\" BASIS,\n+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ *   See the License for the specific language governing permissions and\n+ *   limitations under the License.\n+ */\n+\n+package org.apache.storm.kafka.spout;\n+\n+import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;\n+import org.apache.kafka.clients.consumer.ConsumerRecord;\n+import org.apache.kafka.clients.consumer.ConsumerRecords;\n+import org.apache.kafka.clients.consumer.KafkaConsumer;\n+import org.apache.kafka.clients.consumer.OffsetAndMetadata;\n+import org.apache.kafka.common.TopicPartition;\n+import org.apache.storm.spout.SpoutOutputCollector;\n+import org.apache.storm.task.TopologyContext;\n+import org.apache.storm.topology.OutputFieldsDeclarer;\n+import org.apache.storm.topology.base.BaseRichSpout;\n+import org.apache.storm.tuple.Values;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.util.Collection;\n+import java.util.Comparator;\n+import java.util.HashMap;\n+import java.util.HashSet;\n+import java.util.Map;\n+import java.util.Set;\n+import java.util.TreeSet;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.ScheduledExecutorService;\n+import java.util.concurrent.ThreadFactory;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.locks.Lock;\n+import java.util.concurrent.locks.ReentrantLock;\n+\n+public class KafkaSpout<K,V> extends BaseRichSpout {\n+    private static final Logger LOG = LoggerFactory.getLogger(KafkaSpout.class);\n+    private static final Comparator<org.apache.storm.kafka.spout.MessageId> OFFSET_COMPARATOR = new OffsetComparator();\n+\n+    // Storm\n+    private Map conf;\n+    private TopologyContext context;\n+    protected SpoutOutputCollector collector;\n+\n+    // Kafka\n+    private final org.apache.storm.kafka.spout.KafkaSpoutConfig<K, V> kafkaSpoutConfig;\n+    private KafkaConsumer<K, V> kafkaConsumer;\n+\n+    // Bookkeeping\n+    private org.apache.storm.kafka.spout.KafkaSpoutStream kafkaSpoutStream;\n+    private org.apache.storm.kafka.spout.KafkaTupleBuilder<K,V> tupleBuilder;\n+    private transient ScheduledExecutorService commitOffsetsTask;\n+    private transient Lock ackCommitLock;\n+    private transient volatile boolean commit;\n+    private transient Map<org.apache.storm.kafka.spout.MessageId, Values> emittedTuples;           // Keeps a list of emitted tuples that are pending being acked or failed\n+    private transient Map<TopicPartition, Set<org.apache.storm.kafka.spout.MessageId>> failed;     // failed tuples. They stay in this list until success or max retries is reached\n+    private transient Map<TopicPartition, OffsetEntry> acked;         // emitted tuples that were successfully acked. These tuples will be committed by the commitOffsetsTask or on consumer rebalance\n+    private transient Set<org.apache.storm.kafka.spout.MessageId> blackList;                       // all the tuples that are in traffic when the rebalance occurs will be added to black list to be disregarded when they are either acked or failed\n+    private transient int maxRetries;\n+\n+    public KafkaSpout(org.apache.storm.kafka.spout.KafkaSpoutConfig<K,V> kafkaSpoutConfig, org.apache.storm.kafka.spout.KafkaSpoutStream kafkaSpoutStream, org.apache.storm.kafka.spout.KafkaTupleBuilder<K,V> tupleBuilder) {\n+        this.kafkaSpoutConfig = kafkaSpoutConfig;                 // Pass in configuration\n+        this.kafkaSpoutStream = kafkaSpoutStream;\n+        this.tupleBuilder = tupleBuilder;\n+    }\n+\n+    @Override\n+    public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {\n+        // Spout internals\n+        this.conf = conf;\n+        this.context = context;\n+        this.collector = collector;\n+\n+        // Bookkeeping objects\n+        emittedTuples = new HashMap<>();\n+        failed = new HashMap<>();\n+        acked = new HashMap<>();\n+        blackList = new HashSet<>();\n+        ackCommitLock = new ReentrantLock();\n+        maxRetries = kafkaSpoutConfig.getMaxTupleRetries();\n+\n+        // Kafka consumer\n+        kafkaConsumer = new KafkaConsumer<>(kafkaSpoutConfig.getKafkaProps(),\n+                kafkaSpoutConfig.getKeyDeserializer(), kafkaSpoutConfig.getValueDeserializer());\n+        kafkaConsumer.subscribe(kafkaSpoutConfig.getSubscribedTopics(), new KafkaSpoutConsumerRebalanceListener());\n+\n+        // Create commit offsets task\n+        if (!kafkaSpoutConfig.isConsumerAutoCommitMode()) {     // If it is auto commit, no need to commit offsets manually\n+            createCommitOffsetsTask();\n+        }\n+    }\n+\n+    // ======== Commit Offsets Task =======\n+\n+    private void createCommitOffsetsTask() {\n+        commitOffsetsTask = Executors.newSingleThreadScheduledExecutor(commitOffsetsThreadFactory());\n+        commitOffsetsTask.scheduleAtFixedRate(new Runnable() {\n+            @Override\n+            public void run() {\n+                commit = true;\n+            }\n+        }, 1000, kafkaSpoutConfig.getOffsetsCommitFreqMs(), TimeUnit.MILLISECONDS);\n+    }\n+\n+    private ThreadFactory commitOffsetsThreadFactory() {\n+        return new ThreadFactory() {\n+            @Override\n+            public Thread newThread(Runnable r) {\n+                return new Thread(r, \"kafka-spout-commit-offsets-thread\");\n+            }\n+        };\n+    }\n+\n+    // ======== Next Tuple =======\n+\n+    @Override\n+    public void nextTuple() {\n+        if(commit) {\n+            commitAckedTuples();\n+        } else if (retry()) {              // Don't process new tuples until the failed tuples have all been acked\n+            retryFailedTuples();\n+        } else {\n+            emitTuples(poll());\n+        }\n+    }\n+\n+    private ConsumerRecords<K, V> poll() {\n+        final ConsumerRecords<K, V> consumerRecords = kafkaConsumer.poll(kafkaSpoutConfig.getPollTimeoutMs());\n+        LOG.debug(\"Polled [{]} records from Kafka\", consumerRecords.count());\n+        return consumerRecords;\n+    }\n+\n+    private void emitTuples(ConsumerRecords<K, V> consumerRecords) {\n+        for (TopicPartition tp : consumerRecords.partitions()) {\n+            final Iterable<ConsumerRecord<K, V>> records = consumerRecords.records(tp.topic());     // TODO Decide if want to give flexibility to emmit/poll either per topic or per partition\n+            for (ConsumerRecord<K, V> record : records) {\n+                final Values tuple = tupleBuilder.buildTuple(record);","path":"external/storm-kafka-new-consumer-api/src/main/java/org/apache/storm/kafka/spout/KafkaSpout.java","position":150,"original_position":150,"commit_id":"92ae8a6aff4a1bd66c9153dcfd2c44d16de37637","original_commit_id":"92ae8a6aff4a1bd66c9153dcfd2c44d16de37637","user":{"login":"hmcl","id":10284328,"avatar_url":"https://avatars.githubusercontent.com/u/10284328?v=3","gravatar_id":"","url":"https://api.github.com/users/hmcl","html_url":"https://github.com/hmcl","followers_url":"https://api.github.com/users/hmcl/followers","following_url":"https://api.github.com/users/hmcl/following{/other_user}","gists_url":"https://api.github.com/users/hmcl/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcl/subscriptions","organizations_url":"https://api.github.com/users/hmcl/orgs","repos_url":"https://api.github.com/users/hmcl/repos","events_url":"https://api.github.com/users/hmcl/events{/privacy}","received_events_url":"https://api.github.com/users/hmcl/received_events","type":"User","site_admin":false},"body":"OK","created_at":"2016-02-23T01:24:33Z","updated_at":"2016-02-23T01:24:33Z","html_url":"https://github.com/apache/storm/pull/1131#discussion_r53723835","pull_request_url":"https://api.github.com/repos/apache/storm/pulls/1131","_links":{"self":{"href":"https://api.github.com/repos/apache/storm/pulls/comments/53723835"},"html":{"href":"https://github.com/apache/storm/pull/1131#discussion_r53723835"},"pull_request":{"href":"https://api.github.com/repos/apache/storm/pulls/1131"}}},"pull_request":{"url":"https://api.github.com/repos/apache/storm/pulls/1131","id":60106809,"html_url":"https://github.com/apache/storm/pull/1131","diff_url":"https://github.com/apache/storm/pull/1131.diff","patch_url":"https://github.com/apache/storm/pull/1131.patch","issue_url":"https://api.github.com/repos/apache/storm/issues/1131","number":1131,"state":"open","locked":false,"title":"STORM-822: Kafka Spout New Consumer API","user":{"login":"hmcl","id":10284328,"avatar_url":"https://avatars.githubusercontent.com/u/10284328?v=3","gravatar_id":"","url":"https://api.github.com/users/hmcl","html_url":"https://github.com/hmcl","followers_url":"https://api.github.com/users/hmcl/followers","following_url":"https://api.github.com/users/hmcl/following{/other_user}","gists_url":"https://api.github.com/users/hmcl/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcl/subscriptions","organizations_url":"https://api.github.com/users/hmcl/orgs","repos_url":"https://api.github.com/users/hmcl/repos","events_url":"https://api.github.com/users/hmcl/events{/privacy}","received_events_url":"https://api.github.com/users/hmcl/received_events","type":"User","site_admin":false},"body":"This patch is still under development and was uploaded at this moment for early testing. Please read README.\r\n\r\nThere may be a bug in the offsets management, because of diff o 1. I am looking into it. \r\nCurrently polling from an arbitrary offset is possible but it will come in the next patch, today or tomorrow\r\nI refactored the code a bit and left, maybe, some unnecessary locking. I am also looking into it.\r\n\r\n@connieyang @jianbzhou @tgravescs please let me know of any other requirements you may have and I will address them soon.\r\n\r\nThanks","created_at":"2016-02-21T22:18:37Z","updated_at":"2016-02-23T01:24:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"47f8145125d37f083c5a6ab02cf1ccb94d3e3c85","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/storm/pulls/1131/commits","review_comments_url":"https://api.github.com/repos/apache/storm/pulls/1131/comments","review_comment_url":"https://api.github.com/repos/apache/storm/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/storm/issues/1131/comments","statuses_url":"https://api.github.com/repos/apache/storm/statuses/92ae8a6aff4a1bd66c9153dcfd2c44d16de37637","head":{"label":"hmcl:STORM-822","ref":"STORM-822","sha":"92ae8a6aff4a1bd66c9153dcfd2c44d16de37637","user":{"login":"hmcl","id":10284328,"avatar_url":"https://avatars.githubusercontent.com/u/10284328?v=3","gravatar_id":"","url":"https://api.github.com/users/hmcl","html_url":"https://github.com/hmcl","followers_url":"https://api.github.com/users/hmcl/followers","following_url":"https://api.github.com/users/hmcl/following{/other_user}","gists_url":"https://api.github.com/users/hmcl/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcl/subscriptions","organizations_url":"https://api.github.com/users/hmcl/orgs","repos_url":"https://api.github.com/users/hmcl/repos","events_url":"https://api.github.com/users/hmcl/events{/privacy}","received_events_url":"https://api.github.com/users/hmcl/received_events","type":"User","site_admin":false},"repo":{"id":37676775,"name":"storm-apache","full_name":"hmcl/storm-apache","owner":{"login":"hmcl","id":10284328,"avatar_url":"https://avatars.githubusercontent.com/u/10284328?v=3","gravatar_id":"","url":"https://api.github.com/users/hmcl","html_url":"https://github.com/hmcl","followers_url":"https://api.github.com/users/hmcl/followers","following_url":"https://api.github.com/users/hmcl/following{/other_user}","gists_url":"https://api.github.com/users/hmcl/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcl/subscriptions","organizations_url":"https://api.github.com/users/hmcl/orgs","repos_url":"https://api.github.com/users/hmcl/repos","events_url":"https://api.github.com/users/hmcl/events{/privacy}","received_events_url":"https://api.github.com/users/hmcl/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hmcl/storm-apache","description":"Mirror of Apache Storm","fork":true,"url":"https://api.github.com/repos/hmcl/storm-apache","forks_url":"https://api.github.com/repos/hmcl/storm-apache/forks","keys_url":"https://api.github.com/repos/hmcl/storm-apache/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hmcl/storm-apache/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hmcl/storm-apache/teams","hooks_url":"https://api.github.com/repos/hmcl/storm-apache/hooks","issue_events_url":"https://api.github.com/repos/hmcl/storm-apache/issues/events{/number}","events_url":"https://api.github.com/repos/hmcl/storm-apache/events","assignees_url":"https://api.github.com/repos/hmcl/storm-apache/assignees{/user}","branches_url":"https://api.github.com/repos/hmcl/storm-apache/branches{/branch}","tags_url":"https://api.github.com/repos/hmcl/storm-apache/tags","blobs_url":"https://api.github.com/repos/hmcl/storm-apache/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hmcl/storm-apache/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hmcl/storm-apache/git/refs{/sha}","trees_url":"https://api.github.com/repos/hmcl/storm-apache/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hmcl/storm-apache/statuses/{sha}","languages_url":"https://api.github.com/repos/hmcl/storm-apache/languages","stargazers_url":"https://api.github.com/repos/hmcl/storm-apache/stargazers","contributors_url":"https://api.github.com/repos/hmcl/storm-apache/contributors","subscribers_url":"https://api.github.com/repos/hmcl/storm-apache/subscribers","subscription_url":"https://api.github.com/repos/hmcl/storm-apache/subscription","commits_url":"https://api.github.com/repos/hmcl/storm-apache/commits{/sha}","git_commits_url":"https://api.github.com/repos/hmcl/storm-apache/git/commits{/sha}","comments_url":"https://api.github.com/repos/hmcl/storm-apache/comments{/number}","issue_comment_url":"https://api.github.com/repos/hmcl/storm-apache/issues/comments{/number}","contents_url":"https://api.github.com/repos/hmcl/storm-apache/contents/{+path}","compare_url":"https://api.github.com/repos/hmcl/storm-apache/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hmcl/storm-apache/merges","archive_url":"https://api.github.com/repos/hmcl/storm-apache/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hmcl/storm-apache/downloads","issues_url":"https://api.github.com/repos/hmcl/storm-apache/issues{/number}","pulls_url":"https://api.github.com/repos/hmcl/storm-apache/pulls{/number}","milestones_url":"https://api.github.com/repos/hmcl/storm-apache/milestones{/number}","notifications_url":"https://api.github.com/repos/hmcl/storm-apache/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hmcl/storm-apache/labels{/name}","releases_url":"https://api.github.com/repos/hmcl/storm-apache/releases{/id}","deployments_url":"https://api.github.com/repos/hmcl/storm-apache/deployments","created_at":"2015-06-18T18:19:49Z","updated_at":"2016-02-12T22:37:39Z","pushed_at":"2016-02-21T22:11:42Z","git_url":"git://github.com/hmcl/storm-apache.git","ssh_url":"git@github.com:hmcl/storm-apache.git","clone_url":"https://github.com/hmcl/storm-apache.git","svn_url":"https://github.com/hmcl/storm-apache","homepage":null,"size":29997,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"4ca7522d1bf24fe030a0913607f54ce8d14cd825","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":14135470,"name":"storm","full_name":"apache/storm","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/storm","description":"Mirror of Apache Storm","fork":false,"url":"https://api.github.com/repos/apache/storm","forks_url":"https://api.github.com/repos/apache/storm/forks","keys_url":"https://api.github.com/repos/apache/storm/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/storm/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/storm/teams","hooks_url":"https://api.github.com/repos/apache/storm/hooks","issue_events_url":"https://api.github.com/repos/apache/storm/issues/events{/number}","events_url":"https://api.github.com/repos/apache/storm/events","assignees_url":"https://api.github.com/repos/apache/storm/assignees{/user}","branches_url":"https://api.github.com/repos/apache/storm/branches{/branch}","tags_url":"https://api.github.com/repos/apache/storm/tags","blobs_url":"https://api.github.com/repos/apache/storm/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/storm/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/storm/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/storm/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/storm/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/storm/languages","stargazers_url":"https://api.github.com/repos/apache/storm/stargazers","contributors_url":"https://api.github.com/repos/apache/storm/contributors","subscribers_url":"https://api.github.com/repos/apache/storm/subscribers","subscription_url":"https://api.github.com/repos/apache/storm/subscription","commits_url":"https://api.github.com/repos/apache/storm/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/storm/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/storm/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/storm/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/storm/contents/{+path}","compare_url":"https://api.github.com/repos/apache/storm/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/storm/merges","archive_url":"https://api.github.com/repos/apache/storm/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/storm/downloads","issues_url":"https://api.github.com/repos/apache/storm/issues{/number}","pulls_url":"https://api.github.com/repos/apache/storm/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/storm/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/storm/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/storm/labels{/name}","releases_url":"https://api.github.com/repos/apache/storm/releases{/id}","deployments_url":"https://api.github.com/repos/apache/storm/deployments","created_at":"2013-11-05T08:00:14Z","updated_at":"2016-02-22T22:11:09Z","pushed_at":"2016-02-22T23:03:23Z","git_url":"git://github.com/apache/storm.git","ssh_url":"git@github.com:apache/storm.git","clone_url":"https://github.com/apache/storm.git","svn_url":"https://github.com/apache/storm","homepage":null,"size":159860,"stargazers_count":2868,"watchers_count":2868,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":2146,"mirror_url":"git://git.apache.org/storm.git","open_issues_count":94,"forks":2146,"open_issues":94,"watchers":2868,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/storm/pulls/1131"},"html":{"href":"https://github.com/apache/storm/pull/1131"},"issue":{"href":"https://api.github.com/repos/apache/storm/issues/1131"},"comments":{"href":"https://api.github.com/repos/apache/storm/issues/1131/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/storm/pulls/1131/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/storm/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/storm/pulls/1131/commits"},"statuses":{"href":"https://api.github.com/repos/apache/storm/statuses/92ae8a6aff4a1bd66c9153dcfd2c44d16de37637"}}}}