@nxtedition/rocksdb 7.1.5 → 7.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/binding.cc +32 -14
  2. package/deps/rocksdb/rocksdb/cache/cache.cc +4 -0
  3. package/deps/rocksdb/rocksdb/cache/cache_bench_tool.cc +6 -8
  4. package/deps/rocksdb/rocksdb/cache/cache_key.cc +184 -164
  5. package/deps/rocksdb/rocksdb/cache/cache_key.h +38 -29
  6. package/deps/rocksdb/rocksdb/cache/cache_reservation_manager_test.cc +4 -4
  7. package/deps/rocksdb/rocksdb/cache/clock_cache.cc +4 -2
  8. package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache.cc +11 -9
  9. package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache.h +1 -1
  10. package/deps/rocksdb/rocksdb/cache/compressed_secondary_cache_test.cc +28 -18
  11. package/deps/rocksdb/rocksdb/cache/lru_cache.cc +86 -17
  12. package/deps/rocksdb/rocksdb/cache/lru_cache.h +48 -8
  13. package/deps/rocksdb/rocksdb/cache/lru_cache_test.cc +356 -153
  14. package/deps/rocksdb/rocksdb/db/blob/blob_file_builder.cc +3 -7
  15. package/deps/rocksdb/rocksdb/db/blob/blob_source.cc +4 -5
  16. package/deps/rocksdb/rocksdb/db/blob/blob_source.h +2 -3
  17. package/deps/rocksdb/rocksdb/db/blob/blob_source_test.cc +12 -4
  18. package/deps/rocksdb/rocksdb/db/blob/db_blob_compaction_test.cc +69 -0
  19. package/deps/rocksdb/rocksdb/db/compaction/compaction_iterator.cc +6 -1
  20. package/deps/rocksdb/rocksdb/db/compaction/compaction_job.cc +4 -1
  21. package/deps/rocksdb/rocksdb/db/db_block_cache_test.cc +222 -182
  22. package/deps/rocksdb/rocksdb/db/db_kv_checksum_test.cc +239 -23
  23. package/deps/rocksdb/rocksdb/db/db_test2.cc +6 -2
  24. package/deps/rocksdb/rocksdb/db/event_helpers.cc +2 -1
  25. package/deps/rocksdb/rocksdb/db/import_column_family_job.cc +6 -0
  26. package/deps/rocksdb/rocksdb/db/import_column_family_job.h +6 -0
  27. package/deps/rocksdb/rocksdb/db/import_column_family_test.cc +6 -0
  28. package/deps/rocksdb/rocksdb/db/kv_checksum.h +8 -4
  29. package/deps/rocksdb/rocksdb/db/memtable.cc +173 -33
  30. package/deps/rocksdb/rocksdb/db/memtable.h +10 -0
  31. package/deps/rocksdb/rocksdb/db/table_cache_sync_and_async.h +2 -1
  32. package/deps/rocksdb/rocksdb/db/version_set.cc +37 -18
  33. package/deps/rocksdb/rocksdb/db/version_set_sync_and_async.h +2 -1
  34. package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_common.h +1 -0
  35. package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_gflags.cc +6 -0
  36. package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_test_base.cc +2 -0
  37. package/deps/rocksdb/rocksdb/include/rocksdb/advanced_options.h +15 -0
  38. package/deps/rocksdb/rocksdb/include/rocksdb/cache.h +31 -6
  39. package/deps/rocksdb/rocksdb/memory/memory_allocator_test.cc +1 -1
  40. package/deps/rocksdb/rocksdb/options/cf_options.cc +4 -0
  41. package/deps/rocksdb/rocksdb/options/cf_options.h +4 -0
  42. package/deps/rocksdb/rocksdb/options/options_helper.cc +2 -0
  43. package/deps/rocksdb/rocksdb/options/options_settable_test.cc +2 -1
  44. package/deps/rocksdb/rocksdb/table/block_based/block_based_table_builder.cc +2 -6
  45. package/deps/rocksdb/rocksdb/table/block_based/block_based_table_factory.cc +1 -0
  46. package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader.cc +2 -4
  47. package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader.h +1 -7
  48. package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader_sync_and_async.h +2 -1
  49. package/deps/rocksdb/rocksdb/table/unique_id.cc +22 -24
  50. package/deps/rocksdb/rocksdb/table/unique_id_impl.h +2 -1
  51. package/deps/rocksdb/rocksdb/tools/block_cache_analyzer/block_cache_trace_analyzer_plot.py +7 -0
  52. package/deps/rocksdb/rocksdb/tools/db_bench_tool.cc +27 -3
  53. package/deps/rocksdb/rocksdb/util/async_file_reader.cc +2 -1
  54. package/deps/rocksdb/rocksdb/util/async_file_reader.h +3 -3
  55. package/deps/rocksdb/rocksdb/util/coro_utils.h +2 -1
  56. package/deps/rocksdb/rocksdb/util/hash_test.cc +67 -0
  57. package/deps/rocksdb/rocksdb/util/math.h +41 -0
  58. package/deps/rocksdb/rocksdb/util/math128.h +6 -0
  59. package/deps/rocksdb/rocksdb/util/single_thread_executor.h +2 -1
  60. package/deps/rocksdb/rocksdb/utilities/cache_dump_load_impl.cc +3 -6
  61. package/deps/rocksdb/rocksdb/utilities/transactions/lock/point/point_lock_manager_test.h +5 -0
  62. package/deps/rocksdb/rocksdb/utilities/transactions/lock/range/range_lock_manager.h +6 -0
  63. package/index.js +15 -6
  64. package/package.json +1 -1
  65. package/prebuilds/darwin-arm64/node.napi.node +0 -0
  66. package/prebuilds/darwin-x64/node.napi.node +0 -0
  67. package/prebuilds/linux-x64/node.napi.node +0 -0
@@ -41,13 +41,14 @@ class LRUCacheTest : public testing::Test {
41
41
  }
42
42
 
43
43
  void NewCache(size_t capacity, double high_pri_pool_ratio = 0.0,
44
+ double low_pri_pool_ratio = 1.0,
44
45
  bool use_adaptive_mutex = kDefaultToAdaptiveMutex) {
45
46
  DeleteCache();
46
47
  cache_ = reinterpret_cast<LRUCacheShard*>(
47
48
  port::cacheline_aligned_alloc(sizeof(LRUCacheShard)));
48
49
  new (cache_) LRUCacheShard(
49
50
  capacity, false /*strict_capcity_limit*/, high_pri_pool_ratio,
50
- use_adaptive_mutex, kDontChargeCacheMetadata,
51
+ low_pri_pool_ratio, use_adaptive_mutex, kDontChargeCacheMetadata,
51
52
  24 /*max_upper_hash_bits*/, nullptr /*secondary_cache*/);
52
53
  }
53
54
 
@@ -76,32 +77,66 @@ class LRUCacheTest : public testing::Test {
76
77
  void Erase(const std::string& key) { cache_->Erase(key, 0 /*hash*/); }
77
78
 
78
79
  void ValidateLRUList(std::vector<std::string> keys,
79
- size_t num_high_pri_pool_keys = 0) {
80
+ size_t num_high_pri_pool_keys = 0,
81
+ size_t num_low_pri_pool_keys = 0,
82
+ size_t num_bottom_pri_pool_keys = 0) {
80
83
  LRUHandle* lru;
81
84
  LRUHandle* lru_low_pri;
82
- cache_->TEST_GetLRUList(&lru, &lru_low_pri);
85
+ LRUHandle* lru_bottom_pri;
86
+ cache_->TEST_GetLRUList(&lru, &lru_low_pri, &lru_bottom_pri);
87
+
83
88
  LRUHandle* iter = lru;
89
+
90
+ bool in_low_pri_pool = false;
84
91
  bool in_high_pri_pool = false;
92
+
85
93
  size_t high_pri_pool_keys = 0;
94
+ size_t low_pri_pool_keys = 0;
95
+ size_t bottom_pri_pool_keys = 0;
96
+
97
+ if (iter == lru_bottom_pri) {
98
+ in_low_pri_pool = true;
99
+ in_high_pri_pool = false;
100
+ }
86
101
  if (iter == lru_low_pri) {
102
+ in_low_pri_pool = false;
87
103
  in_high_pri_pool = true;
88
104
  }
105
+
89
106
  for (const auto& key : keys) {
90
107
  iter = iter->next;
91
108
  ASSERT_NE(lru, iter);
92
109
  ASSERT_EQ(key, iter->key().ToString());
93
110
  ASSERT_EQ(in_high_pri_pool, iter->InHighPriPool());
111
+ ASSERT_EQ(in_low_pri_pool, iter->InLowPriPool());
94
112
  if (in_high_pri_pool) {
113
+ ASSERT_FALSE(iter->InLowPriPool());
95
114
  high_pri_pool_keys++;
115
+ } else if (in_low_pri_pool) {
116
+ ASSERT_FALSE(iter->InHighPriPool());
117
+ low_pri_pool_keys++;
118
+ } else {
119
+ bottom_pri_pool_keys++;
120
+ }
121
+ if (iter == lru_bottom_pri) {
122
+ ASSERT_FALSE(in_low_pri_pool);
123
+ ASSERT_FALSE(in_high_pri_pool);
124
+ in_low_pri_pool = true;
125
+ in_high_pri_pool = false;
96
126
  }
97
127
  if (iter == lru_low_pri) {
128
+ ASSERT_TRUE(in_low_pri_pool);
98
129
  ASSERT_FALSE(in_high_pri_pool);
130
+ in_low_pri_pool = false;
99
131
  in_high_pri_pool = true;
100
132
  }
101
133
  }
102
134
  ASSERT_EQ(lru, iter->next);
135
+ ASSERT_FALSE(in_low_pri_pool);
103
136
  ASSERT_TRUE(in_high_pri_pool);
104
137
  ASSERT_EQ(num_high_pri_pool_keys, high_pri_pool_keys);
138
+ ASSERT_EQ(num_low_pri_pool_keys, low_pri_pool_keys);
139
+ ASSERT_EQ(num_bottom_pri_pool_keys, bottom_pri_pool_keys);
105
140
  }
106
141
 
107
142
  private:
@@ -113,98 +148,219 @@ TEST_F(LRUCacheTest, BasicLRU) {
113
148
  for (char ch = 'a'; ch <= 'e'; ch++) {
114
149
  Insert(ch);
115
150
  }
116
- ValidateLRUList({"a", "b", "c", "d", "e"});
151
+ ValidateLRUList({"a", "b", "c", "d", "e"}, 0, 5);
117
152
  for (char ch = 'x'; ch <= 'z'; ch++) {
118
153
  Insert(ch);
119
154
  }
120
- ValidateLRUList({"d", "e", "x", "y", "z"});
155
+ ValidateLRUList({"d", "e", "x", "y", "z"}, 0, 5);
121
156
  ASSERT_FALSE(Lookup("b"));
122
- ValidateLRUList({"d", "e", "x", "y", "z"});
157
+ ValidateLRUList({"d", "e", "x", "y", "z"}, 0, 5);
123
158
  ASSERT_TRUE(Lookup("e"));
124
- ValidateLRUList({"d", "x", "y", "z", "e"});
159
+ ValidateLRUList({"d", "x", "y", "z", "e"}, 0, 5);
125
160
  ASSERT_TRUE(Lookup("z"));
126
- ValidateLRUList({"d", "x", "y", "e", "z"});
161
+ ValidateLRUList({"d", "x", "y", "e", "z"}, 0, 5);
127
162
  Erase("x");
128
- ValidateLRUList({"d", "y", "e", "z"});
163
+ ValidateLRUList({"d", "y", "e", "z"}, 0, 4);
129
164
  ASSERT_TRUE(Lookup("d"));
130
- ValidateLRUList({"y", "e", "z", "d"});
165
+ ValidateLRUList({"y", "e", "z", "d"}, 0, 4);
131
166
  Insert("u");
132
- ValidateLRUList({"y", "e", "z", "d", "u"});
167
+ ValidateLRUList({"y", "e", "z", "d", "u"}, 0, 5);
133
168
  Insert("v");
134
- ValidateLRUList({"e", "z", "d", "u", "v"});
169
+ ValidateLRUList({"e", "z", "d", "u", "v"}, 0, 5);
135
170
  }
136
171
 
137
- TEST_F(LRUCacheTest, MidpointInsertion) {
138
- // Allocate 2 cache entries to high-pri pool.
139
- NewCache(5, 0.45);
172
+ TEST_F(LRUCacheTest, LowPriorityMidpointInsertion) {
173
+ // Allocate 2 cache entries to high-pri pool and 3 to low-pri pool.
174
+ NewCache(5, /* high_pri_pool_ratio */ 0.40, /* low_pri_pool_ratio */ 0.60);
140
175
 
141
176
  Insert("a", Cache::Priority::LOW);
142
177
  Insert("b", Cache::Priority::LOW);
143
178
  Insert("c", Cache::Priority::LOW);
144
179
  Insert("x", Cache::Priority::HIGH);
145
180
  Insert("y", Cache::Priority::HIGH);
146
- ValidateLRUList({"a", "b", "c", "x", "y"}, 2);
181
+ ValidateLRUList({"a", "b", "c", "x", "y"}, 2, 3);
147
182
 
148
183
  // Low-pri entries inserted to the tail of low-pri list (the midpoint).
149
184
  // After lookup, it will move to the tail of the full list.
150
185
  Insert("d", Cache::Priority::LOW);
151
- ValidateLRUList({"b", "c", "d", "x", "y"}, 2);
186
+ ValidateLRUList({"b", "c", "d", "x", "y"}, 2, 3);
152
187
  ASSERT_TRUE(Lookup("d"));
153
- ValidateLRUList({"b", "c", "x", "y", "d"}, 2);
188
+ ValidateLRUList({"b", "c", "x", "y", "d"}, 2, 3);
154
189
 
155
190
  // High-pri entries will be inserted to the tail of full list.
156
191
  Insert("z", Cache::Priority::HIGH);
157
- ValidateLRUList({"c", "x", "y", "d", "z"}, 2);
192
+ ValidateLRUList({"c", "x", "y", "d", "z"}, 2, 3);
193
+ }
194
+
195
+ TEST_F(LRUCacheTest, BottomPriorityMidpointInsertion) {
196
+ // Allocate 2 cache entries to high-pri pool and 2 to low-pri pool.
197
+ NewCache(6, /* high_pri_pool_ratio */ 0.35, /* low_pri_pool_ratio */ 0.35);
198
+
199
+ Insert("a", Cache::Priority::BOTTOM);
200
+ Insert("b", Cache::Priority::BOTTOM);
201
+ Insert("i", Cache::Priority::LOW);
202
+ Insert("j", Cache::Priority::LOW);
203
+ Insert("x", Cache::Priority::HIGH);
204
+ Insert("y", Cache::Priority::HIGH);
205
+ ValidateLRUList({"a", "b", "i", "j", "x", "y"}, 2, 2, 2);
206
+
207
+ // Low-pri entries will be inserted to the tail of low-pri list (the
208
+ // midpoint). After lookup, 'k' will move to the tail of the full list, and
209
+ // 'x' will spill over to the low-pri pool.
210
+ Insert("k", Cache::Priority::LOW);
211
+ ValidateLRUList({"b", "i", "j", "k", "x", "y"}, 2, 2, 2);
212
+ ASSERT_TRUE(Lookup("k"));
213
+ ValidateLRUList({"b", "i", "j", "x", "y", "k"}, 2, 2, 2);
214
+
215
+ // High-pri entries will be inserted to the tail of full list. Although y was
216
+ // inserted with high priority, it got spilled over to the low-pri pool. As
217
+ // a result, j also got spilled over to the bottom-pri pool.
218
+ Insert("z", Cache::Priority::HIGH);
219
+ ValidateLRUList({"i", "j", "x", "y", "k", "z"}, 2, 2, 2);
220
+ Erase("x");
221
+ ValidateLRUList({"i", "j", "y", "k", "z"}, 2, 1, 2);
222
+ Erase("y");
223
+ ValidateLRUList({"i", "j", "k", "z"}, 2, 0, 2);
224
+
225
+ // Bottom-pri entries will be inserted to the tail of bottom-pri list.
226
+ Insert("c", Cache::Priority::BOTTOM);
227
+ ValidateLRUList({"i", "j", "c", "k", "z"}, 2, 0, 3);
228
+ Insert("d", Cache::Priority::BOTTOM);
229
+ ValidateLRUList({"i", "j", "c", "d", "k", "z"}, 2, 0, 4);
230
+ Insert("e", Cache::Priority::BOTTOM);
231
+ ValidateLRUList({"j", "c", "d", "e", "k", "z"}, 2, 0, 4);
232
+
233
+ // Low-pri entries will be inserted to the tail of low-pri list (the
234
+ // midpoint).
235
+ Insert("l", Cache::Priority::LOW);
236
+ ValidateLRUList({"c", "d", "e", "l", "k", "z"}, 2, 1, 3);
237
+ Insert("m", Cache::Priority::LOW);
238
+ ValidateLRUList({"d", "e", "l", "m", "k", "z"}, 2, 2, 2);
239
+
240
+ Erase("k");
241
+ ValidateLRUList({"d", "e", "l", "m", "z"}, 1, 2, 2);
242
+ Erase("z");
243
+ ValidateLRUList({"d", "e", "l", "m"}, 0, 2, 2);
244
+
245
+ // Bottom-pri entries will be inserted to the tail of bottom-pri list.
246
+ Insert("f", Cache::Priority::BOTTOM);
247
+ ValidateLRUList({"d", "e", "f", "l", "m"}, 0, 2, 3);
248
+ Insert("g", Cache::Priority::BOTTOM);
249
+ ValidateLRUList({"d", "e", "f", "g", "l", "m"}, 0, 2, 4);
250
+
251
+ // High-pri entries will be inserted to the tail of full list.
252
+ Insert("o", Cache::Priority::HIGH);
253
+ ValidateLRUList({"e", "f", "g", "l", "m", "o"}, 1, 2, 3);
254
+ Insert("p", Cache::Priority::HIGH);
255
+ ValidateLRUList({"f", "g", "l", "m", "o", "p"}, 2, 2, 2);
158
256
  }
159
257
 
160
258
  TEST_F(LRUCacheTest, EntriesWithPriority) {
161
- // Allocate 2 cache entries to high-pri pool.
162
- NewCache(5, 0.45);
259
+ // Allocate 2 cache entries to high-pri pool and 2 to low-pri pool.
260
+ NewCache(6, /* high_pri_pool_ratio */ 0.35, /* low_pri_pool_ratio */ 0.35);
163
261
 
164
262
  Insert("a", Cache::Priority::LOW);
165
263
  Insert("b", Cache::Priority::LOW);
264
+ ValidateLRUList({"a", "b"}, 0, 2, 0);
265
+ // Low-pri entries can overflow to bottom-pri pool.
166
266
  Insert("c", Cache::Priority::LOW);
167
- ValidateLRUList({"a", "b", "c"}, 0);
267
+ ValidateLRUList({"a", "b", "c"}, 0, 2, 1);
168
268
 
169
- // Low-pri entries can take high-pri pool capacity if available
269
+ // Bottom-pri entries can take high-pri pool capacity if available
270
+ Insert("t", Cache::Priority::LOW);
170
271
  Insert("u", Cache::Priority::LOW);
272
+ ValidateLRUList({"a", "b", "c", "t", "u"}, 0, 2, 3);
171
273
  Insert("v", Cache::Priority::LOW);
172
- ValidateLRUList({"a", "b", "c", "u", "v"}, 0);
274
+ ValidateLRUList({"a", "b", "c", "t", "u", "v"}, 0, 2, 4);
275
+ Insert("w", Cache::Priority::LOW);
276
+ ValidateLRUList({"b", "c", "t", "u", "v", "w"}, 0, 2, 4);
173
277
 
174
278
  Insert("X", Cache::Priority::HIGH);
175
279
  Insert("Y", Cache::Priority::HIGH);
176
- ValidateLRUList({"c", "u", "v", "X", "Y"}, 2);
280
+ ValidateLRUList({"t", "u", "v", "w", "X", "Y"}, 2, 2, 2);
177
281
 
178
- // High-pri entries can overflow to low-pri pool.
282
+ // After lookup, the high-pri entry 'X' got spilled over to the low-pri pool.
283
+ // The low-pri entry 'v' got spilled over to the bottom-pri pool.
179
284
  Insert("Z", Cache::Priority::HIGH);
180
- ValidateLRUList({"u", "v", "X", "Y", "Z"}, 2);
285
+ ValidateLRUList({"u", "v", "w", "X", "Y", "Z"}, 2, 2, 2);
181
286
 
182
287
  // Low-pri entries will be inserted to head of low-pri pool.
183
288
  Insert("a", Cache::Priority::LOW);
184
- ValidateLRUList({"v", "X", "a", "Y", "Z"}, 2);
289
+ ValidateLRUList({"v", "w", "X", "a", "Y", "Z"}, 2, 2, 2);
185
290
 
186
- // Low-pri entries will be inserted to head of high-pri pool after lookup.
291
+ // After lookup, the high-pri entry 'Y' got spilled over to the low-pri pool.
292
+ // The low-pri entry 'X' got spilled over to the bottom-pri pool.
187
293
  ASSERT_TRUE(Lookup("v"));
188
- ValidateLRUList({"X", "a", "Y", "Z", "v"}, 2);
294
+ ValidateLRUList({"w", "X", "a", "Y", "Z", "v"}, 2, 2, 2);
189
295
 
190
- // High-pri entries will be inserted to the head of the list after lookup.
296
+ // After lookup, the high-pri entry 'Z' got spilled over to the low-pri pool.
297
+ // The low-pri entry 'a' got spilled over to the bottom-pri pool.
191
298
  ASSERT_TRUE(Lookup("X"));
192
- ValidateLRUList({"a", "Y", "Z", "v", "X"}, 2);
299
+ ValidateLRUList({"w", "a", "Y", "Z", "v", "X"}, 2, 2, 2);
300
+
301
+ // After lookup, the low pri entry 'Z' got promoted back to high-pri pool. The
302
+ // high-pri entry 'v' got spilled over to the low-pri pool.
193
303
  ASSERT_TRUE(Lookup("Z"));
194
- ValidateLRUList({"a", "Y", "v", "X", "Z"}, 2);
304
+ ValidateLRUList({"w", "a", "Y", "v", "X", "Z"}, 2, 2, 2);
195
305
 
196
306
  Erase("Y");
197
- ValidateLRUList({"a", "v", "X", "Z"}, 2);
307
+ ValidateLRUList({"w", "a", "v", "X", "Z"}, 2, 1, 2);
198
308
  Erase("X");
199
- ValidateLRUList({"a", "v", "Z"}, 1);
309
+ ValidateLRUList({"w", "a", "v", "Z"}, 1, 1, 2);
310
+
200
311
  Insert("d", Cache::Priority::LOW);
201
312
  Insert("e", Cache::Priority::LOW);
202
- ValidateLRUList({"a", "v", "d", "e", "Z"}, 1);
313
+ ValidateLRUList({"w", "a", "v", "d", "e", "Z"}, 1, 2, 3);
314
+
203
315
  Insert("f", Cache::Priority::LOW);
204
316
  Insert("g", Cache::Priority::LOW);
205
- ValidateLRUList({"d", "e", "f", "g", "Z"}, 1);
317
+ ValidateLRUList({"v", "d", "e", "f", "g", "Z"}, 1, 2, 3);
318
+ ASSERT_TRUE(Lookup("d"));
319
+ ValidateLRUList({"v", "e", "f", "g", "Z", "d"}, 2, 2, 2);
320
+
321
+ // Erase some entries.
322
+ Erase("e");
323
+ Erase("f");
324
+ Erase("Z");
325
+ ValidateLRUList({"v", "g", "d"}, 1, 1, 1);
326
+
327
+ // Bottom-pri entries can take low- and high-pri pool capacity if available
328
+ Insert("o", Cache::Priority::BOTTOM);
329
+ ValidateLRUList({"v", "o", "g", "d"}, 1, 1, 2);
330
+ Insert("p", Cache::Priority::BOTTOM);
331
+ ValidateLRUList({"v", "o", "p", "g", "d"}, 1, 1, 3);
332
+ Insert("q", Cache::Priority::BOTTOM);
333
+ ValidateLRUList({"v", "o", "p", "q", "g", "d"}, 1, 1, 4);
334
+
335
+ // High-pri entries can overflow to low-pri pool, and bottom-pri entries will
336
+ // be evicted.
337
+ Insert("x", Cache::Priority::HIGH);
338
+ ValidateLRUList({"o", "p", "q", "g", "d", "x"}, 2, 1, 3);
339
+ Insert("y", Cache::Priority::HIGH);
340
+ ValidateLRUList({"p", "q", "g", "d", "x", "y"}, 2, 2, 2);
341
+ Insert("z", Cache::Priority::HIGH);
342
+ ValidateLRUList({"q", "g", "d", "x", "y", "z"}, 2, 2, 2);
343
+
344
+ // 'g' is bottom-pri before this lookup, it will be inserted to head of
345
+ // high-pri pool after lookup.
346
+ ASSERT_TRUE(Lookup("g"));
347
+ ValidateLRUList({"q", "d", "x", "y", "z", "g"}, 2, 2, 2);
348
+
349
+ // High-pri entries will be inserted to head of high-pri pool after lookup.
350
+ ASSERT_TRUE(Lookup("z"));
351
+ ValidateLRUList({"q", "d", "x", "y", "g", "z"}, 2, 2, 2);
352
+
353
+ // Bottom-pri entries will be inserted to head of high-pri pool after lookup.
206
354
  ASSERT_TRUE(Lookup("d"));
207
- ValidateLRUList({"e", "f", "g", "Z", "d"}, 2);
355
+ ValidateLRUList({"q", "x", "y", "g", "z", "d"}, 2, 2, 2);
356
+
357
+ // Bottom-pri entries will be inserted to the tail of bottom-pri list.
358
+ Insert("m", Cache::Priority::BOTTOM);
359
+ ValidateLRUList({"x", "m", "y", "g", "z", "d"}, 2, 2, 2);
360
+
361
+ // Bottom-pri entries will be inserted to head of high-pri pool after lookup.
362
+ ASSERT_TRUE(Lookup("m"));
363
+ ValidateLRUList({"x", "y", "g", "z", "d", "m"}, 2, 2, 2);
208
364
  }
209
365
 
210
366
  // TODO: FastLRUCache and ClockCache use the same tests. We can probably remove
@@ -547,8 +703,9 @@ class TestSecondaryCache : public SecondaryCache {
547
703
 
548
704
  explicit TestSecondaryCache(size_t capacity)
549
705
  : num_inserts_(0), num_lookups_(0), inject_failure_(false) {
550
- cache_ = NewLRUCache(capacity, 0, false, 0.5, nullptr,
551
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
706
+ cache_ =
707
+ NewLRUCache(capacity, 0, false, 0.5 /* high_pri_pool_ratio */, nullptr,
708
+ kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
552
709
  }
553
710
  ~TestSecondaryCache() override { cache_.reset(); }
554
711
 
@@ -558,19 +715,12 @@ class TestSecondaryCache : public SecondaryCache {
558
715
 
559
716
  void ResetInjectFailure() { inject_failure_ = false; }
560
717
 
561
- void SetDbSessionId(const std::string& db_session_id) {
562
- // NOTE: we assume the file is smaller than kMaxFileSizeStandardEncoding
563
- // for this to work, but that's safe in a test.
564
- auto base = OffsetableCacheKey("unknown", db_session_id, 1, 1);
565
- ckey_prefix_ = base.CommonPrefixSlice().ToString();
566
- }
567
-
568
718
  Status Insert(const Slice& key, void* value,
569
719
  const Cache::CacheItemHelper* helper) override {
570
720
  if (inject_failure_) {
571
721
  return Status::Corruption("Insertion Data Corrupted");
572
722
  }
573
- EXPECT_TRUE(IsDbSessionLowerAsKeyPrefix(key));
723
+ CheckCacheKeyCommonPrefix(key);
574
724
  size_t size;
575
725
  char* buf;
576
726
  Status s;
@@ -648,8 +798,13 @@ class TestSecondaryCache : public SecondaryCache {
648
798
 
649
799
  uint32_t num_lookups() { return num_lookups_; }
650
800
 
651
- bool IsDbSessionLowerAsKeyPrefix(const Slice& key) {
652
- return key.starts_with(ckey_prefix_);
801
+ void CheckCacheKeyCommonPrefix(const Slice& key) {
802
+ Slice current_prefix(key.data(), OffsetableCacheKey::kCommonPrefixSize);
803
+ if (ckey_prefix_.empty()) {
804
+ ckey_prefix_ = current_prefix.ToString();
805
+ } else {
806
+ EXPECT_EQ(ckey_prefix_, current_prefix.ToString());
807
+ }
653
808
  }
654
809
 
655
810
  private:
@@ -787,35 +942,40 @@ Cache::CacheItemHelper LRUCacheSecondaryCacheTest::helper_fail_(
787
942
  LRUCacheSecondaryCacheTest::DeletionCallback);
788
943
 
789
944
  TEST_F(LRUCacheSecondaryCacheTest, BasicTest) {
790
- LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
945
+ LRUCacheOptions opts(1024 /* capacity */, 0 /* num_shard_bits */,
946
+ false /* strict_capacity_limit */,
947
+ 0.5 /* high_pri_pool_ratio */,
948
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
791
949
  kDontChargeCacheMetadata);
792
950
  std::shared_ptr<TestSecondaryCache> secondary_cache =
793
951
  std::make_shared<TestSecondaryCache>(2048);
794
952
  opts.secondary_cache = secondary_cache;
795
953
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
796
954
  std::shared_ptr<Statistics> stats = CreateDBStatistics();
955
+ CacheKey k1 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
956
+ CacheKey k2 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
797
957
 
798
958
  Random rnd(301);
799
959
  std::string str1 = rnd.RandomString(1020);
800
960
  TestItem* item1 = new TestItem(str1.data(), str1.length());
801
- ASSERT_OK(cache->Insert("k1", item1, &LRUCacheSecondaryCacheTest::helper_,
802
- str1.length()));
961
+ ASSERT_OK(cache->Insert(k1.AsSlice(), item1,
962
+ &LRUCacheSecondaryCacheTest::helper_, str1.length()));
803
963
  std::string str2 = rnd.RandomString(1020);
804
964
  TestItem* item2 = new TestItem(str2.data(), str2.length());
805
965
  // k1 should be demoted to NVM
806
- ASSERT_OK(cache->Insert("k2", item2, &LRUCacheSecondaryCacheTest::helper_,
807
- str2.length()));
966
+ ASSERT_OK(cache->Insert(k2.AsSlice(), item2,
967
+ &LRUCacheSecondaryCacheTest::helper_, str2.length()));
808
968
 
809
969
  get_perf_context()->Reset();
810
970
  Cache::Handle* handle;
811
971
  handle =
812
- cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
972
+ cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
813
973
  test_item_creator, Cache::Priority::LOW, true, stats.get());
814
974
  ASSERT_NE(handle, nullptr);
815
975
  cache->Release(handle);
816
976
  // This lookup should promote k1 and demote k2
817
977
  handle =
818
- cache->Lookup("k1", &LRUCacheSecondaryCacheTest::helper_,
978
+ cache->Lookup(k1.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
819
979
  test_item_creator, Cache::Priority::LOW, true, stats.get());
820
980
  ASSERT_NE(handle, nullptr);
821
981
  cache->Release(handle);
@@ -831,27 +991,32 @@ TEST_F(LRUCacheSecondaryCacheTest, BasicTest) {
831
991
  }
832
992
 
833
993
  TEST_F(LRUCacheSecondaryCacheTest, BasicFailTest) {
834
- LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
994
+ LRUCacheOptions opts(1024 /* capacity */, 0 /* num_shard_bits */,
995
+ false /* strict_capacity_limit */,
996
+ 0.5 /* high_pri_pool_ratio */,
997
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
835
998
  kDontChargeCacheMetadata);
836
999
  std::shared_ptr<TestSecondaryCache> secondary_cache =
837
1000
  std::make_shared<TestSecondaryCache>(2048);
838
1001
  opts.secondary_cache = secondary_cache;
839
1002
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
1003
+ CacheKey k1 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
1004
+ CacheKey k2 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
840
1005
 
841
1006
  Random rnd(301);
842
1007
  std::string str1 = rnd.RandomString(1020);
843
1008
  auto item1 = std::make_unique<TestItem>(str1.data(), str1.length());
844
- ASSERT_TRUE(cache->Insert("k1", item1.get(), nullptr, str1.length())
1009
+ ASSERT_TRUE(cache->Insert(k1.AsSlice(), item1.get(), nullptr, str1.length())
845
1010
  .IsInvalidArgument());
846
- ASSERT_OK(cache->Insert("k1", item1.get(),
1011
+ ASSERT_OK(cache->Insert(k1.AsSlice(), item1.get(),
847
1012
  &LRUCacheSecondaryCacheTest::helper_, str1.length()));
848
1013
  item1.release(); // Appease clang-analyze "potential memory leak"
849
1014
 
850
1015
  Cache::Handle* handle;
851
- handle = cache->Lookup("k2", nullptr, test_item_creator, Cache::Priority::LOW,
852
- true);
1016
+ handle = cache->Lookup(k2.AsSlice(), nullptr, test_item_creator,
1017
+ Cache::Priority::LOW, true);
853
1018
  ASSERT_EQ(handle, nullptr);
854
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
1019
+ handle = cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
855
1020
  test_item_creator, Cache::Priority::LOW, false);
856
1021
  ASSERT_EQ(handle, nullptr);
857
1022
 
@@ -860,36 +1025,46 @@ TEST_F(LRUCacheSecondaryCacheTest, BasicFailTest) {
860
1025
  }
861
1026
 
862
1027
  TEST_F(LRUCacheSecondaryCacheTest, SaveFailTest) {
863
- LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
1028
+ LRUCacheOptions opts(1024 /* capacity */, 0 /* num_shard_bits */,
1029
+ false /* strict_capacity_limit */,
1030
+ 0.5 /* high_pri_pool_ratio */,
1031
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
864
1032
  kDontChargeCacheMetadata);
865
1033
  std::shared_ptr<TestSecondaryCache> secondary_cache =
866
1034
  std::make_shared<TestSecondaryCache>(2048);
867
1035
  opts.secondary_cache = secondary_cache;
868
1036
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
1037
+ CacheKey k1 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
1038
+ CacheKey k2 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
869
1039
 
870
1040
  Random rnd(301);
871
1041
  std::string str1 = rnd.RandomString(1020);
872
1042
  TestItem* item1 = new TestItem(str1.data(), str1.length());
873
- ASSERT_OK(cache->Insert(
874
- "k1", item1, &LRUCacheSecondaryCacheTest::helper_fail_, str1.length()));
1043
+ ASSERT_OK(cache->Insert(k1.AsSlice(), item1,
1044
+ &LRUCacheSecondaryCacheTest::helper_fail_,
1045
+ str1.length()));
875
1046
  std::string str2 = rnd.RandomString(1020);
876
1047
  TestItem* item2 = new TestItem(str2.data(), str2.length());
877
1048
  // k1 should be demoted to NVM
878
- ASSERT_OK(cache->Insert(
879
- "k2", item2, &LRUCacheSecondaryCacheTest::helper_fail_, str2.length()));
1049
+ ASSERT_OK(cache->Insert(k2.AsSlice(), item2,
1050
+ &LRUCacheSecondaryCacheTest::helper_fail_,
1051
+ str2.length()));
880
1052
 
881
1053
  Cache::Handle* handle;
882
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_fail_,
883
- test_item_creator, Cache::Priority::LOW, true);
1054
+ handle =
1055
+ cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_fail_,
1056
+ test_item_creator, Cache::Priority::LOW, true);
884
1057
  ASSERT_NE(handle, nullptr);
885
1058
  cache->Release(handle);
886
1059
  // This lookup should fail, since k1 demotion would have failed
887
- handle = cache->Lookup("k1", &LRUCacheSecondaryCacheTest::helper_fail_,
888
- test_item_creator, Cache::Priority::LOW, true);
1060
+ handle =
1061
+ cache->Lookup(k1.AsSlice(), &LRUCacheSecondaryCacheTest::helper_fail_,
1062
+ test_item_creator, Cache::Priority::LOW, true);
889
1063
  ASSERT_EQ(handle, nullptr);
890
1064
  // Since k1 didn't get promoted, k2 should still be in cache
891
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_fail_,
892
- test_item_creator, Cache::Priority::LOW, true);
1065
+ handle =
1066
+ cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_fail_,
1067
+ test_item_creator, Cache::Priority::LOW, true);
893
1068
  ASSERT_NE(handle, nullptr);
894
1069
  cache->Release(handle);
895
1070
  ASSERT_EQ(secondary_cache->num_inserts(), 1u);
@@ -900,36 +1075,41 @@ TEST_F(LRUCacheSecondaryCacheTest, SaveFailTest) {
900
1075
  }
901
1076
 
902
1077
  TEST_F(LRUCacheSecondaryCacheTest, CreateFailTest) {
903
- LRUCacheOptions opts(1024, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
1078
+ LRUCacheOptions opts(1024 /* capacity */, 0 /* num_shard_bits */,
1079
+ false /* strict_capacity_limit */,
1080
+ 0.5 /* high_pri_pool_ratio */,
1081
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
904
1082
  kDontChargeCacheMetadata);
905
1083
  std::shared_ptr<TestSecondaryCache> secondary_cache =
906
1084
  std::make_shared<TestSecondaryCache>(2048);
907
1085
  opts.secondary_cache = secondary_cache;
908
1086
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
1087
+ CacheKey k1 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
1088
+ CacheKey k2 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
909
1089
 
910
1090
  Random rnd(301);
911
1091
  std::string str1 = rnd.RandomString(1020);
912
1092
  TestItem* item1 = new TestItem(str1.data(), str1.length());
913
- ASSERT_OK(cache->Insert("k1", item1, &LRUCacheSecondaryCacheTest::helper_,
914
- str1.length()));
1093
+ ASSERT_OK(cache->Insert(k1.AsSlice(), item1,
1094
+ &LRUCacheSecondaryCacheTest::helper_, str1.length()));
915
1095
  std::string str2 = rnd.RandomString(1020);
916
1096
  TestItem* item2 = new TestItem(str2.data(), str2.length());
917
1097
  // k1 should be demoted to NVM
918
- ASSERT_OK(cache->Insert("k2", item2, &LRUCacheSecondaryCacheTest::helper_,
919
- str2.length()));
1098
+ ASSERT_OK(cache->Insert(k2.AsSlice(), item2,
1099
+ &LRUCacheSecondaryCacheTest::helper_, str2.length()));
920
1100
 
921
1101
  Cache::Handle* handle;
922
1102
  SetFailCreate(true);
923
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
1103
+ handle = cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
924
1104
  test_item_creator, Cache::Priority::LOW, true);
925
1105
  ASSERT_NE(handle, nullptr);
926
1106
  cache->Release(handle);
927
1107
  // This lookup should fail, since k1 creation would have failed
928
- handle = cache->Lookup("k1", &LRUCacheSecondaryCacheTest::helper_,
1108
+ handle = cache->Lookup(k1.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
929
1109
  test_item_creator, Cache::Priority::LOW, true);
930
1110
  ASSERT_EQ(handle, nullptr);
931
1111
  // Since k1 didn't get promoted, k2 should still be in cache
932
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
1112
+ handle = cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
933
1113
  test_item_creator, Cache::Priority::LOW, true);
934
1114
  ASSERT_NE(handle, nullptr);
935
1115
  cache->Release(handle);
@@ -941,38 +1121,43 @@ TEST_F(LRUCacheSecondaryCacheTest, CreateFailTest) {
941
1121
  }
942
1122
 
943
1123
  TEST_F(LRUCacheSecondaryCacheTest, FullCapacityTest) {
944
- LRUCacheOptions opts(1024, 0, /*_strict_capacity_limit=*/true, 0.5, nullptr,
945
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1124
+ LRUCacheOptions opts(1024 /* capacity */, 0 /* num_shard_bits */,
1125
+ true /* strict_capacity_limit */,
1126
+ 0.5 /* high_pri_pool_ratio */,
1127
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1128
+ kDontChargeCacheMetadata);
946
1129
  std::shared_ptr<TestSecondaryCache> secondary_cache =
947
1130
  std::make_shared<TestSecondaryCache>(2048);
948
1131
  opts.secondary_cache = secondary_cache;
949
1132
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
1133
+ CacheKey k1 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
1134
+ CacheKey k2 = CacheKey::CreateUniqueForCacheLifetime(cache.get());
950
1135
 
951
1136
  Random rnd(301);
952
1137
  std::string str1 = rnd.RandomString(1020);
953
1138
  TestItem* item1 = new TestItem(str1.data(), str1.length());
954
- ASSERT_OK(cache->Insert("k1", item1, &LRUCacheSecondaryCacheTest::helper_,
955
- str1.length()));
1139
+ ASSERT_OK(cache->Insert(k1.AsSlice(), item1,
1140
+ &LRUCacheSecondaryCacheTest::helper_, str1.length()));
956
1141
  std::string str2 = rnd.RandomString(1020);
957
1142
  TestItem* item2 = new TestItem(str2.data(), str2.length());
958
1143
  // k1 should be demoted to NVM
959
- ASSERT_OK(cache->Insert("k2", item2, &LRUCacheSecondaryCacheTest::helper_,
960
- str2.length()));
1144
+ ASSERT_OK(cache->Insert(k2.AsSlice(), item2,
1145
+ &LRUCacheSecondaryCacheTest::helper_, str2.length()));
961
1146
 
962
1147
  Cache::Handle* handle;
963
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
1148
+ handle = cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
964
1149
  test_item_creator, Cache::Priority::LOW, true);
965
1150
  ASSERT_NE(handle, nullptr);
966
1151
  // k1 promotion should fail due to the block cache being at capacity,
967
1152
  // but the lookup should still succeed
968
1153
  Cache::Handle* handle2;
969
- handle2 = cache->Lookup("k1", &LRUCacheSecondaryCacheTest::helper_,
1154
+ handle2 = cache->Lookup(k1.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
970
1155
  test_item_creator, Cache::Priority::LOW, true);
971
1156
  ASSERT_NE(handle2, nullptr);
972
1157
  // Since k1 didn't get inserted, k2 should still be in cache
973
1158
  cache->Release(handle);
974
1159
  cache->Release(handle2);
975
- handle = cache->Lookup("k2", &LRUCacheSecondaryCacheTest::helper_,
1160
+ handle = cache->Lookup(k2.AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
976
1161
  test_item_creator, Cache::Priority::LOW, true);
977
1162
  ASSERT_NE(handle, nullptr);
978
1163
  cache->Release(handle);
@@ -990,8 +1175,11 @@ TEST_F(LRUCacheSecondaryCacheTest, FullCapacityTest) {
990
1175
  // if we try to insert block_1 to the block cache, it will always fails. Only
991
1176
  // block_2 will be successfully inserted into the block cache.
992
1177
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness1) {
993
- LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr,
994
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1178
+ LRUCacheOptions opts(4 * 1024 /* capacity */, 0 /* num_shard_bits */,
1179
+ false /* strict_capacity_limit */,
1180
+ 0.5 /* high_pri_pool_ratio */,
1181
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1182
+ kDontChargeCacheMetadata);
995
1183
  std::shared_ptr<TestSecondaryCache> secondary_cache(
996
1184
  new TestSecondaryCache(2048 * 1024));
997
1185
  opts.secondary_cache = secondary_cache;
@@ -1009,9 +1197,6 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness1) {
1009
1197
  // all the blocks will be accessed.
1010
1198
  options.paranoid_file_checks = true;
1011
1199
  DestroyAndReopen(options);
1012
- std::string session_id;
1013
- ASSERT_OK(db_->GetDbSessionId(session_id));
1014
- secondary_cache->SetDbSessionId(session_id);
1015
1200
  Random rnd(301);
1016
1201
  const int N = 6;
1017
1202
  for (int i = 0; i < N; i++) {
@@ -1087,7 +1272,10 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness1) {
1087
1272
  // insert and cache block_1 in the block cache (this is the different place
1088
1273
  // from TestSecondaryCacheCorrectness1)
1089
1274
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) {
1090
- LRUCacheOptions opts(6100, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
1275
+ LRUCacheOptions opts(6100 /* capacity */, 0 /* num_shard_bits */,
1276
+ false /* strict_capacity_limit */,
1277
+ 0.5 /* high_pri_pool_ratio */,
1278
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1091
1279
  kDontChargeCacheMetadata);
1092
1280
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1093
1281
  new TestSecondaryCache(2048 * 1024));
@@ -1103,9 +1291,6 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) {
1103
1291
  options.env = fault_env_.get();
1104
1292
  fault_fs_->SetFailGetUniqueId(true);
1105
1293
  DestroyAndReopen(options);
1106
- std::string session_id;
1107
- ASSERT_OK(db_->GetDbSessionId(session_id));
1108
- secondary_cache->SetDbSessionId(session_id);
1109
1294
  Random rnd(301);
1110
1295
  const int N = 6;
1111
1296
  for (int i = 0; i < N; i++) {
@@ -1180,8 +1365,11 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) {
1180
1365
  // cache all the blocks in the block cache and there is not secondary cache
1181
1366
  // insertion. 2 lookup is needed for the blocks.
1182
1367
  TEST_F(DBSecondaryCacheTest, NoSecondaryCacheInsertion) {
1183
- LRUCacheOptions opts(1024 * 1024, 0, false, 0.5, nullptr,
1184
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1368
+ LRUCacheOptions opts(1024 * 1024 /* capacity */, 0 /* num_shard_bits */,
1369
+ false /* strict_capacity_limit */,
1370
+ 0.5 /* high_pri_pool_ratio */,
1371
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1372
+ kDontChargeCacheMetadata);
1185
1373
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1186
1374
  new TestSecondaryCache(2048 * 1024));
1187
1375
  opts.secondary_cache = secondary_cache;
@@ -1197,9 +1385,6 @@ TEST_F(DBSecondaryCacheTest, NoSecondaryCacheInsertion) {
1197
1385
  fault_fs_->SetFailGetUniqueId(true);
1198
1386
 
1199
1387
  DestroyAndReopen(options);
1200
- std::string session_id;
1201
- ASSERT_OK(db_->GetDbSessionId(session_id));
1202
- secondary_cache->SetDbSessionId(session_id);
1203
1388
  Random rnd(301);
1204
1389
  const int N = 6;
1205
1390
  for (int i = 0; i < N; i++) {
@@ -1234,8 +1419,11 @@ TEST_F(DBSecondaryCacheTest, NoSecondaryCacheInsertion) {
1234
1419
  }
1235
1420
 
1236
1421
  TEST_F(DBSecondaryCacheTest, SecondaryCacheIntensiveTesting) {
1237
- LRUCacheOptions opts(8 * 1024, 0, false, 0.5, nullptr,
1238
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1422
+ LRUCacheOptions opts(8 * 1024 /* capacity */, 0 /* num_shard_bits */,
1423
+ false /* strict_capacity_limit */,
1424
+ 0.5 /* high_pri_pool_ratio */,
1425
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1426
+ kDontChargeCacheMetadata);
1239
1427
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1240
1428
  new TestSecondaryCache(2048 * 1024));
1241
1429
  opts.secondary_cache = secondary_cache;
@@ -1249,9 +1437,6 @@ TEST_F(DBSecondaryCacheTest, SecondaryCacheIntensiveTesting) {
1249
1437
  options.env = fault_env_.get();
1250
1438
  fault_fs_->SetFailGetUniqueId(true);
1251
1439
  DestroyAndReopen(options);
1252
- std::string session_id;
1253
- ASSERT_OK(db_->GetDbSessionId(session_id));
1254
- secondary_cache->SetDbSessionId(session_id);
1255
1440
  Random rnd(301);
1256
1441
  const int N = 256;
1257
1442
  for (int i = 0; i < N; i++) {
@@ -1283,8 +1468,11 @@ TEST_F(DBSecondaryCacheTest, SecondaryCacheIntensiveTesting) {
1283
1468
  // if we try to insert block_1 to the block cache, it will always fails. Only
1284
1469
  // block_2 will be successfully inserted into the block cache.
1285
1470
  TEST_F(DBSecondaryCacheTest, SecondaryCacheFailureTest) {
1286
- LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr,
1287
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1471
+ LRUCacheOptions opts(4 * 1024 /* capacity */, 0 /* num_shard_bits */,
1472
+ false /* strict_capacity_limit */,
1473
+ 0.5 /* high_pri_pool_ratio */,
1474
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1475
+ kDontChargeCacheMetadata);
1288
1476
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1289
1477
  new TestSecondaryCache(2048 * 1024));
1290
1478
  opts.secondary_cache = secondary_cache;
@@ -1299,9 +1487,6 @@ TEST_F(DBSecondaryCacheTest, SecondaryCacheFailureTest) {
1299
1487
  options.env = fault_env_.get();
1300
1488
  fault_fs_->SetFailGetUniqueId(true);
1301
1489
  DestroyAndReopen(options);
1302
- std::string session_id;
1303
- ASSERT_OK(db_->GetDbSessionId(session_id));
1304
- secondary_cache->SetDbSessionId(session_id);
1305
1490
  Random rnd(301);
1306
1491
  const int N = 6;
1307
1492
  for (int i = 0; i < N; i++) {
@@ -1375,13 +1560,17 @@ TEST_F(DBSecondaryCacheTest, SecondaryCacheFailureTest) {
1375
1560
  }
1376
1561
 
1377
1562
  TEST_F(LRUCacheSecondaryCacheTest, BasicWaitAllTest) {
1378
- LRUCacheOptions opts(1024, 2, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
1563
+ LRUCacheOptions opts(1024 /* capacity */, 2 /* num_shard_bits */,
1564
+ false /* strict_capacity_limit */,
1565
+ 0.5 /* high_pri_pool_ratio */,
1566
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1379
1567
  kDontChargeCacheMetadata);
1380
1568
  std::shared_ptr<TestSecondaryCache> secondary_cache =
1381
1569
  std::make_shared<TestSecondaryCache>(32 * 1024);
1382
1570
  opts.secondary_cache = secondary_cache;
1383
1571
  std::shared_ptr<Cache> cache = NewLRUCache(opts);
1384
1572
  const int num_keys = 32;
1573
+ OffsetableCacheKey ock{"foo", "bar", 1};
1385
1574
 
1386
1575
  Random rnd(301);
1387
1576
  std::vector<std::string> values;
@@ -1389,7 +1578,7 @@ TEST_F(LRUCacheSecondaryCacheTest, BasicWaitAllTest) {
1389
1578
  std::string str = rnd.RandomString(1020);
1390
1579
  values.emplace_back(str);
1391
1580
  TestItem* item = new TestItem(str.data(), str.length());
1392
- ASSERT_OK(cache->Insert("k" + std::to_string(i), item,
1581
+ ASSERT_OK(cache->Insert(ock.WithOffset(i).AsSlice(), item,
1393
1582
  &LRUCacheSecondaryCacheTest::helper_,
1394
1583
  str.length()));
1395
1584
  }
@@ -1399,13 +1588,16 @@ TEST_F(LRUCacheSecondaryCacheTest, BasicWaitAllTest) {
1399
1588
  cache->SetCapacity(32 * 1024);
1400
1589
 
1401
1590
  secondary_cache->SetResultMap(
1402
- {{"k3", TestSecondaryCache::ResultType::DEFER},
1403
- {"k4", TestSecondaryCache::ResultType::DEFER_AND_FAIL},
1404
- {"k5", TestSecondaryCache::ResultType::FAIL}});
1591
+ {{ock.WithOffset(3).AsSlice().ToString(),
1592
+ TestSecondaryCache::ResultType::DEFER},
1593
+ {ock.WithOffset(4).AsSlice().ToString(),
1594
+ TestSecondaryCache::ResultType::DEFER_AND_FAIL},
1595
+ {ock.WithOffset(5).AsSlice().ToString(),
1596
+ TestSecondaryCache::ResultType::FAIL}});
1405
1597
  std::vector<Cache::Handle*> results;
1406
1598
  for (int i = 0; i < 6; ++i) {
1407
1599
  results.emplace_back(cache->Lookup(
1408
- "k" + std::to_string(i), &LRUCacheSecondaryCacheTest::helper_,
1600
+ ock.WithOffset(i).AsSlice(), &LRUCacheSecondaryCacheTest::helper_,
1409
1601
  test_item_creator, Cache::Priority::LOW, false));
1410
1602
  }
1411
1603
  cache->WaitAll(results);
@@ -1431,7 +1623,10 @@ TEST_F(LRUCacheSecondaryCacheTest, BasicWaitAllTest) {
1431
1623
  // a sync point callback in TestSecondaryCache::Lookup. We then control the
1432
1624
  // lookup result by setting the ResultMap.
1433
1625
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheMultiGet) {
1434
- LRUCacheOptions opts(1 << 20, 0, false, 0.5, nullptr, kDefaultToAdaptiveMutex,
1626
+ LRUCacheOptions opts(1 << 20 /* capacity */, 0 /* num_shard_bits */,
1627
+ false /* strict_capacity_limit */,
1628
+ 0.5 /* high_pri_pool_ratio */,
1629
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
1435
1630
  kDontChargeCacheMetadata);
1436
1631
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1437
1632
  new TestSecondaryCache(2048 * 1024));
@@ -1514,15 +1709,16 @@ class LRUCacheWithStat : public LRUCache {
1514
1709
  public:
1515
1710
  LRUCacheWithStat(
1516
1711
  size_t _capacity, int _num_shard_bits, bool _strict_capacity_limit,
1517
- double _high_pri_pool_ratio,
1712
+ double _high_pri_pool_ratio, double _low_pri_pool_ratio,
1518
1713
  std::shared_ptr<MemoryAllocator> _memory_allocator = nullptr,
1519
1714
  bool _use_adaptive_mutex = kDefaultToAdaptiveMutex,
1520
1715
  CacheMetadataChargePolicy _metadata_charge_policy =
1521
1716
  kDontChargeCacheMetadata,
1522
1717
  const std::shared_ptr<SecondaryCache>& _secondary_cache = nullptr)
1523
1718
  : LRUCache(_capacity, _num_shard_bits, _strict_capacity_limit,
1524
- _high_pri_pool_ratio, _memory_allocator, _use_adaptive_mutex,
1525
- _metadata_charge_policy, _secondary_cache) {
1719
+ _high_pri_pool_ratio, _low_pri_pool_ratio, _memory_allocator,
1720
+ _use_adaptive_mutex, _metadata_charge_policy,
1721
+ _secondary_cache) {
1526
1722
  insert_count_ = 0;
1527
1723
  lookup_count_ = 0;
1528
1724
  }
@@ -1565,13 +1761,17 @@ class LRUCacheWithStat : public LRUCache {
1565
1761
  #ifndef ROCKSDB_LITE
1566
1762
 
1567
1763
  TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
1568
- LRUCacheOptions cache_opts(1024 * 1024, 0, false, 0.5, nullptr,
1764
+ LRUCacheOptions cache_opts(1024 * 1024 /* capacity */, 0 /* num_shard_bits */,
1765
+ false /* strict_capacity_limit */,
1766
+ 0.5 /* high_pri_pool_ratio */,
1767
+ nullptr /* memory_allocator */,
1569
1768
  kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1570
1769
  LRUCacheWithStat* tmp_cache = new LRUCacheWithStat(
1571
1770
  cache_opts.capacity, cache_opts.num_shard_bits,
1572
1771
  cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio,
1573
- cache_opts.memory_allocator, cache_opts.use_adaptive_mutex,
1574
- cache_opts.metadata_charge_policy, cache_opts.secondary_cache);
1772
+ cache_opts.low_pri_pool_ratio, cache_opts.memory_allocator,
1773
+ cache_opts.use_adaptive_mutex, cache_opts.metadata_charge_policy,
1774
+ cache_opts.secondary_cache);
1575
1775
  std::shared_ptr<Cache> cache(tmp_cache);
1576
1776
  BlockBasedTableOptions table_options;
1577
1777
  table_options.block_cache = cache;
@@ -1642,8 +1842,9 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
1642
1842
  tmp_cache = new LRUCacheWithStat(
1643
1843
  cache_opts.capacity, cache_opts.num_shard_bits,
1644
1844
  cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio,
1645
- cache_opts.memory_allocator, cache_opts.use_adaptive_mutex,
1646
- cache_opts.metadata_charge_policy, cache_opts.secondary_cache);
1845
+ cache_opts.low_pri_pool_ratio, cache_opts.memory_allocator,
1846
+ cache_opts.use_adaptive_mutex, cache_opts.metadata_charge_policy,
1847
+ cache_opts.secondary_cache);
1647
1848
  std::shared_ptr<Cache> cache_new(tmp_cache);
1648
1849
  table_options.block_cache = cache_new;
1649
1850
  table_options.block_size = 4 * 1024;
@@ -1700,13 +1901,17 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
1700
1901
  }
1701
1902
 
1702
1903
  TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
1703
- LRUCacheOptions cache_opts(1024 * 1024, 0, false, 0.5, nullptr,
1904
+ LRUCacheOptions cache_opts(1024 * 1024 /* capacity */, 0 /* num_shard_bits */,
1905
+ false /* strict_capacity_limit */,
1906
+ 0.5 /* high_pri_pool_ratio */,
1907
+ nullptr /* memory_allocator */,
1704
1908
  kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
1705
1909
  LRUCacheWithStat* tmp_cache = new LRUCacheWithStat(
1706
1910
  cache_opts.capacity, cache_opts.num_shard_bits,
1707
1911
  cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio,
1708
- cache_opts.memory_allocator, cache_opts.use_adaptive_mutex,
1709
- cache_opts.metadata_charge_policy, cache_opts.secondary_cache);
1912
+ cache_opts.low_pri_pool_ratio, cache_opts.memory_allocator,
1913
+ cache_opts.use_adaptive_mutex, cache_opts.metadata_charge_policy,
1914
+ cache_opts.secondary_cache);
1710
1915
  std::shared_ptr<Cache> cache(tmp_cache);
1711
1916
  BlockBasedTableOptions table_options;
1712
1917
  table_options.block_cache = cache;
@@ -1804,8 +2009,9 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
1804
2009
  tmp_cache = new LRUCacheWithStat(
1805
2010
  cache_opts.capacity, cache_opts.num_shard_bits,
1806
2011
  cache_opts.strict_capacity_limit, cache_opts.high_pri_pool_ratio,
1807
- cache_opts.memory_allocator, cache_opts.use_adaptive_mutex,
1808
- cache_opts.metadata_charge_policy, cache_opts.secondary_cache);
2012
+ cache_opts.low_pri_pool_ratio, cache_opts.memory_allocator,
2013
+ cache_opts.use_adaptive_mutex, cache_opts.metadata_charge_policy,
2014
+ cache_opts.secondary_cache);
1809
2015
  std::shared_ptr<Cache> cache_new(tmp_cache);
1810
2016
  table_options.block_cache = cache_new;
1811
2017
  table_options.block_size = 4 * 1024;
@@ -1871,8 +2077,11 @@ TEST_F(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
1871
2077
 
1872
2078
  // Test the option not to use the secondary cache in a certain DB.
1873
2079
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionBasic) {
1874
- LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr,
1875
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
2080
+ LRUCacheOptions opts(4 * 1024 /* capacity */, 0 /* num_shard_bits */,
2081
+ false /* strict_capacity_limit */,
2082
+ 0.5 /* high_pri_pool_ratio */,
2083
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
2084
+ kDontChargeCacheMetadata);
1876
2085
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1877
2086
  new TestSecondaryCache(2048 * 1024));
1878
2087
  opts.secondary_cache = secondary_cache;
@@ -1891,9 +2100,6 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionBasic) {
1891
2100
  // all the blocks will be accessed.
1892
2101
  options.paranoid_file_checks = true;
1893
2102
  DestroyAndReopen(options);
1894
- std::string session_id;
1895
- ASSERT_OK(db_->GetDbSessionId(session_id));
1896
- secondary_cache->SetDbSessionId(session_id);
1897
2103
  Random rnd(301);
1898
2104
  const int N = 6;
1899
2105
  for (int i = 0; i < N; i++) {
@@ -1966,8 +2172,11 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionBasic) {
1966
2172
  // with new options, which set the lowest_used_cache_tier to
1967
2173
  // kNonVolatileBlockTier. So secondary cache will be used.
1968
2174
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionChange) {
1969
- LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr,
1970
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
2175
+ LRUCacheOptions opts(4 * 1024 /* capacity */, 0 /* num_shard_bits */,
2176
+ false /* strict_capacity_limit */,
2177
+ 0.5 /* high_pri_pool_ratio */,
2178
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
2179
+ kDontChargeCacheMetadata);
1971
2180
  std::shared_ptr<TestSecondaryCache> secondary_cache(
1972
2181
  new TestSecondaryCache(2048 * 1024));
1973
2182
  opts.secondary_cache = secondary_cache;
@@ -1986,9 +2195,6 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionChange) {
1986
2195
  // all the blocks will be accessed.
1987
2196
  options.paranoid_file_checks = true;
1988
2197
  DestroyAndReopen(options);
1989
- std::string session_id;
1990
- ASSERT_OK(db_->GetDbSessionId(session_id));
1991
- secondary_cache->SetDbSessionId(session_id);
1992
2198
  Random rnd(301);
1993
2199
  const int N = 6;
1994
2200
  for (int i = 0; i < N; i++) {
@@ -2061,8 +2267,11 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionChange) {
2061
2267
  // Two DB test. We create 2 DBs sharing the same block cache and secondary
2062
2268
  // cache. We diable the secondary cache option for DB2.
2063
2269
  TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) {
2064
- LRUCacheOptions opts(4 * 1024, 0, false, 0.5, nullptr,
2065
- kDefaultToAdaptiveMutex, kDontChargeCacheMetadata);
2270
+ LRUCacheOptions opts(4 * 1024 /* capacity */, 0 /* num_shard_bits */,
2271
+ false /* strict_capacity_limit */,
2272
+ 0.5 /* high_pri_pool_ratio */,
2273
+ nullptr /* memory_allocator */, kDefaultToAdaptiveMutex,
2274
+ kDontChargeCacheMetadata);
2066
2275
  std::shared_ptr<TestSecondaryCache> secondary_cache(
2067
2276
  new TestSecondaryCache(2048 * 1024));
2068
2277
  opts.secondary_cache = secondary_cache;
@@ -2087,12 +2296,6 @@ TEST_F(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) {
2087
2296
  ASSERT_OK(DB::Open(options2, dbname2, &db2));
2088
2297
  fault_fs_->SetFailGetUniqueId(true);
2089
2298
 
2090
- // Set the file paranoid check, so after flush, the file will be read
2091
- // all the blocks will be accessed.
2092
- std::string session_id;
2093
- ASSERT_OK(db1->GetDbSessionId(session_id));
2094
- secondary_cache->SetDbSessionId(session_id);
2095
-
2096
2299
  WriteOptions wo;
2097
2300
  Random rnd(301);
2098
2301
  const int N = 6;