Class RolePartitioner
CoordinationCodec.TOPIC that holds every record of
one role.
The rule is Kafka's own key partitioning: murmur2 of the role name in
UTF-8, masked with Utils.toPositive, and then the remainder of the partition
count. The mask clears the sign bit. It is not an absolute value, and the two differ
for every negative hash. krabka-client-rs and krabka-streams-go write
the same topic, so they compute the same rule. A change here needs the same change in
the two ports.
Both writers of a role pin the partition with this rule. The pin is a correctness requirement and not a preference. Kafka's default partitioner hashes the record key. The registration key of a role and the lease key of the same role differ, because the registration key names a member and the lease key does not. A partitioner that reads the key puts the two kinds in two partitions, and the total order that the succession rules rank on is gone.
Example
int partition = RolePartitioner.partitionFor(Role.of("controller"), 16);
producer.send(new ProducerRecord<>(CoordinationCodec.TOPIC, partition, key, value));
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe partition count this module assumes forCoordinationCodec.TOPIC. -
Method Summary
Modifier and TypeMethodDescriptionstatic intpartitionFor(Role role, int partitions) Returns the partition that holds every record of a role.
-
Field Details
-
DEFAULT_PARTITIONS
public static final int DEFAULT_PARTITIONSThe partition count this module assumes forCoordinationCodec.TOPIC.Read the real count of the cluster with
partitionsForwhen an operator created the topic with another count. A wrong count sends the records of a role to the wrong partition, and a reader then finds no state.- See Also:
-
-
Method Details
-
partitionFor
Returns the partition that holds every record of a role.- Parameters:
role- the role to placepartitions- the partition count ofCoordinationCodec.TOPIC- Returns:
- the partition number, from zero up to one below the partition count
- Throws:
CoordinationException- if the partition count is below oneNullPointerException- if the role is null
-