file_util.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2011-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. //
  6. #pragma once
  7. #include <string>
  8. #include "file/filename.h"
  9. #include "options/db_options.h"
  10. #include "rocksdb/env.h"
  11. #include "rocksdb/file_system.h"
  12. #include "rocksdb/sst_file_writer.h"
  13. #include "rocksdb/statistics.h"
  14. #include "rocksdb/status.h"
  15. #include "rocksdb/system_clock.h"
  16. #include "rocksdb/types.h"
  17. #include "trace_replay/io_tracer.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. // use_fsync maps to options.use_fsync, which determines the way that
  20. // the file is synced after copying.
  21. IOStatus CopyFile(FileSystem* fs, const std::string& source,
  22. Temperature src_temp_hint,
  23. std::unique_ptr<WritableFileWriter>& dest_writer,
  24. uint64_t size, bool use_fsync,
  25. const std::shared_ptr<IOTracer>& io_tracer,
  26. uint64_t max_read_buffer_size = 4096,
  27. const std::optional<IOOptions>& readIOOptions = {},
  28. const std::optional<IOOptions>& writeIOOptions = {});
  29. IOStatus CopyFile(FileSystem* fs, const std::string& source,
  30. Temperature src_temp_hint, const std::string& destination,
  31. Temperature dst_temp, uint64_t size, bool use_fsync,
  32. const std::shared_ptr<IOTracer>& io_tracer,
  33. uint64_t max_read_buffer_size = 4096,
  34. const std::optional<IOOptions>& readIOOptions = {},
  35. const std::optional<IOOptions>& writeIOOptions = {});
  36. inline IOStatus CopyFile(const std::shared_ptr<FileSystem>& fs,
  37. const std::string& source, Temperature src_temp_hint,
  38. const std::string& destination, Temperature dst_temp,
  39. uint64_t size, bool use_fsync,
  40. const std::shared_ptr<IOTracer>& io_tracer,
  41. uint64_t max_read_buffer_size = 4096,
  42. const std::optional<IOOptions>& readIOOptions = {},
  43. const std::optional<IOOptions>& writeIOOptions = {}) {
  44. return CopyFile(fs.get(), source, src_temp_hint, destination, dst_temp, size,
  45. use_fsync, io_tracer, max_read_buffer_size, readIOOptions,
  46. writeIOOptions);
  47. }
  48. IOStatus CreateFile(FileSystem* fs, const std::string& destination,
  49. const std::string& contents, bool use_fsync);
  50. inline IOStatus CreateFile(const std::shared_ptr<FileSystem>& fs,
  51. const std::string& destination,
  52. const std::string& contents, bool use_fsync) {
  53. return CreateFile(fs.get(), destination, contents, use_fsync);
  54. }
  55. // Delete a DB file, if this file is a SST file or Blob file and SstFileManager
  56. // is used, it should have already been tracked by SstFileManager via its
  57. // `OnFileAdd` API before passing to this API to be deleted, to ensure
  58. // SstFileManager and its DeleteScheduler are tracking DB size and trash size
  59. // properly.
  60. Status DeleteDBFile(const ImmutableDBOptions* db_options,
  61. const std::string& fname, const std::string& path_to_sync,
  62. const bool force_bg, const bool force_fg);
  63. // Delete an unaccounted DB file that is not tracked by SstFileManager and will
  64. // not be tracked by its DeleteScheduler when getting deleted.
  65. // If a legitimate bucket is provided and this file is scheduled for slow
  66. // deletion, it will be assigned to the specified trash bucket.
  67. Status DeleteUnaccountedDBFile(const ImmutableDBOptions* db_options,
  68. const std::string& fname,
  69. const std::string& dir_to_sync,
  70. const bool force_bg, const bool force_fg,
  71. std::optional<int32_t> bucket);
  72. // TODO(hx235): pass the whole DBOptions intead of its individual fields
  73. IOStatus GenerateOneFileChecksum(
  74. FileSystem* fs, const std::string& file_path,
  75. FileChecksumGenFactory* checksum_factory,
  76. const std::string& requested_checksum_func_name, std::string* file_checksum,
  77. std::string* file_checksum_func_name,
  78. size_t verify_checksums_readahead_size, bool allow_mmap_reads,
  79. std::shared_ptr<IOTracer>& io_tracer, RateLimiter* rate_limiter,
  80. const ReadOptions& read_options, Statistics* stats, SystemClock* clock);
  81. inline IOStatus PrepareIOFromReadOptions(const ReadOptions& ro,
  82. SystemClock* clock, IOOptions& opts,
  83. IODebugContext* dbg = nullptr) {
  84. if (ro.request_id != nullptr) {
  85. if (dbg != nullptr && dbg->request_id == nullptr) {
  86. dbg->SetRequestId(ro.request_id);
  87. }
  88. }
  89. if (ro.deadline.count()) {
  90. std::chrono::microseconds now =
  91. std::chrono::microseconds(clock->NowMicros());
  92. // Ensure there is atleast 1us available. We don't want to pass a value of
  93. // 0 as that means no timeout
  94. if (now >= ro.deadline) {
  95. return IOStatus::TimedOut("Deadline exceeded");
  96. }
  97. opts.timeout = ro.deadline - now;
  98. }
  99. if (ro.io_timeout.count() &&
  100. (!opts.timeout.count() || ro.io_timeout < opts.timeout)) {
  101. opts.timeout = ro.io_timeout;
  102. }
  103. opts.rate_limiter_priority = ro.rate_limiter_priority;
  104. opts.io_activity = ro.io_activity;
  105. return IOStatus::OK();
  106. }
  107. inline IOStatus PrepareIOFromWriteOptions(const WriteOptions& wo,
  108. IOOptions& opts) {
  109. opts.rate_limiter_priority = wo.rate_limiter_priority;
  110. opts.io_activity = wo.io_activity;
  111. return IOStatus::OK();
  112. }
  113. // Test method to delete the input directory and all of its contents.
  114. // This method is destructive and is meant for use only in tests!!!
  115. Status DestroyDir(Env* env, const std::string& dir);
  116. inline bool CheckFSFeatureSupport(FileSystem* fs, FSSupportedOps feat) {
  117. int64_t supported_ops = 0;
  118. fs->SupportedOps(supported_ops);
  119. if (supported_ops & (1ULL << feat)) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. } // namespace ROCKSDB_NAMESPACE