aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/JvmOptionsTest.java
blob: 3435e7faa5e7b62875b99308509e7ff5e76d1f7f (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.model.container.xml;

import com.yahoo.collections.Pair;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.search.config.QrStartConfig;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.ContainerCluster;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author baldersheim
 * @author gjoranv
 */
public class JvmOptionsTest extends ContainerModelBuilderTestBase {

    @Test
    void verify_jvm_tag_with_attributes() throws IOException, SAXException {
        String servicesXml =
                "<container version='1.0'>" +
                        "  <search/>" +
                        "  <nodes>" +
                        "    <jvm options='-XX:SoftRefLRUPolicyMSPerMB=2500' gc-options='-XX:+UseParNewGC' allocated-memory='45%'/>" +
                        "    <node hostalias='mockhost'/>" +
                        "  </nodes>" +
                        "</container>";
        ApplicationPackage applicationPackage = new MockApplicationPackage.Builder().withServices(servicesXml).build();
        // Need to create VespaModel to make deploy properties have effect
        final TestLogger logger = new TestLogger();
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), new DeployState.Builder()
                .applicationPackage(applicationPackage)
                .deployLogger(logger)
                .build());
        QrStartConfig.Builder qrStartBuilder = new QrStartConfig.Builder();
        model.getConfig(qrStartBuilder, "container/container.0");
        QrStartConfig qrStartConfig = new QrStartConfig(qrStartBuilder);
        assertEquals("-XX:+UseParNewGC", qrStartConfig.jvm().gcopts());
        assertEquals(45, qrStartConfig.jvm().heapSizeAsPercentageOfPhysicalMemory());
        assertEquals("-XX:SoftRefLRUPolicyMSPerMB=2500", model.getContainerClusters().values().iterator().next().getContainers().get(0).getJvmOptions());
    }

    @Test
    void honours_jvm_gc_options() {
        Element clusterElem = DomBuilderTest.parse(
                "<container version='1.0'>",
                "  <search/>",
                "  <nodes jvm-gc-options='-XX:+UseG1GC'>",
                "    <node hostalias='mockhost'/>",
                "  </nodes>",
                "</container>");
        createModel(root, clusterElem);
        QrStartConfig.Builder qrStartBuilder = new QrStartConfig.Builder();
        root.getConfig(qrStartBuilder, "container/container.0");
        QrStartConfig qrStartConfig = new QrStartConfig(qrStartBuilder);
        assertEquals("-XX:+UseG1GC", qrStartConfig.jvm().gcopts());
    }

    private static void verifyIgnoreJvmGCOptions(boolean isHosted) throws IOException, SAXException {
        String servicesXml =
                "<container version='1.0'>" +
                        "  <nodes jvm-gc-options='-XX:+UseG1GC' jvm-options='-XX:+UseParNewGC'>" +
                        "    <node hostalias='mockhost'/>" +
                        "  </nodes>" +
                        "</container>";
        ApplicationPackage applicationPackage = new MockApplicationPackage.Builder().withServices(servicesXml).build();
        // Need to create VespaModel to make deploy properties have effect
        final TestLogger logger = new TestLogger();
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), new DeployState.Builder()
                .applicationPackage(applicationPackage)
                .deployLogger(logger)
                .properties(new TestProperties().setHostedVespa(isHosted))
                .build());
        QrStartConfig.Builder qrStartBuilder = new QrStartConfig.Builder();
        model.getConfig(qrStartBuilder, "container/container.0");
        QrStartConfig qrStartConfig = new QrStartConfig(qrStartBuilder);
        assertEquals("-XX:+UseG1GC", qrStartConfig.jvm().gcopts());
    }

    @Test
    void ignores_jvmgcoptions_on_conflicting_jvmoptions() throws IOException, SAXException {
        verifyIgnoreJvmGCOptions(false);
        verifyIgnoreJvmGCOptions(true);
    }

    private void verifyJvmGCOptions(boolean isHosted, String featureFlagDefault, String override, String expected) throws IOException, SAXException  {
        String servicesXml =
                "<container version='1.0'>" +
                        "  <nodes " + ((override == null) ? ">" : ("jvm-gc-options='" + override + "'>")) +
                        "    <node hostalias='mockhost'/>" +
                        "  </nodes>" +
                        "</container>";
        ApplicationPackage applicationPackage = new MockApplicationPackage.Builder().withServices(servicesXml).build();
        // Need to create VespaModel to make deploy properties have effect
        final TestLogger logger = new TestLogger();
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), new DeployState.Builder()
                .applicationPackage(applicationPackage)
                .deployLogger(logger)
                .properties(new TestProperties().setJvmGCOptions(featureFlagDefault).setHostedVespa(isHosted))
                .build());
        QrStartConfig.Builder qrStartBuilder = new QrStartConfig.Builder();
        model.getConfig(qrStartBuilder, "container/container.0");
        QrStartConfig qrStartConfig = new QrStartConfig(qrStartBuilder);
        assertEquals(expected, qrStartConfig.jvm().gcopts());
    }

    @Test
    void requireThatJvmGCOptionsIsHonoured()  throws IOException, SAXException {
        verifyJvmGCOptions(false, null, null, ContainerCluster.G1GC);
        verifyJvmGCOptions(true, null, null, ContainerCluster.PARALLEL_GC);
        verifyJvmGCOptions(true, "", null, ContainerCluster.PARALLEL_GC);
        verifyJvmGCOptions(false, "-XX:+UseG1GC", null, "-XX:+UseG1GC");
        verifyJvmGCOptions(true, "-XX:+UseG1GC", null, "-XX:+UseG1GC");
        verifyJvmGCOptions(false, null, "-XX:+UseG1GC", "-XX:+UseG1GC");
        verifyJvmGCOptions(false, "-XX:+UseParallelGC", "-XX:+UseG1GC", "-XX:+UseG1GC");
        verifyJvmGCOptions(false, null, "-XX:+UseParallelGC", "-XX:+UseParallelGC");
    }

    @Test
    void requireThatValidJvmGcOptionsAreNotLogged() throws IOException, SAXException {
        // Valid options, should not log anything
        verifyLoggingOfJvmGcOptions(true, "-XX:+ParallelGCThreads=8");
        verifyLoggingOfJvmGcOptions(true, "-XX:MaxTenuringThreshold=15"); // No + or - after colon
    }

    @Test
    void requireThatDeprecatedJvmOptionsAreLogged() throws IOException, SAXException {
        String optionName = "jvm-options";
        verifyLoggingOfLegacyJvmOptions(true, optionName, "-XX:+ParallelGCThreads=8", optionName);
        verifyLoggingOfLegacyJvmOptions(false, optionName, "-XX:+ParallelGCThreads=8", optionName);
    }

    @Test
    void requireThatDeprecatedJvmOptionsAreLogged_2() throws IOException, SAXException {
        String optionName = "allocated-memory";
        verifyLoggingOfLegacyJvmOptions(true, optionName, "50%", optionName);
        verifyLoggingOfLegacyJvmOptions(false, optionName, "50%", optionName);
    }

    @Test
    void requireThatDeprecatedJvmGcOptionsAreLogged() throws IOException, SAXException {
        String optionName = "jvm-gc-options";
        verifyLoggingOfLegacyJvmOptions(true, optionName, "-XX:+ParallelGCThreads=8", optionName);
        verifyLoggingOfLegacyJvmOptions(false, optionName, "-XX:+ParallelGCThreads=8", optionName);
    }

    private void verifyThatInvalidJvmGcOptionsFailDeployment(String options, String expected) throws IOException, SAXException {
        try {
            buildModelWithJvmOptions(new TestProperties().setHostedVespa(true), "gc-options", options);
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().startsWith(expected));
        }
    }

    @Test
    void requireThatInvalidJvmGcOptionsFailDeployment() throws IOException, SAXException {
        verifyThatInvalidJvmGcOptionsFailDeployment(
                "-XX:+ParallelGCThreads=8 foo     bar",
                "Invalid or misplaced JVM GC options in services.xml: bar,foo");
        verifyThatInvalidJvmGcOptionsFailDeployment(
                "-XX:+UseConcMarkSweepGC",
                "Invalid or misplaced JVM GC options in services.xml: -XX:+UseConcMarkSweepGC");
    }

    private void verifyLoggingOfJvmGcOptions(boolean isHosted, String override, String... invalidOptions) throws IOException, SAXException  {
        verifyLogMessage(isHosted, "gc-options", override, invalidOptions);
    }

    private void verifyLogMessage(boolean isHosted, String optionName, String override, String... invalidOptions) throws IOException, SAXException  {
        var logger = buildModelWithJvmOptions(isHosted, optionName, override);
        var message = verifyLogMessage(logger, invalidOptions);
        if (message != null)
            assertTrue(message.contains("Invalid or misplaced JVM"), message);
    }

    private String verifyLogMessage(TestLogger logger, String... invalidOptions) {
        List<String> strings = Arrays.asList(invalidOptions.clone());
        // Verify that nothing is logged if there are no invalid options
        if (strings.isEmpty()) {
            assertEquals(0, logger.msgs.size(), logger.msgs.size() > 0 ? logger.msgs.get(0).getSecond() : "");
            return null;
        }

        assertTrue(logger.msgs.size() > 0, "Expected 1 or more log messages for invalid JM options, got none");
        Pair<Level, String> firstOption = logger.msgs.get(0);
        assertEquals(Level.WARNING, firstOption.getFirst());

        Collections.sort(strings);
        return firstOption.getSecond();
    }

    private TestLogger buildModelWithJvmOptions(boolean isHosted, String optionName, String override) throws IOException, SAXException {
        return buildModelWithJvmOptions(new TestProperties().setHostedVespa(isHosted), optionName, override);
    }

    private TestLogger buildModelWithJvmOptions(TestProperties properties, String optionName, String override) throws IOException, SAXException {
        TestLogger logger = new TestLogger();
        String servicesXml =
                "<container version='1.0'>" +
                        "  <nodes>" +
                        "    <jvm " + optionName + "='" + override + "'/>" +
                        "    <node hostalias='mockhost'/>" +
                        "  </nodes>" +
                        "</container>";
        buildModel(properties, logger, servicesXml);
        return logger;
    }

    private void verifyLoggingOfLegacyJvmOptions(boolean isHosted, String optionName, String override, String... invalidOptions) throws IOException, SAXException  {
        var logger = buildModelWithLegacyJvmOptions(isHosted, optionName, override);

        var message = verifyLogMessage(logger, invalidOptions);
        if (message != null)
            assertTrue(message.contains("'" + optionName + "' is deprecated and will be removed"), message);
    }

    private TestLogger buildModelWithLegacyJvmOptions(boolean isHosted, String optionName, String override) throws IOException, SAXException {
        TestProperties properties = new TestProperties().setHostedVespa(isHosted);
        TestLogger logger = new TestLogger();
        String servicesXml =
                "<container version='1.0'>" +
                        "  <nodes " + optionName + "='" + override + "'>" +
                        "    <node hostalias='mockhost'/>" +
                        "  </nodes>" +
                        "</container>";
        buildModel(properties, logger, servicesXml);
        return logger;
    }

    private void buildModel(TestProperties properties, TestLogger logger, String servicesXml) throws IOException, SAXException {
        ApplicationPackage app = new MockApplicationPackage.Builder().withServices(servicesXml).build();
        new VespaModel(new NullConfigModelRegistry(), new DeployState.Builder()
                .applicationPackage(app)
                .deployLogger(logger)
                .properties(properties)
                .build());
    }

    @Test
    void requireThatValidJvmOptionsAreNotLogged() throws IOException, SAXException {
        // Valid options, should not log anything
        verifyLogMessage(true, "options", "-Xms2G");
        verifyLogMessage(true, "options", "-Xlog:gc");
        verifyLogMessage(true, "options", "-Djava.library.path=/opt/vespa/lib64:/home/y/lib64");
        verifyLogMessage(true, "options", "-XX:-OmitStackTraceInFastThrow");
        verifyLogMessage(false, "options", "-Xms2G");
    }

    @Test
    void requireThatInvalidJvmOptionsFailDeployment() throws IOException, SAXException {
        try {
            buildModelWithJvmOptions(new TestProperties().setHostedVespa(true),
                    "options",
                    "-Xms2G foo     bar");
            fail();
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("Invalid or misplaced JVM options in services.xml: bar,foo"));
        }
    }

}