You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by GitBox <gi...@apache.org> on 2022/11/15 03:36:54 UTC

[GitHub] [incubator-pegasus] acelyc111 opened a new pull request, #1241: refactor: use command_deregister to auto deregister from command_manager

acelyc111 opened a new pull request, #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241

   https://github.com/apache/incubator-pegasus/issues/1234
   
   including changes:
   1. change `dsn_handle_t` which is used for files descriptor to `linux_fd_t` which wrap an `int`
   2. remove useless `dsn_handle_t` type member from `class aio_context`, `struct callback_para`
   3. change `file_object` from `void *` to `disk_file *`


-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
empiredan commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1022606601


##########
src/utils/command_manager.h:
##########
@@ -64,20 +66,30 @@ class command_manager : public ::dsn::utils::singleton<command_manager>
         command_handler handler;
     };
 
+    void deregister_command(uintptr_t handle);
+
     typedef ref_ptr<command_instance> command_instance_ptr;
     utils::rw_lock_nr _lock;
     std::map<std::string, command_instance_ptr> _handlers;
 };
 
-} // namespace dsn
+class command_deregister
+{
+public:
+    command_deregister(uintptr_t id) : cmd_id_(id) {}
+    ~command_deregister()
+    {
+        if (cmd_id_ != 0) {
+            dsn::command_manager::instance().deregister_command(cmd_id_);
+            cmd_id_ = 0;
+        }
+    }
 
-#define UNREGISTER_VALID_HANDLER(ptr)                                                              \
-    do {                                                                                           \
-        if (ptr != nullptr) {                                                                      \
-            dsn::command_manager::instance().deregister_command(ptr);                              \
-            ptr = nullptr;                                                                         \
-        }                                                                                          \
-    } while (0)
+private:
+    uintptr_t cmd_id_ = 0;

Review Comment:
   Can `cmd_id_` be type of `command_instance *`, or just `std::shared_ptr<command_instance>` ?



##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>((uintptr_t)c);
 }
 
-void command_manager::deregister_command(dsn_handle_t handle)
+void command_manager::deregister_command(uintptr_t handle)

Review Comment:
   Should `c` be released by `delete c;` at last ?



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
empiredan commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1022703672


##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>(static_cast<uintptr_t>(c));

Review Comment:
   ```suggestion
       return dsn::make_unique<command_deregister>(reinterpret_cast<uintptr_t>(c));
   ```



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] acelyc111 merged pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
acelyc111 merged PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241


-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
empiredan commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1022568316


##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>((uintptr_t)c);

Review Comment:
   ```suggestion
       return dsn::make_unique<command_deregister>(static_cast<uintptr_t>(c));
   ```



##########
src/utils/command_manager.h:
##########
@@ -64,20 +66,30 @@ class command_manager : public ::dsn::utils::singleton<command_manager>
         command_handler handler;
     };
 
+    void deregister_command(uintptr_t handle);
+
     typedef ref_ptr<command_instance> command_instance_ptr;
     utils::rw_lock_nr _lock;
     std::map<std::string, command_instance_ptr> _handlers;
 };
 
-} // namespace dsn
+class command_deregister
+{
+public:
+    command_deregister(uintptr_t id) : cmd_id_(id) {}
+    ~command_deregister()
+    {
+        if (cmd_id_ != 0) {
+            dsn::command_manager::instance().deregister_command(cmd_id_);
+            cmd_id_ = 0;
+        }
+    }
 
-#define UNREGISTER_VALID_HANDLER(ptr)                                                              \
-    do {                                                                                           \
-        if (ptr != nullptr) {                                                                      \
-            dsn::command_manager::instance().deregister_command(ptr);                              \
-            ptr = nullptr;                                                                         \
-        }                                                                                          \
-    } while (0)
+private:
+    uintptr_t cmd_id_ = 0;

Review Comment:
   Can `cmd_id_` be type of `command_instance *`, or just `std::shared_ptr<command_instance>` ?



##########
src/utils/command_manager.cpp:
##########
@@ -24,48 +24,50 @@
  * THE SOFTWARE.
  */
 
+#include "utils/command_manager.h"
+
 #include <iostream>
 #include <sstream>
 #include <thread>
 
-#include "utils/command_manager.h"
 #include "utils/fmt_logging.h"
+#include "utils/smart_pointers.h"
 #include "utils/utils.h"
 
 namespace dsn {
 
-dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
-                                               const std::string &help_one_line,
-                                               const std::string &help_long,
-                                               command_handler handler)
+std::unique_ptr<command_deregister>
+command_manager::register_command(const std::vector<std::string> &commands,
+                                  const std::string &help_one_line,
+                                  const std::string &help_long,
+                                  command_handler handler)
 {
     utils::auto_write_lock l(_lock);
     bool is_valid_cmd = false;
-
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             is_valid_cmd = true;
-            auto it = _handlers.find(cmd);
-            CHECK(it == _handlers.end(), "command '{}' already regisered", cmd);
+            CHECK(_handlers.find(cmd) == _handlers.end(), "command '{}' already regisered", cmd);
         }
     }
     CHECK(is_valid_cmd, "should not register empty command");
 
     command_instance *c = new command_instance();
     c->commands = commands;
-    c->help_long = help_long;
     c->help_short = help_one_line;
+    c->help_long = help_long;
     c->handler = handler;
 
     for (const std::string &cmd : commands) {
         if (!cmd.empty()) {
             _handlers[cmd] = c;
         }
     }
-    return c;
+
+    return dsn::make_unique<command_deregister>((uintptr_t)c);
 }
 
-void command_manager::deregister_command(dsn_handle_t handle)
+void command_manager::deregister_command(uintptr_t handle)

Review Comment:
   Should `c` be released by `delete c;` at last ?



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] acelyc111 commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
acelyc111 commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1024698408


##########
src/meta/app_balance_policy.h:
##########
@@ -25,20 +25,15 @@ class app_balance_policy : public load_balance_policy
 {
 public:
     app_balance_policy(meta_service *svc);

Review Comment:
   OK, let's refactor it in another patch.



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] levy5307 commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
levy5307 commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1024696646


##########
src/meta/app_balance_policy.h:
##########
@@ -25,20 +25,15 @@ class app_balance_policy : public load_balance_policy
 {
 public:
     app_balance_policy(meta_service *svc);

Review Comment:
       app_balance_policy(meta_service *svc) explicit;



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org


[GitHub] [incubator-pegasus] empiredan commented on a diff in pull request #1241: refactor: use command_deregister to auto deregister from command_manager

Posted by GitBox <gi...@apache.org>.
empiredan commented on code in PR #1241:
URL: https://github.com/apache/incubator-pegasus/pull/1241#discussion_r1023941637


##########
src/utils/test/command_manager-test.cpp:
##########
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+

Review Comment:
   Customarily all test source files are named as `*_test.cpp`, would it be better to name this file as `command_manager_test.cpp` ?



-- 
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: dev-unsubscribe@pegasus.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pegasus.apache.org
For additional commands, e-mail: dev-help@pegasus.apache.org