-
Notifications
You must be signed in to change notification settings - Fork 468
/
server.cc
1541 lines (1405 loc) · 59.4 KB
/
server.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "server.h"
#include <fcntl.h>
#include <glog/logging.h>
#include <rocksdb/convenience.h>
#include <sys/resource.h>
#include <sys/statvfs.h>
#include <sys/utsname.h>
#include <memory>
#include <utility>
#include "config.h"
#include "fmt/format.h"
#include "redis_connection.h"
#include "redis_request.h"
#include "storage/compaction_checker.h"
#include "storage/redis_db.h"
#include "storage/scripting.h"
#include "string_util.h"
#include "thread_util.h"
#include "time_util.h"
#include "tls_util.h"
#include "version.h"
#include "worker.h"
std::atomic<int> Server::unix_time_ = {0};
constexpr const char *REDIS_VERSION = "4.0.0";
Server::Server(Engine::Storage *storage, Config *config) : storage_(storage), config_(config) {
// init commands stats here to prevent concurrent insert, and cause core
auto commands = Redis::GetOriginalCommands();
for (const auto &iter : *commands) {
stats_.commands_stats[iter.first].calls = 0;
stats_.commands_stats[iter.first].latency = 0;
}
#ifdef ENABLE_OPENSSL
// init ssl context
if (config->tls_port) {
ssl_ctx_ = CreateSSLContext(config);
if (!ssl_ctx_) {
exit(1);
}
}
#endif
// Init cluster
cluster_ = std::make_unique<Cluster>(this, config_->binds, config_->port);
for (int i = 0; i < config->workers; i++) {
auto worker = std::make_unique<Worker>(this, config);
// multiple workers can't listen to the same unix socket, so
// listen unix socket only from a single worker - the first one
if (!config->unixsocket.empty() && i == 0) {
Status s = worker->ListenUnixSocket(config->unixsocket, config->unixsocketperm, config->backlog);
if (!s.IsOK()) {
LOG(ERROR) << "[server] Failed to listen on unix socket: " << config->unixsocket
<< ", encounter error: " << s.Msg();
exit(1);
}
LOG(INFO) << "[server] Listening on unix socket: " << config->unixsocket;
}
worker_threads_.emplace_back(std::make_unique<WorkerThread>(std::move(worker)));
}
AdjustOpenFilesLimit();
slow_log_.SetMaxEntries(config->slowlog_max_len);
perf_log_.SetMaxEntries(config->profiling_sample_record_max_len);
lua_ = Lua::CreateState();
fetch_file_threads_num_ = 0;
time(&start_time_);
stop_ = false;
is_loading_ = false;
}
Server::~Server() {
for (const auto &iter : conn_ctxs_) {
delete iter.first;
}
// Wait for all fetch file threads stop and exit and force destroy
// the server after 60s.
int counter = 0;
while (GetFetchFileThreadNum() != 0) {
usleep(100000);
if (++counter == 600) {
LOG(WARNING) << "Will force destroy the server after waiting 60s, leave " << GetFetchFileThreadNum()
<< " fetch file threads are still running";
break;
}
}
Lua::DestroyState(lua_);
}
// Kvrocks threads list:
// - Work-thread: process client's connections and requests
// - Task-runner: one thread pool, handle some jobs that may freeze server if run directly
// - Cron-thread: server's crontab, clean backups, resize sst and memtable size
// - Compaction-checker: active compaction according to collected statistics
// - Replication-thread: replicate incremental stream from master if in slave role, there
// are some dynamic threads to fetch files when full sync.
// - fetch-file-thread: fetch SST files from master
// - Feed-slave-thread: feed data to slaves if having slaves, but there also are some dynamic
// threads when full sync, TODO(@shooterit) we should manage this threads uniformly.
// - feed-replica-data-info: generate checkpoint and send files list when full sync
// - feed-replica-file: send SST files when slaves ask for full sync
Status Server::Start() {
if (!config_->master_host.empty()) {
Status s = AddMaster(config_->master_host, static_cast<uint32_t>(config_->master_port), false);
if (!s.IsOK()) return s;
} else {
// Generate new replication id if not a replica
auto s = storage_->ShiftReplId();
if (!s.IsOK()) {
return s.Prefixed("failed to shift replication id");
}
}
if (config_->cluster_enabled) {
auto s = cluster_->LoadClusterNodes(config_->NodesFilePath());
if (!s.IsOK()) {
LOG(ERROR) << "Failed to load cluster nodes info: " << s.Msg();
return Status(Status::NotOK, s.Msg());
}
// Create objects used for slot migration
slot_migrate_ =
std::make_unique<SlotMigrate>(this, config_->migrate_speed, config_->pipeline_size, config_->sequence_gap);
slot_import_ = new SlotImport(this);
// Create migrating thread
s = slot_migrate_->CreateMigrateHandleThread();
if (!s.IsOK()) {
LOG(ERROR) << "Failed to create migration thread, Err: " << s.Msg();
return Status(Status::NotOK);
}
}
for (const auto &worker : worker_threads_) {
worker->Start();
}
task_runner_.Start();
// setup server cron thread
cron_thread_ = std::thread([this]() {
Util::ThreadSetName("server-cron");
this->cron();
});
compaction_checker_thread_ = std::thread([this]() {
uint64_t counter = 0;
time_t last_compact_date = 0;
Util::ThreadSetName("compact-check");
CompactionChecker compaction_checker(this->storage_);
while (!stop_) {
// Sleep first
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// To guarantee accessing DB safely
auto guard = storage_->ReadLockGuard();
if (storage_->IsClosing()) continue;
if (is_loading_ == false && ++counter % 600 == 0 // check every minute
&& config_->compaction_checker_range.Enabled()) {
auto now = static_cast<time_t>(Util::GetTimeStamp());
std::tm local_time{};
localtime_r(&now, &local_time);
if (local_time.tm_hour >= config_->compaction_checker_range.Start &&
local_time.tm_hour <= config_->compaction_checker_range.Stop) {
std::vector<std::string> cf_names = {Engine::kMetadataColumnFamilyName, Engine::kSubkeyColumnFamilyName,
Engine::kZSetScoreColumnFamilyName, Engine::kStreamColumnFamilyName};
for (const auto &cf_name : cf_names) {
compaction_checker.PickCompactionFiles(cf_name);
}
}
// compact once per day
if (now != 0 && last_compact_date != now / 86400) {
last_compact_date = now / 86400;
compaction_checker.CompactPropagateAndPubSubFiles();
}
}
}
});
memory_startup_use_ = Stats::GetMemoryRSS();
LOG(INFO) << "Ready to accept connections";
return Status::OK();
}
void Server::Stop() {
stop_ = true;
if (replication_thread_) replication_thread_->Stop();
for (const auto &worker : worker_threads_) {
worker->Stop();
}
DisconnectSlaves();
rocksdb::CancelAllBackgroundWork(storage_->GetDB(), true);
task_runner_.Stop();
}
void Server::Join() {
for (const auto &worker : worker_threads_) {
worker->Join();
}
task_runner_.Join();
if (cron_thread_.joinable()) cron_thread_.join();
if (compaction_checker_thread_.joinable()) compaction_checker_thread_.join();
}
Status Server::AddMaster(const std::string &host, uint32_t port, bool force_reconnect) {
std::lock_guard<std::mutex> guard(slaveof_mu_);
// Don't check host and port if 'force_reconnect' argument is set to true
if (!force_reconnect && !master_host_.empty() && master_host_ == host && master_port_ == port) {
return Status::OK();
}
// Master is changed
if (!master_host_.empty()) {
if (replication_thread_) replication_thread_->Stop();
replication_thread_ = nullptr;
}
// For master using old version, it uses replication thread to implement
// replication, and uses 'listen-port + 1' as thread listening port.
uint32_t master_listen_port = port;
if (GetConfig()->master_use_repl_port) master_listen_port += 1;
replication_thread_ = std::make_unique<ReplicationThread>(host, master_listen_port, this);
auto s = replication_thread_->Start([this]() { PrepareRestoreDB(); },
[this]() {
this->is_loading_ = false;
task_runner_.Start();
});
if (s.IsOK()) {
master_host_ = host;
master_port_ = port;
config_->SetMaster(host, port);
} else {
replication_thread_ = nullptr;
}
return s;
}
Status Server::RemoveMaster() {
std::lock_guard<std::mutex> guard(slaveof_mu_);
if (!master_host_.empty()) {
master_host_.clear();
master_port_ = 0;
config_->ClearMaster();
if (replication_thread_) {
replication_thread_->Stop();
replication_thread_ = nullptr;
}
return storage_->ShiftReplId();
}
return Status::OK();
}
Status Server::AddSlave(Redis::Connection *conn, rocksdb::SequenceNumber next_repl_seq) {
auto t = new FeedSlaveThread(this, conn, next_repl_seq);
auto s = t->Start();
if (!s.IsOK()) {
delete t;
return s;
}
std::lock_guard<std::mutex> lg(slave_threads_mu_);
slave_threads_.emplace_back(t);
return Status::OK();
}
void Server::DisconnectSlaves() {
std::lock_guard<std::mutex> guard(slaveof_mu_);
for (const auto &slave_thread : slave_threads_) {
if (!slave_thread->IsStopped()) slave_thread->Stop();
}
while (!slave_threads_.empty()) {
auto slave_thread = slave_threads_.front();
slave_threads_.pop_front();
slave_thread->Join();
delete slave_thread;
}
}
void Server::cleanupExitedSlaves() {
std::list<FeedSlaveThread *> exited_slave_threads;
std::lock_guard<std::mutex> guard(slaveof_mu_);
for (const auto &slave_thread : slave_threads_) {
if (slave_thread->IsStopped()) exited_slave_threads.emplace_back(slave_thread);
}
while (!exited_slave_threads.empty()) {
auto t = exited_slave_threads.front();
exited_slave_threads.pop_front();
slave_threads_.remove(t);
t->Join();
delete t;
}
}
void Server::FeedMonitorConns(Redis::Connection *conn, const std::vector<std::string> &tokens) {
if (monitor_clients_ <= 0) return;
for (const auto &worker_thread : worker_threads_) {
auto worker = worker_thread->GetWorker();
worker->FeedMonitorConns(conn, tokens);
}
}
int Server::PublishMessage(const std::string &channel, const std::string &msg) {
int cnt = 0;
int index = 0;
pubsub_channels_mu_.lock();
std::vector<ConnContext> to_publish_conn_ctxs;
auto iter = pubsub_channels_.find(channel);
if (iter != pubsub_channels_.end()) {
for (const auto &conn_ctx : iter->second) {
to_publish_conn_ctxs.emplace_back(*conn_ctx);
}
}
// The patterns variable records the pattern of connections
std::vector<std::string> patterns;
std::vector<ConnContext> to_publish_patterns_conn_ctxs;
for (const auto &iter : pubsub_patterns_) {
if (Util::StringMatch(iter.first, channel, 0)) {
for (const auto &conn_ctx : iter.second) {
to_publish_patterns_conn_ctxs.emplace_back(*conn_ctx);
patterns.emplace_back(iter.first);
}
}
}
pubsub_channels_mu_.unlock();
std::string channel_reply;
channel_reply.append(Redis::MultiLen(3));
channel_reply.append(Redis::BulkString("message"));
channel_reply.append(Redis::BulkString(channel));
channel_reply.append(Redis::BulkString(msg));
for (const auto &conn_ctx : to_publish_conn_ctxs) {
auto s = conn_ctx.owner->Reply(conn_ctx.fd, channel_reply);
if (s.IsOK()) {
cnt++;
}
}
// We should publish corresponding pattern and message for connections
for (const auto &conn_ctx : to_publish_patterns_conn_ctxs) {
std::string pattern_reply;
pattern_reply.append(Redis::MultiLen(4));
pattern_reply.append(Redis::BulkString("pmessage"));
pattern_reply.append(Redis::BulkString(patterns[index++]));
pattern_reply.append(Redis::BulkString(channel));
pattern_reply.append(Redis::BulkString(msg));
auto s = conn_ctx.owner->Reply(conn_ctx.fd, pattern_reply);
if (s.IsOK()) {
cnt++;
}
}
return cnt;
}
void Server::SubscribeChannel(const std::string &channel, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
auto iter = pubsub_channels_.find(channel);
if (iter == pubsub_channels_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
pubsub_channels_.insert(std::pair<std::string, std::list<ConnContext *>>(channel, conn_ctxs));
} else {
iter->second.emplace_back(conn_ctx);
}
}
void Server::UnSubscribeChannel(const std::string &channel, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
auto iter = pubsub_channels_.find(channel);
if (iter == pubsub_channels_.end()) {
return;
}
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
iter->second.remove(conn_ctx);
if (iter->second.empty()) {
pubsub_channels_.erase(iter);
}
break;
}
}
}
void Server::GetChannelsByPattern(const std::string &pattern, std::vector<std::string> *channels) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
for (const auto &iter : pubsub_channels_) {
if (pattern.empty() || Util::StringMatch(pattern, iter.first, 0)) {
channels->emplace_back(iter.first);
}
}
}
void Server::ListChannelSubscribeNum(const std::vector<std::string> &channels,
std::vector<ChannelSubscribeNum> *channel_subscribe_nums) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
for (const auto &chan : channels) {
auto iter = pubsub_channels_.find(chan);
if (iter != pubsub_channels_.end()) {
channel_subscribe_nums->emplace_back(ChannelSubscribeNum{iter->first, iter->second.size()});
} else {
channel_subscribe_nums->emplace_back(ChannelSubscribeNum{chan, 0});
}
}
}
void Server::PSubscribeChannel(const std::string &pattern, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
auto iter = pubsub_patterns_.find(pattern);
if (iter == pubsub_patterns_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
pubsub_patterns_.insert(std::pair<std::string, std::list<ConnContext *>>(pattern, conn_ctxs));
} else {
iter->second.emplace_back(conn_ctx);
}
}
void Server::PUnSubscribeChannel(const std::string &pattern, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
auto iter = pubsub_patterns_.find(pattern);
if (iter == pubsub_patterns_.end()) {
return;
}
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
iter->second.remove(conn_ctx);
if (iter->second.empty()) {
pubsub_patterns_.erase(iter);
}
break;
}
}
}
void Server::AddBlockingKey(const std::string &key, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
auto iter = blocking_keys_.find(key);
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
if (iter == blocking_keys_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
blocking_keys_.insert(std::pair<std::string, std::list<ConnContext *>>(key, conn_ctxs));
} else {
iter->second.emplace_back(conn_ctx);
}
IncrBlockedClientNum();
}
void Server::UnBlockingKey(const std::string &key, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
auto iter = blocking_keys_.find(key);
if (iter == blocking_keys_.end()) {
return;
}
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
iter->second.remove(conn_ctx);
if (iter->second.empty()) {
blocking_keys_.erase(iter);
}
break;
}
}
DecrBlockedClientNum();
}
void Server::BlockOnStreams(const std::vector<std::string> &keys, const std::vector<Redis::StreamEntryID> &entry_ids,
Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
IncrBlockedClientNum();
for (size_t i = 0; i < keys.size(); ++i) {
auto consumer = std::make_shared<StreamConsumer>(conn->Owner(), conn->GetFD(), conn->GetNamespace(), entry_ids[i]);
auto iter = blocked_stream_consumers_.find(keys[i]);
if (iter == blocked_stream_consumers_.end()) {
std::set<std::shared_ptr<StreamConsumer>> consumers;
consumers.insert(consumer);
blocked_stream_consumers_.insert(std::make_pair(keys[i], consumers));
} else {
iter->second.insert(consumer);
}
}
}
void Server::UnblockOnStreams(const std::vector<std::string> &keys, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
DecrBlockedClientNum();
for (const auto &key : keys) {
auto iter = blocked_stream_consumers_.find(key);
if (iter == blocked_stream_consumers_.end()) {
continue;
}
for (auto it = iter->second.begin(); it != iter->second.end();) {
const auto &consumer = *it;
if (conn->GetFD() == consumer->fd && conn->Owner() == consumer->owner) {
iter->second.erase(it);
if (iter->second.empty()) {
blocked_stream_consumers_.erase(iter);
}
break;
}
}
}
}
void Server::WakeupBlockingConns(const std::string &key, size_t n_conns) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
auto iter = blocking_keys_.find(key);
if (iter == blocking_keys_.end() || iter->second.empty()) {
return;
}
while (n_conns-- && !iter->second.empty()) {
auto conn_ctx = iter->second.front();
auto s = conn_ctx->owner->EnableWriteEvent(conn_ctx->fd);
if (!s.IsOK()) {
LOG(ERROR) << "failed to enable write event on blocked client " << conn_ctx->fd << ": " << s.Msg();
}
delConnContext(conn_ctx);
iter->second.pop_front();
}
}
void Server::OnEntryAddedToStream(const std::string &ns, const std::string &key, const Redis::StreamEntryID &entry_id) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
auto iter = blocked_stream_consumers_.find(key);
if (iter == blocked_stream_consumers_.end() || iter->second.empty()) {
return;
}
for (auto it = iter->second.begin(); it != iter->second.end();) {
auto consumer = *it;
if (consumer->ns == ns && entry_id > consumer->last_consumed_id) {
auto s = consumer->owner->EnableWriteEvent(consumer->fd);
if (!s.IsOK()) {
LOG(ERROR) << "failed to enable write event on blocked stream consumer " << consumer->fd << ": " << s.Msg();
}
it = iter->second.erase(it);
} else {
++it;
}
}
}
void Server::delConnContext(ConnContext *c) {
auto conn_ctx_iter = conn_ctxs_.find(c);
if (conn_ctx_iter != conn_ctxs_.end()) {
delete conn_ctx_iter->first;
conn_ctxs_.erase(conn_ctx_iter);
}
}
void Server::updateCachedTime() {
time_t ret = time(nullptr);
if (ret == -1) return;
unix_time_.store(static_cast<int>(ret));
}
int Server::IncrClientNum() {
total_clients_.fetch_add(1, std::memory_order::memory_order_relaxed);
return connected_clients_.fetch_add(1, std::memory_order_relaxed);
}
int Server::DecrClientNum() { return connected_clients_.fetch_sub(1, std::memory_order_relaxed); }
int Server::IncrMonitorClientNum() { return monitor_clients_.fetch_add(1, std::memory_order_relaxed); }
int Server::DecrMonitorClientNum() { return monitor_clients_.fetch_sub(1, std::memory_order_relaxed); }
int Server::IncrBlockedClientNum() { return blocked_clients_.fetch_add(1, std::memory_order_relaxed); }
int Server::DecrBlockedClientNum() { return blocked_clients_.fetch_sub(1, std::memory_order_relaxed); }
std::unique_ptr<RWLock::ReadLock> Server::WorkConcurrencyGuard() {
return std::make_unique<RWLock::ReadLock>(works_concurrency_rw_lock_);
}
std::unique_ptr<RWLock::WriteLock> Server::WorkExclusivityGuard() {
return std::make_unique<RWLock::WriteLock>(works_concurrency_rw_lock_);
}
std::atomic<uint64_t> *Server::GetClientID() { return &client_id_; }
void Server::recordInstantaneousMetrics() {
auto rocksdb_stats = storage_->GetDB()->GetDBOptions().statistics;
stats_.TrackInstantaneousMetric(STATS_METRIC_COMMAND, stats_.total_calls);
stats_.TrackInstantaneousMetric(STATS_METRIC_NET_INPUT, stats_.in_bytes);
stats_.TrackInstantaneousMetric(STATS_METRIC_NET_OUTPUT, stats_.out_bytes);
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_PUT,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_KEYS_WRITTEN));
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_GET,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_KEYS_READ));
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_MULTIGET,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_MULTIGET_KEYS_READ));
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_SEEK,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_DB_SEEK));
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_NEXT,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_DB_NEXT));
stats_.TrackInstantaneousMetric(STATS_METRIC_ROCKSDB_PREV,
rocksdb_stats->getTickerCount(rocksdb::Tickers::NUMBER_DB_PREV));
}
void Server::cron() {
uint64_t counter = 0;
while (!stop_) {
// Sleep first
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// To guarantee accessing DB safely
auto guard = storage_->ReadLockGuard();
if (storage_->IsClosing()) continue;
updateCachedTime();
counter++;
if (is_loading_) {
// We need to skip the cron operations since `is_loading_` means the db is restoring,
// and the db pointer will be modified after that. It will panic if we use the db pointer
// before the new db was reopened.
continue;
}
// check every 20s (use 20s instead of 60s so that cron will execute in critical condition)
if (counter != 0 && counter % 200 == 0) {
auto t = static_cast<time_t>(Util::GetTimeStamp());
std::tm now{};
localtime_r(&t, &now);
// disable compaction cron when the compaction checker was enabled
if (!config_->compaction_checker_range.Enabled() && config_->compact_cron.IsEnabled() &&
config_->compact_cron.IsTimeMatch(&now)) {
Status s = AsyncCompactDB();
LOG(INFO) << "[server] Schedule to compact the db, result: " << s.Msg();
}
if (config_->bgsave_cron.IsEnabled() && config_->bgsave_cron.IsTimeMatch(&now)) {
Status s = AsyncBgsaveDB();
LOG(INFO) << "[server] Schedule to bgsave the db, result: " << s.Msg();
}
}
// check every 10s
if (counter != 0 && counter % 100 == 0) {
Status s = AsyncPurgeOldBackups(config_->max_backup_to_keep, config_->max_backup_keep_hours);
// Purge backup if needed, it will cost much disk space if we keep backup and full sync
// checkpoints at the same time
if (config_->purge_backup_on_fullsync && (storage_->ExistCheckpoint() || storage_->ExistSyncCheckpoint())) {
s = AsyncPurgeOldBackups(0, 0);
}
}
// No replica uses this checkpoint, we can remove it.
if (counter != 0 && counter % 100 == 0) {
time_t create_time = storage_->GetCheckpointCreateTime();
time_t access_time = storage_->GetCheckpointAccessTime();
if (storage_->ExistCheckpoint()) {
// TODO(shooterit): support to config the alive time of checkpoint
auto now = static_cast<time_t>(Util::GetTimeStamp());
if ((GetFetchFileThreadNum() == 0 && now - access_time > 30) || (now - create_time > 24 * 60 * 60)) {
auto s = rocksdb::DestroyDB(config_->checkpoint_dir, rocksdb::Options());
if (!s.ok()) {
LOG(WARNING) << "[server] Fail to clean checkpoint, error: " << s.ToString();
} else {
LOG(INFO) << "[server] Clean checkpoint successfully";
}
}
}
}
// check if DB need to be resumed every minute
// Rocksdb has auto resume feature after retryable io error, earlier version(before v6.22.1) had
// bug when encounter no space error. The current version fixes the no space error issue, but it
// does not completely resolve, which still exists when encountered disk quota exceeded error.
// In order to properly handle all possible situations on rocksdb, we manually resume here
// when encountering no space error and disk quota exceeded error.
if (counter != 0 && counter % 600 == 0 && storage_->IsDBInRetryableIOError()) {
storage_->GetDB()->Resume();
LOG(INFO) << "[server] Schedule to resume DB after retryable io error";
storage_->SetDBInRetryableIOError(false);
}
cleanupExitedSlaves();
recordInstantaneousMetrics();
}
}
void Server::GetRocksDBInfo(std::string *info) {
std::ostringstream string_stream;
rocksdb::DB *db = storage_->GetDB();
uint64_t memtable_sizes = 0, cur_memtable_sizes = 0, num_snapshots = 0, num_running_flushes = 0;
uint64_t num_immutable_tables = 0, memtable_flush_pending = 0, compaction_pending = 0;
uint64_t num_running_compaction = 0, num_live_versions = 0, num_superversion = 0, num_backgroud_errors = 0;
db->GetAggregatedIntProperty("rocksdb.num-snapshots", &num_snapshots);
db->GetAggregatedIntProperty("rocksdb.size-all-mem-tables", &memtable_sizes);
db->GetAggregatedIntProperty("rocksdb.cur-size-all-mem-tables", &cur_memtable_sizes);
db->GetAggregatedIntProperty("rocksdb.num-running-flushes", &num_running_flushes);
db->GetAggregatedIntProperty("rocksdb.num-immutable-mem-table", &num_immutable_tables);
db->GetAggregatedIntProperty("rocksdb.mem-table-flush-pending", &memtable_flush_pending);
db->GetAggregatedIntProperty("rocksdb.num-running-compactions", &num_running_compaction);
db->GetAggregatedIntProperty("rocksdb.current-super-version-number", &num_superversion);
db->GetAggregatedIntProperty("rocksdb.background-errors", &num_backgroud_errors);
db->GetAggregatedIntProperty("rocksdb.compaction-pending", &compaction_pending);
db->GetAggregatedIntProperty("rocksdb.num-live-versions", &num_live_versions);
string_stream << "# RocksDB\r\n";
for (const auto &cf_handle : *storage_->GetCFHandles()) {
uint64_t estimate_keys = 0, block_cache_usage = 0, block_cache_pinned_usage = 0, index_and_filter_cache_usage = 0;
std::map<std::string, std::string> cf_stats_map;
db->GetIntProperty(cf_handle, "rocksdb.estimate-num-keys", &estimate_keys);
string_stream << "estimate_keys[" << cf_handle->GetName() << "]:" << estimate_keys << "\r\n";
db->GetIntProperty(cf_handle, "rocksdb.block-cache-usage", &block_cache_usage);
string_stream << "block_cache_usage[" << cf_handle->GetName() << "]:" << block_cache_usage << "\r\n";
db->GetIntProperty(cf_handle, "rocksdb.block-cache-pinned-usage", &block_cache_pinned_usage);
string_stream << "block_cache_pinned_usage[" << cf_handle->GetName() << "]:" << block_cache_pinned_usage << "\r\n";
db->GetIntProperty(cf_handle, "rocksdb.estimate-table-readers-mem", &index_and_filter_cache_usage);
string_stream << "index_and_filter_cache_usage[" << cf_handle->GetName() << "]:" << index_and_filter_cache_usage
<< "\r\n";
db->GetMapProperty(cf_handle, rocksdb::DB::Properties::kCFStats, &cf_stats_map);
string_stream << "level0_file_limit_slowdown[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.level0_slowdown"] << "\r\n";
string_stream << "level0_file_limit_stop[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.level0_numfiles"] << "\r\n";
string_stream << "pending_compaction_bytes_slowdown[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.slowdown_for_pending_compaction_bytes"] << "\r\n";
string_stream << "pending_compaction_bytes_stop[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.stop_for_pending_compaction_bytes"] << "\r\n";
string_stream << "memtable_count_limit_slowdown[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.memtable_slowdown"] << "\r\n";
string_stream << "memtable_count_limit_stop[" << cf_handle->GetName()
<< "]:" << cf_stats_map["io_stalls.memtable_compaction"] << "\r\n";
}
string_stream << "all_mem_tables:" << memtable_sizes << "\r\n";
string_stream << "cur_mem_tables:" << cur_memtable_sizes << "\r\n";
string_stream << "snapshots:" << num_snapshots << "\r\n";
string_stream << "num_immutable_tables:" << num_immutable_tables << "\r\n";
string_stream << "num_running_flushes:" << num_running_flushes << "\r\n";
string_stream << "memtable_flush_pending:" << memtable_flush_pending << "\r\n";
string_stream << "compaction_pending:" << compaction_pending << "\r\n";
string_stream << "num_running_compactions:" << num_running_compaction << "\r\n";
string_stream << "num_live_versions:" << num_live_versions << "\r\n";
string_stream << "num_superversion:" << num_superversion << "\r\n";
string_stream << "num_background_errors:" << num_backgroud_errors << "\r\n";
string_stream << "flush_count:" << storage_->GetFlushCount() << "\r\n";
string_stream << "compaction_count:" << storage_->GetCompactionCount() << "\r\n";
string_stream << "put_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_PUT) << "\r\n";
string_stream << "get_per_sec:"
<< stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_GET) +
stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_MULTIGET)
<< "\r\n";
string_stream << "seek_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_SEEK) << "\r\n";
string_stream << "next_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_NEXT) << "\r\n";
string_stream << "prev_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_PREV) << "\r\n";
string_stream << "is_bgsaving:" << (is_bgsave_in_progress_ ? "yes" : "no") << "\r\n";
string_stream << "is_compacting:" << (db_compacting_ ? "yes" : "no") << "\r\n";
*info = string_stream.str();
}
void Server::GetServerInfo(std::string *info) {
time_t now = 0;
std::ostringstream string_stream;
static int call_uname = 1;
static utsname name;
if (call_uname) {
/* Uname can be slow and is always the same output. Cache it. */
uname(&name);
call_uname = 0;
}
time(&now);
string_stream << "# Server\r\n";
string_stream << "version:" << VERSION << "\r\n";
string_stream << "redis_version:" << REDIS_VERSION << "\r\n";
string_stream << "git_sha1:" << GIT_COMMIT << "\r\n";
string_stream << "os:" << name.sysname << " " << name.release << " " << name.machine << "\r\n";
#ifdef __GNUC__
string_stream << "gcc_version:" << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << "\r\n";
#else
string_stream << "gcc_version:0,0,0\r\n";
#endif
string_stream << "arch_bits:" << sizeof(void *) * 8 << "\r\n";
string_stream << "process_id:" << getpid() << "\r\n";
string_stream << "tcp_port:" << config_->port << "\r\n";
string_stream << "uptime_in_seconds:" << now - start_time_ << "\r\n";
string_stream << "uptime_in_days:" << (now - start_time_) / 86400 << "\r\n";
*info = string_stream.str();
}
void Server::GetClientsInfo(std::string *info) {
std::ostringstream string_stream;
string_stream << "# Clients\r\n";
string_stream << "maxclients:" << config_->maxclients << "\r\n";
string_stream << "connected_clients:" << connected_clients_ << "\r\n";
string_stream << "monitor_clients:" << monitor_clients_ << "\r\n";
string_stream << "blocked_clients:" << blocked_clients_ << "\r\n";
*info = string_stream.str();
}
void Server::GetMemoryInfo(std::string *info) {
std::ostringstream string_stream;
int64_t rss = Stats::GetMemoryRSS();
int memory_lua = lua_gc(lua_, LUA_GCCOUNT, 0) * 1024;
std::string used_memory_rss_human = Util::BytesToHuman(rss);
std::string used_memory_lua_human = Util::BytesToHuman(memory_lua);
string_stream << "# Memory\r\n";
string_stream << "used_memory_rss:" << rss << "\r\n";
string_stream << "used_memory_human:" << used_memory_rss_human << "\r\n";
string_stream << "used_memory_lua:" << memory_lua << "\r\n";
string_stream << "used_memory_lua_human:" << used_memory_lua_human << "\r\n";
string_stream << "used_memory_startup:" << memory_startup_use_ << "\r\n";
*info = string_stream.str();
}
void Server::GetReplicationInfo(std::string *info) {
time_t now = 0;
std::ostringstream string_stream;
string_stream << "# Replication\r\n";
string_stream << "role:" << (IsSlave() ? "slave" : "master") << "\r\n";
if (IsSlave()) {
time(&now);
string_stream << "master_host:" << master_host_ << "\r\n";
string_stream << "master_port:" << master_port_ << "\r\n";
ReplState state = GetReplicationState();
string_stream << "master_link_status:" << (state == kReplConnected ? "up" : "down") << "\r\n";
string_stream << "master_sync_unrecoverable_error:" << (state == kReplError ? "yes" : "no") << "\r\n";
string_stream << "master_sync_in_progress:" << (state == kReplFetchMeta || state == kReplFetchSST) << "\r\n";
string_stream << "master_last_io_seconds_ago:" << now - replication_thread_->LastIOTime() << "\r\n";
string_stream << "slave_repl_offset:" << storage_->LatestSeq() << "\r\n";
string_stream << "slave_priority:" << config_->slave_priority << "\r\n";
}
int idx = 0;
rocksdb::SequenceNumber latest_seq = storage_->LatestSeq();
slave_threads_mu_.lock();
string_stream << "connected_slaves:" << slave_threads_.size() << "\r\n";
for (const auto &slave : slave_threads_) {
if (slave->IsStopped()) continue;
string_stream << "slave" << std::to_string(idx) << ":";
string_stream << "ip=" << slave->GetConn()->GetIP() << ",port=" << slave->GetConn()->GetListeningPort()
<< ",offset=" << slave->GetCurrentReplSeq() << ",lag=" << latest_seq - slave->GetCurrentReplSeq()
<< "\r\n";
++idx;
}
slave_threads_mu_.unlock();
string_stream << "master_repl_offset:" << latest_seq << "\r\n";
*info = string_stream.str();
}
void Server::GetRoleInfo(std::string *info) {
if (IsSlave()) {
std::vector<std::string> roles;
roles.emplace_back("slave");
roles.emplace_back(master_host_);
roles.emplace_back(std::to_string(master_port_));
auto state = GetReplicationState();
if (state == kReplConnected) {
roles.emplace_back("connected");
} else if (state == kReplFetchMeta || state == kReplFetchSST) {
roles.emplace_back("sync");
} else {
roles.emplace_back("connecting");
}
roles.emplace_back(std::to_string(storage_->LatestSeq()));
*info = Redis::MultiBulkString(roles);
} else {
std::vector<std::string> list;
slave_threads_mu_.lock();
for (const auto &slave : slave_threads_) {
if (slave->IsStopped()) continue;
list.emplace_back(Redis::MultiBulkString({
slave->GetConn()->GetIP(),
std::to_string(slave->GetConn()->GetListeningPort()),
std::to_string(slave->GetCurrentReplSeq()),
}));
}
slave_threads_mu_.unlock();
auto multi_len = 2;
if (list.size() > 0) {
multi_len = 3;
}
info->append(Redis::MultiLen(multi_len));
info->append(Redis::BulkString("master"));
info->append(Redis::BulkString(std::to_string(storage_->LatestSeq())));
if (list.size() > 0) {
info->append(Redis::Array(list));
}
}
}
std::string Server::GetLastRandomKeyCursor() {
std::string cursor;
std::lock_guard<std::mutex> guard(last_random_key_cursor_mu_);
cursor = last_random_key_cursor_;
return cursor;
}
void Server::SetLastRandomKeyCursor(const std::string &cursor) {
std::lock_guard<std::mutex> guard(last_random_key_cursor_mu_);
last_random_key_cursor_ = cursor;
}
int Server::GetUnixTime() {
if (unix_time_.load() == 0) {
time_t ret = time(nullptr);
unix_time_.store(static_cast<int>(ret));
}
return unix_time_.load();
}
void Server::GetStatsInfo(std::string *info) {
std::ostringstream string_stream;
string_stream << "# Stats\r\n";
string_stream << "total_connections_received:" << total_clients_ << "\r\n";
string_stream << "total_commands_processed:" << stats_.total_calls << "\r\n";
string_stream << "instantaneous_ops_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_COMMAND) << "\r\n";
string_stream << "total_net_input_bytes:" << stats_.in_bytes << "\r\n";
string_stream << "total_net_output_bytes:" << stats_.out_bytes << "\r\n";
string_stream << "instantaneous_input_kbps:"
<< static_cast<float>(stats_.GetInstantaneousMetric(STATS_METRIC_NET_INPUT) / 1024) << "\r\n";
string_stream << "instantaneous_output_kbps:"
<< static_cast<float>(stats_.GetInstantaneousMetric(STATS_METRIC_NET_OUTPUT) / 1024) << "\r\n";
string_stream << "sync_full:" << stats_.fullsync_counter << "\r\n";
string_stream << "sync_partial_ok:" << stats_.psync_ok_counter << "\r\n";
string_stream << "sync_partial_err:" << stats_.psync_err_counter << "\r\n";
{
std::lock_guard<std::mutex> lg(pubsub_channels_mu_);
string_stream << "pubsub_channels:" << pubsub_channels_.size() << "\r\n";
string_stream << "pubsub_patterns:" << pubsub_patterns_.size() << "\r\n";
}
*info = string_stream.str();
}
void Server::GetCommandsStatsInfo(std::string *info) {
std::ostringstream string_stream;
string_stream << "# Commandstats\r\n";
for (const auto &cmd_stat : stats_.commands_stats) {
auto calls = cmd_stat.second.calls.load();
auto latency = cmd_stat.second.latency.load();
if (calls == 0) continue;
string_stream << "cmdstat_" << cmd_stat.first << ":calls=" << calls << ",usec=" << latency
<< ",usec_per_call=" << ((calls == 0) ? 0 : static_cast<float>(latency / calls)) << "\r\n";
}
*info = string_stream.str();
}
// WARNING: we must not access DB(i.e.RocksDB) when server is loading since
// DB is closed and the pointer is invalid. Server may crash if we access DB
// during loading.
// If you add new fields which access DB into INFO command output, make sure
// this section cant't be shown when loading(i.e. !is_loading_).
void Server::GetInfo(const std::string &ns, const std::string §ion, std::string *info) {
info->clear();
std::ostringstream string_stream;
bool all = section == "all";
int section_cnt = 0;
if (all || section == "server") {
std::string server_info;
GetServerInfo(&server_info);
if (section_cnt++) string_stream << "\r\n";
string_stream << server_info;
}
if (all || section == "clients") {
std::string clients_info;
GetClientsInfo(&clients_info);
if (section_cnt++) string_stream << "\r\n";
string_stream << clients_info;
}
if (all || section == "memory") {
std::string memory_info;
GetMemoryInfo(&memory_info);