File: ib_trx_is_interrupted.c

package info (click to toggle)
haildb 2.3.2-1.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,844 kB
  • sloc: ansic: 106,695; sh: 11,023; lex: 664; python: 571; yacc: 530; makefile: 265
file content (557 lines) | stat: -rw-r--r-- 13,181 bytes parent folder | download | duplicates (3)
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/***********************************************************************
Copyright (c) 2009 Innobase Oy. All rights reserved.
Copyright (c) 2009 Oracle. All rights reserved.
Copyright (c) 2010 Stewart Smith

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

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.

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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

************************************************************************/

/*
 * This test is the same as ddl except that we have trx_is_interrupted
 * set for the 3rd index being created.
 *
 * We also test what happens if we don't set client data.
 */

#include <stdio.h>
#include <assert.h>

#include "haildb.h"

#include "test0aux.h"

#include <stdarg.h>
#include <string.h>


#define DATABASE	"test"
#define TABLE		"ib_ddl"

/*********************************************************************
Create an InnoDB database (sub-directory). */
static
ib_err_t
create_database(
/*============*/
	const char*	name)
{
	ib_bool_t	err;

	err = ib_database_create(name);
	assert(err == IB_TRUE);

	return(DB_SUCCESS);
}

/*********************************************************************
CREATE TABLE T(C1 INT, C2 VARCHAR(10), C3 BLOB); */
static
ib_err_t
create_table(
/*=========*/
	const char*	dbname,			/*!< in: database name */
	const char*	name)			/*!< in: table name */
{
	ib_trx_t	ib_trx;
	ib_id_t		table_id = 0;
	ib_err_t	err = DB_SUCCESS;
	ib_tbl_sch_t	ib_tbl_sch = NULL;
	char		table_name[IB_MAX_TABLE_NAME_LEN];

#ifdef __WIN__
	sprintf(table_name, "%s/%s", dbname, name);
#else
	snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name);
#endif

	/* Pass a table page size of 0, ie., use default page size. */
	err = ib_table_schema_create(
		table_name, &ib_tbl_sch, IB_TBL_COMPACT, 0);

	assert(err == DB_SUCCESS);

	err = ib_table_schema_add_col(
		ib_tbl_sch, "c1", IB_INT, IB_COL_NONE, 0, sizeof(ib_i32_t));

	assert(err == DB_SUCCESS);

	err = ib_tbl_sch_add_varchar_col(ib_tbl_sch, "c2", 10);
	assert(err == DB_SUCCESS);

	err = ib_tbl_sch_add_blob_col(ib_tbl_sch, "c3");
	assert(err == DB_SUCCESS);

	/* create table */
	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	ib_trx_set_client_data(ib_trx, "create_table");
	err = ib_schema_lock_exclusive(ib_trx);
	assert(err == DB_SUCCESS);

	err = ib_table_create(ib_trx, ib_tbl_sch, &table_id);

	if (err == DB_SUCCESS) {
		err = ib_trx_commit(ib_trx);
	} else {
		fprintf(stderr, "Table: %s create failed: %s\n",
				table_name, ib_strerror(err));

		err = ib_trx_rollback(ib_trx);
	}
	assert(err == DB_SUCCESS);

	if (ib_tbl_sch != NULL) {
		ib_table_schema_delete(ib_tbl_sch);
	}

	return(err);
}

/*********************************************************************
Open a table and return a cursor for the table. */
static
ib_err_t
open_table(
/*=======*/
	const char*	dbname,		/*!< in: database name */
	const char*	name,		/*!< in: table name */
	ib_trx_t	ib_trx,		/*!< in: transaction */
	ib_crsr_t*	crsr)		/*!< out: innodb cursor */
{
	ib_err_t	err = DB_SUCCESS;
	char		table_name[IB_MAX_TABLE_NAME_LEN];

#ifdef __WIN__
	sprintf(table_name, "%s/%s", dbname, name);
#else
	snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name);
#endif

	err = ib_cursor_open_table(table_name, ib_trx, crsr);
	assert(err == DB_SUCCESS);

	return(err);
}

/*********************************************************************
INSERT INTO T VALUE(0, RANDOM(TEXT), RANDOM(TEXT)); ... 100 */
static
ib_err_t
insert_random_rows(
/*===============*/
	ib_crsr_t	crsr)		/*!< in, out: cursor to use for write */
{
	ib_i32_t	i;
	ib_err_t	err;
	ib_tpl_t	tpl;
	char*		ptr = malloc(8192);

	tpl = ib_clust_read_tuple_create(crsr);
	assert(tpl != NULL);

	for (i = 0;  i < 100; ++i) {
		int		l;

		err = ib_tuple_write_i32(tpl, 0, i % 10);
		assert(err == DB_SUCCESS);

		l = gen_rand_text(ptr, 10);
		err = ib_col_set_value(tpl, 1, ptr, l);
		assert(err == DB_SUCCESS);

		l = gen_rand_text(ptr, 8192);
		err = ib_col_set_value(tpl, 2, ptr, l);
		assert(err == DB_SUCCESS);

		err = ib_cursor_insert_row(crsr, tpl);
		assert(err == DB_SUCCESS);

		tpl = ib_tuple_clear(tpl);
		assert(tpl != NULL);
	}

	if (tpl != NULL) {
		ib_tuple_delete(tpl);
	}

	free(ptr);

	return(err);
}

/*********************************************************************
Create a secondary indexes on a table.
@return	DB_SUCCESS or error code */
static
ib_err_t
create_sec_index(
/*=============*/
	const char*	table_name,	/*!< in: table name */
	const char*	col_name,	/*!< in: column name */
	int		prefix_len)	/*!< in: prefix index length */

{
	ib_err_t	err;
	ib_trx_t	ib_trx;
	ib_id_t		index_id = 0;
	ib_idx_sch_t	ib_idx_sch = NULL;
	char		index_name[IB_MAX_TABLE_NAME_LEN];

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);

	/* also test when we don't set client_data */

	if (strcmp(col_name, "c1") != 0)
		ib_trx_set_client_data(ib_trx, col_name);

	err = ib_schema_lock_exclusive(ib_trx);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, col_name);
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, col_name);
#endif
	err = ib_index_schema_create(
		ib_trx, index_name, table_name, &ib_idx_sch);

	assert(err == DB_SUCCESS);

	err = ib_index_schema_add_col(ib_idx_sch, col_name, prefix_len);
	assert(err == DB_SUCCESS);

	err = ib_index_create(ib_idx_sch, &index_id);

	if (ib_idx_sch != NULL) {
		ib_index_schema_delete(ib_idx_sch);
		ib_idx_sch = NULL;
	}

	if (err == DB_SUCCESS) {
		err = ib_trx_commit(ib_trx);
	} else {
		err = ib_trx_rollback(ib_trx);
	}
	assert(err == DB_SUCCESS);

	return(err);
}

/*********************************************************************
Create secondary indexes on T(C1), T(C2), T(C3). */
static
ib_err_t
create_sec_index_1(
/*===============*/
	const char*	dbname,			/*!< in: database name */
	const char*	name)			/*!< in: table to drop */
{
	ib_err_t	err;
	char		table_name[IB_MAX_TABLE_NAME_LEN];

#ifdef __WIN__
	sprintf(table_name, "%s/%s", dbname, name);
#else
	snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name);
#endif

	err = create_sec_index(table_name, "c1", 0);

	if (err == DB_SUCCESS) {
		err = create_sec_index(table_name, "c2", 0);
	}

	if (err == DB_SUCCESS) {
		err = create_sec_index(table_name, "c3", 10);
	}

	return(err);
}

/*********************************************************************
Open the secondary index. */
static
ib_err_t
open_sec_index(
/*===========*/
	ib_crsr_t	crsr,		/*!< in: table cusor */
	const char* 	index_name)	/*!< in: sec. index to open */
{
	ib_err_t	err;
	ib_crsr_t	idx_crsr;

	err = ib_cursor_open_index_using_name(crsr, index_name, &idx_crsr);
	assert(err == DB_SUCCESS);

	err = ib_cursor_close(idx_crsr);
	assert(err == DB_SUCCESS);

	return(err);
}
/*********************************************************************
Open the secondary indexes on T(C1), T(C2), T(C3). */
static
ib_err_t
open_sec_index_1(
/*=============*/
	const char*	dbname,		/*!< in: database name */
	const char* 	name)		/*!< in: table name */
{
	ib_crsr_t	crsr;
	ib_trx_t	ib_trx;
	ib_err_t	err = DB_SUCCESS;
	char		index_name[IB_MAX_TABLE_NAME_LEN];
	char		table_name[IB_MAX_TABLE_NAME_LEN];

#ifdef __WIN__
	sprintf(table_name, "%s/%s", dbname, name);
#else
	snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name);
#endif

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	ib_trx_set_client_data(ib_trx, "open_sec_index_1");

	err = ib_cursor_open_table(table_name, ib_trx, &crsr);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c1");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c1");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c2");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c2");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

#ifdef __WIN__
	sprintf(index_name, "%s_%s", table_name, "c3");
#else
	snprintf(index_name, sizeof(index_name), "%s_%s", table_name, "c3");
#endif
	err = open_sec_index(crsr, index_name);
	assert(err == DB_SUCCESS);

	err = ib_cursor_close(crsr);
	assert(err == DB_SUCCESS);

	err = ib_trx_commit(ib_trx);
	assert(err == DB_SUCCESS);

	return(err);
}

int killed_lock_wait= 0;

ib_err_t test_kill_during_lock_wait(
	const char*	dbname,		/*!< in: database name */
	const char* 	name)		/*!< in: table name */
{
	ib_trx_t locking_trx, kill_trx;
	ib_crsr_t locking_cursor, kill_cursor;
	ib_err_t err;
	ib_tpl_t locking_tpl, kill_tpl;
	char		table_name[IB_MAX_TABLE_NAME_LEN];

#ifdef __WIN__
	sprintf(table_name, "%s/%s", dbname, name);
#else
	snprintf(table_name, sizeof(table_name), "%s/%s", dbname, name);
#endif

	locking_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	assert(locking_trx);

	err = ib_cursor_open_table(table_name, locking_trx, &locking_cursor);
	assert(err == DB_SUCCESS);

	err = ib_cursor_set_lock_mode(locking_cursor, IB_LOCK_X);
	assert(err == DB_SUCCESS);

	locking_tpl = ib_clust_read_tuple_create(locking_cursor);

	assert(err == DB_SUCCESS);

	ib_cursor_first(locking_cursor);

	int rows=0;
	while(err == DB_SUCCESS)
	{
		err = ib_cursor_read_row(locking_cursor, locking_tpl);
		ib_tuple_clear(locking_tpl);
		ib_cursor_next(locking_cursor);
		rows++;
	}
	printf("Read %d rows from table with IB_LOCK_X\n", rows-1);

	ib_tuple_delete(locking_tpl);

	/* All rows in the table are now locked */
	/* We now start a new transaction and attempt to do the same
	   We should end up being aborted due to chk_thd_killed()
	   saying that this trx has been killed */

	kill_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	assert(kill_trx);
	ib_trx_set_client_data(kill_trx, "kill_in_lock_wait");

	err = ib_cursor_open_table(table_name, kill_trx, &kill_cursor);
	assert(err == DB_SUCCESS);

	err = ib_cursor_set_lock_mode(kill_cursor, IB_LOCK_X);
	assert(err == DB_SUCCESS);

	kill_tpl = ib_clust_read_tuple_create(kill_cursor);
	assert(err == DB_SUCCESS);

	err = ib_cursor_first(kill_cursor);
	assert(err == DB_LOCK_WAIT_TIMEOUT);
	assert(killed_lock_wait == 1);

	ib_tuple_delete(kill_tpl);

	/* since we have DB_LOCK_WAIT_TIMEOUT, we release the already
	   rolled back transaction */
	err = ib_cursor_close(kill_cursor);
	assert(err == DB_SUCCESS);

	err = ib_trx_release(kill_trx);
	assert(err == DB_SUCCESS);

	/* cleanup */

	err = ib_cursor_close(locking_cursor);
	assert(err == DB_SUCCESS);
	err = ib_trx_commit(locking_trx);
	assert(err == DB_SUCCESS);

	return DB_SUCCESS;
}

int check_thd_killed(void* data);

int not_killed_thds= 0;
int killed_thds= 0;

int check_thd_killed(void* data)
{
	char* thd_name= (char*)data;

	printf("Checking if THD is KILLED: %s\n", thd_name);

	assert(strcmp(thd_name, "c1") != 0);

	if (strcmp(thd_name, "c3") == 0)
	{
		killed_thds++;
		return 1;
	}

	if (strcmp(thd_name, "kill_in_lock_wait") == 0)
	{
		killed_lock_wait= 1;
		killed_thds++;
		return 1;
	}


	not_killed_thds++;
	return 0;
}

int
main(int argc, char** argv)
{
	ib_err_t	err;
	ib_crsr_t	crsr;
	ib_trx_t	ib_trx;
	ib_u64_t	version;

	(void) argc;
	(void) argv;

	version = ib_api_version();
	printf("API: %d.%d.%d\n",
		(int) (version >> 32),			/* Current version */
		(int) ((version >> 16)) & 0xffff,	/* Revisiion */
	       	(int) (version & 0xffff));		/* Age */

	err = ib_init();
	assert(err == DB_SUCCESS);

	test_configure();

	err = ib_startup("barracuda");
	assert(err == DB_SUCCESS);

	fprintf(stderr, "Set trx_is_interrupted handler\n");

	ib_set_trx_is_interrupted_handler(check_thd_killed);

	err = create_database(DATABASE);
	assert(err == DB_SUCCESS);

	err = create_table(DATABASE, TABLE);
	assert(err == DB_SUCCESS);

	ib_trx = ib_trx_begin(IB_TRX_REPEATABLE_READ);
	ib_trx_set_client_data(ib_trx, "main");
	assert(ib_trx != NULL);

	err = open_table(DATABASE, TABLE, ib_trx, &crsr);
	assert(err == DB_SUCCESS);

	err = ib_cursor_lock(crsr, IB_LOCK_IX);
	assert(err == DB_SUCCESS);

	err = insert_random_rows(crsr);
	assert(err == DB_SUCCESS);

	err = ib_cursor_close(crsr);
	assert(err == DB_SUCCESS);
	crsr = NULL;

	err = ib_trx_commit(ib_trx);
	assert(err == DB_SUCCESS);

	err = create_sec_index_1(DATABASE, TABLE);
	assert(err == DB_SUCCESS);

	err = open_sec_index_1(DATABASE, TABLE);
	assert(err == DB_SUCCESS);

	err = test_kill_during_lock_wait(DATABASE, TABLE);
	assert(err == DB_SUCCESS);

	err = drop_table(DATABASE, TABLE);
	assert(err == DB_SUCCESS);

	err = ib_shutdown(IB_SHUTDOWN_NORMAL);
	assert(err == DB_SUCCESS);

	assert(killed_thds > 0);
	assert(not_killed_thds > 0);
	assert(killed_thds == 3); /* index creation *and* 2 for lock wait */

	printf("Killed %d and didn't kill %d\n", killed_thds, not_killed_thds);

	return(0);
}