sync_page_cache_file.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "sync_page_cache_file.h"
  2. #include "record.h"
  3. #include <util/generic/buffer.h>
  4. #include <util/generic/yexception.h>
  5. #include <util/system/file.h>
  6. #include <util/system/info.h>
  7. #include <util/system/mutex.h>
  8. #include <util/system/align.h>
  9. class TSyncPageCacheFileLogBackend::TImpl: public TNonCopyable {
  10. public:
  11. TImpl(const TString& path, size_t maxBufferSize, size_t maxPendingCacheSize)
  12. : File_{OpenFile(path)}
  13. , MaxBufferSize_{maxBufferSize}
  14. , MaxPendingCacheSize_{maxPendingCacheSize}
  15. , Buffer_{maxBufferSize}
  16. {
  17. ResetPtrs();
  18. }
  19. ~TImpl() noexcept {
  20. try {
  21. Write();
  22. FlushSync(GuaranteedWrittenPtr_, WrittenPtr_);
  23. } catch (...) {
  24. }
  25. }
  26. void WriteData(const TLogRecord& rec) {
  27. TGuard guard{Lock_};
  28. Buffer_.Append(rec.Data, rec.Len);
  29. if (Buffer_.size() >= MaxBufferSize_) {
  30. const i64 prevAlignedEndPtr = PageAlignedWrittenPtr_;
  31. Write();
  32. if (prevAlignedEndPtr < PageAlignedWrittenPtr_) {
  33. FlushAsync(prevAlignedEndPtr, PageAlignedWrittenPtr_);
  34. }
  35. const i64 minPendingCacheOffset = PageAlignedWrittenPtr_ - MaxPendingCacheSize_;
  36. if (minPendingCacheOffset > GuaranteedWrittenPtr_) {
  37. FlushSync(GuaranteedWrittenPtr_, minPendingCacheOffset);
  38. }
  39. }
  40. }
  41. void ReopenLog() {
  42. TGuard guard{Lock_};
  43. Write();
  44. FlushSync(GuaranteedWrittenPtr_, WrittenPtr_);
  45. File_.LinkTo(OpenFile(File_.GetName()));
  46. ResetPtrs();
  47. }
  48. private:
  49. void ResetPtrs() {
  50. WrittenPtr_ = File_.GetLength();
  51. PageAlignedWrittenPtr_ = AlignDown(WrittenPtr_, GetPageSize());
  52. GuaranteedWrittenPtr_ = WrittenPtr_;
  53. }
  54. static TFile OpenFile(const TString& path) {
  55. return TFile{path, OpenAlways | WrOnly | ForAppend | Seq | NoReuse};
  56. }
  57. static i64 GetPageSize() {
  58. static const i64 pageSize = NSystemInfo::GetPageSize();
  59. Y_ASSUME(IsPowerOf2(pageSize));
  60. return pageSize;
  61. }
  62. void Write() {
  63. try {
  64. File_.Write(Buffer_.Data(), Buffer_.Size());
  65. WrittenPtr_ += Buffer_.Size();
  66. PageAlignedWrittenPtr_ = AlignDown(WrittenPtr_, GetPageSize());
  67. Buffer_.Clear();
  68. } catch (TFileError&) {
  69. Buffer_.Clear();
  70. throw;
  71. }
  72. }
  73. void FlushAsync(const i64 from, const i64 to) {
  74. File_.FlushCache(from, to - from, /* wait = */ false);
  75. }
  76. void FlushSync(const i64 from, const i64 to) {
  77. const i64 begin = AlignDown(from, GetPageSize());
  78. const i64 end = AlignUp(to, GetPageSize());
  79. const i64 length = end - begin;
  80. File_.FlushCache(begin, length, /* wait = */ true);
  81. File_.EvictCache(begin, length);
  82. GuaranteedWrittenPtr_ = to;
  83. }
  84. private:
  85. TMutex Lock_;
  86. TFile File_;
  87. const size_t MaxBufferSize_ = 0;
  88. const size_t MaxPendingCacheSize_ = 0;
  89. TBuffer Buffer_;
  90. i64 WrittenPtr_ = 0;
  91. i64 PageAlignedWrittenPtr_ = 0;
  92. i64 GuaranteedWrittenPtr_ = 0;
  93. };
  94. TSyncPageCacheFileLogBackend::TSyncPageCacheFileLogBackend(const TString& path, size_t maxBufferSize, size_t maxPengingCacheSize)
  95. : Impl_(MakeHolder<TImpl>(path, maxBufferSize, maxPengingCacheSize))
  96. {}
  97. TSyncPageCacheFileLogBackend::~TSyncPageCacheFileLogBackend() {
  98. }
  99. void TSyncPageCacheFileLogBackend::WriteData(const TLogRecord& rec) {
  100. Impl_->WriteData(rec);
  101. }
  102. void TSyncPageCacheFileLogBackend::ReopenLog() {
  103. Impl_->ReopenLog();
  104. }