env.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "rocksdb/env.h"
  10. #include <thread>
  11. #include "env/composite_env_wrapper.h"
  12. #include "logging/env_logger.h"
  13. #include "memory/arena.h"
  14. #include "options/db_options.h"
  15. #include "port/port.h"
  16. #include "port/sys_time.h"
  17. #include "rocksdb/options.h"
  18. #include "rocksdb/utilities/object_registry.h"
  19. #include "util/autovector.h"
  20. namespace ROCKSDB_NAMESPACE {
  21. Env::~Env() {
  22. }
  23. Status Env::NewLogger(const std::string& fname,
  24. std::shared_ptr<Logger>* result) {
  25. return NewEnvLogger(fname, this, result);
  26. }
  27. Status Env::LoadEnv(const std::string& value, Env** result) {
  28. Env* env = *result;
  29. Status s;
  30. #ifndef ROCKSDB_LITE
  31. s = ObjectRegistry::NewInstance()->NewStaticObject<Env>(value, &env);
  32. #else
  33. s = Status::NotSupported("Cannot load environment in LITE mode: ", value);
  34. #endif
  35. if (s.ok()) {
  36. *result = env;
  37. }
  38. return s;
  39. }
  40. Status Env::LoadEnv(const std::string& value, Env** result,
  41. std::shared_ptr<Env>* guard) {
  42. assert(result);
  43. Status s;
  44. #ifndef ROCKSDB_LITE
  45. Env* env = nullptr;
  46. std::unique_ptr<Env> uniq_guard;
  47. std::string err_msg;
  48. assert(guard != nullptr);
  49. env = ObjectRegistry::NewInstance()->NewObject<Env>(value, &uniq_guard,
  50. &err_msg);
  51. if (!env) {
  52. s = Status::NotFound(std::string("Cannot load ") + Env::Type() + ": " +
  53. value);
  54. env = Env::Default();
  55. }
  56. if (s.ok() && uniq_guard) {
  57. guard->reset(uniq_guard.release());
  58. *result = guard->get();
  59. } else {
  60. *result = env;
  61. }
  62. #else
  63. (void)result;
  64. (void)guard;
  65. s = Status::NotSupported("Cannot load environment in LITE mode: ", value);
  66. #endif
  67. return s;
  68. }
  69. std::string Env::PriorityToString(Env::Priority priority) {
  70. switch (priority) {
  71. case Env::Priority::BOTTOM:
  72. return "Bottom";
  73. case Env::Priority::LOW:
  74. return "Low";
  75. case Env::Priority::HIGH:
  76. return "High";
  77. case Env::Priority::USER:
  78. return "User";
  79. case Env::Priority::TOTAL:
  80. assert(false);
  81. }
  82. return "Invalid";
  83. }
  84. uint64_t Env::GetThreadID() const {
  85. std::hash<std::thread::id> hasher;
  86. return hasher(std::this_thread::get_id());
  87. }
  88. Status Env::ReuseWritableFile(const std::string& fname,
  89. const std::string& old_fname,
  90. std::unique_ptr<WritableFile>* result,
  91. const EnvOptions& options) {
  92. Status s = RenameFile(old_fname, fname);
  93. if (!s.ok()) {
  94. return s;
  95. }
  96. return NewWritableFile(fname, result, options);
  97. }
  98. Status Env::GetChildrenFileAttributes(const std::string& dir,
  99. std::vector<FileAttributes>* result) {
  100. assert(result != nullptr);
  101. std::vector<std::string> child_fnames;
  102. Status s = GetChildren(dir, &child_fnames);
  103. if (!s.ok()) {
  104. return s;
  105. }
  106. result->resize(child_fnames.size());
  107. size_t result_size = 0;
  108. for (size_t i = 0; i < child_fnames.size(); ++i) {
  109. const std::string path = dir + "/" + child_fnames[i];
  110. if (!(s = GetFileSize(path, &(*result)[result_size].size_bytes)).ok()) {
  111. if (FileExists(path).IsNotFound()) {
  112. // The file may have been deleted since we listed the directory
  113. continue;
  114. }
  115. return s;
  116. }
  117. (*result)[result_size].name = std::move(child_fnames[i]);
  118. result_size++;
  119. }
  120. result->resize(result_size);
  121. return Status::OK();
  122. }
  123. SequentialFile::~SequentialFile() {
  124. }
  125. RandomAccessFile::~RandomAccessFile() {
  126. }
  127. WritableFile::~WritableFile() {
  128. }
  129. MemoryMappedFileBuffer::~MemoryMappedFileBuffer() {}
  130. Logger::~Logger() {}
  131. Status Logger::Close() {
  132. if (!closed_) {
  133. closed_ = true;
  134. return CloseImpl();
  135. } else {
  136. return Status::OK();
  137. }
  138. }
  139. Status Logger::CloseImpl() { return Status::NotSupported(); }
  140. FileLock::~FileLock() {
  141. }
  142. void LogFlush(Logger *info_log) {
  143. if (info_log) {
  144. info_log->Flush();
  145. }
  146. }
  147. static void Logv(Logger *info_log, const char* format, va_list ap) {
  148. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::INFO_LEVEL) {
  149. info_log->Logv(InfoLogLevel::INFO_LEVEL, format, ap);
  150. }
  151. }
  152. void Log(Logger* info_log, const char* format, ...) {
  153. va_list ap;
  154. va_start(ap, format);
  155. Logv(info_log, format, ap);
  156. va_end(ap);
  157. }
  158. void Logger::Logv(const InfoLogLevel log_level, const char* format, va_list ap) {
  159. static const char* kInfoLogLevelNames[5] = { "DEBUG", "INFO", "WARN",
  160. "ERROR", "FATAL" };
  161. if (log_level < log_level_) {
  162. return;
  163. }
  164. if (log_level == InfoLogLevel::INFO_LEVEL) {
  165. // Doesn't print log level if it is INFO level.
  166. // This is to avoid unexpected performance regression after we add
  167. // the feature of log level. All the logs before we add the feature
  168. // are INFO level. We don't want to add extra costs to those existing
  169. // logging.
  170. Logv(format, ap);
  171. } else if (log_level == InfoLogLevel::HEADER_LEVEL) {
  172. LogHeader(format, ap);
  173. } else {
  174. char new_format[500];
  175. snprintf(new_format, sizeof(new_format) - 1, "[%s] %s",
  176. kInfoLogLevelNames[log_level], format);
  177. Logv(new_format, ap);
  178. }
  179. }
  180. static void Logv(const InfoLogLevel log_level, Logger *info_log, const char *format, va_list ap) {
  181. if (info_log && info_log->GetInfoLogLevel() <= log_level) {
  182. if (log_level == InfoLogLevel::HEADER_LEVEL) {
  183. info_log->LogHeader(format, ap);
  184. } else {
  185. info_log->Logv(log_level, format, ap);
  186. }
  187. }
  188. }
  189. void Log(const InfoLogLevel log_level, Logger* info_log, const char* format,
  190. ...) {
  191. va_list ap;
  192. va_start(ap, format);
  193. Logv(log_level, info_log, format, ap);
  194. va_end(ap);
  195. }
  196. static void Headerv(Logger *info_log, const char *format, va_list ap) {
  197. if (info_log) {
  198. info_log->LogHeader(format, ap);
  199. }
  200. }
  201. void Header(Logger* info_log, const char* format, ...) {
  202. va_list ap;
  203. va_start(ap, format);
  204. Headerv(info_log, format, ap);
  205. va_end(ap);
  206. }
  207. static void Debugv(Logger* info_log, const char* format, va_list ap) {
  208. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::DEBUG_LEVEL) {
  209. info_log->Logv(InfoLogLevel::DEBUG_LEVEL, format, ap);
  210. }
  211. }
  212. void Debug(Logger* info_log, const char* format, ...) {
  213. va_list ap;
  214. va_start(ap, format);
  215. Debugv(info_log, format, ap);
  216. va_end(ap);
  217. }
  218. static void Infov(Logger* info_log, const char* format, va_list ap) {
  219. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::INFO_LEVEL) {
  220. info_log->Logv(InfoLogLevel::INFO_LEVEL, format, ap);
  221. }
  222. }
  223. void Info(Logger* info_log, const char* format, ...) {
  224. va_list ap;
  225. va_start(ap, format);
  226. Infov(info_log, format, ap);
  227. va_end(ap);
  228. }
  229. static void Warnv(Logger* info_log, const char* format, va_list ap) {
  230. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::WARN_LEVEL) {
  231. info_log->Logv(InfoLogLevel::WARN_LEVEL, format, ap);
  232. }
  233. }
  234. void Warn(Logger* info_log, const char* format, ...) {
  235. va_list ap;
  236. va_start(ap, format);
  237. Warnv(info_log, format, ap);
  238. va_end(ap);
  239. }
  240. static void Errorv(Logger* info_log, const char* format, va_list ap) {
  241. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::ERROR_LEVEL) {
  242. info_log->Logv(InfoLogLevel::ERROR_LEVEL, format, ap);
  243. }
  244. }
  245. void Error(Logger* info_log, const char* format, ...) {
  246. va_list ap;
  247. va_start(ap, format);
  248. Errorv(info_log, format, ap);
  249. va_end(ap);
  250. }
  251. static void Fatalv(Logger* info_log, const char* format, va_list ap) {
  252. if (info_log && info_log->GetInfoLogLevel() <= InfoLogLevel::FATAL_LEVEL) {
  253. info_log->Logv(InfoLogLevel::FATAL_LEVEL, format, ap);
  254. }
  255. }
  256. void Fatal(Logger* info_log, const char* format, ...) {
  257. va_list ap;
  258. va_start(ap, format);
  259. Fatalv(info_log, format, ap);
  260. va_end(ap);
  261. }
  262. void LogFlush(const std::shared_ptr<Logger>& info_log) {
  263. LogFlush(info_log.get());
  264. }
  265. void Log(const InfoLogLevel log_level, const std::shared_ptr<Logger>& info_log,
  266. const char* format, ...) {
  267. va_list ap;
  268. va_start(ap, format);
  269. Logv(log_level, info_log.get(), format, ap);
  270. va_end(ap);
  271. }
  272. void Header(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  273. va_list ap;
  274. va_start(ap, format);
  275. Headerv(info_log.get(), format, ap);
  276. va_end(ap);
  277. }
  278. void Debug(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  279. va_list ap;
  280. va_start(ap, format);
  281. Debugv(info_log.get(), format, ap);
  282. va_end(ap);
  283. }
  284. void Info(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  285. va_list ap;
  286. va_start(ap, format);
  287. Infov(info_log.get(), format, ap);
  288. va_end(ap);
  289. }
  290. void Warn(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  291. va_list ap;
  292. va_start(ap, format);
  293. Warnv(info_log.get(), format, ap);
  294. va_end(ap);
  295. }
  296. void Error(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  297. va_list ap;
  298. va_start(ap, format);
  299. Errorv(info_log.get(), format, ap);
  300. va_end(ap);
  301. }
  302. void Fatal(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  303. va_list ap;
  304. va_start(ap, format);
  305. Fatalv(info_log.get(), format, ap);
  306. va_end(ap);
  307. }
  308. void Log(const std::shared_ptr<Logger>& info_log, const char* format, ...) {
  309. va_list ap;
  310. va_start(ap, format);
  311. Logv(info_log.get(), format, ap);
  312. va_end(ap);
  313. }
  314. Status WriteStringToFile(Env* env, const Slice& data, const std::string& fname,
  315. bool should_sync) {
  316. std::unique_ptr<WritableFile> file;
  317. EnvOptions soptions;
  318. Status s = env->NewWritableFile(fname, &file, soptions);
  319. if (!s.ok()) {
  320. return s;
  321. }
  322. s = file->Append(data);
  323. if (s.ok() && should_sync) {
  324. s = file->Sync();
  325. }
  326. if (!s.ok()) {
  327. env->DeleteFile(fname);
  328. }
  329. return s;
  330. }
  331. Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
  332. LegacyFileSystemWrapper lfsw(env);
  333. return ReadFileToString(&lfsw, fname, data);
  334. }
  335. EnvWrapper::~EnvWrapper() {
  336. }
  337. namespace { // anonymous namespace
  338. void AssignEnvOptions(EnvOptions* env_options, const DBOptions& options) {
  339. env_options->use_mmap_reads = options.allow_mmap_reads;
  340. env_options->use_mmap_writes = options.allow_mmap_writes;
  341. env_options->use_direct_reads = options.use_direct_reads;
  342. env_options->set_fd_cloexec = options.is_fd_close_on_exec;
  343. env_options->bytes_per_sync = options.bytes_per_sync;
  344. env_options->compaction_readahead_size = options.compaction_readahead_size;
  345. env_options->random_access_max_buffer_size =
  346. options.random_access_max_buffer_size;
  347. env_options->rate_limiter = options.rate_limiter.get();
  348. env_options->writable_file_max_buffer_size =
  349. options.writable_file_max_buffer_size;
  350. env_options->allow_fallocate = options.allow_fallocate;
  351. env_options->strict_bytes_per_sync = options.strict_bytes_per_sync;
  352. options.env->SanitizeEnvOptions(env_options);
  353. }
  354. }
  355. EnvOptions Env::OptimizeForLogWrite(const EnvOptions& env_options,
  356. const DBOptions& db_options) const {
  357. EnvOptions optimized_env_options(env_options);
  358. optimized_env_options.bytes_per_sync = db_options.wal_bytes_per_sync;
  359. optimized_env_options.writable_file_max_buffer_size =
  360. db_options.writable_file_max_buffer_size;
  361. return optimized_env_options;
  362. }
  363. EnvOptions Env::OptimizeForManifestWrite(const EnvOptions& env_options) const {
  364. return env_options;
  365. }
  366. EnvOptions Env::OptimizeForLogRead(const EnvOptions& env_options) const {
  367. EnvOptions optimized_env_options(env_options);
  368. optimized_env_options.use_direct_reads = false;
  369. return optimized_env_options;
  370. }
  371. EnvOptions Env::OptimizeForManifestRead(const EnvOptions& env_options) const {
  372. EnvOptions optimized_env_options(env_options);
  373. optimized_env_options.use_direct_reads = false;
  374. return optimized_env_options;
  375. }
  376. EnvOptions Env::OptimizeForCompactionTableWrite(
  377. const EnvOptions& env_options, const ImmutableDBOptions& db_options) const {
  378. EnvOptions optimized_env_options(env_options);
  379. optimized_env_options.use_direct_writes =
  380. db_options.use_direct_io_for_flush_and_compaction;
  381. return optimized_env_options;
  382. }
  383. EnvOptions Env::OptimizeForCompactionTableRead(
  384. const EnvOptions& env_options, const ImmutableDBOptions& db_options) const {
  385. EnvOptions optimized_env_options(env_options);
  386. optimized_env_options.use_direct_reads = db_options.use_direct_reads;
  387. return optimized_env_options;
  388. }
  389. EnvOptions::EnvOptions(const DBOptions& options) {
  390. AssignEnvOptions(this, options);
  391. }
  392. EnvOptions::EnvOptions() {
  393. DBOptions options;
  394. AssignEnvOptions(this, options);
  395. }
  396. Status NewEnvLogger(const std::string& fname, Env* env,
  397. std::shared_ptr<Logger>* result) {
  398. EnvOptions options;
  399. // TODO: Tune the buffer size.
  400. options.writable_file_max_buffer_size = 1024 * 1024;
  401. std::unique_ptr<WritableFile> writable_file;
  402. const auto status = env->NewWritableFile(fname, &writable_file, options);
  403. if (!status.ok()) {
  404. return status;
  405. }
  406. *result = std::make_shared<EnvLogger>(
  407. NewLegacyWritableFileWrapper(std::move(writable_file)), fname, options,
  408. env);
  409. return Status::OK();
  410. }
  411. } // namespace ROCKSDB_NAMESPACE