2323import static org .testng .Assert .assertTrue ;
2424import static org .testng .Assert .fail ;
2525import com .google .common .collect .Sets ;
26+ import java .lang .reflect .Field ;
2627import java .net .URL ;
2728import java .util .Collections ;
2829import java .util .List ;
2930import java .util .Optional ;
31+ import java .util .concurrent .CompletableFuture ;
3032import lombok .extern .slf4j .Slf4j ;
3133import org .apache .pulsar .broker .BrokerTestUtil ;
3234import org .apache .pulsar .broker .PulsarService ;
3335import org .apache .pulsar .broker .ServiceConfiguration ;
3436import org .apache .pulsar .client .admin .PulsarAdmin ;
3537import org .apache .pulsar .client .api .PulsarClient ;
3638import org .apache .pulsar .client .api .PulsarClientException ;
39+ import org .apache .pulsar .client .impl .ClientCnx ;
40+ import org .apache .pulsar .client .impl .ConnectionPool ;
3741import org .apache .pulsar .client .impl .PulsarClientImpl ;
3842import org .apache .pulsar .common .naming .NamespaceName ;
3943import org .apache .pulsar .common .naming .TopicDomain ;
@@ -226,6 +230,60 @@ public void testAutoCreatingMetadataWhenCallingOldAPI(TopicDomain topicDomain) t
226230 }
227231 }
228232
233+ @ Test (dataProvider = "topicDomains" , priority = Integer .MAX_VALUE )
234+ public void testCompatibilityForNewClientAndOldBroker (TopicDomain topicDomain ) throws Exception {
235+ modifyTopicAutoCreation (true , TopicType .PARTITIONED , 3 );
236+ // Initialize connections.
237+ String pulsarUrl = pulsar1 .getBrokerServiceUrl ();
238+ PulsarClientImpl [] clients = getClientsToTest (false );
239+ for (PulsarClientImpl client : clients ) {
240+ client .getLookup (pulsarUrl ).getBroker (TopicName .get (DEFAULT_NS + "/tp1" ));
241+ }
242+ // Inject a not support flag into the connections initialized.
243+ Field field = ClientCnx .class .getDeclaredField ("supportsGetPartitionedMetadataWithoutAutoCreation" );
244+ field .setAccessible (true );
245+ for (PulsarClientImpl client : clients ) {
246+ ConnectionPool pool = client .getCnxPool ();
247+ for (CompletableFuture <ClientCnx > connectionFuture : pool .getConnections ()) {
248+ ClientCnx clientCnx = connectionFuture .join ();
249+ clientCnx .isSupportsGetPartitionedMetadataWithoutAutoCreation ();
250+ field .set (clientCnx , false );
251+ }
252+ }
253+
254+ // Verify: the method "getPartitionsForTopic(topic, false, true)" will fallback to
255+ // "getPartitionsForTopic(topic)" behavior.
256+ int lookupPermitsBefore = getLookupRequestPermits ();
257+ for (PulsarClientImpl client : clients ) {
258+ // Verify: the behavior of topic creation.
259+ final String tp = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
260+ client .getPartitionedTopicMetadata (tp , false , true ).join ();
261+ Optional <PartitionedTopicMetadata > metadata1 = pulsar1 .getPulsarResources ().getNamespaceResources ()
262+ .getPartitionedTopicResources ()
263+ .getPartitionedTopicMetadataAsync (TopicName .get (tp ), true ).join ();
264+ assertTrue (metadata1 .isPresent ());
265+ assertEquals (metadata1 .get ().partitions , 3 );
266+
267+ // Verify: lookup semaphore has been releases.
268+ Awaitility .await ().untilAsserted (() -> {
269+ assertEquals (getLookupRequestPermits (), lookupPermitsBefore );
270+ });
271+
272+ // Cleanup.
273+ admin1 .topics ().deletePartitionedTopic (tp , false );
274+ }
275+
276+ // reset clients.
277+ for (PulsarClientImpl client : clients ) {
278+ ConnectionPool pool = client .getCnxPool ();
279+ for (CompletableFuture <ClientCnx > connectionFuture : pool .getConnections ()) {
280+ ClientCnx clientCnx = connectionFuture .join ();
281+ clientCnx .isSupportsGetPartitionedMetadataWithoutAutoCreation ();
282+ field .set (clientCnx , true );
283+ }
284+ }
285+ }
286+
229287 @ DataProvider (name = "autoCreationParamsAll" )
230288 public Object [][] autoCreationParamsAll (){
231289 return new Object [][]{
@@ -266,7 +324,7 @@ public void testGetMetadataIfNonPartitionedTopicExists(boolean configAllowAutoTo
266324 for (PulsarClientImpl client : clientArray ) {
267325 // Verify: the result of get partitioned topic metadata.
268326 PartitionedTopicMetadata response =
269- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled ).join ();
327+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false ).join ();
270328 assertEquals (response .partitions , 0 );
271329 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
272330 assertFalse (partitionedTopics .contains (topicNameStr ));
@@ -299,7 +357,7 @@ public void testGetMetadataIfPartitionedTopicExists(boolean configAllowAutoTopic
299357 for (PulsarClientImpl client : clientArray ) {
300358 // Verify: the result of get partitioned topic metadata.
301359 PartitionedTopicMetadata response =
302- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled ).join ();
360+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false ).join ();
303361 assertEquals (response .partitions , 3 );
304362 verifyNonPartitionedTopicNeverCreated (topicNameStr );
305363
@@ -333,7 +391,7 @@ public void testAutoCreatePartitionedTopic(boolean isUsingHttpLookup, TopicDomai
333391 // Case-1: normal topic.
334392 final String topicNameStr = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
335393 // Verify: the result of get partitioned topic metadata.
336- PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true ).join ();
394+ PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true , false ).join ();
337395 assertEquals (response .partitions , 3 );
338396 // Verify: the behavior of topic creation.
339397 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
@@ -348,7 +406,7 @@ public void testAutoCreatePartitionedTopic(boolean isUsingHttpLookup, TopicDomai
348406 topicDomain .value () + "://" + DEFAULT_NS + "/tp" ) + "-partition-1" ;
349407 // Verify: the result of get partitioned topic metadata.
350408 PartitionedTopicMetadata response2 =
351- client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true ).join ();
409+ client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true , false ).join ();
352410 assertEquals (response2 .partitions , 0 );
353411 // Verify: the behavior of topic creation.
354412 List <String > partitionedTopics2 =
@@ -381,7 +439,7 @@ public void testAutoCreateNonPartitionedTopic(boolean isUsingHttpLookup, TopicDo
381439 // Case 1: normal topic.
382440 final String topicNameStr = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
383441 // Verify: the result of get partitioned topic metadata.
384- PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true ).join ();
442+ PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true , false ).join ();
385443 assertEquals (response .partitions , 0 );
386444 // Verify: the behavior of topic creation.
387445 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
@@ -393,7 +451,7 @@ public void testAutoCreateNonPartitionedTopic(boolean isUsingHttpLookup, TopicDo
393451 topicDomain .value () + "://" + DEFAULT_NS + "/tp" ) + "-partition-1" ;
394452 // Verify: the result of get partitioned topic metadata.
395453 PartitionedTopicMetadata response2 =
396- client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true ).join ();
454+ client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true , false ).join ();
397455 assertEquals (response2 .partitions , 0 );
398456 // Verify: the behavior of topic creation.
399457 List <String > partitionedTopics2 =
@@ -444,7 +502,7 @@ public void testGetMetadataIfNotAllowedCreate(boolean configAllowAutoTopicCreati
444502 final TopicName topicName = TopicName .get (topicNameStr );
445503 // Verify: the result of get partitioned topic metadata.
446504 try {
447- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled )
505+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false )
448506 .join ();
449507 fail ("Expect a not found exception" );
450508 } catch (Exception e ) {
@@ -497,7 +555,7 @@ public void testGetMetadataIfNotAllowedCreateOfNonPersistentTopic(boolean config
497555 // Verify: the result of get partitioned topic metadata.
498556 try {
499557 PartitionedTopicMetadata topicMetadata = client
500- .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled )
558+ .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false )
501559 .join ();
502560 log .info ("Get topic metadata: {}" , topicMetadata .partitions );
503561 fail ("Expected a not found ex" );
0 commit comments