-
Notifications
You must be signed in to change notification settings - Fork 411
/
PathPool.cpp
1291 lines (1148 loc) · 40.5 KB
/
PathPool.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
// Copyright 2023 PingCAP, 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 <Common/Exception.h>
#include <Common/Logger.h>
#include <Common/escapeForFileName.h>
#include <Core/Types.h>
#include <IO/FileProvider/FileProvider.h>
#include <IO/WriteHelpers.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <Storages/KVStore/FFI/ProxyFFI.h>
#include <Storages/Page/Page.h>
#include <Storages/Page/PageDefinesBase.h>
#include <Storages/PathCapacityMetrics.h>
#include <Storages/PathPool.h>
#include <TiDB/Schema/SchemaNameMapper.h>
#include <common/likely.h>
#include <common/logger_useful.h>
#include <fmt/core.h>
#include <mutex>
#include <set>
#include <unordered_map>
#include <utility>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
inline String removeTrailingSlash(String s)
{
if (s.back() == '/')
s.erase(s.begin() + s.size() - 1);
return s;
}
inline String getNormalizedPath(const String & s)
{
return removeTrailingSlash(Poco::Path{s}.toString());
}
const String PathPool::log_path_prefix = "log";
const String PathPool::data_path_prefix = "data";
const String PathPool::meta_path_prefix = "meta";
const String PathPool::kvstore_path_prefix = "kvstore";
const String PathPool::write_uni_path_prefix = "write";
const String PathPool::read_node_cache_path_prefix = "read_cache";
// Constructor to be used during initialization
PathPool::PathPool(
const Strings & main_data_paths_,
const Strings & latest_data_paths_,
const Strings & kvstore_paths_, //
PathCapacityMetricsPtr global_capacity_,
FileProviderPtr file_provider_)
: main_data_paths(main_data_paths_)
, latest_data_paths(latest_data_paths_)
, kvstore_paths(kvstore_paths_)
, global_capacity(global_capacity_)
, file_provider(file_provider_)
, log(Logger::get())
{
if (kvstore_paths.empty())
{
// Set default path generated from latest_data_paths
for (const auto & s : latest_data_paths)
{
// Get a normalized path without trailing '/'
auto p = getNormalizedPath(s + "/" + PathPool::kvstore_path_prefix);
kvstore_paths.emplace_back(std::move(p));
}
}
for (const auto & s : latest_data_paths)
{
// Get a normalized path without trailing '/'
auto p = getNormalizedPath(s + "/page");
global_page_paths.emplace_back(std::move(p));
}
}
StoragePathPool PathPool::withTable(const String & database_, const String & table_, bool path_need_database_name_)
const
{
return StoragePathPool(
main_data_paths,
latest_data_paths,
database_,
table_,
path_need_database_name_,
global_capacity,
file_provider);
}
Strings PathPool::listPaths() const
{
std::set<String> path_set;
for (const auto & p : main_data_paths)
path_set.insert(getNormalizedPath(p + "/data"));
for (const auto & p : latest_data_paths)
path_set.insert(getNormalizedPath(p + "/data"));
Strings paths;
for (const auto & p : path_set)
paths.emplace_back(p);
return paths;
}
PSDiskDelegatorPtr PathPool::getPSDiskDelegatorRaft()
{
return std::make_shared<PSDiskDelegatorRaft>(*this);
}
PSDiskDelegatorPtr PathPool::getPSDiskDelegatorGlobalMulti(const String & prefix) const
{
return std::make_shared<PSDiskDelegatorGlobalMulti>(*this, prefix);
}
PSDiskDelegatorPtr PathPool::getPSDiskDelegatorGlobalSingle(const String & prefix) const
{
return std::make_shared<PSDiskDelegatorGlobalSingle>(*this, prefix);
}
PSDiskDelegatorPtr PathPool::getPSDiskDelegatorFixedDirectory(const String & dir) const
{
return std::make_shared<PSDiskDelegatorFixedDirectory>(*this, dir);
}
//==========================================================================================
// StoragePathPool
//==========================================================================================
StoragePathPool::StoragePathPool( //
const Strings & main_data_paths,
const Strings & latest_data_paths, //
String database_,
String table_,
bool path_need_database_name_, //
PathCapacityMetricsPtr global_capacity_,
FileProviderPtr file_provider_)
: database(std::move(database_))
, table(std::move(table_))
, keyspace_id(SchemaNameMapper::getMappedNameKeyspaceID(table_))
, path_need_database_name(path_need_database_name_)
, shutdown_called(false)
, global_capacity(std::move(global_capacity_))
, file_provider(std::move(file_provider_))
, log(Logger::get())
{
RUNTIME_CHECK_MSG(
!database.empty() && !table.empty(),
"Can NOT create StoragePathPool [database={}] [table={}]",
database,
table);
for (const auto & p : main_data_paths)
{
MainPathInfo info;
info.path = getStorePath(p + "/data", database, table);
main_path_infos.emplace_back(info);
}
for (const auto & p : latest_data_paths)
{
LatestPathInfo info;
info.path = getStorePath(p + "/data", database, table);
latest_path_infos.emplace_back(info);
}
}
StoragePathPool::StoragePathPool(StoragePathPool && rhs) noexcept
: main_path_infos(std::move(rhs.main_path_infos))
, latest_path_infos(std::move(rhs.latest_path_infos))
, database(std::move(rhs.database))
, table(std::move(rhs.table))
, keyspace_id(rhs.keyspace_id)
, dt_file_path_map(std::move(rhs.dt_file_path_map))
, path_need_database_name(rhs.path_need_database_name)
, shutdown_called(rhs.shutdown_called.load())
, global_capacity(std::move(rhs.global_capacity))
, file_provider(std::move(rhs.file_provider))
, log(std::move(rhs.log))
{}
StoragePathPool & StoragePathPool::operator=(StoragePathPool && rhs)
{
if (this != &rhs)
{
main_path_infos.swap(rhs.main_path_infos);
latest_path_infos.swap(rhs.latest_path_infos);
dt_file_path_map.swap(rhs.dt_file_path_map);
database.swap(rhs.database);
table.swap(rhs.table);
keyspace_id = rhs.keyspace_id;
path_need_database_name = rhs.path_need_database_name;
shutdown_called = rhs.shutdown_called.load();
global_capacity.swap(rhs.global_capacity);
file_provider.swap(rhs.file_provider);
log.swap(rhs.log);
}
return *this;
}
bool StoragePathPool::createPSV2DeleteMarkFile()
{
try
{
return Poco::File(getPSV2DeleteMarkFilePath()).createFile();
}
catch (...)
{
tryLogCurrentException(log);
return false;
}
}
bool StoragePathPool::isPSV2Deleted() const
{
return Poco::File(getPSV2DeleteMarkFilePath()).exists();
}
void StoragePathPool::clearPSV2ObsoleteData()
{
std::lock_guard lock{mutex};
auto drop_instance_data = [&](const String & prefix) {
for (auto & path_info : latest_path_infos)
{
try
{
auto path = fmt::format("{}/{}", path_info.path, prefix);
if (Poco::File dir(path); dir.exists())
{
// This function is used to clean obsolete data in ps v2 instance at restart,
// so no need to update global_capacity here.
LOG_INFO(log, "Begin to drop obsolete data[dir={}]", path);
file_provider->deleteDirectory(dir.path(), false, true);
}
}
catch (...)
{
tryLogCurrentException(log);
}
}
};
drop_instance_data("meta");
drop_instance_data("log");
drop_instance_data("data");
}
void StoragePathPool::rename(const String & new_database, const String & new_table)
{
RUNTIME_CHECK(!new_database.empty() && !new_table.empty(), database, table, new_database, new_table);
RUNTIME_CHECK(!path_need_database_name, database, table, new_database, new_table);
// The directories for storing table data is not changed, just rename related names.
std::lock_guard lock{mutex};
database = new_database;
table = new_table;
}
void StoragePathPool::drop(bool recursive, bool must_success)
{
std::lock_guard lock{mutex};
for (auto & path_info : main_path_infos)
{
try
{
if (Poco::File dir(path_info.path); dir.exists())
{
LOG_INFO(log, "Begin to drop [dir={}] from main_path_infos", path_info.path);
file_provider->deleteDirectory(dir.path(), false, recursive);
// update global used size
size_t total_bytes = 0;
for (const auto & [file_id, file_size] : path_info.file_size_map)
{
(void)file_id;
total_bytes += file_size;
}
global_capacity->freeUsedSize(path_info.path, total_bytes);
// clear in case delegator->removeDTFile is called after `drop`
dt_file_path_map.clear();
}
}
catch (Poco::DirectoryNotEmptyException & e)
{
if (must_success)
throw;
else
{
// just ignore and keep that directory if it is not empty
LOG_WARNING(log, "Can not remove directory: {}, it is not empty", path_info.path);
}
}
}
for (auto & path_info : latest_path_infos)
{
try
{
if (Poco::File dir(path_info.path); dir.exists())
{
LOG_INFO(log, "Begin to drop [dir={}] from latest_path_infos", path_info.path);
file_provider->deleteDirectory(dir.path(), false, recursive);
// When PageStorage is dropped, it will update the size in global_capacity.
// Don't need to update global_capacity here.
}
}
catch (Poco::DirectoryNotEmptyException & e)
{
if (must_success)
throw;
else
{
// just ignore and keep that directory if it is not empty
LOG_WARNING(log, "Can not remove directory: {}, it is not empty", path_info.path);
}
}
}
for (const auto & [local_file_id, size_entry] : remote_dt_file_size_map)
{
UNUSED(local_file_id);
global_capacity->freeRemoteUsedSize(keyspace_id, size_entry.second);
}
remote_dt_file_size_map.clear();
}
//==========================================================================================
// private methods
//==========================================================================================
String StoragePathPool::getStorePath(
const String & extra_path_root,
const String & database_name,
const String & table_name) const
{
if (likely(!path_need_database_name))
return getNormalizedPath(fmt::format("{}/{}", extra_path_root, escapeForFileName(table_name)));
else
return getNormalizedPath(
fmt::format("{}/{}/{}", extra_path_root, escapeForFileName(database_name), escapeForFileName(table_name)));
}
void StoragePathPool::renamePath(const String & old_path, const String & new_path)
{
LOG_INFO(log, "Renaming {} to {}", old_path, new_path);
if (auto file = Poco::File{old_path}; file.exists())
file.renameTo(new_path);
else
LOG_WARNING(log, "Path \"{}\" is missed.", old_path);
}
String StoragePathPool::getPSV2DeleteMarkFilePath() const
{
return fmt::format("{}/{}", latest_path_infos[0].path, "PS_V2_DELETED");
}
//==========================================================================================
// Generic functions
//==========================================================================================
template <typename T>
String genericChoosePath(
const std::vector<T> & paths,
const PathCapacityMetricsPtr & global_capacity,
std::function<String(const std::vector<T> & paths, size_t idx)> path_generator,
std::function<String(const T & path_info)> path_getter,
const LoggerPtr & log,
const String & log_msg)
{
if (paths.size() == 1)
return path_generator(paths, 0);
UInt64 total_available_size = 0;
std::vector<FsStats> stats;
for (size_t i = 0; i < paths.size(); ++i)
{
stats.emplace_back(std::get<0>(global_capacity->getFsStatsOfPath(path_getter(paths[i]))));
total_available_size += stats.back().avail_size;
}
// We should choose path even if there is no available space.
// If the actual disk space is running out, let the later `write` to throw exception.
// If available space is limited by the quota, then write down a GC-ed file can make
// some files be deleted later.
if (total_available_size == 0)
LOG_WARNING(log, "No available space for all disks, choose randomly.");
std::vector<double> ratio;
for (auto & stat : stats)
{
if (likely(total_available_size != 0))
ratio.push_back(1.0 * stat.avail_size / total_available_size);
else
{
// No available space for all disks, choose randomly
ratio.push_back(1.0 / paths.size());
}
}
double rand_number = static_cast<double>(rand()) / RAND_MAX; // NOLINT(cert-msc50-cpp)
double ratio_sum = 0.0;
for (size_t i = 0; i < ratio.size(); i++)
{
ratio_sum += ratio[i];
if ((rand_number < ratio_sum) || (i == ratio.size() - 1))
{
LOG_INFO(log, "Choose path [index={}] {}", i, log_msg);
return path_generator(paths, i);
}
}
throw Exception("Should not reach here", ErrorCodes::LOGICAL_ERROR);
}
//==========================================================================================
// Stable data
//==========================================================================================
Strings StableDiskDelegator::listPaths() const
{
std::vector<String> paths;
for (auto & main_path_info : pool.main_path_infos)
{
paths.push_back(fmt::format("{}/{}", main_path_info.path, StoragePathPool::STABLE_FOLDER_NAME));
}
return paths;
}
String StableDiskDelegator::choosePath() const
{
std::function<String(const StoragePathPool::MainPathInfos & paths, size_t idx)> path_generator
= [](const StoragePathPool::MainPathInfos & paths, size_t idx) -> String {
return fmt::format("{}/{}", paths[idx].path, StoragePathPool::STABLE_FOLDER_NAME);
};
std::function<String(const StoragePathPool::MainPathInfo & info)> path_getter
= [](const StoragePathPool::MainPathInfo & info) -> String {
return info.path;
};
const String log_msg = fmt::format("[type=stable] [database={}] [table={}]", pool.database, pool.table);
return genericChoosePath(
pool.main_path_infos,
pool.global_capacity,
path_generator,
path_getter,
pool.log,
log_msg);
}
String StableDiskDelegator::getDTFilePath(UInt64 file_id, bool throw_on_not_exist) const
{
std::lock_guard lock{pool.mutex};
auto iter = pool.dt_file_path_map.find(file_id);
if (likely(iter != pool.dt_file_path_map.end()))
return fmt::format("{}/{}", pool.main_path_infos[iter->second].path, StoragePathPool::STABLE_FOLDER_NAME);
if (likely(throw_on_not_exist))
throw Exception(fmt::format("Can not find path for DMFile, file_id={}", file_id));
return "";
}
void StableDiskDelegator::addDTFile(UInt64 file_id, size_t file_size, std::string_view path)
{
// remove '/stable' added in listPathsForStable/getDTFilePath
path.remove_suffix(1 + strlen(StoragePathPool::STABLE_FOLDER_NAME));
std::lock_guard lock{pool.mutex};
if (auto iter = pool.dt_file_path_map.find(file_id); unlikely(iter != pool.dt_file_path_map.end()))
{
const auto & path_info = pool.main_path_infos[iter->second];
throw DB::TiFlashException(
fmt::format(
"Try to add a DTFile with duplicated id, file_id={} path={} existed_path={}",
file_id,
path,
path_info.path),
Errors::DeltaTree::Internal);
}
UInt32 index = UINT32_MAX;
for (size_t i = 0; i < pool.main_path_infos.size(); i++)
{
if (pool.main_path_infos[i].path == path)
{
index = i;
break;
}
}
RUNTIME_CHECK_MSG(
index != UINT32_MAX,
"Try to add a DTFile to an unrecognized path. file_id={} path={}",
file_id,
path);
pool.dt_file_path_map.emplace(file_id, index);
pool.main_path_infos[index].file_size_map.emplace(file_id, file_size);
#ifndef NDEBUG
try
{
auto dmf_path = fmt::format("{}/stable/dmf_{}", path, file_id);
Poco::File dmf_file = {dmf_path};
if (dmf_file.isFile())
{
LOG_DEBUG(
pool.log,
"added new dtfile. [id={}] [path={}] [real_size={}] [reported_size={}]",
file_id,
path,
dmf_file.getSize(),
file_size);
}
else
{
size_t size_sum = 0;
auto get_folder_size = [](const Poco::File & target, size_t & counter) -> void {
auto get_folder_size_impl
= [](const Poco::File & inner_target, size_t & inner_counter, auto & self) -> void {
std::vector<Poco::File> files;
inner_target.list(files);
for (auto & i : files)
{
if (i.isFile())
{
inner_counter += i.getSize();
}
else
{
self(i, inner_counter, self);
}
}
};
get_folder_size_impl(target, counter, get_folder_size_impl);
};
get_folder_size(dmf_file, size_sum);
LOG_DEBUG(
pool.log,
"added new dtfile. [id={}] [path={}] [real_size={}] [reported_size={}]",
file_id,
path,
size_sum,
file_size);
}
}
catch (const Poco::Exception & exp)
{
LOG_WARNING(
pool.log,
"failed to get real size info for dtfile. [id={}] [path={}] [err={}]",
file_id,
path,
exp.displayText());
}
#endif
// update global used size
pool.global_capacity->addUsedSize(path, file_size);
}
bool StableDiskDelegator::updateDTFileSize(UInt64 file_id, size_t file_size)
{
std::lock_guard lock{pool.mutex};
auto iter = pool.dt_file_path_map.find(file_id);
if (iter == pool.dt_file_path_map.end())
{
return false;
}
auto index = iter->second;
auto it = pool.main_path_infos[index].file_size_map.find(file_id);
if (it == pool.main_path_infos[index].file_size_map.end())
{
return false;
}
const auto origin_file_size = it->second;
it->second = file_size;
// update global used size
pool.global_capacity->addUsedSize(pool.main_path_infos[index].path, file_size - origin_file_size);
return true;
}
void StableDiskDelegator::removeDTFile(UInt64 file_id, bool throw_on_not_exist)
{
std::lock_guard lock{pool.mutex};
auto iter = pool.dt_file_path_map.find(file_id);
if (iter == pool.dt_file_path_map.end())
{
if (throw_on_not_exist)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot find DMFile when removing, file_id={}", file_id);
else
return;
}
UInt32 index = iter->second;
const auto file_size = pool.main_path_infos[index].file_size_map.at(file_id);
pool.dt_file_path_map.erase(file_id);
pool.main_path_infos[index].file_size_map.erase(file_id);
// update global used size
pool.global_capacity->freeUsedSize(pool.main_path_infos[index].path, file_size);
}
void StableDiskDelegator::addRemoteDTFileIfNotExists(UInt64 local_external_id, size_t file_size)
{
std::lock_guard lock{pool.mutex};
auto [iter, inserted] = pool.remote_dt_file_size_map.emplace(local_external_id, std::make_pair(true, file_size));
if (!inserted)
{
RUNTIME_CHECK(iter->second.second == file_size, iter->second.second, file_size);
return;
}
// update global used size
pool.global_capacity->addRemoteUsedSize(pool.keyspace_id, file_size);
}
void StableDiskDelegator::addRemoteDTFileWithGCDisabled(UInt64 local_external_id, size_t file_size)
{
std::lock_guard lock{pool.mutex};
auto [_, inserted] = pool.remote_dt_file_size_map.emplace(local_external_id, std::make_pair(false, file_size));
RUNTIME_CHECK(inserted);
// update global used size
pool.global_capacity->addRemoteUsedSize(pool.keyspace_id, file_size);
}
void StableDiskDelegator::enableGCForRemoteDTFile(UInt64 local_page_id)
{
std::lock_guard lock{pool.mutex};
auto iter = pool.remote_dt_file_size_map.find(local_page_id);
// local_page_id may be a ref id, so it may not in remote_dt_file_size_map
if (iter != pool.remote_dt_file_size_map.end())
{
iter->second.first = true;
}
}
void StableDiskDelegator::removeRemoteDTFile(UInt64 local_external_id, bool throw_on_not_exist)
{
std::lock_guard lock{pool.mutex};
auto iter = pool.remote_dt_file_size_map.find(local_external_id);
if (iter == pool.remote_dt_file_size_map.end())
{
if (throw_on_not_exist)
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Cannot find remote DMFile when removing, file_id={}",
local_external_id);
else
return;
}
// update global used size
pool.global_capacity->freeRemoteUsedSize(pool.keyspace_id, iter->second.second);
pool.remote_dt_file_size_map.erase(iter);
}
std::set<UInt64> StableDiskDelegator::getAllRemoteDTFilesForGC()
{
std::lock_guard lock{pool.mutex};
std::set<UInt64> all_local_external_ids;
for (auto & [local_external_id, pair] : pool.remote_dt_file_size_map)
{
if (pair.first)
{
all_local_external_ids.insert(local_external_id);
}
}
return all_local_external_ids;
}
//==========================================================================================
// Delta data
//==========================================================================================
bool PSDiskDelegatorMulti::fileExist(const PageFileIdAndLevel & /*id_lvl*/) const
{
throw Exception("Not Implemented", ErrorCodes::NOT_IMPLEMENTED);
}
size_t PSDiskDelegatorMulti::numPaths() const
{
return pool.latest_path_infos.size();
}
String PSDiskDelegatorMulti::defaultPath() const
{
return fmt::format("{}/{}", pool.latest_path_infos[default_path_index].path, path_prefix);
}
Strings PSDiskDelegatorMulti::listPaths() const
{
// The delta data could be stored in all direcotries.
std::vector<String> paths;
for (auto & latest_path_info : pool.latest_path_infos)
{
paths.push_back(fmt::format("{}/{}", latest_path_info.path, path_prefix));
}
return paths;
}
String PSDiskDelegatorMulti::choosePath(const PageFileIdAndLevel & id_lvl)
{
std::function<String(const StoragePathPool::LatestPathInfos & paths, size_t idx)> path_generator
= [this](const StoragePathPool::LatestPathInfos & paths, size_t idx) -> String {
return fmt::format("{}/{}", paths[idx].path, this->path_prefix);
};
std::function<String(const StoragePathPool::LatestPathInfo & info)> path_getter
= [](const StoragePathPool::LatestPathInfo & info) -> String {
return info.path;
};
{
/// If id exists in page_path_map, just return the same path
auto index_opt = page_path_map.getIndex(id_lvl);
if (index_opt.has_value())
return path_generator(pool.latest_path_infos, *index_opt);
}
const String log_msg = fmt::format("[type=ps_multi] [database={}] [table={}]", pool.database, pool.table);
return genericChoosePath(
pool.latest_path_infos,
pool.global_capacity,
path_generator,
path_getter,
pool.log,
log_msg);
}
size_t PSDiskDelegatorMulti::addPageFileUsedSize(
const PageFileIdAndLevel & id_lvl,
size_t size_to_add,
const String & pf_parent_path,
bool need_insert_location)
{
// Get a normalized path without `path_prefix` and trailing '/'
String upper_path = removeTrailingSlash(Poco::Path(pf_parent_path).parent().toString());
UInt32 index = UINT32_MAX;
for (size_t i = 0; i < pool.latest_path_infos.size(); i++)
{
if (pool.latest_path_infos[i].path == upper_path)
{
index = i;
break;
}
}
RUNTIME_CHECK_MSG(index != UINT32_MAX, "Unrecognized path {}", upper_path);
if (need_insert_location)
page_path_map.setIndex(id_lvl, index);
// update global used size
pool.global_capacity->addUsedSize(upper_path, size_to_add);
return index;
}
void PSDiskDelegatorMulti::freePageFileUsedSize(
const PageFileIdAndLevel & /*id_lvl*/,
size_t /*size_to_free*/,
const String & /*pf_parent_path*/)
{
throw Exception("Not Implemented", ErrorCodes::NOT_IMPLEMENTED);
}
String PSDiskDelegatorMulti::getPageFilePath(const PageFileIdAndLevel & id_lvl) const
{
auto index_opt = page_path_map.getIndex(id_lvl);
RUNTIME_CHECK_MSG(index_opt.has_value(), "Can not find path for PageFile, file_id={}", id_lvl);
return fmt::format("{}/{}", pool.latest_path_infos[*index_opt].path, path_prefix);
}
void PSDiskDelegatorMulti::removePageFile(
const PageFileIdAndLevel & id_lvl,
size_t file_size,
bool meta_left,
bool remove_from_default_path)
{
if (remove_from_default_path)
{
pool.global_capacity->freeUsedSize(pool.latest_path_infos[default_path_index].path, file_size);
}
else
{
auto index_opt = page_path_map.getIndex(id_lvl);
if (unlikely(!index_opt.has_value()))
return;
if (!meta_left)
page_path_map.eraseIfExist(id_lvl);
pool.global_capacity->freeUsedSize(pool.latest_path_infos[*index_opt].path, file_size);
}
}
//==========================================================================================
// Normal data
//==========================================================================================
bool PSDiskDelegatorSingle::fileExist(const PageFileIdAndLevel & /*id_lvl*/) const
{
throw Exception("Not Implemented", ErrorCodes::NOT_IMPLEMENTED);
}
size_t PSDiskDelegatorSingle::numPaths() const
{
return 1;
}
String PSDiskDelegatorSingle::defaultPath() const
{
return fmt::format("{}/{}", pool.latest_path_infos[0].path, path_prefix);
}
Strings PSDiskDelegatorSingle::listPaths() const
{
// only stored in the first path.
std::vector<String> paths;
paths.push_back(fmt::format("{}/{}", pool.latest_path_infos[0].path, path_prefix));
return paths;
}
String PSDiskDelegatorSingle::choosePath(const PageFileIdAndLevel & /*id_lvl*/)
{
return fmt::format("{}/{}", pool.latest_path_infos[0].path, path_prefix);
}
size_t PSDiskDelegatorSingle::addPageFileUsedSize(
const PageFileIdAndLevel & /*id_lvl*/,
size_t size_to_add,
const String & pf_parent_path,
bool /*need_insert_location*/)
{
// In this case, inserting to page_path_map seems useless.
// Simply add used size for global capacity is OK.
pool.global_capacity->addUsedSize(pf_parent_path, size_to_add);
return 0;
}
void PSDiskDelegatorSingle::freePageFileUsedSize(
const PageFileIdAndLevel & id_lvl,
size_t size_to_free,
const String & /*pf_parent_path*/)
{
removePageFile(id_lvl, size_to_free, false, false);
}
String PSDiskDelegatorSingle::getPageFilePath(const PageFileIdAndLevel & /*id_lvl*/) const
{
return fmt::format("{}/{}", pool.latest_path_infos[0].path, path_prefix);
}
void PSDiskDelegatorSingle::removePageFile(
const PageFileIdAndLevel & /*id_lvl*/,
size_t file_size,
bool /*meta_left*/,
bool /*remove_from_default_path*/)
{
pool.global_capacity->freeUsedSize(pool.latest_path_infos[0].path, file_size);
}
//==========================================================================================
// Raft Region data
//==========================================================================================
PSDiskDelegatorRaft::PSDiskDelegatorRaft(PathPool & pool_)
: pool(pool_)
{
for (const auto & s : pool.kvstore_paths)
{
RaftPathInfo info;
// Get a normalized path without trailing '/'
info.path = getNormalizedPath(s);
raft_path_infos.emplace_back(info);
}
}
bool PSDiskDelegatorRaft::fileExist(const PageFileIdAndLevel & id_lvl) const
{
return page_path_map.exist(id_lvl);
}
size_t PSDiskDelegatorRaft::numPaths() const
{
return raft_path_infos.size();
}
String PSDiskDelegatorRaft::defaultPath() const
{
return raft_path_infos[default_path_index].path;
}
Strings PSDiskDelegatorRaft::listPaths() const
{
return pool.kvstore_paths;
}
String PSDiskDelegatorRaft::choosePath(const PageFileIdAndLevel & id_lvl)
{
std::function<String(const RaftPathInfos & paths, size_t idx)> path_generator
= [](const RaftPathInfos & paths, size_t idx) -> String {
return paths[idx].path;
};
std::function<String(const RaftPathInfo & info)> path_getter = [](const RaftPathInfo & info) -> String {
return info.path;
};
{
/// If id exists in page_path_map, just return the same path
auto index_opt = page_path_map.getIndex(id_lvl);
if (index_opt.has_value())
return path_generator(raft_path_infos, *index_opt);
}
// Else choose path randomly
const String log_msg = "[type=ps_raft]";
return genericChoosePath(raft_path_infos, pool.global_capacity, path_generator, path_getter, pool.log, log_msg);
}
size_t PSDiskDelegatorRaft::addPageFileUsedSize(
const PageFileIdAndLevel & id_lvl,
size_t size_to_add,
const String & pf_parent_path,
bool need_insert_location)
{
// Get a normalized path without trailing '/'
String upper_path = getNormalizedPath(pf_parent_path);
UInt32 index = UINT32_MAX;
for (size_t i = 0; i < raft_path_infos.size(); i++)
{
if (raft_path_infos[i].path == upper_path)
{
index = i;
break;
}
}
RUNTIME_CHECK_MSG(index != UINT32_MAX, "Unrecognized path {}", upper_path);
if (need_insert_location)
page_path_map.setIndex(id_lvl, index);
// update global used size
pool.global_capacity->addUsedSize(upper_path, size_to_add);
return index;
}
void PSDiskDelegatorRaft::freePageFileUsedSize(
const PageFileIdAndLevel & id_lvl,
size_t size_to_free,
const String & pf_parent_path)
{
String upper_path = getNormalizedPath(pf_parent_path);
UInt32 index = UINT32_MAX;
for (size_t i = 0; i < raft_path_infos.size(); i++)
{
if (raft_path_infos[i].path == upper_path)
{
index = i;
break;
}
}
RUNTIME_CHECK_MSG(index != UINT32_MAX, "Unrecognized path {}", upper_path);
RUNTIME_CHECK_MSG(
page_path_map.exist(id_lvl),
"Can not find path for PageFile, file_id={} path={}",
id_lvl,
pf_parent_path);
// update global used size
pool.global_capacity->freeUsedSize(upper_path, size_to_free);
}
String PSDiskDelegatorRaft::getPageFilePath(const PageFileIdAndLevel & id_lvl) const
{
auto index_opt = page_path_map.getIndex(id_lvl);
RUNTIME_CHECK_MSG(index_opt.has_value(), "Can not find path for PageFile, file_id={}", id_lvl);
return raft_path_infos[*index_opt].path;
}
void PSDiskDelegatorRaft::removePageFile(
const PageFileIdAndLevel & id_lvl,
size_t file_size,
bool meta_left,
bool remove_from_default_path)
{
if (remove_from_default_path)
{
pool.global_capacity->freeUsedSize(raft_path_infos[default_path_index].path, file_size);
}
else
{
auto index_opt = page_path_map.getIndex(id_lvl);
if (unlikely(!index_opt.has_value()))