作者: 韩晨旭 10225101440 李畅 10225102463

172 linhas
7.7 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
há 5 anos
  1. ## Files
  2. The implementation of leveldb is similar in spirit to the representation of a
  3. single [Bigtable tablet (section 5.3)](https://research.google/pubs/pub27898/).
  4. However the organization of the files that make up the representation is
  5. somewhat different and is explained below.
  6. Each database is represented by a set of files stored in a directory. There are
  7. several different types of files as documented below:
  8. ### Log files
  9. A log file (*.log) stores a sequence of recent updates. Each update is appended
  10. to the current log file. When the log file reaches a pre-determined size
  11. (approximately 4MB by default), it is converted to a sorted table (see below)
  12. and a new log file is created for future updates.
  13. A copy of the current log file is kept in an in-memory structure (the
  14. `memtable`). This copy is consulted on every read so that read operations
  15. reflect all logged updates.
  16. ## Sorted tables
  17. A sorted table (*.ldb) stores a sequence of entries sorted by key. Each entry is
  18. either a value for the key, or a deletion marker for the key. (Deletion markers
  19. are kept around to hide obsolete values present in older sorted tables).
  20. The set of sorted tables are organized into a sequence of levels. The sorted
  21. table generated from a log file is placed in a special **young** level (also
  22. called level-0). When the number of young files exceeds a certain threshold
  23. (currently four), all of the young files are merged together with all of the
  24. overlapping level-1 files to produce a sequence of new level-1 files (we create
  25. a new level-1 file for every 2MB of data.)
  26. Files in the young level may contain overlapping keys. However files in other
  27. levels have distinct non-overlapping key ranges. Consider level number L where
  28. L >= 1. When the combined size of files in level-L exceeds (10^L) MB (i.e., 10MB
  29. for level-1, 100MB for level-2, ...), one file in level-L, and all of the
  30. overlapping files in level-(L+1) are merged to form a set of new files for
  31. level-(L+1). These merges have the effect of gradually migrating new updates
  32. from the young level to the largest level using only bulk reads and writes
  33. (i.e., minimizing expensive seeks).
  34. ### Manifest
  35. A MANIFEST file lists the set of sorted tables that make up each level, the
  36. corresponding key ranges, and other important metadata. A new MANIFEST file
  37. (with a new number embedded in the file name) is created whenever the database
  38. is reopened. The MANIFEST file is formatted as a log, and changes made to the
  39. serving state (as files are added or removed) are appended to this log.
  40. ### Current
  41. CURRENT is a simple text file that contains the name of the latest MANIFEST
  42. file.
  43. ### Info logs
  44. Informational messages are printed to files named LOG and LOG.old.
  45. ### Others
  46. Other files used for miscellaneous purposes may also be present (LOCK, *.dbtmp).
  47. ## Level 0
  48. When the log file grows above a certain size (4MB by default):
  49. Create a brand new memtable and log file and direct future updates here.
  50. In the background:
  51. 1. Write the contents of the previous memtable to an sstable.
  52. 2. Discard the memtable.
  53. 3. Delete the old log file and the old memtable.
  54. 4. Add the new sstable to the young (level-0) level.
  55. ## Compactions
  56. When the size of level L exceeds its limit, we compact it in a background
  57. thread. The compaction picks a file from level L and all overlapping files from
  58. the next level L+1. Note that if a level-L file overlaps only part of a
  59. level-(L+1) file, the entire file at level-(L+1) is used as an input to the
  60. compaction and will be discarded after the compaction. Aside: because level-0
  61. is special (files in it may overlap each other), we treat compactions from
  62. level-0 to level-1 specially: a level-0 compaction may pick more than one
  63. level-0 file in case some of these files overlap each other.
  64. A compaction merges the contents of the picked files to produce a sequence of
  65. level-(L+1) files. We switch to producing a new level-(L+1) file after the
  66. current output file has reached the target file size (2MB). We also switch to a
  67. new output file when the key range of the current output file has grown enough
  68. to overlap more than ten level-(L+2) files. This last rule ensures that a later
  69. compaction of a level-(L+1) file will not pick up too much data from
  70. level-(L+2).
  71. The old files are discarded and the new files are added to the serving state.
  72. Compactions for a particular level rotate through the key space. In more detail,
  73. for each level L, we remember the ending key of the last compaction at level L.
  74. The next compaction for level L will pick the first file that starts after this
  75. key (wrapping around to the beginning of the key space if there is no such
  76. file).
  77. Compactions drop overwritten values. They also drop deletion markers if there
  78. are no higher numbered levels that contain a file whose range overlaps the
  79. current key.
  80. ### Timing
  81. Level-0 compactions will read up to four 1MB files from level-0, and at worst
  82. all the level-1 files (10MB). I.e., we will read 14MB and write 14MB.
  83. Other than the special level-0 compactions, we will pick one 2MB file from level
  84. L. In the worst case, this will overlap ~ 12 files from level L+1 (10 because
  85. level-(L+1) is ten times the size of level-L, and another two at the boundaries
  86. since the file ranges at level-L will usually not be aligned with the file
  87. ranges at level-L+1). The compaction will therefore read 26MB and write 26MB.
  88. Assuming a disk IO rate of 100MB/s (ballpark range for modern drives), the worst
  89. compaction cost will be approximately 0.5 second.
  90. If we throttle the background writing to something small, say 10% of the full
  91. 100MB/s speed, a compaction may take up to 5 seconds. If the user is writing at
  92. 10MB/s, we might build up lots of level-0 files (~50 to hold the 5*10MB). This
  93. may significantly increase the cost of reads due to the overhead of merging more
  94. files together on every read.
  95. Solution 1: To reduce this problem, we might want to increase the log switching
  96. threshold when the number of level-0 files is large. Though the downside is that
  97. the larger this threshold, the more memory we will need to hold the
  98. corresponding memtable.
  99. Solution 2: We might want to decrease write rate artificially when the number of
  100. level-0 files goes up.
  101. Solution 3: We work on reducing the cost of very wide merges. Perhaps most of
  102. the level-0 files will have their blocks sitting uncompressed in the cache and
  103. we will only need to worry about the O(N) complexity in the merging iterator.
  104. ### Number of files
  105. Instead of always making 2MB files, we could make larger files for larger levels
  106. to reduce the total file count, though at the expense of more bursty
  107. compactions. Alternatively, we could shard the set of files into multiple
  108. directories.
  109. An experiment on an ext3 filesystem on Feb 04, 2011 shows the following timings
  110. to do 100K file opens in directories with varying number of files:
  111. | Files in directory | Microseconds to open a file |
  112. |-------------------:|----------------------------:|
  113. | 1000 | 9 |
  114. | 10000 | 10 |
  115. | 100000 | 16 |
  116. So maybe even the sharding is not necessary on modern filesystems?
  117. ## Recovery
  118. * Read CURRENT to find name of the latest committed MANIFEST
  119. * Read the named MANIFEST file
  120. * Clean up stale files
  121. * We could open all sstables here, but it is probably better to be lazy...
  122. * Convert log chunk to a new level-0 sstable
  123. * Start directing new writes to a new log file with recovered sequence#
  124. ## Garbage collection of files
  125. `RemoveObsoleteFiles()` is called at the end of every compaction and at the end
  126. of recovery. It finds the names of all files in the database. It deletes all log
  127. files that are not the current log file. It deletes all table files that are not
  128. referenced from some level and are not the output of an active compaction.