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 ;
@@ -225,6 +229,60 @@ public void testAutoCreatingMetadataWhenCallingOldAPI(TopicDomain topicDomain) t
225229 }
226230 }
227231
232+ @ Test (dataProvider = "topicDomains" , priority = Integer .MAX_VALUE )
233+ public void testCompatibilityForNewClientAndOldBroker (TopicDomain topicDomain ) throws Exception {
234+ modifyTopicAutoCreation (true , TopicType .PARTITIONED , 3 );
235+ // Initialize connections.
236+ String pulsarUrl = pulsar1 .getBrokerServiceUrl ();
237+ PulsarClientImpl [] clients = getClientsToTest (false );
238+ for (PulsarClientImpl client : clients ) {
239+ client .getLookup (pulsarUrl ).getBroker (TopicName .get (DEFAULT_NS + "/tp1" ));
240+ }
241+ // Inject a not support flag into the connections initialized.
242+ Field field = ClientCnx .class .getDeclaredField ("supportsGetPartitionedMetadataWithoutAutoCreation" );
243+ field .setAccessible (true );
244+ for (PulsarClientImpl client : clients ) {
245+ ConnectionPool pool = client .getCnxPool ();
246+ for (CompletableFuture <ClientCnx > connectionFuture : pool .getConnections ()) {
247+ ClientCnx clientCnx = connectionFuture .join ();
248+ clientCnx .isSupportsGetPartitionedMetadataWithoutAutoCreation ();
249+ field .set (clientCnx , false );
250+ }
251+ }
252+
253+ // Verify: the method "getPartitionsForTopic(topic, false, true)" will fallback to
254+ // "getPartitionsForTopic(topic)" behavior.
255+ int lookupPermitsBefore = getLookupRequestPermits ();
256+ for (PulsarClientImpl client : clients ) {
257+ // Verify: the behavior of topic creation.
258+ final String tp = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
259+ client .getPartitionedTopicMetadata (tp , false , true ).join ();
260+ Optional <PartitionedTopicMetadata > metadata1 = pulsar1 .getPulsarResources ().getNamespaceResources ()
261+ .getPartitionedTopicResources ()
262+ .getPartitionedTopicMetadataAsync (TopicName .get (tp ), true ).join ();
263+ assertTrue (metadata1 .isPresent ());
264+ assertEquals (metadata1 .get ().partitions , 3 );
265+
266+ // Verify: lookup semaphore has been releases.
267+ Awaitility .await ().untilAsserted (() -> {
268+ assertEquals (getLookupRequestPermits (), lookupPermitsBefore );
269+ });
270+
271+ // Cleanup.
272+ admin1 .topics ().deletePartitionedTopic (tp , false );
273+ }
274+
275+ // reset clients.
276+ for (PulsarClientImpl client : clients ) {
277+ ConnectionPool pool = client .getCnxPool ();
278+ for (CompletableFuture <ClientCnx > connectionFuture : pool .getConnections ()) {
279+ ClientCnx clientCnx = connectionFuture .join ();
280+ clientCnx .isSupportsGetPartitionedMetadataWithoutAutoCreation ();
281+ field .set (clientCnx , true );
282+ }
283+ }
284+ }
285+
228286 @ DataProvider (name = "autoCreationParamsAll" )
229287 public Object [][] autoCreationParamsAll (){
230288 return new Object [][]{
@@ -265,7 +323,7 @@ public void testGetMetadataIfNonPartitionedTopicExists(boolean configAllowAutoTo
265323 for (PulsarClientImpl client : clientArray ) {
266324 // Verify: the result of get partitioned topic metadata.
267325 PartitionedTopicMetadata response =
268- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled ).join ();
326+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false ).join ();
269327 assertEquals (response .partitions , 0 );
270328 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
271329 assertFalse (partitionedTopics .contains (topicNameStr ));
@@ -298,7 +356,7 @@ public void testGetMetadataIfPartitionedTopicExists(boolean configAllowAutoTopic
298356 for (PulsarClientImpl client : clientArray ) {
299357 // Verify: the result of get partitioned topic metadata.
300358 PartitionedTopicMetadata response =
301- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled ).join ();
359+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false ).join ();
302360 assertEquals (response .partitions , 3 );
303361 verifyNonPartitionedTopicNeverCreated (topicNameStr );
304362
@@ -332,7 +390,7 @@ public void testAutoCreatePartitionedTopic(boolean isUsingHttpLookup, TopicDomai
332390 // Case-1: normal topic.
333391 final String topicNameStr = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
334392 // Verify: the result of get partitioned topic metadata.
335- PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true ).join ();
393+ PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true , false ).join ();
336394 assertEquals (response .partitions , 3 );
337395 // Verify: the behavior of topic creation.
338396 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
@@ -347,7 +405,7 @@ public void testAutoCreatePartitionedTopic(boolean isUsingHttpLookup, TopicDomai
347405 topicDomain .value () + "://" + DEFAULT_NS + "/tp" ) + "-partition-1" ;
348406 // Verify: the result of get partitioned topic metadata.
349407 PartitionedTopicMetadata response2 =
350- client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true ).join ();
408+ client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true , false ).join ();
351409 assertEquals (response2 .partitions , 0 );
352410 // Verify: the behavior of topic creation.
353411 List <String > partitionedTopics2 =
@@ -380,7 +438,7 @@ public void testAutoCreateNonPartitionedTopic(boolean isUsingHttpLookup, TopicDo
380438 // Case 1: normal topic.
381439 final String topicNameStr = BrokerTestUtil .newUniqueName (topicDomain .value () + "://" + DEFAULT_NS + "/tp" );
382440 // Verify: the result of get partitioned topic metadata.
383- PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true ).join ();
441+ PartitionedTopicMetadata response = client .getPartitionedTopicMetadata (topicNameStr , true , false ).join ();
384442 assertEquals (response .partitions , 0 );
385443 // Verify: the behavior of topic creation.
386444 List <String > partitionedTopics = admin1 .topics ().getPartitionedTopicList ("public/default" );
@@ -392,7 +450,7 @@ public void testAutoCreateNonPartitionedTopic(boolean isUsingHttpLookup, TopicDo
392450 topicDomain .value () + "://" + DEFAULT_NS + "/tp" ) + "-partition-1" ;
393451 // Verify: the result of get partitioned topic metadata.
394452 PartitionedTopicMetadata response2 =
395- client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true ).join ();
453+ client .getPartitionedTopicMetadata (topicNameStrWithSuffix , true , false ).join ();
396454 assertEquals (response2 .partitions , 0 );
397455 // Verify: the behavior of topic creation.
398456 List <String > partitionedTopics2 =
@@ -443,7 +501,7 @@ public void testGetMetadataIfNotAllowedCreate(boolean configAllowAutoTopicCreati
443501 final TopicName topicName = TopicName .get (topicNameStr );
444502 // Verify: the result of get partitioned topic metadata.
445503 try {
446- client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled )
504+ client .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false )
447505 .join ();
448506 fail ("Expect a not found exception" );
449507 } catch (Exception e ) {
@@ -496,7 +554,7 @@ public void testGetMetadataIfNotAllowedCreateOfNonPersistentTopic(boolean config
496554 // Verify: the result of get partitioned topic metadata.
497555 try {
498556 PartitionedTopicMetadata topicMetadata = client
499- .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled )
557+ .getPartitionedTopicMetadata (topicNameStr , paramMetadataAutoCreationEnabled , false )
500558 .join ();
501559 log .info ("Get topic metadata: {}" , topicMetadata .partitions );
502560 fail ("Expected a not found ex" );
0 commit comments