aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionTest.java
blob: d5d0fe72dbe5c42bdd7a091f13766d587a7a6975 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.session;

import com.google.common.io.Files;
import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.model.api.ModelContext;
import com.yahoo.config.model.api.ModelCreateResult;
import com.yahoo.config.model.api.ModelFactory;
import com.yahoo.config.model.api.ValidationParameters;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.application.ApplicationSet;
import com.yahoo.vespa.config.server.application.PermanentApplicationPackage;
import com.yahoo.vespa.config.server.modelfactory.ModelFactoryRegistry;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.VespaModelFactory;
import org.junit.Before;
import org.junit.Test;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 * @author bratseth
 */
public class RemoteSessionTest {

    private static final TenantName tenantName = TenantName.from("default");

    private Curator curator;

    @Before
    public void setupTest() {
        curator = new MockCurator();
    }

    @Test
    public void require_that_session_is_initialized() {
        Clock clock = Clock.systemUTC();
        Session session = createSession(2, clock);
        assertThat(session.getSessionId(), is(2l));
        session = createSession(Long.MAX_VALUE, clock);
        assertThat(session.getSessionId(), is(Long.MAX_VALUE));
    }

    @Test
    public void require_that_applications_are_loaded() {
        RemoteSession session = createSession(3, Arrays.asList(new MockModelFactory(), new VespaModelFactory(new NullConfigModelRegistry())));
        session.loadPrepared();
        ApplicationSet applicationSet = session.ensureApplicationLoaded();
        assertNotNull(applicationSet);
        assertThat(applicationSet.getApplicationGeneration(), is(3l));
        assertThat(applicationSet.getForVersionOrLatest(Optional.empty(), Instant.now()).getId().application().value(), is("foo"));
        assertNotNull(applicationSet.getForVersionOrLatest(Optional.empty(), Instant.now()).getModel());
        session.deactivate();

        applicationSet = session.ensureApplicationLoaded();
        assertNotNull(applicationSet);
        assertThat(applicationSet.getApplicationGeneration(), is(3l));
        assertThat(applicationSet.getForVersionOrLatest(Optional.empty(), Instant.now()).getId().application().value(), is("foo"));
        assertNotNull(applicationSet.getForVersionOrLatest(Optional.empty(), Instant.now()).getModel());
    }

    @Test(expected = IllegalArgumentException.class)
    public void require_that_new_invalid_application_throws_exception() {
        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(1, 2, 0);
        failingFactory.throwOnLoad = true;

        MockModelFactory okFactory = new MockModelFactory();
        okFactory.vespaVersion = new Version(1, 1, 0);
        okFactory.throwOnLoad = false;

        RemoteSession session = createSession(3, Arrays.asList(okFactory, failingFactory));
        session.loadPrepared();
    }

    @Test
    public void require_that_application_incompatible_with_latestmajor_is_loaded_on_earlier_major() {
        MockModelFactory okFactory1 = new MockModelFactory();
        okFactory1.vespaVersion = new Version(1, 1, 0);
        okFactory1.throwOnLoad = false;

        MockModelFactory okFactory2 = new MockModelFactory();
        okFactory2.vespaVersion = new Version(1, 2, 0);
        okFactory2.throwOnLoad = false;

        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(2, 0, 0);
        failingFactory.throwOnLoad = true;

        RemoteSession session = createSession(3, Arrays.asList(okFactory1, failingFactory, okFactory2));
        session.loadPrepared();
    }

    @Test
    public void require_that_old_invalid_application_does_not_throw_exception_if_skipped() {
        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(1, 1, 0);
        failingFactory.throwOnLoad = true;

        MockModelFactory okFactory =
            new MockModelFactory("<validation-overrides><allow until='2000-01-30'>skip-old-config-models</allow></validation-overrides>");
        okFactory.vespaVersion = new Version(1, 2, 0);
        okFactory.throwOnLoad = false;

        RemoteSession session = createSession(3, Arrays.asList(okFactory, failingFactory));
        session.loadPrepared();
    }

    @Test
    public void require_that_old_invalid_application_does_not_throw_exception_if_skipped_also_across_major_versions() {
        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(1, 0, 0);
        failingFactory.throwOnLoad = true;

        MockModelFactory okFactory =
                new MockModelFactory("<validation-overrides><allow until='2000-01-30'>skip-old-config-models</allow></validation-overrides>");
        okFactory.vespaVersion = new Version(2, 0, 0);
        okFactory.throwOnLoad = false;

        RemoteSession session = createSession(3, Arrays.asList(okFactory, failingFactory), failingFactory.clock());
        session.loadPrepared();
    }

    @Test
    public void require_that_old_invalid_application_does_not_throw_exception_if_skipped_also_when_new_major_is_incompatible() {
        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(1, 0, 0);
        failingFactory.throwOnLoad = true;

        MockModelFactory okFactory =
                new MockModelFactory("<validation-overrides><allow until='2000-01-30'>skip-old-config-models</allow></validation-overrides>");
        okFactory.vespaVersion = new Version(1, 1, 0);
        okFactory.throwOnLoad = false;

        MockModelFactory tooNewFactory =
                new MockModelFactory("<validation-overrides><allow until='2000-01-30'>skip-old-config-models</allow></validation-overrides>");
        tooNewFactory.vespaVersion = new Version(2, 0, 0);
        tooNewFactory.throwOnLoad = true;

        RemoteSession session = createSession(3, Arrays.asList(tooNewFactory, okFactory, failingFactory));
        session.loadPrepared();
    }

    @Test
    public void require_that_an_application_package_can_limit_to_one_major_version() {
        ApplicationPackage application =
                new MockApplicationPackage.Builder().withServices("<services version='1.0'/>")
                                                    .withDeploymentSpec("<deployment version='1.0' major-version='2'/>")
                                                    .build();
        assertTrue(application.getMajorVersion().isPresent());
        assertEquals(2, (int)application.getMajorVersion().get());

        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(3, 0, 0);
        failingFactory.throwErrorOnLoad = true;

        MockModelFactory okFactory = new MockModelFactory();
        okFactory.vespaVersion = new Version(2, 0, 0);
        okFactory.throwErrorOnLoad = false;

        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, 3, application);
        RemoteSession session = createSession(3, zkc, Arrays.asList(okFactory, failingFactory));
        session.loadPrepared();

        // Does not cause an error because model version 3 is skipped
    }

    @Test
    public void require_that_an_application_package_can_limit_to_one_higher_major_version() {
        ApplicationPackage application =
                new MockApplicationPackage.Builder().withServices("<services version='1.0'/>")
                                                    .withDeploymentSpec("<deployment version='1.0' major-version='3'/>")
                                                    .build();
        assertTrue(application.getMajorVersion().isPresent());
        assertEquals(3, (int)application.getMajorVersion().get());

        MockModelFactory failingFactory = new MockModelFactory();
        failingFactory.vespaVersion = new Version(4, 0, 0);
        failingFactory.throwErrorOnLoad = true;

        MockModelFactory okFactory = new MockModelFactory();
        okFactory.vespaVersion = new Version(2, 0, 0);
        okFactory.throwErrorOnLoad = false;

        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, 3, application);
        RemoteSession session = createSession(4, zkc, Arrays.asList(okFactory, failingFactory));
        session.loadPrepared();

        // Does not cause an error because model version 4 is skipped
    }

    @Test
    public void require_that_session_status_is_updated() {
        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, 3);
        RemoteSession session = createSession(3, zkc, Clock.systemUTC());
        assertThat(session.getStatus(), is(Session.Status.NEW));
        zkc.writeStatus(Session.Status.PREPARE);
        assertThat(session.getStatus(), is(Session.Status.PREPARE));
    }

    @Test
    public void require_that_permanent_app_is_used() {
        Optional<PermanentApplicationPackage> permanentApp = Optional.of(new PermanentApplicationPackage(
                new ConfigserverConfig(new ConfigserverConfig.Builder().applicationDirectory(Files.createTempDir().getAbsolutePath()))));
        MockModelFactory mockModelFactory = new MockModelFactory();
        try {
            int sessionId = 3;
            SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, sessionId);
            createSession(sessionId, zkc, Collections.singletonList(mockModelFactory), permanentApp, Clock.systemUTC()).ensureApplicationLoaded();
        } catch (Exception e) {
            e.printStackTrace();
            // ignore, we're not interested in deploy errors as long as the below state is OK.
        }
        assertNotNull(mockModelFactory.modelContext);
        assertTrue(mockModelFactory.modelContext.permanentApplicationPackage().isPresent());
    }

    private RemoteSession createSession(long sessionId) {
        return createSession(sessionId, Collections.singletonList(new VespaModelFactory(new NullConfigModelRegistry())), Clock.systemUTC());
    }

    private RemoteSession createSession(long sessionId, Clock clock) {
        return createSession(sessionId, Collections.singletonList(new VespaModelFactory(new NullConfigModelRegistry())), clock);
    }

    private RemoteSession createSession(long sessionId, SessionZooKeeperClient zkc, Clock clock) {
        return createSession(sessionId, zkc, Collections.singletonList(new VespaModelFactory(new NullConfigModelRegistry())), clock);
    }

    private RemoteSession createSession(long sessionId, List<ModelFactory> modelFactories) {
        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, sessionId);
        return createSession(sessionId, zkc, modelFactories, Clock.systemUTC());
    }

    private RemoteSession createSession(long sessionId, List<ModelFactory> modelFactories, Clock clock) {
        SessionZooKeeperClient zkc = new MockSessionZKClient(curator, tenantName, sessionId);
        return createSession(sessionId, zkc, modelFactories, clock);
    }

    private RemoteSession createSession(long sessionId, SessionZooKeeperClient zkc, List<ModelFactory> modelFactories) {
        return createSession(sessionId, zkc, modelFactories, Optional.empty(), Clock.systemUTC());
    }

    private RemoteSession createSession(long sessionId, SessionZooKeeperClient zkc, List<ModelFactory> modelFactories, Clock clock) {
        return createSession(sessionId, zkc, modelFactories, Optional.empty(), clock);
    }

    private RemoteSession createSession(long sessionId, SessionZooKeeperClient zkc,
                                        List<ModelFactory> modelFactories,
                                        Optional<PermanentApplicationPackage> permanentApplicationPackage,
                                        Clock clock) {
        zkc.writeStatus(Session.Status.NEW);
        zkc.writeApplicationId(new ApplicationId.Builder().applicationName("foo").instanceName("bim").build());
        TestComponentRegistry.Builder registryBuilder = new TestComponentRegistry.Builder()
                .curator(curator)
                .clock(clock)
                .modelFactoryRegistry(new ModelFactoryRegistry(modelFactories));
        if (permanentApplicationPackage.isPresent())
            registryBuilder.permanentApplicationPackage(permanentApplicationPackage.get());


        return new RemoteSession(tenantName, sessionId, registryBuilder.build(), zkc);
    }

    private class MockModelFactory implements ModelFactory {

        /** Throw a RuntimeException on load - this is handled gracefully during model building */
        public boolean throwOnLoad = false;

        /** Throw an Error on load - this is useful to propagate this condition all the way to the test */
        public boolean throwErrorOnLoad = false;

        public ModelContext modelContext;
        public Version vespaVersion = new Version(1, 2, 3);

        /** The validation overrides of this, or null if none */
        private final String validationOverrides;
        
        private Clock clock = Clock.fixed(LocalDate.parse("2000-01-01", DateTimeFormatter.ISO_DATE).atStartOfDay().atZone(ZoneOffset.UTC).toInstant(), ZoneOffset.UTC);

        public MockModelFactory() { this(null); }

        public MockModelFactory(String validationOverrides) {
            this.validationOverrides = validationOverrides;
        }

        @Override
        public Version version() {
            return vespaVersion;
        }
        
        /** Returns the clock used by this, which is fixed at the instant 2000-01-01T00:00:00 */
        public Clock clock() { return clock; }

        @Override
        public Model createModel(ModelContext modelContext) {
            if (throwErrorOnLoad)
                throw new Error("Foo");
            if (throwOnLoad)
                throw new IllegalArgumentException("Foo");
            this.modelContext = modelContext;
            return loadModel();
        }

        public Model loadModel() {
            try {
                ApplicationPackage application = new MockApplicationPackage.Builder().withEmptyHosts().withEmptyServices().withValidationOverrides(validationOverrides).build();
                DeployState deployState = new DeployState.Builder().applicationPackage(application).now(clock.instant()).build();
                return new VespaModel(deployState);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public ModelCreateResult createAndValidateModel(ModelContext modelContext, ValidationParameters validationParameters) {
            if (throwErrorOnLoad)
                throw new Error("Foo");
            if (throwOnLoad)
                throw new IllegalArgumentException("Foo");
            this.modelContext = modelContext;
            return new ModelCreateResult(loadModel(), new ArrayList<>());
        }
    }

}