-
Notifications
You must be signed in to change notification settings - Fork 612
/
CollectionPropsTest.java
344 lines (294 loc) · 14.1 KB
/
CollectionPropsTest.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cloud;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.common.cloud.CollectionProperties;
import org.apache.solr.common.cloud.CollectionPropsWatcher;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.zookeeper.KeeperException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SolrTestCaseJ4.SuppressSSL
public class CollectionPropsTest extends SolrCloudTestCase {
private static final int TIMEOUT = 5000;
private String collectionName;
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@BeforeClass
public static void setupClass() throws Exception {
configureCluster(4).addConfig("conf", configset("cloud-minimal")).configure();
}
@Before
@Override
public void setUp() throws Exception {
super.setUp();
collectionName = "CollectionPropsTest" + System.nanoTime();
CollectionAdminRequest.Create request =
CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2);
CollectionAdminResponse response = request.process(cluster.getSolrClient());
assertTrue("Unable to create collection: " + response, response.isSuccess());
}
@Test
public void testReadWriteNoCache() throws IOException {
CollectionProperties collectionProps = new CollectionProperties(zkClient());
collectionProps.setCollectionProperty(collectionName, "property1", "value1");
collectionProps.setCollectionProperty(collectionName, "property2", "value2");
checkValue("property1", "value1");
checkValue("property2", "value2");
collectionProps.setCollectionProperty(collectionName, "property1", "value1"); // no change
checkValue("property1", "value1");
collectionProps.setCollectionProperty(collectionName, "property1", null);
collectionProps.setCollectionProperty(collectionName, "property2", "newValue");
checkValue("property1", null);
checkValue("property2", "newValue");
collectionProps.setCollectionProperty(collectionName, "property2", null);
checkValue("property2", null);
collectionProps.setCollectionProperty(collectionName, "property2", null); // no change
checkValue("property2", null);
}
@Test
public void testReadWriteCached() throws InterruptedException, IOException {
CollectionProperties collectionProps = new CollectionProperties(zkClient());
// NOTE: Using a semaphore to ensure we wait for Watcher to fire before proceeding with
// test logic, to prevent triggering SOLR-13678
final Semaphore sawExpectedProps = new Semaphore(0);
final AtomicReference<Map<String, String>> expectedProps =
new AtomicReference<Map<String, String>>(null);
final CollectionPropsWatcher w =
new CollectionPropsWatcher() {
@Override
public boolean onStateChanged(Map<String, String> collectionProperties) {
log.info("collection properties changed. Now: {}", collectionProperties);
final Map<String, String> expected = expectedProps.get();
if (expected != null && expected.equals(collectionProperties)) {
log.info("...new props match expected");
sawExpectedProps.release();
}
return false;
}
};
cluster.getZkStateReader().registerCollectionPropsWatcher(collectionName, w);
collectionProps.setCollectionProperty(collectionName, "property1", "value1");
collectionProps.setCollectionProperty(collectionName, "property2", "value2");
waitForValue("property1", "value1");
waitForValue("property2", "value2");
// HACK: don't let our watcher be removed until we're sure it's "up to date"
// with the final prop values expected below...
expectedProps.set(new HashMap<>());
collectionProps.setCollectionProperty(collectionName, "property1", "value1"); // no change
checkValue("property1", "value1");
collectionProps.setCollectionProperty(collectionName, "property1", null);
collectionProps.setCollectionProperty(collectionName, "property2", "newValue");
waitForValue("property1", null);
waitForValue("property2", "newValue");
collectionProps.setCollectionProperty(collectionName, "property2", null);
waitForValue("property2", null);
collectionProps.setCollectionProperty(collectionName, "property2", null); // no change
checkValue("property2", null);
assertTrue(
"Gave up waitng an excessive amount of time for watcher to see final expected props",
sawExpectedProps.tryAcquire(1, 120, TimeUnit.SECONDS));
cluster.getZkStateReader().removeCollectionPropsWatcher(collectionName, w);
collectionProps.setCollectionProperty(collectionName, "property1", "value1");
// Should be no cache, so the change should take effect immediately
checkValue("property1", "value1");
}
private void checkValue(String propertyName, String expectedValue) {
final Object value =
cluster.getZkStateReader().getCollectionProperties(collectionName).get(propertyName);
assertEquals("Unexpected value for collection property: " + propertyName, expectedValue, value);
}
private void waitForValue(String propertyName, String expectedValue) throws InterruptedException {
final ZkStateReader zkStateReader = cluster.getZkStateReader();
Object lastValueSeen = null;
for (int i = 0; i < TIMEOUT; i += 10) {
final Object value = zkStateReader.getCollectionProperties(collectionName).get(propertyName);
if ((expectedValue == null && value == null)
|| (expectedValue != null && expectedValue.equals(value))) {
return;
}
lastValueSeen = value;
Thread.sleep(10);
}
String collectionpropsInZk = null;
try {
collectionpropsInZk =
new String(
cluster
.getZkClient()
.getData(
"/collections/" + collectionName + "/collectionprops.json", null, null, true),
StandardCharsets.UTF_8);
} catch (Exception e) {
collectionpropsInZk = "Could not get file from ZooKeeper: " + e.getMessage();
log.error("Could not get collectionprops from ZooKeeper for assertion mesage", e);
}
String propertiesInZkReader =
cluster.getZkStateReader().getCollectionProperties(collectionName).toString();
fail(
String.format(
Locale.ROOT,
"Could not see value change after setting collection property. Name: %s, current value: %s, expected value: %s. "
+ "\ncollectionprops.json file in ZooKeeper: %s"
+ "\nCollectionProperties in zkStateReader: %s",
propertyName,
lastValueSeen,
expectedValue,
collectionpropsInZk,
propertiesInZkReader));
}
@Test
public void testWatcher() throws KeeperException, InterruptedException, IOException {
final ZkStateReader zkStateReader = cluster.getZkStateReader();
CollectionProperties collectionProps = new CollectionProperties(zkClient());
// Add a watcher to collection props
final Watcher watcher = new Watcher("Watcher", random().nextBoolean());
zkStateReader.registerCollectionPropsWatcher(collectionName, watcher);
assertEquals(0, watcher.waitForTrigger(TEST_NIGHTLY ? 2000 : 200));
// Trigger a new znode event
log.info("setting value1");
collectionProps.setCollectionProperty(collectionName, "property", "value1");
assertEquals(1, watcher.waitForTrigger());
assertEquals("value1", watcher.getProps().get("property"));
// Trigger a value change event
log.info("setting value2");
collectionProps.setCollectionProperty(collectionName, "property", "value2");
int wft = watcher.waitForTrigger();
log.info("(value2) waitForTrigger=={}", wft);
assertEquals("value2", watcher.getProps().get("property"));
// Delete the properties znode
log.info("deleting props");
zkStateReader
.getZkClient()
.delete("/collections/" + collectionName + "/collectionprops.json", -1, true);
assertEquals(1, watcher.waitForTrigger());
final Map<String, String> props = watcher.getProps();
assertTrue(props.toString(), props.isEmpty());
// Remove watcher and make sure that the watcher is not triggered
log.info("removing watcher");
zkStateReader.removeCollectionPropsWatcher(collectionName, watcher);
log.info("setting value1 (again)");
collectionProps.setCollectionProperty(collectionName, "property", "value1");
assertEquals(
"ZK watcher was triggered after it was removed for collection " + collectionName,
0,
watcher.waitForTrigger());
}
@Test
public void testMultipleWatchers() throws InterruptedException, IOException {
final ZkStateReader zkStateReader = cluster.getZkStateReader();
CollectionProperties collectionProps = new CollectionProperties(zkClient());
// Register the core with ZkStateReader
zkStateReader.registerCore(collectionName);
// Subsequent watchers won't be triggered when adding
final Watcher watcher1 = new Watcher("Watcher1", random().nextBoolean());
zkStateReader.registerCollectionPropsWatcher(collectionName, watcher1);
watcher1.waitForTrigger(); // this might still get triggered because of registerCore
final Watcher watcher2 = new Watcher("Watcher2", random().nextBoolean());
zkStateReader.registerCollectionPropsWatcher(collectionName, watcher2);
assertEquals(0, watcher2.waitForTrigger(TEST_NIGHTLY ? 2000 : 200));
// Make sure a value change triggers both watchers
log.info("setting value1");
collectionProps.setCollectionProperty(collectionName, "property", "value1");
assertEquals(1, watcher1.waitForTrigger());
assertEquals(1, watcher2.waitForTrigger());
assertEquals("value1", watcher1.getProps().get("property"));
assertEquals("value1", watcher2.getProps().get("property"));
// The watchers should be triggered when after the core is unregistered
zkStateReader.unregisterCore(collectionName);
log.info("setting value2");
collectionProps.setCollectionProperty(collectionName, "property", "value2");
assertEquals(1, watcher1.waitForTrigger());
assertEquals(1, watcher2.waitForTrigger());
assertEquals("value2", watcher1.getProps().get("property"));
assertEquals("value2", watcher2.getProps().get("property"));
// The watcher should be triggered after another watcher is removed
log.info("removing watcher2");
zkStateReader.removeCollectionPropsWatcher(collectionName, watcher2);
log.info("setting value3");
collectionProps.setCollectionProperty(collectionName, "property", "value3");
assertEquals(1, watcher1.waitForTrigger());
assertEquals("value3", watcher1.getProps().get("property"));
// The last watcher shouldn't be triggered after removed, even if the core is registered
zkStateReader.registerCore(collectionName);
log.info("removing watcher1");
zkStateReader.removeCollectionPropsWatcher(collectionName, watcher1);
log.info("setting value4");
collectionProps.setCollectionProperty(collectionName, "property", "value4");
assertEquals(0, watcher1.waitForTrigger(TEST_NIGHTLY ? 2000 : 200));
}
private class Watcher implements CollectionPropsWatcher {
private final String name;
private final boolean forceReadPropsFromZk;
private volatile Map<String, String> props = Collections.emptyMap();
private final AtomicInteger triggered = new AtomicInteger();
public Watcher(final String name, final boolean forceReadPropsFromZk) {
this.name = name;
this.forceReadPropsFromZk = forceReadPropsFromZk;
log.info("Watcher '{}' initialized with forceReadPropsFromZk={}", name, forceReadPropsFromZk);
}
@Override
public boolean onStateChanged(Map<String, String> collectionProperties) {
log.info("{}: state changed...", name);
if (forceReadPropsFromZk) {
final ZkStateReader zkStateReader = cluster.getZkStateReader();
props = Map.copyOf(zkStateReader.getCollectionProperties(collectionName));
log.info("{}: Setting props from zk={}", name, props);
} else {
props = Map.copyOf(collectionProperties);
log.info("{}: Setting props from caller={}", name, props);
}
synchronized (this) {
triggered.incrementAndGet();
log.info("{}: notifying", name);
notifyAll();
}
log.info("{}: done", name);
return false;
}
private Map<String, String> getProps() {
return props;
}
private int waitForTrigger() throws InterruptedException {
return waitForTrigger(1000);
}
private int waitForTrigger(int waitTime) throws InterruptedException {
synchronized (this) {
if (triggered.get() > 0) {
return triggered.getAndSet(0);
}
wait(waitTime);
return triggered.getAndSet(0);
}
}
}
}