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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
