Status
Current state: Under Discussion
Discussion thread: here
JIRA:
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
The admin client allows users to instruct the controller to attempt to elect the preferred replica as leader on a given set of topic partitions. In addition to preferred replica leader elections the controller also supports three other types of elections. One of those other election types is what we call unclean leader election. For the user to enable and trigger unclean leader election they need to either modify the topic configuration or the broker configuration. We believe that this is potentially a dangerous configuration that could lead to data loss. Specially if the user forgets to disable unclean leader election after it completes. Being able to trigger unclean leader election once for a given partition is a much safer mechanism.
In this proposal we modify the “PreferredLeaderElection” RPC to support unclean leader election in addition to preferred leader election. The new RPC definition also makes it possible to easily add new type of elections in the future.
Public Interfaces
Network protocol
{ "apiKey": 43, "type": "request", "name": "ElectLeadersRequest", "validVersions": "0-1", "fields": [ { "name": "ElectionType", "type": "int8", "versions": "1+", "about": "Type of elections to conduct for the partition. A value of '0' elects the preferred replica. A value of '1' elects the first live replica if there are no in-sync replica." }, { "name": "TopicPartitions", "type": "[]TopicPartitions", "versions": "0+", "nullableVersions": "0+", "about": "The topic partitions to elect leaders.", "fields": [ { "name": "Topic", "type": "string", "versions": "0+", "about": "The name of a topic." }, { "name": "PartitionId", "type": "[]int32", "versions": "0+", "about": "The partitions of this topic whose leader should be elected." } ] }, { "name": "TimeoutMs", "type": "int32", "versions": "0+", "default": "60000", "about": "The time in ms to wait for the election to complete." } ] }
AdminClient Abstract Class
A new method will be added to the AdminClient abstract class to support this new version of the RPC.
package org.apache.kafka.clients.admin; public abstract class AdminClient ... { ... /** * ... * @deprecated Since TBD. Use {@link #electLeaders}. */ @Deprecated public abstract ElectPreferredLeadersResult electPreferredLeaders(Collection<TopicPartition> partitions, ElectPreferredLeadersOptions options); /** * Attempt to elect a new leader for each of the topic partition in {@code partitions} using the {@code electionType} election. * The type of elections supported are document in the {@link ElectionType} type. * * If {@code partitions} is null, then attempt to perform the election for all of the topic partitions. * * @param electionType The type of election. * @param partitions The partitions for which to conduct elections. * @param options The options to use when electing the leaders. * @return The ElectLeadersResult. */ public abstract ElectLeadersResult electLeaders( ElectionType electionType, Collection<TopicPartition> partitions, ElectLeadersOptions options); }
ElectionType Enumeration
package org.apache.kafka.common; public enum ElectionType { PREFERRED((byte) 0), UNCLEAN((byte) 1); public final byte value; ElectionType(byte value) { this.value = value; } }
Admin Command
The command kafka-preferred-replica-election.{sh,bat} will be deprecated and the following command will be added.
$ bin/kafka-leader-election.sh --help This tool attempts to elect a new leader for a set of topic partitions. The type of elections supported are preferred replicas and unclean replica. Option Description ------ ----------- --admin.config <String: config file> Admin client config properties file to pass to the admin client when --bootstrap-server is given. --all-topic-partitions Perform election on all of the topic partitions. Not allowed if --topic, --partition or --path-to-json-file is specified. --bootstrap-server <String: host:port> A host name and port for the broker to connect to, in the form host:port. Multiple comma-separated URLs can be given. REQUIRED unless --zookeeper is given. --election-type <String: election> Type of election to attempt. Possible values are 0 (or "preferred") for preferred election or 1 (or "unclean") for unclean election. The default value is 0 (or "preferred"). --topic <String: topic> Name of topic for which to perform an election. REQUIRED if --partition is specified. Not allowed if --path-to-json-file or --all-topic-partitions is specified. --partition <Integer: partition id> Partition id for which to perform an election. REQUIRED if --topic is specified. Not allowed if --path-to-json-file or --all-topic-partitions is specified. --help Print usage information. --path-to-json-file <String: list of The JSON file with the list of partitions for which replica partitions for which leader election leader election needs to be should be done. Supported elections triggered> are 0 for preferred and 1 for unclean. If an election is not specified, preferred is the default. This is an example format. {"partitions": [{"topic": "foo", "partition": 1}, {"topic": "foobar", "partition": 2}] } Not allowed if --all-topic-partitions, --topic or --partition flags are specified.
Proposed Changes
In addition to the protocol and client changes enumerated above, the Controller will be extended to allow unclean leader election requests to come from the admin client. Currently the controller only supports preferred leader election from the admin client. Unclean leader election can only be enabled through either a topic configuration change or a broker configuration change.
When performing unclean leader election on a topic partition through the admin client the "unclean leader election" topic configuration and broker configuration will be ignored. In the code this will result is code similar to the one below:
uncleanElectionFromAdminClient || logConfigs(partition.topic).uncleanLeaderElectionEnable.booleanValue()
Compatibility, Deprecation, and Migration Plan
The kafka-preferred-replica-elections.{sh,bat} scripts will be deprecated.
Rejected Alternatives
Not applicable.