-
Notifications
You must be signed in to change notification settings - Fork 0
/
glog_client_impl.cc
254 lines (201 loc) · 7.21 KB
/
glog_client_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <chrono>
#include "glog_client_impl.h"
#include "utils.h"
#include "glog.pb.h"
#include "paxos.pb.h"
#include "cqueue.h"
using grpc::ClientContext;
using grpc::Status;
using namespace std;
namespace {
const int DEFAULT_TIMEOUT = 100;
void set_deadline(grpc::ClientContext& context, int milliseconds)
{
context.set_deadline(
std::chrono::system_clock::now() +
std::chrono::milliseconds{milliseconds});
}
}
namespace glog {
GlogClientImpl::GlogClientImpl(
const uint64_t svrid,
std::shared_ptr<grpc::Channel> channel)
: svrid_(svrid)
, stub_(glog::Glog::NewStub(channel))
{
assert(0 < svrid_);
assert(nullptr != channel);
}
GlogClientImpl::~GlogClientImpl() = default;
void GlogClientImpl::PostMsg(const paxos::Message& msg)
{
NoopMsg reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
hassert(msg.to_id() == svrid_, "msg.peer_id %" PRIu64
" svrid_ %" PRIu64 "", msg.peer_id(), svrid_);
Status status = stub_->PostMsg(&context, msg, &reply);
if (status.ok()) {
logdebug("index %" PRIu64 " PostMsg succ", msg.index());
} else {
logerr("index %" PRIu64 " PostMsg failed", msg.index());
}
return ;
}
std::tuple<glog::ErrorCode, uint64_t, uint64_t>
GlogClientImpl::GetPaxosInfo(uint64_t logid)
{
LogId request;
request.set_logid(logid);
PaxosInfoResponse reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
Status status = stub_->GetPaxosInfo(&context, request, &reply);
if (!status.ok()) {
auto error_message = status.error_message();
logerr("Propose failed error_code %d error_message %s",
static_cast<int>(status.error_code()), error_message.c_str());
return make_tuple(ErrorCode::GRPC_ERROR, 0ull, 0ull);
}
assert(true == status.ok());
assert(ErrorCode::OK == reply.ret() ||
ErrorCode::LOGID_DONT_EXIST == reply.ret());
if (ErrorCode::LOGID_DONT_EXIST == reply.ret()) {
return make_tuple(reply.ret(), 0ull, 0ull);
}
return make_tuple(reply.ret(), reply.max_index(), reply.commited_index());
}
void GlogClientImpl::TryCatchUp()
{
NoopMsg request;
NoopMsg reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
Status status = stub_->TryCatchUp(&context, request, &reply);
if (status.ok()) {
return ;
}
auto error_message = status.error_message();
logerr("Propose failed error_code %d error_message %s",
static_cast<int>(status.error_code()), error_message.c_str());
return ;
}
glog::ErrorCode
GlogClientImpl::Set(uint64_t logid, uint64_t index, gsl::cstring_view<> data)
{
SetRequest request;
request.set_logid(logid);
request.set_index(index);
request.set_data(data.data(), data.size());
RetCode reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
Status status = stub_->Set(&context, request, &reply);
if (!status.ok()) {
auto error_message = status.error_message();
logerr("logid %" PRIu64 " index %" PRIu64
" failed error_code %d error_message %s", logid, index,
static_cast<int>(status.error_code()), error_message.c_str());
return ErrorCode::GRPC_ERROR;
}
if (ErrorCode::OK != reply.ret()) {
logerr("logid %" PRIu64 " index %" PRIu64 " Set ret %d",
logid, index, reply.ret());
return reply.ret();
}
logdebug("logid %" PRIu64 " index %" PRIu64 " Set success", logid, index);
return ErrorCode::OK; // success
}
std::tuple<glog::ErrorCode, uint64_t, std::string>
GlogClientImpl::Get(uint64_t logid, uint64_t index)
{
if (0 == index) {
return std::make_tuple(ErrorCode::INVALID_PARAMS, 0ull, "");
}
assert(0 < index);
GetRequest request;
request.set_logid(logid);
request.set_index(index);
GetResponse reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
Status status = stub_->Get(&context, request, &reply);
if (!status.ok()) {
auto error_message = status.error_message();
logerr("logid %" PRIu64 " index %" PRIu64
"failed error_code %d error_message %s", logid, index,
static_cast<int>(status.error_code()),
error_message.c_str());
return std::make_tuple(ErrorCode::GRPC_ERROR, 0ull, "");
}
if (ErrorCode::OK != reply.ret() &&
ErrorCode::UNCOMMITED_INDEX != reply.ret()) {
logerr("logid %" PRIu64 " failed index %" PRIu64 " ret %d",
logid, index, reply.ret());
return std::make_tuple(reply.ret(), 0ull, "");
}
logdebug("logid %" PRIu64
" index %" PRIu64 " Get success", logid, index);
return std::make_tuple(
reply.ret(), reply.commited_index(),
ErrorCode::OK != reply.ret() ? "" : reply.data());
}
std::tuple<glog::ErrorCode, uint64_t>
GlogClientImpl::CreateANewLog(const std::string& logname)
{
if (true == logname.empty()) {
return make_tuple(ErrorCode::INVALID_PARAMS, 0ull);
}
assert(false == logname.empty());
LogName request;
request.set_logname(logname);
LogIdResponse reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
auto status = stub_->CreateANewLog(&context, request, &reply);
if (!status.ok()) {
auto error_message = status.error_message();
logerr("%s failed error_code %d error_message %s",
logname.c_str(),
static_cast<int>(status.error_code()),
error_message.c_str());
return std::make_tuple(ErrorCode::GRPC_ERROR, 0ull);
}
if (ErrorCode::OK != reply.ret() &&
ErrorCode::UNCOMMITED_INDEX != reply.ret()) {
logerr("failed index %" PRIu64 " ret %d", index, reply.ret());
return std::make_tuple(reply.ret(), 0ull);
}
logdebug("INFO logname %s %s ret %d logid %" PRIu64,
logname.c_str(), __func__, reply.ret(), reply.logid());
return make_tuple(reply.ret(), reply.logid());
}
std::tuple<glog::ErrorCode, uint64_t>
GlogClientImpl::QueryLogId(const std::string& logname)
{
if (true == logname.empty()) {
return make_tuple(ErrorCode::INVALID_PARAMS, 0ull);
}
assert(false == logname.empty());
LogName request;
request.set_logname(logname);
LogIdResponse reply;
ClientContext context;
set_deadline(context, DEFAULT_TIMEOUT);
auto status = stub_->QueryLogId(&context, request, &reply);
if (!status.ok()) {
auto error_message = status.error_message();
logerr("%s failed error_code %d error_message %s", logname.c_str(),
static_cast<int>(status.error_code()), error_message.c_str());
return std::make_tuple(ErrorCode::GRPC_ERROR, 0ull);
}
if (ErrorCode::OK != reply.ret() &&
ErrorCode::LOGNAME_DONT_EXIST) {
logerr("failed index %" PRIu64 " ret %d", index, reply.ret());
return std::make_tuple(reply.ret(), 0ull);
}
logdebug("INFO logname %s %s ret %d logid %" PRIu64,
logname.c_str(), __func__, reply.ret(), reply.logid());
return make_tuple(reply.ret(), reply.logid());
}
} // namespace glog