You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/11/25 06:31:56 UTC

[GitHub] [pulsar-client-cpp] erobot opened a new pull request, #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

erobot opened a new pull request, #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125

   <!--
   ### Contribution Checklist
     
     - PR title format should be *[type][component] summary*. For details, see *[Guideline - Pulsar PR Naming Convention](https://docs.google.com/document/d/1d8Pw6ZbWk-_pCKdOmdvx9rnhPiyuxwq60_TrD68d7BA/edit#heading=h.trs9rsex3xom)*. 
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   -->
   
   ### Motivation
   
   <!-- Explain here the context, and why you're making that change. What is the problem you're trying to solve. -->
   
   PartitionedProducerImpl do not close sub-producers properly when any sub-producer creation fails. Continuing to retry creating producer will eventually reach the maximum producer limit. It seems a regression caused by #54. 
   
   When sub-producer creation fails, state_ is set to Failed. PartitionedProducerImpl::closeAsync only do cleanup when state_==Ready and sub-producers do not close when state_==Failed.
   
   https://github.com/apache/pulsar-client-cpp/blob/f0268ecd29a6d0030b7d07379ec609884b4c14ff/lib/PartitionedProducerImpl.cc#L273-L276
   
   ### Modifications
   
   <!-- Describe the modifications you've done. -->
   
   Close sub-producers when state != Closed.
   
   ### Verifying this change
   
   test code:
   
       Client client("pulsar://localhost:6650");
       Producer producer;
       while (true) {
           Result result = client.createProducer("persistent://public/default/test", producer);
           if (result != ResultOk) {
               cerr << "Error creating producer: " << result << endl;
               std::this_thread::sleep_for(std::chrono::seconds(1));
               continue;
           }
           break;
       }
   
   test cmd:
   
       pulsar-admin namespaces set-max-producers -p 10 public/default
       pulsar-admin topics create-partition-topic -n 2 public/default/test
       pulsar-perf produce -r 1 -n 10 public/default/test-partition-0
   
   test procedure:
   1. set namespace max producer limit to 10
   2. create partitoned topic public/default/test with 2 partitions
   3. use pulsar-perf to create 10 prodcuers on test-partition-0
   4. run test code to create producer for public/default/test, will fail because test-partition-0 already have 10 producer
   5. 
       * without fix, test-partition-1 will have 10 producers because of sub-producer not close
       * with fix, test-partition-1 will have not more than 1 producer
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc-required` 
   (Your PR needs to update docs and you will update later)
   
   - [x] `doc-not-needed` 
   bug-fix only
   
   - [ ] `doc` 
   (Your PR contains doc changes)
   
   - [ ] `doc-complete`
   (Docs have been already added)
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] erobot commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
erobot commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1327272098

   ok, I will fix the code style and add a unit test latter.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] erobot commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
erobot commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033146486


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   I'll change to your suggested codes as you’re perfered.
   
   Some thoughts:
   * c++ client state is more than java client and it's more complicated to maintain.
   * Closing and Closed seems not much different in c++ client.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] RobertIndie commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
RobertIndie commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033067388


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,10 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    State previous_state = state_.exchange(Closed);
+    if (previous_state == Closed) {
+        return closeCallback(ResultAlreadyClosed);

Review Comment:
   We should exchange it with the `Closing` state. And if the producer is already in `Closing`, this code still be passed, which results in wrong behavior.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033140720


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   Maybe the following implementation could be better?
   
   ```c++
       if (state_ == Closed || state_.exchange(Closing) == Closing) {
           closeCallback(ResultAlreadyClosed);
           return;
       }
   ```
   
   (Though there is a rare case that race condition might happen for concurrent `closeAsync`)



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033138406


##########
tests/ProducerTest.cc:
##########
@@ -373,4 +374,62 @@ TEST_P(ProducerTest, testFlushNoBatch) {
     client.close();
 }
 
+TEST_P(ProducerTest, testCloseSubProducerWhenFail) {
+    Client client(serviceUrl);
+
+    std::string ns = "test-close-sub-producer-when-fail";
+    std::string localName = std::string("testCloseSubProducerWhenFail") + std::to_string(time(nullptr));
+    std::string topicName = "persistent://public/" + ns + '/' + localName;
+    const int maxProducersPerTopic = 10;
+    const int partitionNum = 5;
+
+    // call admin api to create namespace with max prodcuer limit
+    std::string url = adminUrl + "admin/v2/namespaces/public/" + ns;
+    int res =
+        makePutRequest(url, "{\"max_producers_per_topic\": " + std::to_string(maxProducersPerTopic) + "}");
+    ASSERT_TRUE(res == 204 || res == 409) << "res:" << res;
+
+    // call admin api to create partitioned topic
+    res = makePutRequest(adminUrl + "admin/v2/persistent/public/" + ns + "/" + localName + "/partitions",
+                         std::to_string(partitionNum));
+    ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
+
+    ProducerConfiguration producerConfiguration;
+    producerConfiguration.setBatchingEnabled(false);
+
+    // create producers for partition-0 up to max producer limit
+    std::vector<Producer> producers;
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        Producer producer;
+        ASSERT_EQ(ResultOk,
+                  client.createProducer(topicName + "-partition-0", producerConfiguration, producer));
+        producers.push_back(producer);
+    }
+
+    // create partitoined producer, should fail because partition-0 already reach max producer limit
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        auto partitionedProducer = std::make_shared<PartitionedProducerImpl>(
+            PulsarFriend::getClientImplPtr(client), TopicName::get(topicName), partitionNum,
+            producerConfiguration);
+        partitionedProducer->start();
+        Result result;
+        ProducerImplBaseWeakPtr ptr;
+        ASSERT_TRUE(
+            partitionedProducer->getProducerCreatedFuture().get(result, ptr, std::chrono::seconds(1)));
+        ASSERT_EQ(result, ResultProducerBusy);  // reach max producer limit

Review Comment:
   ```suggestion
           Producer producer;
           ASSERT_EQ(ResultProducerBusy, client.createProducer(topicName, producer));
   ```
   
   You'd better not use the internal class directly. After that, `#include "PulsarFriend.h"` can be removed as well.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] RobertIndie merged pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
RobertIndie merged PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033137455


##########
tests/ProducerTest.cc:
##########
@@ -373,4 +374,62 @@ TEST_P(ProducerTest, testFlushNoBatch) {
     client.close();
 }
 
+TEST_P(ProducerTest, testCloseSubProducerWhenFail) {
+    Client client(serviceUrl);
+
+    std::string ns = "test-close-sub-producer-when-fail";
+    std::string localName = std::string("testCloseSubProducerWhenFail") + std::to_string(time(nullptr));
+    std::string topicName = "persistent://public/" + ns + '/' + localName;
+    const int maxProducersPerTopic = 10;
+    const int partitionNum = 5;
+
+    // call admin api to create namespace with max prodcuer limit
+    std::string url = adminUrl + "admin/v2/namespaces/public/" + ns;
+    int res =
+        makePutRequest(url, "{\"max_producers_per_topic\": " + std::to_string(maxProducersPerTopic) + "}");
+    ASSERT_TRUE(res == 204 || res == 409) << "res:" << res;
+
+    // call admin api to create partitioned topic
+    res = makePutRequest(adminUrl + "admin/v2/persistent/public/" + ns + "/" + localName + "/partitions",
+                         std::to_string(partitionNum));
+    ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
+
+    ProducerConfiguration producerConfiguration;
+    producerConfiguration.setBatchingEnabled(false);
+
+    // create producers for partition-0 up to max producer limit
+    std::vector<Producer> producers;
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        Producer producer;
+        ASSERT_EQ(ResultOk,
+                  client.createProducer(topicName + "-partition-0", producerConfiguration, producer));
+        producers.push_back(producer);
+    }
+
+    // create partitoined producer, should fail because partition-0 already reach max producer limit

Review Comment:
   ```suggestion
       // create partitioned producer, should fail because partition-0 already reach max producer limit
   ```



##########
tests/ProducerTest.cc:
##########
@@ -373,4 +374,62 @@ TEST_P(ProducerTest, testFlushNoBatch) {
     client.close();
 }
 
+TEST_P(ProducerTest, testCloseSubProducerWhenFail) {
+    Client client(serviceUrl);
+
+    std::string ns = "test-close-sub-producer-when-fail";
+    std::string localName = std::string("testCloseSubProducerWhenFail") + std::to_string(time(nullptr));
+    std::string topicName = "persistent://public/" + ns + '/' + localName;
+    const int maxProducersPerTopic = 10;
+    const int partitionNum = 5;
+
+    // call admin api to create namespace with max prodcuer limit
+    std::string url = adminUrl + "admin/v2/namespaces/public/" + ns;
+    int res =
+        makePutRequest(url, "{\"max_producers_per_topic\": " + std::to_string(maxProducersPerTopic) + "}");
+    ASSERT_TRUE(res == 204 || res == 409) << "res:" << res;
+
+    // call admin api to create partitioned topic
+    res = makePutRequest(adminUrl + "admin/v2/persistent/public/" + ns + "/" + localName + "/partitions",
+                         std::to_string(partitionNum));
+    ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
+
+    ProducerConfiguration producerConfiguration;
+    producerConfiguration.setBatchingEnabled(false);
+
+    // create producers for partition-0 up to max producer limit
+    std::vector<Producer> producers;
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        Producer producer;
+        ASSERT_EQ(ResultOk,
+                  client.createProducer(topicName + "-partition-0", producerConfiguration, producer));
+        producers.push_back(producer);
+    }
+
+    // create partitoined producer, should fail because partition-0 already reach max producer limit
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        auto partitionedProducer = std::make_shared<PartitionedProducerImpl>(
+            PulsarFriend::getClientImplPtr(client), TopicName::get(topicName), partitionNum,
+            producerConfiguration);
+        partitionedProducer->start();
+        Result result;
+        ProducerImplBaseWeakPtr ptr;
+        ASSERT_TRUE(
+            partitionedProducer->getProducerCreatedFuture().get(result, ptr, std::chrono::seconds(1)));
+        ASSERT_EQ(result, ResultProducerBusy);  // reach max producer limit
+    }
+
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+
+    // create producer for partition-1, should succ

Review Comment:
   ```suggestion
       // create producer for partition-1, should succeed
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033143897


##########
tests/ProducerTest.cc:
##########
@@ -373,4 +374,62 @@ TEST_P(ProducerTest, testFlushNoBatch) {
     client.close();
 }
 
+TEST_P(ProducerTest, testCloseSubProducerWhenFail) {

Review Comment:
   ```suggestion
   TEST(ProducerTest, testCloseSubProducerWhenFail) {
   ```
   
   No need to use `TEST_P` here.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033140720


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   Maybe the following implementation could be better?
   
   ```c++
       if (state_ == Closed || state_.exchange(Closing) == Closing) {
           closeCallback(ResultAlreadyClosed);
           return;
       }
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] erobot commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
erobot commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033213097


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   Thanks for your explanation, I found read wrong code. I'm understand the problem better now.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] shibd commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
shibd commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1327168870

   @erobot Hi, Can you add unit test based on the steps`verifying this change` chapter?
   
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1328561682

   I got it. It's caused when some sub-producers can be created successfully while other could fail.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] erobot commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
erobot commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1328528687

   @BewareMyPower add a test to reproduce the problem


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1032261575


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,10 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    State previous_state = state_.exchange(Closed);
+    if (previous_state == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   In addition, we should follow the behavior of the Java client to set the state to `Closing`. Only after all sub-producers are closed should we set the state to `Closed`.
   
   ```java
           if (getState() == State.Closing || getState() == State.Closed) {
               return CompletableFuture.completedFuture(null);
           }
           setState(State.Closing); // here
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] erobot commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
erobot commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033143065


##########
tests/ProducerTest.cc:
##########
@@ -373,4 +374,62 @@ TEST_P(ProducerTest, testFlushNoBatch) {
     client.close();
 }
 
+TEST_P(ProducerTest, testCloseSubProducerWhenFail) {
+    Client client(serviceUrl);
+
+    std::string ns = "test-close-sub-producer-when-fail";
+    std::string localName = std::string("testCloseSubProducerWhenFail") + std::to_string(time(nullptr));
+    std::string topicName = "persistent://public/" + ns + '/' + localName;
+    const int maxProducersPerTopic = 10;
+    const int partitionNum = 5;
+
+    // call admin api to create namespace with max prodcuer limit
+    std::string url = adminUrl + "admin/v2/namespaces/public/" + ns;
+    int res =
+        makePutRequest(url, "{\"max_producers_per_topic\": " + std::to_string(maxProducersPerTopic) + "}");
+    ASSERT_TRUE(res == 204 || res == 409) << "res:" << res;
+
+    // call admin api to create partitioned topic
+    res = makePutRequest(adminUrl + "admin/v2/persistent/public/" + ns + "/" + localName + "/partitions",
+                         std::to_string(partitionNum));
+    ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
+
+    ProducerConfiguration producerConfiguration;
+    producerConfiguration.setBatchingEnabled(false);
+
+    // create producers for partition-0 up to max producer limit
+    std::vector<Producer> producers;
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        Producer producer;
+        ASSERT_EQ(ResultOk,
+                  client.createProducer(topicName + "-partition-0", producerConfiguration, producer));
+        producers.push_back(producer);
+    }
+
+    // create partitoined producer, should fail because partition-0 already reach max producer limit
+    for (int i = 0; i < maxProducersPerTopic; ++i) {
+        auto partitionedProducer = std::make_shared<PartitionedProducerImpl>(
+            PulsarFriend::getClientImplPtr(client), TopicName::get(topicName), partitionNum,
+            producerConfiguration);
+        partitionedProducer->start();
+        Result result;
+        ProducerImplBaseWeakPtr ptr;
+        ASSERT_TRUE(
+            partitionedProducer->getProducerCreatedFuture().get(result, ptr, std::chrono::seconds(1)));
+        ASSERT_EQ(result, ResultProducerBusy);  // reach max producer limit

Review Comment:
   You’re right, I'll fix it. At first I wanted to test the internal prodcuers state, but it's not needed now.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033137330


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   You should set the state to `Closing`, see comment [here](https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033067388)



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1032189493


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,10 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    State previous_state = state_.exchange(Closed);
+    if (previous_state == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   ```suggestion
       State previousState = state_.exchange(Closed);
       if (previousState == Closed) {
           return closeCallback(ResultAlreadyClosed);
       }
   ```
   
   Use camel case instead of snake case to make code style consistent. Or just simplify it to `if (state_.exchange(Closed) == Closed)`.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1327285533

   > without fix, test-partition-1 will have 10 producers because of sub-producer not close
   > with fix, test-partition-1 will have not more than 1 producer
   
   The difference is not clear from the output.
   
   [with-fix.log](https://github.com/apache/pulsar-client-cpp/files/10091081/1.log)
   
   [without-fix.log](https://github.com/apache/pulsar-client-cpp/files/10091083/2.log)
   
   Maybe a unit test would be better to tell the difference.
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#issuecomment-1328486238

   Just digged deeper into this issue. I think there is no problem with the current master.
   
   If one internal producer failed to create, the partitioned producer would not be added into the `ClientImpl` or `ClientConnection`, see https://github.com/apache/pulsar-client-cpp/blob/96d870527d2a078645c4866e9d75b3f3b97193ba/lib/ClientImpl.cc#L202-204
   
   Then the `PartitionedProducerImpl` will be destroyed and `shutdown()` will be called for the cleanup work (cancel the timer) in the destructor. Regarding the `ProducerImpl` objects, since they also failed to create, there is no need to call `closeAsync` on them because **closing a failed producer is redundant.**


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #125: [fix] Fix PartitionedProducerImpl::closeAsync to close sub-producers properly

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on code in PR #125:
URL: https://github.com/apache/pulsar-client-cpp/pull/125#discussion_r1033159823


##########
lib/PartitionedProducerImpl.cc:
##########
@@ -266,13 +266,9 @@ void PartitionedProducerImpl::closeAsync(CloseCallback originalCallback) {
             originalCallback(result);
         }
     };
-    if (state_ == Closed) {
-        closeCallback(ResultAlreadyClosed);
-        return;
-    }
-    State expectedState = Ready;
-    if (!state_.compare_exchange_strong(expectedState, Closing)) {
-        return;
+
+    if (state_.exchange(Closed) == Closed) {
+        return closeCallback(ResultAlreadyClosed);
     }

Review Comment:
   > c++ client state is more than java client and it's more complicated to maintain.
   
   No, the fact is opposite. There are 5 states in C++ client:
   
   ```c++
       enum State
       {
           Pending,
           Ready,
           Closing,
           Closed,
           Failed
   ```
   
   while Java client has 9 states (see `HandlerState.java`):
   
   ```java
       enum State {
           Uninitialized, // Not initialized
           Connecting, // Client connecting to broker
           Ready, // Handler is being used
           Closing, // Close cmd has been sent to broker
           Closed, // Broker acked the close
           Terminated, // Topic associated with this handler
                       // has been terminated
           Failed, // Handler is failed
           RegisteringSchema, // Handler is registering schema
           ProducerFenced, // The producer has been fenced by the broker
       }
   ```
   
   The `Pending` state in C++ client is the `Uninitialized` state in Java client.
   
   > Closing and Closed seems not much different in c++ client.
   
   See the comments in the Java client. `Closing` means the `CloseProducer` command has been sent to the broker and the response is not received yet. In `Closing` state, we should not send any `CloseProducer` command again. It could also be helpful if `producer.close()` stuck. We can debug the application and see the state.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org