[ 
https://issues.apache.org/jira/browse/KAFKA-21612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150826#comment-18150826
 ] 

Lucas Brutschy commented on KAFKA-21612:
----------------------------------------

Thanks for the detailed report, [~chickenchickenlove]. I reproduced this on 
both paths.

The cause is in the partition count calculation for repartition topics. For a 
topic written by a subtopology, it takes the maximum over the inputs that 
happen to be known when that subtopology is visited, and skips repartition 
inputs whose count is not decided yet. The count is then fixed and never 
revisited. So {{downstream}} gets 1 when the subtopology writing it is visited 
before the one writing {{{}upstream{}}}. Both the classic assignor and the 
streams group coordinator have this logic. The result depends on visit order: 
the order of subtopology IDs for the streams protocol, and the iteration order 
of a {{HashSet}} for classic. That explains the difference you saw when 
migrating between the two protocols.

The fix itself is small: decide a topic's count only once all its inputs are 
resolved. The difficulty is compatibility. Existing applications may already 
have a {{downstream}} topic with 1 partition, and with a plain fix they would 
fail with {{expected 3, found 1}} on the next rebalance, or on the next 
coordinator failover for the streams protocol. The topic cannot be 
repartitioned in place either, because the downstream subtopology is typically 
stateful.

My proposal is to keep the existing topic in that case. When a repartition 
topic already exists with a count that an old version could have produced from 
a subset of its inputs, that count is kept and is also used for repartition 
topics further downstream. Concretely, it is kept only if all three of these 
hold:
 * it is smaller than the new maximum,
 * it is at least as large as every external input, because those were always 
known,
 * it equals the partition count of one of the inputs.

Any other mismatch, for example a user growing an input topic, still fails as 
before. New topics get the correct maximum. This needs no config or API change, 
and it also makes behaviour consistent when migrating between protocols and 
during rolling upgrades.

Does anyone see a case where an existing topic with a smaller count should be 
rejected?

Do you want to work on this problem? I have a draft PR now

> Streams group coordinator undercounts repartition topic partitions when it 
> visits a downstream subtopology before its upstream dependency
> -----------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: KAFKA-21612
>                 URL: https://issues.apache.org/jira/browse/KAFKA-21612
>             Project: Kafka
>          Issue Type: Bug
>          Components: group-coordinator, streams
>            Reporter: sanghyeok An
>            Assignee: sanghyeok An
>            Priority: Major
>
> *Description*
> A Kafka Streams application using {{group.protocol=streams}} reads from two 
> input topics, {{high}} with 3 partitions and {{low}} with 1 partition. It 
> repartitions {{{}high{}}}, merges the result with {{{}low{}}}, and 
> repartitions the merged stream. Both {{repartition()}} calls leave the 
> partition count unspecified.
>  
> {code:java}
> KStream<String, String> low = builder.stream("low");
> KStream<String, String> upstream = builder.<String, String>stream("high")
>     .repartition(Repartitioned.as("upstream"));
> low.merge(upstream)
>     .repartition(Repartitioned.as("downstream"))
>     .to("output");
> {code}
>  
> The expected partition count for {{upstream}} is 3 because its only input is 
> {{{}high{}}}. The expected count for {{downstream}} is also 3, the maximum of 
> its two inputs. However, the group coordinator requests {*}3 partitions for 
> {{upstream}} and only 1 for {{downstream}}{*}.
> *Both classic protocol and streams protocol have same problem.*
>  
> *Cause and impact*
> With {{low}} registered first in this topology, the subtopology that writes 
> to {{downstream}} gets ID {{{}0{}}}, and the one that writes to {{upstream}} 
> gets ID {{{}1{}}}. When the coordinator visits subtopology {{0}} first, it 
> knows that {{low}} has 1 partition but has yet to determine the partition 
> count for {{{}upstream{}}}.
> The coordinator skips the unresolved input and records {{{}downstream=1{}}}. 
> When it later determines that {{{}upstream=3{}}}, it does not revisit 
> {{downstream}} because that topic already has a recorded count. As a result, 
> the coordinator can request a new {{downstream}} topic with only 1 partition. 
> If the topic already exists with the expected 3 partitions, the coordinator 
> can reject it with {{{}expected 1, found 3{}}}.
>  
> *Subsequent member joins and coordinator failover*
> The partition count is determined at the group level. Once the group has a 
> {{{}ConfiguredTopology{}}}, the coordinator reuses it when another member 
> joins with a matching topology and the relevant topic metadata is unchanged. 
> A subsequent join therefore does not change {{downstream}} from 1 to 3.
> On failover, the new coordinator loses the in-memory {{ConfiguredTopology}} 
> but restores the group topology from {{{}__consumer_offsets{}}}. If the first 
> member to join after failover supplies a matching topology, the coordinator 
> recalculates the partition count from the restored group topology. With the 
> same calculation logic and topic metadata, this example resolves to 
> {{downstream=1}} again. If the topic has yet to be created, the coordinator 
> requests 1 partition. If it already exists with 1 partition, the coordinator 
> accepts it.
>  
> *How to reproduce*
> {code:java}
> // Start broker and execute this commands in your terminal
> $ bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic high 
>   --partitions 3 --replication-factor 1     11:34:37
> $ bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic low  
>   --partitions 1 --replication-factor 1
> $ bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic 
> output --partitions 1 --replication-factor 1
> // Run this simple stream application
> KStream<String, String> low = builder.stream("low");
> KStream<String, String> upstream = builder.<String, String>stream("high")
>     .repartition(Repartitioned.as("upstream"));
> low.merge(upstream)
>     .repartition(Repartitioned.as("downstream"))
>     .to("output");
> // execute this command in your terminal and see the result
> $ kcat -b localhost:9092 -L | grep "repartition"
>   topic "ASH-HELLO-upstream-repartition" with 3 partitions:
>   topic "ASH-HELLO-downstream-repartition" with 1 partitions:{code}
> *ASH-HELLO-downstream-repartition* should be 3 partitions. (but 1 partition)



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to