summaryrefslogtreecommitdiffstats
path: root/standalone-container/src/main/java/com/yahoo/container/standalone/StandaloneContainerApplication.java
blob: 06e49843eacbbca0b272586b2480807dc97aba65 (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.standalone;

import com.google.inject.AbstractModule;
import com.google.inject.ConfigurationException;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.ProvisionException;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.yahoo.collections.Pair;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.model.ApplicationConfigProducerRoot;
import com.yahoo.config.model.ConfigModelRepo;
import com.yahoo.config.model.api.ConfigServerSpec;
import com.yahoo.config.model.api.ModelContext;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.application.provider.StaticConfigDefinitionRepo;
import com.yahoo.config.model.builder.xml.ConfigModelId;
import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Rotation;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.container.di.config.SubscriberFactory;
import com.yahoo.container.jdisc.ConfiguredApplication;
import com.yahoo.io.IOUtils;
import com.yahoo.jdisc.application.Application;
import com.yahoo.text.XML;
import com.yahoo.vespa.defaults.Defaults;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder;
import com.yahoo.vespa.model.container.Container;
import com.yahoo.vespa.model.container.ContainerModel;
import com.yahoo.vespa.model.container.xml.ConfigServerContainerModelBuilder;
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder;
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder.Networking;
import org.w3c.dom.Element;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yahoo.collections.CollectionUtil.first;

/**
 * @author Tony Vaagenes
 * @author gjoranv
 * @author ollivir
 */
public class StandaloneContainerApplication implements Application {

    public static final String PACKAGE_NAME = "standalone_jdisc_container";
    public static final String APPLICATION_LOCATION_INSTALL_VARIABLE = PACKAGE_NAME + ".app_location";
    public static final String DEPLOYMENT_PROFILE_INSTALL_VARIABLE = PACKAGE_NAME + ".deployment_profile";
    public static final String DISABLE_NETWORKING_ANNOTATION = "JDisc.disableNetworking";
    public static final Named APPLICATION_PATH_NAME = Names.named(APPLICATION_LOCATION_INSTALL_VARIABLE);
    public static final Named CONFIG_MODEL_REPO_NAME = Names.named("ConfigModelRepo");

    private static final String DEFAULT_TMP_BASE_DIR = Defaults.getDefaults().underVespaHome("tmp");
    private static final String TMP_DIR_NAME = "standalone_container";

    private static final StaticConfigDefinitionRepo configDefinitionRepo = new StaticConfigDefinitionRepo();

    private final Injector injector;
    private final Path applicationPath;
    private final LocalFileDb distributedFiles;
    private final ConfigModelRepo configModelRepo;
    private final Networking networkingOption;
    private final VespaModel modelRoot;
    private final Application configuredApplication;
    private final Container container;

    @SuppressWarnings("WeakerAccess")
    @Inject
    public StandaloneContainerApplication(Injector injector) {
        this.injector = injector;
        ConfiguredApplication.ensureVespaLoggingInitialized();
        this.applicationPath = injectedApplicationPath().orElseGet(this::installApplicationPath);
        this.distributedFiles = new LocalFileDb(applicationPath);
        this.configModelRepo = resolveConfigModelRepo();
        this.networkingOption = resolveNetworkingOption();

        try {
            Pair<VespaModel, Container> tpl = withTempDir(preprocessedApplicationDir -> createContainerModel(applicationPath,
                    distributedFiles, preprocessedApplicationDir, networkingOption, configModelRepo));
            this.modelRoot = tpl.getFirst();
            this.container = tpl.getSecond();
        } catch (RuntimeException r) {
            throw r;
        } catch (Exception e) {
            throw new RuntimeException("Failed to create ContainerModel", e);
        }
        this.configuredApplication = createConfiguredApplication(container);
    }

    private ConfigModelRepo resolveConfigModelRepo() {
        try {
            return injector.getInstance(Key.get(ConfigModelRepo.class, CONFIG_MODEL_REPO_NAME));
        } catch (Exception e) {
            return new ConfigModelRepo();
        }
    }

    private Networking resolveNetworkingOption() {
        try {
            Boolean networkingDisable = injector.getInstance(Key.get(Boolean.class, Names.named(DISABLE_NETWORKING_ANNOTATION)));
            if (networkingDisable != null) {
                return networkingDisable ? Networking.disable : Networking.enable;
            }
        } catch (Exception ignored) {
        }
        return Networking.enable;
    }

    private Application createConfiguredApplication(Container container) {
        Injector augmentedInjector = injector.createChildInjector(new AbstractModule() {
            @Override
            public void configure() {
                bind(SubscriberFactory.class).toInstance(new StandaloneSubscriberFactory(modelRoot));
            }
        });

        System.setProperty("config.id", container.getConfigId());
        return augmentedInjector.getInstance(ConfiguredApplication.class);
    }

    private Optional<Path> injectedApplicationPath() {
        try {
            return Optional.ofNullable(injector.getInstance(Key.get(Path.class, APPLICATION_PATH_NAME)));
        } catch (ConfigurationException | ProvisionException ignored) {
        }
        return Optional.empty();
    }

    private Path installApplicationPath() {
        Optional<String> variable = optionalInstallVariable(APPLICATION_LOCATION_INSTALL_VARIABLE);

        return variable.map(Paths::get)
                .orElseThrow(() -> new IllegalStateException("Environment variable not set: " + APPLICATION_LOCATION_INSTALL_VARIABLE));
    }

    @Override
    public void start() {
        try {
            com.yahoo.container.Container.get().setCustomFileAcquirer(distributedFiles);
            configuredApplication.start();
        } catch (Exception e) {
            com.yahoo.container.Container.resetInstance();
            throw e;
        }
    }

    @Override
    public void stop() {
        configuredApplication.stop();
    }

    @Override
    public void destroy() {
        com.yahoo.container.Container.resetInstance();
        configuredApplication.destroy();
    }

    public Container container() {
        return container;
    }

    private interface ThrowingFunction<T, U> {
        U apply(T input) throws Exception;
    }

    private static <T> T withTempDir(ThrowingFunction<File, T> f) throws Exception {
        File tmpDir = createTempDir();
        try {
            return f.apply(tmpDir);
        } finally {
            IOUtils.recursiveDeleteDir(tmpDir);
        }
    }

    private static File createTempDir() {
        Path basePath;
        if (new File(DEFAULT_TMP_BASE_DIR).exists()) {
            basePath = Paths.get(DEFAULT_TMP_BASE_DIR);
        } else {
            basePath = Paths.get(System.getProperty("java.io.tmpdir"));
        }

        try {
            Path tmpDir = Files.createTempDirectory(basePath, TMP_DIR_NAME);
            return tmpDir.toFile();
        } catch (IOException e) {
            throw new RuntimeException("Cannot create temp directory", e);
        }
    }

    private static void validateApplication(ApplicationPackage applicationPackage) {
        try {
            applicationPackage.validateXML();
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    private static ContainerModelBuilder newContainerModelBuilder(Networking networkingOption, CloudConfigInstallVariables cloudConfigInstallVariables) {
        return isConfigServer() ?
                new ConfigServerContainerModelBuilder(cloudConfigInstallVariables) :
                new ContainerModelBuilder(true, networkingOption);
    }

    private static boolean isConfigServer() {
        Optional<String> profile = optionalInstallVariable(DEPLOYMENT_PROFILE_INSTALL_VARIABLE);
        if (profile.isPresent()) {
            String profileName = profile.get();
            if (profileName.equals("configserver"))
                return true;
            else
                throw new RuntimeException("Invalid deployment profile '" + profileName + "'");
        }

        return false;
    }

    static Pair<VespaModel, Container> createContainerModel(Path applicationPath, FileRegistry fileRegistry,
            File preprocessedApplicationDir, Networking networkingOption, ConfigModelRepo configModelRepo) throws Exception {
        DeployLogger logger = new BaseDeployLogger();
        FilesApplicationPackage rawApplicationPackage = new FilesApplicationPackage.Builder(applicationPath.toFile())
                .includeSourceFiles(true).preprocessedDir(preprocessedApplicationDir).build();
        ApplicationPackage applicationPackage = rawApplicationPackage.preprocess(getZone(), logger);
        validateApplication(applicationPackage);
        CloudConfigInstallVariables cloudConfigInstallVariables = new CloudConfigInstallVariables();
        DeployState deployState = createDeployState(applicationPackage, fileRegistry, logger, cloudConfigInstallVariables);

        VespaModel root = VespaModel.createIncomplete(deployState);
        ApplicationConfigProducerRoot vespaRoot = new ApplicationConfigProducerRoot(root, "vespa", deployState.getDocumentModel(),
                deployState.getVespaVersion(), deployState.getProperties().applicationId());

        Element spec = containerRootElement(applicationPackage);
        ContainerModel containerModel = newContainerModelBuilder(networkingOption, cloudConfigInstallVariables)
                .build(deployState, root, configModelRepo, vespaRoot, spec);
        containerModel.getCluster().prepare(deployState);
        initializeContainerModel(containerModel, configModelRepo);
        Container container = first(containerModel.getCluster().getContainers());

        // TODO: Separate out model finalization from the VespaModel constructor,
        // such that the above and below code to finalize the container can be
        // replaced by root.finalize();

        initializeContainer(deployState.getDeployLogger(), container, spec);

        root.freezeModelTopology();
        return new Pair<>(root, container);
    }

    private static Zone getZone() {
        if (!isConfigServer()) {
            return Zone.defaultZone();
        }
        CloudConfigInstallVariables cloudConfigVariables = new CloudConfigInstallVariables();
        if (!cloudConfigVariables.hostedVespa().orElse(false)) {
            return Zone.defaultZone();
        }
        RegionName region = cloudConfigVariables.region().map(RegionName::from).orElseGet(RegionName::defaultName);
        Environment environment = cloudConfigVariables.environment().map(Environment::from).orElseGet(Environment::defaultEnvironment);
        SystemName system = cloudConfigVariables.system().map(SystemName::from).orElseGet(SystemName::defaultSystem);
        return new Zone(system, environment, region);
    }

    private static DeployState createDeployState(ApplicationPackage applicationPackage, FileRegistry fileRegistry,
                                                 DeployLogger logger, CloudConfigInstallVariables cloudConfigInstallVariables) {
        DeployState.Builder builder = new DeployState.Builder()
                .applicationPackage(applicationPackage)
                .fileRegistry(fileRegistry)
                .deployLogger(logger)
                .configDefinitionRepo(configDefinitionRepo)
                .properties(new StandaloneContainerModelContextProperties(cloudConfigInstallVariables));

        return builder.build();
    }

    private static void initializeContainer(DeployLogger deployLogger, Container container, Element spec) {
        HostResource host = container.getRoot().getHostSystem().getHost(Container.SINGLENODE_CONTAINER_SERVICESPEC);

        container.setBasePort(VespaDomBuilder.getXmlWantedPort(spec));
        container.setHostResource(host);
        container.initService(deployLogger);
    }

    private static Element getJDiscInServices(Element element) {
        List<Element> jDiscElements = new ArrayList<>();
        for (ConfigModelId cid : ContainerModelBuilder.configModelIds) {
            List<Element> children = XML.getChildren(element, cid.getName());
            jDiscElements.addAll(children);
        }
        
        if (jDiscElements.size() == 1) {
            return jDiscElements.get(0);
        } else if (jDiscElements.isEmpty()) {
            throw new RuntimeException("No jdisc element found under services.");
        } else {
            List<String> nameAndId = jDiscElements.stream().map(e -> e.getNodeName() + " id='" + e.getAttribute("id") + "'")
                    .collect(Collectors.toList());
            throw new RuntimeException("Found multiple JDisc elements: " + String.join(", ", nameAndId));
        }
    }

    private static Element containerRootElement(ApplicationPackage applicationPackage) {
        Element element = XmlHelper.getDocument(applicationPackage.getServices()).getDocumentElement();
        String nodeName = element.getNodeName();

        if (ContainerModelBuilder.configModelIds.stream().anyMatch(id -> id.getName().equals(nodeName))) {
            return element;
        } else {
            return getJDiscInServices(element);
        }
    }

    private static void initializeContainerModel(ContainerModel containerModel, ConfigModelRepo configModelRepo) {
        containerModel.initialize(configModelRepo);
    }

    private static Optional<String> optionalInstallVariable(String name) {
        Optional<String> fromEnv = Optional.ofNullable(System.getenv((name.replace(".", "__"))));
        if (fromEnv.isPresent()) {
            return fromEnv;
        }
        return Optional.ofNullable(System.getProperty(name)); // for unit testing
    }

    private static class StandaloneContainerModelContextProperties implements ModelContext.Properties {
        private final CloudConfigInstallVariables cloudConfigInstallVariables;

        StandaloneContainerModelContextProperties(CloudConfigInstallVariables cloudConfigInstallVariables) {
            this.cloudConfigInstallVariables = cloudConfigInstallVariables;
        }

        @Override
        public boolean multitenant() {
            return cloudConfigInstallVariables.multiTenant().orElse(Boolean.FALSE);
        }

        @Override
        public ApplicationId applicationId() {
            return ApplicationId.defaultId();
        }

        @Override
        public List<ConfigServerSpec> configServerSpecs() {
            throw new UnsupportedOperationException();
        }

        @Override
        public HostName loadBalancerName() {
            throw new UnsupportedOperationException();
        }

        @Override
        public URI ztsUrl() {
            throw new UnsupportedOperationException();
        }

        @Override
        public String athenzDnsSuffix() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean hostedVespa() {
            return cloudConfigInstallVariables.hostedVespa().orElse(Boolean.FALSE);
        }

        @Override
        public Zone zone() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Set<Rotation> rotations() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isBootstrap() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isFirstTimeDeployment() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean useDedicatedNodeForLogserver() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean useFdispatchByDefault() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean useAdaptiveDispatch() {
            throw new UnsupportedOperationException();
        }
    }

}