-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathredis_store_tester.rs
More file actions
344 lines (307 loc) · 11.2 KB
/
redis_store_tester.rs
File metadata and controls
344 lines (307 loc) · 11.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use core::sync::atomic::{AtomicUsize, Ordering};
use core::time::Duration;
use std::borrow::Cow;
use std::env;
use std::sync::{Arc, RwLock};
use bytes::Bytes;
use clap::{Parser, ValueEnum};
use futures::TryStreamExt;
use nativelink_config::stores::{RedisMode, RedisSpec};
use nativelink_error::{Code, Error, ResultExt};
use nativelink_store::redis_store::RedisStore;
use nativelink_util::buf_channel::make_buf_channel_pair;
use nativelink_util::store_trait::{
SchedulerCurrentVersionProvider, SchedulerIndexProvider, SchedulerStore,
SchedulerStoreDataProvider, SchedulerStoreDecodeTo, SchedulerStoreKeyProvider, StoreDriver,
StoreKey, StoreLike, TrueValue, UploadSizeInfo,
};
use nativelink_util::telemetry::init_tracing;
use nativelink_util::{background_spawn, spawn};
use rand::Rng;
use tokio::time::sleep;
use tracing::{error, info};
// Define test structures that implement the scheduler traits
#[derive(Debug, Clone, PartialEq)]
struct TestSchedulerData {
key: String,
content: String,
version: i64,
}
#[derive(Debug)]
struct TestSchedulerReturn {
version: i64,
}
impl SchedulerStoreKeyProvider for TestSchedulerData {
type Versioned = TrueValue; // Using versioned storage
fn get_key(&self) -> StoreKey<'static> {
StoreKey::Str(Cow::Owned(self.key.clone()))
}
}
impl SchedulerStoreDataProvider for TestSchedulerData {
fn try_into_bytes(self) -> Result<Bytes, Error> {
Ok(Bytes::from(self.content.into_bytes()))
}
fn get_indexes(&self) -> Result<Vec<(&'static str, Bytes)>, Error> {
// Add some test indexes - need to use 'static strings
Ok(vec![
("test_index", Bytes::from("test_value")),
(
"content_prefix",
Bytes::from(self.content.chars().take(10).collect::<String>()),
),
])
}
}
impl SchedulerStoreDecodeTo for TestSchedulerData {
type DecodeOutput = TestSchedulerReturn;
fn decode(version: i64, _data: Bytes) -> Result<Self::DecodeOutput, Error> {
Ok(TestSchedulerReturn { version })
}
}
impl SchedulerCurrentVersionProvider for TestSchedulerData {
fn current_version(&self) -> i64 {
self.version
}
}
struct SearchByContentPrefix {
prefix: String,
}
impl SchedulerIndexProvider for SearchByContentPrefix {
const KEY_PREFIX: &'static str = "test:";
const INDEX_NAME: &'static str = "content_prefix";
type Versioned = TrueValue;
fn index_value(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.prefix)
}
}
impl SchedulerStoreKeyProvider for SearchByContentPrefix {
type Versioned = TrueValue;
fn get_key(&self) -> StoreKey<'static> {
StoreKey::Str(Cow::Owned("dummy_key".to_string()))
}
}
impl SchedulerStoreDecodeTo for SearchByContentPrefix {
type DecodeOutput = TestSchedulerReturn;
fn decode(version: i64, data: Bytes) -> Result<Self::DecodeOutput, Error> {
TestSchedulerData::decode(version, data)
}
}
const MAX_KEY: u16 = 1024;
/// Wrapper type for CLI parsing since we can't implement foreign traits on foreign types.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum RedisModeArg {
Cluster,
Sentinel,
#[default]
Standard,
}
impl From<RedisModeArg> for RedisMode {
fn from(arg: RedisModeArg) -> Self {
match arg {
RedisModeArg::Standard => Self::Standard,
RedisModeArg::Sentinel => Self::Sentinel,
RedisModeArg::Cluster => Self::Cluster,
}
}
}
fn random_key() -> StoreKey<'static> {
let key = rand::rng().random_range(0..MAX_KEY);
StoreKey::new_str(&key.to_string()).into_owned()
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum TestMode {
#[default]
Random,
Sequential,
}
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
#[arg(value_enum, short, long, default_value_t)]
redis_mode: RedisModeArg,
#[arg(value_enum, short, long, default_value_t)]
mode: TestMode,
}
async fn run<S: StoreDriver + SchedulerStore>(
store: Arc<S>,
max_loops: usize,
failed: Arc<RwLock<bool>>,
mode: TestMode,
) -> Result<(), Error> {
let mut count = 0;
let in_flight = Arc::new(AtomicUsize::new(0));
loop {
if count % 1000 == 0 {
info!(
"Loop count {count}. In flight: {}",
in_flight.load(Ordering::Relaxed)
);
if *failed.read().unwrap() {
return Err(Error::new(
Code::Internal,
"Failed in redis_store_tester".to_string(),
));
}
}
if count == max_loops {
loop {
let remaining = in_flight.load(Ordering::Relaxed);
if remaining == 0 {
return Ok(());
}
info!(remaining, "Remaining");
sleep(Duration::from_secs(1)).await;
}
}
count += 1;
in_flight.fetch_add(1, Ordering::Relaxed);
let store_clone = store.clone();
let local_fail = failed.clone();
let local_in_flight = in_flight.clone();
let max_action_value = 7;
let action_value = match mode {
TestMode::Random => rand::rng().random_range(0..max_action_value),
TestMode::Sequential => count % max_action_value,
};
background_spawn!("action", async move {
async fn run_action<S: StoreDriver + SchedulerStore>(
action_value: usize,
store_clone: Arc<S>,
) -> Result<(), Error> {
match action_value {
0 => {
store_clone.has(random_key()).await?;
}
1 => {
let (mut tx, rx) = make_buf_channel_pair();
tx.send(Bytes::from_static(b"12345")).await?;
tx.send_eof()?;
store_clone
.update(random_key(), rx, UploadSizeInfo::ExactSize(5))
.await?;
}
2 => {
let mut results = (0..MAX_KEY).map(|_| None).collect::<Vec<_>>();
store_clone
.has_with_results(
&(0..MAX_KEY)
.map(|i| StoreKey::Str(Cow::Owned(i.to_string())))
.collect::<Vec<_>>(),
&mut results,
)
.await?;
}
3 => {
store_clone
.update_oneshot(random_key(), Bytes::from_static(b"1234"))
.await?;
}
4 => {
let res = store_clone
.list(.., |_key| true)
.await
.err_tip(|| "In list")?;
info!(%res, "end list");
}
5 => {
let search_provider = SearchByContentPrefix {
prefix: "Searchable".to_string(),
};
for i in 0..5 {
let data = TestSchedulerData {
key: format!("test:search_key_{i}"),
content: format!("Searchable content #{i}"),
version: 0,
};
store_clone.update_data(data).await?;
}
let search_results: Vec<_> = store_clone
.search_by_index_prefix(search_provider)
.await?
.try_collect()
.await?;
info!(?search_results, "search results");
}
_ => {
let mut data = TestSchedulerData {
key: "test:scheduler_key_1".to_string(),
content: "Test scheduler data #1".to_string(),
version: 0,
};
let res = store_clone.get_and_decode(data.clone()).await?;
if let Some(existing_data) = res {
data.version = existing_data.version + 1;
}
store_clone.update_data(data).await?;
}
}
Ok(())
}
match run_action(action_value, store_clone).await {
Ok(()) => {}
Err(e) => {
error!(?e, "Error!");
*local_fail.write().unwrap() = true;
}
}
local_in_flight.fetch_sub(1, Ordering::Relaxed);
});
}
}
fn main() -> Result<(), Box<dyn core::error::Error>> {
let args = Args::parse();
let redis_mode: RedisMode = args.redis_mode.into();
let failed = Arc::new(RwLock::new(false));
let redis_host = env::var("REDIS_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let max_client_permits = env::var("MAX_REDIS_PERMITS")
.unwrap_or_else(|_| "100".to_string())
.parse()?;
let max_loops: usize = env::var("MAX_LOOPS")
.unwrap_or_else(|_| "2000000".to_string())
.parse()?;
#[expect(
clippy::disallowed_methods,
reason = "`We need `tokio::runtime::Runtime::block_on` so we can get errors _after_ threads finished"
)]
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
// The OTLP exporters need to run in a Tokio context.
spawn!("init tracing", async { init_tracing().await })
.await?
.expect("Init tracing should work");
let redis_port = match redis_mode {
RedisMode::Standard => 6379,
RedisMode::Sentinel => 26379,
RedisMode::Cluster => 7000,
};
let addr = match redis_mode {
RedisMode::Sentinel => format!("redis+sentinel://{redis_host}:{redis_port}/"),
_ => format!("redis://{redis_host}:{redis_port}/"),
};
let spec = RedisSpec {
addresses: vec![addr],
connection_timeout_ms: 1000,
max_client_permits,
mode: redis_mode,
..Default::default()
};
match spec.mode {
RedisMode::Standard | RedisMode::Sentinel => {
let store = RedisStore::new_standard(spec).await?;
run(store, max_loops, failed.clone(), args.mode).await
}
RedisMode::Cluster => {
let store = RedisStore::new_cluster(spec).await?;
run(store, max_loops, failed.clone(), args.mode).await
}
}
})
.unwrap();
if *failed.read().unwrap() {
return Err(Error::new(Code::Internal, "Failed in redis_store_tester".to_string()).into());
}
Ok(())
}