Skip to content

Commit fa5945b

Browse files
committed
test touches
1 parent 8394eab commit fa5945b

File tree

2 files changed

+93
-132
lines changed

2 files changed

+93
-132
lines changed

ydb/core/tablet_flat/shared_cache_clock_pro.h

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace NKikimr::NCache {
1111

1212
enum class EClockProPageLocation {
1313
None,
14-
Cold,
15-
Hot
14+
Hot,
15+
Cold
1616
};
1717

1818
template <typename TPage, typename TPageTraits>
@@ -32,14 +32,26 @@ class TClockProCache : public ICacheCache<TPage> {
3232
};
3333

3434
struct TPageKeyHash {
35+
using is_transparent = void;
36+
37+
inline size_t operator()(const THolder<TPageEntry>& entry) const {
38+
return TPageTraits::GetHash(entry->Key);
39+
}
40+
3541
inline size_t operator()(const TPageKey& key) const {
3642
return TPageTraits::GetHash(key);
3743
}
3844
};
3945

4046
struct TPageKeyEqual {
41-
inline bool operator()(const TPageKey& left, const TPageKey& right) const {
42-
return TPageTraits::Equals(left, right);
47+
using is_transparent = void;
48+
49+
inline bool operator()(const THolder<TPageEntry>& left, const THolder<TPageEntry>& right) const {
50+
return TPageTraits::Equals(left->Key, right->Key);
51+
}
52+
53+
inline bool operator()(const THolder<TPageEntry>& left, const TPageKey& right) const {
54+
return TPageTraits::Equals(left->Key, right);
4355
}
4456
};
4557

@@ -69,7 +81,7 @@ class TClockProCache : public ICacheCache<TPage> {
6981

7082
TIntrusiveList<TPage> Touch(TPage* page) override {
7183
if (auto it = Entries.find(TPageTraits::GetKey(page)); it != Entries.end()) {
72-
TPageEntry* entry = it->second.Get();
84+
TPageEntry* entry = it->Get();
7385
if (entry->Page) {
7486
TouchFast(entry);
7587
return {};
@@ -108,25 +120,56 @@ class TClockProCache : public ICacheCache<TPage> {
108120
TString Dump() const {
109121
TStringBuilder result;
110122

111-
// auto dump = [&](const TQueue& queue) {
112-
// size_t count = 0;
113-
// ui64 size = 0;
114-
// for (auto it = queue.Queue.begin(); it != queue.Queue.end(); it++) {
115-
// const TPage* page = &*it;
116-
// if (count != 0) result << ", ";
117-
// result << "{" << TPageTraits::GetKeyToString(page) << " " << TPageTraits::GetFrequency(page) << "f " << TPageTraits::GetSize(page) << "b}";
118-
// count++;
119-
// size += TPageTraits::GetSize(page);
120-
// }
121-
// Y_DEBUG_ABORT_UNLESS(queue.Size == size);
122-
// };
123-
124-
// result << "SmallQueue: ";
125-
// dump(SmallQueue);
126-
// result << Endl << "MainQueue: ";
127-
// dump(MainQueue);
128-
// result << Endl << "GhostQueue: ";
129-
// result << GhostQueue.Dump();
123+
size_t count = 0;
124+
ui64 sizeHot = 0, sizeCold = 0, sizeTest = 0;
125+
126+
auto it = HandHot;
127+
while (it != nullptr) {
128+
if (count != 0) result << ", ";
129+
TPageEntry* entry = it->Node();
130+
if (entry == HandHot) result << "Hot>";
131+
if (entry == HandCold) result << "Cold>";
132+
if (entry == HandTest) result << "Test>";
133+
134+
result << "{" << TPageTraits::ToString(entry->Key) << " ";
135+
136+
count++;
137+
if (entry->Page) {
138+
auto location = TPageTraits::GetLocation(entry->Page);
139+
switch (location) {
140+
case EClockProPageLocation::Hot:
141+
result << "H ";
142+
sizeHot += entry->Size;
143+
break;
144+
case EClockProPageLocation::Cold:
145+
result << "C ";
146+
sizeCold += entry->Size;
147+
break;
148+
default:
149+
Y_ABORT("Unknown location");
150+
}
151+
} else {
152+
result << "T ";
153+
sizeTest += entry->Size;
154+
}
155+
156+
if (entry->Page) {
157+
result << TPageTraits::GetReferenced(entry->Page) << "r ";
158+
}
159+
result << entry->Size << "b}";
160+
161+
it = it->Next();
162+
if (it == HandHot) break;
163+
}
164+
165+
Y_DEBUG_ABORT_UNLESS(sizeHot == SizeHot);
166+
Y_DEBUG_ABORT_UNLESS(sizeCold == SizeCold);
167+
Y_DEBUG_ABORT_UNLESS(sizeTest == SizeTest);
168+
if (count == 0) {
169+
Y_DEBUG_ABORT_UNLESS(!HandHot);
170+
Y_DEBUG_ABORT_UNLESS(!HandCold);
171+
Y_DEBUG_ABORT_UNLESS(!HandTest);
172+
}
130173

131174
return result;
132175
}
@@ -166,9 +209,9 @@ class TClockProCache : public ICacheCache<TPage> {
166209
Y_ABORT_UNLESS(TPageTraits::GetLocation(page) == EClockProPageLocation::None);
167210

168211
auto entry_ = MakeHolder<TPageEntry>(TPageTraits::GetKey(page), page, TPageTraits::GetSize(page));
169-
auto inserted = Entries.emplace(entry_->Key, std::move(entry_));
212+
auto inserted = Entries.emplace(std::move(entry_));
170213
Y_ABORT_UNLESS(inserted.second);
171-
TPageEntry* entry = inserted.first->second.Get();
214+
TPageEntry* entry = inserted.first->Get();
172215

173216
LinkEntry(entry);
174217

@@ -231,7 +274,7 @@ class TClockProCache : public ICacheCache<TPage> {
231274
ui64 ReservedSize = 0;
232275

233276
// TODO: unify this with TPageMap
234-
THashMap<TPageKey, THolder<TPageEntry>, TPageKeyHash, TPageKeyEqual> Entries;
277+
THashSet<THolder<TPageEntry>, TPageKeyHash, TPageKeyEqual> Entries;
235278

236279
TIntrusiveListItem<TPageEntry>* HandHot = nullptr;
237280
TIntrusiveListItem<TPageEntry>* HandCold = nullptr;

ydb/core/tablet_flat/shared_cache_clock_pro_ut.cpp

Lines changed: 23 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -95,115 +95,33 @@ Y_UNIT_TEST_SUITE(TClockProCache) {
9595
}
9696

9797
Y_UNIT_TEST(Touch) {
98-
TClockProCache<TPage, TPageTraits> cache(100);
98+
TClockProCache<TPage, TPageTraits> cache(10);
99+
100+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "");
99101

100102
TPage page1{1, 2};
101103
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{});
102-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
103-
<< "SmallQueue: {1 0f 2b}" << Endl
104-
<< "MainQueue: " << Endl
105-
<< "GhostQueue: "));
104+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Cold>Test>{1 C 0r 2b}");
106105

107106
TPage page2{2, 3};
108107
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
109-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
110-
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}" << Endl
111-
<< "MainQueue: " << Endl
112-
<< "GhostQueue: "));
113-
108+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Test>{1 C 0r 2b}, Cold>{2 C 0r 3b}");
109+
114110
TPage page3{3, 4};
115111
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
116-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
117-
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}" << Endl
118-
<< "MainQueue: " << Endl
119-
<< "GhostQueue: "));
112+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Test>{1 C 0r 2b}, Cold>{2 C 0r 3b}, {3 C 0r 4b}");
120113

121114
TPage page4{4, 1};
122115
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<ui32>{});
123-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
124-
<< "SmallQueue: {1 0f 2b}, {2 0f 3b}, {3 0f 4b}, {4 0f 1b}" << Endl
125-
<< "MainQueue: " << Endl
126-
<< "GhostQueue: "));
116+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Test>{1 C 0r 2b}, Cold>{2 C 0r 3b}, {3 C 0r 4b}, {4 C 0r 1b}");
127117

128118
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{});
129-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{});
130-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
131119
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
132-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
133-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
134-
<< "SmallQueue: {1 2f 2b}, {2 1f 3b}, {3 2f 4b}, {4 0f 1b}" << Endl
135-
<< "MainQueue: " << Endl
136-
<< "GhostQueue: "));
137-
138-
TPage page5{5, 8};
139-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page5), TVector<ui32>{2});
140-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
141-
<< "SmallQueue: {4 0f 1b}, {5 0f 8b}" << Endl
142-
<< "MainQueue: {1 2f 2b}, {3 2f 4b}" << Endl
143-
<< "GhostQueue: {2 3b}"));
144-
145-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
146-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
147-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
148-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
149-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
150-
<< "SmallQueue: {4 0f 1b}, {5 0f 8b}" << Endl
151-
<< "MainQueue: {1 2f 2b}, {3 3f 4b}" << Endl
152-
<< "GhostQueue: {2 3b}"));
153-
154-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
155-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
156-
<< "SmallQueue: {4 0f 1b}, {5 0f 8b}" << Endl
157-
<< "MainQueue: {1 2f 2b}, {3 3f 4b}, {2 0f 3b}" << Endl
158-
<< "GhostQueue: "));
159-
}
120+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Test>{1 C 1r 2b}, Cold>{2 C 0r 3b}, {3 C 1r 4b}, {4 C 0r 1b}");
160121

161-
Y_UNIT_TEST(Touch_MainQueue) {
162-
TClockProCache<TPage, TPageTraits> cache(100);
163-
164-
TPage page1{1, 20};
165-
TPage page2{2, 30};
166-
TPage page3{3, 40};
167-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{1});
168-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{2});
169-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{3});
170-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
171-
<< "SmallQueue: " << Endl
172-
<< "MainQueue: " << Endl
173-
<< "GhostQueue: {1 20b}, {2 30b}, {3 40b}"));
174-
175-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
176-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{});
177-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
178-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
179-
<< "SmallQueue: " << Endl
180-
<< "MainQueue: {2 0f 30b}, {1 0f 20b}, {3 0f 40b}" << Endl
181-
<< "GhostQueue: "));
182-
183-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page1), TVector<ui32>{});
184-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
185-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page2), TVector<ui32>{});
186-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
187-
<< "SmallQueue: " << Endl
188-
<< "MainQueue: {2 2f 30b}, {1 1f 20b}, {3 0f 40b}" << Endl
189-
<< "GhostQueue: "));
190-
191-
TPage page4{4, 20};
192-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<ui32>{4});
193-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
194-
<< "SmallQueue: " << Endl
195-
<< "MainQueue: {2 2f 30b}, {1 1f 20b}, {3 0f 40b}" << Endl
196-
<< "GhostQueue: {4 20b}"));
197-
198-
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<ui32>{3});
199-
// MainQueue: {2 2f 30b}, {1 1f 20b}, {3 0f 40b}, {4 0f 20b}
200-
// MainQueue: {1 1f 20b}, {3 0f 40b}, {4 0f 20b}, {2 1f 30b}
201-
// MainQueue: {3 0f 40b}, {4 0f 20b}, {2 1f 30b}, {1 0f 20b}
202-
// MainQueue: {4 0f 20b}, {2 1f 30b}, {1 0f 20b}
203-
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
204-
<< "SmallQueue: " << Endl
205-
<< "MainQueue: {4 0f 20b}, {2 1f 30b}, {1 0f 20b}" << Endl
206-
<< "GhostQueue: "));
122+
TPage page5{5, 1};
123+
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page5), TVector<ui32>{});
124+
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), "Hot>Test>{1 C 1r 2b}, Cold>{2 C 0r 3b}, {3 C 1r 4b}, {4 C 0r 1b}, {5 C 0r 1b}");
207125
}
208126

209127
Y_UNIT_TEST(EvictNext) {
@@ -227,31 +145,31 @@ Y_UNIT_TEST_SUITE(TClockProCache) {
227145
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
228146
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
229147
<< "SmallQueue: " << Endl
230-
<< "MainQueue: {2 0f 30b}, {1 1f 20b}, {3 0f 40b}" << Endl
148+
<< "MainQueue: {2 0r 30b}, {1 1r 20b}, {3 0r 40b}" << Endl
231149
<< "GhostQueue: "));
232150

233151
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<ui32>{});
234152
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
235-
<< "SmallQueue: {4 0f 10b}" << Endl
236-
<< "MainQueue: {2 0f 30b}, {1 1f 20b}, {3 0f 40b}" << Endl
153+
<< "SmallQueue: {4 0r 10b}" << Endl
154+
<< "MainQueue: {2 0r 30b}, {1 1r 20b}, {3 0r 40b}" << Endl
237155
<< "GhostQueue: "));
238156

239157
UNIT_ASSERT_VALUES_EQUAL(EvictNext(cache), 4);
240158
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
241159
<< "SmallQueue: " << Endl
242-
<< "MainQueue: {2 0f 30b}, {1 1f 20b}, {3 0f 40b}" << Endl
160+
<< "MainQueue: {2 0r 30b}, {1 1r 20b}, {3 0r 40b}" << Endl
243161
<< "GhostQueue: {4 10b}"));
244162

245163
UNIT_ASSERT_VALUES_EQUAL(EvictNext(cache), 2);
246164
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
247165
<< "SmallQueue: " << Endl
248-
<< "MainQueue: {1 1f 20b}, {3 0f 40b}" << Endl
166+
<< "MainQueue: {1 1r 20b}, {3 0r 40b}" << Endl
249167
<< "GhostQueue: {4 10b}"));
250168

251169
UNIT_ASSERT_VALUES_EQUAL(EvictNext(cache), 3);
252170
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
253171
<< "SmallQueue: " << Endl
254-
<< "MainQueue: {1 0f 20b}" << Endl
172+
<< "MainQueue: {1 0r 20b}" << Endl
255173
<< "GhostQueue: {4 10b}"));
256174

257175
UNIT_ASSERT_VALUES_EQUAL(EvictNext(cache), 1);
@@ -287,21 +205,21 @@ Y_UNIT_TEST_SUITE(TClockProCache) {
287205
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page3), TVector<ui32>{});
288206
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
289207
<< "SmallQueue: " << Endl
290-
<< "MainQueue: {2 0f 30b}, {1 0f 20b}, {3 0f 40b}" << Endl
208+
<< "MainQueue: {2 0r 30b}, {1 0r 20b}, {3 0r 40b}" << Endl
291209
<< "GhostQueue: "));
292210

293211
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page4), TVector<ui32>{});
294212
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
295-
<< "SmallQueue: {4 0f 10b}" << Endl
296-
<< "MainQueue: {2 0f 30b}, {1 0f 20b}, {3 0f 40b}" << Endl
213+
<< "SmallQueue: {4 0r 10b}" << Endl
214+
<< "MainQueue: {2 0r 30b}, {1 0r 20b}, {3 0r 40b}" << Endl
297215
<< "GhostQueue: "));
298216

299217
cache.UpdateLimit(45);
300218
TPage page5{5, 1};
301219
UNIT_ASSERT_VALUES_EQUAL(Touch(cache, page5), (TVector<ui32>{4, 2, 1}));
302220
UNIT_ASSERT_VALUES_EQUAL(cache.Dump(), (TString)(TStringBuilder()
303-
<< "SmallQueue: {5 0f 1b}" << Endl
304-
<< "MainQueue: {3 0f 40b}" << Endl
221+
<< "SmallQueue: {5 0r 1b}" << Endl
222+
<< "MainQueue: {3 0r 40b}" << Endl
305223
<< "GhostQueue: {4 10b}"));
306224

307225
cache.UpdateLimit(0);

0 commit comments

Comments
 (0)