|
25 | 25 | import java.nio.channels.SelectionKey; |
26 | 26 | import java.util.Collections; |
27 | 27 | import java.util.Optional; |
| 28 | +import java.util.concurrent.CompletableFuture; |
28 | 29 | import java.util.concurrent.atomic.AtomicBoolean; |
29 | 30 | import lombok.extern.slf4j.Slf4j; |
30 | 31 | import org.apache.pulsar.broker.PulsarService; |
@@ -65,7 +66,8 @@ public abstract class CanReconnectZKClientPulsarServiceBaseTest extends TestRetr |
65 | 66 | protected PulsarClient client; |
66 | 67 | protected ZooKeeper localZkOfBroker; |
67 | 68 | protected Object localMetaDataStoreClientCnx; |
68 | | - protected final AtomicBoolean LocalMetadataStoreInReconnectFinishSignal = new AtomicBoolean(); |
| 69 | + protected final AtomicBoolean connectionTerminationThreadKeepRunning = new AtomicBoolean(); |
| 70 | + private volatile Thread connectionTerminationThread; |
69 | 71 |
|
70 | 72 | protected void startZKAndBK() throws Exception { |
71 | 73 | // Start ZK. |
@@ -95,54 +97,66 @@ protected void startBrokers() throws Exception { |
95 | 97 | client = PulsarClient.builder().serviceUrl(url.toString()).build(); |
96 | 98 | } |
97 | 99 |
|
98 | | - protected void makeLocalMetadataStoreKeepReconnect() throws Exception { |
99 | | - if (!LocalMetadataStoreInReconnectFinishSignal.compareAndSet(false, true)) { |
100 | | - throw new RuntimeException("Local metadata store is already keeping reconnect"); |
| 100 | + protected void startLocalMetadataStoreConnectionTermination() throws Exception { |
| 101 | + if (!connectionTerminationThreadKeepRunning.compareAndSet(false, true)) { |
| 102 | + throw new RuntimeException("Local metadata store connection is already being terminated"); |
101 | 103 | } |
| 104 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
102 | 105 | if (localMetaDataStoreClientCnx.getClass().getSimpleName().equals("ClientCnxnSocketNIO")) { |
103 | | - makeLocalMetadataStoreKeepReconnectNIO(); |
| 106 | + startNIOImplTermination(future); |
104 | 107 | } else { |
105 | 108 | // ClientCnxnSocketNetty. |
106 | | - makeLocalMetadataStoreKeepReconnectNetty(); |
| 109 | + startNettyImplTermination(future); |
107 | 110 | } |
| 111 | + // wait until connection is closed at least once |
| 112 | + future.get(); |
108 | 113 | } |
109 | 114 |
|
110 | | - protected void makeLocalMetadataStoreKeepReconnectNIO() { |
111 | | - new Thread(() -> { |
112 | | - while (LocalMetadataStoreInReconnectFinishSignal.get()) { |
| 115 | + private void startNIOImplTermination(CompletableFuture<Void> future) { |
| 116 | + connectionTerminationThread = new Thread(() -> { |
| 117 | + while (connectionTerminationThreadKeepRunning.get()) { |
113 | 118 | try { |
114 | 119 | SelectionKey sockKey = WhiteboxImpl.getInternalState(localMetaDataStoreClientCnx, "sockKey"); |
115 | 120 | if (sockKey != null) { |
116 | 121 | sockKey.channel().close(); |
| 122 | + future.complete(null); |
117 | 123 | } |
118 | 124 | // Prevents high cpu usage. |
119 | 125 | Thread.sleep(5); |
120 | 126 | } catch (Exception e) { |
121 | 127 | log.error("Try close the ZK connection of local metadata store failed: {}", e.toString()); |
122 | 128 | } |
123 | 129 | } |
124 | | - }).start(); |
| 130 | + }); |
| 131 | + connectionTerminationThread.start(); |
125 | 132 | } |
126 | 133 |
|
127 | | - protected void makeLocalMetadataStoreKeepReconnectNetty() { |
128 | | - new Thread(() -> { |
129 | | - while (LocalMetadataStoreInReconnectFinishSignal.get()) { |
| 134 | + private void startNettyImplTermination(CompletableFuture<Void> future) { |
| 135 | + connectionTerminationThread = new Thread(() -> { |
| 136 | + while (connectionTerminationThreadKeepRunning.get()) { |
130 | 137 | try { |
131 | 138 | Channel channel = WhiteboxImpl.getInternalState(localMetaDataStoreClientCnx, "channel"); |
132 | 139 | if (channel != null) { |
133 | 140 | channel.close(); |
| 141 | + future.complete(null); |
134 | 142 | } |
135 | 143 | // Prevents high cpu usage. |
136 | 144 | Thread.sleep(5); |
137 | 145 | } catch (Exception e) { |
138 | 146 | log.error("Try close the ZK connection of local metadata store failed: {}", e.toString()); |
139 | 147 | } |
140 | 148 | } |
141 | | - }).start(); |
| 149 | + }); |
| 150 | + connectionTerminationThread.start(); |
142 | 151 | } |
143 | 152 |
|
144 | | - protected void stopLocalMetadataStoreAlwaysReconnect() { |
145 | | - LocalMetadataStoreInReconnectFinishSignal.set(false); |
| 153 | + protected void stopLocalMetadataStoreConnectionTermination() throws InterruptedException { |
| 154 | + connectionTerminationThreadKeepRunning.set(false); |
| 155 | + if (connectionTerminationThread != null) { |
| 156 | + // Wait for the reconnect thread to finish. |
| 157 | + connectionTerminationThread.join(); |
| 158 | + connectionTerminationThread = null; |
| 159 | + } |
146 | 160 | } |
147 | 161 |
|
148 | 162 | protected void createDefaultTenantsAndClustersAndNamespace() throws Exception { |
@@ -205,7 +219,7 @@ protected void cleanup() throws Exception { |
205 | 219 | markCurrentSetupNumberCleaned(); |
206 | 220 | log.info("--- Shutting down ---"); |
207 | 221 |
|
208 | | - stopLocalMetadataStoreAlwaysReconnect(); |
| 222 | + stopLocalMetadataStoreConnectionTermination(); |
209 | 223 |
|
210 | 224 | // Stop brokers. |
211 | 225 | if (client != null) { |
|
0 commit comments