21
21
import static java .lang .String .format ;
22
22
import static org .apache .pulsar .client .api .PulsarClientException .FailedFeatureCheck .SupportsGetPartitionedMetadataWithoutAutoCreation ;
23
23
import io .netty .buffer .ByteBuf ;
24
+ import io .netty .util .concurrent .DefaultThreadFactory ;
24
25
import java .net .InetSocketAddress ;
25
26
import java .net .URI ;
26
27
import java .util .Optional ;
27
28
import java .util .concurrent .CompletableFuture ;
28
29
import java .util .concurrent .ConcurrentHashMap ;
29
30
import java .util .concurrent .ExecutorService ;
31
+ import java .util .concurrent .Executors ;
30
32
import java .util .concurrent .ScheduledExecutorService ;
31
33
import java .util .concurrent .TimeUnit ;
32
34
import java .util .concurrent .atomic .AtomicLong ;
@@ -55,37 +57,68 @@ public class BinaryProtoLookupService implements LookupService {
55
57
private final PulsarClientImpl client ;
56
58
private final ServiceNameResolver serviceNameResolver ;
57
59
private final boolean useTls ;
58
- private final ExecutorService executor ;
60
+ private final ExecutorService scheduleExecutor ;
59
61
private final String listenerName ;
60
62
private final int maxLookupRedirects ;
63
+ private final ExecutorService lookupPinnedExecutor ;
64
+ private final boolean createdLookupPinnedExecutor ;
61
65
62
66
private final ConcurrentHashMap <TopicName , CompletableFuture <Pair <InetSocketAddress , InetSocketAddress >>>
63
67
lookupInProgress = new ConcurrentHashMap <>();
64
68
65
69
private final ConcurrentHashMap <TopicName , CompletableFuture <PartitionedTopicMetadata >>
66
70
partitionedMetadataInProgress = new ConcurrentHashMap <>();
67
71
72
+ /**
73
+ * @deprecated use {@link
74
+ * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ExecutorService, ExecutorService)} instead.
75
+ */
76
+ @ Deprecated
77
+ public BinaryProtoLookupService (PulsarClientImpl client ,
78
+ String serviceUrl ,
79
+ boolean useTls ,
80
+ ExecutorService scheduleExecutor )
81
+ throws PulsarClientException {
82
+ this (client , serviceUrl , null , useTls , scheduleExecutor );
83
+ }
84
+
85
+ /**
86
+ * @deprecated use {@link
87
+ * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ExecutorService, ExecutorService)} instead.
88
+ */
89
+ @ Deprecated
68
90
public BinaryProtoLookupService (PulsarClientImpl client ,
69
91
String serviceUrl ,
92
+ String listenerName ,
70
93
boolean useTls ,
71
- ExecutorService executor )
94
+ ExecutorService scheduleExecutor )
72
95
throws PulsarClientException {
73
- this (client , serviceUrl , null , useTls , executor );
96
+ this (client , serviceUrl , listenerName , useTls , scheduleExecutor , null );
74
97
}
75
98
76
99
public BinaryProtoLookupService (PulsarClientImpl client ,
77
100
String serviceUrl ,
78
101
String listenerName ,
79
102
boolean useTls ,
80
- ExecutorService executor )
103
+ ExecutorService scheduleExecutor ,
104
+ ExecutorService lookupPinnedExecutor )
81
105
throws PulsarClientException {
82
106
this .client = client ;
83
107
this .useTls = useTls ;
84
- this .executor = executor ;
108
+ this .scheduleExecutor = scheduleExecutor ;
85
109
this .maxLookupRedirects = client .getConfiguration ().getMaxLookupRedirects ();
86
110
this .serviceNameResolver = new PulsarServiceNameResolver ();
87
111
this .listenerName = listenerName ;
88
112
updateServiceUrl (serviceUrl );
113
+
114
+ if (lookupPinnedExecutor == null ) {
115
+ this .createdLookupPinnedExecutor = true ;
116
+ this .lookupPinnedExecutor =
117
+ Executors .newSingleThreadExecutor (new DefaultThreadFactory ("pulsar-client-binary-proto-lookup" ));
118
+ } else {
119
+ this .createdLookupPinnedExecutor = false ;
120
+ this .lookupPinnedExecutor = lookupPinnedExecutor ;
121
+ }
89
122
}
90
123
91
124
@ Override
@@ -153,7 +186,7 @@ private CompletableFuture<Pair<InetSocketAddress, InetSocketAddress>> findBroker
153
186
return addressFuture ;
154
187
}
155
188
156
- client .getCnxPool ().getConnection (socketAddress ).thenAccept (clientCnx -> {
189
+ client .getCnxPool ().getConnection (socketAddress ).thenAcceptAsync (clientCnx -> {
157
190
long requestId = client .newRequestId ();
158
191
ByteBuf request = Commands .newLookup (topicName .toString (), listenerName , authoritative , requestId );
159
192
clientCnx .newLookup (request , requestId ).whenComplete ((r , t ) -> {
@@ -218,7 +251,7 @@ private CompletableFuture<Pair<InetSocketAddress, InetSocketAddress>> findBroker
218
251
}
219
252
client .getCnxPool ().releaseConnection (clientCnx );
220
253
});
221
- }).exceptionally (connectionException -> {
254
+ }, lookupPinnedExecutor ).exceptionally (connectionException -> {
222
255
addressFuture .completeExceptionally (FutureUtil .unwrapCompletionException (connectionException ));
223
256
return null ;
224
257
});
@@ -230,7 +263,7 @@ private CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(
230
263
231
264
CompletableFuture <PartitionedTopicMetadata > partitionFuture = new CompletableFuture <>();
232
265
233
- client .getCnxPool ().getConnection (socketAddress ).thenAccept (clientCnx -> {
266
+ client .getCnxPool ().getConnection (socketAddress ).thenAcceptAsync (clientCnx -> {
234
267
boolean finalAutoCreationEnabled = metadataAutoCreationEnabled ;
235
268
if (!metadataAutoCreationEnabled && !clientCnx .isSupportsGetPartitionedMetadataWithoutAutoCreation ()) {
236
269
if (useFallbackForNonPIP344Brokers ) {
@@ -269,7 +302,7 @@ private CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(
269
302
}
270
303
client .getCnxPool ().releaseConnection (clientCnx );
271
304
});
272
- }).exceptionally (connectionException -> {
305
+ }, lookupPinnedExecutor ).exceptionally (connectionException -> {
273
306
partitionFuture .completeExceptionally (FutureUtil .unwrapCompletionException (connectionException ));
274
307
return null ;
275
308
});
@@ -291,7 +324,7 @@ public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, by
291
324
return schemaFuture ;
292
325
}
293
326
InetSocketAddress socketAddress = serviceNameResolver .resolveHost ();
294
- client .getCnxPool ().getConnection (socketAddress ).thenAccept (clientCnx -> {
327
+ client .getCnxPool ().getConnection (socketAddress ).thenAcceptAsync (clientCnx -> {
295
328
long requestId = client .newRequestId ();
296
329
ByteBuf request = Commands .newGetSchema (requestId , topicName .toString (),
297
330
Optional .ofNullable (BytesSchemaVersion .of (version )));
@@ -305,7 +338,7 @@ public CompletableFuture<Optional<SchemaInfo>> getSchema(TopicName topicName, by
305
338
}
306
339
client .getCnxPool ().releaseConnection (clientCnx );
307
340
});
308
- }).exceptionally (ex -> {
341
+ }, lookupPinnedExecutor ).exceptionally (ex -> {
309
342
schemaFuture .completeExceptionally (FutureUtil .unwrapCompletionException (ex ));
310
343
return null ;
311
344
});
@@ -348,7 +381,7 @@ private void getTopicsUnderNamespace(InetSocketAddress socketAddress,
348
381
Mode mode ,
349
382
String topicsPattern ,
350
383
String topicsHash ) {
351
- client .getCnxPool ().getConnection (socketAddress ).thenAccept (clientCnx -> {
384
+ client .getCnxPool ().getConnection (socketAddress ).thenAcceptAsync (clientCnx -> {
352
385
long requestId = client .newRequestId ();
353
386
ByteBuf request = Commands .newGetTopicsOfNamespaceRequest (
354
387
namespace .toString (), requestId , mode , topicsPattern , topicsHash );
@@ -365,7 +398,7 @@ private void getTopicsUnderNamespace(InetSocketAddress socketAddress,
365
398
}
366
399
client .getCnxPool ().releaseConnection (clientCnx );
367
400
});
368
- }).exceptionally ((e ) -> {
401
+ }, lookupPinnedExecutor ).exceptionally ((e ) -> {
369
402
long nextDelay = Math .min (backoff .next (), remainingTime .get ());
370
403
if (nextDelay <= 0 ) {
371
404
getTopicsResultFuture .completeExceptionally (
@@ -375,7 +408,7 @@ private void getTopicsUnderNamespace(InetSocketAddress socketAddress,
375
408
return null ;
376
409
}
377
410
378
- ((ScheduledExecutorService ) executor ).schedule (() -> {
411
+ ((ScheduledExecutorService ) scheduleExecutor ).schedule (() -> {
379
412
log .warn ("[namespace: {}] Could not get connection while getTopicsUnderNamespace -- Will try again in"
380
413
+ " {} ms" , namespace , nextDelay );
381
414
remainingTime .addAndGet (-nextDelay );
@@ -389,7 +422,9 @@ private void getTopicsUnderNamespace(InetSocketAddress socketAddress,
389
422
390
423
@ Override
391
424
public void close () throws Exception {
392
- // no-op
425
+ if (createdLookupPinnedExecutor && lookupPinnedExecutor != null && !lookupPinnedExecutor .isShutdown ()) {
426
+ lookupPinnedExecutor .shutdown ();
427
+ }
393
428
}
394
429
395
430
public static class LookupDataResult {
0 commit comments