NdbTransaction.cpp 69.3 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   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
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15

   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 */

16
#include <ndb_global.h>
joreland@mysql.com's avatar
joreland@mysql.com committed
17
#include <NdbOut.hpp>
18
#include <NdbTransaction.hpp>
joreland@mysql.com's avatar
joreland@mysql.com committed
19 20 21 22
#include <NdbOperation.hpp>
#include <NdbScanOperation.hpp>
#include <NdbIndexScanOperation.hpp>
#include <NdbIndexOperation.hpp>
23 24 25
#include "NdbApiSignal.hpp"
#include "TransporterFacade.hpp"
#include "API.hpp"
pekka@mysql.com's avatar
pekka@mysql.com committed
26
#include "NdbBlob.hpp"
27 28 29 30 31 32 33 34

#include <signaldata/TcKeyConf.hpp>
#include <signaldata/TcIndx.hpp>
#include <signaldata/TcCommit.hpp>
#include <signaldata/TcKeyFailConf.hpp>
#include <signaldata/TcHbRep.hpp>

/*****************************************************************************
35
NdbTransaction( Ndb* aNdb );
36 37 38 39 40

Return Value:  None
Parameters:    aNdb: Pointers to the Ndb object 
Remark:        Creates a connection object. 
*****************************************************************************/
41
NdbTransaction::NdbTransaction( Ndb* aNdb ) :
42 43 44 45 46 47 48 49 50 51 52 53 54 55
  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),
56
  theCompletedLastOp(NULL),
57 58 59 60 61 62
  theNoOfOpSent(0),
  theNoOfOpCompleted(0),
  theMyRef(0),
  theTCConPtr(0),
  theTransactionId(0),
  theGlobalCheckpointId(0),
63
  p_latest_trans_gci(0),
64 65 66 67 68 69 70
  theStatus(NotConnected),
  theCompletionStatus(NotCompleted), 
  theCommitStatus(NotStarted),
  theMagicNumber(0xFE11DC),
  theTransactionIsStarted(false),
  theDBnode(0),
  theReleaseOnClose(false),
joreland@mysql.com's avatar
joreland@mysql.com committed
71
  // Scan operations
72
  m_waitForReply(true),
joreland@mysql.com's avatar
joreland@mysql.com committed
73 74 75
  m_theFirstScanOperation(NULL),
  m_theLastScanOperation(NULL),
  m_firstExecutedScanOp(NULL),
76 77
  // Scan operations
  theScanningOp(NULL),
pekka@mysql.com's avatar
pekka@mysql.com committed
78
  theBuddyConPtr(0xFFFFFFFF),
pekka@mysql.com's avatar
pekka@mysql.com committed
79 80
  theBlobFlag(false),
  thePendingBlobOps(0)
81 82 83
{
  theListState = NotInList;
  theError.code = 0;
84
  //theId = NdbObjectIdMap::InvalidId;
85
  theId = theNdb->theImpl->theNdbObjectIdMap.map(this);
86 87 88 89 90

#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);
91
}//NdbTransaction::NdbTransaction()
92 93

/*****************************************************************************
94
~NdbTransaction();
95 96 97

Remark:        Deletes the connection object. 
*****************************************************************************/
98
NdbTransaction::~NdbTransaction()
99
{
100
  DBUG_ENTER("NdbTransaction::~NdbTransaction");
101
  theNdb->theImpl->theNdbObjectIdMap.unmap(theId, this);
102
  DBUG_VOID_RETURN;
103
}//NdbTransaction::~NdbTransaction()
104 105 106 107 108 109

/*****************************************************************************
void init();

Remark:         Initialise connection object for new transaction. 
*****************************************************************************/
110
int
111
NdbTransaction::init()
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
  theListState            = NotInList;
  theInUseState           = true;
  theTransactionIsStarted = false;
  theNext		  = NULL;

  theFirstOpInList	  = NULL;
  theLastOpInList	  = NULL;

  theScanningOp            = NULL;

  theFirstExecOpInList	  = NULL;
  theLastExecOpInList	  = NULL;

  theCompletedFirstOp	  = NULL;
127
  theCompletedLastOp	  = NULL;
128 129

  theGlobalCheckpointId   = 0;
130 131
  p_latest_trans_gci      =
    theNdb->theImpl->m_ndb_cluster_connection.get_latest_trans_gci();
132 133 134 135 136 137 138 139 140 141 142
  theCommitStatus         = Started;
  theCompletionStatus     = NotCompleted;

  theError.code		  = 0;
  theErrorLine		  = 0;
  theErrorOperation	  = NULL;

  theReleaseOnClose       = false;
  theSimpleState          = true;
  theSendStatus           = InitState;
  theMagicNumber          = 0x37412619;
joreland@mysql.com's avatar
joreland@mysql.com committed
143
  // Scan operations
144
  m_waitForReply            = true;
joreland@mysql.com's avatar
joreland@mysql.com committed
145 146 147
  m_theFirstScanOperation = NULL;
  m_theLastScanOperation  = NULL;
  m_firstExecutedScanOp   = 0;
148
  theBuddyConPtr            = 0xFFFFFFFF;
pekka@mysql.com's avatar
pekka@mysql.com committed
149 150
  //
  theBlobFlag = false;
pekka@mysql.com's avatar
pekka@mysql.com committed
151
  thePendingBlobOps = 0;
152 153 154 155 156 157 158 159 160 161 162
  if (theId == NdbObjectIdMap::InvalidId)
  {
    theId = theNdb->theImpl->theNdbObjectIdMap.map(this);
    if (theId == NdbObjectIdMap::InvalidId)
    {
      theError.code = 4000;
      return -1;
    }
  }
  return 0;

163
}//NdbTransaction::init()
164 165

/*****************************************************************************
166
setOperationErrorCode(int error);
167 168 169 170 171

Remark:        Sets an error code on the connection object from an 
               operation object. 
*****************************************************************************/
void
172
NdbTransaction::setOperationErrorCode(int error)
173
{
174
  DBUG_ENTER("NdbTransaction::setOperationErrorCode");
175 176 177
  setErrorCode(error);
  DBUG_VOID_RETURN;
}
178 179

/*****************************************************************************
180
setOperationErrorCodeAbort(int error);
181 182 183 184 185

Remark:        Sets an error code on the connection object from an 
               operation object. 
*****************************************************************************/
void
186
NdbTransaction::setOperationErrorCodeAbort(int error, int abortOption)
187
{
188
  DBUG_ENTER("NdbTransaction::setOperationErrorCodeAbort");
189 190
  if (theTransactionIsStarted == false) {
    theCommitStatus = Aborted;
191
  } else if ((theCommitStatus != Committed) &&
192 193 194
             (theCommitStatus != Aborted)) {
    theCommitStatus = NeedAbort;
  }//if
195 196 197
  setErrorCode(error);
  DBUG_VOID_RETURN;
}
198 199 200 201 202 203 204

/*****************************************************************************
setErrorCode(int anErrorCode);

Remark:        Sets an error indication on the connection object. 
*****************************************************************************/
void
205
NdbTransaction::setErrorCode(int error)
206
{
207
  DBUG_ENTER("NdbTransaction::setErrorCode");
208 209
  DBUG_PRINT("enter", ("error: %d, theError.code: %d", error, theError.code));

210
  if (theError.code == 0)
211 212 213
    theError.code = error;

  DBUG_VOID_RETURN;
214
}//NdbTransaction::setErrorCode()
215

joreland@mysql.com's avatar
joreland@mysql.com committed
216
int
217 218
NdbTransaction::restart(){
  DBUG_ENTER("NdbTransaction::restart");
joreland@mysql.com's avatar
joreland@mysql.com committed
219 220 221 222
  if(theCompletionStatus == CompletedSuccess){
    releaseCompletedOperations();
    Uint64 tTransid = theNdb->theFirstTransId;
    theTransactionId = tTransid;
223
    if ((tTransid & 0xFFFFFFFF) == 0xFFFFFFFF) {
joreland@mysql.com's avatar
joreland@mysql.com committed
224 225 226 227
      theNdb->theFirstTransId = (tTransid >> 32) << 32;
    } else {
      theNdb->theFirstTransId = tTransid + 1;
    }
228
    theCommitStatus = Started;
joreland@mysql.com's avatar
joreland@mysql.com committed
229
    theCompletionStatus = NotCompleted;
230
    theTransactionIsStarted = false;
231
    DBUG_RETURN(0);
joreland@mysql.com's avatar
joreland@mysql.com committed
232
  }
233 234
  DBUG_PRINT("error",("theCompletionStatus != CompletedSuccess"));
  DBUG_RETURN(-1);
joreland@mysql.com's avatar
joreland@mysql.com committed
235 236
}

237 238 239 240 241 242
/*****************************************************************************
void handleExecuteCompletion(void);

Remark:        Handle time-out on a transaction object. 
*****************************************************************************/
void
243
NdbTransaction::handleExecuteCompletion()
244 245 246 247 248 249 250 251 252 253
{
  /***************************************************************************
   *	  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;
254 255
    if (theCompletedLastOp == NULL)
      theCompletedLastOp = tLastExecOp;
256 257 258 259 260
    theFirstExecOpInList = NULL;
    theLastExecOpInList = NULL;
  }//if
  theSendStatus = InitState;
  return;
261
}//NdbTransaction::handleExecuteCompletion()
262 263 264 265 266 267 268 269 270 271

/*****************************************************************************
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 
272
NdbTransaction::execute(ExecType aTypeOfExec, 
273 274
			NdbOperation::AbortOption abortOption,
			int forceSend)
pekka@mysql.com's avatar
pekka@mysql.com committed
275
{
276
  NdbError savedError= theError;
277
  DBUG_ENTER("NdbTransaction::execute");
278 279 280
  DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d", 
		       aTypeOfExec, abortOption));

pekka@mysql.com's avatar
pekka@mysql.com committed
281
  if (! theBlobFlag)
282
    DBUG_RETURN(executeNoBlobs(aTypeOfExec, abortOption, forceSend));
pekka@mysql.com's avatar
pekka@mysql.com committed
283

pekka@mysql.com's avatar
pekka@mysql.com committed
284 285 286 287 288 289
  /*
   * 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
   */
pekka@mysql.com's avatar
pekka@mysql.com committed
290 291 292

  ExecType tExecType;
  NdbOperation* tPrepOp;
293 294
  NdbOperation* tCompletedFirstOp = NULL;
  NdbOperation* tCompletedLastOp = NULL;
pekka@mysql.com's avatar
pekka@mysql.com committed
295

pekka@mysql.com's avatar
pekka@mysql.com committed
296
  int ret = 0;
pekka@mysql.com's avatar
pekka@mysql.com committed
297 298 299 300
  do {
    tExecType = aTypeOfExec;
    tPrepOp = theFirstOpInList;
    while (tPrepOp != NULL) {
pekka@mysql.com's avatar
pekka@mysql.com committed
301 302 303 304 305
      if (tPrepOp->theError.code == 0) {
        bool batch = false;
        NdbBlob* tBlob = tPrepOp->theBlobList;
        while (tBlob != NULL) {
          if (tBlob->preExecute(tExecType, batch) == -1)
306
	  {
pekka@mysql.com's avatar
pekka@mysql.com committed
307
            ret = -1;
308 309 310
	    if(savedError.code==0)
	      savedError= theError;
	  }
pekka@mysql.com's avatar
pekka@mysql.com committed
311 312 313 314 315 316 317
          tBlob = tBlob->theNext;
        }
        if (batch) {
          // blob asked to execute all up to here now
          tExecType = NoCommit;
          break;
        }
pekka@mysql.com's avatar
pekka@mysql.com committed
318 319 320
      }
      tPrepOp = tPrepOp->next();
    }
321

pekka@mysql.com's avatar
pekka@mysql.com committed
322
    // save rest of prepared ops if batch
323 324
    NdbOperation* tRestOp= 0;
    NdbOperation* tLastOp= 0;
pekka@mysql.com's avatar
pekka@mysql.com committed
325 326 327 328 329 330
    if (tPrepOp != NULL) {
      tRestOp = tPrepOp->next();
      tPrepOp->next(NULL);
      tLastOp = theLastOpInList;
      theLastOpInList = tPrepOp;
    }
331

pekka@mysql.com's avatar
pekka@mysql.com committed
332 333 334
    if (tExecType == Commit) {
      NdbOperation* tOp = theCompletedFirstOp;
      while (tOp != NULL) {
pekka@mysql.com's avatar
pekka@mysql.com committed
335 336 337 338
        if (tOp->theError.code == 0) {
          NdbBlob* tBlob = tOp->theBlobList;
          while (tBlob != NULL) {
            if (tBlob->preCommit() == -1)
339 340 341 342 343
	    {
	      ret = -1;
	      if(savedError.code==0)
		savedError= theError;
	    }
pekka@mysql.com's avatar
pekka@mysql.com committed
344 345
            tBlob = tBlob->theNext;
          }
pekka@mysql.com's avatar
pekka@mysql.com committed
346 347 348 349
        }
        tOp = tOp->next();
      }
    }
350

351 352 353 354 355 356 357 358 359 360 361 362 363
    // 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;
    }

364 365 366
    if (executeNoBlobs(tExecType, 
		       NdbOperation::DefaultAbortOption,
		       forceSend) == -1)
367 368 369
    {
      if(savedError.code==0)
	savedError= theError;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
370
      
371
      DBUG_RETURN(-1);
372
    }
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
373
    
pekka@mysql.com's avatar
pekka@mysql.com committed
374
#ifdef ndb_api_crash_on_complex_blob_abort
375
    assert(theFirstOpInList == NULL && theLastOpInList == NULL);
pekka@mysql.com's avatar
pekka@mysql.com committed
376 377
#else
    theFirstOpInList = theLastOpInList = NULL;
378
#endif
379

pekka@mysql.com's avatar
pekka@mysql.com committed
380 381 382
    {
      NdbOperation* tOp = theCompletedFirstOp;
      while (tOp != NULL) {
pekka@mysql.com's avatar
pekka@mysql.com committed
383 384 385 386 387
        if (tOp->theError.code == 0) {
          NdbBlob* tBlob = tOp->theBlobList;
          while (tBlob != NULL) {
            // may add new operations if batch
            if (tBlob->postExecute(tExecType) == -1)
388
	    {
pekka@mysql.com's avatar
pekka@mysql.com committed
389
              ret = -1;
390 391 392
	      if(savedError.code==0)
		savedError= theError;
	    }
pekka@mysql.com's avatar
pekka@mysql.com committed
393 394
            tBlob = tBlob->theNext;
          }
pekka@mysql.com's avatar
pekka@mysql.com committed
395 396 397 398
        }
        tOp = tOp->next();
      }
    }
399

pekka@mysql.com's avatar
pekka@mysql.com committed
400 401 402 403 404 405 406 407
    // add saved prepared ops if batch
    if (tPrepOp != NULL && tRestOp != NULL) {
      if (theFirstOpInList == NULL)
        theFirstOpInList = tRestOp;
      else
        theLastOpInList->next(tRestOp);
      theLastOpInList = tLastOp;
    }
408
    assert(theFirstOpInList == NULL || tExecType == NoCommit);
pekka@mysql.com's avatar
pekka@mysql.com committed
409 410
  } while (theFirstOpInList != NULL || tExecType != aTypeOfExec);

411 412 413 414 415 416 417 418 419 420 421 422
  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
423 424 425 426

  if(savedError.code!=0 && theError.code==4350) // Trans already aborted
      theError= savedError;

427
  DBUG_RETURN(ret);
pekka@mysql.com's avatar
pekka@mysql.com committed
428 429 430
}

int 
431 432 433
NdbTransaction::executeNoBlobs(NdbTransaction::ExecType aTypeOfExec, 
			       NdbOperation::AbortOption abortOption,
			       int forceSend)
434
{
435
  DBUG_ENTER("NdbTransaction::executeNoBlobs");
436 437 438
  DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d", 
		       aTypeOfExec, abortOption));

439 440 441 442 443
//------------------------------------------------------------------------
// 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.
444
// The NdbTransactionCallBack-method will receive the return code of the
445 446 447 448
// transaction. The normal methods of reading error codes still apply.
//------------------------------------------------------------------------
  Ndb* tNdb = theNdb;

449
  Uint32 timeout = theNdb->theImpl->m_transporter_facade->m_waitfor_timeout;
450 451 452 453
  m_waitForReply = false;
  executeAsynchPrepare(aTypeOfExec, NULL, NULL, abortOption);
  if (m_waitForReply){
    while (1) {
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
454
      int noOfComp = tNdb->sendPollNdb(3 * timeout, 1, forceSend);
455 456 457 458 459
      if (noOfComp == 0) {
        /** 
         * This timeout situation can occur if NDB crashes.
         */
        ndbout << "This timeout should never occur, execute(..)" << endl;
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
460
	theError.code = 4012;
461
        setOperationErrorCodeAbort(4012);  // Error code for "Cluster Failure"
462
        DBUG_RETURN(-1);
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      }//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 %x", this);
        abort();
      }
#endif
      if (theReturnStatus == ReturnFailure) {
486
        DBUG_RETURN(-1);
487 488 489 490
      }//if
      break;
    }
  }
pekka@mysql.com's avatar
pekka@mysql.com committed
491
  thePendingBlobOps = 0;
492
  DBUG_RETURN(0);
493
}//NdbTransaction::execute()
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

/*****************************************************************************
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 
511
NdbTransaction::executeAsynchPrepare(NdbTransaction::ExecType aTypeOfExec,
512 513
                                     NdbAsynchCallback  aCallback,
                                     void*              anyObject,
514
                                     NdbOperation::AbortOption abortOption)
515
{
516
  DBUG_ENTER("NdbTransaction::executeAsynchPrepare");
517 518
  DBUG_PRINT("enter", ("aTypeOfExec: %d, aCallback: 0x%lx, anyObject: Ox%lx",
		       aTypeOfExec, (long) aCallback, (long) anyObject));
519

520 521 522
  /**
   * Reset error.code on execute
   */
523 524
  if (theError.code != 0)
    DBUG_PRINT("enter", ("Resetting error %d on execute", theError.code));
525
  theError.code = 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
526
  NdbScanOperation* tcOp = m_theFirstScanOperation;
527 528 529 530 531 532
  if (tcOp != 0){
    // Execute any cursor operations
    while (tcOp != NULL) {
      int tReturnCode;
      tReturnCode = tcOp->executeCursor(theDBnode);
      if (tReturnCode == -1) {
533
        DBUG_VOID_RETURN;
534
      }//if
joreland@mysql.com's avatar
joreland@mysql.com committed
535
      tcOp = (NdbScanOperation*)tcOp->next();
536
    } // while
joreland@mysql.com's avatar
joreland@mysql.com committed
537 538
    m_theLastScanOperation->next(m_firstExecutedScanOp);
    m_firstExecutedScanOp = m_theFirstScanOperation;
539 540 541
    // Discard cursor operations, since these are also
    // in the complete operations list we do not need
    // to release them.
joreland@mysql.com's avatar
joreland@mysql.com committed
542
    m_theFirstScanOperation = m_theLastScanOperation = NULL;
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
  }

  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;

  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) {
568
      if (theTransactionIsStarted == false || theSimpleState) {
569 570 571 572 573 574 575 576
	theCommitStatus = Aborted;
	theSendStatus = sendCompleted;
      } else {
	theSendStatus = sendABORT;
      }
    } else {
      theSendStatus = sendABORTfail;
    }//if
577 578 579 580 581
    if (theCommitStatus == Aborted){
      DBUG_PRINT("exit", ("theCommitStatus: Aborted"));
      setErrorCode(4350);
    }
    DBUG_VOID_RETURN;
582 583 584 585 586 587 588 589 590 591 592
  }//if
  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 {
593
      if (aTypeOfExec == Commit && !theSimpleState) {
594 595 596 597 598
	/**********************************************************************
	 *   A Transaction have been started and no more operations exist. 
	 *   We will use the commit method.
	 *********************************************************************/
        theSendStatus = sendCOMMITstate;
599
	DBUG_VOID_RETURN;
600 601 602 603 604 605 606 607 608 609 610
      } 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;
611
	DBUG_VOID_RETURN; // No Commit with no operations is OK
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
      }//if
    }//if
  } else if (tTransactionIsStarted == false) {
    NdbOperation* tFirstOp = theFirstOpInList;
    if (tLastOp != NULL) {
      tFirstOp->setStartIndicator();
      if (aTypeOfExec == Commit) {
        tLastOp->theCommitIndicator = 1;
      }//if
    } else {
      /***********************************************************************
       *    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;
639
      DBUG_VOID_RETURN;
640 641 642 643 644 645 646 647
    }//if
  }

  NdbOperation* tOp = theFirstOpInList;
  theCompletionStatus = NotCompleted;
  while (tOp) {
    int tReturnCode;
    NdbOperation* tNextOp = tOp->next();
648
    tReturnCode = tOp->prepareSend(theTCConPtr, theTransactionId, abortOption);
649 650
    if (tReturnCode == -1) {
      theSendStatus = sendABORTfail;
651
      DBUG_VOID_RETURN;
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    }//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;
  } 

  NdbOperation* tLastOpInList = theLastOpInList;
  NdbOperation* tFirstOpInList = theFirstOpInList;

  theFirstOpInList = NULL;
  theLastOpInList = NULL;
  theFirstExecOpInList = tFirstOpInList;
  theLastExecOpInList = tLastOpInList;

  theCompletionStatus = CompletedSuccess;
  theNoOfOpSent		= 0;
  theNoOfOpCompleted	= 0;
  theSendStatus = sendOperations;
674 675
  NdbNodeBitmask::clear(m_db_nodes);
  NdbNodeBitmask::clear(m_failed_db_nodes);
676
  DBUG_VOID_RETURN;
677
}//NdbTransaction::executeAsynchPrepare()
678

679 680 681 682 683 684 685 686 687 688 689
void
NdbTransaction::executeAsynch(ExecType aTypeOfExec,
                              NdbAsynchCallback aCallback,
                              void* anyObject,
                              AbortOption abortOption,
                              int forceSend)
{
  executeAsynchPrepare(aTypeOfExec, aCallback, anyObject, abortOption);
  theNdb->sendPreparedTransactions(forceSend);
}

690
void NdbTransaction::close()
691 692 693 694
{
  theNdb->closeTransaction(this);
}

695
int NdbTransaction::refresh(){
696 697 698 699 700 701 702 703 704 705 706
  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 	
707
NdbTransaction::sendTC_HBREP()		// Send a TC_HBREP signal;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
{
  NdbApiSignal* tSignal;
  Ndb* tNdb = theNdb;
  Uint32 tTransId1, tTransId2;

  tSignal = tNdb->getSignal();
  if (tSignal == NULL) {
    return -1;
  }

  if (tSignal->setSignal(GSN_TC_HBREP) == -1) {
    return -1;
  }

  TcHbRep * const tcHbRep = CAST_PTR(TcHbRep, tSignal->getDataPtrSend());
  
  tcHbRep->apiConnectPtr = theTCConPtr;    
  
  tTransId1 = (Uint32) theTransactionId;
  tTransId2 = (Uint32) (theTransactionId >> 32);
  tcHbRep->transId1      = tTransId1;
  tcHbRep->transId2      = tTransId2;
 
731
  TransporterFacade *tp = theNdb->theImpl->m_transporter_facade;
732 733 734 735 736 737 738 739 740 741
  tp->lock_mutex(); 
  const int res = tp->sendSignal(tSignal,theDBnode);
  tp->unlock_mutex(); 
  tNdb->releaseSignal(tSignal);

  if (res == -1){
    return -1;
  }    
  
  return 0;
742
}//NdbTransaction::sendTC_HBREP()
743 744 745 746 747 748 749 750 751 752 753

/*****************************************************************************
int doSend();

Return Value:  Return 0 : send was successful.
               Return -1: In all other case.  
Remark:        Send all operations belonging to this connection.
               The caller of this method has the responsibility to remove the
               object from the prepared transactions array on the Ndb-object.
*****************************************************************************/
int
754
NdbTransaction::doSend()
755
{
756
  DBUG_ENTER("NdbTransaction::doSend");
757

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  /*
  This method assumes that at least one operation have been defined. This
  is ensured by the caller of this routine (=execute).
  */

  switch(theSendStatus){
  case sendOperations: {
    NdbOperation * tOp = theFirstExecOpInList;
    do {
      NdbOperation* tNextOp = tOp->next();
      const Uint32 lastFlag = ((tNextOp == NULL) ? 1 : 0);
      const int tReturnCode = tOp->doSend(theDBnode, lastFlag);
      if (tReturnCode == -1) {
        theReturnStatus = ReturnFailure;
        break;
      }//if
      tOp = tNextOp;
    } while (tOp != NULL);
    Ndb* tNdb = theNdb;
    theSendStatus = sendTC_OP;
    theTransactionIsStarted = true;
    tNdb->insert_sent_list(this);
780
    DBUG_RETURN(0);
781 782 783 784 785 786 787 788 789 790 791
  }//case
  case sendABORT:
  case sendABORTfail:{
  /***********************************************************************
   * Rollback have been ordered on a not started transaction. 
   * Simply return OK and set abort status.
   ***********************************************************************/
    if (theSendStatus == sendABORTfail) {
      theReturnStatus = ReturnFailure;
    }//if
    if (sendROLLBACK() == 0) {
792
      DBUG_RETURN(0);
793 794 795 796 797
    }//if
    break;
  }//case
  case sendCOMMITstate:
    if (sendCOMMIT() == 0) {
798
      DBUG_RETURN(0);
799 800 801 802
    }//if
    break;
  case sendCompleted:
    theNdb->insert_completed_list(this); 
803
    DBUG_RETURN(0);
804
  default:
805 806
    ndbout << "Inconsistent theSendStatus = "
	   << (Uint32) theSendStatus << endl;
807 808 809 810 811 812 813
    abort();
    break;
  }//switch
  setOperationErrorCodeAbort(4002);
  theReleaseOnClose = true;
  theTransactionIsStarted = false;
  theCommitStatus = Aborted;
814
  DBUG_RETURN(-1);
815
}//NdbTransaction::doSend()
816 817 818 819 820 821 822 823 824

/**************************************************************************
int sendROLLBACK();

Return Value:  Return -1 if send unsuccessful.  
Parameters :   None.
Remark:        Order NDB to rollback the transaction. 
**************************************************************************/
int 	
825
NdbTransaction::sendROLLBACK()      // Send a TCROLLBACKREQ signal;
826 827 828 829 830 831 832 833 834 835 836
{
  Ndb* tNdb = theNdb;
  if ((theTransactionIsStarted == true) &&
      (theCommitStatus != Committed) &&
      (theCommitStatus != Aborted)) {
/**************************************************************************
 *	The user did not perform any rollback but simply closed the
 *      transaction. We must rollback Ndb since Ndb have been contacted.
 *************************************************************************/
    NdbApiSignal tSignal(tNdb->theMyRef);
    Uint32 tTransId1, tTransId2;
837
    TransporterFacade *tp = theNdb->theImpl->m_transporter_facade;
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
    int	  tReturnCode;

    tTransId1 = (Uint32) theTransactionId;
    tTransId2 = (Uint32) (theTransactionId >> 32);
    tSignal.setSignal(GSN_TCROLLBACKREQ);
    tSignal.setData(theTCConPtr, 1);
    tSignal.setData(tTransId1, 2);
    tSignal.setData(tTransId2, 3);
    tReturnCode = tp->sendSignal(&tSignal,theDBnode);
    if (tReturnCode != -1) {
      theSendStatus = sendTC_ROLLBACK;
      tNdb->insert_sent_list(this);
      return 0;
    }//if
   /*********************************************************************
    * It was not possible to abort the transaction towards the NDB kernel
    * and thus we put it into the array of completed transactions that
    * are ready for reporting to the application.
    *********************************************************************/
    return -1;
  } else {
    /*
     It is not necessary to abort the transaction towards the NDB kernel and
     thus we put it into the array of completed transactions that are ready
     for reporting to the application.
     */
    theSendStatus = sendCompleted;
    tNdb->insert_completed_list(this);
    return 0;
    ;
  }//if
869
}//NdbTransaction::sendROLLBACK()
870 871 872 873 874 875 876 877 878 879

/***************************************************************************
int sendCOMMIT();

Return Value:  Return 0 : send was successful.
               Return -1: In all other case.  
Parameters :   None.
Remark:        Order NDB to commit the transaction. 
***************************************************************************/
int 	
880
NdbTransaction::sendCOMMIT()    // Send a TC_COMMITREQ signal;
881 882 883
{
  NdbApiSignal tSignal(theNdb->theMyRef);
  Uint32 tTransId1, tTransId2;
884
  TransporterFacade *tp = theNdb->theImpl->m_transporter_facade;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
  int	  tReturnCode;

  tTransId1 = (Uint32) theTransactionId;
  tTransId2 = (Uint32) (theTransactionId >> 32);
  tSignal.setSignal(GSN_TC_COMMITREQ);
  tSignal.setData(theTCConPtr, 1);
  tSignal.setData(tTransId1, 2);
  tSignal.setData(tTransId2, 3);
      
  tReturnCode = tp->sendSignal(&tSignal,theDBnode);
  if (tReturnCode != -1) {
    theSendStatus = sendTC_COMMIT;
    theNdb->insert_sent_list(this);
    return 0;
  } else {
    return -1;
  }//if
902
}//NdbTransaction::sendCOMMIT()
903 904 905 906 907 908 909

/******************************************************************************
void release();

Remark:         Release all operations.
******************************************************************************/
void 
910
NdbTransaction::release(){
911 912
  releaseOperations();
  if ( (theTransactionIsStarted == true) &&
joreland@mysql.com's avatar
joreland@mysql.com committed
913 914 915 916 917 918
       ((theCommitStatus != Committed) &&
	(theCommitStatus != Aborted))) {
    /************************************************************************
     *	The user did not perform any rollback but simply closed the
     *      transaction. We must rollback Ndb since Ndb have been contacted.
     ************************************************************************/
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
919 920 921 922
    if (!theSimpleState)
    {
      execute(Rollback);
    }
923 924 925 926 927 928 929 930 931
  }//if
  theMagicNumber = 0xFE11DC;
  theInUseState = false;
#ifdef VM_TRACE
  if (theListState != NotInList) {
    theNdb->printState("release %x", this);
    abort();
  }
#endif
932
}//NdbTransaction::release()
933 934

void
935
NdbTransaction::releaseOps(NdbOperation* tOp){
936 937 938 939 940 941 942 943 944 945 946 947 948 949
  while (tOp != NULL) {
    NdbOperation* tmp = tOp;
    tOp->release();
    tOp = tOp->next();
    theNdb->releaseOperation(tmp);
  }//while
}

/******************************************************************************
void releaseOperations();

Remark:         Release all operations.
******************************************************************************/
void 
950
NdbTransaction::releaseOperations()
951 952
{
  // Release any open scans
joreland@mysql.com's avatar
joreland@mysql.com committed
953 954
  releaseScanOperations(m_theFirstScanOperation);
  releaseScanOperations(m_firstExecutedScanOp);
955 956 957 958 959 960
  
  releaseOps(theCompletedFirstOp);
  releaseOps(theFirstOpInList);
  releaseOps(theFirstExecOpInList);

  theCompletedFirstOp = NULL;
961
  theCompletedLastOp = NULL;
962 963 964 965 966
  theFirstOpInList = NULL;
  theFirstExecOpInList = NULL;
  theLastOpInList = NULL;
  theLastExecOpInList = NULL;
  theScanningOp = NULL;
joreland@mysql.com's avatar
joreland@mysql.com committed
967 968 969
  m_theFirstScanOperation = NULL;
  m_theLastScanOperation = NULL;
  m_firstExecutedScanOp = NULL;
970
}//NdbTransaction::releaseOperations()
971 972

void 
973
NdbTransaction::releaseCompletedOperations()
974 975 976
{
  releaseOps(theCompletedFirstOp);
  theCompletedFirstOp = NULL;
977
  theCompletedLastOp = NULL;
978
}//NdbTransaction::releaseOperations()
979 980

/******************************************************************************
joreland@mysql.com's avatar
joreland@mysql.com committed
981
void releaseScanOperations();
982 983 984 985 986

Remark:         Release all cursor operations. 
                (NdbScanOperation and NdbIndexOperation)
******************************************************************************/
void 
987
NdbTransaction::releaseScanOperations(NdbIndexScanOperation* cursorOp)
988 989
{
  while(cursorOp != 0){
joreland@mysql.com's avatar
joreland@mysql.com committed
990
    NdbIndexScanOperation* next = (NdbIndexScanOperation*)cursorOp->next();
991
    cursorOp->release();
joreland@mysql.com's avatar
joreland@mysql.com committed
992
    theNdb->releaseScanOperation(cursorOp);
993 994
    cursorOp = next;
  }
995
}//NdbTransaction::releaseScanOperations()
996

997
/*****************************************************************************
998
void releaseScanOperation();
999 1000 1001 1002

Remark:         Release scan op when hupp'ed trans closed (save memory)
******************************************************************************/
void 
1003
NdbTransaction::releaseExecutedScanOperation(NdbIndexScanOperation* cursorOp)
1004
{
1005
  DBUG_ENTER("NdbTransaction::releaseExecutedScanOperation");
1006
  DBUG_PRINT("enter", ("this: 0x%lx  op: 0x%lx", (long) this, (long) cursorOp));
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1007 1008 1009 1010 1011
  
  releaseScanOperation(&m_firstExecutedScanOp, 0, cursorOp);
  
  DBUG_VOID_RETURN;
}//NdbTransaction::releaseExecutedScanOperation()
1012

jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1013 1014 1015 1016 1017 1018
bool
NdbTransaction::releaseScanOperation(NdbIndexScanOperation** listhead,
				     NdbIndexScanOperation** listtail,
				     NdbIndexScanOperation* op)
{
  if (* listhead == op)
1019
  {
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1020 1021 1022 1023 1024
    * listhead = (NdbIndexScanOperation*)op->theNext;
    if (listtail && *listtail == op)
    {
      assert(* listhead == 0);
      * listtail = 0;
1025
    }
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1026
      
1027 1028 1029
  }
  else
  {
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    NdbIndexScanOperation* tmp = * listhead;
    while (tmp != NULL)
    {
      if (tmp->theNext == op)
      {
	tmp->theNext = (NdbIndexScanOperation*)op->theNext;
	if (listtail && *listtail == op)
	{
	  assert(op->theNext == 0);
	  *listtail = tmp;
	}
	break;
1042
      }
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1043
      tmp = (NdbIndexScanOperation*)tmp->theNext;
1044
    }
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1045 1046
    if (tmp == NULL)
      op = NULL;
1047
  }
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  
  if (op != NULL)
  {
    op->release();
    theNdb->releaseScanOperation(op);
    return true;
  }
  
  return false;
}
1058

1059 1060 1061 1062 1063 1064 1065 1066
/*****************************************************************************
NdbOperation* getNdbOperation(const char* aTableName);

Return Value    Return a pointer to a NdbOperation object if getNdbOperation 
                was succesful.
                Return NULL : In all other case. 	
Parameters:     aTableName : Name of the database table. 	
Remark:         Get an operation from NdbOperation idlelist and get the 
1067
                NdbTransaction object 
1068 1069 1070 1071 1072
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbOperation object.
                synchronous
******************************************************************************/
NdbOperation*
1073
NdbTransaction::getNdbOperation(const char* aTableName)
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
  if (theCommitStatus == Started){
    NdbTableImpl* table = theNdb->theDictionary->getTable(aTableName);
    if (table != 0){
      return getNdbOperation(table);
    } else {
      setErrorCode(theNdb->theDictionary->getNdbError().code);
      return NULL;
    }//if
  }

  setOperationErrorCodeAbort(4114);
  
  return NULL;
1088
}//NdbTransaction::getNdbOperation()
1089 1090 1091 1092 1093 1094 1095 1096 1097

/*****************************************************************************
NdbOperation* getNdbOperation(int aTableId);

Return Value    Return a pointer to a NdbOperation object if getNdbOperation 
                was succesful.
                Return NULL: In all other case. 	
Parameters:     tableId : Id of the database table beeing deleted.
Remark:         Get an operation from NdbOperation object idlelist and 
1098
                get the NdbTransaction object who was fetch by 
1099 1100 1101 1102 1103
                startTransaction pointing to this operation 
  	        getOperation will set the theTableId in the NdbOperation 
                object, synchronous.
*****************************************************************************/
NdbOperation*
1104
NdbTransaction::getNdbOperation(const NdbTableImpl * tab, NdbOperation* aNextOp)
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
{ 
  NdbOperation* tOp;

  if (theScanningOp != NULL){
    setErrorCode(4607);
    return NULL;
  }
  
  tOp = theNdb->getOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
pekka@mysql.com's avatar
pekka@mysql.com committed
1116 1117 1118 1119 1120 1121 1122 1123 1124
  if (aNextOp == NULL) {
    if (theLastOpInList != NULL) {
       theLastOpInList->next(tOp);
       theLastOpInList = tOp;
    } else {
       theLastOpInList = tOp;
       theFirstOpInList = tOp;
    }//if
    tOp->next(NULL);
1125
  } else {
pekka@mysql.com's avatar
pekka@mysql.com committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    // add before the given op
    if (theFirstOpInList == aNextOp) {
      theFirstOpInList = tOp;
    } else {
      NdbOperation* aLoopOp = theFirstOpInList;
      while (aLoopOp != NULL && aLoopOp->next() != aNextOp)
        aLoopOp = aLoopOp->next();
      assert(aLoopOp != NULL);
      aLoopOp->next(tOp);
    }
    tOp->next(aNextOp);
  }
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  if (tOp->init(tab, this) != -1) {
    return tOp;
  } else {
    theNdb->releaseOperation(tOp);
  }//if
  return NULL;
  
 getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
1148
}//NdbTransaction::getNdbOperation()
1149

1150
NdbOperation* NdbTransaction::getNdbOperation(const NdbDictionary::Table * table)
1151 1152 1153 1154 1155
{
  if (table)
    return getNdbOperation(& NdbTableImpl::getImpl(*table));
  else
    return NULL;
1156
}//NdbTransaction::getNdbOperation()
1157

1158 1159 1160 1161 1162 1163 1164
// NdbScanOperation
/*****************************************************************************
NdbScanOperation* getNdbScanOperation(const char* aTableName);

Return Value    Return a pointer to a NdbScanOperation object if getNdbScanOperation was succesful.
                Return NULL : In all other case. 	
Parameters:     aTableName : Name of the database table. 	
1165
Remark:         Get an operation from NdbScanOperation idlelist and get the NdbTransaction object 
1166 1167 1168 1169
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbOperation object.synchronous
******************************************************************************/
NdbScanOperation*
1170
NdbTransaction::getNdbScanOperation(const char* aTableName)
1171 1172 1173 1174 1175 1176
{
  if (theCommitStatus == Started){
    NdbTableImpl* tab = theNdb->theDictionary->getTable(aTableName);
    if (tab != 0){
      return getNdbScanOperation(tab);
    } else {
joreland@mysql.com's avatar
joreland@mysql.com committed
1177
      setOperationErrorCodeAbort(theNdb->theDictionary->m_error.code);
1178 1179 1180 1181 1182 1183
      return NULL;
    }//if
  } 
  
  setOperationErrorCodeAbort(4114);
  return NULL;
1184
}//NdbTransaction::getNdbScanOperation()
1185 1186

/*****************************************************************************
1187
NdbScanOperation* getNdbIndexScanOperation(const char* anIndexName, const char* aTableName);
1188

1189
Return Value    Return a pointer to a NdbIndexScanOperation object if getNdbIndexScanOperation was succesful.
1190 1191 1192
                Return NULL : In all other case. 	
Parameters:     anIndexName : Name of the index to use. 	
                aTableName : Name of the database table. 	
1193
Remark:         Get an operation from NdbIndexScanOperation idlelist and get the NdbTransaction object 
1194
		who was fetch by startTransaction pointing to this  operation  
1195
		getOperation will set the theTableId in the NdbIndexScanOperation object.synchronous
1196
******************************************************************************/
joreland@mysql.com's avatar
joreland@mysql.com committed
1197
NdbIndexScanOperation*
1198
NdbTransaction::getNdbIndexScanOperation(const char* anIndexName, 
joreland@mysql.com's avatar
joreland@mysql.com committed
1199
					const char* aTableName)
1200 1201 1202
{
  NdbIndexImpl* index = 
    theNdb->theDictionary->getIndex(anIndexName, aTableName);
pekka@mysql.com's avatar
pekka@mysql.com committed
1203
  if (index == 0)
1204 1205
  {
    setOperationErrorCodeAbort(theNdb->theDictionary->getNdbError().code);
pekka@mysql.com's avatar
pekka@mysql.com committed
1206
    return 0;
1207
  }
1208
  NdbTableImpl* table = theNdb->theDictionary->getTable(aTableName);
pekka@mysql.com's avatar
pekka@mysql.com committed
1209
  if (table == 0)
1210 1211
  {
    setOperationErrorCodeAbort(theNdb->theDictionary->getNdbError().code);
pekka@mysql.com's avatar
pekka@mysql.com committed
1212
    return 0;
1213
  }
1214 1215 1216 1217 1218

  return getNdbIndexScanOperation(index, table);
}

NdbIndexScanOperation*
1219
NdbTransaction::getNdbIndexScanOperation(const NdbIndexImpl* index,
joreland@mysql.com's avatar
joreland@mysql.com committed
1220
					const NdbTableImpl* table)
1221 1222
{
  if (theCommitStatus == Started){
1223
    const NdbTableImpl * indexTable = index->getIndexTable();
1224
    if (indexTable != 0){
1225
      NdbIndexScanOperation* tOp = getNdbScanOperation(indexTable);
joreland@mysql.com's avatar
joreland@mysql.com committed
1226 1227 1228
      if(tOp)
      {
	tOp->m_currentTable = table;
1229 1230
        // Mark that this really an NdbIndexScanOperation
        tOp->m_type = NdbOperation::OrderedIndexScan;
joreland@mysql.com's avatar
joreland@mysql.com committed
1231
      }
joreland@mysql.com's avatar
joreland@mysql.com committed
1232
      return tOp;
1233
    } else {
1234
      setOperationErrorCodeAbort(4271);
1235 1236 1237 1238 1239 1240
      return NULL;
    }//if
  } 
  
  setOperationErrorCodeAbort(4114);
  return NULL;
1241
}//NdbTransaction::getNdbIndexScanOperation()
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
NdbIndexScanOperation* 
NdbTransaction::getNdbIndexScanOperation(const NdbDictionary::Index * index)
{ 
  if (index)
  {
    const NdbDictionary::Table *table=
      theNdb->theDictionary->getTable(index->getTable());

    if (table)
      return getNdbIndexScanOperation(index, table);

    setOperationErrorCodeAbort(theNdb->theDictionary->getNdbError().code);
    return NULL;
  }
  setOperationErrorCodeAbort(4271);
  return NULL;
}

1261
NdbIndexScanOperation* 
1262
NdbTransaction::getNdbIndexScanOperation(const NdbDictionary::Index * index,
joreland@mysql.com's avatar
joreland@mysql.com committed
1263
					const NdbDictionary::Table * table)
1264 1265 1266 1267
{
  if (index && table)
    return getNdbIndexScanOperation(& NdbIndexImpl::getImpl(*index),
				    & NdbTableImpl::getImpl(*table));
1268 1269
  setOperationErrorCodeAbort(4271);
  return NULL;
1270
}//NdbTransaction::getNdbIndexScanOperation()
1271 1272 1273 1274

/*****************************************************************************
NdbScanOperation* getNdbScanOperation(int aTableId);

1275
Return Value    Return a pointer to a NdbScanOperation object if getNdbScanOperation was succesful.
1276 1277
                Return NULL: In all other case. 	
Parameters:     tableId : Id of the database table beeing deleted.
1278
Remark:         Get an operation from NdbScanOperation object idlelist and get the NdbTransaction 
1279
                object who was fetch by startTransaction pointing to this  operation 
1280
  	        getOperation will set the theTableId in the NdbScanOperation object, synchronous.
1281
*****************************************************************************/
joreland@mysql.com's avatar
joreland@mysql.com committed
1282
NdbIndexScanOperation*
1283
NdbTransaction::getNdbScanOperation(const NdbTableImpl * tab)
1284
{ 
joreland@mysql.com's avatar
joreland@mysql.com committed
1285
  NdbIndexScanOperation* tOp;
1286 1287 1288 1289 1290 1291
  
  tOp = theNdb->getScanOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
  
  if (tOp->init(tab, this) != -1) {
1292
    define_scan_op(tOp);
mskold@mysql.com's avatar
mskold@mysql.com committed
1293 1294
    // Mark that this NdbIndexScanOperation is used as NdbScanOperation
    tOp->m_type = NdbOperation::TableScan; 
1295 1296 1297 1298 1299 1300 1301 1302 1303
    return tOp;
  } else {
    theNdb->releaseScanOperation(tOp);
  }//if
  return NULL;

getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
1304
}//NdbTransaction::getNdbScanOperation()
1305

1306
void
1307
NdbTransaction::remove_list(NdbOperation*& list, NdbOperation* op){
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
  NdbOperation* tmp= list;
  if(tmp == op)
    list = op->next();
  else {
    while(tmp && tmp->next() != op) tmp = tmp->next();
    if(tmp)
      tmp->next(op->next());
  }
  op->next(NULL);
}

1319
void
1320
NdbTransaction::define_scan_op(NdbIndexScanOperation * tOp){
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
  // Link scan operation into list of cursor operations
  if (m_theLastScanOperation == NULL)
    m_theFirstScanOperation = m_theLastScanOperation = tOp;
  else {
    m_theLastScanOperation->next(tOp);
    m_theLastScanOperation = tOp;
  }
  tOp->next(NULL);
}

1331
NdbScanOperation* 
1332
NdbTransaction::getNdbScanOperation(const NdbDictionary::Table * table)
1333 1334 1335 1336 1337
{
  if (table)
    return getNdbScanOperation(& NdbTableImpl::getImpl(*table));
  else
    return NULL;
1338
}//NdbTransaction::getNdbScanOperation()
1339 1340 1341 1342 1343 1344 1345


// IndexOperation
/*****************************************************************************
NdbIndexOperation* getNdbIndexOperation(const char* anIndexName,
					const char* aTableName);

1346
Return Value    Return a pointer to a NdbOperation object if getNdbIndexOperation was succesful.
1347 1348
                Return NULL : In all other case. 	
Parameters:     aTableName : Name of the database table. 	
1349 1350 1351
Remark:         Get an operation from NdbIndexOperation idlelist and get the NdbTransaction object 
		who was fetch by startTransaction pointing to this operation  
		getOperation will set the theTableId in the NdbIndexOperation object.synchronous
1352 1353
******************************************************************************/
NdbIndexOperation*
1354
NdbTransaction::getNdbIndexOperation(const char* anIndexName, 
1355 1356 1357 1358
                                    const char* aTableName)
{
  if (theCommitStatus == Started) {
    NdbTableImpl * table = theNdb->theDictionary->getTable(aTableName);
1359 1360
    NdbIndexImpl * index;

1361 1362 1363 1364 1365 1366
    if (table == 0)
    {
      setOperationErrorCodeAbort(theNdb->theDictionary->getNdbError().code);
      return NULL;
    }

1367 1368 1369 1370
    if (table->m_frm.get_data())
    {
      // This unique index is defined from SQL level
      static const char* uniqueSuffix= "$unique";
1371 1372 1373
      BaseString uniqueIndexName(anIndexName);
      uniqueIndexName.append(uniqueSuffix);
      index = theNdb->theDictionary->getIndex(uniqueIndexName.c_str(),
1374 1375 1376 1377 1378
					      aTableName);      
    }
    else
      index = theNdb->theDictionary->getIndex(anIndexName,
					      aTableName);
1379 1380 1381 1382 1383 1384 1385 1386 1387
    if(table != 0 && index != 0){
      return getNdbIndexOperation(index, table);
    }
    
    if(index == 0){
      setOperationErrorCodeAbort(4243);
      return NULL;
    }

1388
    setOperationErrorCodeAbort(4243);
1389 1390 1391 1392 1393
    return NULL;
  } 
  
  setOperationErrorCodeAbort(4114);
  return 0;
1394
}//NdbTransaction::getNdbIndexOperation()
1395 1396 1397 1398 1399 1400 1401

/*****************************************************************************
NdbIndexOperation* getNdbIndexOperation(int anIndexId, int aTableId);

Return Value    Return a pointer to a NdbIndexOperation object if getNdbIndexOperation was succesful.
                Return NULL: In all other case. 	
Parameters:     tableId : Id of the database table beeing deleted.
1402
Remark:         Get an operation from NdbIndexOperation object idlelist and get the NdbTransaction 
1403 1404 1405 1406
                object who was fetch by startTransaction pointing to this  operation 
  	        getOperation will set the theTableId in the NdbIndexOperation object, synchronous.
*****************************************************************************/
NdbIndexOperation*
1407
NdbTransaction::getNdbIndexOperation(const NdbIndexImpl * anIndex, 
joreland@mysql.com's avatar
joreland@mysql.com committed
1408
				    const NdbTableImpl * aTable,
pekka@mysql.com's avatar
pekka@mysql.com committed
1409
                                    NdbOperation* aNextOp)
1410 1411 1412 1413 1414 1415
{ 
  NdbIndexOperation* tOp;
  
  tOp = theNdb->getIndexOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
pekka@mysql.com's avatar
pekka@mysql.com committed
1416 1417 1418 1419 1420 1421 1422 1423 1424
  if (aNextOp == NULL) {
    if (theLastOpInList != NULL) {
       theLastOpInList->next(tOp);
       theLastOpInList = tOp;
    } else {
       theLastOpInList = tOp;
       theFirstOpInList = tOp;
    }//if
    tOp->next(NULL);
1425
  } else {
pekka@mysql.com's avatar
pekka@mysql.com committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
    // add before the given op
    if (theFirstOpInList == aNextOp) {
      theFirstOpInList = tOp;
    } else {
      NdbOperation* aLoopOp = theFirstOpInList;
      while (aLoopOp != NULL && aLoopOp->next() != aNextOp)
        aLoopOp = aLoopOp->next();
      assert(aLoopOp != NULL);
      aLoopOp->next(tOp);
    }
    tOp->next(aNextOp);
  }
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
  if (tOp->indxInit(anIndex, aTable, this)!= -1) {
    return tOp;
  } else {
    theNdb->releaseOperation(tOp);
  }//if
  return NULL;
  
 getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
1448
}//NdbTransaction::getNdbIndexOperation()
1449

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
NdbIndexOperation* 
NdbTransaction::getNdbIndexOperation(const NdbDictionary::Index * index)
{ 
  if (index)
  {
    const NdbDictionary::Table *table=
      theNdb->theDictionary->getTable(index->getTable());

    if (table)
      return getNdbIndexOperation(index, table);

    setOperationErrorCodeAbort(theNdb->theDictionary->getNdbError().code);
    return NULL;
  }
  setOperationErrorCodeAbort(4271);
  return NULL;
}

1468
NdbIndexOperation* 
1469
NdbTransaction::getNdbIndexOperation(const NdbDictionary::Index * index,
joreland@mysql.com's avatar
joreland@mysql.com committed
1470
				    const NdbDictionary::Table * table)
1471 1472 1473 1474
{
  if (index && table)
    return getNdbIndexOperation(& NdbIndexImpl::getImpl(*index),
				& NdbTableImpl::getImpl(*table));
1475 1476 1477
  
  setOperationErrorCodeAbort(4271);
  return NULL;
1478
}//NdbTransaction::getNdbIndexOperation()
1479 1480


1481 1482 1483 1484 1485 1486 1487 1488 1489
/*******************************************************************************
int  receiveDIHNDBTAMPER(NdbApiSignal* aSignal)

Return Value:  Return 0 : receiveDIHNDBTAMPER was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        Sets theRestartGCI in the NDB object. 
*******************************************************************************/
int			
1490
NdbTransaction::receiveDIHNDBTAMPER(NdbApiSignal* aSignal)
1491 1492 1493 1494 1495 1496 1497 1498
{
  if (theStatus != Connecting) {
    return -1;
  } else {
    theNdb->RestartGCI((Uint32)aSignal->readData(2));
    theStatus = Connected;
  }//if
  return 0;  
1499
}//NdbTransaction::receiveDIHNDBTAMPER()
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

/*******************************************************************************
int  receiveTCSEIZECONF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCSEIZECONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        Sets TC Connect pointer at reception of TCSEIZECONF. 
*******************************************************************************/
int			
1510
NdbTransaction::receiveTCSEIZECONF(NdbApiSignal* aSignal)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
{
  if (theStatus != Connecting)
  {
    return -1;
  } else
  {
    theTCConPtr = (Uint32)aSignal->readData(2);
    theStatus = Connected;
  }
  return 0;
1521
}//NdbTransaction::receiveTCSEIZECONF()
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

/*******************************************************************************
int  receiveTCSEIZEREF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCSEIZEREF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        Sets TC Connect pointer. 
*******************************************************************************/
int			
1532
NdbTransaction::receiveTCSEIZEREF(NdbApiSignal* aSignal)
1533
{
1534
  DBUG_ENTER("NdbTransaction::receiveTCSEIZEREF");
1535 1536
  if (theStatus != Connecting)
  {
1537
    DBUG_RETURN(-1);
1538 1539 1540 1541
  } else
  {
    theStatus = ConnectFailure;
    theNdb->theError.code = aSignal->readData(2);
1542 1543 1544 1545
    DBUG_PRINT("info",("error code %d, %s",
		       theNdb->getNdbError().code,
		       theNdb->getNdbError().message));
    DBUG_RETURN(0);
1546
  }
1547
}//NdbTransaction::receiveTCSEIZEREF()
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

/*******************************************************************************
int  receiveTCRELEASECONF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCRELEASECONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:         DisConnect TC Connect pointer to NDBAPI. 
*******************************************************************************/
int			
1558
NdbTransaction::receiveTCRELEASECONF(NdbApiSignal* aSignal)
1559 1560 1561 1562 1563 1564 1565 1566 1567
{
  if (theStatus != DisConnecting)
  {
    return -1;
  } else
  {
    theStatus = NotConnected;
  }
  return 0;
1568
}//NdbTransaction::receiveTCRELEASECONF()
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

/*******************************************************************************
int  receiveTCRELEASEREF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCRELEASEREF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        DisConnect TC Connect pointer to NDBAPI Failure. 
*******************************************************************************/
int			
1579
NdbTransaction::receiveTCRELEASEREF(NdbApiSignal* aSignal)
1580 1581 1582 1583 1584 1585 1586 1587
{
  if (theStatus != DisConnecting) {
    return -1;
  } else {
    theStatus = ConnectFailure;
    theNdb->theError.code = aSignal->readData(2);
    return 0;
  }//if
1588
}//NdbTransaction::receiveTCRELEASEREF()
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598

/******************************************************************************
int  receiveTC_COMMITCONF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTC_COMMITCONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
******************************************************************************/
int			
1599
NdbTransaction::receiveTC_COMMITCONF(const TcCommitConf * commitConf)
1600
{ 
joreland@mysql.com's avatar
joreland@mysql.com committed
1601 1602 1603
  if(checkState_TransId(&commitConf->transId1)){
    theCommitStatus = Committed;
    theCompletionStatus = CompletedSuccess;
1604
    theGlobalCheckpointId = commitConf->gci;
1605 1606
    // theGlobalCheckpointId == 0 if NoOp transaction
    if (theGlobalCheckpointId)
1607
      *p_latest_trans_gci = theGlobalCheckpointId;
joreland@mysql.com's avatar
joreland@mysql.com committed
1608
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1609 1610 1611 1612
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1613
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
1614
  return -1;
1615
}//NdbTransaction::receiveTC_COMMITCONF()
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

/******************************************************************************
int  receiveTC_COMMITREF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTC_COMMITREF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
******************************************************************************/
int			
1626
NdbTransaction::receiveTC_COMMITREF(NdbApiSignal* aSignal)
1627
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1628 1629 1630 1631 1632
  const TcCommitRef * ref = CAST_CONSTPTR(TcCommitRef, aSignal->getDataPtr());
  if(checkState_TransId(&ref->transId1)){
    setOperationErrorCodeAbort(ref->errorCode);
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedFailure;
1633
    theReturnStatus = ReturnFailure;
joreland@mysql.com's avatar
joreland@mysql.com committed
1634
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1635 1636 1637 1638
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1639
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
1640

joreland@mysql.com's avatar
joreland@mysql.com committed
1641
  return -1;
1642
}//NdbTransaction::receiveTC_COMMITREF()
1643

joreland@mysql.com's avatar
joreland@mysql.com committed
1644
/******************************************************************************
1645 1646 1647 1648 1649 1650
int  receiveTCROLLBACKCONF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCROLLBACKCONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
joreland@mysql.com's avatar
joreland@mysql.com committed
1651
******************************************************************************/
1652
int			
1653
NdbTransaction::receiveTCROLLBACKCONF(NdbApiSignal* aSignal)
1654
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1655 1656 1657 1658
  if(checkState_TransId(aSignal->getDataPtr() + 1)){
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedSuccess;
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1659 1660 1661 1662
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1663
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
1664

joreland@mysql.com's avatar
joreland@mysql.com committed
1665
  return -1;
1666
}//NdbTransaction::receiveTCROLLBACKCONF()
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

/*******************************************************************************
int  receiveTCROLLBACKREF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCROLLBACKREF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
*******************************************************************************/
int			
1677
NdbTransaction::receiveTCROLLBACKREF(NdbApiSignal* aSignal)
1678
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1679 1680 1681 1682
  if(checkState_TransId(aSignal->getDataPtr() + 1)){
    setOperationErrorCodeAbort(aSignal->readData(4));
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedFailure;
1683
    theReturnStatus = ReturnFailure;
joreland@mysql.com's avatar
joreland@mysql.com committed
1684
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1685 1686 1687 1688
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1689
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
1690

joreland@mysql.com's avatar
joreland@mysql.com committed
1691
  return -1;
1692
}//NdbTransaction::receiveTCROLLBACKREF()
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703

/*****************************************************************************
int receiveTCROLLBACKREP( NdbApiSignal* aSignal)

Return Value:   Return 0 : send was succesful.
                Return -1: In all other case.   
Parameters:     aSignal: the signal object that contains the 
                TCROLLBACKREP signal from TC.
Remark:         Handles the reception of the ROLLBACKREP signal.
*****************************************************************************/
int
1704
NdbTransaction::receiveTCROLLBACKREP( NdbApiSignal* aSignal)
1705
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1706
  /****************************************************************************
1707 1708 1709
Check that we are expecting signals from this transaction and that it doesn't
belong to a transaction already completed. Simply ignore messages from other 
transactions.
joreland@mysql.com's avatar
joreland@mysql.com committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
  ****************************************************************************/
  if(checkState_TransId(aSignal->getDataPtr() + 1)){
    theError.code = aSignal->readData(4);// Override any previous errors

    /**********************************************************************/
    /*	A serious error has occured. This could be due to deadlock or */
    /*	lack of resources or simply a programming error in NDB. This  */
    /*	transaction will be aborted. Actually it has already been     */
    /*	and we only need to report completion and return with the     */
    /*	error code to the application.				      */
    /**********************************************************************/
    theCompletionStatus = CompletedFailure;
    theCommitStatus = Aborted;
1723
    theReturnStatus = ReturnFailure;
joreland@mysql.com's avatar
joreland@mysql.com committed
1724
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1725 1726 1727 1728
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
joreland@mysql.com's avatar
joreland@mysql.com committed
1729
  }
joreland@mysql.com's avatar
joreland@mysql.com committed
1730

joreland@mysql.com's avatar
joreland@mysql.com committed
1731
  return -1;
1732
}//NdbTransaction::receiveTCROLLBACKREP()
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

/*******************************************************************************
int  receiveTCKEYCONF(NdbApiSignal* aSignal, Uint32 long_short_ind);

Return Value:  Return 0 : receiveTCKEYCONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
*******************************************************************************/
int			
1743
NdbTransaction::receiveTCKEYCONF(const TcKeyConf * keyConf, Uint32 aDataLength)
1744
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1745
  NdbReceiver* tOp;
1746
  const Uint32 tTemp = keyConf->confInfo;
joreland@mysql.com's avatar
joreland@mysql.com committed
1747
  /***************************************************************************
1748 1749 1750
Check that we are expecting signals from this transaction and that it
doesn't belong to a transaction already completed. Simply ignore messages
from other transactions.
joreland@mysql.com's avatar
joreland@mysql.com committed
1751 1752
  ***************************************************************************/
  if(checkState_TransId(&keyConf->transId1)){
1753

joreland@mysql.com's avatar
joreland@mysql.com committed
1754 1755
    const Uint32 tNoOfOperations = TcKeyConf::getNoOfOperations(tTemp);
    const Uint32 tCommitFlag = TcKeyConf::getCommitFlag(tTemp);
1756 1757

    const Uint32* tPtr = (Uint32 *)&keyConf->operations[0];
joreland@mysql.com's avatar
joreland@mysql.com committed
1758
    Uint32 tNoComp = theNoOfOpCompleted;
1759
    for (Uint32 i = 0; i < tNoOfOperations ; i++) {
1760 1761
      tOp = theNdb->void2rec(theNdb->int2void(*tPtr++));
      const Uint32 tAttrInfoLen = *tPtr++;
joreland@mysql.com's avatar
joreland@mysql.com committed
1762
      if (tOp && tOp->checkMagicNumber()) {
1763
	Uint32 done = tOp->execTCOPCONF(tAttrInfoLen);
1764
	if(tAttrInfoLen > TcKeyConf::SimpleReadBit){
1765 1766 1767 1768 1769 1770
	  Uint32 node = tAttrInfoLen & (~TcKeyConf::SimpleReadBit);
	  NdbNodeBitmask::set(m_db_nodes, node);
	  if(NdbNodeBitmask::get(m_failed_db_nodes, node) && !done)
	  {
	    done = 1;
	    tOp->setErrorCode(4119);
1771
	    theCompletionStatus = CompletedFailure;
1772
	    theReturnStatus = NdbTransaction::ReturnFailure;
1773
	  }	    
1774
	}
1775
	tNoComp += done;
1776 1777 1778 1779 1780
      } else {
 	return -1;
      }//if
    }//for
    Uint32 tNoSent = theNoOfOpSent;
joreland@mysql.com's avatar
joreland@mysql.com committed
1781
    theNoOfOpCompleted = tNoComp;
1782 1783 1784 1785
    Uint32 tGCI    = keyConf->gci;
    if (tCommitFlag == 1) {
      theCommitStatus = Committed;
      theGlobalCheckpointId = tGCI;
1786 1787 1788 1789
      if (tGCI) // Read(dirty) only transaction doesnt get GCI
      {
	*p_latest_trans_gci = tGCI;
      }
1790 1791
    } else if ((tNoComp >= tNoSent) &&
               (theLastExecOpInList->theCommitIndicator == 1)){
1792 1793
      
      
1794 1795 1796 1797 1798 1799
/**********************************************************************/
// We sent the transaction with Commit flag set and received a CONF with
// no Commit flag set. This is clearly an anomaly.
/**********************************************************************/
      theError.code = 4011;
      theCompletionStatus = CompletedFailure;
1800
      theReturnStatus = NdbTransaction::ReturnFailure;
1801 1802 1803 1804 1805 1806 1807
      theCommitStatus = Aborted;
      return 0;
    }//if
    if (tNoComp >= tNoSent) {
      return 0;	// No more operations to wait for
    }//if
     // Not completed the reception yet.
joreland@mysql.com's avatar
joreland@mysql.com committed
1808 1809 1810 1811 1812 1813
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
  }
  
1814
  return -1;
1815
}//NdbTransaction::receiveTCKEYCONF()
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826

/*****************************************************************************
int receiveTCKEY_FAILCONF( NdbApiSignal* aSignal)

Return Value:   Return 0 : receive was completed.
                Return -1: In all other case.   
Parameters:     aSignal: the signal object that contains the 
                TCKEY_FAILCONF signal from TC.
Remark:         Handles the reception of the TCKEY_FAILCONF signal.
*****************************************************************************/
int
1827
NdbTransaction::receiveTCKEY_FAILCONF(const TcKeyFailConf * failConf)
1828 1829 1830
{
  NdbOperation*	tOp;
  /*
joreland@mysql.com's avatar
joreland@mysql.com committed
1831 1832 1833
    Check that we are expecting signals from this transaction and that it 
    doesn't belong  to a transaction already completed. Simply ignore 
    messages from other transactions.
1834
  */
joreland@mysql.com's avatar
joreland@mysql.com committed
1835
  if(checkState_TransId(&failConf->transId1)){
1836
    /*
joreland@mysql.com's avatar
joreland@mysql.com committed
1837 1838
      A node failure of the TC node occured. The transaction has
      been committed.
1839
    */
joreland@mysql.com's avatar
joreland@mysql.com committed
1840 1841 1842 1843 1844 1845 1846 1847 1848
    theCommitStatus = Committed;
    tOp = theFirstExecOpInList;
    while (tOp != NULL) {
      /*
       * Check if the transaction expected read values...
       * If it did some of them might have gotten lost even if we succeeded
       * in committing the transaction.
       */
      switch(tOp->theOperationType){
joreland@mysql.com's avatar
joreland@mysql.com committed
1849 1850 1851 1852
      case NdbOperation::UpdateRequest:
      case NdbOperation::InsertRequest:
      case NdbOperation::DeleteRequest:
      case NdbOperation::WriteRequest:
joreland@mysql.com's avatar
joreland@mysql.com committed
1853 1854
	tOp = tOp->next();
	break;
joreland@mysql.com's avatar
joreland@mysql.com committed
1855 1856 1857 1858
      case NdbOperation::ReadRequest:
      case NdbOperation::ReadExclusive:
      case NdbOperation::OpenScanRequest:
      case NdbOperation::OpenRangeScanRequest:
joreland@mysql.com's avatar
joreland@mysql.com committed
1859
	theCompletionStatus = CompletedFailure;
1860
	theReturnStatus = NdbTransaction::ReturnFailure;
joreland@mysql.com's avatar
joreland@mysql.com committed
1861 1862 1863
	setOperationErrorCodeAbort(4115);
	tOp = NULL;
	break;
1864 1865
      case NdbOperation::NotDefined:
      case NdbOperation::NotDefined2:
1866
	assert(false);
1867
	break;
joreland@mysql.com's avatar
joreland@mysql.com committed
1868 1869 1870 1871
      }//if
    }//while   
    theReleaseOnClose = true;
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1872 1873 1874 1875
  } else {
#ifdef VM_TRACE
    ndbout_c("Recevied TCKEY_FAILCONF wo/ operation");
#endif
joreland@mysql.com's avatar
joreland@mysql.com committed
1876 1877
  }
  return -1;
1878
}//NdbTransaction::receiveTCKEY_FAILCONF()
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

/*************************************************************************
int receiveTCKEY_FAILREF( NdbApiSignal* aSignal)

Return Value:   Return 0 : receive was completed.
                Return -1: In all other case.   
Parameters:     aSignal: the signal object that contains the 
                TCKEY_FAILREF signal from TC.
Remark:         Handles the reception of the TCKEY_FAILREF signal.
**************************************************************************/
int
1890
NdbTransaction::receiveTCKEY_FAILREF(NdbApiSignal* aSignal)
1891 1892
{
  /*
joreland@mysql.com's avatar
joreland@mysql.com committed
1893 1894 1895
    Check that we are expecting signals from this transaction and
    that it doesn't belong to a transaction already
    completed. Simply ignore messages from other transactions.
1896
  */
joreland@mysql.com's avatar
joreland@mysql.com committed
1897
  if(checkState_TransId(aSignal->getDataPtr()+1)){
1898
    /*
joreland@mysql.com's avatar
joreland@mysql.com committed
1899 1900
      We received an indication of that this transaction was aborted due to a
      node failure.
1901
    */
1902
    if (theSendStatus == NdbTransaction::sendTC_ROLLBACK) {
joreland@mysql.com's avatar
joreland@mysql.com committed
1903 1904 1905 1906
      /*
	We were in the process of sending a rollback anyways. We will
	report it as a success.
      */
1907
      theCompletionStatus = NdbTransaction::CompletedSuccess;
joreland@mysql.com's avatar
joreland@mysql.com committed
1908
    } else {
1909 1910
      theReturnStatus = NdbTransaction::ReturnFailure;
      theCompletionStatus = NdbTransaction::CompletedFailure;
joreland@mysql.com's avatar
joreland@mysql.com committed
1911 1912 1913
      theError.code = 4031;
    }//if
    theReleaseOnClose = true;
1914
    theCommitStatus = NdbTransaction::Aborted;
joreland@mysql.com's avatar
joreland@mysql.com committed
1915
    return 0;
joreland@mysql.com's avatar
joreland@mysql.com committed
1916 1917 1918 1919
  } else {
#ifdef VM_TRACE
    ndbout_c("Recevied TCKEY_FAILREF wo/ operation");
#endif
joreland@mysql.com's avatar
joreland@mysql.com committed
1920 1921
  }
  return -1;
1922
}//NdbTransaction::receiveTCKEY_FAILREF()
1923

joreland@mysql.com's avatar
joreland@mysql.com committed
1924
/******************************************************************************
1925 1926 1927 1928 1929 1930
int  receiveTCINDXCONF(NdbApiSignal* aSignal, Uint32 long_short_ind);

Return Value:  Return 0 : receiveTCINDXCONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
joreland@mysql.com's avatar
joreland@mysql.com committed
1931
******************************************************************************/
1932
int			
1933
NdbTransaction::receiveTCINDXCONF(const TcIndxConf * indxConf, 
joreland@mysql.com's avatar
joreland@mysql.com committed
1934
				 Uint32 aDataLength)
1935
{
joreland@mysql.com's avatar
joreland@mysql.com committed
1936 1937 1938 1939 1940
  if(checkState_TransId(&indxConf->transId1)){
    const Uint32 tTemp = indxConf->confInfo;
    const Uint32 tNoOfOperations = TcIndxConf::getNoOfOperations(tTemp);
    const Uint32 tCommitFlag = TcKeyConf::getCommitFlag(tTemp);
    
1941
    const Uint32* tPtr = (Uint32 *)&indxConf->operations[0];
joreland@mysql.com's avatar
joreland@mysql.com committed
1942
    Uint32 tNoComp = theNoOfOpCompleted;
1943
    for (Uint32 i = 0; i < tNoOfOperations ; i++) {
joreland@mysql.com's avatar
joreland@mysql.com committed
1944
      NdbReceiver* tOp = theNdb->void2rec(theNdb->int2void(*tPtr));
1945 1946 1947
      tPtr++;
      const Uint32 tAttrInfoLen = *tPtr;
      tPtr++;
joreland@mysql.com's avatar
joreland@mysql.com committed
1948 1949
      if (tOp && tOp->checkMagicNumber()) {
	tNoComp += tOp->execTCOPCONF(tAttrInfoLen);
1950 1951 1952 1953 1954 1955
      } else {
	return -1;
      }//if
    }//for
    Uint32 tNoSent = theNoOfOpSent;
    Uint32 tGCI    = indxConf->gci;
joreland@mysql.com's avatar
joreland@mysql.com committed
1956
    theNoOfOpCompleted = tNoComp;
1957 1958 1959
    if (tCommitFlag == 1) {
      theCommitStatus = Committed;
      theGlobalCheckpointId = tGCI;
1960 1961 1962 1963
      if (tGCI) // Read(dirty) only transaction doesnt get GCI
      {
	*p_latest_trans_gci = tGCI;
      }
1964 1965
    } else if ((tNoComp >= tNoSent) &&
               (theLastExecOpInList->theCommitIndicator == 1)){
jonas@perch.ndb.mysql.com's avatar
jonas@perch.ndb.mysql.com committed
1966

joreland@mysql.com's avatar
joreland@mysql.com committed
1967 1968 1969 1970
      /**********************************************************************/
      // We sent the transaction with Commit flag set and received a CONF with
      // no Commit flag set. This is clearly an anomaly.
      /**********************************************************************/
1971
      theError.code = 4011;
1972 1973 1974
      theCompletionStatus = NdbTransaction::CompletedFailure;
      theCommitStatus = NdbTransaction::Aborted;
      theReturnStatus = NdbTransaction::ReturnFailure;
1975 1976 1977 1978 1979 1980
      return 0;
    }//if
    if (tNoComp >= tNoSent) {
      return 0;	// No more operations to wait for
    }//if
     // Not completed the reception yet.
joreland@mysql.com's avatar
joreland@mysql.com committed
1981 1982 1983 1984 1985 1986
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
  }

1987
  return -1;
1988
}//NdbTransaction::receiveTCINDXCONF()
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998

/*******************************************************************************
int OpCompletedFailure();

Return Value:  Return 0 : OpCompleteSuccess was successful.
               Return -1: In all other case.
Parameters:    aErrorCode: The error code.
Remark:        An operation was completed with failure.
*******************************************************************************/
int 
1999
NdbTransaction::OpCompleteFailure(NdbOperation* op)
2000 2001 2002
{
  Uint32 tNoComp = theNoOfOpCompleted;
  Uint32 tNoSent = theNoOfOpSent;
2003

2004 2005
  tNoComp++;
  theNoOfOpCompleted = tNoComp;
2006 2007
  
  return (tNoComp == tNoSent) ? 0 : -1;
2008
}//NdbTransaction::OpCompleteFailure()
2009 2010 2011 2012 2013 2014 2015 2016 2017

/******************************************************************************
int OpCompleteSuccess();

Return Value:  Return 0 : OpCompleteSuccess was successful.
               Return -1: In all other case.  
Remark:        An operation was completed with success.
*******************************************************************************/
int 
2018
NdbTransaction::OpCompleteSuccess()
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
{
  Uint32 tNoComp = theNoOfOpCompleted;
  Uint32 tNoSent = theNoOfOpSent;
  tNoComp++;
  theNoOfOpCompleted = tNoComp;
  if (tNoComp == tNoSent) { // Last operation completed
    return 0;
  } else if (tNoComp < tNoSent) {
    return -1;	// Continue waiting for more signals
  } else {
    setOperationErrorCodeAbort(4113);	// Too many operations, 
                                        // stop waiting for more
2031 2032
    theCompletionStatus = NdbTransaction::CompletedFailure;
    theReturnStatus = NdbTransaction::ReturnFailure;
2033 2034
    return 0;
  }//if
2035
}//NdbTransaction::OpCompleteSuccess()
2036 2037 2038 2039 2040 2041 2042

/******************************************************************************
 int            getGCI();

Remark:		Get global checkpoint identity of the transaction
*******************************************************************************/
int
2043
NdbTransaction::getGCI()
2044
{
2045
  if (theCommitStatus == NdbTransaction::Committed) {
2046 2047 2048
    return theGlobalCheckpointId;
  }//if
  return 0;
2049
}//NdbTransaction::getGCI()
2050 2051 2052 2053 2054 2055 2056

/*******************************************************************************
Uint64 getTransactionId(void);

Remark:        Get the transaction identity. 
*******************************************************************************/
Uint64
2057
NdbTransaction::getTransactionId()
2058 2059
{
  return theTransactionId;
2060
}//NdbTransaction::getTransactionId()
2061

2062 2063
NdbTransaction::CommitStatusType
NdbTransaction::commitStatus()
2064 2065
{
  return theCommitStatus;
2066
}//NdbTransaction::commitStatus()
2067 2068

int 
2069
NdbTransaction::getNdbErrorLine()
2070 2071 2072 2073 2074
{
  return theErrorLine;
}

NdbOperation*
2075
NdbTransaction::getNdbErrorOperation()
2076 2077
{
  return theErrorOperation;
2078
}//NdbTransaction::getNdbErrorOperation()
2079 2080

const NdbOperation * 
2081
NdbTransaction::getNextCompletedOperation(const NdbOperation * current) const {
2082 2083 2084 2085 2086 2087 2088 2089
  if(current == 0)
    return theCompletedFirstOp;
  return current->theNext;
}

#ifdef VM_TRACE
#define CASE(x) case x: ndbout << " " << #x; break
void
2090
NdbTransaction::printState()
2091 2092 2093 2094 2095 2096 2097 2098 2099
{
  ndbout << "con=" << hex << this << dec;
  ndbout << " node=" << getConnectedNodeId();
  switch (theStatus) {
  CASE(NotConnected);
  CASE(Connecting);
  CASE(Connected);
  CASE(DisConnecting);
  CASE(ConnectFailure);
2100
  default: ndbout << (Uint32) theStatus;
2101 2102 2103 2104 2105 2106
  }
  switch (theListState) {
  CASE(NotInList);
  CASE(InPreparedList);
  CASE(InSendList);
  CASE(InCompletedList);
2107
  default: ndbout << (Uint32) theListState;
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
  }
  switch (theSendStatus) {
  CASE(NotInit);
  CASE(InitState);
  CASE(sendOperations);
  CASE(sendCompleted);
  CASE(sendCOMMITstate);
  CASE(sendABORT);
  CASE(sendABORTfail);
  CASE(sendTC_ROLLBACK);
  CASE(sendTC_COMMIT);
  CASE(sendTC_OP);
2120
  default: ndbout << (Uint32) theSendStatus;
2121 2122 2123 2124 2125 2126 2127
  }
  switch (theCommitStatus) {
  CASE(NotStarted);
  CASE(Started);
  CASE(Committed);
  CASE(Aborted);
  CASE(NeedAbort);
2128
  default: ndbout << (Uint32) theCommitStatus;
2129 2130 2131 2132 2133 2134
  }
  switch (theCompletionStatus) {
  CASE(NotCompleted);
  CASE(CompletedSuccess);
  CASE(CompletedFailure);
  CASE(DefinitionFailure);
2135
  default: ndbout << (Uint32) theCompletionStatus;
2136 2137 2138 2139 2140
  }
  ndbout << endl;
}
#undef CASE
#endif
2141 2142

int
2143
NdbTransaction::report_node_failure(Uint32 id){
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
  NdbNodeBitmask::set(m_failed_db_nodes, id);
  if(!NdbNodeBitmask::get(m_db_nodes, id))
  {
    return 0;
  }
  
  /**
   *   Arrived
   *   TCKEYCONF   TRANSIDAI
   * 1)   -           -
   * 2)   -           X
   * 3)   X           -
   * 4)   X           X
   */
  NdbOperation* tmp = theFirstExecOpInList;
  const Uint32 len = TcKeyConf::SimpleReadBit | id;
  Uint32 tNoComp = theNoOfOpCompleted;
  Uint32 tNoSent = theNoOfOpSent;
2162
  Uint32 count = 0;
2163 2164 2165 2166 2167
  while(tmp != 0)
  {
    if(tmp->theReceiver.m_expected_result_length == len && 
       tmp->theReceiver.m_received_result_length == 0)
    {
2168
      count++;
2169 2170 2171 2172
      tmp->theError.code = 4119;
    }
    tmp = tmp->next();
  }
2173
  tNoComp += count;
2174
  theNoOfOpCompleted = tNoComp;
2175
  if(count)
2176
  {
2177
    theReturnStatus = NdbTransaction::ReturnFailure;
2178 2179 2180
    if(tNoComp == tNoSent)
    {
      theError.code = 4119;
2181
      theCompletionStatus = NdbTransaction::CompletedFailure;    
2182 2183
      return 1;
    }
2184 2185 2186
  }
  return 0;
}