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

import com.yahoo.component.Version;
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.model.application.provider.PreGeneratedFileRegistry;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.serialization.AllocatedHostsSerializer;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.log.LogLevel;
import com.yahoo.path.Path;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.server.zookeeper.ConfigCurator;
import com.yahoo.vespa.config.server.zookeeper.ZKApplicationPackage;
import org.apache.commons.io.IOUtils;

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

/**
 * A class used for reading and writing application data to zookeeper.
 *
 * @author hmusum
 */
public class ZooKeeperClient {

    private final ConfigCurator configCurator;
    private final DeployLogger logger;
    private final boolean logFine;
    /* This is the generation that will be used for reading and writing application data. (1 more than last deployed application) */
    private final Path rootPath;

    static final ApplicationFile.PathFilter xmlFilter = new ApplicationFile.PathFilter() {
        @Override
        public boolean accept(Path path) {
            return path.getName().endsWith(".xml");
        }
    };

    public ZooKeeperClient(ConfigCurator configCurator, DeployLogger logger, boolean logFine, Path rootPath) {
        this.configCurator = configCurator;
        this.logger = logger;
        this.logFine = logFine;
        this.rootPath = rootPath;
    }

    /**
     * Sets up basic node structure in ZooKeeper and purges old data.
     * This is the first operation on ZK during deploy-application.
     *
     * We have retries in this method because there have been cases of stray connection loss to ZK,
     * even though the user has started the config server.
     *
     */
    void setupZooKeeper() {
        int retries = 5;
        try {
            while (retries > 0) {
                try {
                    logFine("Setting up ZooKeeper nodes for this application");
                    createZooKeeperNodes();
                    break;
                } catch (RuntimeException e) {
                    logger.log(LogLevel.FINE, "ZK init failed, retrying: " + e);
                    retries--;
                    if (retries == 0) {
                        throw e;
                    }
                    Thread.sleep(100);
                    // Not reconnecting, ZK is supposed to handle that automatically
                    // as long as the session doesn't expire. We'll see.
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Unable to initialize vespa model writing to config server(s) " +
                                            System.getProperty("configsources") + "\n" +
                                            "Please ensure that cloudconfig_server is started on the config server node(s), " +
                                            "and check the vespa log for configserver errors. ", e);
        }
    }

    /** Sets the app id and attempts to set up zookeeper. The app id must be ordered for purge to work OK. */
    private void createZooKeeperNodes() {
        if (!configCurator.exists(rootPath.getAbsolute())) {
            configCurator.createNode(rootPath.getAbsolute());
        }

        for (String subPath : Arrays.asList(
                ConfigCurator.DEFCONFIGS_ZK_SUBPATH,
                ConfigCurator.USER_DEFCONFIGS_ZK_SUBPATH,
                ConfigCurator.USERAPP_ZK_SUBPATH,
                ZKApplicationPackage.fileRegistryNode)) {
            // TODO The replaceFirst below is hackish.
            configCurator.createNode(getZooKeeperAppPath(null).getAbsolute(), subPath.replaceFirst("/", ""));
        }
    }

    /**
     * Writes def files and user config into ZK.
     *
     * @param app the application package to feed to zookeeper
     */
    void write(ApplicationPackage app) {
        logFine("Feeding application config into ZooKeeper");
        // gives lots and lots of debug output: // BasicConfigurator.configure();
        try {
            logFine("zk operations: " + configCurator.getNumberOfOperations());
            logFine("Feeding user def files into ZooKeeper");
            writeUserDefs(app);
            logFine("zk operations: " + configCurator.getNumberOfOperations());
            logFine("Feeding application package into ZooKeeper");
            // TODO 1200 zk operations done in the below method
            writeSomeOf(app);
            writeSearchDefinitions(app);
            writeUserIncludeDirs(app, app.getUserIncludeDirs());
            logFine("zk operations: " + configCurator.getNumberOfOperations());
            logFine("zk read operations: " + configCurator.getNumberOfReadOperations());
            logFine("zk write operations: " + configCurator.getNumberOfWriteOperations());
            logFine("Feeding sd from docproc bundle into ZooKeeper");
            logFine("zk operations: " + configCurator.getNumberOfOperations());
            logFine("Write application metadata into ZooKeeper");
            write(app.getMetaData());
            logFine("zk operations: " + configCurator.getNumberOfOperations());
        } catch (Exception e) {
            throw new IllegalStateException("Unable to write vespa model to config server(s) " + System.getProperty("configsources") + "\n" +
                    "Please ensure that cloudconfig_server is started on the config server node(s), " +
                    "and check the vespa log for configserver errors. ", e);
        }
    }

    private void writeSearchDefinitions(ApplicationPackage app) throws IOException {
        Collection<NamedReader> sds = app.getSearchDefinitions();
        if (sds.isEmpty()) {
            return;
        }
        Path zkPath = getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.SEARCH_DEFINITIONS_DIR);
        configCurator.createNode(zkPath.getAbsolute());
        // Ensures that ranking expressions and other files are also fed.
        writeDir(app.getFile(ApplicationPackage.SEARCH_DEFINITIONS_DIR), zkPath, false);
        for (NamedReader sd : sds) {
            String name = sd.getName();
            Reader reader = sd.getReader();
            String data = com.yahoo.io.IOUtils.readAll(reader);
            reader.close();
            configCurator.putData(zkPath.getAbsolute(), name, data);
        }
    }

    /**
     * Puts some of the application package files into ZK - see write(app).
     *
     * @param app The application package to use as input.
     * @throws java.io.IOException  if not able to write to Zookeeper
     */
    void writeSomeOf(ApplicationPackage app) throws IOException {
        ApplicationFile.PathFilter srFilter = new ApplicationFile.PathFilter() {
            @Override
            public boolean accept(Path path) {
                return path.getName().endsWith(ApplicationPackage.RULES_NAME_SUFFIX);
            }
        };
        // Copy app package files and subdirs into zk
        // TODO: We should have a way of doing this which doesn't require repeating all the content
        writeFile(app.getFile(Path.fromString(ApplicationPackage.SERVICES)),
                  getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH));
        writeFile(app.getFile(Path.fromString(ApplicationPackage.HOSTS)),
                  getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH));
        writeFile(app.getFile(Path.fromString(ApplicationPackage.DEPLOYMENT_FILE.getName())),
                  getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH));
        writeFile(app.getFile(Path.fromString(ApplicationPackage.VALIDATION_OVERRIDES.getName())),
                  getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH));
        writeDir(app.getFile(ApplicationPackage.RULES_DIR),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.RULES_DIR),
                 srFilter, true);
        writeDir(app.getFile(ApplicationPackage.QUERY_PROFILES_DIR),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.QUERY_PROFILES_DIR),
                 xmlFilter, true);
        writeDir(app.getFile(ApplicationPackage.PAGE_TEMPLATES_DIR),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.PAGE_TEMPLATES_DIR),
                 xmlFilter, true);
        writeDir(app.getFile(Path.fromString(ApplicationPackage.SEARCHCHAINS_DIR)),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.SEARCHCHAINS_DIR),
                 xmlFilter, true);
        writeDir(app.getFile(Path.fromString(ApplicationPackage.DOCPROCCHAINS_DIR)),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.DOCPROCCHAINS_DIR),
                 xmlFilter, true);
        writeDir(app.getFile(Path.fromString(ApplicationPackage.ROUTINGTABLES_DIR)),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.ROUTINGTABLES_DIR),
                 xmlFilter, true);
        writeDir(app.getFile(ApplicationPackage.MODELS_GENERATED_REPLICATED_DIR),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.MODELS_GENERATED_REPLICATED_DIR),
                 true);
        writeDir(app.getFile(ApplicationPackage.SECURITY_DIR),
                 getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH).append(ApplicationPackage.SECURITY_DIR),
                 true);
    }

    private void writeDir(ApplicationFile file, Path zooKeeperAppPath, boolean recurse) throws IOException {
        writeDir(file, zooKeeperAppPath, new ApplicationFile.PathFilter() {
            @Override
            public boolean accept(Path path) {
                return true;
            }
        }, recurse);
    }

    private void writeDir(ApplicationFile dir, Path path, ApplicationFile.PathFilter filenameFilter, boolean recurse) throws IOException {
        if (!dir.isDirectory()) {
            logger.log(LogLevel.FINE, dir.getPath().getAbsolute()+" is not a directory. Not feeding the files into ZooKeeper.");
            return;
        }
        for (ApplicationFile file: listFiles(dir, filenameFilter)) {
            String name = file.getPath().getName();
            if (name.startsWith(".")) continue; //.svn , .git ...
            if ("CVS".equals(name)) continue;
            if (file.isDirectory()) {
                configCurator.createNode(path.append(name).getAbsolute());
                if (recurse) {
                    writeDir(file, path.append(name), filenameFilter, recurse);
                }
            } else {
                writeFile(file, path);
            }
        }
    }

    /**
     * Like {@link ApplicationFile#listFiles(com.yahoo.config.application.api.ApplicationFile.PathFilter)} with a slightly different semantic. 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()) {
            IOUtils.copy(inputStream, baos);
            baos.flush();
            configCurator.putData(zkPath.append(file.getPath().getName()).getAbsolute(), baos.toByteArray());
        }
    }

    private void writeUserIncludeDirs(ApplicationPackage applicationPackage, List<String> userIncludeDirs) throws IOException {
        // User defined include directories
        for (String userInclude : userIncludeDirs) {
            ApplicationFile dir = applicationPackage.getFile(Path.fromString(userInclude));
            final List<ApplicationFile> files = dir.listFiles();
            if (files == null || files.isEmpty()) {
                configCurator.createNode(getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + userInclude).getAbsolute());
            }
            writeDir(dir,
                     getZooKeeperAppPath(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + userInclude),
                     xmlFilter, true);
        }
    }

    /**
     * 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(ConfigCurator.USER_DEFCONFIGS_ZK_SUBPATH).getAbsolute(), contents);
            writeConfigDefinition(key.getName(), key.getNamespace(), getZooKeeperAppPath(ConfigCurator.DEFCONFIGS_ZK_SUBPATH).getAbsolute(), contents);
        }
        logger.log(LogLevel.FINE, configDefs.size() + " user config definitions");
    }

    private void writeConfigDefinition(String name, String namespace, String path, String data) {
        configCurator.putDefData(namespace + "." + name, path, com.yahoo.text.Utf8.toBytes(data));
    }

    private void write(Version vespaVersion, FileRegistry fileRegistry) {
        logFine("Feeding file registry data into ZooKeeper");
        String exportedRegistry = PreGeneratedFileRegistry.exportRegistry(fileRegistry);

        configCurator.putData(getZooKeeperAppPath(null).append(ZKApplicationPackage.fileRegistryNode).getAbsolute(),
                vespaVersion.toFullString(),
                exportedRegistry);
    }

    /**
     * Feeds application metadata to zookeeper. Used by vespamodel to create config
     * for application metadata (used by ApplicationStatusHandler)
     *
     * @param metaData The application metadata.
     */
    private void write(ApplicationMetaData metaData) {
        configCurator.putData(getZooKeeperAppPath(ConfigCurator.META_ZK_PATH).getAbsolute(), metaData.asJsonString());
    }

    void cleanupZooKeeper() {
        logFine("Exception occurred. Cleaning up ZooKeeper");
        try {
            for (String subPath : Arrays.asList(
                    ConfigCurator.DEFCONFIGS_ZK_SUBPATH,
                    ConfigCurator.USER_DEFCONFIGS_ZK_SUBPATH,
                    ConfigCurator.USERAPP_ZK_SUBPATH)) {
                configCurator.deleteRecurse(getZooKeeperAppPath(null).append(subPath).getAbsolute());
            }
        } catch (Exception e) {
            logger.log(LogLevel.WARNING, "Could not clean up in zookeeper");
            //Might be called in an exception handler before re-throw, so do not throw here.
        }
    }

    /**
     * Gets a full ZK app path based on id set in Admin object
     *
     *
     * @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
     */
    Path getZooKeeperAppPath(String trailingPath) {
        if (trailingPath != null) {
            return rootPath.append(trailingPath);
        } else {
            return rootPath;
        }
    }

    void logFine(String msg) {
        if (logFine) {
            logger.log(LogLevel.FINE, msg);
        }
    }

    public void write(AllocatedHosts hosts) throws IOException {
        configCurator.putData(
                rootPath.append(ZKApplicationPackage.allocatedHostsNode).getAbsolute(),
                AllocatedHostsSerializer.toJson(hosts));
    }

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

}