acelyc111 commented on code in PR #1436:
URL: 
https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1164387949


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)

Review Comment:
   Use Apache 2.0 license for the new added files.



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Microsoft Corporation
+ *
+ * -=- Robust Distributed System Nucleus (rDSN) -=-
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/autoref_ptr.h"
+#include "utils/fmt_logging.h"
+#include "utils/rand.h"
+#include "utils/synchronize.h"
+
+namespace dsn {
+
+class rpc_group_host_port : public ref_counter

Review Comment:
   Add commments to explain this class, for example why introduce it, how to 
use it, and etc.



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Microsoft Corporation
+ *
+ * -=- Robust Distributed System Nucleus (rDSN) -=-
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/autoref_ptr.h"
+#include "utils/fmt_logging.h"
+#include "utils/rand.h"
+#include "utils/synchronize.h"
+
+namespace dsn {
+
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_host_port &other);
+    rpc_group_host_port &operator=(const rpc_group_host_port &other);
+    bool add(host_port hp) WARN_UNUSED_RESULT;
+    void add_list(const std::vector<host_port> &hps)
+    {
+        for (const auto &hp : hps) {
+            LOG_WARNING_IF(!add(hp), "duplicate adress {}", hp);
+        }
+    }
+    void set_leader(host_port hp);
+    bool remove(host_port hp) WARN_UNUSED_RESULT;
+    bool contains(host_port hp) const WARN_UNUSED_RESULT;
+    int count();
+
+    const std::vector<host_port> &members() const { return _members; }
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[rand::next_u32(0, 
(uint32_t)_members.size() - 1)];
+    }
+    host_port next(host_port current) const;
+    host_port leader() const
+    {
+        alr_t l(_lock);
+        return _leader_index >= 0 ? _members[_leader_index] : 
host_port::s_invalid_host_port;
+    }
+    void leader_forward();
+    host_port possible_leader();
+    bool is_update_leader_automatically() const { return 
_update_leader_automatically; }
+    void set_update_leader_automatically(bool value) { 
_update_leader_automatically = value; }
+    const char *name() const { return _name.c_str(); }
+
+private:
+    typedef std::vector<host_port> members_t;
+    typedef utils::auto_read_lock alr_t;
+    typedef utils::auto_write_lock alw_t;
+
+    mutable utils::rw_lock_nr _lock;
+    members_t _members;
+    int _leader_index;
+    bool _update_leader_automatically;

Review Comment:
   Add some necessary comments to these variables, they are not very 
self-evident.



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Microsoft Corporation
+ *
+ * -=- Robust Distributed System Nucleus (rDSN) -=-
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/autoref_ptr.h"
+#include "utils/fmt_logging.h"
+#include "utils/rand.h"
+#include "utils/synchronize.h"
+
+namespace dsn {
+
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_host_port &other);
+    rpc_group_host_port &operator=(const rpc_group_host_port &other);
+    bool add(host_port hp) WARN_UNUSED_RESULT;
+    void add_list(const std::vector<host_port> &hps)
+    {
+        for (const auto &hp : hps) {
+            LOG_WARNING_IF(!add(hp), "duplicate adress {}", hp);
+        }
+    }
+    void set_leader(host_port hp);
+    bool remove(host_port hp) WARN_UNUSED_RESULT;
+    bool contains(host_port hp) const WARN_UNUSED_RESULT;
+    int count();
+
+    const std::vector<host_port> &members() const { return _members; }
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[rand::next_u32(0, 
(uint32_t)_members.size() - 1)];
+    }
+    host_port next(host_port current) const;
+    host_port leader() const
+    {
+        alr_t l(_lock);
+        return _leader_index >= 0 ? _members[_leader_index] : 
host_port::s_invalid_host_port;
+    }
+    void leader_forward();
+    host_port possible_leader();
+    bool is_update_leader_automatically() const { return 
_update_leader_automatically; }
+    void set_update_leader_automatically(bool value) { 
_update_leader_automatically = value; }
+    const char *name() const { return _name.c_str(); }
+
+private:
+    typedef std::vector<host_port> members_t;
+    typedef utils::auto_read_lock alr_t;
+    typedef utils::auto_write_lock alw_t;
+
+    mutable utils::rw_lock_nr _lock;
+    members_t _members;
+    int _leader_index;
+    bool _update_leader_automatically;
+    std::string _name;
+};
+
+// ------------------ inline implementation --------------------
+
+inline rpc_group_host_port::rpc_group_host_port(const char *name)
+{
+    _name = name;
+    _leader_index = -1;
+    _update_leader_automatically = true;
+}
+
+inline rpc_group_host_port::rpc_group_host_port(const rpc_group_host_port 
&other)
+{
+    _name = other._name;
+    _leader_index = other._leader_index;
+    _update_leader_automatically = other._update_leader_automatically;
+    _members = other._members;
+}
+
+inline rpc_group_host_port &rpc_group_host_port::operator=(const 
rpc_group_host_port &other)
+{
+    if (this == &other) {
+        return *this;
+    }
+    _name = other._name;
+    _leader_index = other._leader_index;
+    _update_leader_automatically = other._update_leader_automatically;
+    _members = other._members;
+    return *this;
+}
+
+inline bool rpc_group_host_port::add(host_port hp)
+{
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must 
be ipv4");
+
+    alw_t l(_lock);
+    if (_members.end() == std::find(_members.begin(), _members.end(), hp)) {
+        _members.push_back(hp);
+        return true;
+    } else {
+        return false;
+    }
+}
+
+inline void rpc_group_host_port::leader_forward()
+{
+    alw_t l(_lock);
+    if (_members.empty()) {
+        return;
+    }
+    _leader_index = (_leader_index + 1) % _members.size();
+}
+
+inline void rpc_group_host_port::set_leader(host_port hp)
+{
+    alw_t l(_lock);
+    if (hp.is_invalid()) {
+        _leader_index = -1;
+        return;
+    }
+
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must 
be ipv4");
+    for (int i = 0; i < (int)_members.size(); i++) {
+        if (_members[i] == hp) {
+            _leader_index = i;
+            return;
+        }
+    }
+
+    _members.push_back(hp);
+    _leader_index = (int)(_members.size() - 1);
+}
+
+inline host_port rpc_group_host_port::possible_leader()
+{
+    alw_t l(_lock);
+    if (_members.empty()) {
+        return host_port::s_invalid_host_port;
+    }
+    if (_leader_index == -1) {
+        _leader_index = rand::next_u32(0, (uint32_t)_members.size() - 1);
+    }
+    return _members[_leader_index];
+}
+
+inline bool rpc_group_host_port::remove(host_port hp)
+{
+    alw_t l(_lock);
+    auto it = std::find(_members.begin(), _members.end(), hp);
+    if (it == _members.end()) {
+        return false;
+    }
+
+    if (-1 != _leader_index && hp == _members[_leader_index]) {

Review Comment:
   How about define `-1` as a static const variable like `kInvalidIndex`?



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -43,8 +44,17 @@ host_port::host_port(rpc_address addr)
               addr.ipv4_str());
         _port = addr.port();
     } break;
-    case HOST_TYPE_GROUP:
-        CHECK(false, "type HOST_TYPE_GROUP not support!");
+    case HOST_TYPE_GROUP: {
+        auto group_address = addr.group_address();
+        *this = host_port();
+        this->assign_group(group_address->name());
+        for (const auto &address : group_address->members()) {
+            CHECK_TRUE(this->group_host_port()->add(host_port(address)));
+        }
+        this->group_host_port()->set_update_leader_automatically(
+            group_address->is_update_leader_automatically());
+        
this->group_host_port()->set_leader(host_port(group_address->leader()));

Review Comment:
   How about encapsulate these code as the c'stor of group_host_port?



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -43,8 +44,17 @@ host_port::host_port(rpc_address addr)
               addr.ipv4_str());
         _port = addr.port();
     } break;
-    case HOST_TYPE_GROUP:
-        CHECK(false, "type HOST_TYPE_GROUP not support!");
+    case HOST_TYPE_GROUP: {
+        auto group_address = addr.group_address();
+        *this = host_port();
+        this->assign_group(group_address->name());

Review Comment:
   `this->` can be omitted?



##########
src/runtime/rpc/rpc_host_port.h:
##########
@@ -57,10 +60,14 @@ class host_port
         return os << hp.to_string();
     }
 
+    rpc_group_host_port *group_host_port() const { return _group_host_port; }

Review Comment:
   Add a check to ensure _group_host_port is not nullptr.



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Microsoft Corporation
+ *
+ * -=- Robust Distributed System Nucleus (rDSN) -=-
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/autoref_ptr.h"
+#include "utils/fmt_logging.h"
+#include "utils/rand.h"
+#include "utils/synchronize.h"
+
+namespace dsn {
+
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_host_port &other);
+    rpc_group_host_port &operator=(const rpc_group_host_port &other);
+    bool add(host_port hp) WARN_UNUSED_RESULT;
+    void add_list(const std::vector<host_port> &hps)
+    {
+        for (const auto &hp : hps) {
+            LOG_WARNING_IF(!add(hp), "duplicate adress {}", hp);
+        }
+    }
+    void set_leader(host_port hp);
+    bool remove(host_port hp) WARN_UNUSED_RESULT;
+    bool contains(host_port hp) const WARN_UNUSED_RESULT;
+    int count();
+
+    const std::vector<host_port> &members() const { return _members; }
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[rand::next_u32(0, 
(uint32_t)_members.size() - 1)];
+    }
+    host_port next(host_port current) const;
+    host_port leader() const
+    {
+        alr_t l(_lock);
+        return _leader_index >= 0 ? _members[_leader_index] : 
host_port::s_invalid_host_port;
+    }
+    void leader_forward();
+    host_port possible_leader();

Review Comment:
   Add some necessary comments to these functions, they are not very 
self-evident.



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -79,7 +90,9 @@ host_port &host_port::operator=(const host_port &other)
         _port = other.port();
         break;
     case HOST_TYPE_GROUP:
-        CHECK(false, "type HOST_TYPE_GROUP not support!");
+        _group_host_port = other.group_host_port();
+        group_host_port()->add_ref();

Review Comment:
   How about add_ref() in the equal operator of group_host_port?



##########
src/runtime/rpc/rpc_host_port.cpp:
##########
@@ -43,8 +44,17 @@ host_port::host_port(rpc_address addr)
               addr.ipv4_str());
         _port = addr.port();
     } break;
-    case HOST_TYPE_GROUP:
-        CHECK(false, "type HOST_TYPE_GROUP not support!");
+    case HOST_TYPE_GROUP: {
+        auto group_address = addr.group_address();
+        *this = host_port();

Review Comment:
   This line can be removed?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,221 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Microsoft Corporation
+ *
+ * -=- Robust Distributed System Nucleus (rDSN) -=-
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/autoref_ptr.h"
+#include "utils/fmt_logging.h"
+#include "utils/rand.h"
+#include "utils/synchronize.h"
+
+namespace dsn {
+
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_host_port &other);
+    rpc_group_host_port &operator=(const rpc_group_host_port &other);
+    bool add(host_port hp) WARN_UNUSED_RESULT;
+    void add_list(const std::vector<host_port> &hps)
+    {
+        for (const auto &hp : hps) {
+            LOG_WARNING_IF(!add(hp), "duplicate adress {}", hp);
+        }
+    }
+    void set_leader(host_port hp);
+    bool remove(host_port hp) WARN_UNUSED_RESULT;
+    bool contains(host_port hp) const WARN_UNUSED_RESULT;
+    int count();
+
+    const std::vector<host_port> &members() const { return _members; }
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[rand::next_u32(0, 
(uint32_t)_members.size() - 1)];
+    }
+    host_port next(host_port current) const;
+    host_port leader() const
+    {
+        alr_t l(_lock);
+        return _leader_index >= 0 ? _members[_leader_index] : 
host_port::s_invalid_host_port;
+    }
+    void leader_forward();
+    host_port possible_leader();
+    bool is_update_leader_automatically() const { return 
_update_leader_automatically; }
+    void set_update_leader_automatically(bool value) { 
_update_leader_automatically = value; }
+    const char *name() const { return _name.c_str(); }
+
+private:
+    typedef std::vector<host_port> members_t;
+    typedef utils::auto_read_lock alr_t;
+    typedef utils::auto_write_lock alw_t;
+
+    mutable utils::rw_lock_nr _lock;
+    members_t _members;
+    int _leader_index;
+    bool _update_leader_automatically;
+    std::string _name;
+};
+
+// ------------------ inline implementation --------------------
+
+inline rpc_group_host_port::rpc_group_host_port(const char *name)
+{
+    _name = name;
+    _leader_index = -1;
+    _update_leader_automatically = true;
+}
+
+inline rpc_group_host_port::rpc_group_host_port(const rpc_group_host_port 
&other)
+{
+    _name = other._name;
+    _leader_index = other._leader_index;
+    _update_leader_automatically = other._update_leader_automatically;
+    _members = other._members;
+}
+
+inline rpc_group_host_port &rpc_group_host_port::operator=(const 
rpc_group_host_port &other)
+{
+    if (this == &other) {
+        return *this;
+    }
+    _name = other._name;
+    _leader_index = other._leader_index;
+    _update_leader_automatically = other._update_leader_automatically;
+    _members = other._members;
+    return *this;
+}
+
+inline bool rpc_group_host_port::add(host_port hp)
+{
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must 
be ipv4");
+
+    alw_t l(_lock);
+    if (_members.end() == std::find(_members.begin(), _members.end(), hp)) {
+        _members.push_back(hp);
+        return true;
+    } else {
+        return false;
+    }
+}
+
+inline void rpc_group_host_port::leader_forward()
+{
+    alw_t l(_lock);
+    if (_members.empty()) {
+        return;
+    }
+    _leader_index = (_leader_index + 1) % _members.size();
+}
+
+inline void rpc_group_host_port::set_leader(host_port hp)
+{
+    alw_t l(_lock);
+    if (hp.is_invalid()) {
+        _leader_index = -1;
+        return;
+    }
+
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must 
be ipv4");
+    for (int i = 0; i < (int)_members.size(); i++) {

Review Comment:
   ```suggestion
       for (int i = 0; i < static_cast<int>(_members.size()); i++) {
   ```
   Some other places are the same.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to