-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjvm.rs
More file actions
310 lines (284 loc) · 11.5 KB
/
Copy pathjvm.rs
File metadata and controls
310 lines (284 loc) · 11.5 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
// As of 2024-07-05 we support multiple Trino versions. Some using Java 17, some Java 21 and the latest (455) uses Java 22.
// This requires a different JVM config
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
memory::{BinaryMultiple, MemoryQuantity},
role_utils::{self, JvmArgumentOverrides},
};
use crate::crd::{
JVM_HEAP_FACTOR, JVM_SECURITY_PROPERTIES, METRICS_PORT, RW_CONFIG_DIR_NAME,
STACKABLE_CLIENT_TLS_DIR, STACKABLE_TLS_STORE_PASSWORD, TrinoRoleType, v1alpha1,
};
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to convert java heap config to unit [{unit}]"))]
FailedToConvertMemoryResourceConfigToJavaHeap {
source: stackable_operator::memory::Error,
unit: String,
},
#[snafu(display("invalid memory resource configuration - missing default or value in crd?"))]
MissingMemoryResourceConfig,
#[snafu(display("could not convert / scale memory resource config to [{unit}]"))]
FailedToConvertMemoryResourceConfig {
source: stackable_operator::memory::Error,
unit: String,
},
#[snafu(display(
"Trino version {version} is not supported. Only specific versions are handled due to version specific JVM configuration generation"
))]
TrinoVersionNotSupported { version: u16 },
#[snafu(display("failed to merge jvm argument overrides"))]
MergeJvmArgumentOverrides { source: role_utils::Error },
}
// Currently works for all supported versions (as of 2024-09-04) but maybe be changed
// in the future depending on the role and version.
pub fn jvm_config(
product_version: u16,
merged_config: &v1alpha1::TrinoConfig,
role: &TrinoRoleType,
role_group: &str,
) -> Result<String, Error> {
let memory_unit = BinaryMultiple::Mebi;
let heap_size = MemoryQuantity::try_from(
merged_config
.resources
.memory
.limit
.as_ref()
.context(MissingMemoryResourceConfigSnafu)?,
)
.context(FailedToConvertMemoryResourceConfigSnafu {
unit: memory_unit.to_java_memory_unit(),
})?
.scale_to(memory_unit)
* JVM_HEAP_FACTOR;
let heap = heap_size.format_for_java().context(
FailedToConvertMemoryResourceConfigToJavaHeapSnafu {
unit: memory_unit.to_java_memory_unit(),
},
)?;
let mut jvm_args = vec![
"-server".to_owned(),
"# Heap settings".to_owned(),
format!("-Xms{heap}"),
format!("-Xmx{heap}"),
"# Specify security.properties".to_owned(),
format!("-Djava.security.properties={RW_CONFIG_DIR_NAME}/{JVM_SECURITY_PROPERTIES}"),
"# Prometheus metrics exporter".to_owned(),
format!(
"-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar={METRICS_PORT}:/stackable/jmx/config.yaml"
),
"# Truststore settings".to_owned(),
format!("-Djavax.net.ssl.trustStore={STACKABLE_CLIENT_TLS_DIR}/truststore.p12"),
"-Djavax.net.ssl.trustStoreType=pkcs12".to_owned(),
format!("-Djavax.net.ssl.trustStorePassword={STACKABLE_TLS_STORE_PASSWORD}"),
];
jvm_args.push("# Recommended JVM arguments from Trino".to_owned());
jvm_args.extend(recommended_trino_jvm_args(product_version)?);
jvm_args.push("# Arguments from jvmArgumentOverrides".to_owned());
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"))
}
/// For tests we don't actually look at the Trino version, and return a single "representative"
/// JVM argument instead.
/// This enables us to write version-independent tests, which don't need updating for every new
/// Trino version.
#[cfg(test)]
fn recommended_trino_jvm_args(_product_version: u16) -> Result<Vec<String>, Error> {
Ok(vec!["-RecommendedTrinoFlag".to_owned()])
}
#[cfg(not(test))]
fn recommended_trino_jvm_args(product_version: u16) -> Result<Vec<String>, Error> {
match product_version {
// Copied from:
// - https://trino.io/docs/477/installation/deployment.html#jvm-config
477 => Ok(vec![
"-XX:InitialRAMPercentage=80".to_owned(),
"-XX:MaxRAMPercentage=80".to_owned(),
"-XX:G1HeapRegionSize=32M".to_owned(),
"-XX:+ExplicitGCInvokesConcurrent".to_owned(),
"-XX:+ExitOnOutOfMemoryError".to_owned(),
"-XX:+HeapDumpOnOutOfMemoryError".to_owned(),
"-XX:-OmitStackTraceInFastThrow".to_owned(),
"-XX:ReservedCodeCacheSize=512M".to_owned(),
"-XX:PerMethodRecompilationCutoff=10000".to_owned(),
"-XX:PerBytecodeRecompilationCutoff=10000".to_owned(),
"-Djdk.attach.allowAttachSelf=true".to_owned(),
"-Djdk.nio.maxCachedBufferSize=2000000".to_owned(),
"-Dfile.encoding=UTF-8".to_owned(),
"-XX:+EnableDynamicAgentLoading".to_owned(),
]),
// Copied from:
// - https://trino.io/docs/479/installation/deployment.html#jvm-config.
// However, the docs are wrong: https://github.com/trinodb/trino/commit/1ddb0f9976fcd9917aaf0b689ca0acc8635e24f1.
// According to the commit we need to add "--add-modules=jdk.incubator.vector"
479 => Ok(vec![
"-XX:InitialRAMPercentage=80".to_owned(),
"-XX:MaxRAMPercentage=80".to_owned(),
"-XX:G1HeapRegionSize=32M".to_owned(),
"-XX:+ExplicitGCInvokesConcurrent".to_owned(),
"-XX:+ExitOnOutOfMemoryError".to_owned(),
"-XX:+HeapDumpOnOutOfMemoryError".to_owned(),
"-XX:-OmitStackTraceInFastThrow".to_owned(),
"-XX:ReservedCodeCacheSize=512M".to_owned(),
"-XX:PerMethodRecompilationCutoff=10000".to_owned(),
"-XX:PerBytecodeRecompilationCutoff=10000".to_owned(),
"-Djdk.attach.allowAttachSelf=true".to_owned(),
"-Djdk.nio.maxCachedBufferSize=2000000".to_owned(),
"-Dfile.encoding=UTF-8".to_owned(),
"-XX:+EnableDynamicAgentLoading".to_owned(),
"--add-modules=jdk.incubator.vector".to_owned(),
]),
// Copied from:
// - https://trino.io/docs/481/installation/deployment.html#jvm-config
481 => Ok(vec![
"-XX:InitialRAMPercentage=80".to_owned(),
"-XX:MaxRAMPercentage=80".to_owned(),
"-XX:G1HeapRegionSize=32M".to_owned(),
"-XX:+ExplicitGCInvokesConcurrent".to_owned(),
"-XX:+ExitOnOutOfMemoryError".to_owned(),
"-XX:+HeapDumpOnOutOfMemoryError".to_owned(),
"-XX:-OmitStackTraceInFastThrow".to_owned(),
"-XX:ReservedCodeCacheSize=512M".to_owned(),
"-XX:PerMethodRecompilationCutoff=10000".to_owned(),
"-XX:PerBytecodeRecompilationCutoff=10000".to_owned(),
"-Djdk.attach.allowAttachSelf=true".to_owned(),
"-Djdk.nio.maxCachedBufferSize=2000000".to_owned(),
"--add-modules=jdk.incubator.vector".to_owned(),
]),
_ => TrinoVersionNotSupportedSnafu {
version: product_version,
}
.fail(),
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use indoc::indoc;
use super::*;
use crate::crd::{TrinoRole, v1alpha1};
#[test]
fn test_jvm_config_defaults() {
let input = r#"
apiVersion: trino.stackable.tech/v1alpha1
kind: TrinoCluster
metadata:
name: simple-trino
spec:
image:
productVersion: "481"
clusterConfig:
catalogLabelSelector: {}
coordinators:
config:
resources:
memory:
limit: 42Gi
roleGroups:
default:
replicas: 1
"#;
let jvm_config = construct_jvm_config(input);
assert_eq!(
jvm_config,
indoc! {"
-server
# Heap settings
-Xms34406m
-Xmx34406m
# Specify security.properties
-Djava.security.properties=/stackable/rwconfig/security.properties
# Prometheus metrics exporter
-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=8081:/stackable/jmx/config.yaml
# Truststore settings
-Djavax.net.ssl.trustStore=/stackable/client_tls/truststore.p12
-Djavax.net.ssl.trustStoreType=pkcs12
-Djavax.net.ssl.trustStorePassword=changeit
# Recommended JVM arguments from Trino
-RecommendedTrinoFlag
# Arguments from jvmArgumentOverrides"}
);
}
#[test]
fn test_jvm_config_jvm_argument_overrides() {
let input = r#"
apiVersion: trino.stackable.tech/v1alpha1
kind: TrinoCluster
metadata:
name: simple-trino
spec:
image:
productVersion: "481"
clusterConfig:
catalogLabelSelector: {}
coordinators:
config:
resources:
memory:
limit: 42Gi
jvmArgumentOverrides:
remove:
- -XX:+HeapDumpOnOutOfMemoryError
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
"#;
let jvm_config = construct_jvm_config(input);
assert_eq!(
jvm_config,
indoc! {"
-server
# Heap settings
-Xms34406m
# Specify security.properties
-Djava.security.properties=/stackable/rwconfig/security.properties
# Prometheus metrics exporter
-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=8081:/stackable/jmx/config.yaml
# Truststore settings
-Djavax.net.ssl.trustStore=/stackable/client_tls/truststore.p12
-Djavax.net.ssl.trustStoreType=pkcs12
-Djavax.net.ssl.trustStorePassword=changeit
# Recommended JVM arguments from Trino
-RecommendedTrinoFlag
# Arguments from jvmArgumentOverrides
-Dhttps.proxyHost=proxy.my.corp
-Djava.net.preferIPv4Stack=true
-Xmx40000m
-Dhttps.proxyPort=1234"}
);
}
fn construct_jvm_config(trino_cluster: &str) -> String {
let trino: v1alpha1::TrinoCluster =
serde_yaml::from_str(trino_cluster).expect("illegal test input");
let role = TrinoRole::Coordinator;
let rolegroup_ref = role.rolegroup_ref(&trino, "default");
let merged_config = trino.merged_config(&role, &rolegroup_ref, &[]).unwrap();
let coordinators = trino.role(&role).unwrap();
let product_version = trino.spec.image.product_version();
jvm_config(
u16::from_str(product_version).expect("trino version as u16"),
&merged_config,
&coordinators,
"default",
)
.unwrap()
}
}