forked from apache/pulsar
/
MetadataCacheTest.java
460 lines (371 loc) · 18.4 KB
/
MetadataCacheTest.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* 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.pulsar.metadata;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.pulsar.common.policies.data.Policies;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.metadata.api.CacheGetResult;
import org.apache.pulsar.metadata.api.MetadataCache;
import org.apache.pulsar.metadata.api.MetadataSerde;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.MetadataStoreConfig;
import org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException;
import org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException;
import org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException;
import org.apache.pulsar.metadata.api.MetadataStoreFactory;
import org.apache.pulsar.metadata.api.Stat;
import org.awaitility.Awaitility;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MetadataCacheTest extends BaseMetadataStoreTest {
@Data
@AllArgsConstructor
@NoArgsConstructor
static class MyClass {
String a;
int b;
}
@Test(dataProvider = "impl")
public void emptyCacheTest(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
assertEquals(objCache.getIfCached("/non-existing-key"), Optional.empty());
assertEquals(objCache.getIfCached("/non-existing-key/child"), Optional.empty());
assertEquals(objCache.get("/non-existing-key").join(), Optional.empty());
assertEquals(objCache.get("/non-existing-key/child").join(), Optional.empty());
try {
objCache.delete("/non-existing-key").join();
fail("should have failed");
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), NotFoundException.class);
}
try {
objCache.delete("/non-existing-key/child").join();
fail("should have failed");
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), NotFoundException.class);
}
}
@DataProvider(name = "zk")
public Object[][] zkimplementations() {
return new Object[][] {
{ "ZooKeeper", zks.getConnectionString() },
};
}
@Test(dataProvider = "zk")
public void crossStoreUpdates(String provider, String url) throws Exception {
@Cleanup
MetadataStore store1 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
@Cleanup
MetadataStore store2 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
@Cleanup
MetadataStore store3 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache1 = store1.getMetadataCache(MyClass.class);
MetadataCache<MyClass> objCache2 = store2.getMetadataCache(MyClass.class);
MetadataCache<MyClass> objCache3 = store3.getMetadataCache(MyClass.class);
List<MetadataCache<MyClass>> allCaches = new ArrayList<>();
allCaches.add(objCache1);
allCaches.add(objCache2);
allCaches.add(objCache3);
// Add on one cache and remove from another
multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
// retry same order to rule out any stale state
multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
// Reverse the operations
multiStoreAddDelete(allCaches, 1, 0, "add cache1 del cache0");
// Ensure that working on same cache continues to work.
multiStoreAddDelete(allCaches, 1, 1, "add cache1 del cache1");
}
private void multiStoreAddDelete(List<MetadataCache<MyClass>> caches, int addOn, int delFrom, String testName) throws InterruptedException {
MetadataCache<MyClass> addCache = caches.get(addOn);
MetadataCache<MyClass> delCache = caches.get(delFrom);
String key1 = "/test-key1";
assertEquals(addCache.getIfCached(key1), Optional.empty());
MyClass value1 = new MyClass(testName, 1);
addCache.create(key1, value1).join();
// all time for changes to propagate to other caches
Awaitility.await().ignoreExceptions().untilAsserted(() -> {
for (MetadataCache<MyClass> cache : caches) {
if (cache == addCache) {
assertEquals(cache.getIfCached(key1), Optional.of(value1));
}
assertEquals(cache.get(key1).join(), Optional.of(value1));
assertEquals(cache.getIfCached(key1), Optional.of(value1));
}
});
delCache.delete(key1).join();
// all time for changes to propagate to other caches
Awaitility.await().ignoreExceptions().untilAsserted(() -> {
// The entry should get removed from all caches
for (MetadataCache<MyClass> cache : caches) {
assertEquals(cache.getIfCached(key1), Optional.empty());
assertEquals(cache.get(key1).join(), Optional.empty());
}
});
}
@Test(dataProvider = "impl")
public void insertionDeletionWitGenericType(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<Map<String, String>> objCache = store.getMetadataCache(new TypeReference<Map<String, String>>() {
});
String key1 = newKey();
assertEquals(objCache.getIfCached(key1), Optional.empty());
Map<String, String> v = new TreeMap<>();
v.put("a", "1");
v.put("b", "2");
objCache.create(key1, v).join();
assertEquals(objCache.getIfCached(key1), Optional.of(v));
assertEquals(objCache.get(key1).join(), Optional.of(v));
objCache.delete(key1).join();
assertEquals(objCache.getIfCached(key1), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.empty());
}
@Test(dataProvider = "impl")
public void insertionDeletion(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
assertEquals(objCache.getIfCached(key1), Optional.empty());
MyClass value1 = new MyClass("a", 1);
objCache.create(key1, value1).join();
MyClass value2 = new MyClass("a", 2);
try {
objCache.create(key1, value2).join();
fail("should have failed to create");
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), AlreadyExistsException.class);
}
assertEquals(objCache.getIfCached(key1), Optional.of(value1));
assertEquals(objCache.get(key1).join(), Optional.of(value1));
assertEquals(objCache.readModifyUpdateOrCreate(key1, __ -> value2).join(), value2);
assertEquals(objCache.getIfCached(key1), Optional.of(value2));
assertEquals(objCache.get(key1).join(), Optional.of(value2));
objCache.delete(key1).join();
assertEquals(objCache.getIfCached(key1), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.empty());
}
@Test(dataProvider = "impl")
public void insertionOutsideCache(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
store.put(key1, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(value1), Optional.of(-1L)).join();
assertEquals(objCache.getIfCached(key1), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.of(value1));
}
@Test(dataProvider = "impl")
public void insertionOutsideCacheWithGenericType(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<Map<String, String>> objCache = store.getMetadataCache(new TypeReference<Map<String, String>>() {
});
String key1 = newKey();
Map<String, String> v = new TreeMap<>();
v.put("a", "1");
v.put("b", "2");
store.put(key1, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(v), Optional.of(-1L)).join();
assertEquals(objCache.getIfCached(key1), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.of(v));
}
@Test(dataProvider = "impl")
public void invalidJsonContent(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
store.put(key1, "-------".getBytes(), Optional.of(-1L)).join();
try {
objCache.get(key1).join();
fail("should have failed to deserialize");
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), ContentDeserializationException.class);
}
assertEquals(objCache.getIfCached(key1), Optional.empty());
}
@Test(dataProvider = "impl")
public void testReadCloned(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<Policies> objCache = store.getMetadataCache(Policies.class);
String path = "/testReadCloned-policies";
// init cache
Policies policies = new Policies();
policies.max_unacked_messages_per_consumer = 100;
policies.replication_clusters.add("1");
objCache.create(path, policies).get();
Policies tempPolicies = objCache.get(path).get().get();
assertSame(tempPolicies, objCache.get(path).get().get());
AtomicReference<Policies> reference = new AtomicReference<>(new Policies());
AtomicReference<Policies> reference2 = new AtomicReference<>(new Policies());
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference.set(policies1);
policies1.max_unacked_messages_per_consumer = 200;
return policies1;
}).get();
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference2.set(policies1);
policies1.max_unacked_messages_per_consumer = 300;
return policies1;
}).get();
//The original object should not be modified
assertEquals(tempPolicies.max_unacked_messages_per_consumer.intValue(), 100);
assertNotSame(reference.get(), reference2.get());
assertNotEquals(reference.get().max_unacked_messages_per_consumer
, reference2.get().max_unacked_messages_per_consumer);
}
@Test(dataProvider = "impl")
public void testCloneInReadModifyUpdateOrCreate(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<Policies> objCache = store.getMetadataCache(Policies.class);
String path = "/testCloneInReadModifyUpdateOrCreate-policies";
// init cache
Policies policies = new Policies();
policies.max_unacked_messages_per_consumer = 100;
objCache.create(path, policies).get();
Policies tempPolicies = objCache.get(path).get().get();
assertSame(tempPolicies, objCache.get(path).get().get());
AtomicReference<Policies> reference = new AtomicReference<>(new Policies());
AtomicReference<Policies> reference2 = new AtomicReference<>(new Policies());
objCache.readModifyUpdateOrCreate(path, (policies1) -> {
Policies policiesRef = policies1.get();
assertNotSame(policiesRef, tempPolicies);
reference.set(policiesRef);
policiesRef.max_unacked_messages_per_consumer = 200;
return policiesRef;
}).get();
objCache.readModifyUpdateOrCreate(path, (policies1) -> {
Policies policiesRef = policies1.get();
assertNotSame(policiesRef, tempPolicies);
reference2.set(policiesRef);
policiesRef.max_unacked_messages_per_consumer = 300;
return policiesRef;
}).get();
//The original object should not be modified
assertEquals(tempPolicies.max_unacked_messages_per_consumer.intValue(), 100);
assertNotSame(reference.get(), reference2.get());
assertNotEquals(reference.get().max_unacked_messages_per_consumer
, reference2.get().max_unacked_messages_per_consumer);
}
@Test(dataProvider = "impl")
public void readModifyUpdate(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
objCache.create(key1, value1).join();
assertEquals(objCache.readModifyUpdate(key1, v -> new MyClass(v.a, v.b + 1)).join(),
new MyClass("a", 2));
Optional<MyClass> newValue1 = objCache.get(key1).join();
assertTrue(newValue1.isPresent());
assertEquals(newValue1.get().a, "a");
assertEquals(newValue1.get().b, 2);
// Should fail if the key does not exist
try {
objCache.readModifyUpdate(newKey(), v -> {
return new MyClass(v.a, v.b + 1);
}).join();
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), NotFoundException.class);
}
}
/**
* This test validates that metadata-cache can handle BadVersion failure if other cache/metadata-source updates the
* data with different version.
*
* @throws Exception
*/
@Test
public void readModifyUpdateBadVersionRetry() throws Exception {
String url = zks.getConnectionString();
@Cleanup
MetadataStore sourceStore1 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataStore sourceStore2 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache1 = sourceStore1.getMetadataCache(MyClass.class);
MetadataCache<MyClass> objCache2 = sourceStore2.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
objCache1.create(key1, value1).join();
objCache1.get(key1).join();
objCache2.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
}).join();
objCache1.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
}).join();
}
@Test(dataProvider = "impl")
public void getWithStats(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
Stat stat1 = store.put(key1, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(value1), Optional.of(-1L)).join();
CacheGetResult<MyClass> res = objCache.getWithStats(key1).join().get();
assertEquals(res.getValue(), value1);
assertEquals(res.getStat().getVersion(), stat1.getVersion());
}
@Test(dataProvider = "impl")
public void cacheWithCustomSerde(String provider, String url) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
// Simple serde that convert numbers to ascii
MetadataCache<Integer> objCache = store.getMetadataCache(new MetadataSerde<Integer>() {
@Override
public byte[] serialize(Integer value) throws IOException {
return value.toString().getBytes(StandardCharsets.UTF_8);
}
@Override
public Integer deserialize(byte[] content) throws IOException {
return Integer.parseInt(new String(content, StandardCharsets.UTF_8));
}
});
String key1 = newKey();
objCache.create(key1, 1).join();
assertEquals(objCache.get(key1).join().get(), (Integer) 1);
}
}