filename.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "file/filename.h"
  10. #include <cinttypes>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <vector>
  14. #include "file/writable_file_writer.h"
  15. #include "logging/logging.h"
  16. #include "rocksdb/env.h"
  17. #include "test_util/sync_point.h"
  18. #include "util/stop_watch.h"
  19. #include "util/string_util.h"
  20. namespace ROCKSDB_NAMESPACE {
  21. static const std::string kRocksDbTFileExt = "sst";
  22. static const std::string kLevelDbTFileExt = "ldb";
  23. static const std::string kRocksDBBlobFileExt = "blob";
  24. // Given a path, flatten the path name by replacing all chars not in
  25. // {[0-9,a-z,A-Z,-,_,.]} with _. And append '_LOG\0' at the end.
  26. // Return the number of chars stored in dest not including the trailing '\0'.
  27. static size_t GetInfoLogPrefix(const std::string& path, char* dest, int len) {
  28. const char suffix[] = "_LOG";
  29. size_t write_idx = 0;
  30. size_t i = 0;
  31. size_t src_len = path.size();
  32. while (i < src_len && write_idx < len - sizeof(suffix)) {
  33. if ((path[i] >= 'a' && path[i] <= 'z') ||
  34. (path[i] >= '0' && path[i] <= '9') ||
  35. (path[i] >= 'A' && path[i] <= 'Z') ||
  36. path[i] == '-' ||
  37. path[i] == '.' ||
  38. path[i] == '_'){
  39. dest[write_idx++] = path[i];
  40. } else {
  41. if (i > 0) {
  42. dest[write_idx++] = '_';
  43. }
  44. }
  45. i++;
  46. }
  47. assert(sizeof(suffix) <= len - write_idx);
  48. // "\0" is automatically added by snprintf
  49. snprintf(dest + write_idx, len - write_idx, suffix);
  50. write_idx += sizeof(suffix) - 1;
  51. return write_idx;
  52. }
  53. static std::string MakeFileName(uint64_t number, const char* suffix) {
  54. char buf[100];
  55. snprintf(buf, sizeof(buf), "%06llu.%s",
  56. static_cast<unsigned long long>(number), suffix);
  57. return buf;
  58. }
  59. static std::string MakeFileName(const std::string& name, uint64_t number,
  60. const char* suffix) {
  61. return name + "/" + MakeFileName(number, suffix);
  62. }
  63. std::string LogFileName(const std::string& name, uint64_t number) {
  64. assert(number > 0);
  65. return MakeFileName(name, number, "log");
  66. }
  67. std::string LogFileName(uint64_t number) {
  68. assert(number > 0);
  69. return MakeFileName(number, "log");
  70. }
  71. std::string BlobFileName(const std::string& blobdirname, uint64_t number) {
  72. assert(number > 0);
  73. return MakeFileName(blobdirname, number, kRocksDBBlobFileExt.c_str());
  74. }
  75. std::string BlobFileName(const std::string& dbname, const std::string& blob_dir,
  76. uint64_t number) {
  77. assert(number > 0);
  78. return MakeFileName(dbname + "/" + blob_dir, number,
  79. kRocksDBBlobFileExt.c_str());
  80. }
  81. std::string ArchivalDirectory(const std::string& dir) {
  82. return dir + "/" + ARCHIVAL_DIR;
  83. }
  84. std::string ArchivedLogFileName(const std::string& name, uint64_t number) {
  85. assert(number > 0);
  86. return MakeFileName(name + "/" + ARCHIVAL_DIR, number, "log");
  87. }
  88. std::string MakeTableFileName(const std::string& path, uint64_t number) {
  89. return MakeFileName(path, number, kRocksDbTFileExt.c_str());
  90. }
  91. std::string MakeTableFileName(uint64_t number) {
  92. return MakeFileName(number, kRocksDbTFileExt.c_str());
  93. }
  94. std::string Rocks2LevelTableFileName(const std::string& fullname) {
  95. assert(fullname.size() > kRocksDbTFileExt.size() + 1);
  96. if (fullname.size() <= kRocksDbTFileExt.size() + 1) {
  97. return "";
  98. }
  99. return fullname.substr(0, fullname.size() - kRocksDbTFileExt.size()) +
  100. kLevelDbTFileExt;
  101. }
  102. uint64_t TableFileNameToNumber(const std::string& name) {
  103. uint64_t number = 0;
  104. uint64_t base = 1;
  105. int pos = static_cast<int>(name.find_last_of('.'));
  106. while (--pos >= 0 && name[pos] >= '0' && name[pos] <= '9') {
  107. number += (name[pos] - '0') * base;
  108. base *= 10;
  109. }
  110. return number;
  111. }
  112. std::string TableFileName(const std::vector<DbPath>& db_paths, uint64_t number,
  113. uint32_t path_id) {
  114. assert(number > 0);
  115. std::string path;
  116. if (path_id >= db_paths.size()) {
  117. path = db_paths.back().path;
  118. } else {
  119. path = db_paths[path_id].path;
  120. }
  121. return MakeTableFileName(path, number);
  122. }
  123. void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
  124. size_t out_buf_size) {
  125. if (path_id == 0) {
  126. snprintf(out_buf, out_buf_size, "%" PRIu64, number);
  127. } else {
  128. snprintf(out_buf, out_buf_size, "%" PRIu64
  129. "(path "
  130. "%" PRIu32 ")",
  131. number, path_id);
  132. }
  133. }
  134. std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
  135. assert(number > 0);
  136. char buf[100];
  137. snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
  138. static_cast<unsigned long long>(number));
  139. return dbname + buf;
  140. }
  141. std::string CurrentFileName(const std::string& dbname) {
  142. return dbname + "/CURRENT";
  143. }
  144. std::string LockFileName(const std::string& dbname) {
  145. return dbname + "/LOCK";
  146. }
  147. std::string TempFileName(const std::string& dbname, uint64_t number) {
  148. return MakeFileName(dbname, number, kTempFileNameSuffix.c_str());
  149. }
  150. InfoLogPrefix::InfoLogPrefix(bool has_log_dir,
  151. const std::string& db_absolute_path) {
  152. if (!has_log_dir) {
  153. const char kInfoLogPrefix[] = "LOG";
  154. // "\0" is automatically added to the end
  155. snprintf(buf, sizeof(buf), kInfoLogPrefix);
  156. prefix = Slice(buf, sizeof(kInfoLogPrefix) - 1);
  157. } else {
  158. size_t len = GetInfoLogPrefix(db_absolute_path, buf, sizeof(buf));
  159. prefix = Slice(buf, len);
  160. }
  161. }
  162. std::string InfoLogFileName(const std::string& dbname,
  163. const std::string& db_path, const std::string& log_dir) {
  164. if (log_dir.empty()) {
  165. return dbname + "/LOG";
  166. }
  167. InfoLogPrefix info_log_prefix(true, db_path);
  168. return log_dir + "/" + info_log_prefix.buf;
  169. }
  170. // Return the name of the old info log file for "dbname".
  171. std::string OldInfoLogFileName(const std::string& dbname, uint64_t ts,
  172. const std::string& db_path, const std::string& log_dir) {
  173. char buf[50];
  174. snprintf(buf, sizeof(buf), "%llu", static_cast<unsigned long long>(ts));
  175. if (log_dir.empty()) {
  176. return dbname + "/LOG.old." + buf;
  177. }
  178. InfoLogPrefix info_log_prefix(true, db_path);
  179. return log_dir + "/" + info_log_prefix.buf + ".old." + buf;
  180. }
  181. std::string OptionsFileName(const std::string& dbname, uint64_t file_num) {
  182. char buffer[256];
  183. snprintf(buffer, sizeof(buffer), "%s%06" PRIu64,
  184. kOptionsFileNamePrefix.c_str(), file_num);
  185. return dbname + "/" + buffer;
  186. }
  187. std::string TempOptionsFileName(const std::string& dbname, uint64_t file_num) {
  188. char buffer[256];
  189. snprintf(buffer, sizeof(buffer), "%s%06" PRIu64 ".%s",
  190. kOptionsFileNamePrefix.c_str(), file_num,
  191. kTempFileNameSuffix.c_str());
  192. return dbname + "/" + buffer;
  193. }
  194. std::string MetaDatabaseName(const std::string& dbname, uint64_t number) {
  195. char buf[100];
  196. snprintf(buf, sizeof(buf), "/METADB-%llu",
  197. static_cast<unsigned long long>(number));
  198. return dbname + buf;
  199. }
  200. std::string IdentityFileName(const std::string& dbname) {
  201. return dbname + "/IDENTITY";
  202. }
  203. // Owned filenames have the form:
  204. // dbname/IDENTITY
  205. // dbname/CURRENT
  206. // dbname/LOCK
  207. // dbname/<info_log_name_prefix>
  208. // dbname/<info_log_name_prefix>.old.[0-9]+
  209. // dbname/MANIFEST-[0-9]+
  210. // dbname/[0-9]+.(log|sst|blob)
  211. // dbname/METADB-[0-9]+
  212. // dbname/OPTIONS-[0-9]+
  213. // dbname/OPTIONS-[0-9]+.dbtmp
  214. // Disregards / at the beginning
  215. bool ParseFileName(const std::string& fname,
  216. uint64_t* number,
  217. FileType* type,
  218. WalFileType* log_type) {
  219. return ParseFileName(fname, number, "", type, log_type);
  220. }
  221. bool ParseFileName(const std::string& fname, uint64_t* number,
  222. const Slice& info_log_name_prefix, FileType* type,
  223. WalFileType* log_type) {
  224. Slice rest(fname);
  225. if (fname.length() > 1 && fname[0] == '/') {
  226. rest.remove_prefix(1);
  227. }
  228. if (rest == "IDENTITY") {
  229. *number = 0;
  230. *type = kIdentityFile;
  231. } else if (rest == "CURRENT") {
  232. *number = 0;
  233. *type = kCurrentFile;
  234. } else if (rest == "LOCK") {
  235. *number = 0;
  236. *type = kDBLockFile;
  237. } else if (info_log_name_prefix.size() > 0 &&
  238. rest.starts_with(info_log_name_prefix)) {
  239. rest.remove_prefix(info_log_name_prefix.size());
  240. if (rest == "" || rest == ".old") {
  241. *number = 0;
  242. *type = kInfoLogFile;
  243. } else if (rest.starts_with(".old.")) {
  244. uint64_t ts_suffix;
  245. // sizeof also counts the trailing '\0'.
  246. rest.remove_prefix(sizeof(".old.") - 1);
  247. if (!ConsumeDecimalNumber(&rest, &ts_suffix)) {
  248. return false;
  249. }
  250. *number = ts_suffix;
  251. *type = kInfoLogFile;
  252. }
  253. } else if (rest.starts_with("MANIFEST-")) {
  254. rest.remove_prefix(strlen("MANIFEST-"));
  255. uint64_t num;
  256. if (!ConsumeDecimalNumber(&rest, &num)) {
  257. return false;
  258. }
  259. if (!rest.empty()) {
  260. return false;
  261. }
  262. *type = kDescriptorFile;
  263. *number = num;
  264. } else if (rest.starts_with("METADB-")) {
  265. rest.remove_prefix(strlen("METADB-"));
  266. uint64_t num;
  267. if (!ConsumeDecimalNumber(&rest, &num)) {
  268. return false;
  269. }
  270. if (!rest.empty()) {
  271. return false;
  272. }
  273. *type = kMetaDatabase;
  274. *number = num;
  275. } else if (rest.starts_with(kOptionsFileNamePrefix)) {
  276. uint64_t ts_suffix;
  277. bool is_temp_file = false;
  278. rest.remove_prefix(kOptionsFileNamePrefix.size());
  279. const std::string kTempFileNameSuffixWithDot =
  280. std::string(".") + kTempFileNameSuffix;
  281. if (rest.ends_with(kTempFileNameSuffixWithDot)) {
  282. rest.remove_suffix(kTempFileNameSuffixWithDot.size());
  283. is_temp_file = true;
  284. }
  285. if (!ConsumeDecimalNumber(&rest, &ts_suffix)) {
  286. return false;
  287. }
  288. *number = ts_suffix;
  289. *type = is_temp_file ? kTempFile : kOptionsFile;
  290. } else {
  291. // Avoid strtoull() to keep filename format independent of the
  292. // current locale
  293. bool archive_dir_found = false;
  294. if (rest.starts_with(ARCHIVAL_DIR)) {
  295. if (rest.size() <= ARCHIVAL_DIR.size()) {
  296. return false;
  297. }
  298. rest.remove_prefix(ARCHIVAL_DIR.size() + 1); // Add 1 to remove / also
  299. if (log_type) {
  300. *log_type = kArchivedLogFile;
  301. }
  302. archive_dir_found = true;
  303. }
  304. uint64_t num;
  305. if (!ConsumeDecimalNumber(&rest, &num)) {
  306. return false;
  307. }
  308. if (rest.size() <= 1 || rest[0] != '.') {
  309. return false;
  310. }
  311. rest.remove_prefix(1);
  312. Slice suffix = rest;
  313. if (suffix == Slice("log")) {
  314. *type = kLogFile;
  315. if (log_type && !archive_dir_found) {
  316. *log_type = kAliveLogFile;
  317. }
  318. } else if (archive_dir_found) {
  319. return false; // Archive dir can contain only log files
  320. } else if (suffix == Slice(kRocksDbTFileExt) ||
  321. suffix == Slice(kLevelDbTFileExt)) {
  322. *type = kTableFile;
  323. } else if (suffix == Slice(kRocksDBBlobFileExt)) {
  324. *type = kBlobFile;
  325. } else if (suffix == Slice(kTempFileNameSuffix)) {
  326. *type = kTempFile;
  327. } else {
  328. return false;
  329. }
  330. *number = num;
  331. }
  332. return true;
  333. }
  334. Status SetCurrentFile(Env* env, const std::string& dbname,
  335. uint64_t descriptor_number,
  336. Directory* directory_to_fsync) {
  337. // Remove leading "dbname/" and add newline to manifest file name
  338. std::string manifest = DescriptorFileName(dbname, descriptor_number);
  339. Slice contents = manifest;
  340. assert(contents.starts_with(dbname + "/"));
  341. contents.remove_prefix(dbname.size() + 1);
  342. std::string tmp = TempFileName(dbname, descriptor_number);
  343. Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp, true);
  344. if (s.ok()) {
  345. TEST_KILL_RANDOM("SetCurrentFile:0", rocksdb_kill_odds * REDUCE_ODDS2);
  346. s = env->RenameFile(tmp, CurrentFileName(dbname));
  347. TEST_KILL_RANDOM("SetCurrentFile:1", rocksdb_kill_odds * REDUCE_ODDS2);
  348. }
  349. if (s.ok()) {
  350. if (directory_to_fsync != nullptr) {
  351. s = directory_to_fsync->Fsync();
  352. }
  353. } else {
  354. env->DeleteFile(tmp);
  355. }
  356. return s;
  357. }
  358. Status SetIdentityFile(Env* env, const std::string& dbname,
  359. const std::string& db_id) {
  360. std::string id;
  361. if (db_id.empty()) {
  362. id = env->GenerateUniqueId();
  363. } else {
  364. id = db_id;
  365. }
  366. assert(!id.empty());
  367. // Reserve the filename dbname/000000.dbtmp for the temporary identity file
  368. std::string tmp = TempFileName(dbname, 0);
  369. Status s = WriteStringToFile(env, id, tmp, true);
  370. if (s.ok()) {
  371. s = env->RenameFile(tmp, IdentityFileName(dbname));
  372. }
  373. if (!s.ok()) {
  374. env->DeleteFile(tmp);
  375. }
  376. return s;
  377. }
  378. Status SyncManifest(Env* env, const ImmutableDBOptions* db_options,
  379. WritableFileWriter* file) {
  380. TEST_KILL_RANDOM("SyncManifest:0", rocksdb_kill_odds * REDUCE_ODDS2);
  381. StopWatch sw(env, db_options->statistics.get(), MANIFEST_FILE_SYNC_MICROS);
  382. return file->Sync(db_options->use_fsync);
  383. }
  384. Status GetInfoLogFiles(Env* env, const std::string& db_log_dir,
  385. const std::string& dbname, std::string* parent_dir,
  386. std::vector<std::string>* info_log_list) {
  387. assert(parent_dir != nullptr);
  388. assert(info_log_list != nullptr);
  389. uint64_t number = 0;
  390. FileType type = kLogFile;
  391. if (!db_log_dir.empty()) {
  392. *parent_dir = db_log_dir;
  393. } else {
  394. *parent_dir = dbname;
  395. }
  396. InfoLogPrefix info_log_prefix(!db_log_dir.empty(), dbname);
  397. std::vector<std::string> file_names;
  398. Status s = env->GetChildren(*parent_dir, &file_names);
  399. if (!s.ok()) {
  400. return s;
  401. }
  402. for (auto& f : file_names) {
  403. if (ParseFileName(f, &number, info_log_prefix.prefix, &type) &&
  404. (type == kInfoLogFile)) {
  405. info_log_list->push_back(f);
  406. }
  407. }
  408. return Status::OK();
  409. }
  410. } // namespace ROCKSDB_NAMESPACE