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
|
// 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.zookeeper;
import com.google.common.base.Joiner;
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.ComponentInfo;
import com.yahoo.config.application.api.FileRegistry;
import com.yahoo.config.application.api.UnparsedConfigDefinition;
import com.yahoo.config.codegen.DefParser;
import com.yahoo.config.model.application.provider.PreGeneratedFileRegistry;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.serialization.AllocatedHostsSerializer;
import com.yahoo.io.IOUtils;
import com.yahoo.io.reader.NamedReader;
import com.yahoo.path.Path;
import com.yahoo.vespa.config.ConfigDefinition;
import com.yahoo.vespa.config.ConfigDefinitionBuilder;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.util.ConfigUtils;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* Represents an application residing in zookeeper.
*
* @author Tony Vaagenes
*/
public class ZKApplicationPackage implements ApplicationPackage {
private final ZKApplication zkApplication;
private final Map<Version, PreGeneratedFileRegistry> fileRegistryMap = new HashMap<>();
private final Optional<AllocatedHosts> allocatedHosts;
public static final String fileRegistryNode = "fileregistry";
public static final String allocatedHostsNode = "allocatedHosts";
private final ApplicationMetaData metaData;
public ZKApplicationPackage(ConfigCurator zk, Path sessionPath) {
verifyAppPath(zk, sessionPath);
zkApplication = new ZKApplication(zk, sessionPath);
metaData = readMetaDataFromLiveApp(zkApplication);
importFileRegistries();
allocatedHosts = importAllocatedHosts();
}
private Optional<AllocatedHosts> importAllocatedHosts() {
if ( ! zkApplication.exists(allocatedHostsNode)) return Optional.empty();
return Optional.of(readAllocatedHosts());
}
/**
* Reads allocated hosts at the given node.
*
* @return the allocated hosts at this node or empty if there is no data at this path
*/
private AllocatedHosts readAllocatedHosts() {
try {
return AllocatedHostsSerializer.fromJson(zkApplication.getBytes(allocatedHostsNode));
} catch (Exception e) {
throw new RuntimeException("Unable to read allocated hosts", e);
}
}
private void importFileRegistries() {
List<String> perVersionFileRegistryNodes = zkApplication.getChildren(fileRegistryNode);
perVersionFileRegistryNodes
.forEach(version ->
fileRegistryMap.put(Version.fromString(version),
importFileRegistry(Joiner.on("/").join(fileRegistryNode, version))));
}
private PreGeneratedFileRegistry importFileRegistry(String fileRegistryNode) {
try {
return PreGeneratedFileRegistry.importRegistry(zkApplication.getDataReader(fileRegistryNode));
} catch (Exception e) {
throw new RuntimeException("Could not determine which files to distribute", e);
}
}
private ApplicationMetaData readMetaDataFromLiveApp(ZKApplication liveApp) {
String metaDataString = liveApp.getData(ConfigCurator.META_ZK_PATH);
if (metaDataString == null || metaDataString.isEmpty()) {
return null;
}
return ApplicationMetaData.fromJsonString(liveApp.getData(ConfigCurator.META_ZK_PATH));
}
@Override
public ApplicationMetaData getMetaData() {
return metaData;
}
private static void verifyAppPath(ConfigCurator zk, Path appPath) {
if (!zk.exists(appPath.getAbsolute()))
throw new RuntimeException("App with path " + appPath + " does not exist");
}
@Override
@SuppressWarnings("deprecation")
public String getApplicationName() {
return metaData.getApplicationId().application().value();
}
@Override
public ApplicationId getApplicationId() { return metaData.getApplicationId(); }
@Override
public Reader getServices() {
return getUserAppData(SERVICES);
}
@Override
public Reader getHosts() {
if (zkApplication.exists(ConfigCurator.USERAPP_ZK_SUBPATH, HOSTS))
return getUserAppData(HOSTS);
return null;
}
@Override
public List<NamedReader> getSchemas() {
List<NamedReader> schemas = new ArrayList<>();
for (String sd : zkApplication.getChildren(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SCHEMAS_DIR)) {
if (sd.endsWith(SD_NAME_SUFFIX))
schemas.add(new NamedReader(sd, new StringReader(zkApplication.getData(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SCHEMAS_DIR, sd))));
}
// TODO: Remove when 7.414.19 is oldest version in use
for (String sd : zkApplication.getChildren(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SEARCH_DEFINITIONS_DIR)) {
if (sd.endsWith(SD_NAME_SUFFIX))
schemas.add(new NamedReader(sd, new StringReader(zkApplication.getData(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SEARCH_DEFINITIONS_DIR, sd))));
}
return schemas;
}
@Override
public Optional<AllocatedHosts> getAllocatedHosts() {
return allocatedHosts;
}
@Override
public Map<Version, FileRegistry> getFileRegistries() {
return Collections.unmodifiableMap(fileRegistryMap);
}
private Optional<PreGeneratedFileRegistry> getPreGeneratedFileRegistry(Version vespaVersion) {
// Assumes at least one file registry, which we always have.
Optional<PreGeneratedFileRegistry> fileRegistry = Optional.ofNullable(fileRegistryMap.get(vespaVersion));
if (fileRegistry.isEmpty()) {
fileRegistry = Optional.of(fileRegistryMap.values().iterator().next());
}
return fileRegistry;
}
private Reader retrieveConfigDefReader(String def) {
try {
return zkApplication.getDataReader(ConfigCurator.DEFCONFIGS_ZK_SUBPATH, def);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Could not retrieve config definition " + def, e);
}
}
@Override
public Map<ConfigDefinitionKey, UnparsedConfigDefinition> getAllExistingConfigDefs() {
Map<ConfigDefinitionKey, UnparsedConfigDefinition> ret = new LinkedHashMap<>();
List<String> allDefs = zkApplication.getChildren(ConfigCurator.DEFCONFIGS_ZK_SUBPATH);
for (String nodeName : allDefs) {
ConfigDefinitionKey key = ConfigUtils.createConfigDefinitionKeyFromZKString(nodeName);
ret.put(key, new UnparsedConfigDefinition() {
@Override
public ConfigDefinition parse() {
DefParser parser = new DefParser(key.getName(), retrieveConfigDefReader(nodeName));
return ConfigDefinitionBuilder.createConfigDefinition(parser.getTree());
}
@Override
public String getUnparsedContent() {
try {
return IOUtils.readAll(retrieveConfigDefReader(nodeName));
} catch (Exception e) {
throw new RuntimeException("Error retriving def file", e);
}
}
});
}
return ret;
}
/**
* Returns readers for all the children of a node.
* The node is looked up relative to the location of the active application package in zookeeper.
*/
@Override
public List<NamedReader> getFiles(Path relativePath, String suffix, boolean recurse) {
return zkApplication.getAllDataFromDirectory(ConfigCurator.USERAPP_ZK_SUBPATH + '/' + relativePath.getRelative(), suffix, recurse);
}
@Override
public ApplicationFile getFile(Path file) {
return new ZKApplicationFile(file, zkApplication);
}
@Override
public String getHostSource() {
return "zookeeper hosts file";
}
@Override
public String getServicesSource() {
return "zookeeper services file";
}
@Override
public Optional<Reader> getDeployment() { return optionalFile(DEPLOYMENT_FILE.getName()); }
@Override
public Optional<Reader> getValidationOverrides() { return optionalFile(VALIDATION_OVERRIDES.getName()); }
private Optional<Reader> optionalFile(String file) {
if (zkApplication.exists(ConfigCurator.USERAPP_ZK_SUBPATH, file))
return Optional.of(getUserAppData(file));
else
return Optional.empty();
}
@Override
public List<ComponentInfo> getComponentsInfo(Version vespaVersion) {
List<ComponentInfo> components = new ArrayList<>();
PreGeneratedFileRegistry fileRegistry = getPreGeneratedFileRegistry(vespaVersion).get();
for (String path : fileRegistry.getPaths()) {
if (path.startsWith(COMPONENT_DIR + File.separator) && path.endsWith(".jar")) {
ComponentInfo component = new ComponentInfo(path);
components.add(component);
}
}
return components;
}
private Reader getUserAppData(String node) {
return zkApplication.getDataReader(ConfigCurator.USERAPP_ZK_SUBPATH, node);
}
@Override
public Reader getRankingExpression(String name) {
Optional<Reader> reader = zkApplication.getOptionalDataReader(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SCHEMAS_DIR, name);
// TODO: Remove when 7.414.19 is oldest version in use
return reader.orElseGet(() -> zkApplication.getDataReader(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + SEARCH_DEFINITIONS_DIR, name));
}
@Override
public File getFileReference(Path pathRelativeToAppDir) {
String fileName = zkApplication.getData(ConfigCurator.USERAPP_ZK_SUBPATH + "/" + pathRelativeToAppDir.getRelative());
// File does not exist: Manufacture a non-existing file
return new File(Objects.requireNonNullElseGet(fileName, pathRelativeToAppDir::getRelative));
}
@Override
public void validateIncludeDir(String dirName) {
String fullPath = ConfigCurator.USERAPP_ZK_SUBPATH + "/" + dirName;
if ( ! zkApplication.exists(fullPath)) {
throw new IllegalArgumentException("Cannot include directory '" + dirName +
"', as it does not exist in ZooKeeper!");
}
}
}
|