cachetable-prefetch-close-test.cc 3.35 KB
Newer Older
1 2
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 4

// verify that closing the cachetable with prefetches in progress works
5
#ident "$Id$"
6
#ident "Copyright (c) 2007-2012 Tokutek Inc.  All rights reserved."
7 8 9
#include "includes.h"
#include "test.h"

Yoni Fogel's avatar
Yoni Fogel committed
10
bool expect_pf;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
11

12 13
static void
flush (CACHEFILE f __attribute__((__unused__)),
14
       int UU(fd),
15 16
       CACHEKEY k  __attribute__((__unused__)),
       void *v     __attribute__((__unused__)),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
17
       void** UU(dd),
18
       void *e     __attribute__((__unused__)),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
19 20
       PAIR_ATTR s      __attribute__((__unused__)),
       PAIR_ATTR* new_size      __attribute__((__unused__)),
Yoni Fogel's avatar
Yoni Fogel committed
21 22 23 24
       bool w      __attribute__((__unused__)),
       bool keep   __attribute__((__unused__)),
       bool c      __attribute__((__unused__)),
        bool UU(is_clone)
25
       ) {
Yoni Fogel's avatar
Yoni Fogel committed
26
    assert(w == false);
27 28 29 30 31 32
}

static int fetch_calls = 0;

static int
fetch (CACHEFILE f        __attribute__((__unused__)),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
33
       PAIR UU(p),
34
       int UU(fd),
35
       CACHEKEY k         __attribute__((__unused__)),
Yoni Fogel's avatar
Yoni Fogel committed
36
       uint32_t fullhash __attribute__((__unused__)),
37
       void **value       __attribute__((__unused__)),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
38
       void** UU(dd),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
39
       PAIR_ATTR *sizep        __attribute__((__unused__)),
40
       int  *dirtyp       __attribute__((__unused__)),
41
       void *extraargs    __attribute__((__unused__))
42 43 44
       ) {

    fetch_calls++;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
45
    sleep(2);
46 47

    *value = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
48
    *sizep = make_pair_attr(1);
49
    *dirtyp = 0;
50 51 52 53

    return 0;
}

Yoni Fogel's avatar
Yoni Fogel committed
54
static void cachetable_prefetch_full_test (bool partial_fetch) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
55
    const int test_limit = 2;
Yoni Fogel's avatar
Yoni Fogel committed
56
    expect_pf = false;
57 58 59
    int r;
    CACHETABLE ct;
    r = toku_create_cachetable(&ct, test_limit, ZERO_LSN, NULL_LOGGER); assert(r == 0);
60
    char fname1[] = __SRCFILE__ "test1.dat";
61 62
    unlink(fname1);
    CACHEFILE f1;
Yoni Fogel's avatar
Yoni Fogel committed
63
    r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
64

Zardosht Kasheff's avatar
Zardosht Kasheff committed
65
    // prefetch block 0. this will take 2 seconds.
66
    CACHEKEY key = make_blocknum(0);
Yoni Fogel's avatar
Yoni Fogel committed
67
    uint32_t fullhash = toku_cachetable_hash(f1, make_blocknum(0));
Zardosht Kasheff's avatar
Zardosht Kasheff committed
68 69 70 71

    // if we want to do a test of partial fetch,
    // we first put the key into the cachefile so that
    // the subsequent prefetch does a partial fetch
Zardosht Kasheff's avatar
Zardosht Kasheff committed
72 73
    CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
    wc.flush_callback = flush;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
74
    if (partial_fetch) {
Yoni Fogel's avatar
Yoni Fogel committed
75
        expect_pf = true;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
76 77 78 79 80 81 82 83
        void* value;
        long size;
        r = toku_cachetable_get_and_pin(
            f1, 
            key, 
            fullhash, 
            &value, 
            &size, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
84
            wc, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
85
            fetch,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
86 87
            def_pf_req_callback,
            def_pf_callback,
Yoni Fogel's avatar
Yoni Fogel committed
88
            true, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
89 90 91
            0
            );
        assert(r==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
92
        r = toku_test_cachetable_unpin(f1, key, fullhash, CACHETABLE_CLEAN, make_pair_attr(1));
Zardosht Kasheff's avatar
Zardosht Kasheff committed
93 94
    }
    
Zardosht Kasheff's avatar
Zardosht Kasheff committed
95
    r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, NULL);
96 97 98 99
    toku_cachetable_verify(ct);

    // close with the prefetch in progress. the close should block until
    // all of the reads and writes are complete.
Yoni Fogel's avatar
Yoni Fogel committed
100
    r = toku_cachefile_close(&f1, 0, false, ZERO_LSN); assert(r == 0);
101 102 103 104 105 106
    r = toku_cachetable_close(&ct); assert(r == 0 && ct == 0);
}

int
test_main(int argc, const char *argv[]) {
    default_parse_args(argc, argv);
Yoni Fogel's avatar
Yoni Fogel committed
107 108
    cachetable_prefetch_full_test(true);
    cachetable_prefetch_full_test(false);
109 110
    return 0;
}