-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjvm.rs
More file actions
287 lines (258 loc) · 9.33 KB
/
jvm.rs
File metadata and controls
287 lines (258 loc) · 9.33 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
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_operator::{
memory::{BinaryMultiple, MemoryQuantity},
role_utils::{self, JvmArgumentOverrides},
};
use crate::crd::{
AnyServiceConfig, CONFIG_DIR_NAME, HbaseRole, JVM_SECURITY_PROPERTIES_FILE, METRICS_PORT,
v1alpha1,
};
const JAVA_HEAP_FACTOR: f32 = 0.8;
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("invalid memory resource configuration - missing default or value in crd?"))]
MissingMemoryResourceConfig,
#[snafu(display("invalid memory config"))]
InvalidMemoryConfig {
source: stackable_operator::memory::Error,
},
#[snafu(display("failed to merge jvm argument overrides"))]
MergeJvmArgumentOverrides { source: role_utils::Error },
#[snafu(display("the HBase role [{role}] is missing from spec"))]
MissingHbaseRole { role: String },
}
// Applies to both the servers and the CLI
pub fn construct_global_jvm_args(kerberos_enabled: bool) -> String {
let mut jvm_args = Vec::new();
if kerberos_enabled {
jvm_args.push("-Djava.security.krb5.conf=/stackable/kerberos/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(" ")
}
/// JVM arguments that are specifically for the role (server), so will *not* be used e.g. by CLI tools.
/// Heap settings are excluded, as they go into `HBASE_HEAPSIZE`.
pub fn construct_role_specific_non_heap_jvm_args(
hbase: &v1alpha1::HbaseCluster,
hbase_role: &HbaseRole,
role_group: &str,
product_version: &str,
) -> Result<String, Error> {
let mut jvm_args = vec![format!(
"-Djava.security.properties={CONFIG_DIR_NAME}/{JVM_SECURITY_PROPERTIES_FILE}"
)];
// Starting with HBase 2.6 the JVM exporter is not needed anymore
if product_version.starts_with("2.4") || product_version.starts_with("2.5") {
jvm_args.push(
format!("-javaagent:/stackable/jmx/jmx_prometheus_javaagent.jar={METRICS_PORT}:/stackable/jmx/{hbase_role}.yaml")
);
}
if hbase.has_kerberos_enabled() {
jvm_args.push("-Djava.security.krb5.conf=/stackable/kerberos/krb5.conf".to_owned());
}
let operator_generated = JvmArgumentOverrides::new_with_only_additions(jvm_args);
let merged = match hbase_role {
HbaseRole::Master => hbase
.spec
.masters
.as_ref()
.context(MissingHbaseRoleSnafu {
role: hbase_role.to_string(),
})?
.get_merged_jvm_argument_overrides(role_group, &operator_generated)
.context(MergeJvmArgumentOverridesSnafu)?,
HbaseRole::RegionServer => hbase
.spec
.region_servers
.as_ref()
.context(MissingHbaseRoleSnafu {
role: hbase_role.to_string(),
})?
.get_merged_jvm_argument_overrides(role_group, &operator_generated)
.context(MergeJvmArgumentOverridesSnafu)?,
HbaseRole::RestServer => hbase
.spec
.rest_servers
.as_ref()
.context(MissingHbaseRoleSnafu {
role: hbase_role.to_string(),
})?
.get_merged_jvm_argument_overrides(role_group, &operator_generated)
.context(MergeJvmArgumentOverridesSnafu)?,
};
jvm_args = merged
.effective_jvm_config_after_merging()
// Sorry for the clone, that's how operator-rs is currently modelled :P
.clone();
jvm_args.retain(|arg| !is_heap_jvm_argument(arg));
Ok(jvm_args.join(" "))
}
/// This will be put into `HBASE_HEAPSIZE`, which is just the heap size in megabytes (with the `m`
/// unit prepended).
///
/// The `bin/hbase` script will use this to set the needed JVM arguments.
/// Looking at `bin/hbase`, you can actually add the `m` suffix to make the unit more clear, the
/// script will detect this [here](https://github.com/apache/hbase/blob/777010361abb203b8b17673d84acf4f7f1d0283a/bin/hbase#L165)
/// and work correctly.
pub fn construct_hbase_heapsize_env(merged_config: &AnyServiceConfig) -> Result<String, Error> {
let heap_size = MemoryQuantity::try_from(
merged_config
.resources()
.memory
.limit
.as_ref()
.context(MissingMemoryResourceConfigSnafu)?,
)
.context(InvalidMemoryConfigSnafu)?
.scale_to(BinaryMultiple::Mebi)
* JAVA_HEAP_FACTOR;
heap_size
.format_for_java()
.context(InvalidMemoryConfigSnafu)
}
fn is_heap_jvm_argument(jvm_argument: &str) -> bool {
let lowercase = jvm_argument.to_lowercase();
lowercase.starts_with("-xms") || lowercase.starts_with("-xmx")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::crd::{HbaseRole, v1alpha1};
#[test]
fn test_construct_jvm_arguments_defaults() {
let input = r#"
apiVersion: hbase.stackable.tech/v1alpha1
kind: HbaseCluster
metadata:
name: simple-hbase
spec:
image:
productVersion: 2.6.1
clusterConfig:
hdfsConfigMapName: simple-hdfs
zookeeperConfigMapName: simple-znode
masters:
roleGroups:
default:
replicas: 1
regionServers:
roleGroups:
default:
replicas: 1
"#;
let (hbase, hbase_role, merged_config, role_group, product_version) =
construct_boilerplate(input);
let global_jvm_args = construct_global_jvm_args(false);
let role_specific_non_heap_jvm_args = construct_role_specific_non_heap_jvm_args(
&hbase,
&hbase_role,
&role_group,
&product_version,
)
.unwrap();
let hbase_heapsize_env = construct_hbase_heapsize_env(&merged_config).unwrap();
assert_eq!(global_jvm_args, "");
assert_eq!(
role_specific_non_heap_jvm_args,
"-Djava.security.properties=/stackable/conf/security.properties"
);
assert_eq!(hbase_heapsize_env, "819m");
}
#[test]
fn test_construct_jvm_argument_overrides() {
let input = r#"
apiVersion: hbase.stackable.tech/v1alpha1
kind: HbaseCluster
metadata:
name: simple-hbase
spec:
image:
productVersion: 2.6.1
clusterConfig:
hdfsConfigMapName: simple-hdfs
zookeeperConfigMapName: simple-znode
authentication:
tlsSecretClass: tls
kerberos:
secretClass: kerberos-simple
masters:
roleGroups:
default:
replicas: 1
regionServers:
config:
resources:
memory:
limit: 42Gi
jvmArgumentOverrides:
add:
- -Dhttps.proxyHost=proxy.my.corp
- -Dhttps.proxyPort=8080
- -Djava.net.preferIPv4Stack=true
roleGroups:
default:
replicas: 1
jvmArgumentOverrides:
removeRegex:
- -Dhttps.proxyPort=.*
add:
- -Xmx40000m # This has no effect!
- -Dhttps.proxyPort=1234
"#;
let (hbase, hbase_role, merged_config, role_group, product_version) =
construct_boilerplate(input);
let global_jvm_args = construct_global_jvm_args(hbase.has_kerberos_enabled());
let role_specific_non_heap_jvm_args = construct_role_specific_non_heap_jvm_args(
&hbase,
&hbase_role,
&role_group,
&product_version,
)
.unwrap();
let hbase_heapsize_env = construct_hbase_heapsize_env(&merged_config).unwrap();
assert_eq!(
global_jvm_args,
"-Djava.security.krb5.conf=/stackable/kerberos/krb5.conf"
);
assert_eq!(
role_specific_non_heap_jvm_args,
"-Djava.security.properties=/stackable/conf/security.properties \
-Djava.security.krb5.conf=/stackable/kerberos/krb5.conf \
-Dhttps.proxyHost=proxy.my.corp \
-Djava.net.preferIPv4Stack=true \
-Dhttps.proxyPort=1234"
);
assert_eq!(hbase_heapsize_env, "34406m");
}
fn construct_boilerplate(
hbase_cluster: &str,
) -> (
v1alpha1::HbaseCluster,
HbaseRole,
AnyServiceConfig,
String,
String,
) {
let hbase: v1alpha1::HbaseCluster =
serde_yaml::from_str(hbase_cluster).expect("illegal test input");
let hbase_role = HbaseRole::RegionServer;
let merged_config = hbase
.merged_config(&hbase_role, "default", "my-hdfs")
.unwrap();
let product_version = hbase.spec.image.product_version().to_owned();
(
hbase,
hbase_role,
merged_config,
"default".to_owned(),
product_version,
)
}
}