Skip to content

Commit 5498eed

Browse files
authored
MINOR: Move populateSynonyms from KafkaConfig to AbstractKafkaConfig (#21381)
Move `populateSynonyms` from `KafkaConfig` to `AbstractKafkaConfig` and migrate the related tests. Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
1 parent b3d77f9 commit 5498eed

File tree

4 files changed

+73
-42
lines changed

4 files changed

+73
-42
lines changed

core/src/main/scala/kafka/server/KafkaConfig.scala

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,6 @@ object KafkaConfig {
128128
}
129129
if (maybeSensitive) Password.HIDDEN else value
130130
}
131-
132-
/**
133-
* Copy a configuration map, populating some keys that we want to treat as synonyms.
134-
*/
135-
def populateSynonyms(input: util.Map[_, _]): util.Map[Any, Any] = {
136-
val output = new util.HashMap[Any, Any](input)
137-
val brokerId = output.get(ServerConfigs.BROKER_ID_CONFIG)
138-
val nodeId = output.get(KRaftConfigs.NODE_ID_CONFIG)
139-
if (brokerId == null && nodeId != null) {
140-
output.put(ServerConfigs.BROKER_ID_CONFIG, nodeId)
141-
} else if (brokerId != null && nodeId == null) {
142-
output.put(KRaftConfigs.NODE_ID_CONFIG, brokerId)
143-
}
144-
output
145-
}
146131
}
147132

148133
/**
@@ -154,8 +139,8 @@ object KafkaConfig {
154139
class KafkaConfig private(doLog: Boolean, val props: util.Map[_, _])
155140
extends AbstractKafkaConfig(KafkaConfig.configDef, props, Utils.castToStringObjectMap(props), doLog) with Logging {
156141

157-
def this(props: java.util.Map[_, _]) = this(true, KafkaConfig.populateSynonyms(props))
158-
def this(props: java.util.Map[_, _], doLog: Boolean) = this(doLog, KafkaConfig.populateSynonyms(props))
142+
def this(props: java.util.Map[_, _]) = this(true, AbstractKafkaConfig.populateSynonyms(props))
143+
def this(props: java.util.Map[_, _], doLog: Boolean) = this(doLog, AbstractKafkaConfig.populateSynonyms(props))
159144

160145
// Cache the current config to avoid acquiring read lock to access from dynamicConfig
161146
@volatile private var currentConfig = this

core/src/test/scala/unit/kafka/server/KafkaConfigTest.scala

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,31 +1574,6 @@ class KafkaConfigTest {
15741574
assertEquals(util.List.of(dataDir1, dataDir2), config.logDirs)
15751575
}
15761576

1577-
@Test
1578-
def testPopulateSynonymsOnEmptyMap(): Unit = {
1579-
assertEquals(Collections.emptyMap(), KafkaConfig.populateSynonyms(Collections.emptyMap()))
1580-
}
1581-
1582-
@Test
1583-
def testPopulateSynonymsOnMapWithoutNodeId(): Unit = {
1584-
val input = new util.HashMap[String, String]()
1585-
input.put(ServerConfigs.BROKER_ID_CONFIG, "4")
1586-
val expectedOutput = new util.HashMap[String, String]()
1587-
expectedOutput.put(ServerConfigs.BROKER_ID_CONFIG, "4")
1588-
expectedOutput.put(KRaftConfigs.NODE_ID_CONFIG, "4")
1589-
assertEquals(expectedOutput, KafkaConfig.populateSynonyms(input))
1590-
}
1591-
1592-
@Test
1593-
def testPopulateSynonymsOnMapWithoutBrokerId(): Unit = {
1594-
val input = new util.HashMap[String, String]()
1595-
input.put(KRaftConfigs.NODE_ID_CONFIG, "4")
1596-
val expectedOutput = new util.HashMap[String, String]()
1597-
expectedOutput.put(ServerConfigs.BROKER_ID_CONFIG, "4")
1598-
expectedOutput.put(KRaftConfigs.NODE_ID_CONFIG, "4")
1599-
assertEquals(expectedOutput, KafkaConfig.populateSynonyms(input))
1600-
}
1601-
16021577
@Test
16031578
def testNodeIdMustNotBeDifferentThanBrokerId(): Unit = {
16041579
val props = new Properties()

server/src/main/java/org/apache/kafka/server/config/AbstractKafkaConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import org.apache.commons.validator.routines.InetAddressValidator;
4646

47+
import java.util.HashMap;
4748
import java.util.List;
4849
import java.util.Locale;
4950
import java.util.Map;
@@ -186,6 +187,21 @@ public static Map<String, String> getMap(String propName, String propValue) {
186187
}
187188
}
188189

190+
/**
191+
* Copy a configuration map, populating some keys that we want to treat as synonyms.
192+
*/
193+
public static Map<Object, Object> populateSynonyms(Map<?, ?> input) {
194+
Map<Object, Object> output = new HashMap<>(input);
195+
Object brokerId = output.get(ServerConfigs.BROKER_ID_CONFIG);
196+
Object nodeId = output.get(KRaftConfigs.NODE_ID_CONFIG);
197+
if (brokerId == null && nodeId != null) {
198+
output.put(ServerConfigs.BROKER_ID_CONFIG, nodeId);
199+
} else if (brokerId != null && nodeId == null) {
200+
output.put(KRaftConfigs.NODE_ID_CONFIG, brokerId);
201+
}
202+
return output;
203+
}
204+
189205
public static List<Endpoint> listenerListToEndPoints(List<String> listeners, Map<ListenerName, SecurityProtocol> securityProtocolMap) {
190206
return listenerListToEndPoints(listeners, securityProtocolMap, true);
191207
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.kafka.server.config;
18+
19+
import org.apache.kafka.raft.KRaftConfigs;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
29+
public class AbstractKafkaConfigTest {
30+
31+
@Test
32+
public void testPopulateSynonymsOnEmptyMap() {
33+
assertEquals(Collections.emptyMap(), AbstractKafkaConfig.populateSynonyms(Collections.emptyMap()));
34+
}
35+
36+
@Test
37+
public void testPopulateSynonymsOnMapWithoutNodeId() {
38+
Map<String, String> input = new HashMap<>();
39+
input.put(ServerConfigs.BROKER_ID_CONFIG, "4");
40+
Map<String, String> expectedOutput = new HashMap<>();
41+
expectedOutput.put(ServerConfigs.BROKER_ID_CONFIG, "4");
42+
expectedOutput.put(KRaftConfigs.NODE_ID_CONFIG, "4");
43+
assertEquals(expectedOutput, AbstractKafkaConfig.populateSynonyms(input));
44+
}
45+
46+
@Test
47+
public void testPopulateSynonymsOnMapWithoutBrokerId() {
48+
Map<String, String> input = new HashMap<>();
49+
input.put(KRaftConfigs.NODE_ID_CONFIG, "4");
50+
Map<String, String> expectedOutput = new HashMap<>();
51+
expectedOutput.put(ServerConfigs.BROKER_ID_CONFIG, "4");
52+
expectedOutput.put(KRaftConfigs.NODE_ID_CONFIG, "4");
53+
assertEquals(expectedOutput, AbstractKafkaConfig.populateSynonyms(input));
54+
}
55+
}

0 commit comments

Comments
 (0)