{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/20767","repository_url":"https://api.github.com/repos/apache/spark","labels_url":"https://api.github.com/repos/apache/spark/issues/20767/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/20767/comments","events_url":"https://api.github.com/repos/apache/spark/issues/20767/events","html_url":"https://github.com/apache/spark/pull/20767","id":303334939,"number":20767,"title":"[SPARK-23623] [SS] Avoid concurrent use of cached consumers in CachedKafkaConsumer","user":{"login":"tdas","id":663212,"avatar_url":"https://avatars1.githubusercontent.com/u/663212?v=4","gravatar_id":"","url":"https://api.github.com/users/tdas","html_url":"https://github.com/tdas","followers_url":"https://api.github.com/users/tdas/followers","following_url":"https://api.github.com/users/tdas/following{/other_user}","gists_url":"https://api.github.com/users/tdas/gists{/gist_id}","starred_url":"https://api.github.com/users/tdas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdas/subscriptions","organizations_url":"https://api.github.com/users/tdas/orgs","repos_url":"https://api.github.com/users/tdas/repos","events_url":"https://api.github.com/users/tdas/events{/privacy}","received_events_url":"https://api.github.com/users/tdas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2018-03-08T02:37:22Z","updated_at":"2018-03-09T22:37:37Z","closed_at":null,"author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/20767","html_url":"https://github.com/apache/spark/pull/20767","diff_url":"https://github.com/apache/spark/pull/20767.diff","patch_url":"https://github.com/apache/spark/pull/20767.patch"},"body":"## What changes were proposed in this pull request?\r\n\r\nCacheKafkaConsumer in the project `kafka-0-10-sql` is designed to maintain a pool of KafkaConsumers that can be reused. However, it was built with the assumption there will be only one task using trying to read the same Kafka TopicPartition at the same time. Hence, the cache was keyed by the TopicPartition a consumer is supposed to read. And any cases where this assumption may not be true, we have SparkPlan flag to disable the use of a cache. So it was up to the planner to correctly identify when it was not safe to use the cache and set the flag accordingly. \r\n\r\nFundamentally, this is the wrong way to approach the problem. It is HARD for a high-level planner to reason about the low-level execution model, whether there will be multiple tasks in the same query trying to read the same partition. Case in point, 2.3.0 introduced stream-stream joins, and you can build a streaming self-join query on Kafka. It's pretty non-trivial to figure out how this leads to two tasks reading the same partition twice, possibly concurrently. And due to the non-triviality, it is hard to figure this out in the planner and set the flag to avoid the cache / consumer pool. And this can inadvertently lead to ConcurrentModificationException ,or worse, silent reading of incorrect data.\r\n\r\nHere is a better way to design this. The planner shouldnt have to understand these low-level optimizations. Rather the consumer pool should be smart enough avoid concurrent use of a cached consumer. Currently, it tries to do so but incorrectly (the flag inuse is not checked when returning a cached consumer, see [this](https://github.com/apache/spark/blob/master/external/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/CachedKafkaConsumer.scala#L403)). If there is another request for the same partition as a currently in-use consumer, the pool should automatically return a fresh consumer that should be closed when the task is done. Then the planner does not have to have a flag to avoid reuses.\r\n\r\nThis PR is a step towards that goal. It does the following. \r\n- There are effectively two kinds of consumer that may be generated \r\n  - Cached consumer - this should be returned to the pool at task end\r\n  - Non-cached consumer - this should be closed at task end\r\n- A trait called KafkaConsumer is introduced to hide this difference from the users of the consumer so that the client code does not have to reason about whether to stop and release. They simply called `val consumer = KafkaConsumer.acquire` and then `consumer.release()`.\r\n- If there is request for a consumer that is in-use, then a new consumer is generated.\r\n- If there is a concurrent attempt of the same task, then a new consumer is generated, and the existing cached consumer is marked for close upon release. \r\n- In addition, I renamed the classes because CachedKafkaConsumer is a misnomer given that what it returns may or may not be cached. \r\n\r\nThis PR does not remove the planner flag to avoid reuse to make this patch safe enough for merging in branch-2.3. This can be done later in master-only.\r\n\r\n\r\n\r\n## How was this patch tested?\r\nA new stress test that verifies it is safe to concurrently get consumers for the same partition from the consumer pool."},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/371965412","html_url":"https://github.com/apache/spark/pull/20767#issuecomment-371965412","issue_url":"https://api.github.com/repos/apache/spark/issues/20767","id":371965412,"user":{"login":"AmplabJenkins","id":4031291,"avatar_url":"https://avatars3.githubusercontent.com/u/4031291?v=4","gravatar_id":"","url":"https://api.github.com/users/AmplabJenkins","html_url":"https://github.com/AmplabJenkins","followers_url":"https://api.github.com/users/AmplabJenkins/followers","following_url":"https://api.github.com/users/AmplabJenkins/following{/other_user}","gists_url":"https://api.github.com/users/AmplabJenkins/gists{/gist_id}","starred_url":"https://api.github.com/users/AmplabJenkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AmplabJenkins/subscriptions","organizations_url":"https://api.github.com/users/AmplabJenkins/orgs","repos_url":"https://api.github.com/users/AmplabJenkins/repos","events_url":"https://api.github.com/users/AmplabJenkins/events{/privacy}","received_events_url":"https://api.github.com/users/AmplabJenkins/received_events","type":"User","site_admin":false},"created_at":"2018-03-09T22:37:37Z","updated_at":"2018-03-09T22:37:37Z","author_association":"NONE","body":"Test PASSed.\nRefer to this link for build results (access rights to CI server needed): \nhttps://amplab.cs.berkeley.edu/jenkins//job/testing-k8s-prb-make-spark-distribution/1439/\nTest PASSed."}}
{"actor":{"display_login":"AmplabJenkins"}}