-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand.rs
More file actions
181 lines (160 loc) · 6.46 KB
/
Copy pathcommand.rs
File metadata and controls
181 lines (160 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use indoc::formatdoc;
use stackable_operator::{
product_logging::framework::{
create_vector_shutdown_file_command, remove_vector_shutdown_file_command,
},
utils::COMMON_BASH_TRAP_FUNCTIONS,
};
use crate::{
crd::{
KafkaPodDescriptor, STACKABLE_CONFIG_DIR, STACKABLE_KERBEROS_KRB5_PATH,
role::{broker::BROKER_PROPERTIES_FILE, controller::CONTROLLER_PROPERTIES_FILE},
security::KafkaTlsSecurity,
v1alpha1,
},
product_logging::STACKABLE_LOG_DIR,
};
/// Returns the commands to start the main Kafka container
pub fn broker_kafka_container_commands(
kafka: &v1alpha1::KafkaCluster,
cluster_id: &str,
controller_descriptors: Vec<KafkaPodDescriptor>,
kafka_security: &KafkaTlsSecurity,
product_version: &str,
) -> String {
formatdoc! {"
{COMMON_BASH_TRAP_FUNCTIONS}
{remove_vector_shutdown_file_command}
prepare_signal_handlers
containerdebug --output={STACKABLE_LOG_DIR}/containerdebug-state.json --loop &
{set_realm_env}
{broker_start_command}
wait_for_termination $!
{create_vector_shutdown_file_command}
",
remove_vector_shutdown_file_command = remove_vector_shutdown_file_command(STACKABLE_LOG_DIR),
create_vector_shutdown_file_command = create_vector_shutdown_file_command(STACKABLE_LOG_DIR),
set_realm_env = match kafka_security.has_kerberos_enabled() {
true => format!("export KERBEROS_REALM=$(grep -oP 'default_realm = \\K.*' {STACKABLE_KERBEROS_KRB5_PATH})"),
false => "".to_string(),
},
broker_start_command = broker_start_command(kafka, cluster_id, controller_descriptors, product_version),
}
}
fn broker_start_command(
kafka: &v1alpha1::KafkaCluster,
cluster_id: &str,
controller_descriptors: Vec<KafkaPodDescriptor>,
product_version: &str,
) -> String {
if kafka.is_controller_configured() {
formatdoc! {"
POD_INDEX=$(echo \"$POD_NAME\" | grep -oE '[0-9]+$')
export REPLICA_ID=$((POD_INDEX+NODE_ID_OFFSET))
cp {config_dir}/{properties_file} /tmp/{properties_file}
config-utils template /tmp/{properties_file}
cp {config_dir}/jaas.properties /tmp/jaas.properties
config-utils template /tmp/jaas.properties
bin/kafka-storage.sh format --cluster-id {cluster_id} --config /tmp/{properties_file} --ignore-formatted {initial_controller_command}
bin/kafka-server-start.sh /tmp/{properties_file} &
",
config_dir = STACKABLE_CONFIG_DIR,
properties_file = BROKER_PROPERTIES_FILE,
initial_controller_command = initial_controllers_command(&controller_descriptors, product_version),
}
} else {
formatdoc! {"
cp {config_dir}/{properties_file} /tmp/{properties_file}
config-utils template /tmp/{properties_file}
cp {config_dir}/jaas.properties /tmp/jaas.properties
config-utils template /tmp/jaas.properties
bin/kafka-server-start.sh /tmp/{properties_file} &",
config_dir = STACKABLE_CONFIG_DIR,
properties_file = BROKER_PROPERTIES_FILE,
}
}
}
// During a namespace or stacklet delete the Kafka controllers shut down too fast leaving the brokers
// in a bad state.
// Brokers try to connect to controllers before gracefully shutting down but by that time, all
// controllers are already gone.
// The broker pods are then kept alive until the value of `gracefulShutdownTimeout` is reached.
// The environment variable `PRE_STOP_CONTROLLER_SLEEP_SECONDS` delays the termination of the
// controller processes to give the brokers more time to offload data and shutdown gracefully.
// Kubernetes has a built in `pre-stop` hook feature that is not yet generally available on all platforms
// supported by the operator.
const BASH_TRAP_FUNCTIONS: &str = r#"
prepare_signal_handlers()
{
unset term_child_pid
unset term_kill_needed
trap 'handle_term_signal' TERM
}
handle_term_signal()
{
if [ "${term_child_pid}" ]; then
[ -n "$PRE_STOP_CONTROLLER_SLEEP_SECONDS" ] && sleep "$PRE_STOP_CONTROLLER_SLEEP_SECONDS"
kill -TERM "${term_child_pid}" 2>/dev/null
else
term_kill_needed="yes"
fi
}
wait_for_termination()
{
set +e
term_child_pid=$1
if [[ -v term_kill_needed ]]; then
[ -n "$PRE_STOP_CONTROLLER_SLEEP_SECONDS" ] && sleep "$PRE_STOP_CONTROLLER_SLEEP_SECONDS"
kill -TERM "${term_child_pid}" 2>/dev/null
fi
wait ${term_child_pid} 2>/dev/null
trap - TERM
wait ${term_child_pid} 2>/dev/null
set -e
}
"#;
pub fn controller_kafka_container_command(
cluster_id: &str,
controller_descriptors: Vec<KafkaPodDescriptor>,
product_version: &str,
) -> String {
formatdoc! {"
{BASH_TRAP_FUNCTIONS}
{remove_vector_shutdown_file_command}
prepare_signal_handlers
containerdebug --output={STACKABLE_LOG_DIR}/containerdebug-state.json --loop &
POD_INDEX=$(echo \"$POD_NAME\" | grep -oE '[0-9]+$')
export REPLICA_ID=$((POD_INDEX+NODE_ID_OFFSET))
cp {config_dir}/{properties_file} /tmp/{properties_file}
config-utils template /tmp/{properties_file}
bin/kafka-storage.sh format --cluster-id {cluster_id} --config /tmp/{properties_file} --ignore-formatted {initial_controller_command}
bin/kafka-server-start.sh /tmp/{properties_file} &
wait_for_termination $!
{create_vector_shutdown_file_command}
",
remove_vector_shutdown_file_command = remove_vector_shutdown_file_command(STACKABLE_LOG_DIR),
config_dir = STACKABLE_CONFIG_DIR,
properties_file = CONTROLLER_PROPERTIES_FILE,
initial_controller_command = initial_controllers_command(&controller_descriptors, product_version),
create_vector_shutdown_file_command = create_vector_shutdown_file_command(STACKABLE_LOG_DIR)
}
}
fn to_initial_controllers(controller_descriptors: &[KafkaPodDescriptor]) -> String {
controller_descriptors
.iter()
.map(|desc| desc.as_voter())
.collect::<Vec<String>>()
.join(",")
}
fn initial_controllers_command(
controller_descriptors: &[KafkaPodDescriptor],
product_version: &str,
) -> String {
match product_version.starts_with("3.7") {
true => "".to_string(),
false => format!(
"--initial-controllers {initial_controllers}",
initial_controllers = to_initial_controllers(controller_descriptors),
),
}
}