aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/ConfigServerBootstrapTest.java
blob: d1877e1e0576e14400e70b1c1c1fe8b937034864 (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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.component.Version;
import com.yahoo.concurrent.maintenance.Maintainer;
import com.yahoo.config.model.provision.Host;
import com.yahoo.config.model.provision.Hosts;
import com.yahoo.config.model.provision.InMemoryProvisioner;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Zone;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.container.core.VipStatusConfig;
import com.yahoo.container.handler.ClustersStatus;
import com.yahoo.container.handler.VipStatus;
import com.yahoo.container.jdisc.state.StateMonitor;
import com.yahoo.docproc.jdisc.metric.NullMetric;
import com.yahoo.path.Path;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.deploy.DeployTester;
import com.yahoo.vespa.config.server.filedistribution.FileDirectory;
import com.yahoo.vespa.config.server.rpc.RpcServer;
import com.yahoo.vespa.config.server.version.VersionState;
import com.yahoo.vespa.config.server.version.VespaVersion;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;

import static com.yahoo.vespa.config.server.ConfigServerBootstrap.RedeployingApplicationsFails.CONTINUE;
import static com.yahoo.vespa.config.server.ConfigServerBootstrap.VipStatusMode;
import static com.yahoo.vespa.config.server.ConfigServerBootstrap.VipStatusMode.VIP_STATUS_FILE;
import static com.yahoo.vespa.config.server.ConfigServerBootstrap.VipStatusMode.VIP_STATUS_PROGRAMMATICALLY;
import static com.yahoo.vespa.config.server.deploy.DeployTester.createHostedModelFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Ulf Lilleengen
 * @author Harald Musum
 */
public class ConfigServerBootstrapTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testBootstrap() throws Exception {
        ConfigserverConfig configserverConfig = createConfigserverConfig(temporaryFolder);
        InMemoryProvisioner provisioner = new InMemoryProvisioner(9, false);
        DeployTester tester = new DeployTester.Builder(temporaryFolder).modelFactory(createHostedModelFactory())
                                                                       .configserverConfig(configserverConfig)
                                                                       .hostProvisioner(provisioner).build();
        tester.deployApp("src/test/apps/hosted/");

        RpcServer rpcServer = createRpcServer(configserverConfig);
        // Take a host away so that there are too few for the application, to verify we can still bootstrap
        provisioner.allocations().values().iterator().next().remove(0);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY);
        assertTrue(bootstrap.isUpgraded());
        assertEquals(List.of("ApplicationPackageMaintainer", "TenantsMaintainer"),
                     bootstrap.configServerMaintenance().maintainers().stream()
                              .map(Maintainer::name)
                              .sorted().toList());
        assertFalse(bootstrap.vipStatus().isInRotation());

        bootstrap.doStart();
        waitUntil(rpcServer::isRunning, "failed waiting for Rpc server running");
        assertTrue(rpcServer.isServingConfigRequests());
        waitUntil(() -> bootstrap.status() == StateMonitor.Status.up, "failed waiting for status 'up'");
        waitUntil(() -> bootstrap.vipStatus().isInRotation(), "failed waiting for server to be in rotation");
        assertEquals(List.of("ApplicationPackageMaintainer", "FileDistributionMaintainer", "ReindexingMaintainer", "SessionsMaintainer", "TenantsMaintainer"),
                     bootstrap.configServerMaintenance().maintainers().stream()
                              .map(Maintainer::name)
                              .sorted().toList());

        bootstrap.deconstruct();
        assertEquals(StateMonitor.Status.down, bootstrap.status());
        assertFalse(rpcServer.isRunning());
        assertTrue(rpcServer.isServingConfigRequests());
        assertFalse(bootstrap.vipStatus().isInRotation());
    }

    // Just tests setup, the actual response of accessing /status.html depends on the status
    // file existing or not, which cannot be tested here
    @Test
    public void testBootstrapWithVipStatusFile() throws Exception {
        ConfigserverConfig configserverConfig = createConfigserverConfig(temporaryFolder);
        InMemoryProvisioner provisioner = new InMemoryProvisioner(9, false);
        DeployTester tester = new DeployTester.Builder(temporaryFolder).modelFactory(createHostedModelFactory())
                .configserverConfig(configserverConfig).hostProvisioner(provisioner).build();
        tester.deployApp("src/test/apps/hosted/");

        RpcServer rpcServer = createRpcServer(configserverConfig);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_FILE);
        assertTrue(bootstrap.isUpgraded());
        assertTrue(bootstrap.vipStatus().isInRotation()); // default is in rotation when using status file

        bootstrap.doStart();
        waitUntil(rpcServer::isRunning, "failed waiting for Rpc server running");
        assertTrue(rpcServer.isServingConfigRequests());
        waitUntil(() -> bootstrap.status() == StateMonitor.Status.up, "failed waiting for status 'up'");
        waitUntil(() -> bootstrap.vipStatus().isInRotation(), "failed waiting for server to be in rotation");
        bootstrap.deconstruct();
    }

    @Test
    public void testBootstrapWhenRedeploymentFails() throws Exception {
        ConfigserverConfig configserverConfig = createConfigserverConfig(temporaryFolder);
        DeployTester tester = new DeployTester.Builder(temporaryFolder).modelFactory(createHostedModelFactory())
                .configserverConfig(configserverConfig).build();
        tester.deployApp("src/test/apps/hosted/");

        // Manipulate application package so that it will fail deployment when config server starts
        java.nio.file.Files.delete(Paths.get(configserverConfig.configServerDBDir())
                                           .resolve("tenants/")
                                           .resolve(tester.tenant().getName().value())
                                           .resolve("sessions/2/services.xml"));

        RpcServer rpcServer = createRpcServer(configserverConfig);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY);
        assertTrue(bootstrap.isUpgraded());
        assertFalse(bootstrap.vipStatus().isInRotation());
        // Call method directly, to be sure that it is finished redeploying all applications and we can check status
        bootstrap.doStart();
        // App is invalid, bootstrapping was unsuccessful. Status should be 'initializing',
        // rpc server should not be running and it should be out of rotation
        assertEquals(StateMonitor.Status.initializing, bootstrap.status());
        assertTrue(rpcServer.isRunning());
        assertFalse(rpcServer.isServingConfigRequests());
        assertFalse(bootstrap.vipStatus().isInRotation());

        bootstrap.deconstruct();
    }

    @Test
    public void testBootstrapNonHostedOneConfigModel() throws Exception {
        ConfigserverConfig configserverConfig = createConfigserverConfigNonHosted(temporaryFolder);
        String vespaVersion = "1.2.3";
        List<Host> hosts = createHosts(vespaVersion);
        Curator curator = new MockCurator();
        DeployTester tester = new DeployTester.Builder(temporaryFolder)
                .modelFactory(DeployTester.createModelFactory(Version.fromString(vespaVersion)))
                .hostProvisioner(new InMemoryProvisioner(new Hosts(hosts), true, false))
                .configserverConfig(configserverConfig)
                .zone(new Zone(Environment.dev, RegionName.defaultName()))
                .curator(curator)
                .build();
        tester.deployApp("src/test/apps/app/", vespaVersion);
        ApplicationId applicationId = tester.applicationId();

        // Ugly hack, but I see no other way of doing it:
        // Manipulate application version in zookeeper so that it is an older version than the model we know, which is
        // the case when upgrading on non-hosted installations
        curator.set(Path.fromString("/config/v2/tenants/" + applicationId.tenant().value() + "/sessions/2/version"), Utf8.toBytes("1.2.2"));

        RpcServer rpcServer = createRpcServer(configserverConfig);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY);
        assertTrue(bootstrap.isUpgraded());
        bootstrap.doStart();
        waitUntil(rpcServer::isRunning, "failed waiting for Rpc server running");
        assertTrue(rpcServer.isServingConfigRequests());
        waitUntil(() -> bootstrap.status() == StateMonitor.Status.up, "failed waiting for status 'up'");
        waitUntil(() -> bootstrap.vipStatus().isInRotation(), "failed waiting for server to be in rotation");
    }

    @Test
    public void testBootstrapBetweenVersions() throws Exception {
        ConfigserverConfig configserverConfig = createConfigserverConfigNonHosted(temporaryFolder);
        String oldVespaVersion = "8.100.1";
        Curator curator = new MockCurator();
        DeployTester tester = new DeployTester.Builder(temporaryFolder)
                .modelFactory(DeployTester.createModelFactory(Version.fromString(oldVespaVersion)))
                .configserverConfig(configserverConfig)
                .curator(curator)
                .build();
        RpcServer rpcServer = createRpcServer(configserverConfig);

        assertUpgradeWorks("7,.594.36", "8.110.1", tester, rpcServer);
        assertUpgradeWorks("8.100.1", "8.110.1", tester, rpcServer);

        assertUpgradeFails("8.100.1", "8.131.1", tester, rpcServer,
                           "Cannot upgrade from 8.100.1 to 8.131.1. Please upgrade to an older version first, " +
                                   "the interval between the two versions is too large (> 30 releases)." +
                                   " Setting VESPA_SKIP_UPGRADE_CHECK=true will skip this check at your own risk," +
                                   " see https://vespa.ai/releases.html#versions");
        assertUpgradeFails("7.99.1", "8.100.1", tester, rpcServer,
                           "Cannot upgrade from 7.99.1 to 8.100.1 (new major version). Please upgrade to 7.594.36 first." +
                                   " Setting VESPA_SKIP_UPGRADE_CHECK=true will skip this check at your own risk," +
                                   " see https://vespa.ai/releases.html#versions");
        assertUpgradeFails("7.594.36", "8.121.1", tester, rpcServer,
                           "Cannot upgrade from 7.594.36 to 8.121.1. Please upgrade to an older version first," +
                                   " the interval between the two versions is too large (> 30 releases)." +
                                   " Setting VESPA_SKIP_UPGRADE_CHECK=true will skip this check at your own risk," +
                                   " see https://vespa.ai/releases.html#versions");
        assertUpgradeFails("6.11.11", "8.121.1", tester, rpcServer,
                           "Cannot upgrade from 6.11.11 to 8.121.1 (upgrade across 2 major versions not supported)." +
                                   " Please upgrade to 7.594.36 first." +
                                   " Setting VESPA_SKIP_UPGRADE_CHECK=true will skip this check at your own risk," +
                                   " see https://vespa.ai/releases.html#versions");

        Version versionToUpgradeTo = Version.fromString("8.131.1");
        boolean skipUpgradeCheck = true;
        VersionState versionState = createVersionState(tester.curator(), versionToUpgradeTo, skipUpgradeCheck);
        assertUpgradeWorks("7.594.36", tester, rpcServer, versionState);
        assertUpgradeWorks("8.100.1", tester, rpcServer, versionState);
    }

    private void assertUpgradeWorks(String from, String to, DeployTester tester, RpcServer rpcServer) throws IOException {
        Version versionToUpgradeTo = Version.fromString(to);
        VersionState versionState = createVersionState(tester.curator(), versionToUpgradeTo);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY, versionState);
        bootstrap.versionState().storeVersion(from);
        bootstrap.doStart();
    }

    private void assertUpgradeWorks(String from, DeployTester tester, RpcServer rpcServer, VersionState versionState) {
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY, versionState);
        bootstrap.versionState().storeVersion(from);
        bootstrap.doStart();
    }

    private void assertUpgradeFails(String from, String to, DeployTester tester, RpcServer rpcServer, String expected) throws IOException {
        Version versionToUpgradeTo = Version.fromString(to);
        VersionState versionState = createVersionState(tester.curator(), versionToUpgradeTo);
        Bootstrapper bootstrap = createBootstrapper(tester, rpcServer, VIP_STATUS_PROGRAMMATICALLY, versionState);
        bootstrap.versionState().storeVersion(from);
        try {
            bootstrap.doStart();
            fail("Expected bootstrap to fail");
        } catch (RuntimeException e) {
            assertEquals(expected, e.getMessage());
        }
    }

    private Bootstrapper createBootstrapper(DeployTester tester, RpcServer rpcServer, VipStatusMode vipStatusMode) throws IOException {
        Version versionToUpgradeTo = new Version(VespaVersion.major, VespaVersion.minor, VespaVersion.micro);
        return createBootstrapper(tester,rpcServer, vipStatusMode, createVersionState(tester.curator(), versionToUpgradeTo));
    }

    private Bootstrapper createBootstrapper(DeployTester tester,
                                            RpcServer rpcServer,
                                            VipStatusMode vipStatusMode,
                                            VersionState versionState) {
        StateMonitor stateMonitor = StateMonitor.createForTesting();
        VipStatus vipStatus = createVipStatus(stateMonitor);
        return new Bootstrapper(tester.applicationRepository(),
                                rpcServer,
                                versionState,
                                stateMonitor,
                                vipStatus,
                                vipStatusMode,
                                new FileDirectory(tester.applicationRepository().configserverConfig()));
    }

    private void waitUntil(BooleanSupplier booleanSupplier, String messageIfWaitingFails) throws InterruptedException {
        Duration timeout = Duration.ofSeconds(60);
        Instant endTime = Instant.now().plus(timeout);
        while (Instant.now().isBefore(endTime)) {
            if (booleanSupplier.getAsBoolean())
                return;
            Thread.sleep(10);
        }
        throw new RuntimeException(messageIfWaitingFails);
    }

    private MockRpcServer createRpcServer(ConfigserverConfig configserverConfig) throws IOException {
        return new MockRpcServer(configserverConfig.rpcport(), temporaryFolder.newFolder());
    }

    private static ConfigserverConfig createConfigserverConfig(TemporaryFolder temporaryFolder) throws IOException {
        return createConfigserverConfig(temporaryFolder, true);
    }

    private static ConfigserverConfig createConfigserverConfigNonHosted(TemporaryFolder temporaryFolder) throws IOException {
        return createConfigserverConfig(temporaryFolder, false);
    }

    private static ConfigserverConfig createConfigserverConfig(TemporaryFolder temporaryFolder, boolean hosted) throws IOException {
        var servers = List.of(new ConfigserverConfig.Zookeeperserver.Builder().hostname("foo").port(1),
                              new ConfigserverConfig.Zookeeperserver.Builder().hostname("bar").port(1),
                              new ConfigserverConfig.Zookeeperserver.Builder().hostname("baz").port(1));
        return new ConfigserverConfig(new ConfigserverConfig.Builder()
                                              .configServerDBDir(temporaryFolder.newFolder("serverdb").getAbsolutePath())
                                              .configDefinitionsDir(temporaryFolder.newFolder("configdefinitions").getAbsolutePath())
                                              .fileReferencesDir(temporaryFolder.newFolder("filedistribution").getAbsolutePath())
                                              .hostedVespa(hosted)
                                              .multitenant(hosted)
                                              .maxDurationOfBootstrap(0) /* seconds, 0 => it will not retry deployment if bootstrap fails */
                                              .sleepTimeWhenRedeployingFails(0) /* seconds */
                                              .zookeeperserver(servers));
    }

    private List<Host> createHosts(String vespaVersion) {
        return List.of(createHost("host1", vespaVersion), createHost("host2", vespaVersion), createHost("host3", vespaVersion));
    }

    private Host createHost(String hostname, String version) {
        return new Host(hostname, Collections.emptyList(), Optional.empty(), Optional.of(com.yahoo.component.Version.fromString(version)));
    }

    private VipStatus createVipStatus(StateMonitor stateMonitor) {
        return new VipStatus(new QrSearchersConfig.Builder().build(),
                             new VipStatusConfig.Builder().build(),
                             new ClustersStatus(),
                             stateMonitor,
                             new NullMetric());
    }

    private VersionState createVersionState(Curator curator, Version vespaVersion) throws IOException {
        return new VersionState(temporaryFolder.newFile(), curator, vespaVersion, false);
    }

    private VersionState createVersionState(Curator curator,
                                            Version vespaVersion,
                                            boolean throwIfUpgradeBetweenVersionsTooFarApart) throws IOException {
        return new VersionState(temporaryFolder.newFile(), curator, vespaVersion, throwIfUpgradeBetweenVersionsTooFarApart);
    }

    public static class MockRpcServer extends com.yahoo.vespa.config.server.rpc.MockRpcServer {

        volatile boolean isRunning = false;

        MockRpcServer(int port, File tempDir) {
            super(port, tempDir);
        }

        @Override
        public void run() {
            isRunning = true;
        }

        @Override
        public void stop() {
            isRunning = false;
        }

        @Override
        public boolean isRunning() {
            return isRunning;
        }
    }

    private static class Bootstrapper extends ConfigServerBootstrap {

        public Bootstrapper(ApplicationRepository applicationRepository,
                            RpcServer server,
                            VersionState versionState,
                            StateMonitor stateMonitor,
                            VipStatus vipStatus,
                            VipStatusMode vipStatusMode,
                            FileDirectory fileDirectory) {
            super(applicationRepository, server, versionState, stateMonitor, vipStatus, CONTINUE, vipStatusMode, fileDirectory);
        }

        @Override
        public void start() {
            // Do nothing, avoids bootstrapping apps in constructor, use doStart() below to really bootstrap apps
        }

        public void doStart() {
            super.start();
        }

        public VersionState versionState() { return versionState; }

        public boolean isUpgraded() { return versionState.isUpgraded(); }

    }

}