|
19 | 19 | package org.apache.pulsar.broker.service; |
20 | 20 |
|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
22 | 24 | import static org.mockito.Mockito.spy; |
| 25 | +import static org.mockito.Mockito.times; |
| 26 | +import static org.mockito.Mockito.verify; |
23 | 27 | import static org.testng.AssertJUnit.assertEquals; |
| 28 | +import static org.testng.AssertJUnit.assertFalse; |
24 | 29 | import static org.testng.AssertJUnit.assertNotNull; |
25 | 30 | import static org.testng.AssertJUnit.assertNull; |
26 | 31 | import static org.testng.AssertJUnit.assertTrue; |
27 | 32 | import java.lang.reflect.Field; |
28 | 33 | import java.time.Duration; |
| 34 | +import java.util.ArrayList; |
29 | 35 | import java.util.HashSet; |
30 | 36 | import java.util.List; |
31 | 37 | import java.util.Map; |
|
42 | 48 | import lombok.Cleanup; |
43 | 49 | import lombok.extern.slf4j.Slf4j; |
44 | 50 | import org.apache.commons.lang3.reflect.FieldUtils; |
| 51 | +import org.apache.logging.log4j.LogManager; |
| 52 | +import org.apache.logging.log4j.core.LogEvent; |
| 53 | +import org.apache.logging.log4j.core.Logger; |
| 54 | +import org.apache.logging.log4j.core.appender.AbstractAppender; |
45 | 55 | import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; |
46 | 56 | import org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException; |
47 | 57 | import org.apache.pulsar.broker.systopic.SystemTopicClient; |
@@ -532,4 +542,186 @@ public void testCreateNamespaceEventsSystemTopicFactoryException() throws Except |
532 | 542 | Assert.assertNotNull(topicPolicies); |
533 | 543 | Assert.assertEquals(topicPolicies.getMaxConsumerPerTopic(), 10); |
534 | 544 | } |
| 545 | + |
| 546 | + @Test |
| 547 | + public void testPrepareInitPoliciesCacheAsyncThrowExceptionAfterCreateReader() throws Exception { |
| 548 | + // catch the log output in SystemTopicBasedTopicPoliciesService |
| 549 | + Logger logger = (Logger) LogManager.getLogger(SystemTopicBasedTopicPoliciesService.class); |
| 550 | + List<String> logMessages = new ArrayList<>(); |
| 551 | + AbstractAppender appender = new AbstractAppender("TestAppender", null, null) { |
| 552 | + @Override |
| 553 | + public void append(LogEvent event) { |
| 554 | + logMessages.add(event.getMessage().getFormattedMessage()); |
| 555 | + } |
| 556 | + }; |
| 557 | + appender.start(); |
| 558 | + logger.get().addAppender(appender, null, null); |
| 559 | + logger.addAppender(appender); |
| 560 | + |
| 561 | + // create namespace-5 and topic |
| 562 | + SystemTopicBasedTopicPoliciesService spyService = Mockito.spy(new SystemTopicBasedTopicPoliciesService(pulsar)); |
| 563 | + FieldUtils.writeField(pulsar, "topicPoliciesService", spyService, true); |
| 564 | + |
| 565 | + |
| 566 | + admin.namespaces().createNamespace(NAMESPACE5); |
| 567 | + final String topic = "persistent://" + NAMESPACE5 + "/test" + UUID.randomUUID(); |
| 568 | + admin.topics().createPartitionedTopic(topic, 1); |
| 569 | + |
| 570 | + CompletableFuture<Void> future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 571 | + Assert.assertNull(future); |
| 572 | + |
| 573 | + // mock readerCache and new a reader, then put this reader in readerCache. |
| 574 | + // when new reader, would trigger __change_event topic of namespace-5 created |
| 575 | + // and would trigger prepareInitPoliciesCacheAsync() |
| 576 | + ConcurrentHashMap<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> |
| 577 | + spyReaderCaches = new ConcurrentHashMap<>(); |
| 578 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = |
| 579 | + spyService.createSystemTopicClient(NamespaceName.get(NAMESPACE5)); |
| 580 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 581 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 582 | + |
| 583 | + // set topic policy. create producer for __change_event topic |
| 584 | + admin.topicPolicies().setMaxConsumersPerSubscription(topic, 1); |
| 585 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 586 | + Assert.assertNotNull(future); |
| 587 | + |
| 588 | + // trigger close reader of __change_event directly, simulate that reader |
| 589 | + // is closed for some reason, such as topic unload or broker restart. |
| 590 | + // since prepareInitPoliciesCacheAsync() has been executed, it would go into readMorePoliciesAsync(), |
| 591 | + // throw exception, output "Closing the topic policies reader for" and do cleanCacheAndCloseReader() |
| 592 | + SystemTopicClient.Reader<PulsarEvent> reader = readerCompletableFuture.get(); |
| 593 | + reader.close(); |
| 594 | + log.info("successfully close spy reader"); |
| 595 | + Awaitility.await().untilAsserted(() -> { |
| 596 | + boolean logFound = logMessages.stream() |
| 597 | + .anyMatch(msg -> msg.contains("Closing the topic policies reader for")); |
| 598 | + assertTrue(logFound); |
| 599 | + }); |
| 600 | + |
| 601 | + // Since cleanCacheAndCloseReader() is executed, should add the failed reader into readerCache again. |
| 602 | + // Then in SystemTopicBasedTopicPoliciesService, readerCache has a closed reader, |
| 603 | + // and policyCacheInitMap do not contain a future. |
| 604 | + // To simulate the situation: when getTopicPolicy() execute, it will do prepareInitPoliciesCacheAsync() and |
| 605 | + // use a closed reader to read the __change_event topic. Then throw exception |
| 606 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 607 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 608 | + |
| 609 | + CompletableFuture<Void> prepareFuture = new CompletableFuture<>(); |
| 610 | + try { |
| 611 | + prepareFuture = spyService.prepareInitPoliciesCacheAsync(NamespaceName.get(NAMESPACE5)); |
| 612 | + prepareFuture.get(); |
| 613 | + Assert.fail(); |
| 614 | + } catch (Exception e) { |
| 615 | + // that is ok |
| 616 | + } |
| 617 | + |
| 618 | + |
| 619 | + // since prepareInitPoliciesCacheAsync() throw exception when initPolicesCache(), |
| 620 | + // would clean readerCache and policyCacheInitMap |
| 621 | + // sleep 500ms to make sure clean operation finish. |
| 622 | + Thread.sleep(500); |
| 623 | + Assert.assertTrue(prepareFuture.isCompletedExceptionally()); |
| 624 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 625 | + Assert.assertNull(future); |
| 626 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture1 = |
| 627 | + spyReaderCaches.get(NamespaceName.get(NAMESPACE5)); |
| 628 | + Assert.assertNull(readerCompletableFuture1); |
| 629 | + |
| 630 | + |
| 631 | + // make sure not do cleanCacheAndCloseReader() twice |
| 632 | + // totally trigger prepareInitPoliciesCacheAsync() twice, so the time of cleanCacheAndCloseReader() is 2. |
| 633 | + // in previous code, the time would be 3 |
| 634 | + boolean logFound = logMessages.stream() |
| 635 | + .anyMatch(msg -> msg.contains("occur exception on reader of __change_events topic")); |
| 636 | + assertTrue(logFound); |
| 637 | + boolean logFound2 = logMessages.stream() |
| 638 | + .anyMatch(msg -> msg.contains("Failed to check the move events for the system topic")); |
| 639 | + assertTrue(logFound2); |
| 640 | + verify(spyService, times(2)).cleanCacheAndCloseReader(any(), anyBoolean(), anyBoolean()); |
| 641 | + |
| 642 | + // make sure not occur Recursive update |
| 643 | + boolean logFound3 = logMessages.stream() |
| 644 | + .anyMatch(msg -> msg.contains("Recursive update")); |
| 645 | + assertFalse(logFound3); |
| 646 | + |
| 647 | + // clean log appender |
| 648 | + appender.stop(); |
| 649 | + logger.removeAppender(appender); |
| 650 | + } |
| 651 | + |
| 652 | + @Test |
| 653 | + public void testPrepareInitPoliciesCacheAsyncThrowExceptionInCreateReader() throws Exception { |
| 654 | + // catch the log output in SystemTopicBasedTopicPoliciesService |
| 655 | + Logger logger = (Logger) LogManager.getLogger(SystemTopicBasedTopicPoliciesService.class); |
| 656 | + List<String> logMessages = new ArrayList<>(); |
| 657 | + AbstractAppender appender = new AbstractAppender("TestAppender", null, null) { |
| 658 | + @Override |
| 659 | + public void append(LogEvent event) { |
| 660 | + logMessages.add(event.getMessage().getFormattedMessage()); |
| 661 | + } |
| 662 | + }; |
| 663 | + appender.start(); |
| 664 | + logger.get().addAppender(appender, null, null); |
| 665 | + logger.addAppender(appender); |
| 666 | + |
| 667 | + // create namespace-5 and topic |
| 668 | + SystemTopicBasedTopicPoliciesService spyService = |
| 669 | + Mockito.spy(new SystemTopicBasedTopicPoliciesService(pulsar)); |
| 670 | + FieldUtils.writeField(pulsar, "topicPoliciesService", spyService, true); |
| 671 | + |
| 672 | + |
| 673 | + admin.namespaces().createNamespace(NAMESPACE5); |
| 674 | + final String topic = "persistent://" + NAMESPACE5 + "/test" + UUID.randomUUID(); |
| 675 | + admin.topics().createPartitionedTopic(topic, 1); |
| 676 | + |
| 677 | + CompletableFuture<Void> future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 678 | + Assert.assertNull(future); |
| 679 | + |
| 680 | + // mock readerCache and put a failed readerCreateFuture in readerCache. |
| 681 | + // simulate that when trigger prepareInitPoliciesCacheAsync(), |
| 682 | + // it would use this failed readerFuture and go into corresponding logic |
| 683 | + ConcurrentHashMap<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> |
| 684 | + spyReaderCaches = new ConcurrentHashMap<>(); |
| 685 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = new CompletableFuture<>(); |
| 686 | + readerCompletableFuture.completeExceptionally(new Exception("create reader fail")); |
| 687 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 688 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 689 | + |
| 690 | + // trigger prepareInitPoliciesCacheAsync() |
| 691 | + CompletableFuture<Void> prepareFuture = new CompletableFuture<>(); |
| 692 | + try { |
| 693 | + prepareFuture = spyService.prepareInitPoliciesCacheAsync(NamespaceName.get(NAMESPACE5)); |
| 694 | + prepareFuture.get(); |
| 695 | + Assert.fail(); |
| 696 | + } catch (Exception e) { |
| 697 | + // that is ok |
| 698 | + } |
| 699 | + |
| 700 | + // since prepareInitPoliciesCacheAsync() throw exception when createReader, |
| 701 | + // would clean readerCache and policyCacheInitMap. |
| 702 | + // sleep 500ms to make sure clean operation finish. |
| 703 | + Thread.sleep(500); |
| 704 | + Assert.assertTrue(prepareFuture.isCompletedExceptionally()); |
| 705 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 706 | + Assert.assertNull(future); |
| 707 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture1 = |
| 708 | + spyReaderCaches.get(NamespaceName.get(NAMESPACE5)); |
| 709 | + Assert.assertNull(readerCompletableFuture1); |
| 710 | + |
| 711 | + |
| 712 | + // make sure not do cleanCacheAndCloseReader() twice |
| 713 | + // totally trigger prepareInitPoliciesCacheAsync() once, so the time of cleanCacheAndCloseReader() is 1. |
| 714 | + boolean logFound = logMessages.stream() |
| 715 | + .anyMatch(msg -> msg.contains("occur exception on reader of __change_events topic")); |
| 716 | + assertTrue(logFound); |
| 717 | + boolean logFound2 = logMessages.stream() |
| 718 | + .anyMatch(msg -> msg.contains("Failed to check the move events for the system topic") |
| 719 | + || msg.contains("Failed to read event from the system topic")); |
| 720 | + assertFalse(logFound2); |
| 721 | + verify(spyService, times(1)).cleanCacheAndCloseReader(any(), anyBoolean(), anyBoolean()); |
| 722 | + |
| 723 | + // clean log appender |
| 724 | + appender.stop(); |
| 725 | + logger.removeAppender(appender); |
| 726 | + } |
535 | 727 | } |
0 commit comments