Skip to content

Commit baede83

Browse files
authored
Add config topic-management.minPartitionNum (linkedin#73)
1 parent 90b178b commit baede83

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ different Kafka clusters that are connected by MirrorMaker to monitor their
4848
end-to-end latency.
4949

5050
- Kafka Monitor by default will automatically create the monitor topic based on
51-
the e.g. `topic-management.replicationFactor` and `topic-management.partitionsToBrokerRatio`
51+
the e.g. `topic-management.replicationFactor` and `topic-management.partitionsToBrokersRatio`
5252
specified in the config. replicationFactor is 1 by default and you probably
5353
want to change it to the same replication factor as used for your existing
5454
topics. You can disable auto topic creation by setting `produce.topic.topicCreationEnabled` to false.

src/main/java/com/linkedin/kmf/services/MultiClusterTopicManagementService.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@
5353
* This service periodically checks and rebalances the monitor topics across a pipeline of Kafka clusters so that
5454
* leadership of the partitions of the monitor topic in each cluster is distributed evenly across brokers in the cluster.
5555
*
56-
* It also makes sure that the number of partitions of the monitor topics is same across the monitored clusters.
56+
* More specifically, this service may do some or all of the following tasks depending on the config:
57+
*
58+
* - Create the monitor topic using the user-specified replication factor and partition number
59+
* - Increase partition number of the monitor topic if either partitionsToBrokersRatio or minPartitionNum is not satisfied
60+
* - Increase replication factor of the monitor topic if the user-specified replicationFactor is not satisfied
61+
* - Reassign partition across brokers to make sure each broker acts as preferred leader of at least one partition of the monitor topic
62+
* - Trigger preferred leader election to make sure each broker acts as leader of at least one partition of the monitor topic.
63+
* - Make sure the number of partitions of the monitor topic is same across all monitored custers.
64+
*
5765
*/
5866
public class MultiClusterTopicManagementService implements Service {
5967
private static final Logger LOG = LoggerFactory.getLogger(MultiClusterTopicManagementService.class);
@@ -134,6 +142,13 @@ public void run() {
134142
helper.maybeCreateTopic();
135143
}
136144

145+
/*
146+
* The partition number of the monitor topics should be the minimum partition number that satisifies the following conditions:
147+
* - partition number of the monitor topics across all monitored clusters should be the same
148+
* - partitionNum / brokerNum >= user-configured partitionsToBrokersRatio.
149+
* - partitionNum >= user-configured minPartitionNum
150+
*/
151+
137152
int minPartitionNum = 0;
138153
for (TopicManagementHelper helper : _topicManagementByCluster.values()) {
139154
minPartitionNum = Math.max(minPartitionNum, helper.minPartitionNum());
@@ -164,7 +179,8 @@ static class TopicManagementHelper {
164179
private final String _topic;
165180
private final String _zkConnect;
166181
private final int _replicationFactor;
167-
private final double _partitionsToBrokerRatio;
182+
private final double _minPartitionsToBrokersRatio;
183+
private final int _minPartitionNum;
168184
private final TopicFactory _topicFactory;
169185

170186
TopicManagementHelper(Map<String, Object> props) throws Exception {
@@ -173,7 +189,8 @@ static class TopicManagementHelper {
173189
_topic = config.getString(TopicManagementServiceConfig.TOPIC_CONFIG);
174190
_zkConnect = config.getString(TopicManagementServiceConfig.ZOOKEEPER_CONNECT_CONFIG);
175191
_replicationFactor = config.getInt(TopicManagementServiceConfig.TOPIC_REPLICATION_FACTOR_CONFIG);
176-
_partitionsToBrokerRatio = config.getDouble(TopicManagementServiceConfig.PARTITIONS_TO_BROKERS_RATIO_CONFIG);
192+
_minPartitionsToBrokersRatio = config.getDouble(TopicManagementServiceConfig.PARTITIONS_TO_BROKERS_RATIO_CONFIG);
193+
_minPartitionNum = config.getInt(TopicManagementServiceConfig.MIN_PARTITION_NUM_CONFIG);
177194
String topicFactoryClassName = config.getString(TopicManagementServiceConfig.TOPIC_FACTORY_CLASS_CONFIG);
178195
Map topicFactoryConfig = props.containsKey(TopicManagementServiceConfig.TOPIC_FACTORY_PROPS_CONFIG) ?
179196
(Map) props.get(TopicManagementServiceConfig.TOPIC_FACTORY_PROPS_CONFIG) : new HashMap();
@@ -182,13 +199,13 @@ static class TopicManagementHelper {
182199

183200
void maybeCreateTopic() throws Exception {
184201
if (_topicCreationEnabled) {
185-
_topicFactory.createTopicIfNotExist(_zkConnect, _topic, _replicationFactor, _partitionsToBrokerRatio, new Properties());
202+
_topicFactory.createTopicIfNotExist(_zkConnect, _topic, _replicationFactor, _minPartitionsToBrokersRatio, new Properties());
186203
}
187204
}
188205

189206
int minPartitionNum() {
190207
int brokerCount = Utils.getBrokerCount(_zkConnect);
191-
return (int) Math.ceil(_partitionsToBrokerRatio * brokerCount);
208+
return Math.max((int) Math.ceil(_minPartitionsToBrokersRatio * brokerCount), _minPartitionNum);
192209
}
193210

194211
void maybeAddPartitions(int minPartitionNum) {

src/main/java/com/linkedin/kmf/services/configs/TopicManagementServiceConfig.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ public class TopicManagementServiceConfig extends AbstractConfig {
3232
public static final String TOPIC_DOC = CommonServiceConfig.TOPIC_DOC;
3333

3434
public static final String PARTITIONS_TO_BROKERS_RATIO_CONFIG = "topic-management.partitionsToBrokersRatio";
35-
public static final String PARTITIONS_TO_BROKERS_RATIO_DOC = "New partitions are created when the actual ratio falls below this threshold."
36-
+ " partitionsToBrokerRatio will be the lower bound on the number of partitions per broker when a topic is"
37-
+ " created or when the partition is added.";
35+
public static final String PARTITIONS_TO_BROKERS_RATIO_DOC = "New partitions are added when the actual ratio falls below this threshold."
36+
+ " This config provides a loose lower bound on the partitionNum-to-brokerNum ratio when the monitor topic is created or when partition is added.";
37+
38+
public static final String MIN_PARTITION_NUM_CONFIG = "topic-management.minPartitionNum";
39+
public static final String MIN_PARTITION_NUM_DOC = "New partitions are added when the actual partition number falls below this threshold."
40+
+ " This config provides a loose lower bound on the partition number of the monitor topic when the topic is created or when partition is added.";
3841

3942
public static final String TOPIC_REPLICATION_FACTOR_CONFIG = "topic-management.replicationFactor";
4043
public static final String TOPIC_REPLICATION_FACTOR_DOC = "When a topic is created automatically this is the "
4144
+ "replication factor used.";
4245

4346
public static final String TOPIC_CREATION_ENABLED_CONFIG = "topic-management.topicCreationEnabled";
44-
public static final String TOPIC_CREATION_ENABLED_DOC = "When true this automatically creates the topic mentioned by \""
45-
+ CommonServiceConfig.TOPIC_CONFIG + "\" with replication factor \"" + TOPIC_REPLICATION_FACTOR_CONFIG + "and min ISR of max("
46-
+ TOPIC_REPLICATION_FACTOR_CONFIG + "-1, 1) with number of brokers * \"" + PARTITIONS_TO_BROKERS_RATIO_CONFIG + "\" partitions.";
47+
public static final String TOPIC_CREATION_ENABLED_DOC = String.format("When true this service automatically creates the topic named"
48+
+ " in the config with replication factor %s and min ISR as max(%s - 1, 1). The partition number is determined based on %s and %s",
49+
TOPIC_REPLICATION_FACTOR_CONFIG, TOPIC_REPLICATION_FACTOR_CONFIG, PARTITIONS_TO_BROKERS_RATIO_CONFIG, MIN_PARTITION_NUM_DOC);
4750

4851
public static final String TOPIC_FACTORY_CLASS_CONFIG = "topic-management.topicFactory.class.name";
4952
public static final String TOPIC_FACTORY_CLASS_DOC = "The name of the class used to create topics. This class must implement "
@@ -64,8 +67,14 @@ public class TopicManagementServiceConfig extends AbstractConfig {
6467
TOPIC_DOC)
6568
.define(PARTITIONS_TO_BROKERS_RATIO_CONFIG,
6669
ConfigDef.Type.DOUBLE,
67-
1.5,
68-
ConfigDef.Importance.LOW, PARTITIONS_TO_BROKERS_RATIO_DOC)
70+
1.0,
71+
ConfigDef.Importance.LOW,
72+
PARTITIONS_TO_BROKERS_RATIO_DOC)
73+
.define(MIN_PARTITION_NUM_CONFIG,
74+
ConfigDef.Type.INT,
75+
1,
76+
ConfigDef.Importance.LOW,
77+
MIN_PARTITION_NUM_DOC)
6978
.define(TOPIC_CREATION_ENABLED_CONFIG,
7079
ConfigDef.Type.BOOLEAN,
7180
true,

0 commit comments

Comments
 (0)