aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/config/model/ApplicationConfigProducerRoot.java
blob: f324ceef5ab8b77bd9ee02b42ad6b6a0a1e80893 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model;

import com.yahoo.cloud.config.ApplicationIdConfig;
import com.yahoo.cloud.config.ClusterListConfig;
import com.yahoo.cloud.config.ModelConfig;
import com.yahoo.cloud.config.ModelConfig.Hosts;
import com.yahoo.cloud.config.ModelConfig.Hosts.Services;
import com.yahoo.cloud.config.ModelConfig.Hosts.Services.Ports;
import com.yahoo.cloud.config.SlobroksConfig;
import com.yahoo.cloud.config.ZookeepersConfig;
import com.yahoo.cloud.config.log.LogdConfig;
import com.yahoo.component.Version;
import com.yahoo.config.model.api.ModelContext;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.producer.AnyConfigProducer;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.document.config.DocumenttypesConfig;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.documentapi.messagebus.protocol.DocumentrouteselectorpolicyConfig;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocolPoliciesConfig;
import com.yahoo.messagebus.MessagebusConfig;
import com.yahoo.vespa.config.content.AllClustersBucketSpacesConfig;
import com.yahoo.vespa.config.content.DistributionConfig;
import com.yahoo.vespa.configmodel.producers.DocumentManager;
import com.yahoo.vespa.configmodel.producers.DocumentTypes;
import com.yahoo.vespa.documentmodel.DocumentModel;
import com.yahoo.vespa.model.ConfigProducer;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.HostSystem;
import com.yahoo.vespa.model.PortsMeta;
import com.yahoo.vespa.model.Service;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.admin.Admin;
import com.yahoo.vespa.model.content.cluster.ContentCluster;
import com.yahoo.vespa.model.routing.Routing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * This is the parent of all ConfigProducers in the system resulting from configuring an application.
 *
 * @author gjoranv
 */
public class ApplicationConfigProducerRoot extends TreeConfigProducer<AnyConfigProducer> implements CommonConfigsProducer {

    private final DocumentModel documentModel;
    private Routing routing = null;
    // The ConfigProducers contained in this Vespa. (configId->producer)
    Map<String, ConfigProducer> id2producer = new LinkedHashMap<>();
    private Admin admin = null;
    private HostSystem hostSystem = null;
    private final Version vespaVersion;
    private final ApplicationId applicationId;

    /**
     * Creates and initializes a new Vespa from the service config file
     * in the given application directory.
     *
     * @param parent the parent, usually VespaModel
     * @param name   the name, used as configId
     * @param documentModel DocumentModel to serve global document config from.
     */
    public ApplicationConfigProducerRoot(TreeConfigProducer<AnyConfigProducer> parent,
                                         String name, DocumentModel documentModel, Version vespaVersion, ApplicationId applicationId) {
        super(parent, name);
        this.documentModel = documentModel;
        this.vespaVersion = vespaVersion;
        this.applicationId = applicationId;
    }

    private boolean useV8GeoPositions = false;

    public void useFeatureFlags(ModelContext.FeatureFlags featureFlags) {
        this.useV8GeoPositions = featureFlags.useV8GeoPositions();
    }

    /**
     * @return an unmodifiable copy of the set of configIds in this VespaModel.
     */
    public Set<String> getConfigIds() {
        return Collections.unmodifiableSet(id2producer.keySet());
    }

    /**
     * Returns the ConfigProducer with the given id, or null if no such
     * configId exists.
     *
     * @param configId The configId, e.g. "search.0/tld.0"
     * @return ConfigProducer with the given configId
     */
    public ConfigProducer getConfigProducer(String configId) {
        return id2producer.get(configId);
    }

    /**
     * Adds the descendant (at any depth level), so it can be looked up
     * on configId in the Map.
     *
     * @param descendant The configProducer descendant to add
     */
    // TODO: Make protected if this moves to the same package as TreeConfigProducer
    public void addDescendant(AnyConfigProducer descendant) {
        id2producer.put(descendant.getConfigId(), descendant);
    }

    /**
     * Prepares the model for start. The {@link VespaModel} calls
     * this methods after it has loaded this and all plugins have been loaded and
     * their initialize() methods have been called.
     *
     * @param plugins All initialized plugins of the vespa model.
     */
    public void prepare(ConfigModelRepo plugins) {
        if (routing != null) {
            routing.deriveCommonSettings(plugins);
        }
    }

    public void setupAdmin(Admin admin) {
        this.admin = admin;
    }

    // TODO: Do this as another config model depending on the other models
    public void setupRouting(DeployState deployState, VespaModel vespaModel, ConfigModelRepo configModels) {
        if (admin != null) {
            Routing routing = configModels.getRouting();
            if (routing == null) {
                routing = new Routing(ConfigModelContext.create(deployState, vespaModel, configModels, this, "routing"));
                configModels.add(routing);
            }
            this.routing = routing;
        }
    }

    @Override
    public void getConfig(DocumentmanagerConfig.Builder builder) {
        new DocumentManager()
            .useV8GeoPositions(this.useV8GeoPositions)
            .produce(documentModel, builder);
    }

    @Override
    public void getConfig(DocumenttypesConfig.Builder builder) {
        new DocumentTypes()
            .useV8GeoPositions(this.useV8GeoPositions)
            .produce(documentModel, builder);
    }

    @Override
    public void getConfig(DocumentrouteselectorpolicyConfig.Builder builder) {
        if (routing != null) {
            routing.getConfig(builder);
        }
    }

    @Override
    public void getConfig(DocumentProtocolPoliciesConfig.Builder builder) {
        if (routing != null) {
            routing.getConfig(builder);
        }
    }

    @Override
    public void getConfig(MessagebusConfig.Builder builder) {
        if (routing != null) {
            routing.getConfig(builder);
        }
    }

    @Override
    public void getConfig(LogdConfig.Builder builder) {
        if (admin != null) {
            admin.getConfig(builder);
        }
    }

    @Override
    public void getConfig(SlobroksConfig.Builder builder) {
        if (admin != null) {
            admin.getConfig(builder);
        }
    }

    @Override
    public void getConfig(ZookeepersConfig.Builder builder) {
        if (admin != null) {
            admin.getConfig(builder);
        }
    }

    @Override
    public void getConfig(ClusterListConfig.Builder builder) {
        VespaModel model = (VespaModel) getRoot();
        for (ContentCluster cluster : model.getContentClusters().values()) {
            ClusterListConfig.Storage.Builder storage = new ClusterListConfig.Storage.Builder();
            storage.name(cluster.getName());
            storage.configid(cluster.getConfigId());
            builder.storage(storage);
        }
    }

    @Override
    public void getConfig(DistributionConfig.Builder builder) {
        for (ContentCluster cluster : ((VespaModel) getRoot()).getContentClusters().values()) {
            cluster.getConfig(builder);
        }
    }

    @Override
    public void getConfig(AllClustersBucketSpacesConfig.Builder builder) {
        VespaModel model = (VespaModel) getRoot();
        for (ContentCluster cluster : model.getContentClusters().values()) {
            builder.cluster(cluster.getName(), cluster.clusterBucketSpaceConfigBuilder());
        }
    }

    @Override
    public void getConfig(ModelConfig.Builder builder) {
        builder.vespaVersion(vespaVersion.toFullString());
        for (HostResource modelHost : hostSystem().getHosts()) {
            builder.hosts(new Hosts.Builder()
                    .name(modelHost.getHostname())
                    .services(getServices(modelHost))
            );
        }
    }

    // add cluster type?
    // add cluster name?
    public record StatePortInfo(String hostName, int portNumber,
                                String serviceName, String serviceType)
    {}

    public List<StatePortInfo> getStatePorts() {
        List<StatePortInfo> result = new ArrayList<>();
        for (HostResource modelHost : hostSystem().getHosts()) {
            String hostName = modelHost.getHostname();
            for (Service modelService : modelHost.getServices()) {
                String serviceName = modelService.getServiceName();
                String serviceType = modelService.getServiceType();
                PortsMeta portsMeta = modelService.getPortsMeta();
                for (int i = 0; i < portsMeta.getNumPorts(); i++) {
                    int portNumber = modelService.getRelativePort(i);
                    boolean hasState = false;
                    boolean isHttp = false;
                    for (String tag : portsMeta.getTagsAt(i)) {
                        if (tag.equals("state")) hasState = true;
                        if (tag.equals("http")) isHttp = true;
                    }
                    if (hasState && isHttp) {
                        result.add(new StatePortInfo(hostName, portNumber, serviceName, serviceType));
                    }
                }
            }
        }
        return result;
    }

    private List<Services.Builder> getServices(HostResource modelHost) {
        List<Services.Builder> ret = new ArrayList<>();
        for (Service modelService : modelHost.getServices()) {
            ret.add(new Services.Builder()
                    .name(modelService.getServiceName())
                    .type(modelService.getServiceType())
                    .configid(modelService.getConfigId())
                    .clustertype(modelService.getServicePropertyString("clustertype", ""))
                    .clustername(modelService.getServicePropertyString("clustername", ""))
                    .index(Integer.parseInt(modelService.getServicePropertyString("index", "999999")))
                    .ports(getPorts(modelService))
            );
        }
        return ret;
    }

    private List<Ports.Builder> getPorts(Service modelService) {
        List<Ports.Builder> ret = new ArrayList<>();
        PortsMeta portsMeta = modelService.getPortsMeta();
        for (int i = 0; i < portsMeta.getNumPorts(); i++) {
            ret.add(new Ports.Builder()
                    .number(modelService.getRelativePort(i))
                    .tags(getPortTags(portsMeta, i))
            );
        }
        return ret;
    }

    public static String getPortTags(PortsMeta portsMeta, int portNumber) {
        StringBuilder sb = new StringBuilder();
        boolean firstTag = true;
        for (String s : portsMeta.getTagsAt(portNumber)) {
            if (!firstTag) {
                sb.append(" ");
            } else {
                firstTag = false;
            }
            sb.append(s);
        }
        return sb.toString();
    }

    public void setHostSystem(HostSystem hostSystem) {
        this.hostSystem = hostSystem;
    }

    @Override
    public HostSystem hostSystem() {
        return hostSystem;
    }

    public Admin getAdmin() {
        return admin;
    }

    @Override
    public void getConfig(ApplicationIdConfig.Builder builder) {
        builder.tenant(applicationId.tenant().value());
        builder.application(applicationId.application().value());
        builder.instance(applicationId.instance().value());
    }

}