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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2011, 2015 Oracle and/or its affiliates. All rights reserved.
*/
/*
* This is the MVCC test for XA. It runs 3 tests, which are as follows:
* 1. No MVCC
* 2. MVCC enabled by DB_CONFIG
* 3. MVCC enabled by flags
*/
#include <sys/types.h>
#include <sys/time.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <tx.h>
#include <atmi.h>
#include <fml32.h>
#include <fml1632.h>
#include <db.h>
#include "../utilities/bdb_xa_util.h"
#define HOME "../data"
#define TABLE1 "../data/table1.db"
#define TABLE2 "../data/table2.db"
#define NUM_TESTS 4
#define NUM_THREADS 2
#define TIMEOUT 60
static int error = 1;
char *progname; /* Client run-time name. */
int
usage()
{
fprintf(stderr, "usage: %s [-v] -t[0|1|2]\n", progname);
return (EXIT_FAILURE);
}
enum test_type{NO_MVCC, MVCC_DBCONFIG, MVCC_FLAG};
struct thread_args {
char *thread_name;
int type_test;
};
/* Used to sync the threads. */
static pthread_cond_t cond_vars[NUM_TESTS*3];
static int counters[NUM_TESTS*3];
void init_tests(char *ops[], int success[], int type, int thread_num);
/*
*
*/
void *
call_server_thread(void *arguments)
{
char *thread_name, *ops[NUM_TESTS];
int ttype;
struct thread_args args;
int commit, j, success[NUM_TESTS], thread_num, ret;
void *result = NULL;
TPINIT *initBuf = NULL;
FBFR *replyBuf = NULL;
long replyLen = 0;
args = *((struct thread_args *)arguments);
thread_name = args.thread_name;
thread_num = atoi(thread_name);
ttype = args.type_test;
init_tests(ops, success, ttype, thread_num);
if (verbose)
printf("%s:%s: starting thread %i\n", progname, thread_name,
thread_num);
/* Allocate init buffer */
if ((initBuf = (TPINIT *)tpalloc("TPINIT", NULL, TPINITNEED(0))) == 0)
goto tuxedo_err;
initBuf->flags = TPMULTICONTEXTS;
if (tpinit(initBuf) == -1)
goto tuxedo_err;
if (verbose)
printf("%s:%s: tpinit() OK\n", progname, thread_name);
/* Allocate reply buffer. */
replyLen = 1024;
if ((replyBuf = (FBFR*)tpalloc("FML32", NULL, replyLen)) == NULL)
goto tuxedo_err;
if (verbose)
printf("%s:%s: tpalloc(\"FML32\"), reply buffer OK\n",
progname, thread_name);
for (j = 0; j < NUM_TESTS; j++) {
commit = 1;
/* Sync both threads. */
if ((ret = sync_thr(&(counters[j*3]), NUM_THREADS,
&(cond_vars[j*3]))) != 0) {
fprintf(stderr,
"%s:%s: Error syncing threads: %i \n",
progname, thread_name, ret);
goto end;
}
/* Begin the XA transaction. */
if (tpbegin(TIMEOUT, 0L) == -1)
goto tuxedo_err;
if (verbose)
printf("%s:%s: tpbegin() OK\n", progname, thread_name);
/* Force thread 2 to wait till thread 1 does its operation.*/
if (thread_num == 2) {
if ((ret = sync_thr(&(counters[(j*3)+1]), NUM_THREADS,
&(cond_vars[(j*3)+1]))) != 0) {
fprintf(stderr,
"%s:%s: Error syncing thread 1: %i \n",
progname, thread_name, ret);
goto end;
}
}
if (verbose)
printf("%s:%s: calling server %s\n", progname,
thread_name, ops[j]);
/* Read or insert into the database. */
if (tpcall(ops[j], NULL, 0L, (char **)&replyBuf,
&replyLen, 0) == -1)
goto tuxedo_err;
/* Wake up thread 2.*/
if (thread_num == 1) {
if ((ret = sync_thr(&(counters[(j*3)+1]), NUM_THREADS,
&(cond_vars[(j*3)+1]))) != 0) {
fprintf(stderr,
"%s:%s: Error syncing thread 1: %i \n",
progname, thread_name, ret);
goto end;
}
}
/* Sync both threads. */
if ((ret = sync_thr(&(counters[(j*3)+2]), NUM_THREADS,
&(cond_vars[(j*3)+2]))) != 0) {
fprintf(stderr,
"%s:%s: Error syncing threads: %i \n",
progname, thread_name, ret);
goto end;
}
/*
* Commit or abort the transaction depending the what the
* server returns. Check that it matched expectation
* (We abort on LOCK_NOTGRANTED and DEADLOCK errors, and
* commit otherwise. Other errors result in returning
* without committing or aborting.
*/
commit = !tpurcode;
if (commit != success[j]) {
fprintf(stderr,
"%s:%s: Expected: %i Got: %i.\n",
progname, thread_name, success[j], commit);
if (verbose) {
printf("%s:%s: Expected: %i Got: %i.\n",
progname, thread_name, success[j], commit);
}
}
if (commit) {
if (tpcommit(0L) == -1)
goto tuxedo_err;
if (verbose) {
printf("%s:%s: tpcommit() OK\n", progname,
thread_name);
}
} else {
if (tpabort(0L) == -1) {
goto tuxedo_err;
}
if (verbose) {
printf("%s:%s: tpabort() OK\n", progname,
thread_name);
}
}
}
if (0) {
tuxedo_err: fprintf(stderr, "%s:%s: TUXEDO ERROR: %s (code %d)\n",
progname, thread_name, tpstrerror(tperrno), tperrno);
/*
* Does not matter what result is, as long as it is not
* NULL on error.
*/
result = (void *)&error;
}
end: tpterm();
if (verbose)
printf("%s:%s: tpterm() OK\n", progname, thread_name);
if (initBuf != NULL)
tpfree((char *)initBuf);
if (replyBuf != NULL)
tpfree((char *)replyBuf);
return(result);
}
/*
* Create the threads to call the servers, and check that data in the two
* databases is identical.
*/
int
main(int argc, char* argv[])
{
int ch, i, ret, ttype;
pthread_t threads[NUM_THREADS];
struct thread_args args[NUM_THREADS];
void *results = NULL;
char *names[] = {"1", "2"};
progname = argv[0];
i = 1;
verbose = 0;
while ((ch = getopt(argc, argv, "vt")) != EOF) {
switch (ch) {
case 't':
ttype = atoi(argv[++i]);
break;
case 'v':
verbose = 1;
break;
case '?':
default:
return (usage());
}
i++;
}
argc -= optind;
argv += optind;
if (ttype > 2 || ttype < 0)
return (usage());
if (verbose)
printf("%s: called with type %i\n", progname, ttype);
for(i = 0; i < (NUM_TESTS*3); i++) {
pthread_cond_init(&cond_vars[i], NULL);
counters[i] = 0;
}
/* Create threads for different contexts*/
for (i = 0; i < NUM_THREADS; i++) {
args[i].thread_name = names[i];
args[i].type_test = ttype;
if (verbose)
printf("calling server thread\n");
if ((ret = pthread_create(&threads[i], NULL,
call_server_thread, &(args[i]))) != 0) {
fprintf(stderr, "%s: failed to create thread %s.\n",
progname, ret);
goto err;
}
}
/* Wait for each thread to finish. */
for (i = 0; i < NUM_THREADS; i++) {
if ((ret = pthread_join(threads[i], &results)) != 0) {
fprintf(stderr, "%s: failed to join thread %s.\n",
progname, ret);
goto err;
}
if (results != NULL)
goto err;
}
if (0) {
err: ret = EXIT_FAILURE;
}
for(i = 0; i < (NUM_TESTS*2); i++)
pthread_cond_destroy(&cond_vars[i]);
return (ret);
}
static char *rdb1 = "read_db1";
static char *wdb1 = "write_db1";
/*
* Operation Success
* Thread 1 Thread 2 no MVCC MVCC
* write write no no
* write read no yes
* read read yes yes
* read write no yes
*/
void init_tests(char *ops[], int success[], int type, int thread_num)
{
if (thread_num == 1) {
ops[0] = wdb1;
ops[1] = wdb1;
ops[2] = rdb1;
ops[3] = rdb1;
/* Thread 1 is always successful. */
success[0] = 1;
success[1] = 1;
success[2] = 1;
success[3] = 1;
} else {
ops[0] = wdb1;
ops[1] = rdb1;
ops[2] = rdb1;
ops[3] = wdb1;
if (type == NO_MVCC) {
success[0] = 0;
success[1] = 0;
success[2] = 1;
success[3] = 0;
} else {
success[0] = 0;
success[1] = 1;
success[2] = 1;
success[3] = 1;
}
}
}
|