1 |
28918
|
claudio.at
|
package eu.dnetlib.functionality.index.utils;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
|
|
import java.io.InputStream;
|
5 |
|
|
import java.util.Map;
|
6 |
|
|
import java.util.Map.Entry;
|
7 |
|
|
|
8 |
|
|
import org.apache.commons.io.IOUtils;
|
9 |
|
|
import org.apache.commons.logging.Log;
|
10 |
|
|
import org.apache.commons.logging.LogFactory;
|
11 |
|
|
import org.apache.solr.common.cloud.SolrZkClient;
|
12 |
|
|
import org.apache.zookeeper.CreateMode;
|
13 |
|
|
import org.apache.zookeeper.KeeperException;
|
14 |
|
|
import org.dom4j.Document;
|
15 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
16 |
|
|
import org.springframework.beans.factory.annotation.Required;
|
17 |
|
|
import org.springframework.core.io.Resource;
|
18 |
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
19 |
|
|
|
20 |
|
|
import com.google.common.collect.Maps;
|
21 |
|
|
|
22 |
|
|
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* The Class ZkUtils.
|
26 |
|
|
*/
|
27 |
|
|
public class ZkUtils {
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* The log.
|
31 |
|
|
*/
|
32 |
|
|
private static final Log log = LogFactory.getLog(ZkUtils.class); // NOPMD by marko on 11/24/08 5:02 PM
|
33 |
|
|
|
34 |
|
|
/** The Constant CONFIGS_PATH. */
|
35 |
|
|
private static final String CONFIGS_PATH = "/configs";
|
36 |
|
|
|
37 |
|
|
/** The schema factory. */
|
38 |
|
|
@Autowired
|
39 |
|
|
private IndexSchemaFactory schemaFactory;
|
40 |
|
|
|
41 |
|
|
/** The config factory. */
|
42 |
|
|
private IndexConfigFactory configFactory;
|
43 |
|
|
|
44 |
|
|
/** The static configuration classpath. */
|
45 |
|
|
private String staticConfigurationClasspath;
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Upload zookeper config.
|
49 |
|
|
*
|
50 |
|
|
* @param zkClient
|
51 |
|
|
* the zk client
|
52 |
|
|
* @param coreName
|
53 |
|
|
* the core name
|
54 |
|
|
* @param fields
|
55 |
|
|
* the fields
|
56 |
|
|
* @param params
|
57 |
|
|
* the params
|
58 |
|
|
* @param overwrite
|
59 |
|
|
* the overwrite
|
60 |
|
|
*/
|
61 |
|
|
public void uploadZookeperConfig(final SolrZkClient zkClient,
|
62 |
|
|
final String coreName,
|
63 |
|
|
final Document fields,
|
64 |
|
|
final Map<String, String> params,
|
65 |
|
|
final boolean overwrite) {
|
66 |
|
|
|
67 |
|
|
final String basepath = CONFIGS_PATH + "/" + coreName;
|
68 |
|
|
|
69 |
|
|
log.info("uploading solr configuration to ZK for index collection: " + coreName);
|
70 |
|
|
|
71 |
|
|
try {
|
72 |
|
|
if (overwrite) {
|
73 |
|
|
log.info("cleanup ZK configuration: " + coreName);
|
74 |
|
|
for (String child : zkClient.getSolrZooKeeper().getChildren(basepath, false)) {
|
75 |
|
|
final String path = basepath + "/" + child;
|
76 |
|
|
log.debug("cleanup ZK file: " + path);
|
77 |
|
|
zkClient.delete(path, -1, true);
|
78 |
|
|
}
|
79 |
|
|
zkClient.delete(basepath, -1, true);
|
80 |
|
|
}
|
81 |
|
|
if (!zkClient.exists(basepath, true)) {
|
82 |
|
|
log.info("upload ZK configuration: " + coreName);
|
83 |
|
|
zkClient.makePath(basepath, true);
|
84 |
|
|
uploadConfiguration(zkClient, basepath, buildConfiguration(coreName, fields, params));
|
85 |
|
|
}
|
86 |
|
|
log.info("upload ZK configuration complete");
|
87 |
|
|
} catch (Exception e) {
|
88 |
|
|
log.error("unable to upload solr configuration", e);
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
/**
|
93 |
|
|
* Builds the configuration.
|
94 |
|
|
*
|
95 |
|
|
* @param indexName
|
96 |
|
|
* the index name
|
97 |
|
|
* @param fields
|
98 |
|
|
* the fields
|
99 |
|
|
* @param params
|
100 |
|
|
* the params
|
101 |
|
|
* @return the map
|
102 |
|
|
* @throws IndexServiceException
|
103 |
|
|
* the index service exception
|
104 |
|
|
*/
|
105 |
|
|
private Map<String, byte[]> buildConfiguration(final String indexName, final Document fields, final Map<String, String> params)
|
106 |
|
|
throws IndexServiceException {
|
107 |
|
|
|
108 |
|
|
Map<String, byte[]> res = Maps.newHashMap();
|
109 |
|
|
|
110 |
|
|
try {
|
111 |
|
|
res.put("schema.xml", schemaFactory.getSchema(fields).getBytes());
|
112 |
|
|
|
113 |
|
|
res.put("solrconfig.xml", configFactory.getConfig(params).getBytes());
|
114 |
|
|
|
115 |
|
|
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
116 |
|
|
Resource[] resources = resolver.getResources(getStaticConfigurationClasspath());
|
117 |
|
|
|
118 |
|
|
for (Resource r : resources) {
|
119 |
|
|
InputStream is = r.getInputStream();
|
120 |
|
|
res.put(r.getFilename(), IOUtils.toByteArray(is));
|
121 |
|
|
is.close();
|
122 |
|
|
}
|
123 |
|
|
return res;
|
124 |
|
|
} catch (Throwable e) {
|
125 |
|
|
throw new IndexServiceException("failed to build configuration", e);
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Upload configuration.
|
131 |
|
|
*
|
132 |
|
|
* @param zkClient
|
133 |
|
|
* the zk client
|
134 |
|
|
* @param basePath
|
135 |
|
|
* the base path
|
136 |
|
|
* @param resources
|
137 |
|
|
* the resources
|
138 |
|
|
* @throws KeeperException
|
139 |
|
|
* the keeper exception
|
140 |
|
|
* @throws InterruptedException
|
141 |
|
|
* the interrupted exception
|
142 |
|
|
* @throws IOException
|
143 |
|
|
* Signals that an I/O exception has occurred.
|
144 |
|
|
*/
|
145 |
|
|
private void uploadConfiguration(final SolrZkClient zkClient, final String basePath, final Map<String, byte[]> resources) throws KeeperException,
|
146 |
|
|
InterruptedException, IOException {
|
147 |
|
|
|
148 |
|
|
if (!zkClient.exists(basePath, true)) {
|
149 |
|
|
zkClient.makePath(basePath, true);
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
for (final Entry<String, byte[]> e : resources.entrySet()) {
|
153 |
|
|
String path = basePath + "/" + e.getKey();
|
154 |
|
|
log.debug("upload ZK configuration: " + path);
|
155 |
|
|
zkClient.create(path, e.getValue(), CreateMode.PERSISTENT, true);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Gets the config factory.
|
161 |
|
|
*
|
162 |
|
|
* @return the config factory
|
163 |
|
|
*/
|
164 |
|
|
public IndexConfigFactory getConfigFactory() {
|
165 |
|
|
return configFactory;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/**
|
169 |
|
|
* Sets the config factory.
|
170 |
|
|
*
|
171 |
|
|
* @param configFactory
|
172 |
|
|
* the new config factory
|
173 |
|
|
*/
|
174 |
|
|
@Required
|
175 |
|
|
public void setConfigFactory(final IndexConfigFactory configFactory) {
|
176 |
|
|
this.configFactory = configFactory;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
/**
|
180 |
|
|
* Gets the static configuration classpath.
|
181 |
|
|
*
|
182 |
|
|
* @return the static configuration classpath
|
183 |
|
|
*/
|
184 |
|
|
public String getStaticConfigurationClasspath() {
|
185 |
|
|
return staticConfigurationClasspath;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
/**
|
189 |
|
|
* Sets the static configuration classpath.
|
190 |
|
|
*
|
191 |
|
|
* @param staticConfigurationClasspath
|
192 |
|
|
* the new static configuration classpath
|
193 |
|
|
*/
|
194 |
|
|
@Required
|
195 |
|
|
public void setStaticConfigurationClasspath(final String staticConfigurationClasspath) {
|
196 |
|
|
this.staticConfigurationClasspath = staticConfigurationClasspath;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
}
|