remotecache 0.0.0

A general purpose cache with possibly multiple remote servers for storing and retrieving data.
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::{
    any::Any,
    path::Path,
    sync::{Arc, Mutex},
    time::Duration,
};

use serde::{de::DeserializeOwned, Serialize};
use test_log::test;
use tokio::runtime::Handle;

use crate::{
    error::{Error, Result},
    persistent::client::{
        create_runtime, create_server_and_clients, setup_test, ServerKind,
        TEST_SERVER_HEARTBEAT_TIMEOUT,
    },
    tests::Key,
    CacheHandle,
};

use crate::persistent::client::{Client, ClientKind};

pub(crate) const BASIC_TEST_NAMESPACE: &str = "test";
pub(crate) const BASIC_TEST_PARAM: (u64, u64) = (3, 5);
pub(crate) const BASIC_TEST_GENERATE_FN: fn(&(u64, u64)) -> u64 = tuple_sum;
pub(crate) const BASIC_TEST_ALT_NAMESPACE: &str = "test_alt";
pub(crate) const BASIC_TEST_ALT_GENERATE_FN: fn(&(u64, u64)) -> u64 = tuple_multiply;

pub(crate) fn cached_generate<
    K: Serialize + Send + Sync + Any,
    V: Serialize + DeserializeOwned + Send + Sync + Any,
>(
    client: &Client,
    duration: Option<Duration>,
    count: Option<Arc<Mutex<u64>>>,
    namespace: impl Into<String>,
    key: K,
    generate_fn_inner: impl FnOnce(&K) -> V + Send + Any,
) -> CacheHandle<V> {
    client.generate(namespace, key, move |k| {
        if let Some(duration) = duration {
            std::thread::sleep(duration);
        }
        let value = generate_fn_inner(k);
        if let Some(inner) = count {
            *inner.lock().unwrap() += 1;
        }
        value
    })
}

pub(crate) fn tuple_sum(tuple: &(u64, u64)) -> u64 {
    tuple.0 + tuple.1
}

pub(crate) fn tuple_multiply(tuple: &(u64, u64)) -> u64 {
    tuple.0 * tuple.1
}

/// Generates values corresponding to the same key in two namespaces, potentially multiple times.
///
/// The generate function for each namespace should only be called once, adding 2 to the count of
/// generate function calls (unless the values are already computed before calling this function.
pub(crate) fn run_basic_test(
    root: impl AsRef<Path>,
    client_kind: ClientKind,
    count: Option<Arc<Mutex<u64>>>,
    duration: Option<Duration>,
    handle: &Handle,
) -> Result<()> {
    let root = root.as_ref();

    let (_, local, remote) =
        create_server_and_clients(root.to_path_buf(), client_kind.into(), handle);

    let client = match client_kind {
        ClientKind::Local => local,
        ClientKind::Remote => remote,
    };

    let handle1 = cached_generate(
        &client,
        duration,
        count.clone(),
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_GENERATE_FN,
    );
    let handle2 = cached_generate(
        &client,
        duration,
        count.clone(),
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_GENERATE_FN,
    );

    assert_eq!(*handle1.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));
    assert_eq!(*handle2.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));

    let handle1 = cached_generate(
        &client,
        duration,
        count.clone(),
        BASIC_TEST_ALT_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_ALT_GENERATE_FN,
    );
    let handle2 = cached_generate(
        &client,
        duration,
        count,
        BASIC_TEST_ALT_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_ALT_GENERATE_FN,
    );

    assert_eq!(
        *handle1.get(),
        BASIC_TEST_ALT_GENERATE_FN(&BASIC_TEST_PARAM)
    );
    assert_eq!(
        *handle2.get(),
        BASIC_TEST_ALT_GENERATE_FN(&BASIC_TEST_PARAM)
    );

    Ok(())
}

pub(crate) fn run_basic_persistence_test(test_name: &str, client_kind: ClientKind) -> Result<()> {
    let (root, count, runtime) = setup_test(test_name)?;

    run_basic_test(
        &root,
        client_kind,
        Some(count.clone()),
        None,
        runtime.handle(),
    )?;

    runtime.shutdown_timeout(Duration::from_millis(500));
    let runtime = create_runtime();

    let (_, local, remote) =
        create_server_and_clients(root.clone(), client_kind.into(), runtime.handle());

    let client = match client_kind {
        ClientKind::Local => local,
        ClientKind::Remote => remote,
    };

    let handle = cached_generate(
        &client,
        None,
        Some(count.clone()),
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_GENERATE_FN,
    );
    assert_eq!(*handle.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));

    let handle = cached_generate(
        &client,
        None,
        Some(count.clone()),
        BASIC_TEST_ALT_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_ALT_GENERATE_FN,
    );
    assert_eq!(*handle.get(), BASIC_TEST_ALT_GENERATE_FN(&BASIC_TEST_PARAM));

    assert_eq!(*count.lock().unwrap(), 2);

    Ok(())
}

pub(crate) fn run_basic_long_running_task_test(
    test_name: &str,
    client_kind: ClientKind,
) -> Result<()> {
    let (root, count, runtime) = setup_test(test_name)?;
    run_basic_test(
        root,
        client_kind,
        Some(count.clone()),
        Some(TEST_SERVER_HEARTBEAT_TIMEOUT + Duration::from_millis(500)),
        runtime.handle(),
    )?;
    assert_eq!(*count.lock().unwrap(), 2);
    Ok(())
}

pub(crate) fn run_failure_test(
    test_name: &str,
    client_kind: ClientKind,
    restart_server: bool,
) -> Result<()> {
    let (root, count, mut runtime) = setup_test(test_name)?;

    let (_, local, remote) =
        create_server_and_clients(root.clone(), client_kind.into(), runtime.handle());

    let mut client = match client_kind {
        ClientKind::Local => local,
        ClientKind::Remote => remote,
    };

    // Generator should panic and stop sending heartbeats. Since the generator does not
    // successfully, the task should be reassigned.
    let handle1 = cached_generate(
        &client,
        None,
        None,
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        |_param| -> u64 { panic!() },
    );

    assert!(matches!(handle1.get_err().as_ref(), Error::Panic));

    if restart_server {
        runtime.shutdown_timeout(Duration::from_millis(500));
        runtime = create_runtime();

        let (_, local, remote) =
            create_server_and_clients(root, client_kind.into(), runtime.handle());

        client = match client_kind {
            ClientKind::Local => local,
            ClientKind::Remote => remote,
        };
    }

    // The task should be assigned once, and new requesters should be able to retrieve the new
    // value.
    let handle2 = cached_generate(
        &client,
        None,
        Some(count.clone()),
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_GENERATE_FN,
    );
    let handle3 = cached_generate(
        &client,
        None,
        Some(count.clone()),
        BASIC_TEST_NAMESPACE,
        BASIC_TEST_PARAM,
        BASIC_TEST_GENERATE_FN,
    );

    assert_eq!(*handle2.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));
    assert_eq!(*handle3.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));
    assert_eq!(*count.lock().unwrap(), 1);
    Ok(())
}

pub(crate) fn run_cacheable_api_test(test_name: &str, client_kind: ClientKind) -> Result<()> {
    let (root, _, runtime) = setup_test(test_name)?;

    let (_, local, remote) = create_server_and_clients(root, client_kind.into(), runtime.handle());

    let client = match client_kind {
        ClientKind::Local => local,
        ClientKind::Remote => remote,
    };

    let handle1 = client.get(BASIC_TEST_NAMESPACE, Key(0));
    let handle2 = client.get(BASIC_TEST_NAMESPACE, Key(5));
    let handle3 = client.get(BASIC_TEST_NAMESPACE, Key(8));

    assert_eq!(*handle1.unwrap_inner(), 0);
    assert_eq!(
        format!("{}", handle2.unwrap_err_inner().root_cause()),
        "invalid key"
    );
    assert!(matches!(handle3.get_err().as_ref(), Error::Panic));

    let state = Arc::new(Mutex::new(Vec::new()));
    let handle1 = client.get_with_state(BASIC_TEST_ALT_NAMESPACE, Key(0), state.clone());
    let handle2 = client.get_with_state(BASIC_TEST_ALT_NAMESPACE, Key(5), state.clone());
    let handle3 = client.get_with_state(BASIC_TEST_ALT_NAMESPACE, Key(8), state.clone());

    assert_eq!(*handle1.unwrap_inner(), 0);
    assert_eq!(
        format!("{}", handle2.unwrap_err_inner().root_cause()),
        "invalid key"
    );
    assert!(matches!(handle3.get_err().as_ref(), Error::Panic));

    assert_eq!(state.lock().unwrap().clone(), vec![0]);

    Ok(())
}

#[test]
fn servers_cannot_be_started_with_same_root() -> Result<()> {
    let (root, _, runtime) = setup_test("servers_cannot_be_started_with_same_root")?;
    let (_, _, _) = create_server_and_clients(root.clone(), ServerKind::Local, runtime.handle());
    let (server2, _, _) = create_server_and_clients(root, ServerKind::Remote, runtime.handle());
    assert!(server2.get().is_err());
    Ok(())
}

#[test]
fn local_server_persists_cached_values() -> Result<()> {
    run_basic_persistence_test("local_server_persists_cached_values", ClientKind::Local)
}

#[test]
fn remote_server_persists_cached_values() -> Result<()> {
    run_basic_persistence_test("remote_server_persists_cached_values", ClientKind::Remote)
}

#[test]
fn local_client_cacheable_api_works() -> Result<()> {
    run_cacheable_api_test("local_client_cacheable_api_works", ClientKind::Local)
}

#[test]
fn remote_client_cacheable_api_works() -> Result<()> {
    run_cacheable_api_test("remote_client_cacheable_api_works", ClientKind::Remote)
}

#[test]
fn local_remote_apis_work_concurrently() -> Result<()> {
    let (root, count, runtime) = setup_test("local_remote_apis_work_concurrently")?;

    let (_, local, remote) =
        create_server_and_clients(root.to_path_buf(), ServerKind::Both, runtime.handle());

    let mut handles = Vec::new();

    for _ in 0..5 {
        for client in [&local, &remote] {
            handles.push(cached_generate(
                client,
                None,
                Some(count.clone()),
                BASIC_TEST_NAMESPACE,
                BASIC_TEST_PARAM,
                BASIC_TEST_GENERATE_FN,
            ));
        }
    }

    for handle in handles {
        assert_eq!(*handle.get(), BASIC_TEST_GENERATE_FN(&BASIC_TEST_PARAM));
    }

    assert_eq!(*count.lock().unwrap(), 1);

    Ok(())
}

#[test]
fn local_server_does_not_reassign_long_running_tasks() -> Result<()> {
    run_basic_long_running_task_test(
        "local_server_does_not_reassign_long_running_tasks",
        ClientKind::Local,
    )
}

#[test]
fn remote_server_does_not_reassign_long_running_tasks() -> Result<()> {
    run_basic_long_running_task_test(
        "remote_server_does_not_reassign_long_running_tasks",
        ClientKind::Remote,
    )
}

#[test]
fn local_server_reassigns_failed_tasks() -> Result<()> {
    run_failure_test(
        "local_server_reassigns_failed_tasks",
        ClientKind::Local,
        false,
    )
}

#[test]
fn remote_server_reassigns_failed_tasks() -> Result<()> {
    run_failure_test(
        "remote_server_reassigns_failed_tasks",
        ClientKind::Remote,
        false,
    )
}

#[test]
fn local_server_recovers_from_failures_on_restart() -> Result<()> {
    run_failure_test(
        "local_server_recovers_from_failures_on_restart",
        ClientKind::Local,
        true,
    )
}

#[test]
fn remote_server_recovers_from_failures_on_restart() -> Result<()> {
    run_failure_test(
        "remote_server_recovers_from_failures_on_restart",
        ClientKind::Remote,
        true,
    )
}