OffloadingTopicManagerImpl.java
13.1 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.http.HttpEntity
* org.apache.http.StatusLine
* org.apache.http.client.config.RequestConfig
* org.apache.http.client.config.RequestConfig$Builder
* org.apache.http.client.entity.UrlEncodedFormEntity
* org.apache.http.client.methods.CloseableHttpResponse
* org.apache.http.client.methods.HttpPost
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.impl.client.HttpClients
* org.apache.http.message.BasicNameValuePair
* org.apache.sling.discovery.DiscoveryService
* org.apache.sling.discovery.InstanceDescription
* org.apache.sling.discovery.InstanceFilter
* org.apache.sling.discovery.TopologyView
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.offloading.impl;
import com.adobe.granite.offloading.api.OffloadingException;
import com.adobe.granite.offloading.api.OffloadingTopicManager;
import com.adobe.granite.offloading.api.TopicConfigurationAction;
import com.adobe.granite.offloading.api.TopicInstancesHolder;
import com.adobe.granite.offloading.impl.TopicInstancesHolderImpl;
import com.adobe.granite.offloading.impl.util.OffloadingUtil;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.sling.discovery.DiscoveryService;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.InstanceFilter;
import org.apache.sling.discovery.TopologyView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class OffloadingTopicManagerImpl
implements OffloadingTopicManager {
private static final Logger LOG = LoggerFactory.getLogger(OffloadingTopicManagerImpl.class);
private static final int NETWORK_TEST_TIMEOUT_SECONDS = 3;
@Reference
private DiscoveryService discoveryService;
@Override
public Map<String, TopicInstancesHolder> getInstances() {
HashMap<String, TopicInstancesHolder> topicToInstanceMap = new HashMap<String, TopicInstancesHolder>();
TopologyView topologyView = this.discoveryService.getTopology();
Set instances = topologyView.getInstances();
for (InstanceDescription instance : instances) {
Set<String> enabledTopics = OffloadingUtil.expandCSV(instance.getProperty("org.apache.sling.event.jobs.consumer.topics"));
for (String topic : enabledTopics) {
TopicInstancesHolder topicHolder = topicToInstanceMap.get(topic);
if (topicHolder == null) {
topicHolder = new TopicInstancesHolderImpl(topic);
topicToInstanceMap.put(topic, topicHolder);
}
Set<InstanceDescription> enabledInstances = topicHolder.getEnabledInstances();
enabledInstances.add(instance);
}
Set<String> disabledTopics = OffloadingUtil.expandCSV(instance.getProperty("com.adobe.granite.offloading.job.registeredtopics"));
disabledTopics.removeAll(enabledTopics);
for (String topic2 : disabledTopics) {
TopicInstancesHolder topicHolder = topicToInstanceMap.get(topic2);
if (topicHolder == null) {
topicHolder = new TopicInstancesHolderImpl(topic2);
topicToInstanceMap.put(topic2, topicHolder);
}
Set<InstanceDescription> disabledInstances = topicHolder.getDisabledInstances();
disabledInstances.add(instance);
}
}
return topicToInstanceMap;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void configureInstance(String slingID, Map<TopicConfigurationAction, Set<String>> configuration) {
Set<String> enable;
String currentWhitelistString;
Set<String> whitelist;
Set<String> exclusive;
InstanceDescription instance = this.findInstanceByID(slingID);
if (instance == null) {
throw new OffloadingException("Cannot find instance with Sling ID " + slingID);
}
String endpointsString = instance.getProperty("org.apache.sling.instance.endpoints");
if (StringUtils.isEmpty((String)endpointsString)) {
throw new OffloadingException("Cannot detect endpoints for instance with Sling ID " + slingID);
}
if (configuration == null || configuration.isEmpty()) {
throw new OffloadingException("Empty configuration map received for instance with Sling ID " + slingID);
}
Set<String> wildcard = this.getRegisteredTopics(instance);
String currentBlacklistString = instance.getProperty("job.consumermanager.blacklist");
Set<String> blacklist = OffloadingUtil.expandCSV(currentBlacklistString);
if (blacklist.contains("*")) {
blacklist = new HashSet<String>(wildcard);
}
if ((whitelist = OffloadingUtil.expandCSV(currentWhitelistString = instance.getProperty("job.consumermanager.whitelist"))).contains("*")) {
whitelist = new HashSet<String>(wildcard);
}
if ((exclusive = configuration.get((Object)TopicConfigurationAction.EXCLUSIVE)) != null) {
Iterator<String> whitelistIterator = whitelist.iterator();
while (whitelistIterator.hasNext()) {
String topic = whitelistIterator.next();
if (exclusive.contains(topic)) continue;
whitelistIterator.remove();
}
whitelist.addAll(exclusive);
blacklist.removeAll(exclusive);
}
if ((enable = configuration.get((Object)TopicConfigurationAction.ENABLE)) != null && enable.contains("*")) {
enable = new HashSet<String>(wildcard);
}
Set<String> disable = configuration.get((Object)TopicConfigurationAction.DISABLE);
if (exclusive != null) {
disable = null;
}
if (disable != null && disable.contains("*")) {
disable = new HashSet<String>(wildcard);
}
if (enable != null && disable != null) {
Iterator<String> enableIterator = enable.iterator();
while (enableIterator.hasNext()) {
String topic = enableIterator.next();
if (!disable.remove(topic)) continue;
enableIterator.remove();
}
}
if (enable != null) {
for (String s : enable) {
blacklist.remove(s);
if (wildcard.equals(whitelist)) continue;
whitelist.remove(s);
}
}
if (disable != null) {
for (String s : disable) {
blacklist.add(s);
if (wildcard.equals(whitelist)) continue;
whitelist.remove(s);
}
}
ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
if (whitelist.size() == 0 || wildcard.equals(whitelist)) {
nvps.add(new BasicNameValuePair("job.consumermanager.whitelist", "*"));
} else {
int i = 0;
for (String topic : whitelist) {
nvps.add(new BasicNameValuePair("job.consumermanager.whitelist", topic));
++i;
}
}
if (blacklist.size() == 0) {
nvps.add(new BasicNameValuePair("job.consumermanager.blacklist", ""));
} else {
int i = 0;
for (String topic : blacklist) {
nvps.add(new BasicNameValuePair("job.consumermanager.blacklist", topic));
++i;
}
}
nvps.add(new BasicNameValuePair("org.apache.sling.installer.configuration.persist", Boolean.FALSE.toString()));
int statusCode = 0;
Set<String> endpoints = OffloadingUtil.expandCSV(endpointsString);
Iterator<String> i$ = endpoints.iterator();
while (i$.hasNext()) {
String endpoint = i$.next();
String host = OffloadingUtil.getHostFromEndpoint(endpoint);
try {
if (InetAddress.getByName(host).isReachable(3000)) {
if (endpoint.endsWith("/")) {
endpoint = endpoint.substring(0, endpoint.length() - 1);
}
String url = endpoint + "/libs/granite/offloading/config/jobconsumer";
HttpPost post = new HttpPost(url);
post.setEntity((HttpEntity)new UrlEncodedFormEntity(nvps));
CloseableHttpClient client = this.getHTTPClient();
CloseableHttpResponse response = null;
try {
response = client.execute((HttpUriRequest)post);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200 && statusCode != 201) continue;
break;
}
catch (Exception ex) {
LOG.error("Unable to send configuration request to " + url, (Throwable)ex);
continue;
}
finally {
if (response != null) {
response.close();
}
continue;
}
}
LOG.error("Unreachable host: " + endpoint);
}
catch (Exception e) {
LOG.error("Unreachable host: " + endpoint);
}
}
if (statusCode != 200 && statusCode != 201) {
throw new OffloadingException("Unable to send configuration request to instance with Sling ID " + slingID + ". " + "Got status code " + statusCode);
}
}
@Override
public Set<String> getRegisteredTopics(InstanceDescription instance) {
String registeredTopics = instance.getProperty("com.adobe.granite.offloading.job.registeredtopics");
return OffloadingUtil.expandCSV(registeredTopics);
}
@Override
public Set<String> getWhitelistedTopics(InstanceDescription instance) {
String whitelistString = instance.getProperty("job.consumermanager.whitelist");
Set<String> whitelist = OffloadingUtil.expandCSV(whitelistString);
for (String topic : whitelist) {
if (!"*".equals(topic)) continue;
whitelist = this.getRegisteredTopics(instance);
break;
}
return whitelist;
}
@Override
public Set<String> getBlacklistedTopics(InstanceDescription instance) {
String blacklistString = instance.getProperty("job.consumermanager.blacklist");
Set<String> blacklist = OffloadingUtil.expandCSV(blacklistString);
for (String topic : blacklist) {
if (!"*".equals(topic)) continue;
blacklist = this.getRegisteredTopics(instance);
break;
}
return blacklist;
}
private InstanceDescription findInstanceByID(final String slingID) {
TopologyView topologyView = this.discoveryService.getTopology();
InstanceDescription id = null;
Set instances = topologyView.findInstances(new InstanceFilter(){
public boolean accept(InstanceDescription instanceDescription) {
return slingID.equals(instanceDescription.getSlingId());
}
});
if (instances != null && instances.size() > 0) {
Iterator it = instances.iterator();
id = (InstanceDescription)it.next();
}
return id;
}
private CloseableHttpClient getHTTPClient() {
RequestConfig config = RequestConfig.custom().setSocketTimeout(10000).build();
HttpClientBuilder httpClient = HttpClients.custom().setDefaultRequestConfig(config);
return httpClient.build();
}
protected void bindDiscoveryService(DiscoveryService discoveryService) {
this.discoveryService = discoveryService;
}
protected void unbindDiscoveryService(DiscoveryService discoveryService) {
if (this.discoveryService == discoveryService) {
this.discoveryService = null;
}
}
}