-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjvm.rs
213 lines (190 loc) · 7.05 KB
/
jvm.rs
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
use snafu::{ResultExt, Snafu};
use stackable_operator::{
k8s_openapi::api::core::v1::ResourceRequirements,
memory::{BinaryMultiple, MemoryQuantity},
role_utils::JvmArgumentOverrides,
};
use crate::{
crd::{constants::JVM_SECURITY_PROPERTIES_FILE, v1alpha1, HdfsNodeRole},
security::kerberos::KERBEROS_CONTAINER_PATH,
};
const JVM_HEAP_FACTOR: f32 = 0.8;
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("invalid java heap config for {role:?}"))]
InvalidJavaHeapConfig {
source: stackable_operator::memory::Error,
role: String,
},
#[snafu(display("failed to merge jvm argument overrides"))]
MergeJvmArgumentOverrides { source: crate::crd::Error },
}
// All init or sidecar containers must have access to the following settings.
// As the Prometheus metric emitter is not part of this config it's safe to use for hdfs cli tools as well.
// This will not only enable the init containers to work, but also the user to run e.g.
// `bin/hdfs dfs -ls /` without getting `Caused by: java.lang.IllegalArgumentException: KrbException: Cannot locate default realm`
// because the `-Djava.security.krb5.conf` setting is missing
pub fn construct_global_jvm_args(kerberos_enabled: bool) -> String {
let mut jvm_args = Vec::new();
if kerberos_enabled {
jvm_args.push(format!(
"-Djava.security.krb5.conf={KERBEROS_CONTAINER_PATH}/krb5.conf"
));
}
// We do *not* add user overrides to the global JVM args, but only the role specific JVM arguments.
// This allows users to configure stuff for the server (probably what they want to do), without
// also influencing e.g. startup scripts.
//
// However, this is just an assumption. If it is wrong users can still envOverride the global
// JVM args.
//
// Please feel absolutely free to change this behavior!
jvm_args.join(" ")
}
pub fn construct_role_specific_jvm_args(
hdfs: &v1alpha1::HdfsCluster,
hdfs_role: &HdfsNodeRole,
role_group: &str,
kerberos_enabled: bool,
resources: Option<&ResourceRequirements>,
config_dir: &str,
metrics_port: u16,
) -> Result<String, Error> {
let mut jvm_args = Vec::new();
if let Some(memory_limit) = resources.and_then(|r| r.limits.as_ref()?.get("memory")) {
let memory_limit = MemoryQuantity::try_from(memory_limit).with_context(|_| {
InvalidJavaHeapConfigSnafu {
role: hdfs_role.to_string(),
}
})?;
let heap = memory_limit.scale_to(BinaryMultiple::Mebi) * JVM_HEAP_FACTOR;
let heap = heap
.format_for_java()
.with_context(|_| InvalidJavaHeapConfigSnafu {
role: hdfs_role.to_string(),
})?;
jvm_args.push(format!("-Xms{heap}"));
jvm_args.push(format!("-Xmx{heap}"));
}
jvm_args.extend([
format!("-Djava.security.properties={config_dir}/{JVM_SECURITY_PROPERTIES_FILE}"),
format!("-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar={metrics_port}:/stackable/jmx/{hdfs_role}.yaml")
]);
if kerberos_enabled {
jvm_args.push(format!(
"-Djava.security.krb5.conf={KERBEROS_CONTAINER_PATH}/krb5.conf"
));
}
let operator_generated = JvmArgumentOverrides::new_with_only_additions(jvm_args);
let merged_jvm_args = hdfs
.get_merged_jvm_argument_overrides(hdfs_role, role_group, &operator_generated)
.context(MergeJvmArgumentOverridesSnafu)?;
Ok(merged_jvm_args
.effective_jvm_config_after_merging()
.join(" "))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{container::ContainerConfig, crd::constants::DEFAULT_NAME_NODE_METRICS_PORT};
#[test]
fn test_global_jvm_args() {
assert_eq!(construct_global_jvm_args(false), "");
assert_eq!(
construct_global_jvm_args(true),
format!("-Djava.security.krb5.conf={KERBEROS_CONTAINER_PATH}/krb5.conf")
);
}
#[test]
fn test_jvm_config_defaults_without_kerberos() {
let input = r#"
apiVersion: hdfs.stackable.tech/v1alpha1
kind: HdfsCluster
metadata:
name: hdfs
spec:
image:
productVersion: 3.4.1
clusterConfig:
zookeeperConfigMapName: hdfs-zk
nameNodes:
roleGroups:
default:
replicas: 1
"#;
let jvm_config = construct_test_role_specific_jvm_args(input, false);
assert_eq!(
jvm_config,
"-Xms819m \
-Xmx819m \
-Djava.security.properties=/stackable/config/security.properties \
-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=8183:/stackable/jmx/namenode.yaml"
);
}
#[test]
fn test_jvm_config_jvm_argument_overrides() {
let input = r#"
apiVersion: hdfs.stackable.tech/v1alpha1
kind: HdfsCluster
metadata:
name: hdfs
spec:
image:
productVersion: 3.4.1
clusterConfig:
zookeeperConfigMapName: hdfs-zk
nameNodes:
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
"#;
let jvm_config = construct_test_role_specific_jvm_args(input, true);
assert_eq!(
jvm_config,
format!(
"-Xms34406m \
-Djava.security.properties=/stackable/config/security.properties \
-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar=8183:/stackable/jmx/namenode.yaml \
-Djava.security.krb5.conf={KERBEROS_CONTAINER_PATH}/krb5.conf \
-Dhttps.proxyHost=proxy.my.corp \
-Djava.net.preferIPv4Stack=true \
-Xmx40000m \
-Dhttps.proxyPort=1234")
);
}
fn construct_test_role_specific_jvm_args(hdfs_cluster: &str, kerberos_enabled: bool) -> String {
let hdfs: v1alpha1::HdfsCluster =
serde_yaml::from_str(hdfs_cluster).expect("illegal test input");
let role = HdfsNodeRole::Name;
let merged_config = role.merged_config(&hdfs, "default").unwrap();
let container_config = ContainerConfig::from(role);
let resources = container_config.resources(&merged_config);
construct_role_specific_jvm_args(
&hdfs,
&role,
"default",
kerberos_enabled,
resources.as_ref(),
"/stackable/config",
DEFAULT_NAME_NODE_METRICS_PORT,
)
.unwrap()
}
}