cachetable-eviction-getandpin-test2.c 4.01 KB
Newer Older
1 2 3
/* -*- mode: C; c-basic-offset: 4 -*- */

/* verify that get_and_pin waits while a prefetch block is pending */
Zardosht Kasheff's avatar
Zardosht Kasheff committed
4
#ident "$Id$"
5
#ident "Copyright (c) 2007-2011 Tokutek Inc.  All rights reserved."
6 7 8
#include "includes.h"
#include "test.h"

Zardosht Kasheff's avatar
Zardosht Kasheff committed
9

Zardosht Kasheff's avatar
Zardosht Kasheff committed
10 11
static void 
pe_est_callback(
12
    void* UU(ftnode_pv), 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
13
    void* UU(dd),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
14 15 16 17 18
    long* bytes_freed_estimate, 
    enum partial_eviction_cost *cost, 
    void* UU(write_extraargs)
    )
{
Zardosht Kasheff's avatar
Zardosht Kasheff committed
19 20
    *bytes_freed_estimate = 7;
    *cost = PE_EXPENSIVE;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
21 22
}

23 24
static int 
pe_callback (
25
    void *ftnode_pv __attribute__((__unused__)), 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
26 27
    PAIR_ATTR bytes_to_free __attribute__((__unused__)), 
    PAIR_ATTR* bytes_freed, 
28 29 30
    void* extraargs __attribute__((__unused__))
    ) 
{
Zardosht Kasheff's avatar
Zardosht Kasheff committed
31
    sleep(2);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
32
    *bytes_freed = make_pair_attr(bytes_to_free.size-7);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
33
    return 0;
34
}
35

36 37 38 39 40 41 42
static uint64_t tdelta_usec(struct timeval *tend, struct timeval *tstart) {
    uint64_t t = tend->tv_sec * 1000000 + tend->tv_usec;
    t -= tstart->tv_sec * 1000000 + tstart->tv_usec;
    return t;
}

static void cachetable_prefetch_maybegetandpin_test (void) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
43
    const int test_limit = 12;
44 45 46
    int r;
    CACHETABLE ct;
    r = toku_create_cachetable(&ct, test_limit, ZERO_LSN, NULL_LOGGER); assert(r == 0);
47
    char fname1[] = __SRCFILE__ "test1.dat";
48 49
    unlink(fname1);
    CACHEFILE f1;
Yoni Fogel's avatar
Yoni Fogel committed
50
    r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
51 52
    CACHEKEY key = make_blocknum(0);
    u_int32_t fullhash = toku_cachetable_hash(f1, make_blocknum(0));
53

Zardosht Kasheff's avatar
Zardosht Kasheff committed
54
    // let's get and pin this node a bunch of times to drive up the clock count
Zardosht Kasheff's avatar
Zardosht Kasheff committed
55 56 57
    CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
    wc.pe_est_callback = pe_est_callback;
    wc.pe_callback = pe_callback;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
58 59 60 61 62 63 64 65 66
    for (int i = 0; i < 20; i++) {
        void* value;
        long size;
        r = toku_cachetable_get_and_pin(
            f1, 
            key, 
            fullhash, 
            &value, 
            &size, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
67
            wc, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
68 69 70
            def_fetch,
            def_pf_req_callback,
            def_pf_callback,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
71
            TRUE, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
72 73 74
            0
            );
        assert(r==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
75
        r = toku_cachetable_unpin(f1, key, fullhash, CACHETABLE_DIRTY, make_pair_attr(8));
Zardosht Kasheff's avatar
Zardosht Kasheff committed
76 77
    }
    
78 79 80
    struct timeval tstart;
    gettimeofday(&tstart, NULL);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
81 82 83 84 85 86 87 88 89
    // fetch another block, causing an eviction of the first block we made above
    void* value2;
    long size2;
    r = toku_cachetable_get_and_pin(
        f1,
        make_blocknum(1),
        1,
        &value2,
        &size2,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
90
        wc, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
91 92 93
        def_fetch,
        def_pf_req_callback,
        def_pf_callback,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
94
        TRUE, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
95 96 97
        0
        );
    assert(r==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
98
    r = toku_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
Zardosht Kasheff's avatar
Zardosht Kasheff committed
99
        
100 101 102 103
    toku_cachetable_verify(ct);

    void *v = 0;
    long size = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
104
    // now verify that the block we are trying to evict may be pinned
Zardosht Kasheff's avatar
Zardosht Kasheff committed
105 106 107 108 109 110
    r = toku_cachetable_get_and_pin_nonblocking(
        f1, 
        key, 
        fullhash, 
        &v, 
        &size, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
111
        wc, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
112 113 114
        def_fetch, 
        def_pf_req_callback, 
        def_pf_callback, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
115
        TRUE,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
116 117 118
        NULL, 
        NULL
        );
Zardosht Kasheff's avatar
Zardosht Kasheff committed
119
    assert(r==TOKUDB_TRY_AGAIN);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
120 121 122 123 124 125
    r = toku_cachetable_get_and_pin(
        f1, 
        key, 
        fullhash, 
        &v, 
        &size, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
126
        wc, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
127 128 129
        def_fetch, 
        def_pf_req_callback, 
        def_pf_callback, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
130
        TRUE, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
131 132
        NULL
        );
Zardosht Kasheff's avatar
Zardosht Kasheff committed
133
    assert(r == 0 && v == 0 && size == 1);
134 135 136 137

    struct timeval tend; 
    gettimeofday(&tend, NULL);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
138 139 140 141
    assert(tdelta_usec(&tend, &tstart) >= 2000000); 
    if (verbose) printf("time %"PRIu64" \n", tdelta_usec(&tend, &tstart));
    toku_cachetable_verify(ct);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
142
    r = toku_cachetable_unpin(f1, key, fullhash, CACHETABLE_CLEAN, make_pair_attr(1));
Zardosht Kasheff's avatar
Zardosht Kasheff committed
143
    assert(r == 0);
144 145
    toku_cachetable_verify(ct);

146
    r = toku_cachefile_close(&f1, 0, FALSE, ZERO_LSN); assert(r == 0);
147 148 149 150 151 152 153 154 155
    r = toku_cachetable_close(&ct); assert(r == 0 && ct == 0);
}

int
test_main(int argc, const char *argv[]) {
    default_parse_args(argc, argv);
    cachetable_prefetch_maybegetandpin_test();
    return 0;
}