|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.broker.service; |
20 | 20 |
|
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
21 | 23 | import static org.mockito.Mockito.spy; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
22 | 26 | import static org.testng.AssertJUnit.assertEquals; |
| 27 | +import static org.testng.AssertJUnit.assertFalse; |
23 | 28 | import static org.testng.AssertJUnit.assertNotNull; |
24 | 29 | import static org.testng.AssertJUnit.assertNull; |
| 30 | +import static org.testng.AssertJUnit.assertTrue; |
| 31 | +import java.util.ArrayList; |
25 | 32 | import java.util.HashSet; |
26 | 33 | import java.util.List; |
27 | 34 | import java.util.Map; |
|
36 | 43 | import lombok.Cleanup; |
37 | 44 | import lombok.extern.slf4j.Slf4j; |
38 | 45 | import org.apache.commons.lang3.reflect.FieldUtils; |
| 46 | +import org.apache.logging.log4j.LogManager; |
| 47 | +import org.apache.logging.log4j.core.LogEvent; |
| 48 | +import org.apache.logging.log4j.core.Logger; |
| 49 | +import org.apache.logging.log4j.core.appender.AbstractAppender; |
39 | 50 | import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; |
40 | 51 | import org.apache.pulsar.broker.systopic.SystemTopicClient; |
41 | 52 | import org.apache.pulsar.client.admin.PulsarAdminException; |
@@ -465,4 +476,186 @@ public void testCreateNamespaceEventsSystemTopicFactoryException() throws Except |
465 | 476 | Assert.assertNotNull(topicPolicies); |
466 | 477 | Assert.assertEquals(topicPolicies.getMaxConsumerPerTopic(), 10); |
467 | 478 | } |
| 479 | + |
| 480 | + @Test |
| 481 | + public void testPrepareInitPoliciesCacheAsyncThrowExceptionAfterCreateReader() throws Exception { |
| 482 | + // catch the log output in SystemTopicBasedTopicPoliciesService |
| 483 | + Logger logger = (Logger) LogManager.getLogger(SystemTopicBasedTopicPoliciesService.class); |
| 484 | + List<String> logMessages = new ArrayList<>(); |
| 485 | + AbstractAppender appender = new AbstractAppender("TestAppender", null, null) { |
| 486 | + @Override |
| 487 | + public void append(LogEvent event) { |
| 488 | + logMessages.add(event.getMessage().getFormattedMessage()); |
| 489 | + } |
| 490 | + }; |
| 491 | + appender.start(); |
| 492 | + logger.addAppender(appender); |
| 493 | + |
| 494 | + // create namespace-5 and topic |
| 495 | + SystemTopicBasedTopicPoliciesService spyService = |
| 496 | + Mockito.spy(new SystemTopicBasedTopicPoliciesService(pulsar)); |
| 497 | + FieldUtils.writeField(pulsar, "topicPoliciesService", spyService, true); |
| 498 | + |
| 499 | + |
| 500 | + admin.namespaces().createNamespace(NAMESPACE5); |
| 501 | + final String topic = "persistent://" + NAMESPACE5 + "/test" + UUID.randomUUID(); |
| 502 | + admin.topics().createPartitionedTopic(topic, 1); |
| 503 | + |
| 504 | + CompletableFuture<Void> future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 505 | + Assert.assertNull(future); |
| 506 | + |
| 507 | + // mock readerCache and new a reader, then put this reader in readerCache. |
| 508 | + // when new reader, would trigger __change_event topic of namespace-5 created |
| 509 | + // and would trigger prepareInitPoliciesCacheAsync() |
| 510 | + ConcurrentHashMap<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> |
| 511 | + spyReaderCaches = new ConcurrentHashMap<>(); |
| 512 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = |
| 513 | + spyService.createSystemTopicClient(NamespaceName.get(NAMESPACE5)); |
| 514 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 515 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 516 | + |
| 517 | + // set topic policy. create producer for __change_event topic |
| 518 | + admin.topicPolicies().setMaxConsumersPerSubscription(topic, 1); |
| 519 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 520 | + Assert.assertNotNull(future); |
| 521 | + |
| 522 | + // trigger close reader of __change_event directly, simulate that reader |
| 523 | + // is closed for some reason, such as topic unload or broker restart. |
| 524 | + // since prepareInitPoliciesCacheAsync() has been executed, it would go into readMorePoliciesAsync(), |
| 525 | + // throw exception, output "Closing the topic policies reader for" and do cleanPoliciesCacheInitMap() |
| 526 | + SystemTopicClient.Reader<PulsarEvent> reader = readerCompletableFuture.get(); |
| 527 | + reader.close(); |
| 528 | + log.info("successfully close spy reader"); |
| 529 | + Awaitility.await().untilAsserted(() -> { |
| 530 | + boolean logFound = logMessages.stream() |
| 531 | + .anyMatch(msg -> msg.contains("Closing the topic policies reader for")); |
| 532 | + assertTrue(logFound); |
| 533 | + }); |
| 534 | + |
| 535 | + |
| 536 | + // Since cleanPoliciesCacheInitMap() is executed, should add the failed reader into readerCache again. |
| 537 | + // Then in SystemTopicBasedTopicPoliciesService, readerCache has a closed reader, |
| 538 | + // and policyCacheInitMap do not contain a future. |
| 539 | + // To simulate the situation: when getTopicPolicy() execute, it will do prepareInitPoliciesCacheAsync() and |
| 540 | + // use a closed reader to read the __change_event topic. Then throw exception |
| 541 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 542 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 543 | + |
| 544 | + CompletableFuture<Boolean> prepareFuture = new CompletableFuture<>(); |
| 545 | + try { |
| 546 | + prepareFuture = spyService.prepareInitPoliciesCacheAsync(NamespaceName.get(NAMESPACE5)); |
| 547 | + prepareFuture.get(); |
| 548 | + Assert.fail(); |
| 549 | + } catch (Exception e) { |
| 550 | + // that is ok |
| 551 | + } |
| 552 | + |
| 553 | + // since prepareInitPoliciesCacheAsync() throw exception when initPolicesCache(), |
| 554 | + // would clean readerCache and policyCacheInitMap. |
| 555 | + // sleep 500ms to make sure clean operation finish. |
| 556 | + Thread.sleep(500); |
| 557 | + Assert.assertTrue(prepareFuture.isCompletedExceptionally()); |
| 558 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 559 | + Assert.assertNull(future); |
| 560 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture1 = |
| 561 | + spyReaderCaches.get(NamespaceName.get(NAMESPACE5)); |
| 562 | + Assert.assertNull(readerCompletableFuture1); |
| 563 | + |
| 564 | + |
| 565 | + // make sure not do cleanPoliciesCacheInitMap() twice |
| 566 | + // totally trigger prepareInitPoliciesCacheAsync() twice, so the time of cleanCacheAndCloseReader() is 2. |
| 567 | + // in previous code, the time would be 3 |
| 568 | + boolean logFound = logMessages.stream() |
| 569 | + .anyMatch(msg -> msg.contains("Failed to create reader on __change_events topic")); |
| 570 | + assertFalse(logFound); |
| 571 | + boolean logFound2 = logMessages.stream() |
| 572 | + .anyMatch(msg -> msg.contains("Failed to check the move events for the system topic")); |
| 573 | + assertTrue(logFound2); |
| 574 | + verify(spyService, times(2)).cleanPoliciesCacheInitMap(any(), anyBoolean()); |
| 575 | + |
| 576 | + // make sure not occur Recursive update |
| 577 | + boolean logFound3 = logMessages.stream() |
| 578 | + .anyMatch(msg -> msg.contains("Recursive update")); |
| 579 | + assertFalse(logFound3); |
| 580 | + |
| 581 | + // clean log appender |
| 582 | + appender.stop(); |
| 583 | + logger.removeAppender(appender); |
| 584 | + } |
| 585 | + |
| 586 | + @Test |
| 587 | + public void testPrepareInitPoliciesCacheAsyncThrowExceptionInCreateReader() throws Exception { |
| 588 | + // catch the log output in SystemTopicBasedTopicPoliciesService |
| 589 | + Logger logger = (Logger) LogManager.getLogger(SystemTopicBasedTopicPoliciesService.class); |
| 590 | + List<String> logMessages = new ArrayList<>(); |
| 591 | + AbstractAppender appender = new AbstractAppender("TestAppender", null, null) { |
| 592 | + @Override |
| 593 | + public void append(LogEvent event) { |
| 594 | + logMessages.add(event.getMessage().getFormattedMessage()); |
| 595 | + } |
| 596 | + }; |
| 597 | + appender.start(); |
| 598 | + logger.get().addAppender(appender, null, null); |
| 599 | + logger.addAppender(appender); |
| 600 | + |
| 601 | + // create namespace-5 and topic |
| 602 | + SystemTopicBasedTopicPoliciesService spyService = |
| 603 | + Mockito.spy(new SystemTopicBasedTopicPoliciesService(pulsar)); |
| 604 | + FieldUtils.writeField(pulsar, "topicPoliciesService", spyService, true); |
| 605 | + |
| 606 | + |
| 607 | + admin.namespaces().createNamespace(NAMESPACE5); |
| 608 | + final String topic = "persistent://" + NAMESPACE5 + "/test" + UUID.randomUUID(); |
| 609 | + admin.topics().createPartitionedTopic(topic, 1); |
| 610 | + |
| 611 | + CompletableFuture<Void> future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 612 | + Assert.assertNull(future); |
| 613 | + |
| 614 | + // mock readerCache and put a failed readerCreateFuture in readerCache. |
| 615 | + // simulate that when trigger prepareInitPoliciesCacheAsync(), |
| 616 | + // it would use this failed readerFuture and go into corresponding logic |
| 617 | + ConcurrentHashMap<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> |
| 618 | + spyReaderCaches = new ConcurrentHashMap<>(); |
| 619 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = new CompletableFuture<>(); |
| 620 | + readerCompletableFuture.completeExceptionally(new Exception("create reader fail")); |
| 621 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 622 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 623 | + |
| 624 | + // trigger prepareInitPoliciesCacheAsync() |
| 625 | + CompletableFuture<Boolean> prepareFuture = new CompletableFuture<>(); |
| 626 | + try { |
| 627 | + prepareFuture = spyService.prepareInitPoliciesCacheAsync(NamespaceName.get(NAMESPACE5)); |
| 628 | + prepareFuture.get(); |
| 629 | + Assert.fail(); |
| 630 | + } catch (Exception e) { |
| 631 | + // that is ok |
| 632 | + } |
| 633 | + |
| 634 | + // since prepareInitPoliciesCacheAsync() throw exception when createReader, |
| 635 | + // would clean readerCache and policyCacheInitMap. |
| 636 | + // sleep 500ms to make sure clean operation finish. |
| 637 | + Thread.sleep(500); |
| 638 | + Assert.assertTrue(prepareFuture.isCompletedExceptionally()); |
| 639 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 640 | + Assert.assertNull(future); |
| 641 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture1 = |
| 642 | + spyReaderCaches.get(NamespaceName.get(NAMESPACE5)); |
| 643 | + Assert.assertNull(readerCompletableFuture1); |
| 644 | + |
| 645 | + |
| 646 | + // make sure not do cleanPoliciesCacheInitMap() twice |
| 647 | + // totally trigger prepareInitPoliciesCacheAsync() once, so the time of cleanCacheAndCloseReader() is 1. |
| 648 | + boolean logFound = logMessages.stream() |
| 649 | + .anyMatch(msg -> msg.contains("Failed to create reader on __change_events topic")); |
| 650 | + assertTrue(logFound); |
| 651 | + boolean logFound2 = logMessages.stream() |
| 652 | + .anyMatch(msg -> msg.contains("Failed to check the move events for the system topic") |
| 653 | + || msg.contains("Failed to read event from the system topic")); |
| 654 | + assertFalse(logFound2); |
| 655 | + verify(spyService, times(1)).cleanPoliciesCacheInitMap(any(), anyBoolean()); |
| 656 | + |
| 657 | + // clean log appender |
| 658 | + appender.stop(); |
| 659 | + logger.removeAppender(appender); |
| 660 | + } |
468 | 661 | } |
0 commit comments