-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathprotocol_client.cc
384 lines (318 loc) · 11.1 KB
/
protocol_client.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2022, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//
#include "server/protocol_client.h"
#include "facade/tls_helpers.h"
extern "C" {
#include "redis/rdb.h"
}
#include <absl/cleanup/cleanup.h>
#include <absl/flags/flag.h>
#include <absl/functional/bind_front.h>
#include <absl/strings/escaping.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/strip.h>
#include <boost/asio/ip/tcp.hpp>
#include <string>
#include "base/logging.h"
#include "facade/dragonfly_connection.h"
#include "facade/redis_parser.h"
#include "server/error.h"
#include "server/journal/executor.h"
#include "server/journal/serializer.h"
#include "server/main_service.h"
#include "server/rdb_load.h"
#include "strings/human_readable.h"
#include "util/fibers/dns_resolve.h"
#ifdef DFLY_USE_SSL
#include "util/tls/tls_socket.h"
#endif
ABSL_FLAG(std::string, masteruser, "", "username for authentication with master");
ABSL_FLAG(std::string, masterauth, "", "password for authentication with master");
ABSL_FLAG(bool, tls_replication, false, "Enable TLS on replication");
ABSL_DECLARE_FLAG(std::string, tls_cert_file);
ABSL_DECLARE_FLAG(std::string, tls_key_file);
ABSL_DECLARE_FLAG(std::string, tls_ca_cert_file);
ABSL_DECLARE_FLAG(std::string, tls_ca_cert_dir);
namespace dfly {
using namespace std;
using namespace util;
using namespace boost::asio;
using namespace facade;
using absl::GetFlag;
using absl::StrCat;
namespace {
error_code Recv(FiberSocketBase* input, base::IoBuf* dest) {
auto buf = dest->AppendBuffer();
io::Result<size_t> exp_size = input->Recv(buf);
if (!exp_size)
return exp_size.error();
dest->CommitWrite(*exp_size);
return error_code{};
}
} // namespace
std::string ProtocolClient::ServerContext::Description() const {
return absl::StrCat(host, ":", port);
}
void ValidateClientTlsFlags() {
if (!absl::GetFlag(FLAGS_tls_replication)) {
return;
}
bool has_auth = false;
if (!absl::GetFlag(FLAGS_tls_key_file).empty()) {
if (absl::GetFlag(FLAGS_tls_cert_file).empty()) {
LOG(ERROR) << "tls_cert_file flag should be set";
exit(1);
}
has_auth = true;
}
if (!absl::GetFlag(FLAGS_masterauth).empty())
has_auth = true;
if (!has_auth) {
LOG(ERROR) << "No authentication method configured!";
exit(1);
}
}
void ProtocolClient::MaybeInitSslCtx() {
#ifdef DFLY_USE_SSL
if (absl::GetFlag(FLAGS_tls_replication)) {
ssl_ctx_ = CreateSslCntx(facade::TlsContextRole::CLIENT);
}
#endif
}
ProtocolClient::ProtocolClient(string host, uint16_t port) {
server_context_.host = std::move(host);
server_context_.port = port;
#ifdef DFLY_USE_SSL
MaybeInitSslCtx();
#endif
}
ProtocolClient::ProtocolClient(ServerContext context) : server_context_(std::move(context)) {
#ifdef DFLY_USE_SSL
MaybeInitSslCtx();
#endif
}
ProtocolClient::~ProtocolClient() {
cntx_.JoinErrorHandler();
// FIXME: We should close the socket explictly outside of the destructor. This currently
// breaks test_cancel_replication_immediately.
if (sock_) {
std::error_code ec;
sock_->proactor()->Await([this, &ec]() { ec = sock_->Close(); });
LOG_IF(ERROR, ec) << "Error closing socket " << ec;
}
#ifdef DFLY_USE_SSL
if (ssl_ctx_) {
SSL_CTX_free(ssl_ctx_);
}
#endif
}
error_code ProtocolClient::ResolveHostDns() {
char ip_addr[INET6_ADDRSTRLEN];
std::string host = server_context_.host;
if (!host.empty() && host.front() == '[' && host.back() == ']') {
host = host.substr(1, host.size() - 2);
}
auto ec = util::fb2::DnsResolve(host, 0, ip_addr, ProactorBase::me());
if (ec) {
LOG(ERROR) << "Dns error " << ec << ", host: " << server_context_.host;
return make_error_code(errc::host_unreachable);
}
LOG_IF(INFO, std::string(ip_addr) != server_context_.host)
<< "Resolved endpoint " << server_context_.Description() << " to " << ip_addr << ":"
<< server_context_.port;
server_context_.endpoint = {ip::make_address(ip_addr), server_context_.port};
return error_code{};
}
error_code ProtocolClient::ConnectAndAuth(std::chrono::milliseconds connect_timeout_ms,
ExecutionState* cntx) {
ProactorBase* mythread = ProactorBase::me();
CHECK(mythread);
{
unique_lock lk(sock_mu_);
// The context closes sock_. So if the context error handler has already
// run we must not create a new socket. sock_mu_ syncs between the two
// functions.
if (cntx->IsRunning()) {
if (sock_) {
LOG_IF(WARNING, sock_->Close()) << "Error closing socket";
sock_.reset(nullptr);
}
if (ssl_ctx_) {
auto tls_sock = std::make_unique<tls::TlsSocket>(mythread->CreateSocket());
tls_sock->InitSSL(ssl_ctx_);
sock_ = std::move(tls_sock);
} else {
sock_.reset(mythread->CreateSocket());
}
} else {
return cntx->GetError();
}
}
// We set this timeout because this call blocks other REPLICAOF commands. We don't need it for the
// rest of the sync.
{
uint32_t timeout = sock_->timeout();
sock_->set_timeout(connect_timeout_ms.count());
RETURN_ON_ERR(sock_->Connect(server_context_.endpoint));
sock_->set_timeout(timeout);
}
/* These may help but require additional field testing to learn.
int yes = 1;
CHECK_EQ(0, setsockopt(sock_->native_handle(), IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)));
CHECK_EQ(0, setsockopt(sock_->native_handle(), SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)));
int intv = 15;
CHECK_EQ(0, setsockopt(sock_->native_handle(), IPPROTO_TCP, TCP_KEEPIDLE, &intv, sizeof(intv)));
intv /= 3;
CHECK_EQ(0, setsockopt(sock_->native_handle(), IPPROTO_TCP, TCP_KEEPINTVL, &intv, sizeof(intv)));
intv = 3;
CHECK_EQ(0, setsockopt(sock_->native_handle(), IPPROTO_TCP, TCP_KEEPCNT, &intv, sizeof(intv)));
*/
auto masterauth = absl::GetFlag(FLAGS_masterauth);
auto masteruser = absl::GetFlag(FLAGS_masteruser);
ResetParser(RedisParser::Mode::CLIENT);
if (!masterauth.empty()) {
auto cmd = masteruser.empty() ? StrCat("AUTH ", masterauth)
: StrCat("AUTH ", masteruser, " ", masterauth);
RETURN_ON_ERR(SendCommandAndReadResponse(cmd));
last_cmd_ = "AUTH"; // Make sure the password is not printed to logs
PC_RETURN_ON_BAD_RESPONSE(CheckRespIsSimpleReply("OK"));
}
return error_code{};
}
void ProtocolClient::CloseSocket() {
unique_lock lk(sock_mu_);
if (sock_) {
sock_->proactor()->Await([this] {
if (sock_->IsOpen()) {
auto ec = sock_->Shutdown(SHUT_RDWR);
LOG_IF(ERROR, ec) << "Could not shutdown socket " << ec;
}
});
}
}
void ProtocolClient::DefaultErrorHandler(const GenericError& err) {
CloseSocket();
}
io::Result<ProtocolClient::ReadRespRes> ProtocolClient::ReadRespReply(base::IoBuf* buffer,
bool copy_msg) {
DCHECK(parser_);
error_code ec;
if (!buffer) {
buffer = &resp_buf_;
buffer->Clear();
}
last_resp_ = "";
uint32_t processed_bytes = 0;
RedisParser::Result result = RedisParser::OK;
while (!ec) {
uint32_t consumed;
if (buffer->InputLen() == 0 || result == RedisParser::INPUT_PENDING) {
DCHECK_GT(buffer->AppendLen(), 0u);
io::MutableBytes buf = buffer->AppendBuffer();
io::Result<size_t> size_res = sock_->Recv(buf);
if (!size_res) {
LOG(ERROR) << "Socket error " << size_res.error();
return nonstd::make_unexpected(size_res.error());
}
VLOG(2) << "Read master response of " << *size_res << " bytes";
TouchIoTime();
buffer->CommitWrite(*size_res);
}
result = parser_->Parse(buffer->InputBuffer(), &consumed, &resp_args_);
processed_bytes += consumed;
if (copy_msg)
last_resp_ +=
std::string_view(reinterpret_cast<char*>(buffer->InputBuffer().data()), consumed);
if (result == RedisParser::OK) {
return ReadRespRes{processed_bytes, consumed}; // success path
}
buffer->ConsumeInput(consumed);
if (result != RedisParser::INPUT_PENDING) {
LOG(ERROR) << "Invalid parser status " << result << " for response " << last_resp_;
return nonstd::make_unexpected(std::make_error_code(std::errc::bad_message));
}
// We need to read more data. Check that we have enough space.
if (buffer->AppendLen() < 64u) {
buffer->EnsureCapacity(buffer->Capacity() * 2);
}
}
return nonstd::make_unexpected(ec);
}
io::Result<ProtocolClient::ReadRespRes> ProtocolClient::ReadRespReply(uint32_t timeout) {
auto prev_timeout = sock_->timeout();
sock_->set_timeout(timeout);
auto res = ReadRespReply();
sock_->set_timeout(prev_timeout);
return res;
}
error_code ProtocolClient::ReadLine(base::IoBuf* io_buf, string_view* line) {
size_t eol_pos;
std::string_view input_str = ToSV(io_buf->InputBuffer());
// consume whitespace.
while (true) {
auto it = find_if_not(input_str.begin(), input_str.end(), absl::ascii_isspace);
size_t ws_len = it - input_str.begin();
io_buf->ConsumeInput(ws_len);
input_str = ToSV(io_buf->InputBuffer());
if (!input_str.empty())
break;
RETURN_ON_ERR(Recv(sock_.get(), io_buf));
input_str = ToSV(io_buf->InputBuffer());
};
// find eol.
while (true) {
eol_pos = input_str.find('\n');
if (eol_pos != std::string_view::npos) {
DCHECK_GT(eol_pos, 0u); // can not be 0 because then would be consumed as a whitespace.
if (input_str[eol_pos - 1] != '\r') {
break;
}
*line = input_str.substr(0, eol_pos - 1);
return error_code{};
}
RETURN_ON_ERR(Recv(sock_.get(), io_buf));
input_str = ToSV(io_buf->InputBuffer());
}
LOG(ERROR) << "Bad replication header: " << input_str;
return std::make_error_code(std::errc::illegal_byte_sequence);
}
bool ProtocolClient::CheckRespIsSimpleReply(string_view reply) const {
return resp_args_.size() == 1 && resp_args_.front().type == RespExpr::STRING &&
ToSV(resp_args_.front().GetBuf()) == reply;
}
bool ProtocolClient::CheckRespFirstTypes(initializer_list<RespExpr::Type> types) const {
unsigned i = 0;
for (RespExpr::Type type : types) {
if (i >= resp_args_.size() || resp_args_[i].type != type)
return false;
++i;
}
return true;
}
error_code ProtocolClient::SendCommand(string_view command) {
string formatted_command = RedisReplyBuilderBase::SerializeCommand(command);
DCHECK(sock_->proactor() == ProactorBase::me());
auto ec = sock_->Write(io::Buffer(formatted_command));
if (!ec)
TouchIoTime();
return ec;
}
error_code ProtocolClient::SendCommandAndReadResponse(string_view command) {
last_cmd_ = command;
if (auto ec = SendCommand(command); ec)
return ec;
auto response_res = ReadRespReply();
return response_res.has_value() ? error_code{} : response_res.error();
}
void ProtocolClient::ResetParser(RedisParser::Mode mode) {
// We accept any length for the parser because it has been approved by the master.
parser_.reset(new RedisParser(mode));
}
uint64_t ProtocolClient::LastIoTime() const {
return last_io_time_;
}
void ProtocolClient::TouchIoTime() {
last_io_time_ = Proactor()->GetMonotonicTimeNs();
}
} // namespace dfly