fs_on_demand.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2024-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <string>
  7. #include "rocksdb/file_system.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. // A FileSystem that links files to a local (destination) directory from a
  10. // corresponding remote (source) directory on demand. The decision to link
  11. // depends on the file type, with appendable or rename-able files, such as,
  12. // descriptors, logs, CURRENT, being read in place in the remote directory,
  13. // and SST files being linked. In the future, files read in place may be
  14. // mirrored to the local directory, so the local dir has a complete database
  15. // for troubleshooting purposes.
  16. class OnDemandFileSystem : public FileSystemWrapper {
  17. public:
  18. OnDemandFileSystem(const std::shared_ptr<FileSystem>& target,
  19. const std::string& remote_path,
  20. const std::string& local_path)
  21. : FileSystemWrapper(target),
  22. remote_path_(remote_path),
  23. local_path_(local_path) {}
  24. const char* Name() const override { return "OnDemandFileSystem"; }
  25. IOStatus NewSequentialFile(const std::string& fname,
  26. const FileOptions& file_opts,
  27. std::unique_ptr<FSSequentialFile>* result,
  28. IODebugContext* dbg) override;
  29. IOStatus NewRandomAccessFile(const std::string& fname,
  30. const FileOptions& file_opts,
  31. std::unique_ptr<FSRandomAccessFile>* result,
  32. IODebugContext* dbg) override;
  33. IOStatus NewWritableFile(const std::string& fname,
  34. const FileOptions& file_opts,
  35. std::unique_ptr<FSWritableFile>* result,
  36. IODebugContext* dbg) override;
  37. IOStatus ReuseWritableFile(const std::string& /*fname*/,
  38. const std::string& /*old_fname*/,
  39. const FileOptions& /*fopts*/,
  40. std::unique_ptr<FSWritableFile>* /*result*/,
  41. IODebugContext* /*dbg*/) override {
  42. return IOStatus::NotSupported("ReuseWritableFile");
  43. }
  44. IOStatus NewDirectory(const std::string& name, const IOOptions& io_opts,
  45. std::unique_ptr<FSDirectory>* result,
  46. IODebugContext* dbg) override;
  47. IOStatus FileExists(const std::string& fname, const IOOptions& options,
  48. IODebugContext* dbg) override;
  49. IOStatus GetChildren(const std::string& dir, const IOOptions& options,
  50. std::vector<std::string>* result,
  51. IODebugContext* dbg) override;
  52. IOStatus GetChildrenFileAttributes(const std::string& dir,
  53. const IOOptions& options,
  54. std::vector<FileAttributes>* result,
  55. IODebugContext* dbg) override;
  56. IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
  57. uint64_t* file_size, IODebugContext* dbg) override;
  58. private:
  59. bool CheckPathAndAdjust(const std::string& orig, const std::string& replace,
  60. std::string& path);
  61. bool LookupFileType(const std::string& name, FileType* type);
  62. const std::string remote_path_;
  63. const std::string local_path_;
  64. };
  65. // A wrapper class around an FSSequentialFile object. Its mainly
  66. // intended to be used for appendable files like MANIFEST and logs.
  67. // Beneath the covers, it tracks when EOF is reached, and reopens
  68. // the file in order to read the latest appended data. This is
  69. // necessary on some distributed file systems as they may have
  70. // stale metadata about the file.
  71. // TODO: Mirror the data read to a local file for troubleshooting
  72. // purposes, as well as recovery in case the source dir is
  73. // deleted.
  74. class OnDemandSequentialFile : public FSSequentialFile {
  75. public:
  76. OnDemandSequentialFile(std::unique_ptr<FSSequentialFile>&& file,
  77. OnDemandFileSystem* fs, const FileOptions& file_opts,
  78. const std::string& path)
  79. : file_(std::move(file)),
  80. fs_(fs),
  81. file_opts_(file_opts),
  82. path_(path),
  83. eof_(false),
  84. offset_(0) {}
  85. virtual ~OnDemandSequentialFile() {}
  86. IOStatus Read(size_t n, const IOOptions& options, Slice* result,
  87. char* scratch, IODebugContext* dbg) override;
  88. IOStatus Skip(uint64_t n) override;
  89. bool use_direct_io() const override;
  90. size_t GetRequiredBufferAlignment() const override;
  91. IOStatus InvalidateCache(size_t /*offset*/, size_t /*length*/) override {
  92. return IOStatus::NotSupported("InvalidateCache not supported.");
  93. }
  94. IOStatus PositionedRead(uint64_t /*offset*/, size_t /*n*/,
  95. const IOOptions& /*options*/, Slice* /*result*/,
  96. char* /*scratch*/, IODebugContext* /*dbg*/) override {
  97. return IOStatus::NotSupported("PositionedRead");
  98. }
  99. Temperature GetTemperature() const override;
  100. private:
  101. std::unique_ptr<FSSequentialFile> file_;
  102. OnDemandFileSystem* fs_;
  103. const FileOptions file_opts_;
  104. const std::string path_;
  105. bool eof_;
  106. uint64_t offset_;
  107. };
  108. std::shared_ptr<FileSystem> NewOnDemandFileSystem(
  109. const std::shared_ptr<FileSystem>& fs, std::string remote_path,
  110. std::string local_path);
  111. } // namespace ROCKSDB_NAMESPACE