Skip to content

Commit 212794a

Browse files
authored
Do not specify a JVM garbage collector if none specified in the Coherence yaml. (#760)
1 parent 160cdd7 commit 212794a

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

api/v1/create_statefulset_jvmspec_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestCreateStatefulSetWithJvmSpecWithGarbageCollector(t *testing.T) {
221221
spec := coh.CoherenceResourceSpec{
222222
JVM: &coh.JVMSpec{
223223
Gc: &coh.JvmGarbageCollectorSpec{
224-
Collector: stringPtr("G1"),
224+
Collector: stringPtr("ZGC"),
225225
},
226226
},
227227
}
@@ -230,7 +230,7 @@ func TestCreateStatefulSetWithJvmSpecWithGarbageCollector(t *testing.T) {
230230
deployment := createTestDeployment(spec)
231231
// Create expected StatefulSet
232232
stsExpected := createMinimalExpectedStatefulSet(deployment)
233-
addEnvVarsToAll(stsExpected, corev1.EnvVar{Name: "JVM_GC_COLLECTOR", Value: "G1"})
233+
addEnvVarsToAll(stsExpected, corev1.EnvVar{Name: "JVM_GC_COLLECTOR", Value: "ZGC"})
234234

235235
// assert that the StatefulSet is as expected
236236
assertStatefulSetCreation(t, deployment, stsExpected)

docs/jvm/040_gc.adoc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ If different GC logging arguments are required then the relevant JVM arguments c
6161
The garbage collector to use can be set using the `jvm.gc.collector` field.
6262
This field can be set to either `G1`, `CMS` or `Parallel`
6363
(the field is case-insensitive, invalid values will be silently ignored).
64-
The default collector set, if none has been specified, will be `G1`.
64+
65+
The default collector set, if none has been specified, will be whatever is the default for the JVM being used.
6566
6667
|====
6768
| Parameter | JVM Argument Set
6869
| `G1` | `-XX:+UseG1GC`
6970
| `CMS` | `-XX:+UseConcMarkSweepGC`
7071
| `Parallel` | `-XX:+UseParallelGC`
72+
| `Serial` | `-XX:+UseSerialGC`
73+
| `ZGC` | `-XX:+UseZGC`
7174
|====
7275
7376
For example:
@@ -80,9 +83,15 @@ metadata:
8083
spec:
8184
jvm:
8285
gc:
83-
collector: "G1"
86+
collector: "ZGC"
8487
----
85-
The example above will add `-XX:+UseG1GC` to the command line.
88+
The example above will add `-XX:+UseZGC` to the command line.
89+
90+
[NOTE]
91+
====
92+
The JVM only allows a single `-XX:Use****` option that sets the garbage collector to use, so the collector should not be
93+
specified in both the `spec.jvm.gc.collector` field, and in the `spec.jvm.args` field.
94+
====
8695
8796
=== Adding Arbitrary GC Args
8897

pkg/runner/runner.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,16 @@ func configureCommand(details *run_details.RunDetails) error {
426426

427427
gc := strings.ToLower(details.Getenv(v1.EnvVarJvmGcCollector))
428428
switch {
429-
case gc == "" || gc == "g1":
429+
case gc == "g1":
430430
details.AddMemoryOption("-XX:+UseG1GC")
431431
case gc == "cms":
432432
details.AddMemoryOption("-XX:+UseConcMarkSweepGC")
433433
case gc == "parallel":
434434
details.AddMemoryOption("-XX:+UseParallelGC")
435+
case gc == "serial":
436+
details.AddMemoryOption("-XX:+UseSerialGC")
437+
case gc == "zgc":
438+
details.AddMemoryOption("-XX:+UseZGC")
435439
}
436440

437441
maxRAM := details.Getenv(v1.EnvVarJvmMaxRAM)

pkg/runner/runner_jvm_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,72 @@ func TestJvmGarbageCollectorParallel(t *testing.T) {
265265
g.Expect(e.OsCmd.Args).To(ConsistOf(expected))
266266
}
267267

268+
func TestJvmGarbageCollectorSerial(t *testing.T) {
269+
g := NewGomegaWithT(t)
270+
271+
d := &coh.Coherence{
272+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
273+
Spec: coh.CoherenceStatefulSetResourceSpec{
274+
CoherenceResourceSpec: coh.CoherenceResourceSpec{
275+
JVM: &coh.JVMSpec{
276+
Gc: &coh.JvmGarbageCollectorSpec{
277+
Collector: ptr.To("serial"),
278+
},
279+
},
280+
},
281+
},
282+
}
283+
284+
expectedArgs := append(GetExpectedArgsFileContentWithoutPrefix("-XX:+UseG1GC"), "-XX:+UseSerialGC")
285+
verifyConfigFilesWithArgs(t, d, expectedArgs)
286+
287+
args := []string{"server", "--dry-run"}
288+
env := EnvVarsFromDeployment(t, d)
289+
290+
e, err := ExecuteWithArgsAndNewViper(env, args)
291+
g.Expect(err).NotTo(HaveOccurred())
292+
g.Expect(e).NotTo(BeNil())
293+
g.Expect(e.OsCmd).NotTo(BeNil())
294+
295+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
296+
g.Expect(e.OsCmd.Path).To(Equal(GetJavaCommand()))
297+
expected := append(RemoveArgWithPrefix(GetMinimalExpectedArgs(t), "-XX:+UseG1GC"), "-XX:+UseSerialGC")
298+
g.Expect(e.OsCmd.Args).To(ConsistOf(expected))
299+
}
300+
301+
func TestJvmGarbageCollectorZGC(t *testing.T) {
302+
g := NewGomegaWithT(t)
303+
304+
d := &coh.Coherence{
305+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
306+
Spec: coh.CoherenceStatefulSetResourceSpec{
307+
CoherenceResourceSpec: coh.CoherenceResourceSpec{
308+
JVM: &coh.JVMSpec{
309+
Gc: &coh.JvmGarbageCollectorSpec{
310+
Collector: ptr.To("zgc"),
311+
},
312+
},
313+
},
314+
},
315+
}
316+
317+
expectedArgs := append(GetExpectedArgsFileContentWithoutPrefix("-XX:+UseG1GC"), "-XX:+UseZGC")
318+
verifyConfigFilesWithArgs(t, d, expectedArgs)
319+
320+
args := []string{"server", "--dry-run"}
321+
env := EnvVarsFromDeployment(t, d)
322+
323+
e, err := ExecuteWithArgsAndNewViper(env, args)
324+
g.Expect(err).NotTo(HaveOccurred())
325+
g.Expect(e).NotTo(BeNil())
326+
g.Expect(e.OsCmd).NotTo(BeNil())
327+
328+
g.Expect(e.OsCmd.Dir).To(Equal(TestAppDir))
329+
g.Expect(e.OsCmd.Path).To(Equal(GetJavaCommand()))
330+
expected := append(RemoveArgWithPrefix(GetMinimalExpectedArgs(t), "-XX:+UseG1GC"), "-XX:+UseZGC")
331+
g.Expect(e.OsCmd.Args).To(ConsistOf(expected))
332+
}
333+
268334
func TestJvmGarbageCollectorLoggingTrue(t *testing.T) {
269335
g := NewGomegaWithT(t)
270336

pkg/runner/runner_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func AppendCommonExpectedNonServerArgs(args []string, role string) []string {
217217
"-Dcoherence.operator.diagnostics.dir=/coherence-operator/jvm/unknown/unknown",
218218
"-XX:HeapDumpPath=/coherence-operator/jvm/unknown/unknown/heap-dumps/unknown-unknown.hprof",
219219
"-Dcoherence.operator.can.resume.services=true",
220-
"-XX:+UseG1GC",
221220
"-Dcoherence.ttl=0",
222221
"-XX:+UnlockDiagnosticVMOptions",
223222
"-XX:ErrorFile=/coherence-operator/jvm/unknown/unknown/hs-err-unknown-unknown.log",

0 commit comments

Comments
 (0)