Skip to content

Commit 750792d

Browse files
committed
wip tests
1 parent f19e805 commit 750792d

File tree

3 files changed

+130
-45
lines changed

3 files changed

+130
-45
lines changed

ydb/core/tablet_flat/shared_cache_s3fifo.h

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NKikimr::NCache {
1414
template <typename TPageKey
1515
, typename TPageKeyHash
1616
, typename TPageKeyEqual>
17-
class TGhostQueue {
17+
class TTS3FIFOGhostQueue {
1818
struct TGhostPage {
1919
TPageKey Key;
2020
ui64 Size; // zero size is tombstone
@@ -38,7 +38,7 @@ class TGhostQueue {
3838
};
3939

4040
public:
41-
TGhostQueue(ui64 limit)
41+
TTS3FIFOGhostQueue(ui64 limit)
4242
: Limit(limit)
4343
{}
4444

@@ -78,19 +78,22 @@ class TGhostQueue {
7878
EvictWhileFull();
7979
}
8080

81-
TString Dump() {
81+
TString Dump() const {
8282
TStringBuilder result;
83-
size_t size = 0;
84-
for (auto it : GhostsQueue) {
85-
const TGhostPage* ghost = &it;
83+
size_t count = 0;
84+
ui64 size = 0;
85+
for (auto it = GhostsQueue.begin(); it != GhostsQueue.end(); it++) {
86+
const TGhostPage* ghost = &*it;
8687
if (ghost->Size) { // isn't deleted
8788
Y_DEBUG_ABORT_UNLESS(GhostsSet.contains(ghost));
88-
if (size != 0) result << ", ";
89+
if (count != 0) result << ", ";
8990
result << "{" << ghost->Key.ToString() << " " << ghost->Size << "b}";
90-
size++;
91+
count++;
92+
size += ghost->Size;
9193
}
9294
}
93-
Y_DEBUG_ABORT_UNLESS(GhostsSet.size() == size);
95+
Y_DEBUG_ABORT_UNLESS(GhostsSet.size() == count);
96+
Y_DEBUG_ABORT_UNLESS(Size == size);
9497
return result;
9598
}
9699

@@ -207,6 +210,32 @@ class TS3FIFOCache : public ICacheCache<TPage> {
207210
GhostQueue.UpdateLimit(limit);
208211
}
209212

213+
TString Dump() const {
214+
TStringBuilder result;
215+
216+
auto dump = [&](const TQueue& queue) {
217+
size_t count = 0;
218+
ui64 size = 0;
219+
for (auto it = queue.Queue.begin(); it != queue.Queue.end(); it++) {
220+
const TPage* page = &*it;
221+
if (count != 0) result << ", ";
222+
result << "{" << GetKey(page).ToString() << " " << GetFrequency(page) << "f " << GetSize(page) << "b}";
223+
count++;
224+
size += GetSize(page);
225+
}
226+
Y_DEBUG_ABORT_UNLESS(queue.Size == size);
227+
};
228+
229+
result << "SmallQueue: ";
230+
dump(SmallQueue);
231+
result << Endl << "MainQueue: ";
232+
dump(MainQueue);
233+
result << Endl << "GhostQueue: ";
234+
result << GhostQueue.Dump();
235+
236+
return result;
237+
}
238+
210239
private:
211240
TPage* EvictOneIfFull() {
212241
while (true) {
@@ -322,7 +351,7 @@ class TS3FIFOCache : public ICacheCache<TPage> {
322351
TLimit Limit;
323352
TQueue SmallQueue;
324353
TQueue MainQueue;
325-
TGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> GhostQueue;
354+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> GhostQueue;
326355

327356
};
328357

ydb/core/tablet_flat/shared_cache_s3fifo_ut.cpp

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,78 @@ namespace NKikimr::NCache {
55

66
namespace {
77

8-
struct TPage {
8+
struct TPage : public TIntrusiveListItem<TPage> {
99
ui32 Id;
1010
size_t Size;
1111

12+
TPage(ui32 id, size_t size)
13+
: Id(id), Size(size)
14+
{}
15+
1216
ui32 CacheFlags1 : 4 = 0;
1317
ui32 CacheFlags2 : 4 = 0;
1418
};
1519

16-
struct TKey {
20+
struct TPageKey {
1721
ui32 Id;
1822

19-
TKey(ui32 id)
23+
TPageKey(ui32 id)
2024
: Id(id)
2125
{}
2226

27+
static TPageKey Get(const TPage* page) {
28+
return {page->Id};
29+
}
30+
2331
TString ToString() const {
2432
return std::to_string(Id);
2533
}
2634
};
2735

28-
struct TKeyHash {
29-
inline size_t operator()(const TKey& key) const {
36+
struct TPageKeyHash {
37+
inline size_t operator()(const TPageKey& key) const {
3038
return std::hash<ui32>()(key.Id);
3139
}
3240
};
3341

34-
struct TKeyEqual {
35-
inline bool operator()(const TKey& left, const TKey& right) const {
42+
struct TPageKeyEqual {
43+
inline bool operator()(const TPageKey& left, const TPageKey& right) const {
3644
return left.Id == right.Id;
3745
}
3846
};
3947

40-
struct TSize {
41-
static ui64 Get(const TPage *x) {
42-
return x->Size;
48+
struct TPageSize {
49+
static ui64 Get(const TPage *page) {
50+
return page->Size;
4351
}
4452
};
4553

46-
struct TCacheFlags1 {
47-
static ui32 Get(const TPage *x) {
48-
return x->CacheFlags1;
54+
struct TPageLocation {
55+
static ui32 Get(const TPage *page) {
56+
return page->CacheFlags1;
4957
}
5058
static void Set(TPage *x, ui32 flags) {
5159
Y_ABORT_UNLESS(flags < (1 << 4));
5260
x->CacheFlags1 = flags;
5361
}
5462
};
5563

56-
struct TCacheFlags2 {
57-
static ui32 Get(const TPage *x) {
58-
return x->CacheFlags2;
64+
struct TPageFrequency {
65+
static ui32 Get(const TPage *page) {
66+
return page->CacheFlags2;
5967
}
60-
static void Set(TPage *x, ui32 flags) {
68+
static void Set(TPage *page, ui32 flags) {
6169
Y_ABORT_UNLESS(flags < (1 << 4));
62-
x->CacheFlags2 = flags;
70+
page->CacheFlags2 = flags;
6371
}
6472
};
6573

6674
}
6775

68-
Y_UNIT_TEST_SUITE(TGhostQueue) {
76+
Y_UNIT_TEST_SUITE(TTS3FIFOGhostQueue) {
6977

7078
Y_UNIT_TEST(Add) {
71-
TGhostQueue<TKey, TKeyHash, TKeyEqual> queue(100);
79+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> queue(100);
7280
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
7381

7482
queue.Add(1, 10);
@@ -88,7 +96,7 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
8896
}
8997

9098
Y_UNIT_TEST(Erase) {
91-
TGhostQueue<TKey, TKeyHash, TKeyEqual> queue(100);
99+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> queue(100);
92100
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
93101

94102
queue.Add(1, 10);
@@ -110,7 +118,7 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
110118
}
111119

112120
Y_UNIT_TEST(Erase_Add) {
113-
TGhostQueue<TKey, TKeyHash, TKeyEqual> queue(100);
121+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> queue(100);
114122
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
115123

116124
queue.Add(1, 10);
@@ -129,15 +137,15 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
129137
}
130138

131139
Y_UNIT_TEST(Add_Big) {
132-
TGhostQueue<TKey, TKeyHash, TKeyEqual> queue(100);
140+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> queue(100);
133141
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
134142

135143
queue.Add(1, 101);
136144
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
137145
}
138146

139147
Y_UNIT_TEST(UpdateLimit) {
140-
TGhostQueue<TKey, TKeyHash, TKeyEqual> queue(100);
148+
TTS3FIFOGhostQueue<TPageKey, TPageKeyHash, TPageKeyEqual> queue(100);
141149
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "");
142150

143151
queue.Add(1, 10);
@@ -149,6 +157,54 @@ Y_UNIT_TEST_SUITE(TGhostQueue) {
149157
queue.UpdateLimit(80);
150158
UNIT_ASSERT_VALUES_EQUAL(queue.Dump(), "{3 30b}, {4 40b}");
151159
}
160+
161+
}
162+
163+
Y_UNIT_TEST_SUITE(TS3FIFOCache) {
164+
165+
TVector<TPage*> Touch(auto& cache, TPage& page) {
166+
auto evicted = cache.Touch(&page);
167+
TVector<TPage*> result;
168+
for (auto& p : evicted) {
169+
result.push_back(&p);
170+
}
171+
return result;
172+
}
173+
174+
Y_UNIT_TEST(Touch) {
175+
TS3FIFOCache<TPage, TPageKey, TPageKeyHash, TPageKeyEqual, TPageSize, TPageLocation, TPageFrequency> cache(100);
176+
177+
TPage page1{1, 2};
178+
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<TPage*>{});
179+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
180+
<< "SmallQueue: {1 0f 2b}" << Endl
181+
<< "MainQueue: " << Endl
182+
<< "GhostQueue: "));
183+
184+
TPage page2{2, 3};
185+
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<TPage*>{});
186+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
187+
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}" << Endl
188+
<< "MainQueue: " << Endl
189+
<< "GhostQueue: "));
190+
191+
TPage page3{3, 4};
192+
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<TPage*>{});
193+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
194+
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}" << Endl
195+
<< "MainQueue: " << Endl
196+
<< "GhostQueue: "));
197+
198+
TPage page4{4, 1};
199+
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<TPage*>{});
200+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
201+
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}, {4 0f 1b}" << Endl
202+
<< "MainQueue: " << Endl
203+
<< "GhostQueue: "));
204+
205+
206+
}
207+
152208
}
153209

154210
}

ydb/core/tablet_flat/shared_sausagecache.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ class TSharedPageCache : public TActorBootstrapped<TSharedPageCache> {
137137

138138
auto operator<=>(const TKey&) const = default;
139139

140-
static TKey Get(const TPage *x) {
141-
return {x->Collection->MetaId, x->PageId};
140+
static TKey Get(const TPage *page) {
141+
return {page->Collection->MetaId, page->PageId};
142142
}
143143

144144
TString ToString() const {
@@ -159,15 +159,15 @@ class TSharedPageCache : public TActorBootstrapped<TSharedPageCache> {
159159
};
160160

161161
struct TSize {
162-
static ui64 Get(const TPage *x) {
163-
Y_DEBUG_ABORT_UNLESS(x->State == PageStateLoaded);
164-
return sizeof(TPage) + x->Size;
162+
static ui64 Get(const TPage *page) {
163+
Y_DEBUG_ABORT_UNLESS(page->State == PageStateLoaded);
164+
return sizeof(TPage) + page->Size;
165165
}
166166
};
167167

168168
struct TCacheFlags1 {
169-
static ui32 Get(const TPage *x) {
170-
return x->CacheFlags1;
169+
static ui32 Get(const TPage *page) {
170+
return page->CacheFlags1;
171171
}
172172
static void Set(TPage *x, ui32 flags) {
173173
Y_ABORT_UNLESS(flags < (1 << 4));
@@ -176,12 +176,12 @@ class TSharedPageCache : public TActorBootstrapped<TSharedPageCache> {
176176
};
177177

178178
struct TCacheFlags2 {
179-
static ui32 Get(const TPage *x) {
180-
return x->CacheFlags2;
179+
static ui32 Get(const TPage *page) {
180+
return page->CacheFlags2;
181181
}
182-
static void Set(TPage *x, ui32 flags) {
182+
static void Set(TPage *page, ui32 flags) {
183183
Y_ABORT_UNLESS(flags < (1 << 4));
184-
x->CacheFlags2 = flags;
184+
page->CacheFlags2 = flags;
185185
}
186186
};
187187
};

0 commit comments

Comments
 (0)