| 1 | /* |
|---|
| 2 | * (C) 2001 Clemson University and The University of Chicago |
|---|
| 3 | * |
|---|
| 4 | * See COPYING in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <unistd.h> |
|---|
| 9 | |
|---|
| 10 | #include "pvfs2.h" |
|---|
| 11 | #include "ncache.h" |
|---|
| 12 | #include "gossip.h" |
|---|
| 13 | #include "pvfs2-internal.h" |
|---|
| 14 | |
|---|
| 15 | /* this is a test program that exercises the ncache interface and |
|---|
| 16 | * demonstrates how to use it. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | int main(int argc, char **argv) |
|---|
| 20 | { |
|---|
| 21 | int ret = -1; |
|---|
| 22 | |
|---|
| 23 | PVFS_object_ref test_ref; |
|---|
| 24 | |
|---|
| 25 | PVFS_object_ref root_ref = {100,0}; |
|---|
| 26 | |
|---|
| 27 | PVFS_object_ref first_ref = {1,0}; |
|---|
| 28 | char first_name[] = "first"; |
|---|
| 29 | |
|---|
| 30 | PVFS_object_ref second_ref = {2,0}; |
|---|
| 31 | char second_name[] = "second"; |
|---|
| 32 | |
|---|
| 33 | PVFS_object_ref third_ref = {3,0}; |
|---|
| 34 | char third_name[] = "third"; |
|---|
| 35 | |
|---|
| 36 | /* set debugging stuff */ |
|---|
| 37 | gossip_enable_stderr(); |
|---|
| 38 | gossip_set_debug_mask(1, GOSSIP_NCACHE_DEBUG); |
|---|
| 39 | |
|---|
| 40 | /* initialize the cache */ |
|---|
| 41 | ret = PINT_ncache_initialize(); |
|---|
| 42 | if(ret < 0) |
|---|
| 43 | { |
|---|
| 44 | fprintf(stderr, "ncache_initialize() failure.\n"); |
|---|
| 45 | return(-1); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | PINT_ncache_set_info(TCACHE_TIMEOUT_MSECS,5000); |
|---|
| 49 | |
|---|
| 50 | /* try to lookup something when there is nothing in there */ |
|---|
| 51 | ret = PINT_ncache_get_cached_entry((const char*) "first", |
|---|
| 52 | &test_ref, |
|---|
| 53 | (const PVFS_object_ref*) &root_ref); |
|---|
| 54 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 55 | { |
|---|
| 56 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 57 | return(-1); |
|---|
| 58 | } |
|---|
| 59 | if (ret == 0) |
|---|
| 60 | { |
|---|
| 61 | fprintf(stderr, "Failure: lookup succeeded when it shouldn't have.\n"); |
|---|
| 62 | return(-1); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /* insert a few things */ |
|---|
| 66 | ret = PINT_ncache_update((const char*) first_name, |
|---|
| 67 | (const PVFS_object_ref*) &first_ref, |
|---|
| 68 | (const PVFS_object_ref*) &root_ref); |
|---|
| 69 | if(ret < 0) |
|---|
| 70 | { |
|---|
| 71 | fprintf(stderr, "Error: failed to insert entry.\n"); |
|---|
| 72 | return(-1); |
|---|
| 73 | } |
|---|
| 74 | ret = PINT_ncache_update((const char*) second_name, |
|---|
| 75 | (const PVFS_object_ref*) &second_ref, |
|---|
| 76 | (const PVFS_object_ref*) &first_ref); |
|---|
| 77 | if(ret < 0) |
|---|
| 78 | { |
|---|
| 79 | fprintf(stderr, "Error: failed to insert entry.\n"); |
|---|
| 80 | return(-1); |
|---|
| 81 | } |
|---|
| 82 | ret = PINT_ncache_update((const char*) third_name, |
|---|
| 83 | (const PVFS_object_ref*) &third_ref, |
|---|
| 84 | (const PVFS_object_ref*) &second_ref); |
|---|
| 85 | if(ret < 0) |
|---|
| 86 | { |
|---|
| 87 | fprintf(stderr, "Error: failed to insert entry.\n"); |
|---|
| 88 | return(-1); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /* lookup a few things */ |
|---|
| 92 | ret = PINT_ncache_get_cached_entry((const char*) "first", |
|---|
| 93 | &test_ref, |
|---|
| 94 | (const PVFS_object_ref*) &root_ref); |
|---|
| 95 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 96 | { |
|---|
| 97 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 98 | return(-1); |
|---|
| 99 | } |
|---|
| 100 | if (ret == -PVFS_ENOENT || test_ref.handle != first_ref.handle) |
|---|
| 101 | { |
|---|
| 102 | fprintf(stderr, "Failure: lookup didn't return correct " |
|---|
| 103 | "handle (%s != %s)\n", PVFS_handle_to_str(test_ref.handle), |
|---|
| 104 | PVFS_handle_to_str(first_ref.handle)); |
|---|
| 105 | return(-1); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | ret = PINT_ncache_get_cached_entry((const char*) "second", |
|---|
| 109 | &test_ref, |
|---|
| 110 | (const PVFS_object_ref*) &first_ref); |
|---|
| 111 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 112 | { |
|---|
| 113 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 114 | return(-1); |
|---|
| 115 | } |
|---|
| 116 | if (ret == -PVFS_ENOENT || test_ref.handle != second_ref.handle) |
|---|
| 117 | { |
|---|
| 118 | fprintf(stderr, "Failure: lookup didn't return correct " |
|---|
| 119 | "handle (%s != %s)\n", PVFS_handle_to_str(test_ref.handle), |
|---|
| 120 | PVFS_handle_to_str(second_ref.handle)); |
|---|
| 121 | return(-1); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | ret = PINT_ncache_get_cached_entry((const char*) "third", |
|---|
| 125 | &test_ref, |
|---|
| 126 | (const PVFS_object_ref*) &second_ref); |
|---|
| 127 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 128 | { |
|---|
| 129 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 130 | return(-1); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if ((ret == -PVFS_ENOENT) || (test_ref.handle != third_ref.handle)) |
|---|
| 134 | { |
|---|
| 135 | fprintf(stderr, "Failure: lookup didn't return correct " |
|---|
| 136 | "handle (%s != %s)\n", PVFS_handle_to_str(test_ref.handle), |
|---|
| 137 | PVFS_handle_to_str(third_ref.handle)); |
|---|
| 138 | return(-1); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /* sleep a little bit to let entries expire, then retry */ |
|---|
| 142 | printf("sleeping a few seconds before trying cache again.\n"); |
|---|
| 143 | sleep(7); |
|---|
| 144 | |
|---|
| 145 | ret = PINT_ncache_get_cached_entry((const char*) "second", |
|---|
| 146 | &test_ref, |
|---|
| 147 | (const PVFS_object_ref*) &first_ref); |
|---|
| 148 | if ((ret < 0) && (ret != -PVFS_ENOENT)) |
|---|
| 149 | { |
|---|
| 150 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 151 | return(-1); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (ret != -PVFS_ENOENT) |
|---|
| 155 | { |
|---|
| 156 | fprintf(stderr, "Failure: lookup didn't return correct " |
|---|
| 157 | "handle (%s != %s)\n", PVFS_handle_to_str(test_ref.handle), |
|---|
| 158 | PVFS_handle_to_str(first_ref.handle)); |
|---|
| 159 | fprintf(stderr, "Failure: lookup didn't return correct handle.\n"); |
|---|
| 160 | return(-1); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | ret = PINT_ncache_get_cached_entry((const char*) "first", |
|---|
| 164 | &test_ref, |
|---|
| 165 | (const PVFS_object_ref*) &root_ref); |
|---|
| 166 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 167 | { |
|---|
| 168 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 169 | return(-1); |
|---|
| 170 | } |
|---|
| 171 | if (ret != -PVFS_ENOENT) |
|---|
| 172 | { |
|---|
| 173 | fprintf(stderr, "Failure: lookup didn't return correct handle.\n"); |
|---|
| 174 | return(-1); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | ret = PINT_ncache_get_cached_entry((const char*) "third", |
|---|
| 178 | &test_ref, |
|---|
| 179 | (const PVFS_object_ref*) &second_ref); |
|---|
| 180 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 181 | { |
|---|
| 182 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 183 | return(-1); |
|---|
| 184 | } |
|---|
| 185 | if (ret != -PVFS_ENOENT) |
|---|
| 186 | { |
|---|
| 187 | fprintf(stderr, "Failure: lookup didn't return correct handle.\n"); |
|---|
| 188 | return(-1); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | /* try inserting twice */ |
|---|
| 192 | ret = PINT_ncache_update((const char*) first_name, |
|---|
| 193 | (const PVFS_object_ref*) &first_ref, |
|---|
| 194 | (const PVFS_object_ref*) &root_ref); |
|---|
| 195 | if(ret < 0) |
|---|
| 196 | { |
|---|
| 197 | fprintf(stderr, "Error: failed to insert entry.\n"); |
|---|
| 198 | return(-1); |
|---|
| 199 | } |
|---|
| 200 | ret = PINT_ncache_update(first_name, |
|---|
| 201 | (const PVFS_object_ref*) &first_ref, |
|---|
| 202 | (const PVFS_object_ref*) &root_ref); |
|---|
| 203 | if(ret < 0) |
|---|
| 204 | { |
|---|
| 205 | fprintf(stderr, "Error: failed to insert entry.\n"); |
|---|
| 206 | return(-1); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /* then remove once */ |
|---|
| 210 | PINT_ncache_invalidate((const char*) first_name, |
|---|
| 211 | (const PVFS_object_ref*) &root_ref); |
|---|
| 212 | |
|---|
| 213 | /* lookup the same entry, shouldn't get it */ |
|---|
| 214 | ret = PINT_ncache_get_cached_entry((const char*) "first", |
|---|
| 215 | &test_ref, |
|---|
| 216 | (const PVFS_object_ref*) &root_ref); |
|---|
| 217 | if (ret < 0 && ret != -PVFS_ENOENT) |
|---|
| 218 | { |
|---|
| 219 | fprintf(stderr, "ncache_lookup() failure.\n"); |
|---|
| 220 | return(-1); |
|---|
| 221 | } |
|---|
| 222 | if (ret != -PVFS_ENOENT) |
|---|
| 223 | { |
|---|
| 224 | fprintf(stderr, "Failure: lookup didn't return correct handle.\n"); |
|---|
| 225 | return(-1); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /* then remove again - found flag should be zero now */ |
|---|
| 229 | PINT_ncache_invalidate((const char*) first_name, |
|---|
| 230 | (const PVFS_object_ref*) &root_ref); |
|---|
| 231 | |
|---|
| 232 | /* finalize the cache */ |
|---|
| 233 | PINT_ncache_finalize(); |
|---|
| 234 | gossip_disable(); |
|---|
| 235 | |
|---|
| 236 | return(0); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /* |
|---|
| 240 | * Local variables: |
|---|
| 241 | * mode: c |
|---|
| 242 | * c-indent-level: 4 |
|---|
| 243 | * c-basic-offset: 4 |
|---|
| 244 | * End: |
|---|
| 245 | * |
|---|
| 246 | * vim: ts=8 sts=4 sw=4 expandtab |
|---|
| 247 | */ |
|---|