You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pegasus.apache.org by "GehaFearless (via GitHub)" <gi...@apache.org> on 2023/04/12 12:42:37 UTC

[GitHub] [incubator-pegasus] GehaFearless opened a new pull request, #1436: feat(FQDN): Implement of struct host_port_group

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

   issue: https://github.com/apache/incubator-pegasus/issues/1404
   
   Implement of struct `group_host_port` used by 'rpc_host_port'.
   Also add some code on class `rpc_host_port` whose be related to `group_host_port` used.
   


-- 
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] GehaFearless commented on a diff in pull request #1436: feat(FQDN): Implement of struct host_port_group

Posted by "GehaFearless (via GitHub)" <gi...@apache.org>.
GehaFearless commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1177284731


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;

Review Comment:
   Be like `src/runtime/rpc/group_address.h`,could I change it?



-- 
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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "acelyc111 (via GitHub)" <gi...@apache.org>.
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: 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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "acelyc111 (via GitHub)" <gi...@apache.org>.
acelyc111 commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1175437577


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const { return _members; }

Review Comment:
   ```suggestion
       const std::vector<host_port> &members() const
       {
           alr_t l(_lock);
           return _members;
       }
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    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, static_cast<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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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 = kInvalidIndex;
+    _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(const rpc_group_address *g_addr)
+{
+    _name = g_addr->name();
+    for (const auto &addr : g_addr->members()) {
+        CHECK_TRUE(add(host_port(addr)));
+    }
+    _update_leader_automatically = g_addr->is_update_leader_automatically();
+    set_leader(host_port(g_addr->leader()));
+}
+
+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)
+{
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must be ipv4");
+    alw_t l(_lock);
+    if (hp.is_invalid()) {
+        _leader_index = kInvalidIndex;
+        return;
+    }
+    for (int i = 0; i < _members.size(); i++) {
+        if (_members[i] == hp) {
+            _leader_index = i;
+            return;
+        }
+    }
+
+    _members.push_back(hp);
+    _leader_index = static_cast<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 == kInvalidIndex) {
+        _leader_index = rand::next_u32(0, static_cast<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 (kInvalidIndex != _leader_index && hp == _members[_leader_index]) {
+        _leader_index = kInvalidIndex;
+    }
+
+    _members.erase(it);
+
+    return true;
+}
+
+inline bool rpc_group_host_port::contains(host_port hp) const
+{
+    alr_t l(_lock);
+    return _members.end() != std::find(_members.begin(), _members.end(), hp);
+}
+
+inline int rpc_group_host_port::count() const
+{
+    alr_t l(_lock);
+    return _members.size();
+}
+
+inline host_port rpc_group_host_port::next(host_port current) const
+{
+    alr_t l(_lock);
+    if (_members.empty()) {
+        return host_port::s_invalid_host_port;
+    }
+
+    if (current.is_invalid()) {
+        return _members[rand::next_u32(0, static_cast<uint32_t>(_members.size() - 1))];
+    }
+
+    auto it = std::find(_members.begin(), _members.end(), current);
+    if (it == _members.end()) {
+        return _members[rand::next_u32(0, static_cast<uint32_t>(_members.size() - 1))];

Review Comment:
   `rand::next_u32(0, static_cast<uint32_t>(_members.size() - 1))` apears 4 times, let's define it as a function like `uint32_t random_index_unlocked()`?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    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, static_cast<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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;

Review Comment:
   Add some comments for `_leader_index`, for example mention that it's not always valid even if `_members` is not empty.



-- 
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] GehaFearless commented on a diff in pull request #1436: feat(FQDN): Implement of struct host_port_group

Posted by "GehaFearless (via GitHub)" <gi...@apache.org>.
GehaFearless commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1171265148


##########
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:
   `ref_counter` operation function equal not do it, so do derived class 'rpc_group_host_port'.



-- 
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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "acelyc111 (via GitHub)" <gi...@apache.org>.
acelyc111 commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1178944587


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;

Review Comment:
   For group_address, keep it as before and refactor it in another patch.
   
   But it's needed to use arl_t 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: 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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "acelyc111 (via GitHub)" <gi...@apache.org>.
acelyc111 merged PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436


-- 
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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "empiredan (via GitHub)" <gi...@apache.org>.
empiredan commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1176368883


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;

Review Comment:
   arl_t ?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    host_port next(host_port current) const;

Review Comment:
   ```suggestion
       host_port next(const host_port &current) const;
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;

Review Comment:
   awl_t ?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;

Review Comment:
   ```suggestion
       bool remove(const host_port &hp) WARN_UNUSED_RESULT;
   ```



##########
src/runtime/rpc/rpc_host_port.h:
##########
@@ -57,10 +59,18 @@ class host_port
         return os << hp.to_string();
     }
 
+    rpc_group_host_port *group_host_port() const
+    {
+        CHECK_NOTNULL(_group_host_port, "group_host_port cannot be null!");
+        return _group_host_port;
+    }
+    void assign_group(const char *name);
+
 private:
     std::string _host;
     uint16_t _port = 0;
     dsn_host_type_t _type = HOST_TYPE_INVALID;
+    rpc_group_host_port *_group_host_port = nullptr;

Review Comment:
   Use ref_ptr<rpc_group_host_port> to manage `ref_counter`-typed pointer ? Or just shared_ptr ?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;
+
+    const std::vector<host_port> &members() const
+    {
+        alr_t l(_lock);
+        return _members;
+    }
+
+    uint32_t random_index_unlocked() const;
+    host_port random_member() const
+    {
+        alr_t l(_lock);
+        return _members.empty() ? host_port::s_invalid_host_port
+                                : _members[random_index_unlocked()];
+    }
+    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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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;
+    // It's not always valid even if _members is not empty.
+    // Initialization is a possible value, which needs to be negotiated.
+    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 = kInvalidIndex;
+    _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(const rpc_group_address *g_addr)
+{
+    _name = g_addr->name();
+    for (const auto &addr : g_addr->members()) {
+        CHECK_TRUE(add(host_port(addr)));
+    }
+    _update_leader_automatically = g_addr->is_update_leader_automatically();
+    set_leader(host_port(g_addr->leader()));
+}
+
+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)
+{
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must be ipv4");
+    alw_t l(_lock);
+    if (hp.is_invalid()) {
+        _leader_index = kInvalidIndex;
+        return;
+    }
+    for (int i = 0; i < _members.size(); i++) {
+        if (_members[i] == hp) {
+            _leader_index = i;
+            return;
+        }
+    }
+
+    _members.push_back(hp);
+    _leader_index = static_cast<int>(_members.size() - 1);
+}
+
+inline uint32_t rpc_group_host_port::random_index_unlocked() const
+{

Review Comment:
   Is it necessary here to CHECK if `_members` is not empty() ?



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;

Review Comment:
   ```suggestion
       bool contains(const host_port &hp) const WARN_UNUSED_RESULT;
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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;

Review Comment:
   Original `bool add(rpc_address addr)` for `rpc_group_address` is not declared as const, for the reason that its size is actually 8 bytes (`uint64_t`). `host_port` is not a simple struct (including a string-typed `_host`). Thus it's better to declare it with const.
   ```suggestion
       bool add(const host_port &hp) WARN_UNUSED_RESULT;
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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);

Review Comment:
   ```suggestion
       void set_leader(const host_port &hp);
   ```



-- 
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 #1436: feat(FQDN): Implement of struct host_port_group

Posted by "acelyc111 (via GitHub)" <gi...@apache.org>.
acelyc111 commented on code in PR #1436:
URL: https://github.com/apache/incubator-pegasus/pull/1436#discussion_r1173290001


##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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();

Review Comment:
   ```suggestion
       int count() const;
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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, static_cast<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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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 = kInvalidIndex;
+    _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(const rpc_group_address *g_addr)
+{
+    _name = g_addr->name();
+    for (const auto &addr : g_addr->members()) {
+        CHECK_TRUE(add(host_port(addr)));
+    }
+    _update_leader_automatically = g_addr->is_update_leader_automatically();
+    set_leader(host_port(g_addr->leader()));
+}
+
+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 = kInvalidIndex;
+        return;
+    }
+
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must be ipv4");

Review Comment:
   ```suggestion
       CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must be ipv4");
       alw_t l(_lock);
       if (hp.is_invalid()) {
           _leader_index = kInvalidIndex;
           return;
       }
   ```



##########
src/runtime/rpc/group_host_port.h:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "runtime/rpc/group_address.h"
+#include "runtime/rpc/group_host_port.h"
+#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 {
+
+static constexpr int kInvalidIndex = -1;
+
+// Base on group_address, a group of host_post.
+// Please use host_port like example if you want call group of host_port.
+//  e.g.
+//
+//  dsn::rpc_host_port group;
+//  group.assign_group("test");
+//  group.group_host_port()->add(host_port("test_fqdn", 34601));
+//  group.group_host_port()->add(host_port("test_fqdn", 34602));
+//  group.group_host_port()->add(host_port("test_fqdn", 34603));
+//
+class rpc_group_host_port : public ref_counter
+{
+public:
+    rpc_group_host_port(const char *name);
+    rpc_group_host_port(const rpc_group_address *g_addr);
+    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, static_cast<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();
+    // We should use 'possible_leader' for rpc group call, but not 'leader()'.
+    // Caz we not have leader sometimes in initialization phase.
+    host_port possible_leader();
+
+    // failure_detector should avoid failure detecting logic is affected by rpc failure or rpc
+    // forwarding. So we need a switch to contronl update leader automatically.
+    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 = kInvalidIndex;
+    _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(const rpc_group_address *g_addr)
+{
+    _name = g_addr->name();
+    for (const auto &addr : g_addr->members()) {
+        CHECK_TRUE(add(host_port(addr)));
+    }
+    _update_leader_automatically = g_addr->is_update_leader_automatically();
+    set_leader(host_port(g_addr->leader()));
+}
+
+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 = kInvalidIndex;
+        return;
+    }
+
+    CHECK_EQ_MSG(hp.type(), HOST_TYPE_IPV4, "rpc group host_port member must be ipv4");
+    for (int i = 0; i < _members.size(); i++) {
+        if (_members[i] == hp) {
+            _leader_index = i;
+            return;
+        }
+    }
+
+    _members.push_back(hp);
+    _leader_index = static_cast<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 == kInvalidIndex) {
+        _leader_index = rand::next_u32(0, static_cast<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 (kInvalidIndex != _leader_index && hp == _members[_leader_index]) {
+        _leader_index = kInvalidIndex;
+    }
+
+    _members.erase(it);
+
+    return true;
+}
+
+inline bool rpc_group_host_port::contains(host_port hp) const
+{
+    alr_t l(_lock);
+    return _members.end() != std::find(_members.begin(), _members.end(), hp);
+}
+
+inline int rpc_group_host_port::count()

Review Comment:
   ```suggestion
   inline int rpc_group_host_port::count() const
   ```



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