{"action":"created","comment":{"url":"https://api.github.com/repos/apache/kafka/pulls/comments/207673602","pull_request_review_id":143341066,"id":207673602,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwNzY3MzYwMg==","diff_hunk":"@@ -252,6 +288,111 @@ public void buildAndOptimizeTopology() {\n         }\n     }\n \n+    private void maybePerformOptimizations(final Properties props) {\n+\n+        if (props != null && props.getProperty(StreamsConfig.TOPOLOGY_OPTIMIZATION).equals(StreamsConfig.OPTIMIZE)) {\n+            LOG.debug(\"Optimizing the Kafka Streams graph for repartition nodes\");\n+            maybeOptimizeRepartitionOperations();\n+        }\n+    }\n+\n+    private void maybeOptimizeRepartitionOperations() {\n+        StreamsGraphNode optimizedSingleRepartition = null;\n+\n+        for (final Map.Entry<StreamsGraphNode, Set<OptimizableRepartitionNode>> streamsGraphNodeSetEntry : keyChangingOperationsToOptimizableRepartitionNodes.entrySet()) {\n+\n+            StreamsGraphNode keyChangingNode = streamsGraphNodeSetEntry.getKey();\n+\n+            if (streamsGraphNodeSetEntry.getValue().isEmpty()) {\n+                continue;\n+            }\n+\n+            final StreamSourceNode parentSourceNode = (StreamSourceNode) findParentNodeMatching(keyChangingNode, n -> n instanceof StreamSourceNode);\n+\n+            if (parentSourceNode == null) {\n+                throw new IllegalStateException(String.format(\"Can't find parent source node for %s \", keyChangingNode));\n+            }\n+\n+            optimizedSingleRepartition = createRepartitionNode(keyChangingNode.nodeName(),\n+                                                               parentSourceNode.keySerde(),\n+                                                               parentSourceNode.valueSerde());\n+\n+            // re-use parent id to make sure the single repartition graph node is evaluated before downstream nodes\n+            optimizedSingleRepartition.setId(parentSourceNode.id());\n+\n+            for (final OptimizableRepartitionNode repartitionNodeToBeReplaced : streamsGraphNodeSetEntry.getValue()) {\n+\n+                final StreamsGraphNode keyChangingNodeChild = findParentNodeMatching(repartitionNodeToBeReplaced, gn -> gn.parentNodes().contains(keyChangingNode));\n+\n+                LOG.debug(\"Found the node downstream of key-changer {}\", keyChangingNodeChild);\n+\n+                // need to add children of key-changing node as children of optimized repartition\n+                // in order to process records from re-partitioning\n+                optimizedSingleRepartition.addChildNode(keyChangingNodeChild);\n+\n+                LOG.debug(\"Removing {} from {}  children {}\", keyChangingNodeChild, keyChangingNode, keyChangingNode.children());\n+                // now remove children from key-changing node\n+                keyChangingNode.removeChild(keyChangingNodeChild);\n+\n+                // now need to get children of repartition node so we can remove repartition node\n+                final Collection<StreamsGraphNode> repartitionNodeToBeReplacedChildren = repartitionNodeToBeReplaced.children();\n+\n+                final StreamsGraphNode parentOfRepartitionNodeToBeReplaced = findParentNodeMatching(repartitionNodeToBeReplaced, n -> repartitionNodeToBeReplaced.parentNodes().contains(n));","path":"streams/src/main/java/org/apache/kafka/streams/kstream/internals/InternalStreamsBuilder.java","position":147,"original_position":147,"commit_id":"c34094fa1c4ec95f41ab72cbfe475f44e49ae1b9","original_commit_id":"c34094fa1c4ec95f41ab72cbfe475f44e49ae1b9","user":{"login":"vvcephei","id":832787,"node_id":"MDQ6VXNlcjgzMjc4Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/832787?v=4","gravatar_id":"","url":"https://api.github.com/users/vvcephei","html_url":"https://github.com/vvcephei","followers_url":"https://api.github.com/users/vvcephei/followers","following_url":"https://api.github.com/users/vvcephei/following{/other_user}","gists_url":"https://api.github.com/users/vvcephei/gists{/gist_id}","starred_url":"https://api.github.com/users/vvcephei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vvcephei/subscriptions","organizations_url":"https://api.github.com/users/vvcephei/orgs","repos_url":"https://api.github.com/users/vvcephei/repos","events_url":"https://api.github.com/users/vvcephei/events{/privacy}","received_events_url":"https://api.github.com/users/vvcephei/received_events","type":"User","site_admin":false},"body":"I think this says, \"we are looking for a parent node that is among my parent nodes\".\r\n\r\nIt seems like we can generalize this logic by not assuming there's a single parent node at all.","created_at":"2018-08-03T21:36:08Z","updated_at":"2018-08-03T21:51:38Z","html_url":"https://github.com/apache/kafka/pull/5451#discussion_r207673602","pull_request_url":"https://api.github.com/repos/apache/kafka/pulls/5451","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments/207673602"},"html":{"href":"https://github.com/apache/kafka/pull/5451#discussion_r207673602"},"pull_request":{"href":"https://api.github.com/repos/apache/kafka/pulls/5451"}}},"pull_request":{"url":"https://api.github.com/repos/apache/kafka/pulls/5451","id":205848267,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA1ODQ4MjY3","html_url":"https://github.com/apache/kafka/pull/5451","diff_url":"https://github.com/apache/kafka/pull/5451.diff","patch_url":"https://github.com/apache/kafka/pull/5451.patch","issue_url":"https://api.github.com/repos/apache/kafka/issues/5451","number":5451,"state":"open","locked":false,"title":"KAFKA-6761: Reduce streams footprint part IV add optimization","user":{"login":"bbejeck","id":199238,"node_id":"MDQ6VXNlcjE5OTIzOA==","avatar_url":"https://avatars3.githubusercontent.com/u/199238?v=4","gravatar_id":"","url":"https://api.github.com/users/bbejeck","html_url":"https://github.com/bbejeck","followers_url":"https://api.github.com/users/bbejeck/followers","following_url":"https://api.github.com/users/bbejeck/following{/other_user}","gists_url":"https://api.github.com/users/bbejeck/gists{/gist_id}","starred_url":"https://api.github.com/users/bbejeck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbejeck/subscriptions","organizations_url":"https://api.github.com/users/bbejeck/orgs","repos_url":"https://api.github.com/users/bbejeck/repos","events_url":"https://api.github.com/users/bbejeck/events{/privacy}","received_events_url":"https://api.github.com/users/bbejeck/received_events","type":"User","site_admin":false},"body":"This PR adds the optimization of eliminating multiple repartition topics when the `KStream` resulting from a key-changing operation executes other methods using the new key and reduces the repartition topics to one.\r\n\r\nNote that this PR leaves in place the optimization for re-using a source topic as a changelog topic for source `KTable` instances.  I'll have another follow-up PR to move the source topic optimization to a method within `InternalStreamsBuilder` so it can be performed in the same area of the code.\r\n\r\nAdditionally, the current value of `StreamsConfig.OPTIMIZE` is `all` and we'll need to have another KIP to change the value to `2.1`.   \r\n\r\nAn integration test `RepartitionOptimizingIntegrationTest` which asserts the same results for an optimized topology with one repartition topic as the un-optimized version with four repartition topics.\r\nMore tests will be added, but I wanted to get reviews on the approach now.\r\n\r\n### Committer Checklist (excluded from commit message)\r\n- [ ] Verify design and implementation \r\n- [ ] Verify test coverage and CI build status\r\n- [ ] Verify documentation (including upgrade notes)\r\n","created_at":"2018-08-02T20:14:01Z","updated_at":"2018-08-03T21:51:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4c0bdc34e839a33aacedc874a626184f890bc66b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/kafka/pulls/5451/commits","review_comments_url":"https://api.github.com/repos/apache/kafka/pulls/5451/comments","review_comment_url":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/kafka/issues/5451/comments","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/c34094fa1c4ec95f41ab72cbfe475f44e49ae1b9","head":{"label":"bbejeck:KAFKA-6761_reduce_streams_footprint_part_IV_add_optimization","ref":"KAFKA-6761_reduce_streams_footprint_part_IV_add_optimization","sha":"c34094fa1c4ec95f41ab72cbfe475f44e49ae1b9","user":{"login":"bbejeck","id":199238,"node_id":"MDQ6VXNlcjE5OTIzOA==","avatar_url":"https://avatars3.githubusercontent.com/u/199238?v=4","gravatar_id":"","url":"https://api.github.com/users/bbejeck","html_url":"https://github.com/bbejeck","followers_url":"https://api.github.com/users/bbejeck/followers","following_url":"https://api.github.com/users/bbejeck/following{/other_user}","gists_url":"https://api.github.com/users/bbejeck/gists{/gist_id}","starred_url":"https://api.github.com/users/bbejeck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbejeck/subscriptions","organizations_url":"https://api.github.com/users/bbejeck/orgs","repos_url":"https://api.github.com/users/bbejeck/repos","events_url":"https://api.github.com/users/bbejeck/events{/privacy}","received_events_url":"https://api.github.com/users/bbejeck/received_events","type":"User","site_admin":false},"repo":{"id":42054236,"node_id":"MDEwOlJlcG9zaXRvcnk0MjA1NDIzNg==","name":"kafka","full_name":"bbejeck/kafka","owner":{"login":"bbejeck","id":199238,"node_id":"MDQ6VXNlcjE5OTIzOA==","avatar_url":"https://avatars3.githubusercontent.com/u/199238?v=4","gravatar_id":"","url":"https://api.github.com/users/bbejeck","html_url":"https://github.com/bbejeck","followers_url":"https://api.github.com/users/bbejeck/followers","following_url":"https://api.github.com/users/bbejeck/following{/other_user}","gists_url":"https://api.github.com/users/bbejeck/gists{/gist_id}","starred_url":"https://api.github.com/users/bbejeck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbejeck/subscriptions","organizations_url":"https://api.github.com/users/bbejeck/orgs","repos_url":"https://api.github.com/users/bbejeck/repos","events_url":"https://api.github.com/users/bbejeck/events{/privacy}","received_events_url":"https://api.github.com/users/bbejeck/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bbejeck/kafka","description":"Mirror of Apache Kafka","fork":true,"url":"https://api.github.com/repos/bbejeck/kafka","forks_url":"https://api.github.com/repos/bbejeck/kafka/forks","keys_url":"https://api.github.com/repos/bbejeck/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bbejeck/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bbejeck/kafka/teams","hooks_url":"https://api.github.com/repos/bbejeck/kafka/hooks","issue_events_url":"https://api.github.com/repos/bbejeck/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/bbejeck/kafka/events","assignees_url":"https://api.github.com/repos/bbejeck/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/bbejeck/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/bbejeck/kafka/tags","blobs_url":"https://api.github.com/repos/bbejeck/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bbejeck/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bbejeck/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/bbejeck/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bbejeck/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/bbejeck/kafka/languages","stargazers_url":"https://api.github.com/repos/bbejeck/kafka/stargazers","contributors_url":"https://api.github.com/repos/bbejeck/kafka/contributors","subscribers_url":"https://api.github.com/repos/bbejeck/kafka/subscribers","subscription_url":"https://api.github.com/repos/bbejeck/kafka/subscription","commits_url":"https://api.github.com/repos/bbejeck/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/bbejeck/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/bbejeck/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/bbejeck/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/bbejeck/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/bbejeck/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bbejeck/kafka/merges","archive_url":"https://api.github.com/repos/bbejeck/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bbejeck/kafka/downloads","issues_url":"https://api.github.com/repos/bbejeck/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/bbejeck/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/bbejeck/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/bbejeck/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bbejeck/kafka/labels{/name}","releases_url":"https://api.github.com/repos/bbejeck/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/bbejeck/kafka/deployments","created_at":"2015-09-07T13:23:16Z","updated_at":"2018-08-01T23:37:45Z","pushed_at":"2018-08-02T21:58:51Z","git_url":"git://github.com/bbejeck/kafka.git","ssh_url":"git@github.com:bbejeck/kafka.git","clone_url":"https://github.com/bbejeck/kafka.git","svn_url":"https://github.com/bbejeck/kafka","homepage":null,"size":69420,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"trunk"}},"base":{"label":"apache:trunk","ref":"trunk","sha":"596c6c0c0b27c13f2017c770ea37cd39e27e5dcf","user":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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":2211243,"node_id":"MDEwOlJlcG9zaXRvcnkyMjExMjQz","name":"kafka","full_name":"apache/kafka","owner":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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/kafka","description":"Mirror of Apache Kafka","fork":false,"url":"https://api.github.com/repos/apache/kafka","forks_url":"https://api.github.com/repos/apache/kafka/forks","keys_url":"https://api.github.com/repos/apache/kafka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/kafka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/kafka/teams","hooks_url":"https://api.github.com/repos/apache/kafka/hooks","issue_events_url":"https://api.github.com/repos/apache/kafka/issues/events{/number}","events_url":"https://api.github.com/repos/apache/kafka/events","assignees_url":"https://api.github.com/repos/apache/kafka/assignees{/user}","branches_url":"https://api.github.com/repos/apache/kafka/branches{/branch}","tags_url":"https://api.github.com/repos/apache/kafka/tags","blobs_url":"https://api.github.com/repos/apache/kafka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/kafka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/kafka/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/kafka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/kafka/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/kafka/languages","stargazers_url":"https://api.github.com/repos/apache/kafka/stargazers","contributors_url":"https://api.github.com/repos/apache/kafka/contributors","subscribers_url":"https://api.github.com/repos/apache/kafka/subscribers","subscription_url":"https://api.github.com/repos/apache/kafka/subscription","commits_url":"https://api.github.com/repos/apache/kafka/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/kafka/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/kafka/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/kafka/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/kafka/contents/{+path}","compare_url":"https://api.github.com/repos/apache/kafka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/kafka/merges","archive_url":"https://api.github.com/repos/apache/kafka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/kafka/downloads","issues_url":"https://api.github.com/repos/apache/kafka/issues{/number}","pulls_url":"https://api.github.com/repos/apache/kafka/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/kafka/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/kafka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/kafka/labels{/name}","releases_url":"https://api.github.com/repos/apache/kafka/releases{/id}","deployments_url":"https://api.github.com/repos/apache/kafka/deployments","created_at":"2011-08-15T18:06:16Z","updated_at":"2018-08-03T20:47:31Z","pushed_at":"2018-08-03T21:19:20Z","git_url":"git://github.com/apache/kafka.git","ssh_url":"git@github.com:apache/kafka.git","clone_url":"https://github.com/apache/kafka.git","svn_url":"https://github.com/apache/kafka","homepage":null,"size":71568,"stargazers_count":9164,"watchers_count":9164,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5233,"mirror_url":null,"archived":false,"open_issues_count":483,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":5233,"open_issues":483,"watchers":9164,"default_branch":"trunk"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/kafka/pulls/5451"},"html":{"href":"https://github.com/apache/kafka/pull/5451"},"issue":{"href":"https://api.github.com/repos/apache/kafka/issues/5451"},"comments":{"href":"https://api.github.com/repos/apache/kafka/issues/5451/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/kafka/pulls/5451/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/kafka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/kafka/pulls/5451/commits"},"statuses":{"href":"https://api.github.com/repos/apache/kafka/statuses/c34094fa1c4ec95f41ab72cbfe475f44e49ae1b9"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"vvcephei"}}