forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdbTransaction.cpp
More file actions
3241 lines (2903 loc) · 99.8 KB
/
NdbTransaction.cpp
File metadata and controls
3241 lines (2903 loc) · 99.8 KB
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ndb_global.h>
#include <NdbOut.hpp>
#include "API.hpp"
#include <AttributeHeader.hpp>
#include <signaldata/TcKeyConf.hpp>
#include <signaldata/TcIndx.hpp>
#include <signaldata/TcCommit.hpp>
#include <signaldata/TcKeyFailConf.hpp>
#include <signaldata/TcHbRep.hpp>
#include <signaldata/TcRollbackRep.hpp>
/*****************************************************************************
NdbTransaction( Ndb* aNdb );
Return Value: None
Parameters: aNdb: Pointers to the Ndb object
Remark: Creates a connection object.
*****************************************************************************/
NdbTransaction::NdbTransaction( Ndb* aNdb ) :
theSendStatus(NotInit),
theCallbackFunction(NULL),
theCallbackObject(NULL),
theTransArrayIndex(0),
theStartTransTime(0),
theErrorLine(0),
theErrorOperation(NULL),
theNdb(aNdb),
theNext(NULL),
theFirstOpInList(NULL),
theLastOpInList(NULL),
theFirstExecOpInList(NULL),
theLastExecOpInList(NULL),
theCompletedFirstOp(NULL),
theCompletedLastOp(NULL),
theNoOfOpSent(0),
theNoOfOpCompleted(0),
theMyRef(0),
theTCConPtr(0),
theTransactionId(0),
theGlobalCheckpointId(0),
p_latest_trans_gci(0),
theStatus(NotConnected),
theCompletionStatus(NotCompleted),
theCommitStatus(NotStarted),
theMagicNumber(0xFE11DC),
theTransactionIsStarted(false),
theDBnode(0),
theReleaseOnClose(false),
// Composite query operations
m_firstQuery(NULL),
m_firstExecQuery(NULL),
m_firstActiveQuery(NULL),
// Scan operations
m_waitForReply(true),
m_theFirstScanOperation(NULL),
m_theLastScanOperation(NULL),
m_firstExecutedScanOp(NULL),
// Scan operations
theScanningOp(NULL),
m_scanningQuery(NULL),
theBuddyConPtr(0xFFFFFFFF),
theBlobFlag(false),
thePendingBlobOps(0),
maxPendingBlobReadBytes(~Uint32(0)),
maxPendingBlobWriteBytes(~Uint32(0)),
pendingBlobReadBytes(0),
pendingBlobWriteBytes(0),
m_theFirstLockHandle(NULL),
m_theLastLockHandle(NULL),
m_tcRef(numberToRef(DBTC, 0))
{
theListState = NotInList;
theError.code = 0;
//theId = NdbObjectIdMap::InvalidId;
theId = theNdb->theImpl->theNdbObjectIdMap.map(this);
#define CHECK_SZ(mask, sz) assert((sizeof(mask)/sizeof(mask[0])) == sz)
CHECK_SZ(m_db_nodes, NdbNodeBitmask::Size);
CHECK_SZ(m_failed_db_nodes, NdbNodeBitmask::Size);
}//NdbTransaction::NdbTransaction()
/*****************************************************************************
~NdbTransaction();
Remark: Deletes the connection object.
*****************************************************************************/
NdbTransaction::~NdbTransaction()
{
DBUG_ENTER("NdbTransaction::~NdbTransaction");
theNdb->theImpl->theNdbObjectIdMap.unmap(theId, this);
DBUG_VOID_RETURN;
}//NdbTransaction::~NdbTransaction()
/*****************************************************************************
void init();
Remark: Initialise connection object for new transaction.
*****************************************************************************/
int
NdbTransaction::init()
{
theListState = NotInList;
theInUseState = true;
theTransactionIsStarted = false;
theNext = NULL;
theFirstOpInList = NULL;
theLastOpInList = NULL;
theScanningOp = NULL;
m_scanningQuery = NULL;
theFirstExecOpInList = NULL;
theLastExecOpInList = NULL;
theCompletedFirstOp = NULL;
theCompletedLastOp = NULL;
theGlobalCheckpointId = 0;
p_latest_trans_gci =
theNdb->theImpl->m_ndb_cluster_connection.get_latest_trans_gci();
theCommitStatus = Started;
theCompletionStatus = NotCompleted;
theError.code = 0;
theErrorLine = 0;
theErrorOperation = NULL;
theReleaseOnClose = false;
theSimpleState = true;
theSendStatus = InitState;
theMagicNumber = 0x37412619;
// Query operations
m_firstQuery = NULL;
m_firstExecQuery = NULL;
m_firstActiveQuery = NULL;
// Scan operations
m_waitForReply = true;
m_theFirstScanOperation = NULL;
m_theLastScanOperation = NULL;
m_firstExecutedScanOp = 0;
theBuddyConPtr = 0xFFFFFFFF;
//
theBlobFlag = false;
thePendingBlobOps = 0;
m_theFirstLockHandle = NULL;
m_theLastLockHandle = NULL;
pendingBlobReadBytes = 0;
pendingBlobWriteBytes = 0;
if (theId == NdbObjectIdMap::InvalidId)
{
theId = theNdb->theImpl->theNdbObjectIdMap.map(this);
if (theId == NdbObjectIdMap::InvalidId)
{
theError.code = 4000;
return -1;
}
}
return 0;
}//NdbTransaction::init()
/*****************************************************************************
setOperationErrorCode(int error);
Remark: Sets an error code on the connection object from an
operation object.
*****************************************************************************/
void
NdbTransaction::setOperationErrorCode(int error)
{
DBUG_ENTER("NdbTransaction::setOperationErrorCode");
setErrorCode(error);
DBUG_VOID_RETURN;
}
/*****************************************************************************
setOperationErrorCodeAbort(int error);
Remark: Sets an error code on the connection object from an
operation object.
*****************************************************************************/
void
NdbTransaction::setOperationErrorCodeAbort(int error, int abortOption)
{
DBUG_ENTER("NdbTransaction::setOperationErrorCodeAbort");
if (theTransactionIsStarted == false) {
theCommitStatus = Aborted;
} else if ((theCommitStatus != Committed) &&
(theCommitStatus != Aborted)) {
theCommitStatus = NeedAbort;
}//if
setErrorCode(error);
DBUG_VOID_RETURN;
}
/*****************************************************************************
setErrorCode(int anErrorCode);
Remark: Sets an error indication on the connection object.
*****************************************************************************/
void
NdbTransaction::setErrorCode(int error)
{
DBUG_ENTER("NdbTransaction::setErrorCode");
DBUG_PRINT("enter", ("error: %d, theError.code: %d", error, theError.code));
if (theError.code == 0)
theError.code = error;
DBUG_VOID_RETURN;
}//NdbTransaction::setErrorCode()
int
NdbTransaction::restart(){
DBUG_ENTER("NdbTransaction::restart");
if(theCompletionStatus == CompletedSuccess){
releaseCompletedOperations();
releaseCompletedQueries();
theTransactionId = theNdb->allocate_transaction_id();
theCommitStatus = Started;
theCompletionStatus = NotCompleted;
theTransactionIsStarted = false;
DBUG_RETURN(0);
}
DBUG_PRINT("error",("theCompletionStatus != CompletedSuccess"));
DBUG_RETURN(-1);
}
/*****************************************************************************
void handleExecuteCompletion(void);
Remark: Handle time-out on a transaction object.
*****************************************************************************/
void
NdbTransaction::handleExecuteCompletion()
{
/***************************************************************************
* Move the NdbOperation objects from the list of executing
* operations to list of completed
**************************************************************************/
NdbOperation* tFirstExecOp = theFirstExecOpInList;
NdbOperation* tLastExecOp = theLastExecOpInList;
if (tLastExecOp != NULL) {
tLastExecOp->next(theCompletedFirstOp);
theCompletedFirstOp = tFirstExecOp;
if (theCompletedLastOp == NULL)
theCompletedLastOp = tLastExecOp;
theFirstExecOpInList = NULL;
theLastExecOpInList = NULL;
}//if
theSendStatus = InitState;
return;
}//NdbTransaction::handleExecuteCompletion()
/*****************************************************************************
int execute(ExecType aTypeOfExec, CommitType aTypeOfCommit, int forceSend);
Return Value: Return 0 : execute was successful.
Return -1: In all other case.
Parameters : aTypeOfExec: Type of execute.
Remark: Initialise connection object for new transaction.
*****************************************************************************/
int
NdbTransaction::execute(ExecType aTypeOfExec,
NdbOperation::AbortOption abortOption,
int forceSend)
{
NdbError existingTransError = theError;
NdbError firstTransError;
DBUG_ENTER("NdbTransaction::execute");
DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d",
aTypeOfExec, abortOption));
if (! theBlobFlag)
DBUG_RETURN(executeNoBlobs(aTypeOfExec, abortOption, forceSend));
/*
* execute prepared ops in batches, as requested by blobs
* - blob error does not terminate execution
* - blob error sets error on operation
* - if error on operation skip blob calls
*
* In the call to preExecute(), each operation involving blobs can
* add (and execute) extra operations before (reads) and after
* (writes) the operation on the main row.
* In the call to postExecute(), each blob can add extra read and
* write operations to be executed immediately
* It is assumed that all operations added in preExecute() are
* defined 'before' operations added in postExecute().
* To facilitate this, the transaction's list of operations is
* pre-emptively split when a Blob operation is encountered.
* preExecute can add operations before and after the operation being
* processed, and if no batch execute is required, the list is rejoined.
* If batch execute is required, then execute() is performed, and then
* the postExecute() actions (which can add operations) are called before
* the list is rejoined. See NdbBlob::preExecute() and
* NdbBlob::postExecute() for more info.
*/
NdbOperation* tPrepOp;
if (abortOption != NdbOperation::DefaultAbortOption)
{
DBUG_PRINT("info", ("Forcing operations to take execute() abortOption %d",
abortOption));
/* For Blobs, we have to execute with DefaultAbortOption
* If the user supplied a non default AbortOption to execute()
* then we need to make sure that all of the operations in their
* batch are set to use the supplied AbortOption so that the
* expected behaviour is obtained when executing below
*/
tPrepOp= theFirstOpInList;
while(tPrepOp != NULL)
{
DBUG_PRINT("info", ("Changing abortOption from %d",
tPrepOp->m_abortOption));
tPrepOp->m_abortOption= abortOption;
tPrepOp= tPrepOp->next();
}
}
ExecType tExecType;
NdbOperation* tCompletedFirstOp = NULL;
NdbOperation* tCompletedLastOp = NULL;
int ret = 0;
do {
NdbOperation* firstSavedOp= NULL;
NdbOperation* lastSavedOp= NULL;
tExecType = aTypeOfExec;
tPrepOp = theFirstOpInList;
while (tPrepOp != NULL) {
if (tPrepOp->theError.code == 0) {
bool batch = false;
NdbBlob* tBlob = tPrepOp->theBlobList;
if (tBlob !=NULL) {
/* We split the operation list just after this
* operation, in case it adds extra ops
*/
firstSavedOp = tPrepOp->next(); // Could be NULL
lastSavedOp = theLastOpInList;
DBUG_PRINT("info", ("Splitting ops list between %p and %p",
firstSavedOp, lastSavedOp));
tPrepOp->next(NULL);
theLastOpInList= tPrepOp;
}
while (tBlob != NULL) {
if (tBlob->preExecute(tExecType, batch) == -1)
{
ret = -1;
if (firstTransError.code==0)
firstTransError= theError;
}
tBlob = tBlob->theNext;
}
if (batch) {
// blob asked to execute all up to lastOpInBatch now
tExecType = NoCommit;
break;
}
else {
/* No batching yet - rejoin the current and
* saved operation lists
*/
DBUG_PRINT("info", ("Rejoining ops list after preExecute between %p and %p",
theLastOpInList,
firstSavedOp));
if (firstSavedOp != NULL && lastSavedOp != NULL) {
if (theFirstOpInList == NULL)
theFirstOpInList = firstSavedOp;
else
theLastOpInList->next(firstSavedOp);
theLastOpInList = lastSavedOp;
}
firstSavedOp= lastSavedOp= NULL;
}
}
tPrepOp = tPrepOp->next();
}
if (tExecType == Commit) {
NdbOperation* tOp = theCompletedFirstOp;
while (tOp != NULL) {
if (tOp->theError.code == 0) {
NdbBlob* tBlob = tOp->theBlobList;
while (tBlob != NULL) {
if (tBlob->preCommit() == -1)
{
ret = -1;
if (firstTransError.code==0)
firstTransError= theError;
}
tBlob = tBlob->theNext;
}
}
tOp = tOp->next();
}
}
// completed ops are in unspecified order
if (theCompletedFirstOp != NULL) {
if (tCompletedFirstOp == NULL) {
tCompletedFirstOp = theCompletedFirstOp;
tCompletedLastOp = theCompletedLastOp;
} else {
tCompletedLastOp->next(theCompletedFirstOp);
tCompletedLastOp = theCompletedLastOp;
}
theCompletedFirstOp = NULL;
theCompletedLastOp = NULL;
}
if (executeNoBlobs(tExecType,
NdbOperation::DefaultAbortOption,
forceSend) == -1)
{
/**
* We abort the execute here. But we still need to put the split-off
* operation list back into the transaction object, or we will get a
* memory leak.
*/
if (firstSavedOp != NULL && lastSavedOp != NULL) {
DBUG_PRINT("info", ("Rejoining ops list after postExecute between "
"%p and %p", theLastOpInList, firstSavedOp));
if (theFirstOpInList == NULL)
theFirstOpInList = firstSavedOp;
else
theLastOpInList->next(firstSavedOp);
theLastOpInList = lastSavedOp;
}
if (tCompletedFirstOp != NULL) {
tCompletedLastOp->next(theCompletedFirstOp);
theCompletedFirstOp = tCompletedFirstOp;
if (theCompletedLastOp == NULL)
theCompletedLastOp = tCompletedLastOp;
}
/* executeNoBlobs will have set transaction error */
DBUG_RETURN(-1);
}
/* Capture any trans error left by the execute() in case it gets trampled */
if (firstTransError.code==0)
firstTransError= theError;
#ifdef ndb_api_crash_on_complex_blob_abort
assert(theFirstOpInList == NULL && theLastOpInList == NULL);
#else
theFirstOpInList = theLastOpInList = NULL;
#endif
{
NdbOperation* tOp = theCompletedFirstOp;
while (tOp != NULL) {
if (tOp->theError.code == 0) {
NdbBlob* tBlob = tOp->theBlobList;
while (tBlob != NULL) {
// may add new operations if batch
if (tBlob->postExecute(tExecType) == -1)
{
ret = -1;
if (firstTransError.code==0)
firstTransError= theError;
}
tBlob = tBlob->theNext;
}
}
tOp = tOp->next();
}
}
// Restore any saved prepared ops if we batched
if (firstSavedOp != NULL && lastSavedOp != NULL) {
DBUG_PRINT("info", ("Rejoining ops list after postExecute between %p and %p",
theLastOpInList,
firstSavedOp));
if (theFirstOpInList == NULL)
theFirstOpInList = firstSavedOp;
else
theLastOpInList->next(firstSavedOp);
theLastOpInList = lastSavedOp;
}
assert(theFirstOpInList == NULL || tExecType == NoCommit);
} while (theFirstOpInList != NULL || tExecType != aTypeOfExec);
if (tCompletedFirstOp != NULL) {
tCompletedLastOp->next(theCompletedFirstOp);
theCompletedFirstOp = tCompletedFirstOp;
if (theCompletedLastOp == NULL)
theCompletedLastOp = tCompletedLastOp;
}
#if ndb_api_count_completed_ops_after_blob_execute
{ NdbOperation* tOp; unsigned n = 0;
for (tOp = theCompletedFirstOp; tOp != NULL; tOp = tOp->next()) n++;
ndbout << "completed ops: " << n << endl;
}
#endif
/* Sometimes the original error is trampled by 'Trans already aborted',
* detect this case and attempt to restore the original error
*/
if (theError.code == 4350) // Trans already aborted
{
DBUG_PRINT("info", ("Trans already aborted, existingTransError.code %u, "
"firstTransError.code %u",
existingTransError.code,
firstTransError.code));
if (existingTransError.code != 0)
{
theError = existingTransError;
}
else if (firstTransError.code != 0)
{
theError = firstTransError;
}
}
/* Generally return the first error which we encountered as
* the Trans error. Caller can traverse the op list to
* get the full picture
*/
if (firstTransError.code != 0)
{
DBUG_PRINT("info", ("Setting error to first error. firstTransError.code = %u, "
"theError.code = %u",
firstTransError.code,
theError.code));
theError = firstTransError;
}
DBUG_RETURN(ret);
}
int
NdbTransaction::executeNoBlobs(NdbTransaction::ExecType aTypeOfExec,
NdbOperation::AbortOption abortOption,
int forceSend)
{
DBUG_ENTER("NdbTransaction::executeNoBlobs");
DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d",
aTypeOfExec, abortOption));
//------------------------------------------------------------------------
// We will start by preparing all operations in the transaction defined
// since last execute or since beginning. If this works ok we will continue
// by calling the poll with wait method. This method will return when
// the NDB kernel has completed its task or when 10 seconds have passed.
// The NdbTransactionCallBack-method will receive the return code of the
// transaction. The normal methods of reading error codes still apply.
//------------------------------------------------------------------------
Ndb* tNdb = theNdb;
Uint32 timeout = theNdb->theImpl->get_waitfor_timeout();
m_waitForReply = false;
executeAsynchPrepare(aTypeOfExec, NULL, NULL, abortOption);
if (m_waitForReply){
while (1) {
int noOfComp = tNdb->sendPollNdb(3 * timeout, 1, forceSend);
if (unlikely(noOfComp == 0)) {
/*
* Just for fun, this is only one of two places where
* we could hit this error... It's quite possible we
* hit it in Ndbif.cpp in Ndb::check_send_timeout()
*
* We behave rather similarly in both places.
* Hitting this is certainly a bug though...
*/
g_eventLogger->error("WARNING: Timeout in executeNoBlobs() waiting for "
"response from NDB data nodes. This should NEVER "
"occur. You have likely hit a NDB Bug. Please "
"file a bug.");
DBUG_PRINT("error",("This timeout should never occure, execute()"));
g_eventLogger->error("Forcibly trying to rollback txn (%p"
") to try to clean up data node resources.",
this);
executeNoBlobs(NdbTransaction::Rollback);
theError.code = 4012;
theError.status= NdbError::PermanentError;
theError.classification= NdbError::TimeoutExpired;
setOperationErrorCodeAbort(4012); // ndbd timeout
DBUG_RETURN(-1);
}//if
/*
* Check that the completed transactions include this one. There
* could be another thread running asynchronously. Even in pure
* async case rollback is done synchronously.
*/
if (theListState != NotInList)
continue;
#ifdef VM_TRACE
unsigned anyway = 0;
for (unsigned i = 0; i < theNdb->theNoOfPreparedTransactions; i++)
anyway += theNdb->thePreparedTransactionsArray[i] == this;
for (unsigned i = 0; i < theNdb->theNoOfSentTransactions; i++)
anyway += theNdb->theSentTransactionsArray[i] == this;
for (unsigned i = 0; i < theNdb->theNoOfCompletedTransactions; i++)
anyway += theNdb->theCompletedTransactionsArray[i] == this;
if (anyway) {
theNdb->printState("execute %lx", (long)this);
abort();
}
#endif
if (theReturnStatus == ReturnFailure) {
DBUG_RETURN(-1);
}//if
break;
}
}
thePendingBlobOps = 0;
pendingBlobReadBytes = 0;
pendingBlobWriteBytes = 0;
DBUG_RETURN(0);
}//NdbTransaction::executeNoBlobs()
/**
* Get the first query in the current transaction that has a lookup operation
* as its root.
*/
static NdbQueryImpl* getFirstLookupQuery(NdbQueryImpl* firstQuery)
{
NdbQueryImpl* current = firstQuery;
while (current != NULL && current->getQueryDef().isScanQuery()) {
current = current->getNext();
}
return current;
}
/**
* Get the last query in the current transaction that has a lookup operation
* as its root.
*/
static NdbQueryImpl* getLastLookupQuery(NdbQueryImpl* firstQuery)
{
NdbQueryImpl* current = firstQuery;
NdbQueryImpl* last = NULL;
while (current != NULL) {
if (!current->getQueryDef().isScanQuery()) {
last = current;
}
current = current->getNext();
}
return last;
}
/*****************************************************************************
void executeAsynchPrepare(ExecType aTypeOfExec,
NdbAsynchCallback callBack,
void* anyObject,
CommitType aTypeOfCommit);
Return Value: No return value
Parameters : aTypeOfExec: Type of execute.
anyObject: An object provided in the callback method
callBack: The callback method
aTypeOfCommit: What to do when read/updated/deleted records
are missing or inserted records already exist.
Remark: Prepare a part of a transaction in an asynchronous manner.
*****************************************************************************/
void
NdbTransaction::executeAsynchPrepare(NdbTransaction::ExecType aTypeOfExec,
NdbAsynchCallback aCallback,
void* anyObject,
NdbOperation::AbortOption abortOption)
{
DBUG_ENTER("NdbTransaction::executeAsynchPrepare");
DBUG_PRINT("enter", ("aTypeOfExec: %d, aCallback: 0x%lx, anyObject: Ox%lx",
aTypeOfExec, (long) aCallback, (long) anyObject));
/**
* Reset error.code on execute
*/
#ifndef DBUG_OFF
if (theError.code != 0)
DBUG_PRINT("enter", ("Resetting error %d on execute", theError.code));
#endif
{
switch (aTypeOfExec)
{
case NdbTransaction::Commit:
theNdb->theImpl->incClientStat(Ndb::TransCommitCount, 1);
break;
case NdbTransaction::Rollback:
theNdb->theImpl->incClientStat(Ndb::TransAbortCount, 1);
break;
default:
break;
}
}
/**
* for timeout (4012) we want sendROLLBACK to behave differently.
* Else, normal behaviour of reset errcode
*/
if (theError.code != 4012)
theError.code = 0;
/***************************************************************************
* Eager garbage collect queries which has completed execution
* w/ all its results made available to client.
* TODO: Add a member 'doEagerRelease' to check below.
**************************************************************************/
if (false) {
releaseCompletedQueries();
}
NdbScanOperation* tcOp = m_theFirstScanOperation;
if (tcOp != 0){
// Execute any cursor operations
while (tcOp != NULL) {
int tReturnCode;
tReturnCode = tcOp->executeCursor(theDBnode);
if (tReturnCode == -1) {
DBUG_VOID_RETURN;
}//if
tcOp->postExecuteRelease(); // Release unneeded resources
// outside TP mutex
tcOp = (NdbScanOperation*)tcOp->next();
} // while
m_theLastScanOperation->next(m_firstExecutedScanOp);
m_firstExecutedScanOp = m_theFirstScanOperation;
// Discard cursor operations, since these are also
// in the complete operations list we do not need
// to release them.
m_theFirstScanOperation = m_theLastScanOperation = NULL;
}
bool tTransactionIsStarted = theTransactionIsStarted;
NdbOperation* tLastOp = theLastOpInList;
Ndb* tNdb = theNdb;
CommitStatusType tCommitStatus = theCommitStatus;
Uint32 tnoOfPreparedTransactions = tNdb->theNoOfPreparedTransactions;
theReturnStatus = ReturnSuccess;
theCallbackFunction = aCallback;
theCallbackObject = anyObject;
m_waitForReply = true;
tNdb->thePreparedTransactionsArray[tnoOfPreparedTransactions] = this;
theTransArrayIndex = tnoOfPreparedTransactions;
theListState = InPreparedList;
tNdb->theNoOfPreparedTransactions = tnoOfPreparedTransactions + 1;
theNoOfOpSent = 0;
theNoOfOpCompleted = 0;
NdbNodeBitmask::clear(m_db_nodes);
NdbNodeBitmask::clear(m_failed_db_nodes);
if ((tCommitStatus != Started) ||
(aTypeOfExec == Rollback)) {
/*****************************************************************************
* Rollback have been ordered on a started transaction. Call rollback.
* Could also be state problem or previous problem which leads to the
* same action.
****************************************************************************/
if (aTypeOfExec == Rollback) {
if (theTransactionIsStarted == false || theSimpleState) {
theCommitStatus = Aborted;
theSendStatus = sendCompleted;
} else {
theSendStatus = sendABORT;
}
} else {
theSendStatus = sendABORTfail;
}//if
if (theCommitStatus == Aborted){
DBUG_PRINT("exit", ("theCommitStatus: Aborted"));
setErrorCode(4350);
}
DBUG_VOID_RETURN;
}//if
NdbQueryImpl* const lastLookupQuery = getLastLookupQuery(m_firstQuery);
if (tTransactionIsStarted == true) {
if (tLastOp != NULL) {
if (aTypeOfExec == Commit) {
/*****************************************************************************
* Set commit indicator on last operation when commit has been ordered
* and also a number of operations.
******************************************************************************/
tLastOp->theCommitIndicator = 1;
}//if
} else if (lastLookupQuery != NULL) {
if (aTypeOfExec == Commit) {
lastLookupQuery->setCommitIndicator();
}
} else if (m_firstQuery == NULL) {
if (aTypeOfExec == Commit && !theSimpleState) {
/**********************************************************************
* A Transaction have been started and no more operations exist.
* We will use the commit method.
*********************************************************************/
theSendStatus = sendCOMMITstate;
DBUG_VOID_RETURN;
} else {
/**********************************************************************
* We need to put it into the array of completed transactions to
* ensure that we report the completion in a proper way.
* We cannot do this here since that would endanger the completed
* transaction array since that is also updated from the receiver
* thread and thus we need to do it under mutex lock and thus we
* set the sendStatus to ensure that the send method will
* put it into the completed array.
**********************************************************************/
theSendStatus = sendCompleted;
DBUG_VOID_RETURN; // No Commit with no operations is OK
}//if
}//if
} else if (tTransactionIsStarted == false) {
NdbOperation* tFirstOp = theFirstOpInList;
/*
* Lookups that are roots of queries are sent before non-linked lookups.
* If both types are present, then the start indicator should be set
* on a query root lookup, and the commit indicator on a non-linked
* lookup.
*/
if (lastLookupQuery != NULL) {
getFirstLookupQuery(m_firstQuery)->setStartIndicator();
} else if (tFirstOp != NULL) {
tFirstOp->setStartIndicator();
}
if (tFirstOp != NULL) {
if (aTypeOfExec == Commit) {
tLastOp->theCommitIndicator = 1;
}//if
} else if (lastLookupQuery != NULL) {
if (aTypeOfExec == Commit) {
lastLookupQuery->setCommitIndicator();
}//if
} else if (m_firstQuery == NULL) {
/***********************************************************************
* No operations are defined and we have not started yet.
* Simply return OK. Set commit status if Commit.
***********************************************************************/
if (aTypeOfExec == Commit) {
theCommitStatus = Committed;
}//if
/***********************************************************************
* We need to put it into the array of completed transactions to
* ensure that we report the completion in a proper way. We
* cannot do this here since that would endanger the completed
* transaction array since that is also updated from the
* receiver thread and thus we need to do it under mutex lock
* and thus we set the sendStatus to ensure that the send method
* will put it into the completed array.
***********************************************************************/
theSendStatus = sendCompleted;
DBUG_VOID_RETURN;
}//if
}
theCompletionStatus = NotCompleted;
// Prepare sending of all pending NdbQuery's
if (m_firstQuery) {
NdbQueryImpl* query = m_firstQuery;
NdbQueryImpl* last = NULL;
while (query!=NULL) {
const int tReturnCode = query->prepareSend();
if (unlikely(tReturnCode != 0)) {
theSendStatus = sendABORTfail;
DBUG_VOID_RETURN;
}//if
last = query;
query = query->getNext();
}
assert (m_firstExecQuery==NULL);
last->setNext(m_firstExecQuery);
m_firstExecQuery = m_firstQuery;
m_firstQuery = NULL;
}
// Prepare sending of all pending (non-scan) NdbOperations's
NdbOperation* tOp = theFirstOpInList;
Uint32 pkOpCount = 0;
Uint32 ukOpCount = 0;
while (tOp) {
int tReturnCode;
NdbOperation* tNextOp = tOp->next();
/* Count operation */
if (tOp->theTCREQ->theVerId_signalNumber == GSN_TCINDXREQ)
ukOpCount++;
else
pkOpCount++;
if (tOp->Status() == NdbOperation::UseNdbRecord)
tReturnCode = tOp->prepareSendNdbRecord(abortOption);
else
tReturnCode= tOp->prepareSend(theTCConPtr, theTransactionId, abortOption);
if (tReturnCode == -1) {
theSendStatus = sendABORTfail;
DBUG_VOID_RETURN;
}//if
/*************************************************************************
* Now that we have successfully prepared the send of this operation we
* move it to the list of executing operations and remove it from the
* list of defined operations.
************************************************************************/
tOp = tNextOp;
}
theNdb->theImpl->incClientStat(Ndb::PkOpCount, pkOpCount);
theNdb->theImpl->incClientStat(Ndb::UkOpCount, ukOpCount);
NdbOperation* tLastOpInList = theLastOpInList;
NdbOperation* tFirstOpInList = theFirstOpInList;
theFirstOpInList = NULL;
theLastOpInList = NULL;
theFirstExecOpInList = tFirstOpInList;
theLastExecOpInList = tLastOpInList;
theCompletionStatus = CompletedSuccess;
theSendStatus = sendOperations;
DBUG_VOID_RETURN;
}//NdbTransaction::executeAsynchPrepare()
void
NdbTransaction::executeAsynch(ExecType aTypeOfExec,
NdbAsynchCallback aCallback,
void* anyObject,
NdbOperation::AbortOption abortOption,
int forceSend)
{
executeAsynchPrepare(aTypeOfExec, aCallback, anyObject, abortOption);
theNdb->sendPreparedTransactions(forceSend);
}
void NdbTransaction::close()
{
theNdb->closeTransaction(this);
}
int NdbTransaction::refresh()
{
for(NdbIndexScanOperation* scan_op = m_firstExecutedScanOp;
scan_op != 0; scan_op = (NdbIndexScanOperation *) scan_op->theNext)
{
NdbTransaction* scan_trans = scan_op->theNdbCon;
if (scan_trans)
{
scan_trans->sendTC_HBREP();
}
}
return sendTC_HBREP();
}
/*****************************************************************************
int sendTC_HBREP();
Return Value: No return value.
Parameters : None.
Remark: Order NDB to refresh the timeout counter of the transaction.
******************************************************************************/
int
NdbTransaction::sendTC_HBREP() // Send a TC_HBREP signal;
{
NdbApiSignal* tSignal;
Ndb* tNdb = theNdb;
Uint32 tTransId1, tTransId2;
tSignal = tNdb->getSignal();
if (tSignal == NULL) {
return -1;
}
if (tSignal->setSignal(GSN_TC_HBREP, refToBlock(m_tcRef)) == -1) {
return -1;