GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][broker] Synchronize PulsarService.closeAsync when tearing down … · apache/pulsar@590b97d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-broker/src
    • main/java/org/apache/pulsar/broker
      • PulsarService.java
    • test/java/org/apache/pulsar/broker
      • PulsarServiceTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,14 @@ public CompletableFuture<Void> closeAsync(boolean waitForWebServiceToStop) {
556556
// It only tells the Pulsar clients that this service is not ready to serve for the lookup requests
557557
state = State.Closing;
558558

559-
if (healthChecker != null) {
560-
healthChecker.close();
561-
healthChecker = null;
559+
HealthChecker healthCheckerToClose;
560+
// Under the lock of getHealthChecker(), to also close a checker that is still being created
561+
synchronized (this) {
562+
healthCheckerToClose = this.healthChecker;
563+
this.healthChecker = null;
564+
}
565+
if (healthCheckerToClose != null) {
566+
healthCheckerToClose.close();
562567
}
563568

564569
// close the service in reverse order v.s. in which they are started
Collapse file

‎pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceTest.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceTest.java
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import java.util.concurrent.atomic.AtomicBoolean;
5151
import java.util.concurrent.atomic.AtomicReference;
5252
import java.util.function.BiConsumer;
53+
import java.util.function.Consumer;
54+
import java.util.function.Function;
5355
import lombok.Cleanup;
5456
import lombok.CustomLog;
5557
import org.apache.bookkeeper.mledger.LedgerOffloader;
@@ -68,6 +70,7 @@
6870
import org.apache.pulsar.client.admin.PulsarAdminException;
6971
import org.apache.pulsar.client.api.Producer;
7072
import org.apache.pulsar.client.api.PulsarClientException;
73+
import org.apache.pulsar.client.impl.PulsarClientImpl;
7174
import org.apache.pulsar.common.naming.NamespaceName;
7275
import org.apache.pulsar.common.naming.TopicName;
7376
import org.apache.pulsar.functions.worker.WorkerConfig;
@@ -100,6 +103,8 @@ public class PulsarServiceTest extends MockedPulsarServiceBaseTest {
100103
private boolean useStaticPorts = false;
101104
// When set, what the broker's getCreateAdminClientBuilder() returns instead of a builder of its own
102105
private volatile PulsarAdminBuilder brokerAdminClientBuilder;
106+
private volatile Function<Consumer<PulsarClientImpl.PulsarClientImplBuilder>, PulsarClientImpl>
107+
healthCheckerClientFactory;
103108

104109
@Override
105110
protected void setup() throws Exception {
@@ -115,6 +120,11 @@ protected void beforePulsarStart(PulsarService pulsar) throws Exception {
115120
PulsarAdminBuilder builder = brokerAdminClientBuilder;
116121
return builder != null ? builder : invocation.callRealMethod();
117122
}).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));
118128
}
119129

120130
@AfterMethod(alwaysRun = true)
@@ -123,6 +133,7 @@ protected void cleanup() throws Exception {
123133
super.internalCleanup();
124134
useStaticPorts = false;
125135
brokerAdminClientBuilder = null;
136+
healthCheckerClientFactory = null;
126137
}
127138

128139
@Override
@@ -587,6 +598,36 @@ public void testCloseClosesInternalAdminClientBeingCreated() throws Exception {
587598
verify(adminClient).close();
588599
}
589600

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+
590631
@Test
591632
public void testNoInternalAdminClientCreatedOnceClosed() throws Exception {
592633
setup();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.