Loading...
Searching...
No Matches
geo_pbft_commitment.cpp
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
21
22#include <glog/logging.h>
23
25
26namespace resdb {
27
29 std::unique_ptr<GeoGlobalExecutor> global_executor,
30 const ResDBConfig& config, std::unique_ptr<SystemInfo> system_info,
31 ReplicaCommunicator* replica_communicator, SignatureVerifier* verifier)
32 : global_executor_(std::move(global_executor)),
33 stop_(false),
34 config_(std::move(config)),
35 system_info_(std::move(system_info)),
36 replica_communicator_(std::move(replica_communicator)),
37 verifier_(verifier) {
38 global_stats_ = Stats::GetGlobalStats();
39 executed_thread_ =
40 std::thread(&GeoPBFTCommitment::PostProcessExecutedMsg, this);
41}
42
44 stop_ = true;
45 if (executed_thread_.joinable()) {
46 executed_thread_.join();
47 }
48}
49
50bool GeoPBFTCommitment::VerifyCerts(const BatchUserRequest& request,
51 const std::string& raw_data) {
52 if (verifier_) {
53 std::string hash = request.hash();
54 for (const auto& sig : request.committed_certs().committed_certs()) {
55 bool valid = verifier_->VerifyMessage(hash, sig);
56 if (!valid) {
57 LOG(ERROR) << "sign is not valid:" << sig.DebugString();
58 return false;
59 }
60 }
61 }
62 return true;
63}
64
65bool GeoPBFTCommitment::AddNewReq(uint64_t seq, uint32_t sender_region) {
66 std::lock_guard<std::mutex> lk(mutex_);
67 if (seq < min_seq_) {
68 return false;
69 }
70 // LOG(ERROR)<<"add req seq:"<<seq<<" regioin:"<<sender_region;
71 auto ret = checklist_[seq % (1 << 20)].insert(sender_region);
72 return ret.second;
73}
74
75void GeoPBFTCommitment::UpdateSeq(uint64_t seq) {
76 std::lock_guard<std::mutex> lk(mutex_);
77 if (seq > min_seq_) {
78 min_seq_ = seq;
79 }
80}
81
82int GeoPBFTCommitment::GeoProcessCcm(std::unique_ptr<Context> context,
83 std::unique_ptr<Request> request) {
84 int sender_region_id = request->region_info().region_id();
85 if (!AddNewReq(request->seq(), sender_region_id)) {
86 // LOG(ERROR) << "geo_request already received, from sender id: "
87 // << request->sender_id() << " seq:" << request->seq() <<"
88 // region:"<<sender_region_id
89 // << " ,no more action needed.";
90 return 1;
91 }
92 // global_stats_->IncGeoRequest();
93
94 BatchUserRequest batch_request;
95 if (!batch_request.ParseFromString(request->data())) {
96 LOG(ERROR) << "[GeoGlobalExecutor] parse data fail!";
97 return -2;
98 }
99
100 if (!batch_request.has_committed_certs() ||
101 !VerifyCerts(batch_request, request->data())) {
102 // CheckCertificates
103 LOG(ERROR) << "no certs";
104 return -2;
105 }
106 // return global_executor_->OrderGeoRequest(std::move(request));
107
108 ResConfigData config_data = config_.GetConfigData();
109 int self_region_id = config_data.self_region_id();
110 // LOG(ERROR)<<"get request seq:"<<request->seq()<<" from:"<<sender_region_id;
111 // if the request comes from another region, do local broadcast
112 if (sender_region_id != self_region_id) {
113 std::unique_ptr<Request> broadcast_geo_req =
114 resdb::NewRequest(Request::TYPE_GEO_REQUEST, *request,
115 config_.GetSelfInfo().id(), sender_region_id);
116 // LOG(ERROR) << "[GeoProcessCcm] start broadcasting geo_request locally...
117 // ";
118 replica_communicator_->BroadCast(*broadcast_geo_req);
119 }
120 return global_executor_->OrderGeoRequest(std::move(request));
121}
122
123int GeoPBFTCommitment::PostProcessExecutedMsg() {
124 while (!stop_) {
125 auto batch_resp = global_executor_->GetResponseMsg();
126 if (batch_resp == nullptr) {
127 continue;
128 }
129 Request request;
130 request.set_seq(batch_resp->seq());
131 request.set_type(Request::TYPE_RESPONSE);
132 request.set_sender_id(config_.GetSelfInfo().id());
133 request.set_current_view(batch_resp->current_view());
134 request.set_proxy_id(batch_resp->proxy_id());
135 UpdateSeq(batch_resp->seq());
136 batch_resp->SerializeToString(request.mutable_data());
137 replica_communicator_->SendMessage(request, request.proxy_id());
138 }
139 return 0;
140}
141
142} // namespace resdb
MockReplicaCommunicator replica_communicator_
int GeoProcessCcm(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
GeoPBFTCommitment(std::unique_ptr< GeoGlobalExecutor > global_executor, const ResDBConfig &config, std::unique_ptr< SystemInfo > system_info_, ReplicaCommunicator *replica_communicator_, SignatureVerifier *verifier)
virtual void BroadCast(const google::protobuf::Message &message)
virtual int SendMessage(const google::protobuf::Message &message)
ResConfigData GetConfigData() const
const ReplicaInfo & GetSelfInfo() const
virtual bool VerifyMessage(const std::string &message, const SignatureInfo &sign)
static Stats * GetGlobalStats(int sleep_seconds=5)
Definition stats.cpp:29
SystemInfo system_info_
MockSignatureVerifier verifier_
std::unique_ptr< Request > NewRequest(Request::Type type, const Request &request, int sender_id)
std::unique_ptr< ResDBConfig > config_