Loading...
Searching...
No Matches
transaction_constructor.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
24namespace resdb {
25
27 : NetChannel("", 0),
28 config_(config),
29 timeout_ms_(
30 config.GetClientTimeoutMs()) { // default 2s for process timeout
31 socket_->SetRecvTimeout(timeout_ms_);
32}
33
34absl::StatusOr<std::string> TransactionConstructor::GetResponseData(
35 const Response& response) {
36 std::string hash_;
37 std::set<int64_t> hash_counter;
38 std::string resp_str;
39 for (const auto& each_resp : response.resp()) {
40 // Check signature
41 std::string hash = SignatureVerifier::CalculateHash(each_resp.data());
42
43 if (!hash_.empty() && hash != hash_) {
44 LOG(ERROR) << "hash not the same";
45 return absl::InvalidArgumentError("hash not match");
46 }
47 if (hash_.empty()) {
48 hash_ = hash;
49 resp_str = each_resp.data();
50 }
51 hash_counter.insert(each_resp.signature().node_id());
52 }
53 // LOG(INFO) << "recv hash:" << hash_counter.size()
54 // << " need:" << config_.GetMinClientReceiveNum()
55 // << " data len:" << resp_str.size();
56 if (hash_counter.size() >=
57 static_cast<size_t>(config_.GetMinClientReceiveNum())) {
58 return resp_str;
59 }
60 return absl::InvalidArgumentError("data not enough");
61}
62
64 const google::protobuf::Message& message, Request::Type type) {
65 // Use the replica obtained from the server.
67 return NetChannel::SendRequest(message, type, false);
68}
69
71 const google::protobuf::Message& message,
72 google::protobuf::Message* response, Request::Type type) {
74 int ret = NetChannel::SendRequest(message, type, true);
75 if (ret == 0) {
76 std::string resp_str;
77 int ret = NetChannel::RecvRawMessageData(&resp_str);
78 if (ret >= 0) {
79 if (!response->ParseFromString(resp_str)) {
80 LOG(ERROR) << "parse response fail:" << resp_str.size();
81 return -2;
82 }
83 return 0;
84 }
85 }
86 return -1;
87}
88
89} // namespace resdb
virtual int RecvRawMessageData(std::string *message_str)
void SetRecvTimeout(int microseconds)
std::unique_ptr< Socket > socket_
Definition net_channel.h:98
virtual int SendRequest(const google::protobuf::Message &message, Request::Type cmd, bool need_response=false)
void SetDestReplicaInfo(const ReplicaInfo &replica)
int GetMinClientReceiveNum() const
const std::vector< ReplicaInfo > & GetReplicaInfos() const
static std::string CalculateHash(const std::string &str)
int SendRequest(const google::protobuf::Message &message, Request::Type type=Request::TYPE_CLIENT_REQUEST)
TransactionConstructor(const ResDBConfig &config)
std::unique_ptr< ResDBConfig > config_