forked from apache/incubator-pegasus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation_log.cpp
More file actions
1240 lines (1071 loc) · 41.4 KB
/
mutation_log.cpp
File metadata and controls
1240 lines (1071 loc) · 41.4 KB
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mutation_log.h"
#include <fmt/core.h>
#include <algorithm>
#include <cstdint>
#include <ctime>
#include <iterator>
#include <list>
#include <set>
#include <unordered_map>
#include "aio/aio_task.h"
#include "aio/file_io.h"
#include "common/replication.codes.h"
#include "consensus_types.h"
#include "mutation_log_utils.h"
#include "replica.h"
#include "replica/log_block.h"
#include "replica/log_file.h"
#include "replica/mutation.h"
#include "runtime/api_layer1.h"
#include "utils/binary_writer.h"
#include "utils/blob.h"
#include "utils/defer.h"
#include "utils/filesystem.h"
#include "utils/flags.h"
#include "utils/fmt_logging.h"
#include "utils/latency_tracer.h"
#include "utils/ports.h"
DSN_DEFINE_bool(replication,
plog_force_flush,
false,
"when write private log, whether to flush file after write done");
namespace dsn {
namespace replication {
mutation_log_private::mutation_log_private(const std::string &dir,
int32_t max_log_file_mb,
gpid gpid,
replica *r)
: mutation_log(dir, max_log_file_mb, gpid, r), replica_base(r)
{
mutation_log_private::init_states();
}
::dsn::task_ptr mutation_log_private::append(mutation_ptr &mu,
dsn::task_code callback_code,
dsn::task_tracker *tracker,
aio_handler &&callback,
int hash,
int64_t *pending_size)
{
dsn::aio_task_ptr cb =
callback ? file::create_aio_task(
callback_code, tracker, std::forward<aio_handler>(callback), hash)
: nullptr;
_plock.lock();
ADD_POINT(mu->_tracer);
// init pending buffer
if (nullptr == _pending_write) {
_pending_write = std::make_unique<log_appender>(mark_new_offset(0, true).second);
}
_pending_write->append_mutation(mu, cb);
// update meta
_pending_write_max_commit =
std::max(_pending_write_max_commit, mu->data.header.last_committed_decree);
_pending_write_max_decree = std::max(_pending_write_max_decree, mu->data.header.decree);
// start to write if possible
if (!_is_writing.load(std::memory_order_acquire)) {
write_pending_mutations(true);
if (pending_size) {
*pending_size = 0;
}
} else {
if (pending_size) {
*pending_size = _pending_write->size();
}
_plock.unlock();
}
return cb;
}
bool mutation_log_private::get_learn_state_in_memory(decree start_decree,
binary_writer &writer) const
{
std::shared_ptr<log_appender> issued_write;
mutations pending_mutations;
{
zauto_lock l(_plock);
issued_write = _issued_write.lock();
if (_pending_write) {
pending_mutations = _pending_write->mutations();
}
}
int learned_count = 0;
if (issued_write) {
for (auto &mu : issued_write->mutations()) {
if (mu->get_decree() >= start_decree) {
mu->write_to(writer, nullptr);
learned_count++;
}
}
}
for (auto &mu : pending_mutations) {
if (mu->get_decree() >= start_decree) {
mu->write_to(writer, nullptr);
learned_count++;
}
}
return learned_count > 0;
}
void mutation_log_private::get_in_memory_mutations(decree start_decree,
ballot start_ballot,
std::vector<mutation_ptr> &mutation_list) const
{
std::shared_ptr<log_appender> issued_write;
mutations pending_mutations;
{
zauto_lock l(_plock);
issued_write = _issued_write.lock();
if (_pending_write) {
pending_mutations = _pending_write->mutations();
}
}
if (issued_write) {
for (auto &mu : issued_write->mutations()) {
// if start_ballot is invalid or equal to mu.ballot, check decree
// otherwise check ballot
ballot current_ballot = (start_ballot == invalid_ballot) ? invalid_ballot
: mu->get_ballot();
if ((mu->get_decree() >= start_decree && start_ballot == current_ballot) ||
current_ballot > start_ballot) {
mutation_list.push_back(mutation::copy_no_reply(mu));
}
}
}
for (auto &mu : pending_mutations) {
// if start_ballot is invalid or equal to mu.ballot, check decree
// otherwise check ballot
ballot current_ballot = (start_ballot == invalid_ballot) ? invalid_ballot
: mu->get_ballot();
if ((mu->get_decree() >= start_decree && start_ballot == current_ballot) ||
current_ballot > start_ballot) {
mutation_list.push_back(mutation::copy_no_reply(mu));
}
}
}
void mutation_log_private::flush() { flush_internal(-1); }
void mutation_log_private::flush_once() { flush_internal(1); }
void mutation_log_private::flush_internal(int max_count)
{
int count = 0;
while (max_count <= 0 || count < max_count) {
if (_is_writing.load(std::memory_order_acquire)) {
_tracker.wait_outstanding_tasks();
} else {
_plock.lock();
if (_is_writing.load(std::memory_order_acquire)) {
_plock.unlock();
continue;
}
if (!_pending_write) {
// !_is_writing && !_pending_write, means flush done
_plock.unlock();
break;
}
// !_is_writing && _pending_write, start next write
write_pending_mutations(true);
count++;
}
}
}
void mutation_log_private::init_states()
{
mutation_log::init_states();
_is_writing.store(false, std::memory_order_release);
_issued_write.reset();
_pending_write = nullptr;
_pending_write_max_commit = 0;
_pending_write_max_decree = 0;
}
void mutation_log_private::write_pending_mutations(bool release_lock_required)
{
CHECK(release_lock_required, "lock must be hold at this point");
CHECK(!_is_writing.load(std::memory_order_relaxed), "");
CHECK_NOTNULL(_pending_write, "");
CHECK_GT(_pending_write->size(), 0);
auto pr = mark_new_offset(_pending_write->size(), false);
CHECK_EQ_PREFIX(pr.second, _pending_write->start_offset());
_is_writing.store(true, std::memory_order_release);
update_max_decree(_private_gpid, _pending_write_max_decree);
// move or reset pending variables
std::shared_ptr<log_appender> pending = std::move(_pending_write);
_issued_write = pending;
decree max_decree = _pending_write_max_decree;
decree max_commit = _pending_write_max_commit;
_pending_write_max_commit = 0;
_pending_write_max_decree = 0;
// Free plog from lock during committing log block, in the meantime
// new mutations can still be appended.
_plock.unlock();
commit_pending_mutations(pr.first, pending, max_decree, max_commit);
}
void mutation_log_private::commit_pending_mutations(log_file_ptr &lf,
std::shared_ptr<log_appender> &pending,
decree max_decree,
decree max_commit)
{
if (dsn_unlikely(FLAGS_enable_latency_tracer)) {
for (const auto &mu : pending->mutations()) {
ADD_POINT(mu->_tracer);
}
}
lf->commit_log_blocks(
*pending,
LPC_WRITE_REPLICATION_LOG_PRIVATE,
&_tracker,
[this, lf, pending, max_decree, max_commit](error_code err, size_t sz) mutable {
CHECK(_is_writing.load(std::memory_order_relaxed), "");
for (auto &block : pending->all_blocks()) {
auto hdr = (log_block_header *)block.front().data();
CHECK_EQ(hdr->magic, 0xdeadbeef);
}
if (dsn_unlikely(FLAGS_enable_latency_tracer)) {
for (const auto &mu : pending->mutations()) {
ADD_CUSTOM_POINT(mu->_tracer, "commit_pending_completed");
}
}
// notify the callbacks
// ATTENTION: callback may be called before this code block executed
// done.
for (auto &c : pending->callbacks()) {
c->enqueue(err, sz);
}
if (err != ERR_OK) {
LOG_ERROR("write private log failed, err = {}", err);
_is_writing.store(false, std::memory_order_relaxed);
if (_io_error_callback) {
_io_error_callback(err);
}
return;
}
CHECK_EQ(sz, pending->size());
// flush to ensure that there is no gap between private log and
// in-memory buffer
// so that we can get all mutations in learning process.
//
// FIXME : the file could have been closed
if (FLAGS_plog_force_flush) {
lf->flush();
}
// Update both _plog_max_decree_on_disk and _plog_max_commit_on_disk
// after written into log file done.
update_max_decree_on_disk(max_decree, max_commit);
_is_writing.store(false, std::memory_order_relaxed);
// start to write if possible
_plock.lock();
if (!_is_writing.load(std::memory_order_acquire) && _pending_write) {
write_pending_mutations(true);
} else {
_plock.unlock();
}
},
get_gpid().thread_hash());
}
///////////////////////////////////////////////////////////////
mutation_log::mutation_log(const std::string &dir, int32_t max_log_file_mb, gpid gpid, replica *r)
{
_dir = dir;
_is_private = (gpid.value() != 0);
_max_log_file_size_in_bytes = static_cast<int64_t>(max_log_file_mb) * 1024L * 1024L;
_min_log_file_size_in_bytes = _max_log_file_size_in_bytes / 10;
_owner_replica = r;
_private_gpid = gpid;
if (r) {
CHECK_EQ(_private_gpid, r->get_gpid());
}
mutation_log::init_states();
}
void mutation_log::init_states()
{
_is_opened = false;
_switch_file_hint = false;
_switch_file_demand = false;
// logs
_last_file_index = 0;
_log_files.clear();
_current_log_file = nullptr;
_global_start_offset = 0;
_global_end_offset = 0;
// replica states
_private_log_info = {0, 0};
_plog_max_decree_on_disk = 0;
_plog_max_commit_on_disk = 0;
_cleanable_decree = 0;
}
error_code mutation_log::open(replay_callback read_callback,
io_failure_callback write_error_callback)
{
std::map<gpid, decree> replay_condition;
return open(read_callback, write_error_callback, replay_condition);
}
error_code mutation_log::open(replay_callback read_callback,
io_failure_callback write_error_callback,
const std::map<gpid, decree> &replay_condition)
{
CHECK(!_is_opened, "cannot open an opened mutation_log");
CHECK_NULL(_current_log_file, "");
// create dir if necessary
if (!dsn::utils::filesystem::path_exists(_dir)) {
if (!dsn::utils::filesystem::create_directory(_dir)) {
LOG_ERROR("open mutation_log: create log path failed");
return ERR_FILE_OPERATION_FAILED;
}
}
// load the existing logs
_log_files.clear();
_io_error_callback = write_error_callback;
std::vector<std::string> file_list;
if (!dsn::utils::filesystem::get_subfiles(_dir, file_list, false)) {
LOG_ERROR("open mutation_log: get subfiles failed.");
return ERR_FILE_OPERATION_FAILED;
}
if (nullptr == read_callback) {
CHECK(file_list.empty(), "");
}
std::sort(file_list.begin(), file_list.end());
error_code err = ERR_OK;
for (auto &fpath : file_list) {
log_file_ptr log = log_file::open_read(fpath.c_str(), err);
if (log == nullptr) {
if (err == ERR_HANDLE_EOF || err == ERR_INCOMPLETE_DATA ||
err == ERR_INVALID_PARAMETERS) {
LOG_WARNING("skip file {} during log init, err = {}", fpath, err);
continue;
}
return err;
}
if (_is_private) {
LOG_INFO("open private log {} succeed, start_offset = {}, end_offset = {}, size = "
"{}, previous_max_decree = {}",
fpath,
log->start_offset(),
log->end_offset(),
log->end_offset() - log->start_offset(),
log->previous_log_max_decree(_private_gpid));
} else {
LOG_INFO("open shared log {} succeed, start_offset = {}, end_offset = {}, size = {}",
fpath,
log->start_offset(),
log->end_offset(),
log->end_offset() - log->start_offset());
}
CHECK(_log_files.find(log->index()) == _log_files.end(),
"invalid log_index, index = {}",
log->index());
_log_files[log->index()] = log;
}
file_list.clear();
// filter useless log
log_file_map_by_index::iterator replay_begin = _log_files.begin();
log_file_map_by_index::iterator replay_end = _log_files.end();
if (!replay_condition.empty()) {
if (_is_private) {
auto find = replay_condition.find(_private_gpid);
CHECK(find != replay_condition.end(), "invalid gpid({})", _private_gpid);
for (auto it = _log_files.begin(); it != _log_files.end(); ++it) {
if (it->second->previous_log_max_decree(_private_gpid) <= find->second) {
// previous logs can be ignored
replay_begin = it;
} else {
break;
}
}
} else {
// find the largest file which can be ignored.
// after iterate, the 'mark_it' will point to the largest file which can be ignored.
log_file_map_by_index::reverse_iterator mark_it;
std::set<gpid> kickout_replicas;
replica_log_info_map max_decrees; // max_decrees for log file at mark_it.
for (mark_it = _log_files.rbegin(); mark_it != _log_files.rend(); ++mark_it) {
bool ignore_this = true;
if (mark_it == _log_files.rbegin()) {
// the last file should not be ignored
ignore_this = false;
}
if (ignore_this) {
for (auto &kv : replay_condition) {
if (kickout_replicas.find(kv.first) != kickout_replicas.end()) {
// no need to consider this replica
continue;
}
auto find = max_decrees.find(kv.first);
if (find == max_decrees.end() || find->second.max_decree <= kv.second) {
// can ignore for this replica
kickout_replicas.insert(kv.first);
} else {
ignore_this = false;
break;
}
}
}
if (ignore_this) {
// found the largest file which can be ignored
break;
}
// update max_decrees for the next log file
max_decrees = mark_it->second->previous_log_max_decrees();
}
if (mark_it != _log_files.rend()) {
// set replay_begin to the next position of mark_it.
replay_begin = _log_files.find(mark_it->first);
CHECK(replay_begin != _log_files.end(),
"invalid log_index, index = {}",
mark_it->first);
replay_begin++;
CHECK(replay_begin != _log_files.end(),
"invalid log_index, index = {}",
mark_it->first);
}
}
for (auto it = _log_files.begin(); it != replay_begin; it++) {
LOG_INFO("ignore log {}", it->second->path());
}
}
// replay with the found files
log_file_map_by_index replay_logs(replay_begin, replay_end);
int64_t end_offset = 0;
err = replay(
replay_logs,
[this, read_callback](int log_length, mutation_ptr &mu) {
bool ret = true;
if (read_callback) {
ret = read_callback(log_length,
mu); // actually replica::replay_mutation(mu, true|false);
}
if (ret) {
this->update_max_decree_no_lock(mu->data.header.pid, mu->data.header.decree);
if (this->_is_private) {
this->update_max_decree_on_disk_no_lock(mu->data.header.decree);
this->update_max_commit_on_disk_no_lock(mu->data.header.last_committed_decree);
}
}
return ret;
},
end_offset);
if (ERR_OK == err) {
_global_start_offset = _log_files.size() > 0 ? _log_files.begin()->second->start_offset()
: 0;
_global_end_offset = end_offset;
_last_file_index = _log_files.size() > 0 ? _log_files.rbegin()->first : 0;
_is_opened = true;
} else {
// clear
for (auto &kv : _log_files) {
kv.second->close();
}
_log_files.clear();
init_states();
}
return err;
}
void mutation_log::close()
{
{
zauto_lock l(_lock);
if (!_is_opened) {
return;
}
_is_opened = false;
}
LOG_DEBUG("close mutation log {}", dir());
// make all data is on disk
flush();
{
zauto_lock l(_lock);
// close current log file
if (nullptr != _current_log_file) {
_current_log_file->close();
_current_log_file = nullptr;
}
}
// reset all states
init_states();
}
error_code mutation_log::create_new_log_file()
{
// create file
uint64_t start = dsn_now_ns();
log_file_ptr logf =
log_file::create_write(_dir.c_str(), _last_file_index + 1, _global_end_offset);
if (logf == nullptr) {
LOG_ERROR("cannot create log file with index {}", _last_file_index + 1);
return ERR_FILE_OPERATION_FAILED;
}
CHECK_EQ(logf->end_offset(), logf->start_offset());
CHECK_EQ(_global_end_offset, logf->end_offset());
LOG_INFO(
"create new log file {} succeed, time_used = {} ns", logf->path(), dsn_now_ns() - start);
// update states
_last_file_index++;
CHECK(_log_files.find(_last_file_index) == _log_files.end(),
"invalid log_offset, offset = {}",
_last_file_index);
_log_files[_last_file_index] = logf;
// switch the current log file
// the old log file may be hold by _log_files or aio_task
_current_log_file = logf;
// create new pending buffer because we need write file header
// write file header into pending buffer
size_t header_len = 0;
binary_writer temp_writer;
CHECK_TRUE(_is_private);
replica_log_info_map ds;
ds[_private_gpid] =
replica_log_info(_private_log_info.max_decree, _private_log_info.valid_start_offset);
header_len = logf->write_file_header(temp_writer, ds);
log_block *blk = new log_block();
blk->add(temp_writer.get_buffer());
_global_end_offset += blk->size();
logf->commit_log_block(
*blk,
_current_log_file->start_offset(),
LPC_WRITE_REPLICATION_LOG_COMMON,
&_tracker,
[this, blk, logf](::dsn::error_code err, size_t sz) {
delete blk;
if (ERR_OK != err) {
LOG_ERROR("write mutation log file header failed, file = {}, err = {}",
logf->path(),
err);
CHECK(_io_error_callback, "");
_io_error_callback(err);
}
},
0);
CHECK_EQ_MSG(_global_end_offset,
_current_log_file->start_offset() + sizeof(log_block_header) + header_len,
"current log file start offset: {}, log_block_header size: {}, header_len: {}",
_current_log_file->start_offset(),
sizeof(log_block_header),
header_len);
return ERR_OK;
}
std::pair<log_file_ptr, int64_t> mutation_log::mark_new_offset(size_t size,
bool create_new_log_if_needed)
{
zauto_lock l(_lock);
if (create_new_log_if_needed) {
bool create_file = false;
if (_current_log_file == nullptr) {
create_file = true;
} else {
int64_t file_size = _global_end_offset - _current_log_file->start_offset();
const char *reason = nullptr;
if (_switch_file_demand) {
create_file = true;
reason = "demand";
} else if (file_size >= _max_log_file_size_in_bytes) {
create_file = true;
reason = "limit";
} else if (_switch_file_hint && file_size >= _min_log_file_size_in_bytes) {
create_file = true;
reason = "hint";
}
if (create_file) {
LOG_INFO("switch log file by {}, old_file = {}, size = {}",
reason,
_current_log_file->path(),
file_size);
}
}
if (create_file) {
auto ec = create_new_log_file();
CHECK_EQ_MSG(ec,
ERR_OK,
"{} create new log file failed",
_is_private ? _private_gpid.to_string() : "");
_switch_file_hint = false;
_switch_file_demand = false;
}
} else {
CHECK_NOTNULL(_current_log_file, "");
}
int64_t write_start_offset = _global_end_offset;
_global_end_offset += size;
return std::make_pair(_current_log_file, write_start_offset);
}
decree mutation_log::max_decree(gpid gpid) const
{
zauto_lock l(_lock);
CHECK_TRUE(_is_private);
CHECK_EQ(gpid, _private_gpid);
return _private_log_info.max_decree;
}
decree mutation_log::max_decree_on_disk() const
{
zauto_lock l(_lock);
CHECK(_is_private, "this method is only valid for private logs");
return _plog_max_decree_on_disk;
}
decree mutation_log::max_commit_on_disk() const
{
zauto_lock l(_lock);
CHECK(_is_private, "this method is only valid for private logs");
return _plog_max_commit_on_disk;
}
decree mutation_log::max_gced_decree(gpid gpid) const
{
zauto_lock l(_lock);
return max_gced_decree_no_lock(gpid);
}
decree mutation_log::max_gced_decree_no_lock(gpid gpid) const
{
CHECK(_is_private, "");
decree result = invalid_decree;
for (auto &log : _log_files) {
auto it = log.second->previous_log_max_decrees().find(gpid);
if (it != log.second->previous_log_max_decrees().end()) {
if (result == invalid_decree) {
result = it->second.max_decree;
} else {
result = std::min(result, it->second.max_decree);
}
}
}
return result;
}
void mutation_log::check_valid_start_offset(gpid gpid, int64_t valid_start_offset) const
{
zauto_lock l(_lock);
CHECK_TRUE(_is_private);
CHECK_EQ(valid_start_offset, _private_log_info.valid_start_offset);
}
int64_t mutation_log::total_size() const
{
zauto_lock l(_lock);
return total_size_no_lock();
}
int64_t mutation_log::total_size_no_lock() const
{
return _log_files.empty() ? 0 : _global_end_offset - _global_start_offset;
}
error_code mutation_log::reset_from(const std::string &dir,
replay_callback replay_error_callback,
io_failure_callback write_error_callback)
{
// Close for flushing current log and get ready to open new log files after reset.
close();
// Ensure that log files in `dir` (such as "/learn") are valid.
error_s es = log_utils::check_log_files_continuity(dir);
if (!es.is_ok()) {
LOG_ERROR("the log files of source dir {} are invalid: {}, will remove it", dir, es);
if (!utils::filesystem::remove_path(dir)) {
LOG_ERROR("remove source dir {} failed", dir);
return ERR_FILE_OPERATION_FAILED;
}
return es.code();
}
std::string temp_dir = fmt::format("{}.{}", _dir, dsn_now_ns());
if (!utils::filesystem::rename_path(_dir, temp_dir)) {
LOG_ERROR("rename current log dir {} to temp dir {} failed", _dir, temp_dir);
return ERR_FILE_OPERATION_FAILED;
}
LOG_INFO("rename current log dir {} to temp dir {}", _dir, temp_dir);
error_code err = ERR_OK;
// If successful, just remove temp dir; otherwise, rename temp dir back to current dir.
auto temp_dir_resolve = dsn::defer([this, temp_dir, &err]() {
if (err == ERR_OK) {
if (!dsn::utils::filesystem::remove_path(temp_dir)) {
// Removing temp dir failed is allowed, it's just garbage.
LOG_ERROR("remove temp dir {} failed", temp_dir);
}
} else {
// Once rollback failed, dir should be recovered manually in case data is lost.
CHECK(utils::filesystem::rename_path(temp_dir, _dir),
"rename temp dir {} back to current dir {} failed",
temp_dir,
_dir);
}
});
// Rename source dir to current dir.
if (!utils::filesystem::rename_path(dir, _dir)) {
LOG_ERROR("rename source dir {} to current dir {} failed", dir, _dir);
return err;
}
LOG_INFO("rename source dir {} to current dir {} successfully", dir, _dir);
auto dir_resolve = dsn::defer([this, dir, &err]() {
if (err != ERR_OK) {
CHECK(utils::filesystem::rename_path(_dir, dir),
"rename current dir {} back to source dir {} failed",
_dir,
dir);
}
});
// 1. Ensure that logs in current dir(such as "/plog") are valid and can be opened
// successfully;
// 2. Reopen, load and register new log files into replica;
// 3. Be sure that the old log files should have been closed.
err = open(replay_error_callback, write_error_callback);
if (err != ERR_OK) {
LOG_ERROR("the log files of current dir {} are invalid, thus open failed: {}", _dir, err);
}
return err;
}
void mutation_log::set_valid_start_offset_on_open(gpid gpid, int64_t valid_start_offset)
{
zauto_lock l(_lock);
CHECK_TRUE(_is_private);
CHECK_EQ(gpid, _private_gpid);
_private_log_info.valid_start_offset = valid_start_offset;
}
int64_t mutation_log::on_partition_reset(gpid gpid, decree max_decree)
{
zauto_lock l(_lock);
CHECK_TRUE(_is_private);
CHECK_EQ(_private_gpid, gpid);
replica_log_info old_info = _private_log_info;
_private_log_info.max_decree = max_decree;
_private_log_info.valid_start_offset = _global_end_offset;
LOG_WARNING("replica {} has changed private log max_decree from {} to {}, "
"valid_start_offset from {} to {}",
gpid,
old_info.max_decree,
_private_log_info.max_decree,
old_info.valid_start_offset,
_private_log_info.valid_start_offset);
return _global_end_offset;
}
void mutation_log::update_max_decree(gpid gpid, decree d)
{
zauto_lock l(_lock);
update_max_decree_no_lock(gpid, d);
}
void mutation_log::update_max_decree_no_lock(gpid gpid, decree d)
{
CHECK_TRUE(_is_private);
CHECK_EQ(gpid, _private_gpid);
if (d > _private_log_info.max_decree) {
_private_log_info.max_decree = d;
}
}
void mutation_log::update_max_decree_on_disk(decree max_decree, decree max_commit)
{
zauto_lock l(_lock);
update_max_decree_on_disk_no_lock(max_decree);
update_max_commit_on_disk_no_lock(max_commit);
}
void mutation_log::update_max_decree_on_disk_no_lock(decree d)
{
CHECK(_is_private, "this method is only valid for private logs");
if (d > _plog_max_decree_on_disk) {
_plog_max_decree_on_disk = d;
}
}
void mutation_log::update_max_commit_on_disk_no_lock(decree d)
{
CHECK(_is_private, "this method is only valid for private logs");
if (d > _plog_max_commit_on_disk) {
_plog_max_commit_on_disk = d;
}
}
decree mutation_log::get_cleanable_decree() const
{
zauto_lock l(_lock);
return _cleanable_decree;
}
void mutation_log::set_cleanable_decree(decree d)
{
zauto_lock l(_lock);
_cleanable_decree = d;
}
bool mutation_log::get_learn_state(gpid gpid, decree start, /*out*/ learn_state &state) const
{
CHECK(_is_private, "this method is only valid for private logs");
CHECK_EQ(_private_gpid, gpid);
binary_writer temp_writer;
if (get_learn_state_in_memory(start, temp_writer)) {
state.meta = temp_writer.get_buffer();
}
log_file_map_by_index files;
{
zauto_lock l(_lock);
if (state.meta.length() == 0 && start > _private_log_info.max_decree) {
// no memory data and no disk data
LOG_INFO("gpid({}) get_learn_state returns false"
"learn_start_decree={}, max_decree_in_private_log={}",
gpid,
start,
_private_log_info.max_decree);
return false;
}
files = _log_files;
}
// find all applicable files
bool skip_next = false;
std::list<std::string> learn_files;
log_file_ptr log;
decree last_max_decree = 0;
int learned_file_head_index = 0;
int learned_file_tail_index = 0;
int64_t learned_file_start_offset = 0;
for (auto itr = files.rbegin(); itr != files.rend(); ++itr) {
log = itr->second;
if (log->end_offset() <= _private_log_info.valid_start_offset)
break;
if (skip_next) {
skip_next = (log->previous_log_max_decrees().size() == 0);
continue;
}
if (log->end_offset() > log->start_offset()) {
// not empty file
learn_files.push_back(log->path());
if (learned_file_tail_index == 0)
learned_file_tail_index = log->index();
learned_file_head_index = log->index();
learned_file_start_offset = log->start_offset();
}
skip_next = (log->previous_log_max_decrees().size() == 0);
// continue checking as this file may be a fault
if (skip_next)
continue;
last_max_decree = log->previous_log_max_decrees().begin()->second.max_decree;
// when all possible decrees are not needed
if (last_max_decree < start) {
// skip all older logs
break;
}
}
// reverse the order, to make files ordered by index incrementally
state.files.reserve(learn_files.size());
for (auto it = learn_files.rbegin(); it != learn_files.rend(); ++it) {
state.files.push_back(*it);
}
bool ret = (learned_file_start_offset >= _private_log_info.valid_start_offset &&
last_max_decree > 0 && last_max_decree < start);
LOG_INFO("gpid({}) get_learn_state returns {}, private logs count {} ({} => {}), learned "
"files count {} ({} => {}): learned_file_start_offset({}) >= valid_start_offset({}) "
"&& last_max_decree({}) > 0 && last_max_decree({}) < learn_start_decree({})",
gpid,
ret ? "true" : "false",
files.size(),
files.empty() ? 0 : files.begin()->first,
files.empty() ? 0 : files.rbegin()->first,
learn_files.size(),
learned_file_head_index,