-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ts_tablet_manager.cc
2700 lines (2374 loc) · 109 KB
/
ts_tablet_manager.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.
//
// The following only applies to changes made to this file as part of YugaByte development.
//
// Portions Copyright (c) YugaByte, Inc.
//
// Licensed 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 "yb/tserver/ts_tablet_manager.h"
#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <boost/container/static_vector.hpp>
#include <boost/optional/optional.hpp>
#include <glog/logging.h>
#include "yb/client/client.h"
#include "yb/client/transaction_manager.h"
#include "yb/common/wire_protocol.h"
#include "yb/consensus/consensus.h"
#include "yb/consensus/multi_raft_batcher.h"
#include "yb/consensus/consensus_meta.h"
#include "yb/consensus/log.h"
#include "yb/consensus/log_anchor_registry.h"
#include "yb/consensus/metadata.pb.h"
#include "yb/consensus/opid_util.h"
#include "yb/consensus/quorum_util.h"
#include "yb/consensus/raft_consensus.h"
#include "yb/consensus/retryable_requests.h"
#include "yb/consensus/state_change_context.h"
#include "yb/docdb/docdb_rocksdb_util.h"
#include "yb/fs/fs_manager.h"
#include "yb/gutil/bind.h"
#include "yb/gutil/stl_util.h"
#include "yb/gutil/strings/substitute.h"
#include "yb/gutil/sysinfo.h"
#include "yb/master/master_heartbeat.pb.h"
#include "yb/master/sys_catalog.h"
#include "yb/rocksdb/util/task_metrics.h"
#include "yb/rpc/messenger.h"
#include "yb/rpc/poller.h"
#include "yb/tablet/metadata.pb.h"
#include "yb/tablet/operations/split_operation.h"
#include "yb/tablet/tablet.h"
#include "yb/tablet/tablet.pb.h"
#include "yb/tablet/tablet_bootstrap_if.h"
#include "yb/tablet/tablet_metadata.h"
#include "yb/tablet/tablet_options.h"
#include "yb/tablet/tablet_peer.h"
#include "yb/tserver/heartbeater.h"
#include "yb/tserver/remote_bootstrap_client.h"
#include "yb/tserver/remote_bootstrap_session.h"
#include "yb/tserver/tablet_server.h"
#include "yb/tserver/tserver.pb.h"
#include "yb/util/debug/long_operation_tracker.h"
#include "yb/util/debug/trace_event.h"
#include "yb/util/debug-util.h"
#include "yb/util/env.h"
#include "yb/util/fault_injection.h"
#include "yb/util/flag_tags.h"
#include "yb/util/format.h"
#include "yb/util/logging.h"
#include "yb/util/mem_tracker.h"
#include "yb/util/metrics.h"
#include "yb/util/pb_util.h"
#include "yb/util/scope_exit.h"
#include "yb/util/shared_lock.h"
#include "yb/util/status_format.h"
#include "yb/util/status_log.h"
#include "yb/util/stopwatch.h"
#include "yb/util/trace.h"
using namespace std::literals;
using namespace std::placeholders;
DEFINE_int32(num_tablets_to_open_simultaneously, 0,
"Number of threads available to open tablets during startup. If this "
"is set to 0 (the default), then the number of bootstrap threads will "
"be set based on the number of data directories. If the data directories "
"are on some very fast storage device such as SSD or a RAID array, it "
"may make sense to manually tune this.");
TAG_FLAG(num_tablets_to_open_simultaneously, advanced);
DEFINE_int32(tablet_start_warn_threshold_ms, 500,
"If a tablet takes more than this number of millis to start, issue "
"a warning with a trace.");
TAG_FLAG(tablet_start_warn_threshold_ms, hidden);
DEFINE_int32(cleanup_split_tablets_interval_sec, 60,
"Interval at which tablet manager tries to cleanup split tablets which are no longer "
"needed. Setting this to 0 disables cleanup of split tablets.");
DEFINE_test_flag(double, fault_crash_after_blocks_deleted, 0.0,
"Fraction of the time when the tablet will crash immediately "
"after deleting the data blocks during tablet deletion.");
DEFINE_test_flag(double, fault_crash_after_wal_deleted, 0.0,
"Fraction of the time when the tablet will crash immediately "
"after deleting the WAL segments during tablet deletion.");
DEFINE_test_flag(double, fault_crash_after_cmeta_deleted, 0.0,
"Fraction of the time when the tablet will crash immediately "
"after deleting the consensus metadata during tablet deletion.");
DEFINE_test_flag(double, fault_crash_after_rb_files_fetched, 0.0,
"Fraction of the time when the tablet will crash immediately "
"after fetching the files during a remote bootstrap but before "
"marking the superblock as TABLET_DATA_READY.");
DEFINE_test_flag(double, fault_crash_in_split_after_log_copied, 0.0,
"Fraction of the time when the tablet will crash immediately after initiating a "
"Log::CopyTo from parent to child tablet, but before marking the child tablet as "
"TABLET_DATA_READY.");
DEFINE_test_flag(bool, simulate_already_present_in_remote_bootstrap, false,
"If true, return an AlreadyPresent error in remote bootstrap after starting the "
"remote bootstrap client.");
DEFINE_test_flag(double, fault_crash_in_split_before_log_flushed, 0.0,
"Fraction of the time when the tablet will crash immediately before flushing a "
"parent tablet's kSplit operation.");
DEFINE_test_flag(uint64, crash_if_remote_bootstrap_sessions_greater_than, 0,
"If greater than zero, this process will crash if we detect more than the "
"specified number of remote bootstrap sessions.");
DEFINE_test_flag(uint64, crash_if_remote_bootstrap_sessions_per_table_greater_than, 0,
"If greater than zero, this process will crash if for any table we exceed the "
"specified number of remote bootstrap sessions");
DEFINE_test_flag(bool, crash_after_tablet_split_completed, false,
"Crash inside TSTabletManager::ApplyTabletSplit after tablet "
"being marked as TABLET_DATA_SPLIT_COMPLETED.");
DEFINE_test_flag(bool, crash_before_apply_tablet_split_op, false,
"Crash inside TSTabletManager::ApplyTabletSplit before doing anything.");
DEFINE_test_flag(bool, crash_before_source_tablet_mark_split_done, false,
"Crash inside TSTabletManager::ApplyTabletSplit before tablet "
"being marked TABLET_DATA_SPLIT_COMPLETED.");
DEFINE_test_flag(bool, force_single_tablet_failure, false,
"Force exactly one tablet to a failed state.");
DEFINE_test_flag(int32, apply_tablet_split_inject_delay_ms, 0,
"Inject delay into TSTabletManager::ApplyTabletSplit.");
DEFINE_test_flag(bool, pause_apply_tablet_split, false,
"Pause TSTabletManager::ApplyTabletSplit.");
DEFINE_test_flag(bool, skip_deleting_split_tablets, false,
"Skip deleting tablets which have been split.");
DEFINE_test_flag(bool, skip_post_split_compaction, false,
"Skip processing post split compaction.");
DEFINE_int32(verify_tablet_data_interval_sec, 0,
"The tick interval time for the tablet data integrity verification background task. "
"This defaults to 0, which means disable the background task.");
DEFINE_int32(cleanup_metrics_interval_sec, 60,
"The tick interval time for the metrics cleanup background task. "
"If set to 0, it disables the background task.");
DEFINE_int32(send_wait_for_report_interval_ms, 60000,
"The tick interval time to trigger updating all transaction coordinators with wait-for"
" relationships.");
DEFINE_bool(skip_tablet_data_verification, false,
"Skip checking tablet data for corruption.");
DEFINE_int32(read_pool_max_threads, 128,
"The maximum number of threads allowed for read_pool_. This pool is used "
"to run multiple read operations, that are part of the same tablet rpc, "
"in parallel.");
DEFINE_int32(read_pool_max_queue_size, 128,
"The maximum number of tasks that can be held in the queue for read_pool_. This pool "
"is used to run multiple read operations, that are part of the same tablet rpc, "
"in parallel.");
DEFINE_int32(post_split_trigger_compaction_pool_max_threads, 1,
"The maximum number of threads allowed for post_split_trigger_compaction_pool_. This "
"pool is used to run compactions on tablets after they have been split and still "
"contain irrelevant data from the tablet they were sourced from.");
DEFINE_int32(post_split_trigger_compaction_pool_max_queue_size, 16,
"The maximum number of tasks that can be held in the pool for "
"post_split_trigger_compaction_pool_. This pool is used to run compactions on tablets "
"after they have been split and still contain irrelevant data from the tablet they "
"were sourced from.");
DEFINE_test_flag(int32, sleep_after_tombstoning_tablet_secs, 0,
"Whether we sleep in LogAndTombstone after calling DeleteTabletData.");
DEFINE_bool(enable_restart_transaction_status_tablets_first, true,
"Set to true to prioritize bootstrapping transaction status tablets first.");
DEFINE_bool(enable_wait_queue_based_pessimistic_locking, false,
"If true, use pessimistic locking behavior in conflict resolution.");
TAG_FLAG(enable_wait_queue_based_pessimistic_locking, evolving);
TAG_FLAG(enable_wait_queue_based_pessimistic_locking, hidden);
DECLARE_bool(auto_promote_nonlocal_transactions_to_global);
DECLARE_string(rocksdb_compact_flush_rate_limit_sharing_mode);
namespace yb {
namespace tserver {
METRIC_DEFINE_coarse_histogram(server, op_apply_queue_length, "Operation Apply Queue Length",
MetricUnit::kTasks,
"Number of operations waiting to be applied to the tablet. "
"High queue lengths indicate that the server is unable to process "
"operations as fast as they are being written to the WAL.");
METRIC_DEFINE_coarse_histogram(server, op_apply_queue_time, "Operation Apply Queue Time",
MetricUnit::kMicroseconds,
"Time that operations spent waiting in the apply queue before being "
"processed. High queue times indicate that the server is unable to "
"process operations as fast as they are being written to the WAL.");
METRIC_DEFINE_coarse_histogram(server, op_apply_run_time, "Operation Apply Run Time",
MetricUnit::kMicroseconds,
"Time that operations spent being applied to the tablet. "
"High values may indicate that the server is under-provisioned or "
"that operations consist of very large batches.");
METRIC_DEFINE_coarse_histogram(server, op_read_queue_length, "Operation Read op Queue Length",
MetricUnit::kTasks,
"Number of operations waiting to be applied to the tablet. "
"High queue lengths indicate that the server is unable to process "
"operations as fast as they are being written to the WAL.");
METRIC_DEFINE_coarse_histogram(server, op_read_queue_time, "Operation Read op Queue Time",
MetricUnit::kMicroseconds,
"Time that operations spent waiting in the read queue before being "
"processed. High queue times indicate that the server is unable to "
"process operations as fast as they are being written to the WAL.");
METRIC_DEFINE_coarse_histogram(server, op_read_run_time, "Operation Read op Run Time",
MetricUnit::kMicroseconds,
"Time that operations spent being applied to the tablet. "
"High values may indicate that the server is under-provisioned or "
"that operations consist of very large batches.");
METRIC_DEFINE_coarse_histogram(server, ts_bootstrap_time, "TServer Bootstrap Time",
MetricUnit::kMicroseconds,
"Time that the tablet server takes to bootstrap all of its tablets.");
METRIC_DEFINE_gauge_uint64(server, ts_split_op_apply, "Split Apply",
MetricUnit::kOperations,
"Number of split operations successfully applied in Raft.");
METRIC_DEFINE_gauge_uint64(server, ts_split_compaction_added, "Post-Split Compaction Submitted",
MetricUnit::kRequests,
"Number of post-split compaction requests submitted.");
THREAD_POOL_METRICS_DEFINE(
server, post_split_trigger_compaction_pool, "Thread pool for tablet compaction jobs.");
THREAD_POOL_METRICS_DEFINE(
server, admin_triggered_compaction_pool, "Thread pool for tablet compaction jobs.");
ROCKSDB_PRIORITY_THREAD_POOL_METRICS_DEFINE(server);
using consensus::ConsensusMetadata;
using consensus::RaftConfigPB;
using consensus::StartRemoteBootstrapRequestPB;
using log::Log;
using master::ReportedTabletPB;
using master::TabletReportPB;
using master::TabletReportUpdatesPB;
using std::shared_ptr;
using std::string;
using std::unordered_set;
using std::vector;
using strings::Substitute;
using tablet::BOOTSTRAPPING;
using tablet::NOT_STARTED;
using tablet::RaftGroupMetadata;
using tablet::RaftGroupMetadataPtr;
using tablet::RaftGroupStatePB;
using tablet::RUNNING;
using tablet::TABLET_DATA_COPYING;
using tablet::TABLET_DATA_DELETED;
using tablet::TABLET_DATA_INIT_STARTED;
using tablet::TABLET_DATA_READY;
using tablet::TABLET_DATA_SPLIT_COMPLETED;
using tablet::TABLET_DATA_TOMBSTONED;
using tablet::TabletDataState;
using tablet::TabletPeer;
using tablet::TabletPeerPtr;
constexpr int32_t kDefaultTserverBlockCacheSizePercentage = 50;
void TSTabletManager::VerifyTabletData() {
LOG_WITH_PREFIX(INFO) << "Beginning tablet data verification checks";
for (const TabletPeerPtr& peer : GetTabletPeers()) {
if (peer->state() == RUNNING) {
if (PREDICT_FALSE(FLAGS_skip_tablet_data_verification)) {
LOG_WITH_PREFIX(INFO)
<< Format("Skipped tablet data verification check on $0", peer->tablet_id());
} else {
Status s = peer->tablet()->VerifyDataIntegrity();
if (!s.ok()) {
LOG(WARNING) << "Tablet data integrity verification failed on " << peer->tablet_id()
<< ": " << s;
}
}
}
}
}
void TSTabletManager::CleanupOldMetrics() {
VLOG(2) << "Cleaning up old metrics";
metric_registry_->RetireOldMetrics();
}
void TSTabletManager::PollWaitingTxnRegistry() {
DCHECK_NOTNULL(waiting_txn_registry_)->SendWaitForGraph();
}
TSTabletManager::TSTabletManager(FsManager* fs_manager,
TabletServer* server,
MetricRegistry* metric_registry)
: fs_manager_(fs_manager),
server_(server),
metric_registry_(metric_registry),
state_(MANAGER_INITIALIZING) {
ThreadPoolMetrics metrics = {
METRIC_op_apply_queue_length.Instantiate(server_->metric_entity()),
METRIC_op_apply_queue_time.Instantiate(server_->metric_entity()),
METRIC_op_apply_run_time.Instantiate(server_->metric_entity())
};
CHECK_OK(ThreadPoolBuilder("apply")
.set_metrics(std::move(metrics))
.Build(&apply_pool_));
// This pool is shared by all replicas hosted by this server.
//
// Some submitted tasks use blocking IO, so we configure no upper bound on
// the maximum number of threads in each pool (otherwise the default value of
// "number of CPUs" may cause blocking tasks to starve other "fast" tasks).
// However, the effective upper bound is the number of replicas as each will
// submit its own tasks via a dedicated token.
CHECK_OK(ThreadPoolBuilder("consensus")
.set_min_threads(1)
.unlimited_threads()
.Build(&raft_pool_));
CHECK_OK(ThreadPoolBuilder("log-sync")
.set_min_threads(1)
.unlimited_threads()
.Build(&log_sync_pool_));
CHECK_OK(ThreadPoolBuilder("prepare")
.set_min_threads(1)
.unlimited_threads()
.Build(&tablet_prepare_pool_));
CHECK_OK(ThreadPoolBuilder("append")
.set_min_threads(1)
.unlimited_threads()
.set_idle_timeout(MonoDelta::FromMilliseconds(10000))
.Build(&append_pool_));
CHECK_OK(ThreadPoolBuilder("log-alloc")
.set_min_threads(1)
.unlimited_threads()
.Build(&allocation_pool_));
ThreadPoolMetrics read_metrics = {
METRIC_op_read_queue_length.Instantiate(server_->metric_entity()),
METRIC_op_read_queue_time.Instantiate(server_->metric_entity()),
METRIC_op_read_run_time.Instantiate(server_->metric_entity())
};
CHECK_OK(ThreadPoolBuilder("read-parallel")
.set_max_threads(FLAGS_read_pool_max_threads)
.set_max_queue_size(FLAGS_read_pool_max_queue_size)
.set_metrics(std::move(read_metrics))
.Build(&read_pool_));
CHECK_OK(ThreadPoolBuilder("tablet-split-compaction")
.set_max_threads(FLAGS_post_split_trigger_compaction_pool_max_threads)
.set_max_queue_size(FLAGS_post_split_trigger_compaction_pool_max_queue_size)
.set_metrics(THREAD_POOL_METRICS_INSTANCE(
server_->metric_entity(), post_split_trigger_compaction_pool))
.Build(&post_split_trigger_compaction_pool_));
CHECK_OK(ThreadPoolBuilder("admin-compaction")
.set_max_threads(std::max(docdb::GetGlobalRocksDBPriorityThreadPoolSize(), 0))
.set_metrics(THREAD_POOL_METRICS_INSTANCE(
server_->metric_entity(), admin_triggered_compaction_pool))
.Build(&admin_triggered_compaction_pool_));
ts_split_op_apply_ = METRIC_ts_split_op_apply.Instantiate(server_->metric_entity(), 0);
ts_split_compaction_added_ =
METRIC_ts_split_compaction_added.Instantiate(server_->metric_entity(), 0);
mem_manager_ = std::make_shared<TabletMemoryManager>(
&tablet_options_,
server_->mem_tracker(),
kDefaultTserverBlockCacheSizePercentage,
server_->metric_entity(),
[this](){ return GetTabletPeers(); });
tablet_options_.priority_thread_pool_metrics =
std::make_shared<rocksdb::RocksDBPriorityThreadPoolMetrics>(
ROCKSDB_PRIORITY_THREAD_POOL_METRICS_INSTANCE(server_->metric_entity()));
}
TSTabletManager::~TSTabletManager() {
}
Status TSTabletManager::Init() {
CHECK_EQ(state(), MANAGER_INITIALIZING);
tablet_options_.env = server_->GetEnv();
tablet_options_.rocksdb_env = server_->GetRocksDBEnv();
tablet_options_.listeners = server_->options().listeners;
if (docdb::GetRocksDBRateLimiterSharingMode() == docdb::RateLimiterSharingMode::TSERVER) {
tablet_options_.rate_limiter = docdb::CreateRocksDBRateLimiter();
}
// Start the threadpool we'll use to open tablets.
// This has to be done in Init() instead of the constructor, since the
// FsManager isn't initialized until this point.
int max_bootstrap_threads = FLAGS_num_tablets_to_open_simultaneously;
if (max_bootstrap_threads == 0) {
int num_cpus = base::NumCPUs();
if (num_cpus <= 2) {
max_bootstrap_threads = 2;
} else {
max_bootstrap_threads = min(
num_cpus - 1, narrow_cast<int>(fs_manager_->GetDataRootDirs().size()) * 8);
}
LOG_WITH_PREFIX(INFO) << "max_bootstrap_threads=" << max_bootstrap_threads;
}
ThreadPoolMetrics bootstrap_metrics = {
nullptr,
nullptr,
METRIC_ts_bootstrap_time.Instantiate(server_->metric_entity())
};
RETURN_NOT_OK(ThreadPoolBuilder("tablet-bootstrap")
.set_max_threads(max_bootstrap_threads)
.set_metrics(std::move(bootstrap_metrics))
.Build(&open_tablet_pool_));
CleanupCheckpoints();
// Search for tablets in the metadata dir.
vector<string> tablet_ids = VERIFY_RESULT(fs_manager_->ListTabletIds());
InitLocalRaftPeerPB();
multi_raft_manager_ = std::make_unique<consensus::MultiRaftManager>(server_->messenger(),
&server_->proxy_cache(),
local_peer_pb_.cloud_info());
if (FLAGS_enable_wait_queue_based_pessimistic_locking) {
if (FLAGS_auto_promote_nonlocal_transactions_to_global) {
LOG(WARNING) << "Ignoring enable_wait_queue_based_pessimistic_locking=true since "
<< "auto_promote_nonlocal_transactions_to_global is enabled. These two features "
<< "are not yet supported together.";
} else {
waiting_txn_registry_ = std::make_unique<tablet::LocalWaitingTxnRegistry>(
client_future(), scoped_refptr<server::Clock>(server_->clock()));
}
}
deque<RaftGroupMetadataPtr> metas;
// First, load all of the tablet metadata. We do this before we start
// submitting the actual OpenTablet() tasks so that we don't have to compete
// for disk resources, etc, with bootstrap processes and running tablets.
MonoTime start(MonoTime::Now());
for (const string& tablet_id : tablet_ids) {
RaftGroupMetadataPtr meta;
RETURN_NOT_OK_PREPEND(OpenTabletMeta(tablet_id, &meta),
"Failed to open tablet metadata for tablet: " + tablet_id);
if (PREDICT_FALSE(!CanServeTabletData(meta->tablet_data_state()))) {
RETURN_NOT_OK(HandleNonReadyTabletOnStartup(meta));
continue;
}
RegisterDataAndWalDir(
fs_manager_, meta->table_id(), meta->raft_group_id(), meta->data_root_dir(),
meta->wal_root_dir());
if (FLAGS_enable_restart_transaction_status_tablets_first) {
// Prioritize bootstrapping transaction status tablets first.
if (meta->table_type() == TRANSACTION_STATUS_TABLE_TYPE) {
metas.push_front(meta);
} else {
metas.push_back(meta);
}
} else {
metas.push_back(meta);
}
}
MonoDelta elapsed = MonoTime::Now().GetDeltaSince(start);
LOG(INFO) << "Loaded metadata for " << tablet_ids.size() << " tablet in "
<< elapsed.ToMilliseconds() << " ms";
// Now submit the "Open" task for each.
for (const RaftGroupMetadataPtr& meta : metas) {
scoped_refptr<TransitionInProgressDeleter> deleter;
RETURN_NOT_OK(StartTabletStateTransition(
meta->raft_group_id(), "opening tablet", &deleter));
TabletPeerPtr tablet_peer = VERIFY_RESULT(CreateAndRegisterTabletPeer(meta, NEW_PEER));
RETURN_NOT_OK(open_tablet_pool_->SubmitFunc(
std::bind(&TSTabletManager::OpenTablet, this, meta, deleter)));
}
{
std::lock_guard<RWMutex> lock(mutex_);
state_ = MANAGER_RUNNING;
}
RETURN_NOT_OK(mem_manager_->Init());
tablets_cleaner_ = std::make_unique<rpc::Poller>(
LogPrefix(), std::bind(&TSTabletManager::CleanupSplitTablets, this));
verify_tablet_data_poller_ = std::make_unique<rpc::Poller>(
LogPrefix(), std::bind(&TSTabletManager::VerifyTabletData, this));
metrics_cleaner_ = std::make_unique<rpc::Poller>(
LogPrefix(), std::bind(&TSTabletManager::CleanupOldMetrics, this));
waiting_txn_registry_poller_ = std::make_unique<rpc::Poller>(
LogPrefix(), std::bind(&TSTabletManager::PollWaitingTxnRegistry, this));
return Status::OK();
}
void TSTabletManager::CleanupCheckpoints() {
for (const auto& data_root : fs_manager_->GetDataRootDirs()) {
auto tables_dir = JoinPathSegments(data_root, FsManager::kRocksDBDirName);
auto tables = fs_manager_->env()->GetChildren(tables_dir, ExcludeDots::kTrue);
if (!tables.ok()) {
LOG_WITH_PREFIX(WARNING)
<< "Failed to get tables in " << tables_dir << ": " << tables.status();
continue;
}
for (const auto& table : *tables) {
auto table_dir = JoinPathSegments(tables_dir, table);
auto tablets = fs_manager_->env()->GetChildren(table_dir, ExcludeDots::kTrue);
if (!tablets.ok()) {
LOG_WITH_PREFIX(WARNING)
<< "Failed to get tablets in " << table_dir << ": " << tablets.status();
continue;
}
for (const auto& tablet : *tablets) {
auto checkpoints_dir = JoinPathSegments(
table_dir, tablet, RemoteBootstrapSession::kCheckpointsDir);
if (fs_manager_->env()->FileExists(checkpoints_dir)) {
LOG_WITH_PREFIX(INFO) << "Cleaning up checkpoints dir: " << yb::ToString(checkpoints_dir);
auto status = fs_manager_->env()->DeleteRecursively(checkpoints_dir);
WARN_NOT_OK(status, Format("Cleanup of checkpoints dir $0 failed", checkpoints_dir));
}
}
}
}
}
Status TSTabletManager::Start() {
if (FLAGS_cleanup_split_tablets_interval_sec > 0) {
tablets_cleaner_->Start(
&server_->messenger()->scheduler(), FLAGS_cleanup_split_tablets_interval_sec * 1s);
LOG(INFO) << "Split tablets cleanup monitor started...";
} else {
LOG(INFO)
<< "Split tablets cleanup is disabled by cleanup_split_tablets_interval_sec flag set to 0";
}
if (FLAGS_verify_tablet_data_interval_sec > 0) {
verify_tablet_data_poller_->Start(
&server_->messenger()->scheduler(), FLAGS_verify_tablet_data_interval_sec * 1s);
LOG(INFO) << "Tablet data verification task started...";
} else {
LOG(INFO)
<< "Tablet data verification is disabled by verify_tablet_data_interval_sec flag set to 0";
}
if (FLAGS_cleanup_metrics_interval_sec > 0) {
metrics_cleaner_->Start(
&server_->messenger()->scheduler(), FLAGS_cleanup_metrics_interval_sec * 1s);
LOG(INFO) << "Old metrics cleanup task started...";
} else {
LOG(INFO)
<< "Old metrics cleanup is disabled by cleanup_metrics_interval_sec flag set to 0";
}
if (waiting_txn_registry_) {
waiting_txn_registry_poller_->Start(
&server_->messenger()->scheduler(), FLAGS_send_wait_for_report_interval_ms * 1ms);
}
return Status::OK();
}
void TSTabletManager::CleanupSplitTablets() {
VLOG_WITH_PREFIX_AND_FUNC(3) << "looking for tablets to cleanup...";
auto tablet_peers = GetTabletPeers();
for (const auto& tablet_peer : tablet_peers) {
if (tablet_peer->CanBeDeleted()) {
const auto& tablet_id = tablet_peer->tablet_id();
if (PREDICT_FALSE(FLAGS_TEST_skip_deleting_split_tablets)) {
LOG_WITH_PREFIX(INFO) << Format("Skipped triggering delete of tablet $0", tablet_id);
} else {
LOG_WITH_PREFIX(INFO) << Format("Triggering delete of tablet $0", tablet_id);
client().DeleteNotServingTablet(
tablet_peer->tablet_id(), [tablet_id](const Status& status) {
LOG(INFO) << Format("Tablet $0 deletion result: $1", tablet_id, status);
});
}
}
}
}
Status TSTabletManager::WaitForAllBootstrapsToFinish() {
CHECK_EQ(state(), MANAGER_RUNNING);
open_tablet_pool_->Wait();
Status s = Status::OK();
SharedLock<RWMutex> shared_lock(mutex_);
for (const TabletMap::value_type& entry : tablet_map_) {
if (entry.second->state() == tablet::FAILED) {
if (s.ok()) {
s = entry.second->error();
}
}
}
return s;
}
Result<scoped_refptr<TransitionInProgressDeleter>>
TSTabletManager::StartTabletStateTransitionForCreation(const TabletId& tablet_id) {
scoped_refptr<TransitionInProgressDeleter> deleter;
SharedLock<RWMutex> lock(mutex_);
TRACE("Acquired tablet manager lock");
// Sanity check that the tablet isn't already registered.
if (LookupTabletUnlocked(tablet_id) != nullptr) {
return STATUS(AlreadyPresent, "Tablet already registered", tablet_id);
}
RETURN_NOT_OK(StartTabletStateTransition(tablet_id, "creating tablet", &deleter));
return deleter;
}
Result<TabletPeerPtr> TSTabletManager::CreateNewTablet(
const tablet::TableInfoPtr& table_info,
const string& tablet_id,
const Partition& partition,
RaftConfigPB config,
const bool colocated,
const std::vector<SnapshotScheduleId>& snapshot_schedules) {
if (state() != MANAGER_RUNNING) {
return STATUS_FORMAT(IllegalState, "Manager is not running: $0", state());
}
CHECK(IsRaftConfigMember(server_->instance_pb().permanent_uuid(), config));
for (int i = 0; i < config.peers_size(); ++i) {
const auto& config_peer = config.peers(i);
CHECK(config_peer.has_member_type());
}
// Set the initial opid_index for a RaftConfigPB to -1.
config.set_opid_index(consensus::kInvalidOpIdIndex);
scoped_refptr<TransitionInProgressDeleter> deleter =
VERIFY_RESULT(StartTabletStateTransitionForCreation(tablet_id));
// Create the metadata.
TRACE("Creating new metadata...");
string data_root_dir;
string wal_root_dir;
GetAndRegisterDataAndWalDir(
fs_manager_, table_info->table_id, tablet_id, &data_root_dir, &wal_root_dir);
fs_manager_->SetTabletPathByDataPath(tablet_id, data_root_dir);
auto create_result = RaftGroupMetadata::CreateNew(tablet::RaftGroupMetadataData {
.fs_manager = fs_manager_,
.table_info = table_info,
.raft_group_id = tablet_id,
.partition = partition,
.tablet_data_state = TABLET_DATA_READY,
.colocated = colocated,
.snapshot_schedules = snapshot_schedules,
}, data_root_dir, wal_root_dir);
if (!create_result.ok()) {
UnregisterDataWalDir(table_info->table_id, tablet_id, data_root_dir, wal_root_dir);
}
RETURN_NOT_OK_PREPEND(create_result, "Couldn't create tablet metadata")
RaftGroupMetadataPtr meta = std::move(*create_result);
LOG(INFO) << TabletLogPrefix(tablet_id)
<< "Created tablet metadata for table: " << table_info->table_id;
// We must persist the consensus metadata to disk before starting a new
// tablet's TabletPeer and Consensus implementation.
std::unique_ptr<ConsensusMetadata> cmeta;
RETURN_NOT_OK_PREPEND(ConsensusMetadata::Create(fs_manager_, tablet_id, fs_manager_->uuid(),
config, consensus::kMinimumTerm, &cmeta),
"Unable to create new ConsensusMeta for tablet " + tablet_id);
TabletPeerPtr new_peer = VERIFY_RESULT(CreateAndRegisterTabletPeer(meta, NEW_PEER));
// We can run this synchronously since there is nothing to bootstrap.
RETURN_NOT_OK(
open_tablet_pool_->SubmitFunc(std::bind(&TSTabletManager::OpenTablet, this, meta, deleter)));
return new_peer;
}
struct TabletCreationMetaData {
TabletId tablet_id;
scoped_refptr<TransitionInProgressDeleter> transition_deleter;
Partition partition;
docdb::KeyBounds key_bounds;
RaftGroupMetadataPtr raft_group_metadata;
};
namespace {
// Creates SplitTabletsCreationMetaData for two new tablets for `tablet` splitting based on request.
SplitTabletsCreationMetaData PrepareTabletCreationMetaDataForSplit(
const tablet::SplitTabletRequestPB& request, const tablet::Tablet& tablet) {
SplitTabletsCreationMetaData metas;
const auto& split_partition_key = request.split_partition_key();
const auto& split_encoded_key = request.split_encoded_key();
std::shared_ptr<Partition> source_partition = tablet.metadata()->partition();
const auto source_key_bounds = *tablet.doc_db().key_bounds;
{
TabletCreationMetaData meta;
meta.tablet_id = request.new_tablet1_id();
meta.partition = *source_partition;
meta.key_bounds = source_key_bounds;
meta.partition.set_partition_key_end(split_partition_key);
meta.key_bounds.upper.Reset(split_encoded_key);
metas.push_back(meta);
}
{
TabletCreationMetaData meta;
meta.tablet_id = request.new_tablet2_id();
meta.partition = *source_partition;
meta.key_bounds = source_key_bounds;
meta.partition.set_partition_key_start(split_partition_key);
meta.key_bounds.lower.Reset(split_encoded_key);
metas.push_back(meta);
}
return metas;
}
} // namespace
Status TSTabletManager::StartSubtabletsSplit(
const RaftGroupMetadata& source_tablet_meta, SplitTabletsCreationMetaData* tcmetas) {
auto* const env = fs_manager_->env();
auto iter = tcmetas->begin();
while (iter != tcmetas->end()) {
const auto& subtablet_id = iter->tablet_id;
auto transition_deleter_result = StartTabletStateTransitionForCreation(subtablet_id);
if (transition_deleter_result.ok()) {
iter->transition_deleter = *transition_deleter_result;
} else if (transition_deleter_result.status().IsAlreadyPresent()) {
// State transition for sub tablet with subtablet_id could be already registered because its
// remote bootstrap (from already split parent tablet leader) is in progress.
iter = tcmetas->erase(iter);
continue;
} else {
return transition_deleter_result.status();
}
// Try to load metadata from previous not completed split.
if (fs_manager_->LookupTablet(subtablet_id)) {
auto load_result = RaftGroupMetadata::Load(fs_manager_, subtablet_id);
if (load_result.ok() && CanServeTabletData((*load_result)->tablet_data_state())) {
// Sub tablet has been already created and ready during previous split attempt at this node
// or as a result of remote bootstrap from another node, no need to re-create.
iter = tcmetas->erase(iter);
continue;
}
}
// Delete on-disk data for new tablet IDs in case it is present as a leftover from previously
// failed tablet split attempt.
// TODO(tsplit): add test for that.
const auto data_dir = source_tablet_meta.GetSubRaftGroupDataDir(subtablet_id);
if (env->FileExists(data_dir)) {
RETURN_NOT_OK_PREPEND(
env->DeleteRecursively(data_dir),
Format("Unable to recursively delete data dir for tablet $0", subtablet_id));
}
RETURN_NOT_OK(Log::DeleteOnDiskData(
env, subtablet_id, source_tablet_meta.GetSubRaftGroupWalDir(subtablet_id),
fs_manager_->uuid()));
RETURN_NOT_OK(ConsensusMetadata::DeleteOnDiskData(fs_manager_, subtablet_id));
++iter;
}
return Status::OK();
}
void TSTabletManager::CreatePeerAndOpenTablet(
const tablet::RaftGroupMetadataPtr& meta,
const scoped_refptr<TransitionInProgressDeleter>& deleter) {
Status s = ResultToStatus(CreateAndRegisterTabletPeer(meta, NEW_PEER));
if (!s.ok()) {
s = s.CloneAndPrepend("Failed to create and register tablet peer");
if (s.IsShutdownInProgress()) {
// If shutdown is in progress, it is not a failure to not being able to create and register
// tablet peer.
LOG_WITH_PREFIX(WARNING) << s;
} else {
LOG_WITH_PREFIX(DFATAL) << s;
}
return;
}
s = open_tablet_pool_->SubmitFunc(std::bind(&TSTabletManager::OpenTablet, this, meta, deleter));
if (!s.ok()) {
LOG(DFATAL) << Format("Failed to schedule opening tablet $0: $1", meta->table_id(), s);
return;
}
}
Status TSTabletManager::ApplyTabletSplit(
tablet::SplitOperation* operation, log::Log* raft_log,
boost::optional<RaftConfigPB> committed_raft_config) {
if (PREDICT_FALSE(FLAGS_TEST_crash_before_apply_tablet_split_op)) {
LOG(FATAL) << "Crashing due to FLAGS_TEST_crash_before_apply_tablet_split_op";
}
if (state() != MANAGER_RUNNING) {
return STATUS_FORMAT(IllegalState, "Manager is not running: $0", state());
}
const auto& split_op_id = operation->op_id();
SCHECK_FORMAT(
split_op_id.valid() && !split_op_id.empty(), InvalidArgument,
"Incorrect ID for SPLIT_OP: $0", operation->ToString());
auto* tablet = CHECK_NOTNULL(operation->tablet());
const auto tablet_id = tablet->tablet_id();
const auto* request = operation->request();
SCHECK_EQ(
request->tablet_id(), tablet_id, IllegalState,
Format(
"Unexpected SPLIT_OP $0 designated for tablet $1 to be applied to tablet $2",
split_op_id, request->tablet_id(), tablet_id));
SCHECK(
tablet_id != request->new_tablet1_id() && tablet_id != request->new_tablet2_id(),
IllegalState,
Format(
"One of SPLIT_OP $0 destination tablet IDs ($1, $2) is the same as source tablet ID $3",
split_op_id, request->new_tablet1_id(), request->new_tablet2_id(), tablet_id));
LOG_WITH_PREFIX(INFO) << "Tablet " << tablet_id << " split operation " << split_op_id
<< " apply started";
auto tablet_peer = VERIFY_RESULT(GetTablet(tablet_id));
auto* raft_consensus = tablet_peer->raft_consensus();
if (raft_log == nullptr) {
raft_log = DCHECK_NOTNULL(raft_consensus)->log().get();
}
if (!committed_raft_config) {
committed_raft_config = DCHECK_NOTNULL(raft_consensus)->CommittedConfigUnlocked();
}
MAYBE_FAULT(FLAGS_TEST_fault_crash_in_split_before_log_flushed);
TEST_PAUSE_IF_FLAG(TEST_pause_apply_tablet_split);
auto& meta = *CHECK_NOTNULL(tablet->metadata());
if (meta.tablet_data_state() == TABLET_DATA_SPLIT_COMPLETED) {
LOG_WITH_PREFIX(INFO) << "Tablet " << tablet_id
<< " split operation has been trivially applied, "
<< "because tablet is already in TABLET_DATA_SPLIT_COMPLETED state";
return Status::OK();
}
// TODO(tsplit): We can later implement better per-disk distribution during compaction of split
// tablets.
const auto table_id = meta.table_id();
const auto data_root_dir =
VERIFY_RESULT(GetAssignedRootDirForTablet(TabletDirType::kData, table_id, tablet_id));
const auto wal_root_dir =
VERIFY_RESULT(GetAssignedRootDirForTablet(TabletDirType::kWal, table_id, tablet_id));
if (FLAGS_TEST_apply_tablet_split_inject_delay_ms > 0) {
LOG(INFO) << "TEST: ApplyTabletSplit: injecting delay of "
<< FLAGS_TEST_apply_tablet_split_inject_delay_ms << " ms for "
<< AsString(*operation);
std::this_thread::sleep_for(FLAGS_TEST_apply_tablet_split_inject_delay_ms * 1ms);
LOG(INFO) << "TEST: ApplyTabletSplit: delay finished";
}
auto tcmetas = PrepareTabletCreationMetaDataForSplit(*request, *tablet);
RETURN_NOT_OK(StartSubtabletsSplit(meta, &tcmetas));
for (const auto& tcmeta : tcmetas) {
RegisterDataAndWalDir(fs_manager_, table_id, tcmeta.tablet_id, data_root_dir, wal_root_dir);
fs_manager_->SetTabletPathByDataPath(tcmeta.tablet_id, data_root_dir);
}
bool successfully_completed = false;
auto se = ScopeExit([&] {
if (!successfully_completed) {
for (const auto& tcmeta : tcmetas) {
UnregisterDataWalDir(table_id, tcmeta.tablet_id, data_root_dir, wal_root_dir);
}
}
});
std::unique_ptr<ConsensusMetadata> cmeta;
RETURN_NOT_OK(ConsensusMetadata::Create(
fs_manager_, tablet_id, fs_manager_->uuid(), committed_raft_config.value(),
split_op_id.term, &cmeta));
if (request->has_split_parent_leader_uuid()) {
cmeta->set_leader_uuid(request->split_parent_leader_uuid());
LOG_WITH_PREFIX(INFO) << "Using Raft config: " << committed_raft_config->ShortDebugString();
}
cmeta->set_split_parent_tablet_id(tablet_id);
for (auto& tcmeta : tcmetas) {
const auto& new_tablet_id = tcmeta.tablet_id;
// Copy raft group metadata.
tcmeta.raft_group_metadata = VERIFY_RESULT(tablet->CreateSubtablet(
new_tablet_id, tcmeta.partition, tcmeta.key_bounds, split_op_id,
operation->hybrid_time()));
LOG_WITH_PREFIX(INFO) << "Created raft group metadata for table: " << table_id
<< " tablet: " << new_tablet_id;
// Store consensus metadata.
// Here we reuse the same cmeta instance for both new tablets. This is safe, because:
// 1) Their consensus metadata only differ by tablet id.
// 2) Flush() will save it into a new path corresponding to tablet ID we set before flushing.
cmeta->set_tablet_id(new_tablet_id);
RETURN_NOT_OK(cmeta->Flush());
const auto& dest_wal_dir = tcmeta.raft_group_metadata->wal_dir();
RETURN_NOT_OK(raft_log->CopyTo(dest_wal_dir, split_op_id));
MAYBE_FAULT(FLAGS_TEST_fault_crash_in_split_after_log_copied);
tcmeta.raft_group_metadata->set_tablet_data_state(TABLET_DATA_READY);
RETURN_NOT_OK(tcmeta.raft_group_metadata->Flush());
}
if (PREDICT_FALSE(FLAGS_TEST_crash_before_source_tablet_mark_split_done)) {
LOG(FATAL) << "Crashing due to FLAGS_TEST_crash_before_source_tablet_mark_split_done";
}
meta.SetSplitDone(split_op_id, request->new_tablet1_id(), request->new_tablet2_id());
RETURN_NOT_OK(meta.Flush());
tablet->SplitDone();
if (PREDICT_FALSE(FLAGS_TEST_crash_after_tablet_split_completed)) {