forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtablet.cpp
2680 lines (2419 loc) · 112 KB
/
tablet.cpp
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 "olap/tablet.h"
#include <butil/logging.h>
#include <bvar/reducer.h>
#include <bvar/window.h>
#include <fmt/format.h>
#include <gen_cpp/FrontendService_types.h>
#include <gen_cpp/MasterService_types.h>
#include <gen_cpp/Metrics_types.h>
#include <gen_cpp/olap_file.pb.h>
#include <gen_cpp/types.pb.h>
#include <rapidjson/document.h>
#include <rapidjson/encodings.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <algorithm>
#include <atomic>
#include <boost/container/detail/std_fwd.hpp>
#include <roaring/roaring.hh>
#include "common/compiler_util.h" // IWYU pragma: keep
// IWYU pragma: no_include <bits/chrono.h>
#include <chrono> // IWYU pragma: keep
#include <filesystem>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <shared_mutex>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include "agent/utils.h"
#include "common/config.h"
#include "common/consts.h"
#include "common/logging.h"
#include "common/signal_handler.h"
#include "common/status.h"
#include "exprs/runtime_filter.h"
#include "gutil/ref_counted.h"
#include "gutil/strings/substitute.h"
#include "io/fs/file_reader.h"
#include "io/fs/file_reader_writer_fwd.h"
#include "io/fs/file_system.h"
#include "io/fs/file_writer.h"
#include "io/fs/path.h"
#include "io/fs/remote_file_system.h"
#include "io/io_common.h"
#include "olap/base_compaction.h"
#include "olap/base_tablet.h"
#include "olap/binlog.h"
#include "olap/cumulative_compaction.h"
#include "olap/cumulative_compaction_policy.h"
#include "olap/cumulative_compaction_time_series_policy.h"
#include "olap/delete_bitmap_calculator.h"
#include "olap/full_compaction.h"
#include "olap/memtable.h"
#include "olap/olap_common.h"
#include "olap/olap_define.h"
#include "olap/olap_meta.h"
#include "olap/primary_key_index.h"
#include "olap/rowset/beta_rowset.h"
#include "olap/rowset/rowset.h"
#include "olap/rowset/rowset_factory.h"
#include "olap/rowset/rowset_meta.h"
#include "olap/rowset/rowset_meta_manager.h"
#include "olap/rowset/rowset_writer.h"
#include "olap/rowset/rowset_writer_context.h"
#include "olap/rowset/segment_v2/column_reader.h"
#include "olap/rowset/segment_v2/common.h"
#include "olap/rowset/segment_v2/indexed_column_reader.h"
#include "olap/rowset/vertical_beta_rowset_writer.h"
#include "olap/schema_change.h"
#include "olap/single_replica_compaction.h"
#include "olap/storage_engine.h"
#include "olap/storage_policy.h"
#include "olap/tablet_manager.h"
#include "olap/tablet_meta.h"
#include "olap/tablet_meta_manager.h"
#include "olap/tablet_schema.h"
#include "olap/txn_manager.h"
#include "olap/types.h"
#include "olap/utils.h"
#include "segment_loader.h"
#include "service/point_query_executor.h"
#include "tablet.h"
#include "util/bvar_helper.h"
#include "util/debug_points.h"
#include "util/defer_op.h"
#include "util/doris_metrics.h"
#include "util/pretty_printer.h"
#include "util/scoped_cleanup.h"
#include "util/stopwatch.hpp"
#include "util/threadpool.h"
#include "util/time.h"
#include "util/trace.h"
#include "util/uid_util.h"
#include "util/work_thread_pool.hpp"
#include "vec/columns/column.h"
#include "vec/columns/column_string.h"
#include "vec/common/schema_util.h"
#include "vec/common/string_ref.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/data_type_factory.hpp"
#include "vec/data_types/serde/data_type_serde.h"
#include "vec/jsonb/serialize.h"
namespace doris {
class TupleDescriptor;
namespace vectorized {
class Block;
} // namespace vectorized
using namespace ErrorCode;
using namespace std::chrono_literals;
using std::pair;
using std::string;
using std::vector;
using io::FileSystemSPtr;
namespace {
bvar::Adder<uint64_t> exceed_version_limit_counter;
bvar::Window<bvar::Adder<uint64_t>> exceed_version_limit_counter_minute(
&exceed_version_limit_counter, 60);
bvar::Adder<uint64_t> cooldown_pending_task("cooldown_pending_task");
bvar::Adder<uint64_t> cooldown_processing_task("cooldown_processing_task");
void set_last_failure_time(Tablet* tablet, const Compaction& compaction, int64_t ms) {
switch (compaction.compaction_type()) {
case ReaderType::READER_CUMULATIVE_COMPACTION:
tablet->set_last_cumu_compaction_failure_time(ms);
return;
case ReaderType::READER_BASE_COMPACTION:
tablet->set_last_base_compaction_failure_time(ms);
return;
case ReaderType::READER_FULL_COMPACTION:
tablet->set_last_full_compaction_failure_time(ms);
return;
default:
LOG(FATAL) << "invalid compaction type " << compaction.compaction_name()
<< " tablet_id: " << tablet->tablet_id();
}
};
} // namespace
bvar::Adder<uint64_t> unused_remote_rowset_num("unused_remote_rowset_num");
WriteCooldownMetaExecutors::WriteCooldownMetaExecutors(size_t executor_nums)
: _executor_nums(executor_nums) {
for (size_t i = 0; i < _executor_nums; i++) {
std::unique_ptr<PriorityThreadPool> pool;
static_cast<void>(ThreadPoolBuilder("WriteCooldownMetaExecutor")
.set_min_threads(1)
.set_max_threads(1)
.set_max_queue_size(std::numeric_limits<int>::max())
.build(&pool));
_executors.emplace_back(std::move(pool));
}
}
void WriteCooldownMetaExecutors::stop() {
for (auto& pool_ptr : _executors) {
if (pool_ptr) {
pool_ptr->shutdown();
}
}
}
void WriteCooldownMetaExecutors::WriteCooldownMetaExecutors::submit(TabletSharedPtr tablet) {
auto tablet_id = tablet->tablet_id();
{
std::shared_lock rdlock(tablet->get_header_lock());
if (!tablet->tablet_meta()->cooldown_meta_id().initialized()) {
VLOG_NOTICE << "tablet " << tablet_id << " is not cooldown replica";
return;
}
if (tablet->tablet_state() == TABLET_SHUTDOWN) [[unlikely]] {
LOG_INFO("tablet {} has been dropped, don't do cooldown", tablet_id);
return;
}
}
{
// one tablet could at most have one cooldown task to be done
std::unique_lock<std::mutex> lck {_latch};
if (_pending_tablets.count(tablet_id) > 0) {
return;
}
_pending_tablets.insert(tablet_id);
}
auto async_write_task = [this, t = std::move(tablet)]() {
{
std::unique_lock<std::mutex> lck {_latch};
_pending_tablets.erase(t->tablet_id());
}
auto s = t->write_cooldown_meta();
if (s.ok()) {
return;
}
if (!s.is<ABORTED>()) {
LOG_EVERY_SECOND(WARNING)
<< "write tablet " << t->tablet_id() << " cooldown meta failed because: " << s;
submit(t);
return;
}
VLOG_DEBUG << "tablet " << t->tablet_id() << " is not cooldown replica";
};
cooldown_pending_task << 1;
_executors[_get_executor_pos(tablet_id)]->offer([task = std::move(async_write_task)]() {
cooldown_pending_task << -1;
cooldown_processing_task << 1;
task();
cooldown_processing_task << -1;
});
}
Tablet::Tablet(StorageEngine& engine, TabletMetaSharedPtr tablet_meta, DataDir* data_dir,
const std::string_view& cumulative_compaction_type)
: BaseTablet(std::move(tablet_meta)),
_engine(engine),
_data_dir(data_dir),
_is_bad(false),
_last_cumu_compaction_failure_millis(0),
_last_base_compaction_failure_millis(0),
_last_full_compaction_failure_millis(0),
_last_cumu_compaction_success_millis(0),
_last_base_compaction_success_millis(0),
_last_full_compaction_success_millis(0),
_cumulative_point(K_INVALID_CUMULATIVE_POINT),
_newly_created_rowset_num(0),
_last_checkpoint_time(0),
_cumulative_compaction_type(cumulative_compaction_type),
_is_tablet_path_exists(true),
_last_missed_version(-1),
_last_missed_time_s(0) {
if (_data_dir != nullptr) {
_tablet_path = fmt::format("{}/{}/{}/{}/{}", _data_dir->path(), DATA_PREFIX,
_tablet_meta->shard_id(), tablet_id(), schema_hash());
}
}
bool Tablet::set_tablet_schema_into_rowset_meta() {
bool flag = false;
for (auto&& rowset_meta : _tablet_meta->all_mutable_rs_metas()) {
if (!rowset_meta->tablet_schema()) {
rowset_meta->set_tablet_schema(_tablet_meta->tablet_schema());
flag = true;
}
}
return flag;
}
Status Tablet::_init_once_action() {
Status res = Status::OK();
VLOG_NOTICE << "begin to load tablet. tablet=" << tablet_id()
<< ", version_size=" << _tablet_meta->version_count();
#ifdef BE_TEST
// init cumulative compaction policy by type
_cumulative_compaction_policy =
CumulativeCompactionPolicyFactory::create_cumulative_compaction_policy(
_tablet_meta->compaction_policy());
#endif
for (const auto& rs_meta : _tablet_meta->all_rs_metas()) {
Version version = rs_meta->version();
RowsetSharedPtr rowset;
res = create_rowset(rs_meta, &rowset);
if (!res.ok()) {
LOG(WARNING) << "fail to init rowset. tablet_id=" << tablet_id()
<< ", schema_hash=" << schema_hash() << ", version=" << version
<< ", res=" << res;
return res;
}
_rs_version_map[version] = std::move(rowset);
}
// init stale rowset
for (const auto& stale_rs_meta : _tablet_meta->all_stale_rs_metas()) {
Version version = stale_rs_meta->version();
RowsetSharedPtr rowset;
res = create_rowset(stale_rs_meta, &rowset);
if (!res.ok()) {
LOG(WARNING) << "fail to init stale rowset. tablet_id:" << tablet_id()
<< ", schema_hash:" << schema_hash() << ", version=" << version
<< ", res:" << res;
return res;
}
_stale_rs_version_map[version] = std::move(rowset);
}
return res;
}
Status Tablet::init() {
return _init_once.call([this] { return _init_once_action(); });
}
// should save tablet meta to remote meta store
// if it's a primary replica
void Tablet::save_meta() {
auto res = _tablet_meta->save_meta(_data_dir);
CHECK_EQ(res, Status::OK()) << "fail to save tablet_meta. res=" << res
<< ", root=" << _data_dir->path();
}
// Caller should hold _meta_lock.
Status Tablet::revise_tablet_meta(const std::vector<RowsetSharedPtr>& to_add,
const std::vector<RowsetSharedPtr>& to_delete,
bool is_incremental_clone) {
LOG(INFO) << "begin to revise tablet. tablet_id=" << tablet_id();
// 1. for incremental clone, we have to add the rowsets first to make it easy to compute
// all the delete bitmaps, and it's easy to delete them if we end up with a failure
// 2. for full clone, we can calculate delete bitmaps on the cloned rowsets directly.
if (is_incremental_clone) {
CHECK(to_delete.empty()); // don't need to delete rowsets
add_rowsets(to_add);
// reconstruct from tablet meta
_timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas());
}
Status calc_bm_status;
std::vector<RowsetSharedPtr> base_rowsets_for_full_clone = to_add; // copy vector
while (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) {
std::vector<RowsetSharedPtr> calc_delete_bitmap_rowsets;
int64_t to_add_min_version = INT64_MAX;
int64_t to_add_max_version = INT64_MIN;
for (auto& rs : to_add) {
if (to_add_min_version > rs->start_version()) {
to_add_min_version = rs->start_version();
}
if (to_add_max_version < rs->end_version()) {
to_add_max_version = rs->end_version();
}
}
Version calc_delete_bitmap_ver;
if (is_incremental_clone) {
// From the rowset of to_add with smallest version, all other rowsets
// need to recalculate the delete bitmap
// For example:
// local tablet: [0-1] [2-5] [6-6] [9-10]
// clone tablet: [7-7] [8-8]
// new tablet: [0-1] [2-5] [6-6] [7-7] [8-8] [9-10]
// [7-7] [8-8] [9-10] need to recalculate delete bitmap
calc_delete_bitmap_ver = Version(to_add_min_version, max_version_unlocked());
} else {
// the delete bitmap of to_add's rowsets has clone from remote when full clone.
// only other rowsets in local need to recalculate the delete bitmap.
// For example:
// local tablet: [0-1]x [2-5]x [6-6]x [7-7]x [9-10]
// clone tablet: [0-1] [2-4] [5-6] [7-8]
// new tablet: [0-1] [2-4] [5-6] [7-8] [9-10]
// only [9-10] need to recalculate delete bitmap
CHECK_EQ(to_add_min_version, 0) << "to_add_min_version is: " << to_add_min_version;
calc_delete_bitmap_ver = Version(to_add_max_version + 1, max_version_unlocked());
}
if (calc_delete_bitmap_ver.first <= calc_delete_bitmap_ver.second) {
calc_bm_status = capture_consistent_rowsets_unlocked(calc_delete_bitmap_ver,
&calc_delete_bitmap_rowsets);
if (!calc_bm_status.ok()) {
LOG(WARNING) << "fail to capture_consistent_rowsets, res: " << calc_bm_status;
break;
}
// FIXME(plat1ko): Use `const TabletSharedPtr&` as parameter
auto self = _engine.tablet_manager()->get_tablet(tablet_id());
CHECK(self);
for (auto rs : calc_delete_bitmap_rowsets) {
if (is_incremental_clone) {
calc_bm_status = update_delete_bitmap_without_lock(self, rs);
} else {
calc_bm_status = update_delete_bitmap_without_lock(
self, rs, &base_rowsets_for_full_clone);
base_rowsets_for_full_clone.push_back(rs);
}
if (!calc_bm_status.ok()) {
LOG(WARNING) << "fail to update_delete_bitmap_without_lock, res: "
<< calc_bm_status;
break;
}
}
}
break; // while (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write())
}
DBUG_EXECUTE_IF("Tablet.revise_tablet_meta_fail", {
auto ptablet_id = dp->param("tablet_id", 0);
if (tablet_id() == ptablet_id) {
LOG(INFO) << "injected revies_tablet_meta failure for tabelt: " << ptablet_id;
calc_bm_status = Status::InternalError("fault injection error");
}
});
// error handling
if (!calc_bm_status.ok()) {
if (is_incremental_clone) {
delete_rowsets(to_add, false);
LOG(WARNING) << "incremental clone on tablet: " << tablet_id() << " failed due to "
<< calc_bm_status.msg() << ", revert " << to_add.size()
<< " rowsets added before.";
} else {
LOG(WARNING) << "full clone on tablet: " << tablet_id() << " failed due to "
<< calc_bm_status.msg() << ", will not update tablet meta.";
}
return calc_bm_status;
}
// full clone, calculate delete bitmap succeeded, update rowset
if (!is_incremental_clone) {
delete_rowsets(to_delete, false);
add_rowsets(to_add);
// reconstruct from tablet meta
_timestamped_version_tracker.construct_versioned_tracker(_tablet_meta->all_rs_metas());
// check the rowsets used for delete bitmap calculation is equal to the rowsets
// that we can capture by version
if (keys_type() == UNIQUE_KEYS && enable_unique_key_merge_on_write()) {
Version full_version = Version(0, max_version_unlocked());
std::vector<RowsetSharedPtr> expected_rowsets;
auto st = capture_consistent_rowsets_unlocked(full_version, &expected_rowsets);
DCHECK(st.ok()) << st;
DCHECK_EQ(base_rowsets_for_full_clone.size(), expected_rowsets.size());
if (st.ok() && base_rowsets_for_full_clone.size() != expected_rowsets.size())
[[unlikely]] {
LOG(WARNING) << "full clone succeeded, but the count("
<< base_rowsets_for_full_clone.size()
<< ") of base rowsets used for delete bitmap calculation is not match "
"expect count("
<< expected_rowsets.size() << ") we capture from tablet meta";
}
}
}
// clear stale rowset
for (auto& [v, rs] : _stale_rs_version_map) {
_engine.add_unused_rowset(rs);
}
_stale_rs_version_map.clear();
_tablet_meta->clear_stale_rowset();
save_meta();
LOG(INFO) << "finish to revise tablet. tablet_id=" << tablet_id();
return Status::OK();
}
Status Tablet::add_rowset(RowsetSharedPtr rowset) {
DCHECK(rowset != nullptr);
std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
// If the rowset already exist, just return directly. The rowset_id is an unique-id,
// we can use it to check this situation.
if (_contains_rowset(rowset->rowset_id())) {
return Status::OK();
}
// Otherwise, the version should be not contained in any existing rowset.
RETURN_IF_ERROR(_contains_version(rowset->version()));
RETURN_IF_ERROR(_tablet_meta->add_rs_meta(rowset->rowset_meta()));
_rs_version_map[rowset->version()] = rowset;
_timestamped_version_tracker.add_version(rowset->version());
std::vector<RowsetSharedPtr> rowsets_to_delete;
// yiguolei: temp code, should remove the rowset contains by this rowset
// but it should be removed in multi path version
for (auto& it : _rs_version_map) {
if (rowset->version().contains(it.first) && rowset->version() != it.first) {
CHECK(it.second != nullptr)
<< "there exist a version=" << it.first
<< " contains the input rs with version=" << rowset->version()
<< ", but the related rs is null";
rowsets_to_delete.push_back(it.second);
}
}
std::vector<RowsetSharedPtr> empty_vec;
RETURN_IF_ERROR(modify_rowsets(empty_vec, rowsets_to_delete));
++_newly_created_rowset_num;
return Status::OK();
}
Status Tablet::modify_rowsets(std::vector<RowsetSharedPtr>& to_add,
std::vector<RowsetSharedPtr>& to_delete, bool check_delete) {
// the compaction process allow to compact the single version, eg: version[4-4].
// this kind of "single version compaction" has same "input version" and "output version".
// which means "to_add->version()" equals to "to_delete->version()".
// So we should delete the "to_delete" before adding the "to_add",
// otherwise, the "to_add" will be deleted from _rs_version_map, eventually.
//
// And if the version of "to_add" and "to_delete" are exactly same. eg:
// to_add: [7-7]
// to_delete: [7-7]
// In this case, we no longer need to add the rowset in "to_delete" to
// _stale_rs_version_map, but can delete it directly.
if (to_add.empty() && to_delete.empty()) {
return Status::OK();
}
if (check_delete) {
for (auto&& rs : to_delete) {
if (auto it = _rs_version_map.find(rs->version()); it == _rs_version_map.end()) {
return Status::Error<DELETE_VERSION_ERROR>(
"try to delete not exist version {} from {}", rs->version().to_string(),
tablet_id());
} else if (rs->rowset_id() != it->second->rowset_id()) {
return Status::Error<DELETE_VERSION_ERROR>(
"try to delete version {} from {}, but rowset id changed, delete rowset id "
"is {}, exists rowsetid is {}",
rs->version().to_string(), tablet_id(), rs->rowset_id().to_string(),
it->second->rowset_id().to_string());
}
}
}
bool same_version = true;
std::sort(to_add.begin(), to_add.end(), Rowset::comparator);
std::sort(to_delete.begin(), to_delete.end(), Rowset::comparator);
if (to_add.size() == to_delete.size()) {
for (int i = 0; i < to_add.size(); ++i) {
if (to_add[i]->version() != to_delete[i]->version()) {
same_version = false;
break;
}
}
} else {
same_version = false;
}
std::vector<RowsetMetaSharedPtr> rs_metas_to_delete;
for (auto& rs : to_delete) {
rs_metas_to_delete.push_back(rs->rowset_meta());
_rs_version_map.erase(rs->version());
if (!same_version) {
// put compaction rowsets in _stale_rs_version_map.
_stale_rs_version_map[rs->version()] = rs;
}
}
std::vector<RowsetMetaSharedPtr> rs_metas_to_add;
for (auto& rs : to_add) {
rs_metas_to_add.push_back(rs->rowset_meta());
_rs_version_map[rs->version()] = rs;
if (!same_version) {
// If version are same, then _timestamped_version_tracker
// already has this version, no need to add again.
_timestamped_version_tracker.add_version(rs->version());
}
++_newly_created_rowset_num;
}
_tablet_meta->modify_rs_metas(rs_metas_to_add, rs_metas_to_delete, same_version);
if (!same_version) {
// add rs_metas_to_delete to tracker
_timestamped_version_tracker.add_stale_path_version(rs_metas_to_delete);
} else {
// delete rowset in "to_delete" directly
for (auto& rs : to_delete) {
LOG(INFO) << "add unused rowset " << rs->rowset_id() << " because of same version";
if (rs->is_local()) {
_engine.add_unused_rowset(rs);
}
}
}
return Status::OK();
}
void Tablet::add_rowsets(const std::vector<RowsetSharedPtr>& to_add) {
if (to_add.empty()) {
return;
}
std::vector<RowsetMetaSharedPtr> rs_metas;
rs_metas.reserve(to_add.size());
for (auto& rs : to_add) {
_rs_version_map.emplace(rs->version(), rs);
_timestamped_version_tracker.add_version(rs->version());
rs_metas.push_back(rs->rowset_meta());
}
_tablet_meta->modify_rs_metas(rs_metas, {});
}
void Tablet::delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, bool move_to_stale) {
if (to_delete.empty()) {
return;
}
std::vector<RowsetMetaSharedPtr> rs_metas;
rs_metas.reserve(to_delete.size());
for (auto& rs : to_delete) {
rs_metas.push_back(rs->rowset_meta());
_rs_version_map.erase(rs->version());
}
_tablet_meta->modify_rs_metas({}, rs_metas, !move_to_stale);
if (move_to_stale) {
for (auto& rs : to_delete) {
_stale_rs_version_map[rs->version()] = rs;
}
_timestamped_version_tracker.add_stale_path_version(rs_metas);
} else {
for (auto& rs : to_delete) {
_timestamped_version_tracker.delete_version(rs->version());
if (rs->is_local()) {
_engine.add_unused_rowset(rs);
}
}
}
}
RowsetSharedPtr Tablet::_rowset_with_largest_size() {
RowsetSharedPtr largest_rowset = nullptr;
for (auto& it : _rs_version_map) {
if (it.second->empty() || it.second->zero_num_rows()) {
continue;
}
if (largest_rowset == nullptr || it.second->rowset_meta()->index_disk_size() >
largest_rowset->rowset_meta()->index_disk_size()) {
largest_rowset = it.second;
}
}
return largest_rowset;
}
// add inc rowset should not persist tablet meta, because it will be persisted when publish txn.
Status Tablet::add_inc_rowset(const RowsetSharedPtr& rowset) {
DCHECK(rowset != nullptr);
std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
if (_contains_rowset(rowset->rowset_id())) {
return Status::OK();
}
RETURN_IF_ERROR(_contains_version(rowset->version()));
RETURN_IF_ERROR(_tablet_meta->add_rs_meta(rowset->rowset_meta()));
_rs_version_map[rowset->version()] = rowset;
_timestamped_version_tracker.add_version(rowset->version());
++_newly_created_rowset_num;
return Status::OK();
}
void Tablet::_delete_stale_rowset_by_version(const Version& version) {
RowsetMetaSharedPtr rowset_meta = _tablet_meta->acquire_stale_rs_meta_by_version(version);
if (rowset_meta == nullptr) {
return;
}
_tablet_meta->delete_stale_rs_meta_by_version(version);
VLOG_NOTICE << "delete stale rowset. tablet=" << tablet_id() << ", version=" << version;
}
void Tablet::delete_expired_stale_rowset() {
int64_t now = UnixSeconds();
// hold write lock while processing stable rowset
{
std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
// Compute the end time to delete rowsets, when a expired rowset createtime less then this time, it will be deleted.
double expired_stale_sweep_endtime =
::difftime(now, config::tablet_rowset_stale_sweep_time_sec);
if (config::tablet_rowset_stale_sweep_by_size) {
expired_stale_sweep_endtime = now;
}
std::vector<int64_t> path_id_vec;
// capture the path version to delete
_timestamped_version_tracker.capture_expired_paths(
static_cast<int64_t>(expired_stale_sweep_endtime), &path_id_vec);
if (path_id_vec.empty()) {
return;
}
const RowsetSharedPtr lastest_delta = get_rowset_with_max_version();
if (lastest_delta == nullptr) {
LOG(WARNING) << "lastest_delta is null " << tablet_id();
return;
}
// fetch missing version before delete
Versions missed_versions = get_missed_versions_unlocked(lastest_delta->end_version());
if (!missed_versions.empty()) {
LOG(WARNING) << "tablet:" << tablet_id()
<< ", missed version for version:" << lastest_delta->end_version();
_print_missed_versions(missed_versions);
return;
}
// do check consistent operation
auto path_id_iter = path_id_vec.begin();
std::map<int64_t, PathVersionListSharedPtr> stale_version_path_map;
while (path_id_iter != path_id_vec.end()) {
PathVersionListSharedPtr version_path =
_timestamped_version_tracker.fetch_and_delete_path_by_id(*path_id_iter);
Version test_version = Version(0, lastest_delta->end_version());
stale_version_path_map[*path_id_iter] = version_path;
Status status =
capture_consistent_versions_unlocked(test_version, nullptr, false, false);
// 1. When there is no consistent versions, we must reconstruct the tracker.
if (!status.ok()) {
// 2. fetch missing version after delete
Versions after_missed_versions =
get_missed_versions_unlocked(lastest_delta->end_version());
// 2.1 check whether missed_versions and after_missed_versions are the same.
// when they are the same, it means we can delete the path securely.
bool is_missing = missed_versions.size() != after_missed_versions.size();
if (!is_missing) {
for (int ver_index = 0; ver_index < missed_versions.size(); ver_index++) {
if (missed_versions[ver_index] != after_missed_versions[ver_index]) {
is_missing = true;
break;
}
}
}
if (is_missing) {
LOG(WARNING) << "The consistent version check fails, there are bugs. "
<< "Reconstruct the tracker to recover versions in tablet="
<< tablet_id();
// 3. try to recover
_timestamped_version_tracker.recover_versioned_tracker(stale_version_path_map);
// 4. double check the consistent versions
// fetch missing version after recover
Versions recover_missed_versions =
get_missed_versions_unlocked(lastest_delta->end_version());
// 4.1 check whether missed_versions and recover_missed_versions are the same.
// when they are the same, it means we recover successfully.
bool is_recover_missing =
missed_versions.size() != recover_missed_versions.size();
if (!is_recover_missing) {
for (int ver_index = 0; ver_index < missed_versions.size(); ver_index++) {
if (missed_versions[ver_index] != recover_missed_versions[ver_index]) {
is_recover_missing = true;
break;
}
}
}
// 5. check recover fail, version is mission
if (is_recover_missing) {
if (!config::ignore_rowset_stale_unconsistent_delete) {
LOG(FATAL)
<< "rowset stale unconsistent delete. tablet= " << tablet_id();
} else {
LOG(WARNING)
<< "rowset stale unconsistent delete. tablet= " << tablet_id();
}
}
}
return;
}
path_id_iter++;
}
auto old_size = _stale_rs_version_map.size();
auto old_meta_size = _tablet_meta->all_stale_rs_metas().size();
// do delete operation
auto to_delete_iter = stale_version_path_map.begin();
while (to_delete_iter != stale_version_path_map.end()) {
std::vector<TimestampedVersionSharedPtr>& to_delete_version =
to_delete_iter->second->timestamped_versions();
for (auto& timestampedVersion : to_delete_version) {
auto it = _stale_rs_version_map.find(timestampedVersion->version());
if (it != _stale_rs_version_map.end()) {
it->second->clear_cache();
// delete rowset
if (it->second->is_local()) {
_engine.add_unused_rowset(it->second);
}
_stale_rs_version_map.erase(it);
VLOG_NOTICE << "delete stale rowset tablet=" << tablet_id() << " version["
<< timestampedVersion->version().first << ","
<< timestampedVersion->version().second
<< "] move to unused_rowset success " << std::fixed
<< expired_stale_sweep_endtime;
} else {
LOG(WARNING) << "delete stale rowset tablet=" << tablet_id() << " version["
<< timestampedVersion->version().first << ","
<< timestampedVersion->version().second
<< "] not find in stale rs version map";
}
_delete_stale_rowset_by_version(timestampedVersion->version());
}
to_delete_iter++;
}
bool reconstructed = _reconstruct_version_tracker_if_necessary();
VLOG_NOTICE << "delete stale rowset _stale_rs_version_map tablet=" << tablet_id()
<< " current_size=" << _stale_rs_version_map.size() << " old_size=" << old_size
<< " current_meta_size=" << _tablet_meta->all_stale_rs_metas().size()
<< " old_meta_size=" << old_meta_size << " sweep endtime " << std::fixed
<< expired_stale_sweep_endtime << ", reconstructed=" << reconstructed;
}
#ifndef BE_TEST
{
std::shared_lock<std::shared_mutex> rlock(_meta_lock);
save_meta();
}
#endif
}
Status Tablet::capture_consistent_versions_unlocked(const Version& spec_version,
Versions* version_path,
bool skip_missing_version, bool quiet) const {
Status status =
_timestamped_version_tracker.capture_consistent_versions(spec_version, version_path);
if (!status.ok() && !quiet) {
Versions missed_versions = get_missed_versions_unlocked(spec_version.second);
if (missed_versions.empty()) {
// if version_path is null, it may be a compaction check logic.
// so to avoid print too many logs.
if (version_path != nullptr) {
LOG(WARNING) << "tablet:" << tablet_id()
<< ", version already has been merged. spec_version: " << spec_version
<< ", max_version: " << max_version_unlocked();
}
status = Status::Error<VERSION_ALREADY_MERGED>(
"missed_versions is empty, spec_version "
"{}, max_version {}, tablet_id {}",
spec_version.second, max_version_unlocked(), tablet_id());
} else {
if (version_path != nullptr) {
LOG(WARNING) << "status:" << status << ", tablet:" << tablet_id()
<< ", missed version for version:" << spec_version;
_print_missed_versions(missed_versions);
if (skip_missing_version) {
LOG(WARNING) << "force skipping missing version for tablet:" << tablet_id();
return Status::OK();
}
}
}
}
DBUG_EXECUTE_IF("TTablet::capture_consistent_versions.inject_failure", {
auto tablet_id = dp->param<int64>("tablet_id", -1);
if (tablet_id != -1 && tablet_id == _tablet_meta->tablet_id()) {
status = Status::Error<VERSION_ALREADY_MERGED>("version already merged");
}
});
return status;
}
Status Tablet::check_version_integrity(const Version& version, bool quiet) {
std::shared_lock rdlock(_meta_lock);
return capture_consistent_versions_unlocked(version, nullptr, false, quiet);
}
bool Tablet::exceed_version_limit(int32_t limit) {
if (_tablet_meta->version_count() > limit) {
exceed_version_limit_counter << 1;
return true;
}
return false;
}
// If any rowset contains the specific version, it means the version already exist
bool Tablet::check_version_exist(const Version& version) const {
std::shared_lock rdlock(_meta_lock);
for (auto& it : _rs_version_map) {
if (it.first.contains(version)) {
return true;
}
}
return false;
}
// The meta read lock should be held before calling
void Tablet::acquire_version_and_rowsets(
std::vector<std::pair<Version, RowsetSharedPtr>>* version_rowsets) const {
for (const auto& it : _rs_version_map) {
version_rowsets->emplace_back(it.first, it.second);
}
}
Status Tablet::capture_consistent_rowsets_unlocked(const Version& spec_version,
std::vector<RowsetSharedPtr>* rowsets) const {
std::vector<Version> version_path;
RETURN_IF_ERROR(
capture_consistent_versions_unlocked(spec_version, &version_path, false, false));
RETURN_IF_ERROR(_capture_consistent_rowsets_unlocked(version_path, rowsets));
return Status::OK();
}
Status Tablet::capture_rs_readers(const Version& spec_version, std::vector<RowSetSplits>* rs_splits,
bool skip_missing_version) {
std::shared_lock rlock(_meta_lock);
std::vector<Version> version_path;
RETURN_IF_ERROR(capture_consistent_versions_unlocked(spec_version, &version_path,
skip_missing_version, false));
RETURN_IF_ERROR(capture_rs_readers_unlocked(version_path, rs_splits));
return Status::OK();
}
Versions Tablet::calc_missed_versions(int64_t spec_version, Versions existing_versions) const {
DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version;
// sort the existing versions in ascending order
std::sort(existing_versions.begin(), existing_versions.end(),
[](const Version& a, const Version& b) {
// simple because 2 versions are certainly not overlapping
return a.first < b.first;
});
// From the first version(=0), find the missing version until spec_version
int64_t last_version = -1;
Versions missed_versions;
for (const Version& version : existing_versions) {
if (version.first > last_version + 1) {
for (int64_t i = last_version + 1; i < version.first && i <= spec_version; ++i) {
// Don't merge missed_versions because clone & snapshot use single version.
// For example, if miss 4 ~ 6, clone need [4, 4], [5, 5], [6, 6], but not [4, 6].
missed_versions.emplace_back(i, i);
}
}
last_version = version.second;
if (last_version >= spec_version) {
break;
}
}
for (int64_t i = last_version + 1; i <= spec_version; ++i) {
missed_versions.emplace_back(i, i);
}
return missed_versions;
}
bool Tablet::can_do_compaction(size_t path_hash, CompactionType compaction_type) {
if (compaction_type == CompactionType::BASE_COMPACTION && tablet_state() != TABLET_RUNNING) {
// base compaction can only be done for tablet in TABLET_RUNNING state.
// but cumulative compaction can be done for TABLET_NOTREADY, such as tablet under alter process.
return false;
}
if (data_dir()->path_hash() != path_hash || !is_used() || !init_succeeded()) {
return false;
}
// In TABLET_NOTREADY, we keep last 10 versions in new tablet so base tablet max_version
// not merged in new tablet and then we can do compaction
return tablet_state() == TABLET_RUNNING || tablet_state() == TABLET_NOTREADY;
}
uint32_t Tablet::calc_compaction_score(
CompactionType compaction_type,
std::shared_ptr<CumulativeCompactionPolicy> cumulative_compaction_policy) {
// Need meta lock, because it will iterator "all_rs_metas" of tablet meta.
std::shared_lock rdlock(_meta_lock);
if (compaction_type == CompactionType::CUMULATIVE_COMPACTION) {
return _calc_cumulative_compaction_score(cumulative_compaction_policy);
} else {
DCHECK_EQ(compaction_type, CompactionType::BASE_COMPACTION);
return _calc_base_compaction_score();
}
}
uint32_t Tablet::calc_cold_data_compaction_score() const {
uint32_t score = 0;
std::vector<RowsetMetaSharedPtr> cooldowned_rowsets;