block_cache_tier_metadata.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2013, 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 <functional>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "rocksdb/slice.h"
  10. #include "utilities/persistent_cache/block_cache_tier_file.h"
  11. #include "utilities/persistent_cache/hash_table.h"
  12. #include "utilities/persistent_cache/hash_table_evictable.h"
  13. #include "utilities/persistent_cache/lrulist.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. //
  16. // Block Cache Tier Metadata
  17. //
  18. // The BlockCacheTierMetadata holds all the metadata associated with block
  19. // cache. It
  20. // fundamentally contains 2 indexes and an LRU.
  21. //
  22. // Block Cache Index
  23. //
  24. // This is a forward index that maps a given key to a LBA (Logical Block
  25. // Address). LBA is a disk pointer that points to a record on the cache.
  26. //
  27. // LBA = { cache-id, offset, size }
  28. //
  29. // Cache File Index
  30. //
  31. // This is a forward index that maps a given cache-id to a cache file object.
  32. // Typically you would lookup using LBA and use the object to read or write
  33. struct BlockInfo {
  34. explicit BlockInfo(const Slice& key, const LBA& lba = LBA())
  35. : key_(key.ToString()), lba_(lba) {}
  36. std::string key_;
  37. LBA lba_;
  38. };
  39. class BlockCacheTierMetadata {
  40. public:
  41. explicit BlockCacheTierMetadata(const uint32_t blocks_capacity = 1024 * 1024,
  42. const uint32_t cachefile_capacity = 10 * 1024)
  43. : cache_file_index_(cachefile_capacity), block_index_(blocks_capacity) {}
  44. virtual ~BlockCacheTierMetadata() {}
  45. // Insert a given cache file
  46. bool Insert(BlockCacheFile* file);
  47. // Lookup cache file based on cache_id
  48. BlockCacheFile* Lookup(const uint32_t cache_id);
  49. // Insert block information to block index
  50. BlockInfo* Insert(const Slice& key, const LBA& lba);
  51. // bool Insert(BlockInfo* binfo);
  52. // Lookup block information from block index
  53. bool Lookup(const Slice& key, LBA* lba);
  54. // Remove a given from the block index
  55. BlockInfo* Remove(const Slice& key);
  56. // Find and evict a cache file using LRU policy
  57. BlockCacheFile* Evict();
  58. // Clear the metadata contents
  59. virtual void Clear();
  60. protected:
  61. // Remove all block information from a given file
  62. virtual void RemoveAllKeys(BlockCacheFile* file);
  63. private:
  64. // Cache file index definition
  65. //
  66. // cache-id => BlockCacheFile
  67. struct BlockCacheFileHash {
  68. uint64_t operator()(const BlockCacheFile* rec) {
  69. return std::hash<uint32_t>()(rec->cacheid());
  70. }
  71. };
  72. struct BlockCacheFileEqual {
  73. uint64_t operator()(const BlockCacheFile* lhs, const BlockCacheFile* rhs) {
  74. return lhs->cacheid() == rhs->cacheid();
  75. }
  76. };
  77. using CacheFileIndexType =
  78. EvictableHashTable<BlockCacheFile, BlockCacheFileHash,
  79. BlockCacheFileEqual>;
  80. // Block Lookup Index
  81. //
  82. // key => LBA
  83. struct Hash {
  84. size_t operator()(BlockInfo* node) const {
  85. return std::hash<std::string>()(node->key_);
  86. }
  87. };
  88. struct Equal {
  89. size_t operator()(BlockInfo* lhs, BlockInfo* rhs) const {
  90. return lhs->key_ == rhs->key_;
  91. }
  92. };
  93. using BlockIndexType = HashTable<BlockInfo*, Hash, Equal>;
  94. CacheFileIndexType cache_file_index_;
  95. BlockIndexType block_index_;
  96. };
  97. } // namespace ROCKSDB_NAMESPACE