-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathPageFile.cpp
1089 lines (945 loc) · 38.9 KB
/
PageFile.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
#include <Common/CurrentMetrics.h>
#include <Common/Exception.h>
#include <Common/FailPoint.h>
#include <Common/ProfileEvents.h>
#include <Common/StringUtils/StringUtils.h>
#include <IO/WriteHelpers.h>
#include <common/logger_useful.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#ifndef __APPLE__
#include <fcntl.h>
#endif
#include <IO/WriteBufferFromFile.h>
#include <Poco/File.h>
#include <Storages/Page/PageFile.h>
#include <Storages/Page/PageUtil.h>
#include <ext/scope_guard.h>
namespace CurrentMetrics
{
extern const Metric OpenFileForRead;
}
namespace DB
{
// =========================================================
// Page Meta format
// =========================================================
namespace FailPoints
{
extern const char exception_before_page_file_write_sync[];
extern const char force_formal_page_file_not_exists[];
extern const char force_legacy_or_checkpoint_page_file_exists[];
} // namespace FailPoints
static constexpr bool PAGE_CHECKSUM_ON_READ = true;
#ifndef O_DIRECT
#define O_DIRECT 00040000
#endif
namespace PageMetaFormat
{
using WBSize = UInt32;
// TODO we should align these alias with type in PageCache
using PageTag = UInt64;
using IsPut = std::underlying_type<WriteBatch::WriteType>::type;
using PageOffset = UInt64;
using Checksum = UInt64;
struct PageFlags
{
UInt32 flags = 0x00000000;
// Detach page means the meta and data not in the same PageFile
void setIsDetachPage() { flags |= 0x1; }
bool isDetachPage() const { return flags & 0x1; }
};
static_assert(std::is_trivially_copyable_v<PageFlags>);
static_assert(sizeof(PageFlags) == sizeof(UInt32));
static const size_t PAGE_META_SIZE = sizeof(PageId) + sizeof(PageFileId) + sizeof(PageFileLevel) + sizeof(PageFlags) + sizeof(PageTag)
+ sizeof(PageOffset) + sizeof(PageSize) + sizeof(Checksum);
/// Return <data to write into meta file, data to write into data file>.
std::pair<ByteBuffer, ByteBuffer> genWriteData( //
WriteBatch & wb,
PageFile & page_file,
PageEntriesEdit & edit)
{
WBSize meta_write_bytes = 0;
size_t data_write_bytes = 0;
meta_write_bytes += sizeof(WBSize) + sizeof(PageFormat::Version) + sizeof(WriteBatch::SequenceID);
for (const auto & write : wb.getWrites())
{
meta_write_bytes += sizeof(IsPut);
switch (write.type)
{
case WriteBatch::WriteType::PUT:
case WriteBatch::WriteType::UPSERT:
if (write.read_buffer)
data_write_bytes += write.size;
meta_write_bytes += PAGE_META_SIZE;
meta_write_bytes += sizeof(UInt64); // size of field_offsets + checksum
meta_write_bytes += ((sizeof(UInt64) + sizeof(UInt64)) * write.offsets.size());
break;
case WriteBatch::WriteType::DEL:
// For delete page, store page id only. And don't need to write data file.
meta_write_bytes += sizeof(PageId);
break;
case WriteBatch::WriteType::REF:
// For ref page, store RefPageId -> PageId. And don't need to write data file.
meta_write_bytes += (sizeof(PageId) + sizeof(PageId));
break;
}
}
meta_write_bytes += sizeof(Checksum);
char * meta_buffer = (char *)page_file.alloc(meta_write_bytes);
char * data_buffer = (char *)page_file.alloc(data_write_bytes);
char * meta_pos = meta_buffer;
char * data_pos = data_buffer;
PageUtil::put(meta_pos, meta_write_bytes);
PageUtil::put(meta_pos, STORAGE_FORMAT_CURRENT.page);
PageUtil::put(meta_pos, wb.getSequence());
PageOffset page_data_file_off = page_file.getDataFileAppendPos();
for (auto & write : wb.getWrites())
{
PageUtil::put(meta_pos, static_cast<IsPut>(write.type));
switch (write.type)
{
case WriteBatch::WriteType::PUT:
case WriteBatch::WriteType::UPSERT: {
PageFlags flags;
Checksum page_checksum = 0;
PageOffset page_offset = 0;
if (write.read_buffer)
{
write.read_buffer->readStrict(data_pos, write.size);
page_checksum = CityHash_v1_0_2::CityHash64(data_pos, write.size);
page_offset = page_data_file_off;
// In this case, checksum of each fields (inside `write.offsets[i].second`)
// is simply 0, we need to calulate the checksums of each fields
for (size_t i = 0; i < write.offsets.size(); ++i)
{
const auto field_beg = write.offsets[i].first;
const auto field_end = (i == write.offsets.size() - 1) ? write.size : write.offsets[i + 1].first;
write.offsets[i].second = CityHash_v1_0_2::CityHash64(data_pos + field_beg, field_end - field_beg);
}
data_pos += write.size;
page_data_file_off += write.size;
}
else
{
// Notice: in this case, we need to copy checksum instead of calculate from buffer(which is null)
// Do NOT use `data_pos` outside this if-else branch
// get page_checksum from write when read_buffer is nullptr
flags.setIsDetachPage();
page_checksum = write.page_checksum;
page_offset = write.page_offset;
// `entry.field_offsets`(and checksum) just simply copy `write.offsets`
// page_data_file_off += 0;
}
// UPSERT may point to another PageFile
PageEntry entry;
entry.file_id = (write.type == WriteBatch::WriteType::PUT ? page_file.getFileId() : write.target_file_id.first);
entry.level = (write.type == WriteBatch::WriteType::PUT ? page_file.getLevel() : write.target_file_id.second);
entry.tag = write.tag;
entry.size = write.size;
entry.offset = page_offset;
entry.checksum = page_checksum;
// entry.field_offsets = write.offsets;
// we can swap from WriteBatch instead of copying
entry.field_offsets.swap(write.offsets);
PageUtil::put(meta_pos, (PageId)write.page_id);
PageUtil::put(meta_pos, (PageFileId)entry.file_id);
PageUtil::put(meta_pos, (PageFileLevel)entry.level);
PageUtil::put(meta_pos, (PageFlags)flags);
PageUtil::put(meta_pos, (PageTag)write.tag);
PageUtil::put(meta_pos, (PageOffset)entry.offset);
PageUtil::put(meta_pos, (PageSize)write.size);
PageUtil::put(meta_pos, (Checksum)page_checksum);
PageUtil::put(meta_pos, (UInt64)entry.field_offsets.size());
for (size_t i = 0; i < entry.field_offsets.size(); ++i)
{
PageUtil::put(meta_pos, (UInt64)entry.field_offsets[i].first);
PageUtil::put(meta_pos, (UInt64)entry.field_offsets[i].second);
}
if (write.type == WriteBatch::WriteType::PUT)
edit.put(write.page_id, entry);
else if (write.type == WriteBatch::WriteType::UPSERT)
edit.upsertPage(write.page_id, entry);
break;
}
case WriteBatch::WriteType::DEL:
PageUtil::put(meta_pos, (PageId)write.page_id);
edit.del(write.page_id);
break;
case WriteBatch::WriteType::REF:
PageUtil::put(meta_pos, static_cast<PageId>(write.page_id));
PageUtil::put(meta_pos, static_cast<PageId>(write.ori_page_id));
edit.ref(write.page_id, write.ori_page_id);
break;
}
}
const Checksum wb_checksum = CityHash_v1_0_2::CityHash64(meta_buffer, meta_write_bytes - sizeof(Checksum));
PageUtil::put(meta_pos, wb_checksum);
if (unlikely(meta_pos != meta_buffer + meta_write_bytes || data_pos != data_buffer + data_write_bytes))
throw Exception("pos not match", ErrorCodes::LOGICAL_ERROR);
return {{meta_buffer, meta_pos}, {data_buffer, data_pos}};
}
} // namespace PageMetaFormat
// =========================================================
// PageFile::MetaMergingReader
// =========================================================
PageFile::MetaMergingReader::~MetaMergingReader()
{
page_file.free(meta_buffer, meta_size);
}
// Try to initiallize access to meta, read the whole metadata to memory.
// Status -> Finished if metadata size is zero.
// -> Opened if metadata successfully load from disk.
void PageFile::MetaMergingReader::initialize()
{
if (status == Status::Opened)
return;
else if (status == Status::Finished)
throw Exception("Try to reopen finished merging reader: " + toString(), ErrorCodes::LOGICAL_ERROR);
const auto path = page_file.metaPath();
Poco::File file(path);
if (unlikely(!file.exists()))
throw Exception("Try to read meta of " + page_file.toString() + ", but not exists. Path: " + path, ErrorCodes::LOGICAL_ERROR);
meta_size = file.getSize();
if (meta_size == 0) // Empty file
{
status = Status::Finished;
return;
}
auto underlying_file = page_file.file_provider->newRandomAccessFile(path, page_file.metaEncryptionPath());
// File not exists.
if (unlikely(underlying_file->getFd() == -1))
throw Exception("Try to read meta of " + page_file.toString() + ", but open file error. Path: " + path, ErrorCodes::LOGICAL_ERROR);
SCOPE_EXIT({ underlying_file->close(); });
meta_buffer = (char *)page_file.alloc(meta_size);
PageUtil::readFile(underlying_file, 0, meta_buffer, meta_size);
status = Status::Opened;
}
bool PageFile::MetaMergingReader::hasNext() const
{
return (status == Status::Uninitialized) || (status == Status::Opened && meta_file_offset < meta_size);
}
void PageFile::MetaMergingReader::moveNext(PageFormat::Version * v)
{
curr_edit.clear();
curr_write_batch_sequence = 0;
if (status == Status::Uninitialized)
initialize();
// Note that we need to check if status is finished after initialize.
if (status == Status::Finished)
return;
char * meta_data_end = meta_buffer + meta_size;
char * pos = meta_buffer + meta_file_offset;
if (pos + sizeof(PageMetaFormat::WBSize) > meta_data_end)
{
LOG_WARNING(page_file.log,
"Incomplete write batch {" << toString() << "} [batch_start_pos=" << meta_file_offset << "] [meta_size=" << meta_size
<< "] [file=" << page_file.metaPath() << "] ignored.");
status = Status::Finished;
return;
}
const char * wb_start_pos = pos;
const auto wb_bytes = PageUtil::get<PageMetaFormat::WBSize>(pos);
if (wb_start_pos + wb_bytes > meta_data_end)
{
LOG_WARNING(page_file.log,
"Incomplete write batch {" << toString() << "} [expect_batch_bytes=" << wb_bytes << "] [meta_size=" << meta_size
<< "] [file=" << page_file.metaPath() << "] ignored.");
status = Status::Finished;
return;
}
WriteBatch::SequenceID wb_sequence = 0;
const auto binary_version = PageUtil::get<PageFormat::Version>(pos);
switch (binary_version)
{
case PageFormat::V1:
wb_sequence = 0;
break;
case PageFormat::V2:
wb_sequence = PageUtil::get<WriteBatch::SequenceID>(pos);
break;
default:
throw Exception("PageFile binary version not match {" + toString() + "} [unknown_version=" + DB::toString(binary_version)
+ "] [file=" + page_file.metaPath() + "]",
ErrorCodes::LOGICAL_ERROR);
}
// return the binary_version if `v` is not null
if (unlikely(v != nullptr))
*v = binary_version;
// check the checksum of WriteBatch
const auto wb_bytes_without_checksum = wb_bytes - sizeof(PageMetaFormat::Checksum);
const auto wb_checksum = PageUtil::get<PageMetaFormat::Checksum, false>(wb_start_pos + wb_bytes_without_checksum);
const auto checksum_calc = CityHash_v1_0_2::CityHash64(wb_start_pos, wb_bytes_without_checksum);
if (wb_checksum != checksum_calc)
{
std::stringstream ss;
ss << "[expecte_checksum=" << std::hex << wb_checksum << "] [actual_checksum" << checksum_calc << "]";
throw Exception("Write batch checksum not match {" + toString() + "} [path=" + page_file.folderPath()
+ "] [batch_bytes=" + DB::toString(wb_bytes) + "] " + ss.str(),
ErrorCodes::CHECKSUM_DOESNT_MATCH);
}
// recover WriteBatch
size_t curr_wb_data_offset = 0;
while (pos < wb_start_pos + wb_bytes_without_checksum)
{
const auto write_type = static_cast<WriteBatch::WriteType>(PageUtil::get<PageMetaFormat::IsPut>(pos));
switch (write_type)
{
case WriteBatch::WriteType::PUT:
case WriteBatch::WriteType::UPSERT: {
PageMetaFormat::PageFlags flags;
auto page_id = PageUtil::get<PageId>(pos);
PageEntry entry;
switch (binary_version)
{
case PageFormat::V1: {
entry.file_id = page_file.getFileId();
entry.level = page_file.getLevel();
break;
}
case PageFormat::V2: {
entry.file_id = PageUtil::get<PageFileId>(pos);
entry.level = PageUtil::get<PageFileLevel>(pos);
flags = PageUtil::get<PageMetaFormat::PageFlags>(pos);
break;
}
default:
throw Exception("PageFile binary version not match {" + toString() + "} [unknown_version=" + DB::toString(binary_version)
+ "] [file=" + page_file.metaPath() + "]",
ErrorCodes::LOGICAL_ERROR);
}
entry.tag = PageUtil::get<PageMetaFormat::PageTag>(pos);
entry.offset = PageUtil::get<PageMetaFormat::PageOffset>(pos);
entry.size = PageUtil::get<PageSize>(pos);
entry.checksum = PageUtil::get<PageMetaFormat::Checksum>(pos);
if (binary_version == PageFormat::V2)
{
const UInt64 num_fields = PageUtil::get<UInt64>(pos);
entry.field_offsets.reserve(num_fields);
for (size_t i = 0; i < num_fields; ++i)
{
auto field_offset = PageUtil::get<UInt64>(pos);
auto field_checksum = PageUtil::get<UInt64>(pos);
entry.field_offsets.emplace_back(field_offset, field_checksum);
}
}
if (write_type == WriteBatch::WriteType::PUT)
{
curr_edit.put(page_id, entry);
curr_wb_data_offset += entry.size;
}
else if (write_type == WriteBatch::WriteType::UPSERT)
{
curr_edit.upsertPage(page_id, entry);
// If it is a deatch page, don't move data offset.
curr_wb_data_offset += flags.isDetachPage() ? 0 : entry.size;
}
break;
}
case WriteBatch::WriteType::DEL: {
auto page_id = PageUtil::get<PageId>(pos);
curr_edit.del(page_id);
break;
}
case WriteBatch::WriteType::REF: {
const auto ref_id = PageUtil::get<PageId>(pos);
const auto page_id = PageUtil::get<PageId>(pos);
curr_edit.ref(ref_id, page_id);
break;
}
}
}
// move `pos` over the checksum of WriteBatch
pos += sizeof(PageMetaFormat::Checksum);
if (unlikely(pos != wb_start_pos + wb_bytes))
throw Exception("pos not match {" + toString() + "} [batch_bytes=" + DB::toString(wb_bytes)
+ "] [actual_bytes=" + DB::toString(pos - wb_start_pos) + "] [file=" + page_file.metaPath() + "]",
ErrorCodes::LOGICAL_ERROR);
curr_write_batch_sequence = wb_sequence;
meta_file_offset = pos - meta_buffer;
data_file_offset += curr_wb_data_offset;
}
// =========================================================
// PageFile::Writer
// =========================================================
PageFile::Writer::Writer(PageFile & page_file_, bool sync_on_write_, bool truncate_if_exists)
: page_file(page_file_), sync_on_write(sync_on_write_), data_file{nullptr}, meta_file{nullptr}, last_write_time(Clock::now())
{
// Create data and meta file, prevent empty page folder from being removed by GC.
data_file = page_file.file_provider->newWritableFile(
page_file.dataPath(), page_file.dataEncryptionPath(), truncate_if_exists, /*create_new_encryption_info_*/ truncate_if_exists);
meta_file = page_file.file_provider->newWritableFile(
page_file.metaPath(), page_file.metaEncryptionPath(), truncate_if_exists, /*create_new_encryption_info_*/ truncate_if_exists);
data_file->close();
meta_file->close();
}
PageFile::Writer::~Writer()
{
if (data_file->isClosed())
return;
closeFd();
}
const String & PageFile::Writer::parentPath() const
{
return page_file.parent_path;
}
size_t PageFile::Writer::write(WriteBatch & wb, PageEntriesEdit & edit, const RateLimiterPtr & rate_limiter)
{
ProfileEvents::increment(ProfileEvents::PSMWritePages, wb.putWriteCount());
if (data_file->isClosed())
{
data_file->open();
meta_file->open();
}
// TODO: investigate if not copy data into heap, write big pages can be faster?
ByteBuffer meta_buf, data_buf;
std::tie(meta_buf, data_buf) = PageMetaFormat::genWriteData(wb, page_file, edit);
SCOPE_EXIT({ page_file.free(meta_buf.begin(), meta_buf.size()); });
SCOPE_EXIT({ page_file.free(data_buf.begin(), data_buf.size()); });
#ifndef NDEBUG
auto write_buf = [&](WritableFilePtr & file, UInt64 offset, ByteBuffer buf, bool enable_failpoint) {
PageUtil::writeFile(file, offset, buf.begin(), buf.size(), rate_limiter, enable_failpoint);
if (sync_on_write)
PageUtil::syncFile(file);
};
write_buf(data_file, page_file.data_file_pos, data_buf, false);
write_buf(meta_file, page_file.meta_file_pos, meta_buf, true);
#else
auto write_buf = [&](WritableFilePtr & file, UInt64 offset, ByteBuffer buf) {
PageUtil::writeFile(file, offset, buf.begin(), buf.size(), rate_limiter);
if (sync_on_write)
PageUtil::syncFile(file);
};
write_buf(data_file, page_file.data_file_pos, data_buf);
write_buf(meta_file, page_file.meta_file_pos, meta_buf);
#endif
fiu_do_on(FailPoints::exception_before_page_file_write_sync,
{ // Mock that writing page file meta is not completed
auto f = Poco::File(meta_file->getFileName());
auto size = f.getSize();
f.setSize(size - 2);
auto size_after = f.getSize();
LOG_WARNING(page_file.log,
"Failpoint truncate [file=" << meta_file->getFileName() << "] [origin_size=" << size
<< "] [truncated_size=" << size_after << "]");
throw Exception(String("Fail point ") + FailPoints::exception_before_page_file_write_sync + " is triggered.",
ErrorCodes::FAIL_POINT_ERROR);
});
page_file.data_file_pos += data_buf.size();
page_file.meta_file_pos += meta_buf.size();
last_write_time = Clock::now();
// return how may bytes written
return data_buf.size() + meta_buf.size();
}
void PageFile::Writer::tryCloseIdleFd(const Seconds & max_idle_time)
{
if (max_idle_time.count() == 0 || data_file->isClosed())
return;
if (Clock::now() - last_write_time >= max_idle_time)
closeFd();
}
PageFileIdAndLevel PageFile::Writer::fileIdLevel() const
{
return page_file.fileIdLevel();
}
void PageFile::Writer::closeFd()
{
if (data_file->isClosed())
return;
SCOPE_EXIT({
data_file->close();
meta_file->close();
});
PageUtil::syncFile(data_file);
PageUtil::syncFile(meta_file);
}
// =========================================================
// PageFile::Reader
// =========================================================
PageFile::Reader::Reader(PageFile & page_file)
: data_file_path(page_file.dataPath()),
data_file{page_file.file_provider->newRandomAccessFile(page_file.dataPath(), page_file.dataEncryptionPath())},
last_read_time(Clock::now())
{
}
PageFile::Reader::~Reader()
{
data_file->close();
}
PageMap PageFile::Reader::read(PageIdAndEntries & to_read)
{
ProfileEvents::increment(ProfileEvents::PSMReadPages, to_read.size());
// Sort in ascending order by offset in file.
std::sort(to_read.begin(), to_read.end(), [](const PageIdAndEntry & a, const PageIdAndEntry & b) {
return a.second.offset < b.second.offset;
});
// allocate data_buf that can hold all pages
size_t buf_size = 0;
for (const auto & p : to_read)
{
buf_size += p.second.size;
}
// TODO optimization:
// 1. Succeeding pages can be read by one call.
// 2. Pages with small gaps between them can also read together.
// 3. Refactor this function to support iterator mode, and then use hint to do data pre-read.
char * data_buf = (char *)alloc(buf_size);
MemHolder mem_holder = createMemHolder(data_buf, [&, buf_size](char * p) { free(p, buf_size); });
char * pos = data_buf;
PageMap page_map;
for (const auto & [page_id, entry] : to_read)
{
PageUtil::readFile(data_file, entry.offset, pos, entry.size);
if constexpr (PAGE_CHECKSUM_ON_READ)
{
auto checksum = CityHash_v1_0_2::CityHash64(pos, entry.size);
if (unlikely(entry.size != 0 && checksum != entry.checksum))
{
std::stringstream ss;
ss << ", expected: " << std::hex << entry.checksum << ", but: " << checksum;
throw Exception("Page [" + DB::toString(page_id) + "] checksum not match, broken file: " + data_file_path + ss.str(),
ErrorCodes::CHECKSUM_DOESNT_MATCH);
}
}
Page page;
page.page_id = page_id;
page.data = ByteBuffer(pos, pos + entry.size);
page.mem_holder = mem_holder;
page_map.emplace(page_id, page);
pos += entry.size;
}
if (unlikely(pos != data_buf + buf_size))
throw Exception("pos not match", ErrorCodes::LOGICAL_ERROR);
last_read_time = Clock::now();
return page_map;
}
void PageFile::Reader::read(PageIdAndEntries & to_read, const PageHandler & handler)
{
ProfileEvents::increment(ProfileEvents::PSMReadPages, to_read.size());
// Sort in ascending order by offset in file.
std::sort(to_read.begin(), to_read.end(), [](const PageIdAndEntry & a, const PageIdAndEntry & b) {
return a.second.offset < b.second.offset;
});
size_t buf_size = 0;
for (const auto & p : to_read)
buf_size = std::max(buf_size, p.second.size);
char * data_buf = (char *)alloc(buf_size);
MemHolder mem_holder = createMemHolder(data_buf, [&, buf_size](char * p) { free(p, buf_size); });
auto it = to_read.begin();
while (it != to_read.end())
{
auto && [page_id, entry] = *it;
PageUtil::readFile(data_file, entry.offset, data_buf, entry.size);
if constexpr (PAGE_CHECKSUM_ON_READ)
{
auto checksum = CityHash_v1_0_2::CityHash64(data_buf, entry.size);
if (unlikely(entry.size != 0 && checksum != entry.checksum))
{
std::stringstream ss;
ss << ", expected: " << std::hex << entry.checksum << ", but: " << checksum;
throw Exception("Page [" + DB::toString(page_id) + "] checksum not match, broken file: " + data_file_path + ss.str(),
ErrorCodes::CHECKSUM_DOESNT_MATCH);
}
}
Page page;
page.page_id = page_id;
page.data = ByteBuffer(data_buf, data_buf + entry.size);
page.mem_holder = mem_holder;
++it;
//#ifndef __APPLE__
// if (it != to_read.end())
// {
// auto & next_page_cache = it->second;
// ::posix_fadvise(data_file_fd, next_page_cache.offset, next_page_cache.size, POSIX_FADV_WILLNEED);
// }
//#endif
handler(page_id, page);
}
last_read_time = Clock::now();
}
PageMap PageFile::Reader::read(PageFile::Reader::FieldReadInfos & to_read)
{
ProfileEvents::increment(ProfileEvents::PSMReadPages, to_read.size());
// Sort in ascending order by offset in file.
std::sort(
to_read.begin(), to_read.end(), [](const FieldReadInfo & a, const FieldReadInfo & b) { return a.entry.offset < b.entry.offset; });
// allocate data_buf that can hold all pages with specify fields
size_t buf_size = 0;
for (auto & [page_id, entry, fields] : to_read)
{
(void)page_id;
(void)entry;
// Sort fields to get better read on disk
std::sort(fields.begin(), fields.end());
for (const auto field_index : fields)
{
buf_size += entry.getFieldSize(field_index);
}
}
// TODO optimization:
// 1. Succeeding pages can be read by one call.
// 2. Pages with small gaps between them can also read together.
// 3. Refactor this function to support iterator mode, and then use hint to do data pre-read.
char * data_buf = (char *)alloc(buf_size);
MemHolder mem_holder = createMemHolder(data_buf, [&, buf_size](char * p) { free(p, buf_size); });
std::set<Page::FieldOffset> fields_offset_in_page;
char * pos = data_buf;
PageMap page_map;
for (const auto & [page_id, entry, fields] : to_read)
{
size_t read_size_this_entry = 0;
char * write_offset = pos;
for (const auto field_index : fields)
{
// TODO: Continuously fields can read by one system call.
const auto [beg_offset, end_offset] = entry.getFieldOffsets(field_index);
const auto size_to_read = end_offset - beg_offset;
PageUtil::readFile(data_file, entry.offset + beg_offset, write_offset, size_to_read);
fields_offset_in_page.emplace(field_index, read_size_this_entry);
if constexpr (PAGE_CHECKSUM_ON_READ)
{
auto expect_checksum = entry.field_offsets[field_index].second;
auto field_checksum = CityHash_v1_0_2::CityHash64(write_offset, size_to_read);
if (unlikely(entry.size != 0 && field_checksum != expect_checksum))
{
std::stringstream ss;
ss << ", expected: " << std::hex << expect_checksum << ", but: " << field_checksum;
throw Exception("Page[" + DB::toString(page_id) + "] field[" + DB::toString(field_index)
+ "] checksum not match, broken file: " + data_file_path + ss.str(),
ErrorCodes::CHECKSUM_DOESNT_MATCH);
}
}
read_size_this_entry += size_to_read;
write_offset += size_to_read;
}
Page page;
page.page_id = page_id;
page.data = ByteBuffer(pos, write_offset);
page.mem_holder = mem_holder;
page.field_offsets.swap(fields_offset_in_page);
fields_offset_in_page.clear();
page_map.emplace(page_id, std::move(page));
pos = write_offset;
}
if (unlikely(pos != data_buf + buf_size))
throw Exception("Pos not match, expect to read " + DB::toString(buf_size) + " bytes, but only " + DB::toString(pos - data_buf),
ErrorCodes::LOGICAL_ERROR);
last_read_time = Clock::now();
return page_map;
}
bool PageFile::Reader::isIdle(const Seconds & max_idle_time)
{
if (max_idle_time.count() == 0)
return false;
return (Clock::now() - last_read_time >= max_idle_time);
}
// =========================================================
// PageFile
// =========================================================
PageFile::PageFile(PageFileId file_id_,
UInt32 level_,
const std::string & parent_path,
const FileProviderPtr & file_provider_,
PageFile::Type type_,
bool is_create,
Logger * log_)
: file_id(file_id_),
level(level_),
type(type_),
parent_path(parent_path),
file_provider{file_provider_},
data_file_pos(0),
meta_file_pos(0),
log(log_)
{
if (is_create)
{
Poco::File file(folderPath());
if (file.exists())
{
deleteEncryptionInfo();
file.remove(true);
}
file.createDirectories();
}
}
std::pair<PageFile, PageFile::Type>
PageFile::recover(const String & parent_path, const FileProviderPtr & file_provider_, const String & page_file_name, Logger * log)
{
if (!startsWith(page_file_name, folder_prefix_formal) && !startsWith(page_file_name, folder_prefix_temp)
&& !startsWith(page_file_name, folder_prefix_legacy) && !startsWith(page_file_name, folder_prefix_checkpoint))
{
LOG_INFO(log, "Not page file, ignored " + page_file_name);
return {{}, Type::Invalid};
}
std::vector<std::string> ss;
boost::split(ss, page_file_name, boost::is_any_of("_"));
if (ss.size() != 3)
{
LOG_INFO(log, "Unrecognized file, ignored: " + page_file_name);
return {{}, Type::Invalid};
}
PageFileId file_id = std::stoull(ss[1]);
UInt32 level = std::stoi(ss[2]);
PageFile pf(file_id, level, parent_path, file_provider_, Type::Formal, /* is_create */ false, log);
if (ss[0] == folder_prefix_temp)
{
LOG_INFO(log, "Temporary page file, ignored: " + page_file_name);
pf.type = Type::Temp;
return {pf, Type::Temp};
}
else if (ss[0] == folder_prefix_legacy)
{
pf.type = Type::Legacy;
// ensure meta exist
if (!Poco::File(pf.metaPath()).exists())
{
LOG_INFO(log, "Broken page without meta file, ignored: " + pf.metaPath());
return {{}, Type::Invalid};
}
return {pf, Type::Legacy};
}
else if (ss[0] == folder_prefix_formal)
{
// ensure both meta && data exist
if (!Poco::File(pf.metaPath()).exists())
{
LOG_INFO(log, "Broken page without meta file, ignored: " + pf.metaPath());
return {{}, Type::Invalid};
}
if (!Poco::File(pf.dataPath()).exists())
{
LOG_INFO(log, "Broken page without data file, ignored: " + pf.dataPath());
return {{}, Type::Invalid};
}
return {pf, Type::Formal};
}
else if (ss[0] == folder_prefix_checkpoint)
{
pf.type = Type::Checkpoint;
if (!Poco::File(pf.metaPath()).exists())
{
LOG_INFO(log, "Broken page without meta file, ignored: " + pf.metaPath());
return {{}, Type::Invalid};
}
pf.type = Type::Checkpoint;
return {pf, Type::Checkpoint};
}
LOG_INFO(log, "Unrecognized file prefix, ignored: " + page_file_name);
return {{}, Type::Invalid};
}
PageFile PageFile::newPageFile(PageFileId file_id,
UInt32 level,
const std::string & parent_path,
const FileProviderPtr & file_provider_,
PageFile::Type type,
Logger * log)
{
#ifndef NDEBUG
// PageStorage may create a "Formal" PageFile for writing,
// or a "Temp" PageFile for gc data.
if (type != PageFile::Type::Temp && type != PageFile::Type::Formal)
throw Exception("Should not create page file with type: " + typeToString(type));
#endif
return PageFile(file_id, level, parent_path, file_provider_, type, true, log);
}
PageFile PageFile::openPageFileForRead(PageFileId file_id,
UInt32 level,
const std::string & parent_path,
const FileProviderPtr & file_provider_,
PageFile::Type type,
Logger * log)
{
return PageFile(file_id, level, parent_path, file_provider_, type, false, log);
}
bool PageFile::isPageFileExist(
PageFileIdAndLevel file_id, const String & parent_path, const FileProviderPtr & file_provider_, Type type, Poco::Logger * log)
{
PageFile pf = openPageFileForRead(file_id.first, file_id.second, parent_path, file_provider_, type, log);
return pf.isExist();
}
void PageFile::setFileAppendPos(size_t meta_pos, size_t data_pos)
{
meta_file_pos = meta_pos;
data_file_pos = data_pos;
// If `meta_file_pos` is less than the file size on disk, it must
// be some incomplete write batches meta been ignored.
// Truncate the meta file size to throw those broken meta away.
Poco::File meta_on_disk(metaPath());
const auto meta_size_on_disk = meta_on_disk.getSize();
if (unlikely(meta_size_on_disk != meta_pos))
{
LOG_WARNING(log,
"Truncate incomplete write batches"
" [orig_size="
<< meta_size_on_disk << "] [set_size=" << meta_file_pos << "] [file=" << metaPath() << "]");
meta_on_disk.setSize(meta_file_pos);
}
}
void PageFile::setFormal()
{
if (type != Type::Temp)
return;
auto old_meta_encryption_path = metaEncryptionPath();
auto old_data_encryption_path = dataEncryptionPath();
Poco::File file(folderPath());
type = Type::Formal;
file_provider->linkEncryptionInfo(old_meta_encryption_path, metaEncryptionPath());
file_provider->linkEncryptionInfo(old_data_encryption_path, dataEncryptionPath());
try
{
file.renameTo(folderPath());
}
catch (Poco::Exception & e)
{
throw DB::Exception(e); // wrap Poco::Exception as DB::Exception for better stack backtrace
}
file_provider->deleteEncryptionInfo(old_meta_encryption_path);
file_provider->deleteEncryptionInfo(old_data_encryption_path);
}
size_t PageFile::setLegacy()
{
if (type != Type::Formal)
return 0;
// Rename to legacy dir. Note that we can NOT remove the data part before
// successfully rename to legacy status.
auto old_meta_encryption_path = metaEncryptionPath();
auto old_data_encryption_path = dataEncryptionPath();
Poco::File formal_dir(folderPath());
type = Type::Legacy;
file_provider->linkEncryptionInfo(old_meta_encryption_path, metaEncryptionPath());
try
{
formal_dir.renameTo(folderPath());
}
catch (Poco::Exception & e)
{
throw DB::Exception(e); // wrap Poco::Exception as DB::Exception for better stack backtrace
}
file_provider->deleteEncryptionInfo(old_meta_encryption_path);
file_provider->deleteEncryptionInfo(old_data_encryption_path);
// remove the data part
return removeDataIfExists();
}
size_t PageFile::setCheckpoint()
{
if (type != Type::Temp)
return 0;
{
// The data part of checkpoint file should be empty.
const auto data_size = getDataFileSize();
if (data_size != 0)
throw Exception("Setting " + toString() + " to checkpoint, but data size is not zero: " + DB::toString(data_size)
+ ", path: " + folderPath(),
ErrorCodes::LOGICAL_ERROR);
}
auto old_meta_encryption_path = metaEncryptionPath();
Poco::File file(folderPath());
type = Type::Checkpoint;
file_provider->linkEncryptionInfo(old_meta_encryption_path, metaEncryptionPath());
try
{
file.renameTo(folderPath());
}
catch (Poco::Exception & e)
{
throw DB::Exception(e); // wrap Poco::Exception as DB::Exception for better stack backtrace
}
file_provider->deleteEncryptionInfo(old_meta_encryption_path);
// Remove the data part, should be an emtpy file.
return removeDataIfExists();
}
size_t PageFile::removeDataIfExists() const
{
size_t bytes_removed = 0;
if (auto data_file = Poco::File(dataPath()); data_file.exists())
{
bytes_removed = data_file.getSize();
file_provider->deleteRegularFile(dataPath(), dataEncryptionPath());
}
return bytes_removed;
}
void PageFile::destroy() const
{
Poco::File file(folderPath());
if (file.exists())