|
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; |
@@ -398,7 +408,7 @@ public void testGetTopicPoliciesWithCleanCache() throws Exception { |
398 | 408 | Mockito.doAnswer(invocation -> { |
399 | 409 | Thread.sleep(1000); |
400 | 410 | return invocation.callRealMethod(); |
401 | | - }).when(spyPoliciesCache).get(Mockito.any()); |
| 411 | + }).when(spyPoliciesCache).get(any()); |
402 | 412 |
|
403 | 413 | CompletableFuture<Void> result = new CompletableFuture<>(); |
404 | 414 | Thread thread = new Thread(() -> { |
@@ -532,4 +542,97 @@ 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 testPrepareInitPoliciesCacheAsyncWithErrorReader() 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 | + |
| 596 | + // Since cleanCacheAndCloseReader() is executed, should add the failed reader into readerCache again. |
| 597 | + // Then in SystemTopicBasedTopicPoliciesService, readerCache has a closed reader, |
| 598 | + // and policyCacheInitMap do not contain a future. |
| 599 | + // To simulate the situation: when getTopicPolicy() execute, it will do prepareInitPoliciesCacheAsync() and |
| 600 | + // use a closed reader to reader the __change_event topic. Then throw exception |
| 601 | + spyReaderCaches.put(NamespaceName.get(NAMESPACE5), readerCompletableFuture); |
| 602 | + FieldUtils.writeDeclaredField(spyService, "readerCaches", spyReaderCaches, true); |
| 603 | + |
| 604 | + CompletableFuture<Void> prepareFuture = new CompletableFuture<>(); |
| 605 | + try { |
| 606 | + prepareFuture = spyService.prepareInitPoliciesCacheAsync(NamespaceName.get(NAMESPACE5)); |
| 607 | + prepareFuture.get(); |
| 608 | + Assert.fail(); |
| 609 | + } catch (Exception e) { |
| 610 | + // that is ok |
| 611 | + } |
| 612 | + |
| 613 | + |
| 614 | + // since prepareInitPoliciesCacheAsync() throw exception when initPolicesCache(), |
| 615 | + // would clean readerCache and policyCacheInitMap |
| 616 | + Assert.assertTrue(prepareFuture.isCompletedExceptionally()); |
| 617 | + future = spyService.getPoliciesCacheInit(NamespaceName.get(NAMESPACE5)); |
| 618 | + Assert.assertNull(future); |
| 619 | + CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture1 = |
| 620 | + spyReaderCaches.get(NamespaceName.get(NAMESPACE5)); |
| 621 | + Assert.assertNull(readerCompletableFuture1); |
| 622 | + |
| 623 | + |
| 624 | + // make sure not do cleanCacheAndCloseReader() twice |
| 625 | + boolean logFound = logMessages.stream() |
| 626 | + .anyMatch(msg -> msg.contains("occur exception on reader of __change_events topic")); |
| 627 | + assertTrue(logFound); |
| 628 | + boolean logFound2 = logMessages.stream() |
| 629 | + .anyMatch(msg -> msg.contains("Failed to check the move events for the system topic")); |
| 630 | + assertTrue(logFound2); |
| 631 | + verify(spyService, times(2)).cleanCacheAndCloseReader(any(), anyBoolean(), anyBoolean()); |
| 632 | + |
| 633 | + // make sure not occur Recursive update |
| 634 | + boolean logFound3 = logMessages.stream() |
| 635 | + .anyMatch(msg -> msg.contains("Recursive update")); |
| 636 | + assertFalse(logFound3); |
| 637 | + } |
535 | 638 | } |
0 commit comments