/
JVMOptionTunerTest.java
338 lines (292 loc) · 13.3 KB
/
JVMOptionTunerTest.java
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
330
331
332
333
334
335
336
337
338
/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.priam.tuner;
import static org.junit.Assert.*;
import com.netflix.priam.config.FakeConfiguration;
import com.netflix.priam.config.IConfiguration;
import com.netflix.priam.scheduler.UnsupportedTypeException;
import java.util.*;
import java.util.stream.Collectors;
import org.junit.Test;
/** Created by aagrawal on 8/29/17. */
public class JVMOptionTunerTest {
private IConfiguration config;
JVMOptionsTuner tuner;
@Test
public void testCMS() throws Exception {
config = new GCConfiguration(GCType.CMS, null, null, null, null);
List<JVMOption> jvmOptionMap = getConfiguredJVMVersionOptions(config);
// Validate that all CMS options should be uncommented.
long failedVerification =
jvmOptionMap
.stream()
.map(
jvmOption -> {
GCType gcType = GCTuner.getGCType(jvmOption);
if (gcType != null && gcType != GCType.CMS) {
return 1;
}
return 0;
})
.filter(returncode -> (returncode != 0))
.count();
if (failedVerification > 0) throw new Exception("Failed validation for CMS");
}
@Test
public void testG1GC() throws Exception {
config = new GCConfiguration(GCType.G1GC, null, null, null, null);
List<JVMOption> jvmOptionMap = getConfiguredJVMVersionOptions(config);
// Validate that all G1GC options should be uncommented.
long failedVerification =
jvmOptionMap
.stream()
.map(
jvmOption -> {
GCType gcType = GCTuner.getGCType(jvmOption);
if (gcType != null && gcType != GCType.G1GC) {
return 1;
}
return 0;
})
.filter(returncode -> (returncode != 0))
.count();
if (failedVerification > 0) throw new Exception("Failed validation for G1GC");
}
@Test
public void testCMSUpsert() throws Exception {
JVMOption option1 = new JVMOption("-Dsample");
JVMOption option2 = new JVMOption("-Dsample2", "10", false, false);
JVMOption option3 = new JVMOption("-XX:NumberOfGCLogFiles", "20", false, false);
JVMOption xmnOption = new JVMOption("-Xmn", "3G", false, true);
JVMOption xmxOption = new JVMOption("-Xmx", "20G", false, true);
JVMOption xmsOption = new JVMOption("-Xms", "20G", false, true);
StringBuffer buffer =
new StringBuffer(
option1.toJVMOptionString()
+ ","
+ option2.toJVMOptionString()
+ ","
+ option3.toJVMOptionString());
config =
new GCConfiguration(
GCType.CMS,
null,
buffer.toString(),
xmnOption.getValue(),
xmxOption.getValue());
List<JVMOption> jvmOptions = getConfiguredJVMOptions(config);
List<JVMOption> jvmVersionOptions = getConfiguredJVMVersionOptions(config);
// Verify all the options do exist.
assertTrue(jvmVersionOptions.contains(option3));
assertTrue(jvmOptions.contains(option2));
assertTrue(jvmOptions.contains(option1));
// Verify heap options exist with the value provided.
assertTrue(jvmOptions.contains(xmnOption));
assertTrue(jvmOptions.contains(xmxOption));
assertTrue(jvmOptions.contains(xmsOption));
}
@Test
public void testCMSExclude() throws Exception {
JVMOption youngHeap = new JVMOption("-Xmn", "3G", false, true);
JVMOption maxHeap = new JVMOption("-Xmx", "12G", false, true);
JVMOption option1 = new JVMOption("-XX:+UseParNewGC");
JVMOption option2 = new JVMOption("-XX:NumberOfGCLogFiles", "20", false, false);
JVMOption option3 = new JVMOption("-XX:+UseG1GC", null, false, false);
StringBuffer buffer =
new StringBuffer(
option1.toJVMOptionString()
+ ","
+ option2.toJVMOptionString()
+ ","
+ option3.toJVMOptionString());
config = new GCConfiguration(GCType.CMS, buffer.toString(), null, "3G", "12G");
List<JVMOption> jvmOptions = getConfiguredJVMOptions(config);
List<JVMOption> jvmVersionOptions = getConfiguredJVMVersionOptions(config);
// Verify all the options do not exist.
assertFalse(jvmVersionOptions.contains(option3));
assertFalse(jvmVersionOptions.contains(option2));
assertFalse(jvmVersionOptions.contains(option1));
// Verify that Xmn is present since CMS needs tuning of young gen heap
assertTrue(jvmOptions.contains(maxHeap));
assertTrue(jvmOptions.contains(youngHeap));
}
@Test
public void testG1GCUpsertExclude() throws Exception {
JVMOption youngHeap = new JVMOption("-Xmn", "3G", true, true);
JVMOption maxHeap = new JVMOption("-Xmx", "12G", false, true);
JVMOption option1 = new JVMOption("-Dsample");
JVMOption option2 = new JVMOption("-Dsample2", "10", false, false);
JVMOption option3 = new JVMOption("-XX:NumberOfGCLogFiles", "20", false, false);
StringBuffer upsert =
new StringBuffer(
option1.toJVMOptionString()
+ ","
+ option2.toJVMOptionString()
+ ","
+ option3.toJVMOptionString());
JVMOption option4 = new JVMOption("-XX:NumberOfGCLogFiles", null, false, false);
JVMOption option5 = new JVMOption("-XX:+UseG1GC", null, false, false);
StringBuffer exclude =
new StringBuffer(option4.toJVMOptionString() + "," + option5.toJVMOptionString());
config =
new GCConfiguration(
GCType.G1GC, exclude.toString(), upsert.toString(), "3G", "12G");
List<JVMOption> jvmOptions = getConfiguredJVMOptions(config);
List<JVMOption> jvmVersionOptions = getConfiguredJVMVersionOptions(config);
// Verify upserts exist
assertTrue(jvmOptions.contains(option1));
assertTrue(jvmOptions.contains(option2));
// Verify exclude exist. This is to prove that if an element is in EXCLUDE, it will always
// be excluded.
assertFalse(jvmVersionOptions.contains(option3));
assertFalse(jvmVersionOptions.contains(option4));
assertFalse(jvmVersionOptions.contains(option5));
// Verify that Xmn is not present since G1GC autotunes the young gen heap
assertTrue(jvmOptions.contains(maxHeap));
assertFalse(jvmOptions.contains(youngHeap));
List<JVMOption> allJVMOptions = getConfiguredJVMOptions(config, false);
assertTrue(allJVMOptions.contains(youngHeap));
}
@Test
public void testInject() throws Exception {
JVMOption youngHeap = new JVMOption("-Xmn", "3G", true, true);
JVMOption maxHeap = new JVMOption("-Xmx", "12G", false, true);
JVMOption option1 = new JVMOption("-Dsample");
JVMOption option2 = new JVMOption("-Dsample2", "10", false, false);
StringBuffer upsert =
new StringBuffer(option1.toJVMOptionString() + "," + option2.toJVMOptionString());
String upsertString =
"-Dcassandra.schema_delay_ms=60000 -Dcassandra.skip_schema_check_for_versions=ver1,ver2";
config = new GCConfiguration(GCType.G1GC, "", upsert.toString(), "3G", "12G", upsertString);
JVMOptionsTuner tuner = new JVMOptionsTuner(config);
List<String> configuredJVMOptions = tuner.updateJVMOptions().get("configuredJVMOptions");
for (String s :
new String[] {
option1.toJVMOptionString(), option2.toJVMOptionString(), upsertString
}) {
boolean found = false;
for (String f : configuredJVMOptions) {
if (f.equalsIgnoreCase(s)) {
found = true;
break;
}
}
assertTrue("could not find " + s + " in jvm options", found);
}
}
private List<JVMOption> getConfiguredJVMOptions(IConfiguration config) throws Exception {
return getConfiguredJVMOptions(config, true);
}
private List<JVMOption> getConfiguredJVMOptions(IConfiguration config, boolean filter)
throws Exception {
tuner = new JVMOptionsTuner(config);
Map<String, List<String>> options = tuner.updateJVMOptions();
List<String> configuredJVMOptions = options.get("configuredJVMOptions");
if (filter) {
return configuredJVMOptions
.stream()
.map(JVMOption::parse)
.filter(jvmOption -> (jvmOption != null))
.filter(jvmOption -> !jvmOption.isCommented())
.collect(Collectors.toList());
} else {
return configuredJVMOptions.stream().map(JVMOption::parse).collect(Collectors.toList());
}
}
private List<JVMOption> getConfiguredJVMVersionOptions(IConfiguration config) throws Exception {
return getConfiguredJVMVersionOptions(config, true);
}
private List<JVMOption> getConfiguredJVMVersionOptions(IConfiguration config, boolean filter)
throws Exception {
tuner = new JVMOptionsTuner(config);
Map<String, List<String>> options = tuner.updateJVMOptions();
List<String> configuredJVMVersionOptions = options.get("configuredJVMVersionOptions");
if (filter) {
return configuredJVMVersionOptions
.stream()
.map(JVMOption::parse)
.filter(jvmOption -> (jvmOption != null))
.filter(jvmOption -> !jvmOption.isCommented())
.collect(Collectors.toList());
} else {
return configuredJVMVersionOptions
.stream()
.map(JVMOption::parse)
.collect(Collectors.toList());
}
}
private class GCConfiguration extends FakeConfiguration {
private GCType gcType;
private String configuredJVMExclude;
private String configuredJVMUpsert;
private String configuredHeapNewSize;
private String configuredHeapSize;
private String configuredJVMInject;
GCConfiguration(
GCType gcType,
String configuredJVMExclude,
String configuredJVMUpsert,
String configuredHeapNewSize,
String configuredHeapSize) {
this(
gcType,
configuredJVMExclude,
configuredJVMUpsert,
configuredHeapNewSize,
configuredHeapSize,
"");
}
GCConfiguration(
GCType gcType,
String configuredJVMExclude,
String configuredJVMUpsert,
String configuredHeapNewSize,
String configuredHeapSize,
String configuredJVMInject) {
this.gcType = gcType;
this.configuredJVMExclude = configuredJVMExclude;
this.configuredJVMUpsert = configuredJVMUpsert;
this.configuredHeapNewSize = configuredHeapNewSize;
this.configuredHeapSize = configuredHeapSize;
this.configuredJVMInject = configuredJVMInject;
}
@Override
public GCType getGCType() throws UnsupportedTypeException {
return gcType;
}
@Override
public String getJVMExcludeSet() {
return configuredJVMExclude;
}
@Override
public String getHeapSize() {
return configuredHeapSize;
}
@Override
public String getHeapNewSize() {
return configuredHeapNewSize;
}
@Override
public String getJVMUpsertSet() {
return configuredJVMUpsert;
}
@Override
public String getJVMInjectSet() {
return configuredJVMInject;
}
}
}