-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathGridSample.c
648 lines (550 loc) · 17.5 KB
/
GridSample.c
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Universal Permissive License v 1.0 as shown
* at http://oss.oracle.com/licenses/upl
*/
/****************************************************************************
*
* TimesTen Scaleout sample programs - GridSample
*
* GridSample.c - main source file
*
****************************************************************************/
#include "GridSample.h"
/*
* Display and/or log the execution summary report.
*/
void
displaySummary(
context_t * ctxt
)
{
long nsecs;
if ( ctxt == NULL )
return;
nsecs = (ctxt->stopTime - ctxt->startTime) / 1000;
sprintf( ctxt->msgBuff,
"Execution time: %ld seconds", nsecs );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
"Total transactions: %ld", ctxt->totalTxnExecuted );
displayMessage( ctxt, NULL );
if ( ctxt->totalTxnExecuted > 0 )
{
sprintf( ctxt->msgBuff,
" Authorize : %ld", ctxt->totalTxnAuthorize );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
" Charge : %ld", ctxt->totalTxnCharge );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
" Topup : %ld", ctxt->totalTxnTopup );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
" Query : %ld", ctxt->totalTxnQuery );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
" Purge : %ld", ctxt->totalTxnPurge );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
"ACCOUNTS table rows: %ld updated",
ctxt->totalAccountUpdates );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
"TRANSACTIONS table rows: %ld inserted, %ld deleted",
ctxt->totalTransactionInserts,
ctxt->totalTransactionDeletes );
displayMessage( ctxt, NULL );
}
sprintf( ctxt->msgBuff,
"Total retries: %ld", ctxt->totalRetries );
displayMessage( ctxt, NULL );
sprintf( ctxt->msgBuff,
"Total C/S failovers: %ld", ctxt->totalCSFailovers );
displayMessage( ctxt, NULL );
} // displaySummary
/*
* dbDisconnect - disconnect from the database performing all
* necessary cleanup
*/
void
dbDisconnect(
context_t * ctxt,
boolean rollback
)
{
int ret = SUCCESS;
if ( ctxt == NULL )
return;
debugMessage( ctxt, "DEBUG: ENTER: dbDisconnect" );
if ( (ctxt->dbconn != NULL) && ctxt->dbconn->connected )
{ // connected
if ( rollback )
{ // rollback flag is set
ret = ODBCRollback( ctxt->dbconn );
#if defined(DEBUG)
sprintf( ctxt->msgBuff,
"DEBUG: dbDisconnect: ODBCRollback: %d", ret );
debugMessage( ctxt, NULL );
#endif
}
ret = ODBCDisconnect( ctxt->dbconn );
#if defined(DEBUG)
sprintf( ctxt->msgBuff,
"DEBUG: dbDisconnect: ODBCDisconnect: %d", ret );
debugMessage( ctxt, NULL );
#endif
}
debugMessage( ctxt, "DEBUG: EXIT: dbDisconnect" );
} // dbDisconnect
/*
* dbConnect - connect to the database and initialise a bunch
* of stuff
*/
int
dbConnect(
context_t * ctxt
)
{
int ret;
txnUpdateGridInfo_t * txnUGI = NULL;
txnGetCounts_t * txnGC = NULL;
txnClearHistory_t * txnCH = NULL;
txnPurge_t * txnP = NULL;
txnQuery_t * txnQ = NULL;
txnAuthorize_t * txnA = NULL;
txnCharge_t * txnC = NULL;
txnTopup_t * txnT = NULL;
if ( (ctxt == NULL) || (ctxt->dbconn == NULL) )
return ERR_PARAM_INTERNAL;
if ( ctxt->dbconn->connected )
return ERR_STATE_INTERNAL; // already connected
debugMessage( ctxt, "DEBUG: ENTER: dbConnect" );
// Connect to the database
ret = ODBCConnect( ctxt->dbconn,
ctxt->dbDSN,
ctxt->dbUID,
ctxt->dbPWD,
ctxt->connName );
if ( ret != SUCCESS )
{
displayError( ctxt, errorMessage( ret, ctxt ),
(ret==ERR_ODBC_NORMAL)?ctxt->dbconn->errors:NULL );
goto error;
}
// Determine grid info.
txnUGI = createTxnUpdateGridInfo( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnUpdateGridInfo( txnUGI );
if ( ret != SUCCESS )
goto error;
ret = executeTxnUpdateGridInfo( txnUGI );
if ( ret != SUCCESS )
goto error;
// Get table row counts
txnGC = createTxnGetCounts( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnGetCounts( txnGC );
if ( ret != SUCCESS )
goto error;
ret = executeTxnGetCounts( txnGC );
if ( ret != SUCCESS )
goto error;
// Prepare for truncate TRANSACTIONS table, if required
if ( ctxt->doCleanup )
{
txnCH = createTxnClearHistory( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnClearHistory( txnCH );
if ( ret != SUCCESS )
goto error;
}
// Prepare all other transactions as required
// Purge
if ( ctxt->pctPurge > 0 )
{
txnP = createTxnPurge( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnPurge( txnP );
if ( ret != SUCCESS )
goto error;
}
// Query
if ( ctxt->pctQuery > 0 )
{
txnQ = createTxnQuery( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnQuery( txnQ );
if ( ret != SUCCESS )
goto error;
}
// Authorize
if ( ctxt->pctAuthorize > 0 )
{
txnA = createTxnAuthorize( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnAuthorize( txnA );
if ( ret != SUCCESS )
goto error;
}
// Charge
if ( ctxt->pctCharge > 0 )
{
txnC = createTxnCharge( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnCharge( txnC );
if ( ret != SUCCESS )
goto error;
}
// TopUp
if ( ctxt->pctTopUp > 0 )
{
txnT = createTxnTopup( ctxt );
if ( ret != SUCCESS )
goto error;
ret = initTxnTopup( txnT );
if ( ret != SUCCESS )
goto error;
}
debugMessage( ctxt, "DEBUG: EXITOK: dbConnect" );
return SUCCESS;
error:
#if defined(DEBUG)
sprintf( ctxt->msgBuff, "DEBUG: EXITERR: dbConnect: %d", ret);
debugMessage( ctxt, NULL );
#endif
return ret;
} // dbConnect
/*
* Display (and perhaps log) the interval report.
*/
void
displayReport(
context_t * ctxt
)
{
long now;
if ( ctxt == NULL )
return;
// if not logging and not verbose then return
if ( (ctxt->vbLevel != VB_VERBOSE) && (ctxt->logOut == NULL) )
return;
// check if reporting interval has elapsed
now = getTimeMS() / 1000;
if ( now < (ctxt->lastReportTime + ctxt->vbInterval) )
return;
// create message and log/display it as required
sprintf( ctxt->msgBuff,
"Eid=%d Txn=%ld A=%ld C=%ld T=%ld Q=%ld P=%ld R=%ld F=%ld",
ctxt->currElementID,
ctxt->intvlTxnExecuted,
ctxt->intvlTxnAuthorize,
ctxt->intvlTxnCharge,
ctxt->intvlTxnTopup,
ctxt->intvlTxnQuery,
ctxt->intvlTxnPurge,
ctxt->intvlRetries,
ctxt->intvlCSFailovers );
displayMessage( ctxt, ctxt->msgBuff );
// reset interval statistics
ctxt->lastReportTime = now;
ctxt->intvlTxnExecuted = 0;
ctxt->intvlTxnAuthorize = 0;
ctxt->intvlTxnQuery = 0;
ctxt->intvlTxnCharge = 0;
ctxt->intvlTxnTopup = 0;
ctxt->intvlTxnPurge = 0;
ctxt->intvlRetries = 0;
ctxt->intvlCSFailovers = 0;
} // displayReport
/*
* Maintain statistics
*/
void
incrementCounters(
context_t * ctxt,
int txnType,
apptxn_t * apptxn
)
{
int ret = SUCCESS;
long rowcount = 0;
if ( (ctxt == NULL) || (apptxn == NULL) )
return;
ctxt->totalTxnExecuted++;
ctxt->intvlTxnExecuted++;
switch ( txnType )
{
case TXN_AUTHORIZE:
ctxt->totalTxnAuthorize++;
ctxt->intvlTxnAuthorize++;
break;
case TXN_QUERY:
ctxt->totalTxnQuery++;
ctxt->intvlTxnQuery++;
break;
case TXN_CHARGE:
ctxt->totalTxnCharge++;
ctxt->intvlTxnCharge++;
ctxt->totalAccountUpdates++;
ctxt->totalTransactionInserts++;
break;
case TXN_TOPUP:
ctxt->totalTxnTopup++;
ctxt->intvlTxnTopup++;
ctxt->totalAccountUpdates++;
ctxt->totalTransactionInserts++;
break;
case TXN_PURGE:
ctxt->totalTxnPurge++;
ctxt->intvlTxnPurge++;
// get rowcount
ret = apptxn->rowcount( apptxn->parent, &rowcount );
if ( (ret == SUCCESS) && (rowcount > 0) )
ctxt->totalTransactionDeletes += rowcount;
break;
}
} // increment counters
/*
* executeTxnGrid - Choose a transaction based on the workload profile,
* generate input parameters (if required) and then
* execute it. If it fails due to a grid transient error
* or a client failover, perform the necessary recovery /
* retry actions.
*/
boolean
executeTxnGrid(
context_t * ctxt
)
{
boolean needReprepare = false;
apptxn_t * apptxn = NULL;
apptxn_t * tapptxn = NULL;
int txnType = 0;
int ret;
if ( ctxt == NULL )
return false;
debugMessage( ctxt, "DEBUG: ENTER: executeTxnGrid" );
// choose a transaction type ramndpmly based on specified ratios
txnType = chooseTxnType( ctxt );
// generate input values as required
switch ( txnType )
{
case TXN_AUTHORIZE:
apptxn = ctxt->atxn->txn;
ctxt->atxn->accountID = getRandomAccount( ctxt );
break;
case TXN_QUERY:
apptxn = ctxt->qtxn->txn;
ctxt->qtxn->custID = getRandomCustomer( ctxt );
break;
case TXN_CHARGE:
apptxn = ctxt->ctxn->txn;
ctxt->ctxn->accountID = getRandomAccount( ctxt );
ctxt->ctxn->adjustAmount =
-getRandomDouble( MIN_CHARGE_VALUE, MAX_CHARGE_VALUE );
break;
case TXN_TOPUP:
apptxn = ctxt->ttxn->txn;
ctxt->ttxn->accountID = getRandomAccount( ctxt );
ctxt->ctxn->adjustAmount = DFLT_TOPUP_VALUE;
break;
case TXN_PURGE:
apptxn = ctxt->ptxn->txn;
break;
}
// reset retry and failover counts
ctxt->rtCount = ctxt->retryLimit;
ctxt->foCount = ctxt->failoverLimit;
// execute transaction handling retries and failovers
while ( (ctxt->rtCount > 0) && (ctxt->foCount > 0) )
{
debugMessage( ctxt, "DEBUG: Start of retry loop" );
ret = SUCCESS;
if ( needReprepare ) // connection has failed over
{
debugMessage( ctxt, "DEBUG: Re-preparing" );
// try and pro-actively re-prepare *all* transactions
tapptxn = ctxt->apptxns->first;
while ( tapptxn != NULL )
{
if ( (tapptxn->init != NULL) && (ret == SUCCESS) )
ret = tapptxn->init( tapptxn->parent );
tapptxn = tapptxn->next;
}
if ( ret == SUCCESS ) // if okay, update grid info
ret = ctxt->ugitxn->txn->execute( ctxt->ugitxn );
if ( ret == SUCCESS )
{ // failover completed successfully
needReprepare = false;
sprintf( ctxt->msgBuff,
"Failover from element %d to element %d",
ctxt->prevElementID, ctxt->currElementID );
displayMessage( ctxt, ctxt->msgBuff );
}
}
if ( ret == SUCCESS )
{ // execute the selected transaction
debugMessage( ctxt, "DEBUG: Executing" );
ret = apptxn->execute( apptxn->parent );
}
if ( (ret == SUCCESS) && (apptxn->fetch != NULL) )
{ // if okay and txn returns a result set, retrieve the rows
debugMessage( ctxt, "DEBUG: Fetching" );
ret = apptxn->fetch( apptxn->parent );
while ( ret == SUCCESS )
ret = apptxn->fetch( apptxn->parent );
if ( (ret == SUCCESS) || (ret == ERR_ODBC_NO_DATA) )
{ // close the cursor
debugMessage( ctxt, "DEBUG: Closing" );
ret = apptxn->close( apptxn->parent );
}
}
if ( ret == SUCCESS )
{ // maintain stats
incrementCounters( ctxt, txnType, apptxn );
return true; // return success indication
}
else
if ( ret == ERR_ODBC_RETRYABLE )
{ // update retry stats
ctxt->intvlRetries++;
ctxt->totalRetries++;
if ( --ctxt->rtCount > 0 )
logMessage( ctxt, "RETRY" );
}
else
if ( ret == ERR_ODBC_FAILOVER )
{ // update failover stats
ctxt->intvlCSFailovers++;
ctxt->totalCSFailovers++;
needReprepare = true; // mark for re-prepare
if ( --ctxt->foCount > 0 )
logMessage( ctxt, "FAILOVER" );
}
else
{ // fatal error
sprintf( ctxt->msgBuff,
"*** Transaction %s failed", getTxnName(txnType) );
displayMessage( ctxt, ctxt->msgBuff );
return false; // return failure indication
}
}
// if we reach here, we have run out of retries
sprintf( ctxt->msgBuff,
"*** Transaction %s retries exhausted", getTxnName(txnType) );
displayMessage( ctxt, ctxt->msgBuff );
return false; // return failure indication
} // executeGridTxn
/*
* runWorkload - runs the workload and accumulates statistics
*/
void
runWorkload(
context_t * ctxt
)
{
sprintf( ctxt->msgBuff,
"%s (A=%d%%,C=%d%%,T=%d%%,Q=%d%%,P=%d%%)",
MSG_WKL_STARTED,
ctxt->pctAuthorize,
ctxt->pctCharge,
ctxt->pctTopUp,
ctxt->pctQuery,
ctxt->pctPurge );
displayMessage( ctxt, ctxt->msgBuff );
// initialize last report time to current time
ctxt->lastReportTime = getTimeMS() / 1000;
// run until finished, interrupted or a fatal error occurs
while ( ! shouldTerminate( ctxt ) && executeTxnGrid( ctxt ) )
displayReport( ctxt );
displayMessage( ctxt, MSG_WKL_ENDED );
} // runWorkload
/*
* main() - drives everything
*/
int
main(
int argc,
char const * argv[]
)
{
context_t * ctxt = NULL;
int ret = SUCCESS;
int sig;
// Allocate and initialize the program context
ret = allocContext( &ctxt );
if ( ret )
{
fprintf( stderr, "*** Failed to initialize context: %s\n",
errorMessage( ret, ctxt ) );
cleanup( &ctxt, EXIT_ERROR );
}
// Parse command line options
ret = parseArgs( ctxt, argc, argv );
if ( ret )
cleanup( &ctxt, (ret==ERR_HELP)?EXIT_HELP:EXIT_PARAM );
// Install signal handlers to catch interrupt signals
ret = installSignalHandlers();
if ( ret )
{
displayError( ctxt, errorMessage( ret, ctxt ), NULL );
cleanup( &ctxt, EXIT_ERROR );
}
// Record start time
ctxt->startTime = getTimeMS();
// display pre-connect message
sprintf( ctxt->msgBuff,
"Connecting to '%s' as '%s'",
ctxt->dbDSN, ctxt->dbUID );
displayMessage( ctxt, ctxt->msgBuff );
// Connect to the database
if ( dbConnect( ctxt ) != SUCCESS )
cleanup( &ctxt, EXIT_ERROR );
sig = signalReceived(); // interrupt?
if ( sig ) // interrupted
{
if ( (ctxt->vbLevel != VB_SILENT) && (sig == SIGINT) )
printf("\010\010"); // erase '^C' from interrupt
displayMessage( ctxt, MSG_INTERRUPT );
cleanup( &ctxt, EXIT_INTERRUPT );
}
// display post-connect message
if ( ctxt->isGrid )
sprintf( ctxt->msgBuff,
"Connected to element %d",
ctxt->currElementID );
else
sprintf( ctxt->msgBuff, "Connected" );
displayMessage( ctxt, ctxt->msgBuff );
// cleanup TRANSACTIONS table if required
if ( (ctxt->doCleanup) && (ctxt->chtxn != NULL) )
{
logMessage( ctxt, "Truncating TRANSACTIONS table" );
ret = executeTxnClearHistory( ctxt->chtxn );
if ( ret != SUCCESS )
cleanup( &ctxt, EXIT_ERROR );
logMessage( ctxt, "Truncate complete" );
}
// Run the workload!
runWorkload( ctxt );
// display pre-disconnection message
displayMessage( ctxt, "Disconnecting" );
// Disconnect from the database
dbDisconnect( ctxt, false );
// display post-disconnection message
displayMessage( ctxt, "Disconnected" );
// Record stop time
ctxt->stopTime = getTimeMS();
// display/log summary
displaySummary( ctxt );
cleanup( &ctxt, SUCCESS );
} // main