<commit_before>// Module included in the following assemblies:
//
// assembly-scaling-clusters.adoc

[id='con-partition-reassignment-{context}']

= Partition reassignment

The Topic Operator does not currently support reassigning replicas to different brokers, so it is necessary to connect directly to broker pods to reassign replicas to brokers.

Within a broker pod, the `kafka-reassign-partitions.sh` utility allows you to reassign partitions to different brokers.

It has three different modes:

`--generate`::
Takes a set of topics and brokers and generates a _reassignment JSON file_ which will result in the partitions of those topics being assigned to those brokers.
Because this operates on whole topics, it cannot be used when you just need to reassign some of the partitions of some topics.

`--execute`::
Takes a _reassignment JSON file_ and applies it to the partitions and brokers in the cluster.
Brokers that gain partitions as a result become followers of the partition leader.
For a given partition, once the new broker has caught up and joined the ISR (in-sync replicas) the old broker will stop being a follower and will delete its replica.

`--verify`::
Using the same _reassignment JSON file_ as the `--execute` step, `--verify` checks whether all of the partitions in the file have been moved to their intended brokers.
If the reassignment is complete, --verify also removes any xref:con-reassignment-throttles-{context}[throttles] that are in effect.
Unless removed, throttles will continue to affect the cluster even after the reassignment has finished.

It is only possible to have one reassignment running in a cluster at any given time, and it is not possible to cancel a running reassignment.
If you need to cancel a reassignment, wait for it to complete and then perform another reassignment to revert the effects of the first reassignment.
The `kafka-reassign-partitions.sh` will print the reassignment JSON for this reversion as part of its output.
Very large reassignments should be broken down into a number of smaller reassignments in case there is a need to stop in-progress reassignment.

== Reassignment JSON file

The _reassignment JSON file_ has a specific structure:

[source,subs=+quotes]
----
{
  "version": 1,
  "partitions": [
    _<PartitionObjects>_
  ]
}
----

Where _<PartitionObjects>_ is a comma-separated list of objects like:

[source,subs=+quotes]
----
{
  "topic": _<TopicName>_,
  "partition": _<Partition>_,
  "replicas": [ _<AssignedBrokerIds>_ ]
}
----

NOTE: Although Kafka also supports a `"log_dirs"` property this should not be used in Strimzi.

The following is an example reassignment JSON file that assigns topic `topic-a`, partition `4` to brokers `2`, `4` and `7`, and topic `topic-b` partition `2` to brokers `1`, `5` and `7`:

[source,json]
----
{
  "version": 1,
  "partitions": [
    {
      "topic": "topic-a",
      "partition": 4,
      "replicas": [2,4,7]
    },<commit_msg>Optimize the documentation to make it clearer (#4166)<commit_after>// Module included in the following assemblies:
//
// assembly-scaling-clusters.adoc

[id='con-partition-reassignment-{context}']

= Partition reassignment

The Topic Operator does not currently support reassigning replicas to different brokers, so it is necessary to connect directly to broker pods to reassign replicas to brokers.

Within a broker pod, the `kafka-reassign-partitions.sh` utility allows you to reassign partitions to different brokers.

It has three different modes:

`--generate`::
Takes a set of topics and brokers and generates a _reassignment JSON file_ which will result in the partitions of those topics being assigned to those brokers.
Because this operates on whole topics, it cannot be used when you just need to reassign some of the partitions of some topics.

`--execute`::
Takes a _reassignment JSON file_ and applies it to the partitions and brokers in the cluster.
Brokers that gain partitions as a result become followers of the partition leader.
For a given partition, once the new broker has caught up and joined the ISR (in-sync replicas) the old broker will stop being a follower and will delete its replica.

`--verify`::
Using the same _reassignment JSON file_ as the `--execute` step, `--verify` checks whether all of the partitions in the file have been moved to their intended brokers.
If the reassignment is complete, --verify also removes any xref:con-reassignment-throttles-{context}[throttles] that are in effect.
Unless removed, throttles will continue to affect the cluster even after the reassignment has finished.

It is only possible to have one reassignment running in a cluster at any given time, and it is not possible to cancel a running reassignment.
If you need to cancel a reassignment, wait for it to complete and then perform another reassignment to revert the effects of the first reassignment.
The `kafka-reassign-partitions.sh` will print the reassignment JSON for this reversion as part of its output.
Very large reassignments should be broken down into a number of smaller reassignments in case there is a need to stop in-progress reassignment.

== Reassignment JSON file

The _reassignment JSON file_ has a specific structure:

[source,subs=+quotes]
----
{
  "version": 1,
  "partitions": [
    _<PartitionObjects>_
  ]
}
----

Where _<PartitionObjects>_ is a comma-separated list of objects like:

[source,subs=+quotes]
----
{
  "topic": _<TopicName>_,
  "partition": _<Partition>_,
  "replicas": [ _<AssignedBrokerIds>_ ]
}
----

NOTE: Although Kafka also supports a `"log_dirs"` property this should not be used in Strimzi.

The following is an example reassignment JSON file that assigns partition `4` of topic `topic-a` to brokers `2`, `4` and `7`, and partition `2` of topic `topic-b` to brokers `1`, `5` and `7`:

[source,json]
----
{
  "version": 1,
  "partitions": [
    {
      "topic": "topic-a",
      "partition": 4,
      "replicas": [2,4,7]
    },
{'fraction_non_alphanumeric': 0.07939444911690496, 'fraction_numerical': 0.005382674516400336, 'mean_word_length': 4.261946902654867, 'pattern_counts': {'":': 20, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4823510', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1618, 'n_words': 817}