|
19 | 19 | package org.apache.pulsar.metadata.bookkeeper; |
20 | 20 |
|
21 | 21 | import static org.apache.bookkeeper.common.concurrent.FutureUtils.result; |
| 22 | +import static org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE; |
| 23 | +import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY; |
22 | 24 | import static org.mockito.Mockito.mock; |
23 | 25 | import static org.testng.Assert.assertEquals; |
24 | 26 | import static org.testng.Assert.assertFalse; |
|
31 | 33 | import java.util.HashSet; |
32 | 34 | import java.util.List; |
33 | 35 | import java.util.Map; |
| 36 | +import java.util.Optional; |
34 | 37 | import java.util.Queue; |
35 | 38 | import java.util.Set; |
36 | 39 | import java.util.UUID; |
| 40 | +import java.util.concurrent.CompletableFuture; |
37 | 41 | import java.util.concurrent.ConcurrentHashMap; |
38 | 42 | import java.util.concurrent.ConcurrentLinkedQueue; |
39 | 43 | import java.util.concurrent.ExecutionException; |
| 44 | +import java.util.concurrent.TimeUnit; |
40 | 45 | import java.util.function.Supplier; |
41 | 46 | import lombok.Cleanup; |
42 | 47 | import lombok.CustomLog; |
|
51 | 56 | import org.apache.bookkeeper.versioning.Version; |
52 | 57 | import org.apache.bookkeeper.versioning.Versioned; |
53 | 58 | import org.apache.pulsar.metadata.BaseMetadataStoreTest; |
| 59 | +import org.apache.pulsar.metadata.api.GetResult; |
54 | 60 | import org.apache.pulsar.metadata.api.MetadataStoreConfig; |
| 61 | +import org.apache.pulsar.metadata.api.MetadataStoreException; |
| 62 | +import org.apache.pulsar.metadata.api.Option; |
55 | 63 | import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; |
| 64 | +import org.apache.pulsar.metadata.impl.LocalMemoryMetadataStore; |
56 | 65 | import org.apache.zookeeper.ZooKeeper; |
57 | 66 | import org.awaitility.Awaitility; |
| 67 | +import org.testng.annotations.DataProvider; |
58 | 68 | import org.testng.annotations.Test; |
59 | 69 |
|
60 | 70 | /** |
@@ -305,6 +315,67 @@ public void testGetAllBookies(String provider, Supplier<String> urlSupplier) thr |
305 | 315 | assertEquals(result.getValue().size(), addresses.size()); |
306 | 316 | } |
307 | 317 |
|
| 318 | + @Test(dataProvider = "readOnly") |
| 319 | + public void testBookieInfoReloadedAfterFailedRead(boolean readOnly) throws Exception { |
| 320 | + @Cleanup |
| 321 | + FailingReadsMetadataStore store = new FailingReadsMetadataStore("memory:" + UUID.randomUUID()); |
| 322 | + |
| 323 | + String ledgersRoot = "/test/ledgers-" + UUID.randomUUID(); |
| 324 | + |
| 325 | + @Cleanup |
| 326 | + RegistrationManager rm = new PulsarRegistrationManager(store, ledgersRoot, mock(AbstractConfiguration.class)); |
| 327 | + |
| 328 | + @Cleanup |
| 329 | + RegistrationClient rc = new PulsarRegistrationClient(store, ledgersRoot); |
| 330 | + |
| 331 | + BookieId bookie = new BookieSocketAddress("127.0.0.1", 3181).toBookieId(); |
| 332 | + String registrationPath = ledgersRoot + "/" + AVAILABLE_NODE + (readOnly ? "/" + READONLY : "") + "/" + bookie; |
| 333 | + |
| 334 | + // The read of the bookie info, triggered by the registration notification, fails |
| 335 | + CompletableFuture<Void> failedRead = store.failNextRead(registrationPath); |
| 336 | + rm.registerBookie(bookie, readOnly, new BookieServiceInfo()); |
| 337 | + failedRead.get(10, TimeUnit.SECONDS); |
| 338 | + |
| 339 | + // The bookie is registered but missing from the client cache, and no other notification will follow |
| 340 | + assertTrue(expectThrows(ExecutionException.class, () -> rc.getBookieServiceInfo(bookie).get()) |
| 341 | + .getCause() instanceof BKException.BKBookieHandleNotAvailableException); |
| 342 | + |
| 343 | + // The miss reloads the bookie info, so a retry finds it |
| 344 | + Awaitility.await().ignoreExceptions().untilAsserted(() -> rc.getBookieServiceInfo(bookie).get()); |
| 345 | + } |
| 346 | + |
| 347 | + @DataProvider(name = "readOnly") |
| 348 | + public static Object[][] readOnly() { |
| 349 | + return new Object[][] {{false}, {true}}; |
| 350 | + } |
| 351 | + |
| 352 | + private static class FailingReadsMetadataStore extends LocalMemoryMetadataStore { |
| 353 | + private final Map<String, CompletableFuture<Void>> failingReads = new ConcurrentHashMap<>(); |
| 354 | + |
| 355 | + FailingReadsMetadataStore(String url) throws MetadataStoreException { |
| 356 | + super(url, MetadataStoreConfig.builder().build()); |
| 357 | + } |
| 358 | + |
| 359 | + /** |
| 360 | + * Fail the next read of the path. The returned future completes once the read has failed. |
| 361 | + */ |
| 362 | + CompletableFuture<Void> failNextRead(String path) { |
| 363 | + CompletableFuture<Void> failed = new CompletableFuture<>(); |
| 364 | + failingReads.put(path, failed); |
| 365 | + return failed; |
| 366 | + } |
| 367 | + |
| 368 | + @Override |
| 369 | + public CompletableFuture<Optional<GetResult>> storeGet(String path, Set<Option> opts) { |
| 370 | + CompletableFuture<Void> failed = failingReads.remove(path); |
| 371 | + if (failed == null) { |
| 372 | + return super.storeGet(path, opts); |
| 373 | + } |
| 374 | + failed.complete(null); |
| 375 | + return CompletableFuture.failedFuture(new MetadataStoreException("Injected read failure")); |
| 376 | + } |
| 377 | + } |
| 378 | + |
308 | 379 | @Test(dataProvider = "impl") |
309 | 380 | public void testWatchWritableBookiesSuccess(String provider, Supplier<String> urlSupplier) throws Exception { |
310 | 381 | testWatchBookiesSuccess(provider, urlSupplier, true); |
|