-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjvm.rs
More file actions
329 lines (311 loc) · 11.3 KB
/
Copy pathjvm.rs
File metadata and controls
329 lines (311 loc) · 11.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use semver::Version;
use snafu::{ResultExt, Snafu};
use stackable_operator::{
crd::s3::v1alpha1::ConnectionSpec,
memory::MemoryQuantity,
role_utils::{self, GenericRoleConfig, JavaCommonConfig, JvmArgumentOverrides, Role},
};
use crate::crd::{
AWS_REGION, DruidConfigOverrides, DruidRole, JVM_SECURITY_PROPERTIES_FILE, LOG4J2_CONFIG,
RW_CONFIG_DIRECTORY, STACKABLE_TRUST_STORE, STACKABLE_TRUST_STORE_PASSWORD,
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to format memory quantity {value:?} for Java"))]
FormatMemoryStringForJava {
value: MemoryQuantity,
source: stackable_operator::memory::Error,
},
#[snafu(display("failed to merge jvm argument overrides"))]
MergeJvmArgumentOverrides { source: role_utils::Error },
}
/// Please note that this function is slightly different than all other operators, because memory
/// management is far more advanced in this operator.
pub fn construct_jvm_args<T>(
druid_role: &DruidRole,
role: &Role<T, DruidConfigOverrides, GenericRoleConfig, JavaCommonConfig>,
role_group: &str,
heap: MemoryQuantity,
direct_memory: Option<MemoryQuantity>,
s3_conn: Option<&ConnectionSpec>,
// TODO (@NickLarsenNZ): Remove this once we don't support Druid less than 37.0.0
druid_version: Result<Version, semver::Error>,
) -> Result<String, Error> {
let heap_str = heap
.format_for_java()
.with_context(|_| FormatMemoryStringForJavaSnafu { value: heap })?;
let direct_memory_str = if let Some(m) = direct_memory {
Some(
m.format_for_java()
.with_context(|_| FormatMemoryStringForJavaSnafu { value: m })?,
)
} else {
None
};
let mut jvm_args = vec![
"-server".to_owned(),
format!("-Xmx{heap_str}"),
format!("-Xms{heap_str}"),
];
if let Some(direct_memory) = direct_memory_str {
jvm_args.push(format!("-XX:MaxDirectMemorySize={direct_memory}"));
}
jvm_args.extend([
"-XX:+ExitOnOutOfMemoryError".to_owned(),
"-XX:+UseG1GC".to_owned(),
format!("-Djava.security.properties={RW_CONFIG_DIRECTORY}/{JVM_SECURITY_PROPERTIES_FILE}"),
"-Duser.timezone=UTC".to_owned(),
"-Dfile.encoding=UTF-8".to_owned(),
"-Djava.io.tmpdir=/tmp".to_owned(),
"-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager".to_owned(),
format!("-Dlog4j.configurationFile={RW_CONFIG_DIRECTORY}/{LOG4J2_CONFIG}"),
format!("-Djavax.net.ssl.trustStore={STACKABLE_TRUST_STORE}"),
format!("-Djavax.net.ssl.trustStorePassword={STACKABLE_TRUST_STORE_PASSWORD}"),
"-Djavax.net.ssl.trustStoreType=pkcs12".to_owned(),
]);
if druid_role == &DruidRole::Coordinator {
jvm_args.push("-Dderby.stream.error.file=/stackable/var/druid/derby.log".to_owned());
}
// TODO (@NickLarsenNZ): Remove the condition (keep the body) once we no longer support Druid
// less than 37.0.0
// Druid >= 37.0.0 uses the AWS SDK v2, which requires a region to be set via the JVM system
// property `aws.region`.
if matches!(&druid_version, Ok(v) if *v >= Version::new(37, 0, 0))
&& let Some(s3) = s3_conn
{
jvm_args.push(format!(
"-D{AWS_REGION}={region_name}",
region_name = s3.region.name
));
}
let operator_generated = JvmArgumentOverrides::new_with_only_additions(jvm_args);
let merged_jvm_argument_overrides = role
.get_merged_jvm_argument_overrides(role_group, &operator_generated)
.context(MergeJvmArgumentOverridesSnafu)?;
Ok(merged_jvm_argument_overrides
.effective_jvm_config_after_merging()
.join("\n"))
}
#[cfg(test)]
mod tests {
use indoc::indoc;
use super::*;
use crate::crd::v1alpha1::DruidCluster;
#[test]
fn test_construct_jvm_arguments_defaults() {
let input = r#"
apiVersion: druid.stackable.tech/v1alpha1
kind: DruidCluster
metadata:
name: simple-druid
spec:
image:
productVersion: 30.0.0
clusterConfig:
deepStorage:
hdfs:
configMapName: simple-hdfs
directory: /druid
metadataDatabase:
postgresql:
host: druid-postgresql
database: druid
credentialsSecretName: mySecret
zookeeperConfigMapName: simple-druid-znode
brokers:
roleGroups:
default:
replicas: 1
coordinators:
roleGroups:
default:
replicas: 1
historicals:
roleGroups:
default:
replicas: 1
middleManagers:
roleGroups:
default:
replicas: 1
routers:
roleGroups:
default:
replicas: 1
"#;
let coordinator_jvm_config = construct_jvm_config_for_test(input, &DruidRole::Coordinator);
let historical_jvm_config = construct_jvm_config_for_test(input, &DruidRole::Historical);
assert_eq!(
coordinator_jvm_config,
indoc! {"
-server
-Xmx468m
-Xms468m
-XX:+ExitOnOutOfMemoryError
-XX:+UseG1GC
-Djava.security.properties=/stackable/rwconfig/security.properties
-Duser.timezone=UTC
-Dfile.encoding=UTF-8
-Djava.io.tmpdir=/tmp
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=/stackable/rwconfig/log4j2.properties
-Djavax.net.ssl.trustStore=/stackable/truststore.p12
-Djavax.net.ssl.trustStorePassword=changeit
-Djavax.net.ssl.trustStoreType=pkcs12
-Dderby.stream.error.file=/stackable/var/druid/derby.log"}
);
assert_eq!(
historical_jvm_config,
indoc! {"
-server
-Xmx900m
-Xms900m
-XX:MaxDirectMemorySize=300m
-XX:+ExitOnOutOfMemoryError
-XX:+UseG1GC
-Djava.security.properties=/stackable/rwconfig/security.properties
-Duser.timezone=UTC
-Dfile.encoding=UTF-8
-Djava.io.tmpdir=/tmp
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=/stackable/rwconfig/log4j2.properties
-Djavax.net.ssl.trustStore=/stackable/truststore.p12
-Djavax.net.ssl.trustStorePassword=changeit
-Djavax.net.ssl.trustStoreType=pkcs12"}
);
}
#[test]
fn test_construct_jvm_argument_overrides() {
let input = r#"
apiVersion: druid.stackable.tech/v1alpha1
kind: DruidCluster
metadata:
name: simple-druid
spec:
image:
productVersion: 30.0.0
clusterConfig:
deepStorage:
hdfs:
configMapName: simple-hdfs
directory: /druid
metadataDatabase:
postgresql:
host: druid-postgresql
database: druid
credentialsSecretName: mySecret
zookeeperConfigMapName: simple-druid-znode
brokers:
roleGroups:
default:
replicas: 1
coordinators:
config:
resources:
memory:
limit: 42Gi
jvmArgumentOverrides:
add:
- -Dhttps.proxyHost=proxy.my.corp
- -Dhttps.proxyPort=8080
- -Djava.net.preferIPv4Stack=true
roleGroups:
default:
replicas: 1
jvmArgumentOverrides:
# We need more memory!
removeRegex:
- -Xmx.*
- -Dhttps.proxyPort=.*
add:
- -Xmx40000m
- -Dhttps.proxyPort=1234
historicals:
config:
resources:
memory:
limit: 13Gi
jvmArgumentOverrides:
add:
- -Dfoo=bar
roleGroups:
default:
replicas: 1
middleManagers:
roleGroups:
default:
replicas: 1
routers:
roleGroups:
default:
replicas: 1
"#;
let coordinator_jvm_config = construct_jvm_config_for_test(input, &DruidRole::Coordinator);
let historical_jvm_config = construct_jvm_config_for_test(input, &DruidRole::Historical);
assert_eq!(
coordinator_jvm_config,
indoc! {"
-server
-Xms42708m
-XX:+ExitOnOutOfMemoryError
-XX:+UseG1GC
-Djava.security.properties=/stackable/rwconfig/security.properties
-Duser.timezone=UTC
-Dfile.encoding=UTF-8
-Djava.io.tmpdir=/tmp
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=/stackable/rwconfig/log4j2.properties
-Djavax.net.ssl.trustStore=/stackable/truststore.p12
-Djavax.net.ssl.trustStorePassword=changeit
-Djavax.net.ssl.trustStoreType=pkcs12
-Dderby.stream.error.file=/stackable/var/druid/derby.log
-Dhttps.proxyHost=proxy.my.corp
-Djava.net.preferIPv4Stack=true
-Xmx40000m
-Dhttps.proxyPort=1234"}
);
assert_eq!(
historical_jvm_config,
indoc! {"
-server
-Xmx9759m
-Xms9759m
-XX:MaxDirectMemorySize=3253m
-XX:+ExitOnOutOfMemoryError
-XX:+UseG1GC
-Djava.security.properties=/stackable/rwconfig/security.properties
-Duser.timezone=UTC
-Dfile.encoding=UTF-8
-Djava.io.tmpdir=/tmp
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=/stackable/rwconfig/log4j2.properties
-Djavax.net.ssl.trustStore=/stackable/truststore.p12
-Djavax.net.ssl.trustStorePassword=changeit
-Djavax.net.ssl.trustStoreType=pkcs12
-Dfoo=bar"}
);
}
fn construct_jvm_config_for_test(druid_cluster: &str, druid_role: &DruidRole) -> String {
let deserializer = serde_yaml::Deserializer::from_str(druid_cluster);
let druid: DruidCluster =
serde_yaml::with::singleton_map_recursive::deserialize(deserializer).unwrap();
let role = druid.get_role(druid_role);
let merged_config = druid.merged_config().unwrap();
let (heap, direct) = merged_config
.common_config(druid_role, "default")
.unwrap()
.resources
.get_memory_sizes(druid_role)
.unwrap();
construct_jvm_args(
druid_role,
&role,
"default",
heap,
direct,
None,
semver::Version::parse("37.0.0"),
)
.unwrap()
}
}