Loading...
Searching...
No Matches
replica_communicator.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21#include <thread>
22
27#include "platform/proto/replica_info.pb.h"
28#include "platform/proto/resdb.pb.h"
30
31namespace resdb {
32
33// ReplicaCommunicator is used for replicas to send messages
34// between replicas.
36 public:
37 ReplicaCommunicator(const std::vector<ReplicaInfo>& replicas,
38 SignatureVerifier* verifier = nullptr,
39 bool is_use_long_conn = false, int epoll_num = 1,
40 int tcp_batch = 100);
41 virtual ~ReplicaCommunicator();
42
43 // HeartBeat message is used to broadcast public keys.
44 // It doesn't need the signature.
45 virtual int SendHeartBeat(const Request& hb_info);
46
47 virtual int SendMessage(const google::protobuf::Message& message);
48 virtual int SendMessage(const google::protobuf::Message& message,
49 const ReplicaInfo& replica_info);
50
51 virtual void BroadCast(const google::protobuf::Message& message);
52 virtual void SendMessage(const google::protobuf::Message& message,
53 int64_t node_id);
54 virtual int SendBatchMessage(
55 const std::vector<std::unique_ptr<Request>>& messages,
56 const ReplicaInfo& replica_info);
57
58 void UpdateClientReplicas(const std::vector<ReplicaInfo>& replicas);
59 std::vector<ReplicaInfo> GetClientReplicas();
60
61 protected:
62 virtual std::unique_ptr<NetChannel> GetClient(const std::string& ip,
63 int port);
64 virtual AsyncReplicaClient* GetClientFromPool(const std::string& ip,
65 int port);
66
68 int SendMessageInternal(const google::protobuf::Message& message,
69 const std::vector<ReplicaInfo>& replicas);
70 int SendMessageFromPool(const google::protobuf::Message& message,
71 const std::vector<ReplicaInfo>& replicas);
72
73 bool IsRunning() const;
74 bool IsInPool(const ReplicaInfo& replica_info);
75
76 private:
77 std::vector<ReplicaInfo> replicas_;
78 SignatureVerifier* verifier_;
79 std::map<std::pair<std::string, int>, std::unique_ptr<AsyncReplicaClient>>
80 client_pools_;
81 std::thread broadcast_thread_;
82 std::atomic<bool> is_running_;
83 struct QueueItem {
84 std::string data;
85 std::vector<ReplicaInfo> dest_replicas;
86 };
88 bool is_use_long_conn_ = false;
89
90 Stats* global_stats_;
91 boost::asio::io_service io_service_;
92 std::unique_ptr<boost::asio::io_service::work> worker_;
93 std::vector<std::thread> worker_threads_;
94 std::vector<ReplicaInfo> clients_;
95 std::mutex mutex_;
96};
97
98} // namespace resdb
std::string data
virtual void BroadCast(const google::protobuf::Message &message)
int SendMessageFromPool(const google::protobuf::Message &message, const std::vector< ReplicaInfo > &replicas)
int SendMessageInternal(const google::protobuf::Message &message, const std::vector< ReplicaInfo > &replicas)
virtual std::unique_ptr< NetChannel > GetClient(const std::string &ip, int port)
virtual int SendBatchMessage(const std::vector< std::unique_ptr< Request > > &messages, const ReplicaInfo &replica_info)
virtual int SendHeartBeat(const Request &hb_info)
std::vector< ReplicaInfo > GetClientReplicas()
bool IsInPool(const ReplicaInfo &replica_info)
virtual int SendMessage(const google::protobuf::Message &message)
virtual AsyncReplicaClient * GetClientFromPool(const std::string &ip, int port)
void UpdateClientReplicas(const std::vector< ReplicaInfo > &replicas)