Skip to content

Commit 767a62a

Browse files
authored
KAFKA-18737 KafkaDockerWrapper setup functions fails due to storage format command (#18844)
The current Docker Hub documentation for Kafka is based on the use of static voters. Since Kafka 4.0 utilizes dynamic voters, users following the doc of docker hub may encounter unexpected behavior. Due to the limited time available for the 4.0.0 release, a simple and quick solution is to revert to using static voters within the Docker image. This can be achieved by adding a configuration file with static voter definitions to the kafka/docker folder, keeping it separate from the main kafka/config directory. This approach allows us to encourage the use of dynamic voters in typical deployments while maintaining compatibility within the Docker image. Reviewers: Vedarth Sharma <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent 2124511 commit 767a62a

File tree

7 files changed

+162
-7
lines changed

7 files changed

+162
-7
lines changed

Diff for: core/src/main/scala/kafka/docker/KafkaDockerWrapper.scala

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
2222
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
2323
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature
2424
import kafka.Kafka
25-
import kafka.tools.StorageTool
25+
import kafka.tools.{StorageTool, TerseFailure}
2626
import kafka.utils.Logging
2727
import net.sourceforge.argparse4j.ArgumentParsers
2828
import net.sourceforge.argparse4j.impl.Arguments.store
2929
import net.sourceforge.argparse4j.inf.Namespace
3030
import org.apache.kafka.common.utils.Exit
31+
import org.apache.kafka.raft.QuorumConfig
3132

3233
import java.nio.charset.StandardCharsets
3334
import java.nio.file.{Files, Path, Paths, StandardCopyOption, StandardOpenOption}
@@ -52,7 +53,15 @@ object KafkaDockerWrapper extends Logging {
5253
}
5354

5455
val formatCmd = formatStorageCmd(finalConfigsPath, envVars)
55-
StorageTool.main(formatCmd)
56+
try {
57+
StorageTool.main(formatCmd)
58+
} catch {
59+
case terseFailure: TerseFailure => if (terseFailure.getMessage.contains(QuorumConfig.QUORUM_VOTERS_CONFIG)) {
60+
throw new TerseFailure("To maximize compatibility, the Docker image continues to use static voters, " +
61+
"which are supported in 3.7 and later.", terseFailure)
62+
} else throw terseFailure
63+
case e: Throwable => throw e
64+
}
5665
case "start" =>
5766
val configFile = namespace.getString("config")
5867
info("Starting Kafka server in the native mode.")
@@ -110,7 +119,9 @@ object KafkaDockerWrapper extends Logging {
110119
case Some(str) => str
111120
case None => throw new RuntimeException("CLUSTER_ID environment variable is not set.")
112121
}
113-
Array("format", "--cluster-id=" + clusterId, "-c", s"${configsPath.toString}/server.properties", "--standalone")
122+
// We maintain static voter configurations in Docker Hub images for better version compatibility and deployment stability,
123+
// despite having dynamic voter support in the latest release.
124+
Array("format", "--cluster-id=" + clusterId, "-c", s"${configsPath.toString}/server.properties")
114125
}
115126

116127
private def prepareConfigs(defaultConfigsPath: Path, mountedConfigsPath: Path, finalConfigsPath: Path): Unit = {

Diff for: core/src/main/scala/kafka/tools/TerseFailure.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.apache.kafka.common.KafkaException
2525
*
2626
* @param message The message to print out before exiting. A stack trace will not
2727
* be printed.
28+
* @param cause The exception's cause
2829
*/
29-
class TerseFailure(message: String) extends KafkaException(message) {
30+
class TerseFailure(message: String, cause: Throwable) extends KafkaException(message, cause) {
31+
def this(message: String) = this(message, null)
3032
}

Diff for: core/src/test/scala/unit/kafka/docker/KafkaDockerWrapperTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class KafkaDockerWrapperTest {
133133
val configsPath = Paths.get("/path/to/configs")
134134
val envVars = Map("CLUSTER_ID" -> "MYwKGPhXQZidgd0qMv8Mkw")
135135

136-
val expected = Array("format", "--cluster-id=MYwKGPhXQZidgd0qMv8Mkw", "-c", "/path/to/configs/server.properties", "--standalone")
136+
val expected = Array("format", "--cluster-id=MYwKGPhXQZidgd0qMv8Mkw", "-c", "/path/to/configs/server.properties")
137137
val actual = KafkaDockerWrapper.formatStorageCmd(configsPath, envVars)
138138

139139
assertArrayEquals(expected.toArray[Object], actual.toArray[Object])

Diff for: docker/common.py

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import subprocess
1919
import tempfile
2020
import os
21+
from distutils.file_util import copy_file
22+
2123
from distutils.dir_util import copy_tree
2224
import shutil
2325

@@ -36,6 +38,7 @@ def build_docker_image_runner(command, image_type):
3638
current_dir = os.path.dirname(os.path.realpath(__file__))
3739
copy_tree(f"{current_dir}/{image_type}", f"{temp_dir_path}/{image_type}")
3840
copy_tree(f"{current_dir}/resources", f"{temp_dir_path}/{image_type}/resources")
41+
copy_file(f"{current_dir}/server.properties", f"{temp_dir_path}/{image_type}")
3942
command = command.replace("$DOCKER_FILE", f"{temp_dir_path}/{image_type}/Dockerfile")
4043
command = command.replace("$DOCKER_DIR", f"{temp_dir_path}/{image_type}")
4144
try:

Diff for: docker/jvm/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ USER root
2424
ARG kafka_url
2525

2626
COPY jsa_launch /etc/kafka/docker/jsa_launch
27+
COPY server.properties /etc/kafka/docker/server.properties
2728

2829
RUN set -eux ; \
2930
apk update ; \
@@ -78,11 +79,11 @@ RUN set -eux ; \
7879
chmod -R ug+w /etc/kafka /var/lib/kafka /etc/kafka/secrets; \
7980
cp /opt/kafka/config/log4j2.yaml /etc/kafka/docker/log4j2.yaml; \
8081
cp /opt/kafka/config/tools-log4j2.yaml /etc/kafka/docker/tools-log4j2.yaml; \
81-
cp /opt/kafka/config/server.properties /etc/kafka/docker/server.properties; \
8282
rm kafka.tgz kafka.tgz.asc KEYS; \
8383
apk del wget gpg gpg-agent; \
8484
apk cache clean;
8585

86+
COPY server.properties /etc/kafka/docker/server.properties
8687
COPY --from=build-jsa kafka.jsa /opt/kafka/kafka.jsa
8788
COPY --from=build-jsa storage.jsa /opt/kafka/storage.jsa
8889
COPY --chown=appuser:appuser resources/common-scripts /etc/kafka/docker

Diff for: docker/native/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ RUN apk update ; \
6262
chown appuser:root -R /etc/kafka /opt/kafka /mnt/shared/config ; \
6363
chmod -R ug+w /etc/kafka /opt/kafka /mnt/shared/config ;
6464

65+
COPY server.properties /etc/kafka/docker/server.properties
6566
COPY --chown=appuser:root --from=build-native-image /app/kafka/kafka.Kafka /opt/kafka/
66-
COPY --chown=appuser:root --from=build-native-image /app/kafka/config/server.properties /etc/kafka/docker/
6767
COPY --chown=appuser:root --from=build-native-image /app/kafka/config/log4j2.yaml /etc/kafka/docker/
6868
COPY --chown=appuser:root --from=build-native-image /app/kafka/config/tools-log4j2.yaml /etc/kafka/docker/
6969
COPY --chown=appuser:root resources/common-scripts /etc/kafka/docker/

Diff for: docker/server.properties

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
################## Why We Need This Separate Config ####################
17+
18+
# While our latest version supports dynamic voters configuration,
19+
# we will continue using static voter configurations in our Docker Hub images.
20+
# This decision ensures broader compatibility across different versions and
21+
# maintains consistent behavior for existing deployments.
22+
# By retaining static voter implementation in our Docker images, we can provide
23+
# a more stable and predictable environment for users across various versions of the application.
24+
25+
############################# Server Basics #############################
26+
27+
# The role of this server. Setting this puts us in KRaft mode
28+
process.roles=broker,controller
29+
30+
# The node id associated with this instance's roles
31+
node.id=1
32+
33+
# The connect string for the controller quorum
34+
controller.quorum.voters=1@localhost:9093
35+
36+
############################# Socket Server Settings #############################
37+
38+
# The address the socket server listens on.
39+
# Combined nodes (i.e. those with `process.roles=broker,controller`) must list the controller listener here at a minimum.
40+
# If the broker listener is not defined, the default listener will use a host name that is equal to the value of java.net.InetAddress.getCanonicalHostName(),
41+
# with PLAINTEXT listener name, and port 9092.
42+
# FORMAT:
43+
# listeners = listener_name://host_name:port
44+
# EXAMPLE:
45+
# listeners = PLAINTEXT://your.host.name:9092
46+
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
47+
48+
# Name of listener used for communication between brokers.
49+
inter.broker.listener.name=PLAINTEXT
50+
51+
# Listener name, hostname and port the broker or the controller will advertise to clients.
52+
# If not set, it uses the value for "listeners".
53+
advertised.listeners=PLAINTEXT://localhost:9092
54+
55+
# A comma-separated list of the names of the listeners used by the controller.
56+
# If no explicit mapping set in `listener.security.protocol.map`, default will be using PLAINTEXT protocol
57+
# This is required if running in KRaft mode.
58+
controller.listener.names=CONTROLLER
59+
60+
# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
61+
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
62+
63+
# The number of threads that the server uses for receiving requests from the network and sending responses to the network
64+
num.network.threads=3
65+
66+
# The number of threads that the server uses for processing requests, which may include disk I/O
67+
num.io.threads=8
68+
69+
# The send buffer (SO_SNDBUF) used by the socket server
70+
socket.send.buffer.bytes=102400
71+
72+
# The receive buffer (SO_RCVBUF) used by the socket server
73+
socket.receive.buffer.bytes=102400
74+
75+
# The maximum size of a request that the socket server will accept (protection against OOM)
76+
socket.request.max.bytes=104857600
77+
78+
79+
############################# Log Basics #############################
80+
81+
# A comma separated list of directories under which to store log files
82+
log.dirs=/tmp/kraft-combined-logs
83+
84+
# The default number of log partitions per topic. More partitions allow greater
85+
# parallelism for consumption, but this will also result in more files across
86+
# the brokers.
87+
num.partitions=1
88+
89+
# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
90+
# This value is recommended to be increased for installations with data dirs located in RAID array.
91+
num.recovery.threads.per.data.dir=1
92+
93+
############################# Internal Topic Settings #############################
94+
# The replication factor for the group metadata internal topics "__consumer_offsets", "__share_group_state" and "__transaction_state"
95+
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
96+
offsets.topic.replication.factor=1
97+
share.coordinator.state.topic.replication.factor=1
98+
share.coordinator.state.topic.min.isr=1
99+
transaction.state.log.replication.factor=1
100+
transaction.state.log.min.isr=1
101+
102+
############################# Log Flush Policy #############################
103+
104+
# Messages are immediately written to the filesystem but by default we only fsync() to sync
105+
# the OS cache lazily. The following configurations control the flush of data to disk.
106+
# There are a few important trade-offs here:
107+
# 1. Durability: Unflushed data may be lost if you are not using replication.
108+
# 2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
109+
# 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
110+
# The settings below allow one to configure the flush policy to flush data after a period of time or
111+
# every N messages (or both). This can be done globally and overridden on a per-topic basis.
112+
113+
# The number of messages to accept before forcing a flush of data to disk
114+
#log.flush.interval.messages=10000
115+
116+
# The maximum amount of time a message can sit in a log before we force a flush
117+
#log.flush.interval.ms=1000
118+
119+
############################# Log Retention Policy #############################
120+
121+
# The following configurations control the disposal of log segments. The policy can
122+
# be set to delete segments after a period of time, or after a given size has accumulated.
123+
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
124+
# from the end of the log.
125+
126+
# The minimum age of a log file to be eligible for deletion due to age
127+
log.retention.hours=168
128+
129+
# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
130+
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
131+
#log.retention.bytes=1073741824
132+
133+
# The maximum size of a log segment file. When this size is reached a new log segment will be created.
134+
log.segment.bytes=1073741824
135+
136+
# The interval at which log segments are checked to see if they can be deleted according
137+
# to the retention policies
138+
log.retention.check.interval.ms=300000

0 commit comments

Comments
 (0)