poorbarcode commented on code in PR #22206:
URL: https://github.com/apache/pulsar/pull/22206#discussion_r1608397938


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -607,35 +609,89 @@ protected void 
handlePartitionMetadataRequest(CommandPartitionedTopicMetadata pa
             isTopicOperationAllowed(topicName, TopicOperation.LOOKUP, 
authenticationData, originalAuthData).thenApply(
                     isAuthorized -> {
                 if (isAuthorized) {
-                    
unsafeGetPartitionedTopicMetadataAsync(getBrokerService().pulsar(), topicName)
-                        .handle((metadata, ex) -> {
-                                if (ex == null) {
-                                    int partitions = metadata.partitions;
-                                    
commandSender.sendPartitionMetadataResponse(partitions, requestId);
-                                } else {
-                                    if (ex instanceof PulsarClientException) {
-                                        log.warn("Failed to authorize {} at 
[{}] on topic {} : {}", getRole(),
-                                                remoteAddress, topicName, 
ex.getMessage());
-                                        
commandSender.sendPartitionMetadataResponse(ServerError.AuthorizationError,
-                                                ex.getMessage(), requestId);
+                    // Get if exists, respond not found error if not exists.
+                    
getBrokerService().isAllowAutoTopicCreationAsync(topicName).thenAccept(brokerAllowAutoCreate
 -> {
+                        boolean autoCreateIfNotExist = 
partitionMetadata.isMetadataAutoCreationEnabled();
+                        if (!autoCreateIfNotExist) {
+                            final NamespaceResources namespaceResources = 
getBrokerService().pulsar()
+                                    
.getPulsarResources().getNamespaceResources();
+                            final TopicResources topicResources = 
getBrokerService().pulsar().getPulsarResources()
+                                    .getTopicResources();
+                            namespaceResources.getPartitionedTopicResources()
+                                .getPartitionedTopicMetadataAsync(topicName, 
false)
+                                .thenAccept(metadata -> {
+                                    if (metadata.isPresent()) {
+                                        
commandSender.sendPartitionMetadataResponse(metadata.get().partitions,
+                                                requestId);
+                                        lookupSemaphore.release();
+                                        return;
+                                    }
+                                    if (topicName.isPersistent()) {
+                                        
topicResources.persistentTopicExists(topicName).thenAccept(exists -> {
+                                            if (exists) {
+                                                
commandSender.sendPartitionMetadataResponse(0, requestId);
+                                                lookupSemaphore.release();
+                                                return;
+                                            }
+                                            
writeAndFlush(Commands.newPartitionMetadataResponse(
+                                                    ServerError.TopicNotFound, 
"", requestId));
+                                            lookupSemaphore.release();
+                                            return;
+                                        }).exceptionally(ex -> {
+                                            log.error("{} {} Failed to get 
partition metadata", topicName,
+                                                    ServerCnx.this.toString(), 
ex);
+                                            writeAndFlush(
+                                                    
Commands.newPartitionMetadataResponse(ServerError.MetadataError,
+                                                            "Failed to check 
partition metadata",
+                                                            requestId));
+                                            lookupSemaphore.release();
+                                            return null;
+                                        });
+                                    }

Review Comment:
   Fixed. And added the tests to cover the following scenarios:
   - `non-persistent topic`
   - `semaphore`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to