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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuFT, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
The technology is licensed by the Massachusetts Institute of
Technology, Rutgers State University of New Jersey, and the Research
Foundation of State University of New York at Stony Brook under
United States of America Serial No. 11/760379 and to the patents
and/or patent applications resulting from it.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
This software is covered by US Patent No. 8,489,638.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#ident "Copyright (c) 2010-2013 Tokutek Inc. All rights reserved."
#ident "$Id$"
#include "test.h"
#include "toku_pthread.h"
#include <db.h>
#include <sys/stat.h>
#include "key-val.h"
toku_mutex_t put_lock;
enum {NUM_INDEXER_INDEXES=1};
static const int NUM_DBS = NUM_INDEXER_INDEXES + 1; // 1 for source DB
static const int NUM_ROWS = 100000;
static int num_rows;
static const int FORWARD = 0;
static const int BACKWARD = 1;
typedef int Direction;
static const int TXN_CREATE = 1;
static const int TXN_END = 2;
typedef int TxnWork;
DB_ENV *env;
/*
* client() is a routine intended to be run in a separate thread from index creation
* - it takes a client spec which describes work to be done
* - direction : move to ever increasing or decreasing rows
* - txnwork : whether a transaction should be created or closed within the client
* (allows client transaction to start before or during index creation,
* and to close during or after index creation)
*/
typedef struct {
uint32_t num; // number of rows to write
uint32_t start; // approximate start row
int offset; // offset from stride (= MAX_CLIENTS)
Direction dir;
TxnWork txnwork;
DB_TXN *txn;
DB **dbs;
int client_number;
uint32_t *flags;
} client_spec_t, *client_spec;
int client_count = 0;
static void * client(void *arg)
{
client_spec CAST_FROM_VOIDP(cs, arg);
client_count++;
if ( verbose ) printf("client[%d]\n", cs->client_number);
assert(cs->client_number < MAX_CLIENTS);
assert(cs->dir == FORWARD || cs->dir == BACKWARD);
int r;
if ( cs->txnwork & TXN_CREATE ) { r = env->txn_begin(env, NULL, &cs->txn, 0); CKERR(r); }
DBT key, val;
DBT dest_keys[NUM_DBS];
DBT dest_vals[NUM_DBS];
uint32_t k, v;
int n = cs->start;
for(int which=0;which<NUM_DBS;which++) {
dbt_init(&dest_keys[which], NULL, 0);
dest_keys[which].flags = DB_DBT_REALLOC;
dbt_init(&dest_vals[which], NULL, 0);
dest_vals[which].flags = DB_DBT_REALLOC;
}
int rr = 0;
int retry = 0;
for (uint32_t i = 0; i < cs->num; i++ ) {
DB_TXN *txn;
env->txn_begin(env, cs->txn, &txn, 0);
k = key_to_put(n, cs->offset);
v = generate_val(k, 0);
dbt_init(&key, &k, sizeof(k));
dbt_init(&val, &v, sizeof(v));
while ( retry++ < 10 ) {
toku_mutex_lock(&put_lock);
rr = env_put_multiple_test_no_array(env,
cs->dbs[0],
txn,
&key,
&val,
NUM_DBS,
cs->dbs, // dest dbs
dest_keys,
dest_vals,
cs->flags);
toku_mutex_unlock(&put_lock);
if ( rr == 0 ) break;
sleep(0);
}
if ( rr != 0 ) {
if ( verbose ) printf("client[%u] : put_multiple returns %d, i=%u, n=%u, key=%u\n", cs->client_number, rr, i, n, k);
r = txn->abort(txn); CKERR(r);
break;
}
r = txn->commit(txn, 0); CKERR(r);
n = ( cs->dir == FORWARD ) ? n + 1 : n - 1;
retry = 0;
}
if ( cs->txnwork & TXN_END ) { r = cs->txn->commit(cs->txn, DB_TXN_SYNC); CKERR(r); }
if (verbose) printf("client[%d] done\n", cs->client_number);
for (int which=0; which<NUM_DBS; which++) {
toku_free(dest_keys[which].data);
toku_free(dest_vals[which].data);
}
return 0;
}
toku_pthread_t *client_threads;
client_spec_t *client_specs;
static void clients_init(DB **dbs, uint32_t *flags)
{
XMALLOC_N(MAX_CLIENTS, client_threads);
XMALLOC_N(MAX_CLIENTS, client_specs);
client_specs[0].client_number = 0;
// client_specs[0].start = 0;
client_specs[0].start = num_rows - 1;
client_specs[0].num = num_rows;
client_specs[0].offset = -1;
// client_specs[0].dir = FORWARD;
client_specs[0].dir = BACKWARD;
client_specs[0].txnwork = TXN_CREATE | TXN_END;
client_specs[0].txn = NULL;
client_specs[0].dbs = dbs;
client_specs[0].flags = flags;
client_specs[1].client_number = 1;
client_specs[1].start = 0;
client_specs[1].num = num_rows;
client_specs[1].offset = 1;
client_specs[1].dir = FORWARD;
client_specs[1].txnwork = TXN_CREATE | TXN_END;
client_specs[1].txn = NULL;
client_specs[1].dbs = dbs;
client_specs[1].flags = flags;
}
static void clients_cleanup(void)
{
toku_free(client_threads); client_threads = NULL;
toku_free(client_specs); client_specs = NULL;
}
// verify results
// - read the keys in the primary table, then calculate what keys should exist
// in the other DB. Read the other table to verify.
static void check_results(DB *src, DB *db)
{
int r;
int pass = 1;
int clients = client_count;
int max_rows = ( clients + 1 ) * num_rows;
unsigned int *db_keys = (unsigned int *) toku_malloc(max_rows * sizeof (unsigned int));
DBT key, val;
unsigned int k=0, v=0;
dbt_init(&key, &k, sizeof(unsigned int));
dbt_init(&val, &v, sizeof(unsigned int));
DB_TXN *txn;
r = env->txn_begin(env, NULL, &txn, 0); CKERR(r);
DBC *cursor;
r = src->cursor(src, txn, &cursor, 0); CKERR(r);
int which = *(uint32_t*)db->app_private;
// scan the primary table,
// calculate the expected keys in 'db'
int row = 0;
while ( r != DB_NOTFOUND ) {
r = cursor->c_get(cursor, &key, &val, DB_NEXT);
if ( r != DB_NOTFOUND ) {
k = *((uint32_t *)(key.data));
db_keys[row] = twiddle32(k, which);
row++;
}
}
if ( verbose ) printf("primary table scanned, contains %d rows\n", row);
int primary_rows = row;
r = cursor->c_close(cursor); CKERR(r);
// sort the expected keys
qsort(db_keys, primary_rows, sizeof (unsigned int), uint_cmp);
if ( verbose > 1 ) {
for(int i=0;i<primary_rows;i++) {
printf("primary table[%u] = %u\n", i, db_keys[i]);
}
}
// scan the indexer-created DB, comparing keys with expected keys
// - there should be exactly 'primary_rows' in the new index
r = db->cursor(db, txn, &cursor, 0); CKERR(r);
for (int i=0;i<primary_rows;i++) {
r = cursor->c_get(cursor, &key, &val, DB_NEXT);
if ( r == DB_NOTFOUND ) {
printf("scan of index finds last row is %d\n", i);
}
CKERR(r);
k = *((uint32_t *)(key.data));
if ( db_keys[i] != k ) {
if ( verbose ) printf("ERROR expecting key %10u for row %d, found key = %10u\n", db_keys[i],i,k);
pass = 0;
i++;
// goto check_results_error;
}
}
// next cursor op should return DB_NOTFOUND
r = cursor->c_get(cursor, &key, &val, DB_NEXT);
assert(r == DB_NOTFOUND);
// we're done - cleanup and close
//check_results_error:
r = cursor->c_close(cursor); CKERR(r);
toku_free(db_keys);
r = txn->commit(txn, 0); CKERR(r);
if ( verbose ) {
if ( pass ) printf("check_results : pass\n");
else printf("check_results : fail\n");
}
assert(pass);
return;
}
static void test_indexer(DB *src, DB **dbs)
{
int r;
DB_TXN *txn;
DB_INDEXER *indexer;
uint32_t db_flags[NUM_DBS];
if ( verbose ) printf("test_indexer\n");
for(int i=0;i<NUM_DBS;i++) {
db_flags[i] = 0;
}
clients_init(dbs, db_flags);
// create and initialize indexer
r = env->txn_begin(env, NULL, &txn, 0);
CKERR(r);
if ( verbose ) printf("test_indexer create_indexer\n");
toku_mutex_lock(&put_lock);
r = env->create_indexer(env, txn, &indexer, src, NUM_DBS-1, &dbs[1], db_flags, 0);
CKERR(r);
r = indexer->set_error_callback(indexer, NULL, NULL);
CKERR(r);
r = indexer->set_poll_function(indexer, poll_print, NULL);
CKERR(r);
toku_mutex_unlock(&put_lock);
// start threads doing additional inserts - no lock issues since indexer already created
r = toku_pthread_create(&client_threads[0], 0, client, (void *)&client_specs[0]); CKERR(r);
// r = toku_pthread_create(&client_threads[1], 0, client, (void *)&client_specs[1]); CKERR(r);
struct timeval start, now;
if ( verbose ) {
printf("test_indexer build\n");
gettimeofday(&start,0);
}
r = indexer->build(indexer);
CKERR(r);
if ( verbose ) {
gettimeofday(&now,0);
int duration = (int)(now.tv_sec - start.tv_sec);
if ( duration > 0 )
printf("test_indexer build : sec = %d\n", duration);
}
if ( verbose ) printf("test_indexer close\n");
toku_mutex_lock(&put_lock);
r = indexer->close(indexer);
CKERR(r);
toku_mutex_unlock(&put_lock);
r = txn->commit(txn, DB_TXN_SYNC);
CKERR(r);
void *t0;
r = toku_pthread_join(client_threads[0], &t0); CKERR(r);
// void *t1;
// r = toku_pthread_join(client_threads[1], &t1); CKERR(r);
clients_cleanup();
if ( verbose ) printf("check_results\n");
check_results(src, dbs[1]);
if ( verbose ) printf("PASS\n");
if ( verbose ) printf("test_indexer done\n");
}
static void run_test(void)
{
int r;
toku_mutex_init(&put_lock, NULL);
toku_os_recursive_delete(TOKU_TEST_FILENAME);
r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
char logname[TOKU_PATH_MAX+1];
r = toku_os_mkdir(toku_path_join(logname, 2, TOKU_TEST_FILENAME, "log"), S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
r = db_env_create(&env, 0); CKERR(r);
r = env->set_redzone(env, 0); CKERR(r);
r = env->set_lg_dir(env, "log"); CKERR(r);
r = env->set_default_bt_compare(env, uint_dbt_cmp); CKERR(r);
generate_permute_tables();
r = env->set_generate_row_callback_for_put(env, put_multiple_generate); CKERR(r);
int envflags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE | DB_INIT_LOG;
r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
env->set_errfile(env, stderr);
r = env->checkpointing_set_period(env, 0); CKERR(r);
DBT desc;
dbt_init(&desc, "foo", sizeof("foo"));
int ids[MAX_DBS];
DB *dbs[MAX_DBS];
for (int i = 0; i < NUM_DBS; i++) {
ids[i] = i;
r = db_create(&dbs[i], env, 0); CKERR(r);
dbs[i]->app_private = &ids[i];
char key_name[32];
sprintf(key_name, "key%d", i);
r = dbs[i]->open(dbs[i], NULL, key_name, NULL, DB_BTREE, DB_AUTO_COMMIT|DB_CREATE, 0666); CKERR(r);
IN_TXN_COMMIT(env, NULL, txn_desc, 0, {
{ int chk_r = dbs[i]->change_descriptor(dbs[i], txn_desc, &desc, 0); CKERR(chk_r); }
});
}
// generate the src DB (do not use put_multiple)
DB_TXN *txn;
r = env->txn_begin(env, NULL, &txn, 0); CKERR(r);
r = generate_initial_table(dbs[0], txn, num_rows); CKERR(r);
r = txn->commit(txn, DB_TXN_SYNC); CKERR(r);
// -------------------------- //
if (1) test_indexer(dbs[0], dbs);
// -------------------------- //
for(int i=0;i<NUM_DBS;i++) {
r = dbs[i]->close(dbs[i], 0); CKERR(r);
}
toku_mutex_destroy(&put_lock);
r = env->close(env, 0); CKERR(r);
}
// ------------ infrastructure ----------
static inline void
do_args (int argc, char * const argv[]) {
const char *progname=argv[0];
num_rows = NUM_ROWS;
argc--; argv++;
while (argc>0) {
if (strcmp(argv[0],"-v")==0) {
verbose++;
} else if (strcmp(argv[0],"-q")==0) {
verbose=0;
} else if (strcmp(argv[0],"-r")==0) {
argc--; argv++;
num_rows = atoi(argv[0]);
} else {
fprintf(stderr, "Usage:\n %s [-v] [-q] [-r rows]\n", progname);
exit(1);
}
argc--; argv++;
}
}
int test_main(int argc, char * const *argv) {
do_args(argc, argv);
run_test();
return 0;
}
/*
* Please ignore this code - I don't think I'm going to use it, but I don't want to lose it
* I will delete this later - Dave
if ( rr != 0 ) { // possible lock deadlock
if (verbose > 1) {
printf("client[%u] : put_multiple returns %d, i=%u, n=%u, key=%u\n", cs->client_number, rr, i, n, k);
if ( verbose > 2 ) print_engine_status(env);
}
// abort the transaction, freeing up locks associated with previous put_multiples
if ( verbose > 1 ) printf("start txn abort\n");
r = txn->abort(txn); CKERR(r);
if ( verbose > 1 ) printf(" txn aborted\n");
sleep(2 + cs->client_number);
// now retry, waiting until the deadlock resolves itself
r = env->txn_begin(env, cs->txn, &txn, 0); CKERR(r);
if ( verbose > 1 ) printf("txn begin\n");
while ( rr != 0 ) {
rr = env->put_multiple(env,
cs->dbs[0],
txn,
&key,
&val,
NUM_DBS,
cs->dbs, // dest dbs
dest_keys,
dest_vals,
cs->flags,
NULL);
if ( rr != 0 ) {
if ( verbose ) printf("client[%u] : put_multiple returns %d, i=%u, n=%u, key=%u\n", cs->client_number, rr, i, n, k);
if ( verbose ) printf("start txn abort\n");
r = txn->abort(txn); CKERR(r);
if ( verbose ) printf(" txn aborted\n");
sleep(2 + cs->client_number);
r = env->txn_begin(env, cs->txn, &txn, 0); CKERR(r);
if ( verbose ) printf("txn begin\n");
}
}
*/
|