Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from kafka.tools.assigner.actions import ActionModule | ||
from kafka.tools.exceptions import NotEnoughReplicasException | ||
|
||
|
||
class ActionDemote(ActionModule): | ||
name = "demote" | ||
helpstr = "Force a broker to relinquish leadership wherever it is safe to do so. This is done by reordering " \ | ||
"replica lists and then performing a PLE." | ||
|
||
def __init__(self, args, cluster): | ||
super(ActionDemote, self).__init__(args, cluster) | ||
|
||
@classmethod | ||
def _add_args(cls, parser): | ||
parser.add_argument( | ||
'-b', '--brokers', type=int, help='List of broker ids to demote', required=True, nargs='*') | ||
parser.add_argument('-t', '--topics', required=False, nargs='*', | ||
help='Optional list of topics to use. If not specified, leadership of all topics lead by the brokers to demote will be changed.') | ||
|
||
def process_cluster(self): | ||
brokers_to_demote = set(self.args.brokers) | ||
topics_to_consider = self.args.topics and set(self.args.topics) or None | ||
|
||
for partition in self.cluster.partitions(self.args.exclude_topics): | ||
if topics_to_consider and partition.topic.name not in topics_to_consider: | ||
continue | ||
|
||
if all(b.id in brokers_to_demote for b in partition.replicas): | ||
msg = 'Topic {0} partition {1} only has replicas in the list of brokers to be demoted: {2}'.format( | ||
partition.topic.name, | ||
str(partition.num), | ||
', '.join(str(b.id) for b in partition.replicas)) | ||
raise NotEnoughReplicasException(msg) | ||
|
||
while partition.replicas[0].id in brokers_to_demote: | ||
broker = partition.replicas[0] | ||
partition.remove_replica(broker) | ||
partition.add_replica(broker) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import unittest | ||
import argparse | ||
|
||
from .fixtures import set_up_cluster_4broker, set_up_subparser | ||
|
||
from kafka.tools.exceptions import NotEnoughReplicasException | ||
from kafka.tools.assigner.actions.demote import ActionDemote | ||
|
||
|
||
class ActionDemoteTests(unittest.TestCase): | ||
def setUp(self): | ||
self.cluster = set_up_cluster_4broker() | ||
(self.parser, self.subparsers) = set_up_subparser() | ||
self.args = argparse.Namespace(exclude_topics=[]) | ||
|
||
def test_create_class(self): | ||
self.args.topics = ['testTopic1'] | ||
self.args.brokers = [1] | ||
action = ActionDemote(self.args, self.cluster) | ||
self.assertTrue(isinstance(action, ActionDemote)) | ||
|
||
def test_configure_args(self): | ||
ActionDemote.configure_args(self.subparsers) | ||
parsed_args = self.parser.parse_args(['demote', '-t', 'testTopic1', '-b', '0']) | ||
self.assertEqual(parsed_args.action, 'demote') | ||
self.assertEqual(parsed_args.brokers, [0]) | ||
self.assertEqual(parsed_args.topics, ['testTopic1']) | ||
|
||
def test_demote_broker(self): | ||
self.args.topics = None | ||
self.args.brokers = [1] | ||
action = ActionDemote(self.args, self.cluster) | ||
action.process_cluster() | ||
|
||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[0].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[1].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[2].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[3].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic2'].partitions[0].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic2'].partitions[1].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic2'].partitions[2].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic2'].partitions[3].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic3'].partitions[0].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic3'].partitions[1].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic3'].partitions[2].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic3'].partitions[3].replicas[0].id, 1) | ||
|
||
def test_impossible_demotion(self): | ||
self.args.topics = None | ||
self.args.brokers = [1, 4] | ||
action = ActionDemote(self.args, self.cluster) | ||
self.assertRaises(NotEnoughReplicasException, action.process_cluster) | ||
|
||
def test_limit_topics(self): | ||
self.args.topics = ['testTopic1'] | ||
self.args.brokers = [1] | ||
action = ActionDemote(self.args, self.cluster) | ||
action.process_cluster() | ||
|
||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[0].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[1].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[2].replicas[0].id, 1) | ||
self.assertNotEqual(self.cluster.topics['testTopic1'].partitions[3].replicas[0].id, 1) | ||
|
||
# Didn't touch this replica | ||
self.assertEqual(self.cluster.topics['testTopic3'].partitions[2].replicas[0].id, 1) |