forked from redpanda-data/redpanda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_cache_test.cc
More file actions
274 lines (213 loc) · 9.4 KB
/
batch_cache_test.cc
File metadata and controls
274 lines (213 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2025 Redpanda Data, Inc.
*
* Licensed as a Redpanda Enterprise file under the Redpanda Community
* License (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://github.com/redpanda-data/redpanda/blob/master/licenses/rcl.md
*/
#include "cloud_topics/batch_cache/batch_cache.h"
#include "model/fundamental.h"
#include "model/record.h"
#include "model/tests/random_batch.h"
#include "redpanda/tests/fixture.h"
#include <seastar/core/sstring.hh>
#include <gtest/gtest.h>
namespace cloud_topics {
struct batch_cache_accessor {
static void evict_offset(
batch_cache& c, const model::topic_id_partition& tidp, model::offset o) {
c._index[tidp]->testing_evict_from_cache(o);
}
static void reclaim(
batch_cache& c, const model::topic_id_partition& tidp, size_t size) {
// it doesn't really matter what tidp is used, all the indices point to
// the same cache.
c._index[tidp]->testing_reclaim_from_cache(size);
}
static bool
contains_tidp(const batch_cache& c, const model::topic_id_partition& tidp) {
return c._index.contains(tidp);
}
};
} // namespace cloud_topics
constexpr auto cache_check_interval = 100ms;
class batch_cache_test_fixture
: public redpanda_thread_fixture
, public ::testing::Test {
public:
batch_cache_test_fixture()
: redpanda_thread_fixture()
, _cache(&app.storage.local().log_mgr(), cache_check_interval) {}
cloud_topics::batch_cache _cache;
bool contains_tidp(const model::topic_id_partition& tidp) {
return cloud_topics::batch_cache_accessor::contains_tidp(_cache, tidp);
}
void evict_offset(const model::topic_id_partition& tidp, model::offset o) {
cloud_topics::batch_cache_accessor::evict_offset(_cache, tidp, o);
}
void reclaim(const model::topic_id_partition& tidp, size_t size) {
cloud_topics::batch_cache_accessor::reclaim(_cache, tidp, size);
}
};
TEST_F(batch_cache_test_fixture, test_batch_cache_put_get) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto batch = model::test::make_random_batch(model::offset(0), 10, false);
// Put batch in cache
_cache.put(tidp, batch);
// Get batch
auto retrieved = _cache.get(tidp, model::offset(0));
ASSERT_TRUE(retrieved.has_value());
ASSERT_EQ(retrieved->base_offset(), batch.base_offset());
ASSERT_EQ(retrieved->header().record_count, batch.header().record_count);
}
TEST_F(batch_cache_test_fixture, test_batch_cache_get_nonexistent) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
// Try to get batch that doesn't exist
auto retrieved = _cache.get(tidp, model::offset(0));
ASSERT_TRUE(!retrieved.has_value());
}
TEST_F(batch_cache_test_fixture, test_batch_cache_multiple_tidps) {
auto tidp1 = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto tidp2 = model::topic_id_partition{
model::topic_id::create(), model::partition_id(1)};
auto batch1 = model::test::make_random_batch(model::offset(0), 5, false);
auto batch2 = model::test::make_random_batch(model::offset(10), 8, false);
// Put batches in cache
_cache.put(tidp1, batch1);
_cache.put(tidp2, batch2);
// Get batches
auto retrieved1 = _cache.get(tidp1, model::offset(0));
auto retrieved2 = _cache.get(tidp2, model::offset(10));
ASSERT_TRUE(retrieved1.has_value());
ASSERT_TRUE(retrieved2.has_value());
ASSERT_EQ(retrieved1->base_offset(), batch1.base_offset());
ASSERT_EQ(retrieved2->base_offset(), batch2.base_offset());
// Try to get batch with wrong offset
auto retrieved = _cache.get(tidp2, model::offset(0));
ASSERT_TRUE(!retrieved.has_value());
}
TEST_F(batch_cache_test_fixture, test_batch_cache_eviction) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto batch = model::test::make_random_batch(model::offset(42), 10, false);
// The cleanup will start in 100ms
_cache.start().get();
// Put batch in cache
_cache.put(tidp, batch);
auto retrieved = _cache.get(tidp, model::offset(42));
ASSERT_TRUE(retrieved.has_value());
ASSERT_TRUE(contains_tidp(tidp));
reclaim(tidp, 1);
ASSERT_TRUE(contains_tidp(tidp));
// This should evict the topic_id_partition
ss::sleep(cache_check_interval * 2).get();
ASSERT_FALSE(contains_tidp(tidp));
_cache.stop().get();
}
TEST_F(batch_cache_test_fixture, test_batch_cache_topic_recreation) {
// Test that recreating a topic with the same name doesn't resurrect batches
auto topic_id_1 = model::topic_id::create();
auto topic_id_2 = model::topic_id::create();
auto tidp1 = model::topic_id_partition{topic_id_1, model::partition_id(0)};
auto tidp2 = model::topic_id_partition{topic_id_2, model::partition_id(0)};
auto batch1 = model::test::make_random_batch(model::offset(0), 5, false);
auto batch2 = model::test::make_random_batch(model::offset(0), 8, false);
// Put batch for first topic
_cache.put(tidp1, batch1);
// Verify we can get it back
auto retrieved1 = _cache.get(tidp1, model::offset(0));
ASSERT_TRUE(retrieved1.has_value());
ASSERT_EQ(retrieved1->base_offset(), batch1.base_offset());
// Put batch for second topic (simulating topic recreation)
_cache.put(tidp2, batch2);
// Verify we get the correct batch for each topic
auto retrieved1_again = _cache.get(tidp1, model::offset(0));
auto retrieved2 = _cache.get(tidp2, model::offset(0));
ASSERT_TRUE(retrieved1_again.has_value());
ASSERT_TRUE(retrieved2.has_value());
// The batches should be different
ASSERT_EQ(retrieved1_again->base_offset(), batch1.base_offset());
ASSERT_EQ(retrieved2->base_offset(), batch2.base_offset());
ASSERT_NE(
retrieved1_again->header().record_count,
retrieved2->header().record_count);
}
// Create a single-record batch whose record is large enough (> 32KiB
// range_size) so each batch gets its own range in the cache. This prevents
// small-batch coalescing from invalidating unrelated entries when a shared
// range is evicted.
model::record_batch make_large_batch(model::offset o) {
model::test::record_batch_spec spec{
.offset = o,
.allow_compression = false,
.count = 1,
.record_sizes = {{33_KiB}},
};
return model::test::make_random_batch(spec);
}
TEST_F(batch_cache_test_fixture, test_evict_up_to_removes_matching_entries) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto batch0 = make_large_batch(model::offset(0));
auto batch1 = make_large_batch(model::offset(1));
auto batch2 = make_large_batch(model::offset(2));
_cache.put(tidp, batch0);
_cache.put(tidp, batch1);
_cache.put(tidp, batch2);
// All three should be retrievable
ASSERT_TRUE(_cache.get(tidp, model::offset(0)).has_value());
ASSERT_TRUE(_cache.get(tidp, model::offset(1)).has_value());
ASSERT_TRUE(_cache.get(tidp, model::offset(2)).has_value());
// Evict up to offset 1 (removes batches at 0 and 1)
_cache.evict_up_to(tidp, model::offset(1));
ASSERT_FALSE(_cache.get(tidp, model::offset(0)).has_value());
ASSERT_FALSE(_cache.get(tidp, model::offset(1)).has_value());
ASSERT_TRUE(_cache.get(tidp, model::offset(2)).has_value());
}
TEST_F(batch_cache_test_fixture, test_evict_up_to_all_entries) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto batch0 = make_large_batch(model::offset(0));
auto batch1 = make_large_batch(model::offset(1));
_cache.put(tidp, batch0);
_cache.put(tidp, batch1);
// Evict up to an offset beyond all entries
_cache.evict_up_to(tidp, model::offset(100));
ASSERT_FALSE(_cache.get(tidp, model::offset(0)).has_value());
ASSERT_FALSE(_cache.get(tidp, model::offset(1)).has_value());
}
TEST_F(batch_cache_test_fixture, test_evict_up_to_no_entries) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto batch0 = make_large_batch(model::offset(5));
_cache.put(tidp, batch0);
// Evict up to offset before any entry - nothing should be removed
_cache.evict_up_to(tidp, model::offset(3));
ASSERT_TRUE(_cache.get(tidp, model::offset(5)).has_value());
}
TEST_F(batch_cache_test_fixture, test_evict_up_to_nonexistent_tidp) {
auto tidp = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
// Evicting from a tidp that was never inserted should be a no-op
_cache.evict_up_to(tidp, model::offset(100));
ASSERT_FALSE(contains_tidp(tidp));
}
TEST_F(batch_cache_test_fixture, test_evict_up_to_does_not_affect_other_tidps) {
auto tidp1 = model::topic_id_partition{
model::topic_id::create(), model::partition_id(0)};
auto tidp2 = model::topic_id_partition{
model::topic_id::create(), model::partition_id(1)};
auto batch1 = make_large_batch(model::offset(0));
auto batch2 = make_large_batch(model::offset(0));
_cache.put(tidp1, batch1);
_cache.put(tidp2, batch2);
// Evict from tidp1 only
_cache.evict_up_to(tidp1, model::offset(100));
ASSERT_FALSE(_cache.get(tidp1, model::offset(0)).has_value());
ASSERT_TRUE(_cache.get(tidp2, model::offset(0)).has_value());
}