|
50 | 50 | import java.util.concurrent.atomic.AtomicBoolean; |
51 | 51 | import java.util.concurrent.atomic.AtomicReference; |
52 | 52 | import java.util.function.BiConsumer; |
| 53 | +import java.util.function.Consumer; |
| 54 | +import java.util.function.Function; |
53 | 55 | import lombok.Cleanup; |
54 | 56 | import lombok.CustomLog; |
55 | 57 | import org.apache.bookkeeper.mledger.LedgerOffloader; |
|
68 | 70 | import org.apache.pulsar.client.admin.PulsarAdminException; |
69 | 71 | import org.apache.pulsar.client.api.Producer; |
70 | 72 | import org.apache.pulsar.client.api.PulsarClientException; |
| 73 | +import org.apache.pulsar.client.impl.PulsarClientImpl; |
71 | 74 | import org.apache.pulsar.common.naming.NamespaceName; |
72 | 75 | import org.apache.pulsar.common.naming.TopicName; |
73 | 76 | import org.apache.pulsar.functions.worker.WorkerConfig; |
@@ -100,6 +103,8 @@ public class PulsarServiceTest extends MockedPulsarServiceBaseTest { |
100 | 103 | private boolean useStaticPorts = false; |
101 | 104 | // When set, what the broker's getCreateAdminClientBuilder() returns instead of a builder of its own |
102 | 105 | private volatile PulsarAdminBuilder brokerAdminClientBuilder; |
| 106 | + private volatile Function<Consumer<PulsarClientImpl.PulsarClientImplBuilder>, PulsarClientImpl> |
| 107 | + healthCheckerClientFactory; |
103 | 108 |
|
104 | 109 | @Override |
105 | 110 | protected void setup() throws Exception { |
@@ -115,6 +120,11 @@ protected void beforePulsarStart(PulsarService pulsar) throws Exception { |
115 | 120 | PulsarAdminBuilder builder = brokerAdminClientBuilder; |
116 | 121 | return builder != null ? builder : invocation.callRealMethod(); |
117 | 122 | }).when(pulsar).getCreateAdminClientBuilder(); |
| 123 | + doAnswer(invocation -> { |
| 124 | + Function<Consumer<PulsarClientImpl.PulsarClientImplBuilder>, PulsarClientImpl> factory = |
| 125 | + healthCheckerClientFactory; |
| 126 | + return factory != null ? factory.apply(invocation.getArgument(0)) : invocation.callRealMethod(); |
| 127 | + }).when(pulsar).createClientImpl(any(Consumer.class)); |
118 | 128 | } |
119 | 129 |
|
120 | 130 | @AfterMethod(alwaysRun = true) |
@@ -123,6 +133,7 @@ protected void cleanup() throws Exception { |
123 | 133 | super.internalCleanup(); |
124 | 134 | useStaticPorts = false; |
125 | 135 | brokerAdminClientBuilder = null; |
| 136 | + healthCheckerClientFactory = null; |
126 | 137 | } |
127 | 138 |
|
128 | 139 | @Override |
@@ -587,6 +598,36 @@ public void testCloseClosesInternalAdminClientBeingCreated() throws Exception { |
587 | 598 | verify(adminClient).close(); |
588 | 599 | } |
589 | 600 |
|
| 601 | + @Test |
| 602 | + public void testCloseClosesHealthCheckerBeingCreated() throws Exception { |
| 603 | + setup(); |
| 604 | + PulsarClientImpl healthCheckerClient = mock(PulsarClientImpl.class); |
| 605 | + AtomicBoolean monitorHeldOnClose = new AtomicBoolean(false); |
| 606 | + doAnswer(inv -> { |
| 607 | + monitorHeldOnClose.set(Thread.holdsLock(pulsar)); |
| 608 | + return null; |
| 609 | + }).when(healthCheckerClient).close(); |
| 610 | + Thread closingThread = new Thread(pulsar::closeAsync, "closing-broker"); |
| 611 | + // The broker starts closing while getHealthChecker() creates the health checker under the service's lock, |
| 612 | + // and the creation ends once the close waits for that lock |
| 613 | + healthCheckerClientFactory = customizer -> { |
| 614 | + Thread creatingThread = Thread.currentThread(); |
| 615 | + closingThread.start(); |
| 616 | + Awaitility.await().until(() -> { |
| 617 | + ThreadInfo threadInfo = ManagementFactory.getThreadMXBean().getThreadInfo(closingThread.threadId()); |
| 618 | + return threadInfo != null |
| 619 | + && threadInfo.getThreadState() == Thread.State.BLOCKED |
| 620 | + && threadInfo.getLockOwnerId() == creatingThread.threadId(); |
| 621 | + }); |
| 622 | + return healthCheckerClient; |
| 623 | + }; |
| 624 | + |
| 625 | + pulsar.getHealthChecker(); |
| 626 | + assertThat(closingThread.join(Duration.ofSeconds(30))).as("close finished").isTrue(); |
| 627 | + verify(healthCheckerClient).close(); |
| 628 | + assertThat(monitorHeldOnClose.get()).isFalse(); |
| 629 | + } |
| 630 | + |
590 | 631 | @Test |
591 | 632 | public void testNoInternalAdminClientCreatedOnceClosed() throws Exception { |
592 | 633 | setup(); |
|