aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/deploy/ZooKeeperDeployer.java
blob: f0228f40ad224282f7c507643fa3ae2e98f75c04 (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
// 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.deploy;

import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.config.application.api.ApplicationMetaData;
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.application.api.UnparsedConfigDefinition;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.serialization.AllocatedHostsSerializer;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.path.Path;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.server.filedistribution.FileDBRegistry;
import com.yahoo.vespa.config.server.zookeeper.ZKApplicationPackage;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.yolean.Exceptions;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import static com.yahoo.config.application.api.ApplicationPackage.DEPLOYMENT_FILE;
import static com.yahoo.config.application.api.ApplicationPackage.DOCPROCCHAINS_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.HOSTS;
import static com.yahoo.config.application.api.ApplicationPackage.MODELS_GENERATED_REPLICATED_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.PAGE_TEMPLATES_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.QUERY_PROFILES_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.ROUTINGTABLES_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.RULES_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.SCHEMAS_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.SEARCHCHAINS_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.SECURITY_DIR;
import static com.yahoo.config.application.api.ApplicationPackage.SERVICES;
import static com.yahoo.config.application.api.ApplicationPackage.VALIDATION_OVERRIDES;
import static com.yahoo.vespa.config.server.session.SessionZooKeeperClient.getSessionPath;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.DEFCONFIGS_ZK_SUBPATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.META_ZK_PATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.USERAPP_ZK_SUBPATH;
import static com.yahoo.vespa.config.server.zookeeper.ZKApplication.USER_DEFCONFIGS_ZK_SUBPATH;

/**
 * Interface for initializing zookeeper and deploying an application package to zookeeper.
 *
 * @author Ulf Lilleengen
 */
public class ZooKeeperDeployer {

    private final Client client;

    public ZooKeeperDeployer(Curator curator, DeployLogger logger, ApplicationId applicationId, long sessionId) {
        Path sessionPath = getSessionPath(applicationId.tenant(), sessionId);
        this.client = new Client(curator, logger, sessionPath);
    }

    /**
     * Deploys an application package to zookeeper
     *
     * @param applicationPackage the application package to persist.
     * @param fileRegistryMap the file registries to persist.
     * @param allocatedHosts the allocated hosts to persist.
     * @throws IOException if deploying fails
     */
    public void deploy(ApplicationPackage applicationPackage, Map<Version, FileRegistry> fileRegistryMap, 
                       AllocatedHosts allocatedHosts) throws IOException {
        client.initialize();
        client.writeApplicationPackage(applicationPackage);
        client.write(fileRegistryMap);
        client.write(allocatedHosts);
    }

    public void cleanup() {
        client.cleanupZooKeeper();
    }

    /**
     * Reads and writes application package to and from ZooKeeper.
     *
     * @author hmusum
     */
    public static class Client {

        private final Curator curator;
        private final DeployLogger logger;
        private final Path sessionPath; // session id

        private static final ApplicationFile.PathFilter xmlFilter = path -> path.getName().endsWith(".xml");

        public Client(Curator curator, DeployLogger logger, Path sessionPath) {
            this.curator = curator;
            this.logger = logger;
            this.sessionPath = sessionPath;
        }

        /**
         * Sets up basic node structure in ZooKeeper and purges old data.
         * This is the first operation on ZK during deploy.
         */
        void initialize() {
            curator.create(sessionPath);

            for (String subPath : Arrays.asList(DEFCONFIGS_ZK_SUBPATH,
                                                USER_DEFCONFIGS_ZK_SUBPATH,
                                                USERAPP_ZK_SUBPATH,
                                                ZKApplicationPackage.fileRegistryNode)) {
                // TODO: The replaceFirst below is hackish.
                curator.create(getZooKeeperAppPath().append(subPath.replaceFirst("/", "")));
            }
        }

        /**
         * Writes def files and user config into ZK.
         *
         * @param app the application package to feed to zookeeper
         */
        void writeApplicationPackage(ApplicationPackage app) {
            try {
                writeUserDefs(app);
                writeSomeOf(app);
                writeSchemas(app);
                writeUserIncludeDirs(app, app.getUserIncludeDirs());
                writeMetadata(app.getMetaData());
            } catch (Exception e) {
                throw new IllegalStateException("Unable to write vespa model to config server(s) " + System.getProperty("configsources") + "\n" +
                                                "Please ensure that config server is started " +
                                                "and check the vespa log for configserver errors. ", e);
            }
        }

        private void writeSchemas(ApplicationPackage app) throws IOException {
            Collection<NamedReader> schemas = app.getSchemas();
            if (schemas.isEmpty()) return;

            Path zkPath = getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(SCHEMAS_DIR);
            curator.create(zkPath);
            // Ensures that ranking expressions and other files are also written
            writeDir(app.getFile(ApplicationPackage.SEARCH_DEFINITIONS_DIR), zkPath);
            writeDir(app.getFile(ApplicationPackage.SCHEMAS_DIR), zkPath);
            for (NamedReader sd : schemas) {
                curator.set(zkPath.append(sd.getName()), Utf8.toBytes(com.yahoo.io.IOUtils.readAll(sd.getReader())));
                sd.getReader().close();
            }
        }

        /**
         * Writes some application package files into ZK - see write(app).
         *
         * @param app the application package to use as input.
         * @throws IOException if not able to write to Zookeeper
         */
        private void writeSomeOf(ApplicationPackage app) throws IOException {
            // TODO: We should have a way of doing this which doesn't require repeating all the content
            writeFile(app.getFile(Path.fromString(SERVICES)), getZooKeeperAppPath(USERAPP_ZK_SUBPATH));
            writeFile(app.getFile(Path.fromString(HOSTS)), getZooKeeperAppPath(USERAPP_ZK_SUBPATH));
            writeFile(app.getFile(Path.fromString(DEPLOYMENT_FILE.getName())), getZooKeeperAppPath(USERAPP_ZK_SUBPATH));
            writeFile(app.getFile(Path.fromString(VALIDATION_OVERRIDES.getName())), getZooKeeperAppPath(USERAPP_ZK_SUBPATH));
            writeDir(app.getFile(RULES_DIR),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(RULES_DIR),
                     (path) -> path.getName().endsWith(ApplicationPackage.RULES_NAME_SUFFIX));
            writeDir(app.getFile(QUERY_PROFILES_DIR),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(QUERY_PROFILES_DIR),
                     xmlFilter);
            writeDir(app.getFile(PAGE_TEMPLATES_DIR),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(PAGE_TEMPLATES_DIR),
                     xmlFilter);
            writeDir(app.getFile(Path.fromString(SEARCHCHAINS_DIR)),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(SEARCHCHAINS_DIR),
                     xmlFilter);
            writeDir(app.getFile(Path.fromString(DOCPROCCHAINS_DIR)),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(DOCPROCCHAINS_DIR),
                     xmlFilter);
            writeDir(app.getFile(Path.fromString(ROUTINGTABLES_DIR)),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(ROUTINGTABLES_DIR),
                     xmlFilter);
            writeDir(app.getFile(MODELS_GENERATED_REPLICATED_DIR),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(MODELS_GENERATED_REPLICATED_DIR));
            writeDir(app.getFile(SECURITY_DIR),
                     getZooKeeperAppPath(USERAPP_ZK_SUBPATH).append(SECURITY_DIR));
        }

        private void writeDir(ApplicationFile file, Path zooKeeperAppPath) throws IOException {
            writeDir(file, zooKeeperAppPath, (__) -> true);
        }

        private void writeDir(ApplicationFile dir, Path path, ApplicationFile.PathFilter filenameFilter) throws IOException {
            if ( ! dir.isDirectory()) return;
            for (ApplicationFile file : listFiles(dir, filenameFilter)) {
                String name = file.getPath().getName();
                if (name.startsWith(".")) continue; //.svn , .git ...
                if (file.isDirectory()) {
                    curator.create(path.append(name));
                    writeDir(file, path.append(name), filenameFilter);
                } else {
                    writeFile(file, path);
                }
            }
        }

        /**
         * Like {@link ApplicationFile#listFiles(ApplicationFile.PathFilter)}
         * with slightly different semantics: Never filter out directories.
         */
        private List<ApplicationFile> listFiles(ApplicationFile dir, ApplicationFile.PathFilter filter) {
            List<ApplicationFile> rawList = dir.listFiles();
            List<ApplicationFile> ret = new ArrayList<>();
            if (rawList != null) {
                for (ApplicationFile f : rawList) {
                    if (f.isDirectory()) {
                        ret.add(f);
                    } else {
                        if (filter.accept(f.getPath())) {
                            ret.add(f);
                        }
                    }
                }
            }
            return ret;
        }

        private void writeFile(ApplicationFile file, Path zkPath) throws IOException {
            if ( ! file.exists()) return;

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try (InputStream inputStream = file.createInputStream()) {
                inputStream.transferTo(baos);
                baos.flush();
                curator.set(zkPath.append(file.getPath().getName()), baos.toByteArray());
            }
        }

        private void writeUserIncludeDirs(ApplicationPackage applicationPackage, List<String> userIncludeDirs) throws IOException {
            for (String userInclude : userIncludeDirs) {
                ApplicationFile dir = applicationPackage.getFile(Path.fromString(userInclude));
                final List<ApplicationFile> files = dir.listFiles();
                if (files == null || files.isEmpty()) {
                    curator.create(getZooKeeperAppPath(USERAPP_ZK_SUBPATH + "/" + userInclude));
                }
                writeDir(dir, getZooKeeperAppPath(USERAPP_ZK_SUBPATH + "/" + userInclude), xmlFilter);
            }
        }

        /**
         * Feeds all user-defined .def file from the application package into ZooKeeper (both into
         * /defconfigs and /userdefconfigs
         */
        private void writeUserDefs(ApplicationPackage applicationPackage) {
            Map<ConfigDefinitionKey, UnparsedConfigDefinition> configDefs = applicationPackage.getAllExistingConfigDefs();
            for (Map.Entry<ConfigDefinitionKey, UnparsedConfigDefinition> entry : configDefs.entrySet()) {
                ConfigDefinitionKey key = entry.getKey();
                String contents = entry.getValue().getUnparsedContent();
                writeConfigDefinition(key.getName(), key.getNamespace(), getZooKeeperAppPath(USER_DEFCONFIGS_ZK_SUBPATH), contents);
                writeConfigDefinition(key.getName(), key.getNamespace(), getZooKeeperAppPath(DEFCONFIGS_ZK_SUBPATH), contents);
            }
            logger.log(Level.FINE, configDefs.size() + " user config definitions");
        }

        private void writeConfigDefinition(String name, String namespace, Path path, String data) {
            curator.set(path.append(namespace + "." + name), Utf8.toBytes(data));
        }

        private void write(Version vespaVersion, FileRegistry fileRegistry) {
            String exportedRegistry = FileDBRegistry.exportRegistry(fileRegistry);
            curator.set(getZooKeeperAppPath(ZKApplicationPackage.fileRegistryNode).append(vespaVersion.toFullString()),
                        Utf8.toBytes(exportedRegistry));
        }

        /**
         * Feeds application metadata to zookeeper. Used by config model to create config
         * for application metadata
         *
         * @param metaData The application metadata.
         */
        private void writeMetadata(ApplicationMetaData metaData) {
            curator.set(getZooKeeperAppPath(META_ZK_PATH), metaData.asJsonBytes());
        }

        void cleanupZooKeeper() {
            try {
                List.of(DEFCONFIGS_ZK_SUBPATH, USER_DEFCONFIGS_ZK_SUBPATH, USERAPP_ZK_SUBPATH)
                    .forEach(path -> curator.delete(getZooKeeperAppPath(path)));
            } catch (Exception e) {
                logger.log(Level.WARNING, "Could not clean up in zookeeper: " + Exceptions.toMessageString(e));
                // Might be called in an exception handler before re-throw, so do not throw here.
            }
        }

        /**
         * Gets a full ZK application path
         *
         * @return a String with the full ZK application path
         */
        private Path getZooKeeperAppPath() {
            return getZooKeeperAppPath(null);
        }

        /**
         * Gets a full ZK application path
         *
         * @param trailingPath trailing part of path to be appended to ZK app path
         * @return a String with the full ZK application path including trailing path, if set
         */
        private Path getZooKeeperAppPath(String trailingPath) {
            if (trailingPath == null) return sessionPath;

            return sessionPath.append(trailingPath);
        }

        public void write(AllocatedHosts hosts) throws IOException {
            curator.set(sessionPath.append(ZKApplicationPackage.allocatedHostsNode),
                        AllocatedHostsSerializer.toJson(hosts));
        }

        public void write(Map<Version, FileRegistry> fileRegistryMap) {
            for (Map.Entry<Version, FileRegistry> versionFileRegistryEntry : fileRegistryMap.entrySet()) {
                write(versionFileRegistryEntry.getKey(), versionFileRegistryEntry.getValue());
            }
        }

    }

}