NdbConnection.cpp 65.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

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

17
#include <ndb_global.h>
unknown's avatar
unknown committed
18 19 20 21 22 23
#include <NdbOut.hpp>
#include <NdbConnection.hpp>
#include <NdbOperation.hpp>
#include <NdbScanOperation.hpp>
#include <NdbIndexScanOperation.hpp>
#include <NdbIndexOperation.hpp>
24 25 26
#include "NdbApiSignal.hpp"
#include "TransporterFacade.hpp"
#include "API.hpp"
unknown's avatar
unknown committed
27
#include "NdbBlob.hpp"
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
#include <ndb_limits.h>

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

/*****************************************************************************
NdbConnection( Ndb* aNdb );

Return Value:  None
Parameters:    aNdb: Pointers to the Ndb object 
Remark:        Creates a connection object. 
*****************************************************************************/
NdbConnection::NdbConnection( 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),
  theNoOfOpSent(0),
  theNoOfOpCompleted(0),
  theNoOfOpFetched(0),
  theMyRef(0),
  theTCConPtr(0),
  theTransactionId(0),
  theGlobalCheckpointId(0),
  theStatus(NotConnected),
  theCompletionStatus(NotCompleted), 
  theCommitStatus(NotStarted),
  theMagicNumber(0xFE11DC),
  theTransactionIsStarted(false),
  theDBnode(0),
  theReleaseOnClose(false),
unknown's avatar
unknown committed
72
  // Scan operations
73
  m_waitForReply(true),
unknown's avatar
unknown committed
74 75 76
  m_theFirstScanOperation(NULL),
  m_theLastScanOperation(NULL),
  m_firstExecutedScanOp(NULL),
77 78
  // Scan operations
  theScanningOp(NULL),
unknown's avatar
unknown committed
79
  theBuddyConPtr(0xFFFFFFFF),
unknown's avatar
unknown committed
80 81
  theBlobFlag(false),
  thePendingBlobOps(0)
82 83 84 85
{
  theListState = NotInList;
  theError.code = 0;
  theId = theNdb->theNdbObjectIdMap->map(this);
unknown's avatar
unknown committed
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 92 93 94 95 96 97 98 99
}//NdbConnection::NdbConnection()

/*****************************************************************************
~NdbConnection();

Remark:        Deletes the connection object. 
*****************************************************************************/
NdbConnection::~NdbConnection()
{
100
  DBUG_ENTER("NdbConnection::~NdbConnection");
101
  theNdb->theNdbObjectIdMap->unmap(theId, this);
102
  DBUG_VOID_RETURN;
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
}//NdbConnection::~NdbConnection()

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

Remark:         Initialise connection object for new transaction. 
*****************************************************************************/
void	
NdbConnection::init()
{
  theListState            = NotInList;
  theInUseState           = true;
  theTransactionIsStarted = false;
  theNext		  = NULL;

  theFirstOpInList	  = NULL;
  theLastOpInList	  = NULL;

  theScanningOp            = NULL;

  theFirstExecOpInList	  = NULL;
  theLastExecOpInList	  = NULL;

  theCompletedFirstOp	  = NULL;

  theGlobalCheckpointId   = 0;
  theCommitStatus         = Started;
  theCompletionStatus     = NotCompleted;
  m_abortOption           = AbortOnError;

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

  theReleaseOnClose       = false;
  theSimpleState          = true;
  theSendStatus           = InitState;
  theMagicNumber          = 0x37412619;
unknown's avatar
unknown committed
141
  // Scan operations
142
  m_waitForReply            = true;
unknown's avatar
unknown committed
143 144 145
  m_theFirstScanOperation = NULL;
  m_theLastScanOperation  = NULL;
  m_firstExecutedScanOp   = 0;
146
  theBuddyConPtr            = 0xFFFFFFFF;
unknown's avatar
unknown committed
147 148
  //
  theBlobFlag = false;
unknown's avatar
unknown committed
149
  thePendingBlobOps = 0;
150 151 152
}//NdbConnection::init()

/*****************************************************************************
153
setOperationErrorCode(int error);
154 155 156 157 158

Remark:        Sets an error code on the connection object from an 
               operation object. 
*****************************************************************************/
void
159
NdbConnection::setOperationErrorCode(int error)
160
{
161 162 163 164
  DBUG_ENTER("NdbConnection::setOperationErrorCode");
  setErrorCode(error);
  DBUG_VOID_RETURN;
}
165 166

/*****************************************************************************
167
setOperationErrorCodeAbort(int error);
168 169 170 171 172

Remark:        Sets an error code on the connection object from an 
               operation object. 
*****************************************************************************/
void
173
NdbConnection::setOperationErrorCodeAbort(int error)
174
{
175
  DBUG_ENTER("NdbConnection::setOperationErrorCodeAbort");
176 177 178 179 180 181 182
  if (theTransactionIsStarted == false) {
    theCommitStatus = Aborted;
  } else if ((m_abortOption == AbortOnError) && 
	     (theCommitStatus != Committed) &&
             (theCommitStatus != Aborted)) {
    theCommitStatus = NeedAbort;
  }//if
183 184 185
  setErrorCode(error);
  DBUG_VOID_RETURN;
}
186 187 188 189 190 191 192

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

Remark:        Sets an error indication on the connection object. 
*****************************************************************************/
void
193
NdbConnection::setErrorCode(int error)
194
{
195 196 197
  DBUG_ENTER("NdbConnection::setErrorCode");
  DBUG_PRINT("enter", ("error: %d, theError.code: %d", error, theError.code));

198
  if (theError.code == 0)
199 200 201
    theError.code = error;

  DBUG_VOID_RETURN;
202 203
}//NdbConnection::setErrorCode()

unknown's avatar
unknown committed
204 205
int
NdbConnection::restart(){
206
  DBUG_ENTER("NdbConnection::restart");
unknown's avatar
unknown committed
207 208 209 210
  if(theCompletionStatus == CompletedSuccess){
    releaseCompletedOperations();
    Uint64 tTransid = theNdb->theFirstTransId;
    theTransactionId = tTransid;
211
    if ((tTransid & 0xFFFFFFFF) == 0xFFFFFFFF) {
unknown's avatar
unknown committed
212 213 214 215
      theNdb->theFirstTransId = (tTransid >> 32) << 32;
    } else {
      theNdb->theFirstTransId = tTransid + 1;
    }
216
    theCommitStatus = Started;
unknown's avatar
unknown committed
217
    theCompletionStatus = NotCompleted;
218
    theTransactionIsStarted = false;
219
    DBUG_RETURN(0);
unknown's avatar
unknown committed
220
  }
221 222
  DBUG_PRINT("error",("theCompletionStatus != CompletedSuccess"));
  DBUG_RETURN(-1);
unknown's avatar
unknown committed
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
/*****************************************************************************
void handleExecuteCompletion(void);

Remark:        Handle time-out on a transaction object. 
*****************************************************************************/
void
NdbConnection::handleExecuteCompletion()
{
  
  if (theCompletionStatus == CompletedFailure) {
    NdbOperation* tOpTemp = theFirstExecOpInList;
    while (tOpTemp != NULL) {
/*****************************************************************************
 *	Ensure that all executing operations report failed for each 
 *      read attribute when failure occurs. 
 *      We do not want any operations to report both failure and 
 *      success on different read attributes.
 ****************************************************************************/
      tOpTemp->handleFailedAI_ElemLen();
      tOpTemp = tOpTemp->next();
    }//while
    theReturnStatus = ReturnFailure;
  }//if
  /***************************************************************************
   *	  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;
    theFirstExecOpInList = NULL;
    theLastExecOpInList = NULL;
  }//if
  theSendStatus = InitState;
  return;
}//NdbConnection::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 
NdbConnection::execute(ExecType aTypeOfExec, 
		       AbortOption abortOption,
		       int forceSend)
unknown's avatar
unknown committed
276
{
277 278 279 280
  DBUG_ENTER("NdbConnection::execute");
  DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d", 
		       aTypeOfExec, abortOption));

unknown's avatar
unknown committed
281
  if (! theBlobFlag)
282
    DBUG_RETURN(executeNoBlobs(aTypeOfExec, abortOption, forceSend));
unknown's avatar
unknown committed
283

unknown's avatar
unknown 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
   */
unknown's avatar
unknown committed
290 291 292 293

  ExecType tExecType;
  NdbOperation* tPrepOp;

unknown's avatar
unknown committed
294
  int ret = 0;
unknown's avatar
unknown committed
295 296 297 298
  do {
    tExecType = aTypeOfExec;
    tPrepOp = theFirstOpInList;
    while (tPrepOp != NULL) {
unknown's avatar
unknown committed
299 300 301 302 303 304 305 306 307 308 309 310 311
      if (tPrepOp->theError.code == 0) {
        bool batch = false;
        NdbBlob* tBlob = tPrepOp->theBlobList;
        while (tBlob != NULL) {
          if (tBlob->preExecute(tExecType, batch) == -1)
            ret = -1;
          tBlob = tBlob->theNext;
        }
        if (batch) {
          // blob asked to execute all up to here now
          tExecType = NoCommit;
          break;
        }
unknown's avatar
unknown committed
312 313 314 315
      }
      tPrepOp = tPrepOp->next();
    }
    // save rest of prepared ops if batch
unknown's avatar
unknown committed
316 317
    NdbOperation* tRestOp= 0;
    NdbOperation* tLastOp= 0;
unknown's avatar
unknown committed
318 319 320 321 322 323 324 325 326
    if (tPrepOp != NULL) {
      tRestOp = tPrepOp->next();
      tPrepOp->next(NULL);
      tLastOp = theLastOpInList;
      theLastOpInList = tPrepOp;
    }
    if (tExecType == Commit) {
      NdbOperation* tOp = theCompletedFirstOp;
      while (tOp != NULL) {
unknown's avatar
unknown committed
327 328 329 330 331 332 333
        if (tOp->theError.code == 0) {
          NdbBlob* tBlob = tOp->theBlobList;
          while (tBlob != NULL) {
            if (tBlob->preCommit() == -1)
              ret = -1;
            tBlob = tBlob->theNext;
          }
unknown's avatar
unknown committed
334 335 336 337 338
        }
        tOp = tOp->next();
      }
    }
    if (executeNoBlobs(tExecType, abortOption, forceSend) == -1)
unknown's avatar
unknown committed
339
        ret = -1;
unknown's avatar
unknown committed
340 341 342
    {
      NdbOperation* tOp = theCompletedFirstOp;
      while (tOp != NULL) {
unknown's avatar
unknown committed
343 344 345 346 347 348 349 350
        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;
            tBlob = tBlob->theNext;
          }
unknown's avatar
unknown committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364
        }
        tOp = tOp->next();
      }
    }
    // add saved prepared ops if batch
    if (tPrepOp != NULL && tRestOp != NULL) {
      if (theFirstOpInList == NULL)
        theFirstOpInList = tRestOp;
      else
        theLastOpInList->next(tRestOp);
      theLastOpInList = tLastOp;
    }
  } while (theFirstOpInList != NULL || tExecType != aTypeOfExec);

365
  DBUG_RETURN(ret);
unknown's avatar
unknown committed
366 367 368 369 370 371
}

int 
NdbConnection::executeNoBlobs(ExecType aTypeOfExec, 
                              AbortOption abortOption,
                              int forceSend)
372
{
373 374 375 376
  DBUG_ENTER("NdbConnection::executeNoBlobs");
  DBUG_PRINT("enter", ("aTypeOfExec: %d, abortOption: %d", 
		       aTypeOfExec, abortOption));

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
//------------------------------------------------------------------------
// 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 NdbConnectionCallBack-method will receive the return code of the
// transaction. The normal methods of reading error codes still apply.
//------------------------------------------------------------------------
  Ndb* tNdb = theNdb;

  m_waitForReply = false;
  executeAsynchPrepare(aTypeOfExec, NULL, NULL, abortOption);
  if (m_waitForReply){
    while (1) {
      int noOfComp = tNdb->sendPollNdb((3 * WAITFOR_RESPONSE_TIMEOUT),
                                       1, forceSend);
      if (noOfComp == 0) {
        /** 
         * This timeout situation can occur if NDB crashes.
         */
        ndbout << "This timeout should never occur, execute(..)" << endl;
        setOperationErrorCodeAbort(4012);  // Error code for "Cluster Failure"
399
        DBUG_RETURN(-1);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
      }//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) {
423
        DBUG_RETURN(-1);
424 425 426 427
      }//if
      break;
    }
  }
unknown's avatar
unknown committed
428
  thePendingBlobOps = 0;
429
  DBUG_RETURN(0);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
}//NdbConnection::execute()

/*****************************************************************************
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 
NdbConnection::executeAsynchPrepare( ExecType           aTypeOfExec,
                                     NdbAsynchCallback  aCallback,
                                     void*              anyObject,
                                     AbortOption abortOption)
{
453 454 455 456
  DBUG_ENTER("NdbConnection::executeAsynchPrepare");
  DBUG_PRINT("enter", ("aTypeOfExec: %d, aCallback: %x, anyObject: %x", 
		       aTypeOfExec, aCallback, anyObject));

457 458 459
  /**
   * Reset error.code on execute
   */
460 461
  if (theError.code != 0)
    DBUG_PRINT("enter", ("Resetting error %d on execute", theError.code));
462
  theError.code = 0;
unknown's avatar
unknown committed
463
  NdbScanOperation* tcOp = m_theFirstScanOperation;
464 465 466 467 468 469
  if (tcOp != 0){
    // Execute any cursor operations
    while (tcOp != NULL) {
      int tReturnCode;
      tReturnCode = tcOp->executeCursor(theDBnode);
      if (tReturnCode == -1) {
470
        DBUG_VOID_RETURN;
471
      }//if
unknown's avatar
unknown committed
472
      tcOp = (NdbScanOperation*)tcOp->next();
473
    } // while
unknown's avatar
unknown committed
474 475
    m_theLastScanOperation->next(m_firstExecutedScanOp);
    m_firstExecutedScanOp = m_theFirstScanOperation;
476 477 478
    // Discard cursor operations, since these are also
    // in the complete operations list we do not need
    // to release them.
unknown's avatar
unknown committed
479
    m_theFirstScanOperation = m_theLastScanOperation = NULL;
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
  }

  bool tTransactionIsStarted = theTransactionIsStarted;
  NdbOperation*	tLastOp = theLastOpInList;
  Ndb* tNdb = theNdb;
  CommitStatusType tCommitStatus = theCommitStatus;
  Uint32 tnoOfPreparedTransactions = tNdb->theNoOfPreparedTransactions;

  theReturnStatus     = ReturnSuccess;
  theCallbackFunction = aCallback;
  theCallbackObject   = anyObject;
  m_abortOption   = abortOption;
  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) {
unknown's avatar
unknown committed
506
      if (theTransactionIsStarted == false || theSimpleState) {
507 508 509 510 511 512 513 514
	theCommitStatus = Aborted;
	theSendStatus = sendCompleted;
      } else {
	theSendStatus = sendABORT;
      }
    } else {
      theSendStatus = sendABORTfail;
    }//if
515 516 517 518 519
    if (theCommitStatus == Aborted){
      DBUG_PRINT("exit", ("theCommitStatus: Aborted"));
      setErrorCode(4350);
    }
    DBUG_VOID_RETURN;
520 521 522 523 524 525 526 527 528 529 530
  }//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 {
unknown's avatar
unknown committed
531
      if (aTypeOfExec == Commit && !theSimpleState) {
532 533 534 535 536
	/**********************************************************************
	 *   A Transaction have been started and no more operations exist. 
	 *   We will use the commit method.
	 *********************************************************************/
        theSendStatus = sendCOMMITstate;
537
	DBUG_VOID_RETURN;
538 539 540 541 542 543 544 545 546 547 548
      } 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;
549
	DBUG_VOID_RETURN; // No Commit with no operations is OK
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
      }//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;
577
      DBUG_VOID_RETURN;
578 579 580 581 582 583 584 585 586 587 588 589
    }//if
  }

  NdbOperation* tOp = theFirstOpInList;
  theCompletionStatus = NotCompleted;
  while (tOp) {
    int tReturnCode;
    NdbOperation* tNextOp = tOp->next();

    tReturnCode = tOp->prepareSend(theTCConPtr, theTransactionId);
    if (tReturnCode == -1) {
      theSendStatus = sendABORTfail;
590
      DBUG_VOID_RETURN;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    }//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;
unknown's avatar
unknown committed
613 614
  NdbNodeBitmask::clear(m_db_nodes);
  NdbNodeBitmask::clear(m_failed_db_nodes);
615
  DBUG_VOID_RETURN;
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
}//NdbConnection::executeAsynchPrepare()

void NdbConnection::close()
{
  theNdb->closeTransaction(this);
}

int NdbConnection::refresh(){
  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 	
NdbConnection::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) == -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;
 
  TransporterFacade *tp = TransporterFacade::instance();
  tp->lock_mutex(); 
  const int res = tp->sendSignal(tSignal,theDBnode);
  tp->unlock_mutex(); 
  tNdb->releaseSignal(tSignal);

  if (res == -1){
    return -1;
  }    
  
  return 0;
}//NdbConnection::sendTC_HBREP()

/*****************************************************************************
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
NdbConnection::doSend()
{
684 685
  DBUG_ENTER("NdbConnection::doSend");

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  /*
  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);
708
    DBUG_RETURN(0);
709 710 711 712 713 714 715 716 717 718 719
  }//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) {
720
      DBUG_RETURN(0);
721 722 723 724 725
    }//if
    break;
  }//case
  case sendCOMMITstate:
    if (sendCOMMIT() == 0) {
726
      DBUG_RETURN(0);
727 728 729 730
    }//if
    break;
  case sendCompleted:
    theNdb->insert_completed_list(this); 
731
    DBUG_RETURN(0);
732
  default:
unknown's avatar
unknown committed
733 734
    ndbout << "Inconsistent theSendStatus = "
	   << (Uint32) theSendStatus << endl;
735 736 737 738 739 740 741
    abort();
    break;
  }//switch
  setOperationErrorCodeAbort(4002);
  theReleaseOnClose = true;
  theTransactionIsStarted = false;
  theCommitStatus = Aborted;
742
  DBUG_RETURN(-1);
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
}//NdbConnection::doSend()

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

Return Value:  Return -1 if send unsuccessful.  
Parameters :   None.
Remark:        Order NDB to rollback the transaction. 
**************************************************************************/
int 	
NdbConnection::sendROLLBACK()      // Send a TCROLLBACKREQ signal;
{
  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;
    TransporterFacade *tp = TransporterFacade::instance();
    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
}//NdbConnection::sendROLLBACK()

/***************************************************************************
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 	
NdbConnection::sendCOMMIT()    // Send a TC_COMMITREQ signal;
{
  NdbApiSignal tSignal(theNdb->theMyRef);
  Uint32 tTransId1, tTransId2;
  TransporterFacade *tp = TransporterFacade::instance(); 
  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
}//NdbConnection::sendCOMMIT()

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

Remark:         Release all operations.
******************************************************************************/
void 
NdbConnection::release(){
  releaseOperations();
  if ( (theTransactionIsStarted == true) &&
unknown's avatar
unknown committed
841 842 843 844 845 846
       ((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.
     ************************************************************************/
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
    execute(Rollback);
  }//if
  theMagicNumber = 0xFE11DC;
  theInUseState = false;
#ifdef VM_TRACE
  if (theListState != NotInList) {
    theNdb->printState("release %x", this);
    abort();
  }
#endif
}//NdbConnection::release()

void
NdbConnection::releaseOps(NdbOperation* tOp){
  while (tOp != NULL) {
    NdbOperation* tmp = tOp;
    tOp->release();
    tOp = tOp->next();
    theNdb->releaseOperation(tmp);
  }//while
}

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

Remark:         Release all operations.
******************************************************************************/
void 
NdbConnection::releaseOperations()
{
  // Release any open scans
unknown's avatar
unknown committed
878 879
  releaseScanOperations(m_theFirstScanOperation);
  releaseScanOperations(m_firstExecutedScanOp);
880 881 882 883 884 885 886 887 888 889 890
  
  releaseOps(theCompletedFirstOp);
  releaseOps(theFirstOpInList);
  releaseOps(theFirstExecOpInList);

  theCompletedFirstOp = NULL;
  theFirstOpInList = NULL;
  theFirstExecOpInList = NULL;
  theLastOpInList = NULL;
  theLastExecOpInList = NULL;
  theScanningOp = NULL;
unknown's avatar
unknown committed
891 892 893
  m_theFirstScanOperation = NULL;
  m_theLastScanOperation = NULL;
  m_firstExecutedScanOp = NULL;
894 895 896 897 898 899 900 901 902 903
}//NdbConnection::releaseOperations()

void 
NdbConnection::releaseCompletedOperations()
{
  releaseOps(theCompletedFirstOp);
  theCompletedFirstOp = NULL;
}//NdbConnection::releaseOperations()

/******************************************************************************
unknown's avatar
unknown committed
904
void releaseScanOperations();
905 906 907 908 909

Remark:         Release all cursor operations. 
                (NdbScanOperation and NdbIndexOperation)
******************************************************************************/
void 
unknown's avatar
unknown committed
910
NdbConnection::releaseScanOperations(NdbIndexScanOperation* cursorOp)
911 912
{
  while(cursorOp != 0){
unknown's avatar
unknown committed
913
    NdbIndexScanOperation* next = (NdbIndexScanOperation*)cursorOp->next();
914
    cursorOp->release();
unknown's avatar
unknown committed
915
    theNdb->releaseScanOperation(cursorOp);
916 917
    cursorOp = next;
  }
unknown's avatar
unknown committed
918
}//NdbConnection::releaseScanOperations()
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

/*****************************************************************************
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 
                NdbConnection object 
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbOperation object.
                synchronous
******************************************************************************/
NdbOperation*
NdbConnection::getNdbOperation(const char* aTableName)
{
  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;
}//NdbConnection::getNdbOperation()

/*****************************************************************************
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 
                get the NdbConnection object who was fetch by 
                startTransaction pointing to this operation 
  	        getOperation will set the theTableId in the NdbOperation 
                object, synchronous.
*****************************************************************************/
NdbOperation*
unknown's avatar
unknown committed
965
NdbConnection::getNdbOperation(const NdbTableImpl * tab, NdbOperation* aNextOp)
966 967 968 969 970 971 972 973 974 975 976
{ 
  NdbOperation* tOp;

  if (theScanningOp != NULL){
    setErrorCode(4607);
    return NULL;
  }
  
  tOp = theNdb->getOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
unknown's avatar
unknown committed
977 978 979 980 981 982 983 984 985
  if (aNextOp == NULL) {
    if (theLastOpInList != NULL) {
       theLastOpInList->next(tOp);
       theLastOpInList = tOp;
    } else {
       theLastOpInList = tOp;
       theFirstOpInList = tOp;
    }//if
    tOp->next(NULL);
986
  } else {
unknown's avatar
unknown committed
987 988 989 990 991 992 993 994 995 996 997 998
    // 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);
  }
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  if (tOp->init(tab, this) != -1) {
    return tOp;
  } else {
    theNdb->releaseOperation(tOp);
  }//if
  return NULL;
  
 getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
}//NdbConnection::getNdbOperation()

unknown's avatar
unknown committed
1011
NdbOperation* NdbConnection::getNdbOperation(const NdbDictionary::Table * table)
1012 1013 1014 1015 1016 1017 1018
{
  if (table)
    return getNdbOperation(& NdbTableImpl::getImpl(*table));
  else
    return NULL;
}//NdbConnection::getNdbOperation()

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
// 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. 	
Remark:         Get an operation from NdbScanOperation idlelist and get the NdbConnection object 
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbOperation object.synchronous
******************************************************************************/
NdbScanOperation*
NdbConnection::getNdbScanOperation(const char* aTableName)
{
  if (theCommitStatus == Started){
    NdbTableImpl* tab = theNdb->theDictionary->getTable(aTableName);
    if (tab != 0){
      return getNdbScanOperation(tab);
    } else {
unknown's avatar
unknown committed
1038
      setOperationErrorCodeAbort(theNdb->theDictionary->m_error.code);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
      return NULL;
    }//if
  } 
  
  setOperationErrorCodeAbort(4114);
  return NULL;
}//NdbConnection::getNdbScanOperation()

/*****************************************************************************
NdbScanOperation* getNdbScanOperation(const char* anIndexName, const char* aTableName);

Return Value    Return a pointer to a NdbScanOperation object if getNdbScanOperation was succesful.
                Return NULL : In all other case. 	
Parameters:     anIndexName : Name of the index to use. 	
                aTableName : Name of the database table. 	
Remark:         Get an operation from NdbScanOperation idlelist and get the NdbConnection object 
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbOperation object.synchronous
******************************************************************************/
unknown's avatar
unknown committed
1058 1059 1060
NdbIndexScanOperation*
NdbConnection::getNdbIndexScanOperation(const char* anIndexName, 
					const char* aTableName)
1061 1062 1063 1064 1065 1066 1067 1068 1069
{
  NdbIndexImpl* index = 
    theNdb->theDictionary->getIndex(anIndexName, aTableName);
  NdbTableImpl* table = theNdb->theDictionary->getTable(aTableName);

  return getNdbIndexScanOperation(index, table);
}

NdbIndexScanOperation*
unknown's avatar
unknown committed
1070 1071
NdbConnection::getNdbIndexScanOperation(const NdbIndexImpl* index,
					const NdbTableImpl* table)
1072 1073
{
  if (theCommitStatus == Started){
1074
    const NdbTableImpl * indexTable = index->getIndexTable();
1075
    if (indexTable != 0){
1076 1077 1078
      NdbIndexScanOperation* tOp = 
	getNdbScanOperation((NdbTableImpl *) indexTable);
      tOp->m_currentTable = table;
unknown's avatar
unknown committed
1079 1080
      if(tOp) tOp->m_cursor_type = NdbScanOperation::IndexCursor;
      return tOp;
1081 1082 1083 1084 1085 1086 1087 1088
    } else {
      setOperationErrorCodeAbort(theNdb->theError.code);
      return NULL;
    }//if
  } 
  
  setOperationErrorCodeAbort(4114);
  return NULL;
1089 1090 1091
}//NdbConnection::getNdbIndexScanOperation()

NdbIndexScanOperation* 
unknown's avatar
unknown committed
1092 1093
NdbConnection::getNdbIndexScanOperation(const NdbDictionary::Index * index,
					const NdbDictionary::Table * table)
1094 1095 1096 1097 1098 1099 1100
{
  if (index && table)
    return getNdbIndexScanOperation(& NdbIndexImpl::getImpl(*index),
				    & NdbTableImpl::getImpl(*table));
  else
    return NULL;
}//NdbConnection::getNdbIndexScanOperation()
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

/*****************************************************************************
NdbScanOperation* getNdbScanOperation(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 NdbScanOperation object idlelist and get the NdbConnection 
                object who was fetch by startTransaction pointing to this  operation 
  	        getOperation will set the theTableId in the NdbOperation object, synchronous.
*****************************************************************************/
unknown's avatar
unknown committed
1112
NdbIndexScanOperation*
unknown's avatar
unknown committed
1113
NdbConnection::getNdbScanOperation(const NdbTableImpl * tab)
1114
{ 
unknown's avatar
unknown committed
1115
  NdbIndexScanOperation* tOp;
1116 1117 1118 1119 1120 1121
  
  tOp = theNdb->getScanOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
  
  if (tOp->init(tab, this) != -1) {
1122
    define_scan_op(tOp);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    return tOp;
  } else {
    theNdb->releaseScanOperation(tOp);
  }//if
  return NULL;

getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
}//NdbConnection::getNdbScanOperation()

unknown's avatar
unknown committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
void
NdbConnection::remove_list(NdbOperation*& list, NdbOperation* op){
  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);
}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
void
NdbConnection::define_scan_op(NdbIndexScanOperation * tOp){
  // 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);
}

1159
NdbScanOperation* 
unknown's avatar
unknown committed
1160
NdbConnection::getNdbScanOperation(const NdbDictionary::Table * table)
1161 1162 1163 1164 1165 1166
{
  if (table)
    return getNdbScanOperation(& NdbTableImpl::getImpl(*table));
  else
    return NULL;
}//NdbConnection::getNdbScanOperation()
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217


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

Return Value    Return a pointer to a NdbOperation object if getNdbScanOperation was succesful.
                Return NULL : In all other case. 	
Parameters:     aTableName : Name of the database table. 	
Remark:         Get an operation from NdbScanOperation idlelist and get the NdbConnection object 
		who was fetch by startTransaction pointing to this  operation  
		getOperation will set the theTableId in the NdbScanOperation object.synchronous
******************************************************************************/
NdbIndexOperation*
NdbConnection::getNdbIndexOperation(const char* anIndexName, 
                                    const char* aTableName)
{
  if (theCommitStatus == Started) {
    NdbTableImpl * table = theNdb->theDictionary->getTable(aTableName);
    NdbIndexImpl * index = theNdb->theDictionary->getIndex(anIndexName,
							   aTableName);
    if(table != 0 && index != 0){
      return getNdbIndexOperation(index, table);
    }
    
    if(index == 0){
      setOperationErrorCodeAbort(4243);
      return NULL;
    }

    // table == 0
    setOperationErrorCodeAbort(theNdb->theError.code);
    return NULL;
  } 
  
  setOperationErrorCodeAbort(4114);
  return 0;
}//NdbConnection::getNdbIndexOperation()

/*****************************************************************************
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.
Remark:         Get an operation from NdbIndexOperation object idlelist and get the NdbConnection 
                object who was fetch by startTransaction pointing to this  operation 
  	        getOperation will set the theTableId in the NdbIndexOperation object, synchronous.
*****************************************************************************/
NdbIndexOperation*
unknown's avatar
unknown committed
1218 1219
NdbConnection::getNdbIndexOperation(const NdbIndexImpl * anIndex, 
				    const NdbTableImpl * aTable,
unknown's avatar
unknown committed
1220
                                    NdbOperation* aNextOp)
1221 1222 1223 1224 1225 1226
{ 
  NdbIndexOperation* tOp;
  
  tOp = theNdb->getIndexOperation();
  if (tOp == NULL)
    goto getNdbOp_error1;
unknown's avatar
unknown committed
1227 1228 1229 1230 1231 1232 1233 1234 1235
  if (aNextOp == NULL) {
    if (theLastOpInList != NULL) {
       theLastOpInList->next(tOp);
       theLastOpInList = tOp;
    } else {
       theLastOpInList = tOp;
       theFirstOpInList = tOp;
    }//if
    tOp->next(NULL);
1236
  } else {
unknown's avatar
unknown committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    // 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);
  }
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
  if (tOp->indxInit(anIndex, aTable, this)!= -1) {
    return tOp;
  } else {
    theNdb->releaseOperation(tOp);
  }//if
  return NULL;
  
 getNdbOp_error1:
  setOperationErrorCodeAbort(4000);
  return NULL;
}//NdbConnection::getNdbIndexOperation()

1261
NdbIndexOperation* 
unknown's avatar
unknown committed
1262 1263
NdbConnection::getNdbIndexOperation(const NdbDictionary::Index * index,
				    const NdbDictionary::Table * table)
1264 1265 1266 1267 1268 1269 1270 1271 1272
{
  if (index && table)
    return getNdbIndexOperation(& NdbIndexImpl::getImpl(*index),
				& NdbTableImpl::getImpl(*table));
  else
    return NULL;
}//NdbConnection::getNdbIndexOperation()


1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
/*******************************************************************************
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			
NdbConnection::receiveDIHNDBTAMPER(NdbApiSignal* aSignal)
{
  if (theStatus != Connecting) {
    return -1;
  } else {
    theNdb->RestartGCI((Uint32)aSignal->readData(2));
    theStatus = Connected;
  }//if
  return 0;  
}//NdbConnection::receiveDIHNDBTAMPER()

/*******************************************************************************
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			
NdbConnection::receiveTCSEIZECONF(NdbApiSignal* aSignal)
{
  if (theStatus != Connecting)
  {
    return -1;
  } else
  {
    theTCConPtr = (Uint32)aSignal->readData(2);
    theStatus = Connected;
  }
  return 0;
}//NdbConnection::receiveTCSEIZECONF()

/*******************************************************************************
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			
NdbConnection::receiveTCSEIZEREF(NdbApiSignal* aSignal)
{
  if (theStatus != Connecting)
  {
    return -1;
  } else
  {
    theStatus = ConnectFailure;
    theNdb->theError.code = aSignal->readData(2);
    return 0;
  }
}//NdbConnection::receiveTCSEIZEREF()

/*******************************************************************************
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			
NdbConnection::receiveTCRELEASECONF(NdbApiSignal* aSignal)
{
  if (theStatus != DisConnecting)
  {
    return -1;
  } else
  {
    theStatus = NotConnected;
  }
  return 0;
}//NdbConnection::receiveTCRELEASECONF()

/*******************************************************************************
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			
NdbConnection::receiveTCRELEASEREF(NdbApiSignal* aSignal)
{
  if (theStatus != DisConnecting) {
    return -1;
  } else {
    theStatus = ConnectFailure;
    theNdb->theError.code = aSignal->readData(2);
    return 0;
  }//if
}//NdbConnection::receiveTCRELEASEREF()

/******************************************************************************
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			
NdbConnection::receiveTC_COMMITCONF(const TcCommitConf * commitConf)
{ 
unknown's avatar
unknown committed
1389 1390 1391 1392
  if(checkState_TransId(&commitConf->transId1)){
    theCommitStatus = Committed;
    theCompletionStatus = CompletedSuccess;
    return 0;
unknown's avatar
unknown committed
1393 1394 1395 1396
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1397
  }
unknown's avatar
unknown committed
1398
  return -1;
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
}//NdbConnection::receiveTC_COMMITCONF()

/******************************************************************************
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			
NdbConnection::receiveTC_COMMITREF(NdbApiSignal* aSignal)
{
unknown's avatar
unknown committed
1412 1413 1414 1415 1416 1417
  const TcCommitRef * ref = CAST_CONSTPTR(TcCommitRef, aSignal->getDataPtr());
  if(checkState_TransId(&ref->transId1)){
    setOperationErrorCodeAbort(ref->errorCode);
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedFailure;
    return 0;
unknown's avatar
unknown committed
1418 1419 1420 1421
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1422
  }
unknown's avatar
unknown committed
1423

unknown's avatar
unknown committed
1424
  return -1;
1425 1426
}//NdbConnection::receiveTC_COMMITREF()

unknown's avatar
unknown committed
1427
/******************************************************************************
1428 1429 1430 1431 1432 1433
int  receiveTCROLLBACKCONF(NdbApiSignal* aSignal);

Return Value:  Return 0 : receiveTCROLLBACKCONF was successful.
               Return -1: In all other case.
Parameters:    aSignal: The signal object pointer.
Remark:        
unknown's avatar
unknown committed
1434
******************************************************************************/
1435 1436 1437
int			
NdbConnection::receiveTCROLLBACKCONF(NdbApiSignal* aSignal)
{
unknown's avatar
unknown committed
1438 1439 1440 1441
  if(checkState_TransId(aSignal->getDataPtr() + 1)){
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedSuccess;
    return 0;
unknown's avatar
unknown committed
1442 1443 1444 1445
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1446
  }
unknown's avatar
unknown committed
1447

unknown's avatar
unknown committed
1448
  return -1;
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
}//NdbConnection::receiveTCROLLBACKCONF()

/*******************************************************************************
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			
NdbConnection::receiveTCROLLBACKREF(NdbApiSignal* aSignal)
{
unknown's avatar
unknown committed
1462 1463 1464 1465 1466
  if(checkState_TransId(aSignal->getDataPtr() + 1)){
    setOperationErrorCodeAbort(aSignal->readData(4));
    theCommitStatus = Aborted;
    theCompletionStatus = CompletedFailure;
    return 0;
unknown's avatar
unknown committed
1467 1468 1469 1470
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
1471
  }
unknown's avatar
unknown committed
1472

unknown's avatar
unknown committed
1473
  return -1;
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
}//NdbConnection::receiveTCROLLBACKREF()

/*****************************************************************************
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
NdbConnection::receiveTCROLLBACKREP( NdbApiSignal* aSignal)
{
unknown's avatar
unknown committed
1488
  /****************************************************************************
1489 1490 1491
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.
unknown's avatar
unknown committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
  ****************************************************************************/
  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;
    return 0;
unknown's avatar
unknown committed
1506 1507 1508 1509
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
unknown's avatar
unknown committed
1510
  }
unknown's avatar
unknown committed
1511

unknown's avatar
unknown committed
1512
  return -1;
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
}//NdbConnection::receiveTCROLLBACKREP()

/*******************************************************************************
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			
NdbConnection::receiveTCKEYCONF(const TcKeyConf * keyConf, Uint32 aDataLength)
{
unknown's avatar
unknown committed
1526
  NdbReceiver* tOp;
1527
  const Uint32 tTemp = keyConf->confInfo;
unknown's avatar
unknown committed
1528
  /***************************************************************************
1529 1530 1531
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.
unknown's avatar
unknown committed
1532 1533
  ***************************************************************************/
  if(checkState_TransId(&keyConf->transId1)){
1534

unknown's avatar
unknown committed
1535 1536
    const Uint32 tNoOfOperations = TcKeyConf::getNoOfOperations(tTemp);
    const Uint32 tCommitFlag = TcKeyConf::getCommitFlag(tTemp);
1537 1538

    const Uint32* tPtr = (Uint32 *)&keyConf->operations[0];
unknown's avatar
unknown committed
1539
    Uint32 tNoComp = theNoOfOpCompleted;
1540
    for (Uint32 i = 0; i < tNoOfOperations ; i++) {
1541 1542
      tOp = theNdb->void2rec(theNdb->int2void(*tPtr++));
      const Uint32 tAttrInfoLen = *tPtr++;
unknown's avatar
unknown committed
1543
      if (tOp && tOp->checkMagicNumber()) {
1544
	Uint32 done = tOp->execTCOPCONF(tAttrInfoLen);
unknown's avatar
unknown committed
1545
	if(tAttrInfoLen > TcKeyConf::SimpleReadBit){
1546 1547 1548 1549 1550 1551
	  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);
1552
	    theCompletionStatus = CompletedFailure;
1553
	  }	    
unknown's avatar
unknown committed
1554
	}
1555
	tNoComp += done;
1556 1557 1558 1559 1560
      } else {
 	return -1;
      }//if
    }//for
    Uint32 tNoSent = theNoOfOpSent;
unknown's avatar
unknown committed
1561
    theNoOfOpCompleted = tNoComp;
1562 1563 1564 1565 1566 1567
    Uint32 tGCI    = keyConf->gci;
    if (tCommitFlag == 1) {
      theCommitStatus = Committed;
      theGlobalCheckpointId = tGCI;
    } else if ((tNoComp >= tNoSent) &&
               (theLastExecOpInList->theCommitIndicator == 1)){
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578


      if (m_abortOption == IgnoreError && theError.code != 0){
	/**
	 * There's always a TCKEYCONF when using IgnoreError
	 */
#ifdef VM_TRACE
	ndbout_c("Not completing transaction 2");
#endif
	return -1;
      }
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
/**********************************************************************/
// 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;
      theCommitStatus = Aborted;
      return 0;
    }//if
    if (tNoComp >= tNoSent) {
      return 0;	// No more operations to wait for
    }//if
     // Not completed the reception yet.
unknown's avatar
unknown committed
1592 1593 1594 1595 1596 1597
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
  }
  
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
  return -1;
}//NdbConnection::receiveTCKEYCONF()

/*****************************************************************************
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
NdbConnection::receiveTCKEY_FAILCONF(const TcKeyFailConf * failConf)
{
  NdbOperation*	tOp;
  /*
unknown's avatar
unknown committed
1615 1616 1617
    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.
1618
  */
unknown's avatar
unknown committed
1619
  if(checkState_TransId(&failConf->transId1)){
1620
    /*
unknown's avatar
unknown committed
1621 1622
      A node failure of the TC node occured. The transaction has
      been committed.
1623
    */
unknown's avatar
unknown committed
1624 1625 1626 1627 1628 1629 1630 1631 1632
    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){
unknown's avatar
unknown committed
1633 1634 1635 1636
      case NdbOperation::UpdateRequest:
      case NdbOperation::InsertRequest:
      case NdbOperation::DeleteRequest:
      case NdbOperation::WriteRequest:
unknown's avatar
unknown committed
1637 1638
	tOp = tOp->next();
	break;
unknown's avatar
unknown committed
1639 1640 1641 1642
      case NdbOperation::ReadRequest:
      case NdbOperation::ReadExclusive:
      case NdbOperation::OpenScanRequest:
      case NdbOperation::OpenRangeScanRequest:
unknown's avatar
unknown committed
1643 1644 1645 1646
	theCompletionStatus = CompletedFailure;
	setOperationErrorCodeAbort(4115);
	tOp = NULL;
	break;
unknown's avatar
unknown committed
1647 1648
      case NdbOperation::NotDefined:
      case NdbOperation::NotDefined2:
1649
	assert(false);
unknown's avatar
unknown committed
1650
	break;
unknown's avatar
unknown committed
1651 1652 1653 1654
      }//if
    }//while   
    theReleaseOnClose = true;
    return 0;
unknown's avatar
unknown committed
1655 1656 1657 1658
  } else {
#ifdef VM_TRACE
    ndbout_c("Recevied TCKEY_FAILCONF wo/ operation");
#endif
unknown's avatar
unknown committed
1659 1660
  }
  return -1;
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
}//NdbConnection::receiveTCKEY_FAILCONF()

/*************************************************************************
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
NdbConnection::receiveTCKEY_FAILREF(NdbApiSignal* aSignal)
{
  /*
unknown's avatar
unknown committed
1676 1677 1678
    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.
1679
  */
unknown's avatar
unknown committed
1680
  if(checkState_TransId(aSignal->getDataPtr()+1)){
1681
    /*
unknown's avatar
unknown committed
1682 1683
      We received an indication of that this transaction was aborted due to a
      node failure.
1684
    */
unknown's avatar
unknown committed
1685
    if (theSendStatus == NdbConnection::sendTC_ROLLBACK) {
unknown's avatar
unknown committed
1686 1687 1688 1689
      /*
	We were in the process of sending a rollback anyways. We will
	report it as a success.
      */
unknown's avatar
unknown committed
1690
      theCompletionStatus = NdbConnection::CompletedSuccess;
unknown's avatar
unknown committed
1691
    } else {
unknown's avatar
unknown committed
1692
      theCompletionStatus = NdbConnection::CompletedFailure;
unknown's avatar
unknown committed
1693 1694 1695
      theError.code = 4031;
    }//if
    theReleaseOnClose = true;
unknown's avatar
unknown committed
1696
    theCommitStatus = NdbConnection::Aborted;
unknown's avatar
unknown committed
1697
    return 0;
unknown's avatar
unknown committed
1698 1699 1700 1701
  } else {
#ifdef VM_TRACE
    ndbout_c("Recevied TCKEY_FAILREF wo/ operation");
#endif
unknown's avatar
unknown committed
1702 1703
  }
  return -1;
1704 1705
}//NdbConnection::receiveTCKEY_FAILREF()

unknown's avatar
unknown committed
1706
/******************************************************************************
1707 1708 1709 1710 1711 1712
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:        
unknown's avatar
unknown committed
1713
******************************************************************************/
1714
int			
unknown's avatar
unknown committed
1715 1716
NdbConnection::receiveTCINDXCONF(const TcIndxConf * indxConf, 
				 Uint32 aDataLength)
1717
{
unknown's avatar
unknown committed
1718 1719 1720 1721 1722
  if(checkState_TransId(&indxConf->transId1)){
    const Uint32 tTemp = indxConf->confInfo;
    const Uint32 tNoOfOperations = TcIndxConf::getNoOfOperations(tTemp);
    const Uint32 tCommitFlag = TcKeyConf::getCommitFlag(tTemp);
    
1723
    const Uint32* tPtr = (Uint32 *)&indxConf->operations[0];
unknown's avatar
unknown committed
1724
    Uint32 tNoComp = theNoOfOpCompleted;
1725
    for (Uint32 i = 0; i < tNoOfOperations ; i++) {
unknown's avatar
unknown committed
1726
      NdbReceiver* tOp = theNdb->void2rec(theNdb->int2void(*tPtr));
1727 1728 1729
      tPtr++;
      const Uint32 tAttrInfoLen = *tPtr;
      tPtr++;
unknown's avatar
unknown committed
1730 1731
      if (tOp && tOp->checkMagicNumber()) {
	tNoComp += tOp->execTCOPCONF(tAttrInfoLen);
1732 1733 1734 1735 1736 1737
      } else {
	return -1;
      }//if
    }//for
    Uint32 tNoSent = theNoOfOpSent;
    Uint32 tGCI    = indxConf->gci;
unknown's avatar
unknown committed
1738
    theNoOfOpCompleted = tNoComp;
1739 1740 1741 1742 1743
    if (tCommitFlag == 1) {
      theCommitStatus = Committed;
      theGlobalCheckpointId = tGCI;
    } else if ((tNoComp >= tNoSent) &&
               (theLastExecOpInList->theCommitIndicator == 1)){
unknown's avatar
unknown committed
1744 1745 1746 1747
      /**********************************************************************/
      // We sent the transaction with Commit flag set and received a CONF with
      // no Commit flag set. This is clearly an anomaly.
      /**********************************************************************/
1748
      theError.code = 4011;
unknown's avatar
unknown committed
1749 1750
      theCompletionStatus = NdbConnection::CompletedFailure;
      theCommitStatus = NdbConnection::Aborted;
1751 1752 1753 1754 1755 1756
      return 0;
    }//if
    if (tNoComp >= tNoSent) {
      return 0;	// No more operations to wait for
    }//if
     // Not completed the reception yet.
unknown's avatar
unknown committed
1757 1758 1759 1760 1761 1762
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
  }

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
  return -1;
}//NdbConnection::receiveTCINDXCONF()

/*****************************************************************************
int receiveTCINDXREF( NdbApiSignal* aSignal)

Return Value:   Return 0 : send was succesful.
                Return -1: In all other case.   
Parameters:     aSignal: the signal object that contains the 
                TCINDXREF signal from TC.
Remark:         Handles the reception of the TCINDXREF signal.
*****************************************************************************/
int
NdbConnection::receiveTCINDXREF( NdbApiSignal* aSignal)
{
unknown's avatar
unknown committed
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
  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.				      */
    /**********************************************************************/
unknown's avatar
unknown committed
1788 1789
    theCompletionStatus = NdbConnection::CompletedFailure;
    theCommitStatus = NdbConnection::Aborted;
unknown's avatar
unknown committed
1790
    return 0;
unknown's avatar
unknown committed
1791 1792 1793 1794
  } else {
#ifdef NDB_NO_DROPPED_SIGNAL
    abort();
#endif
unknown's avatar
unknown committed
1795
  }
unknown's avatar
unknown committed
1796

unknown's avatar
unknown committed
1797
  return -1;
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
}//NdbConnection::receiveTCINDXREF()

/*******************************************************************************
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 
unknown's avatar
unknown committed
1809
NdbConnection::OpCompleteFailure(Uint8 abortOption)
1810 1811 1812
{
  Uint32 tNoComp = theNoOfOpCompleted;
  Uint32 tNoSent = theNoOfOpSent;
unknown's avatar
unknown committed
1813
  theCompletionStatus = NdbConnection::CompletedFailure;
1814 1815 1816 1817 1818 1819 1820 1821 1822
  tNoComp++;
  theNoOfOpCompleted = tNoComp;
  if (tNoComp == tNoSent) {
    //------------------------------------------------------------------------
    //If the transaction consists of only simple reads we can set
    //Commit state Aborted.  Otherwise this simple operation cannot
    //decide the success of the whole transaction since a simple
    //operation is not really part of that transaction.
    //------------------------------------------------------------------------
unknown's avatar
unknown committed
1823
    if (abortOption == IgnoreError){
1824 1825 1826 1827 1828 1829 1830 1831 1832
      /**
       * There's always a TCKEYCONF when using IgnoreError
       */
#ifdef VM_TRACE
      ndbout_c("Not completing transaction");
#endif
      return -1;
    }
    
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
    return 0;	// Last operation received
  } else if (tNoComp > tNoSent) {
    setOperationErrorCodeAbort(4113);	// Too many operations, 
                                        // stop waiting for more
    return 0;
  } else {
    return -1;	// Continue waiting for more signals
  }//if
}//NdbConnection::OpCompleteFailure()

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

Return Value:  Return 0 : OpCompleteSuccess was successful.
               Return -1: In all other case.  
Remark:        An operation was completed with success.
*******************************************************************************/
int 
NdbConnection::OpCompleteSuccess()
{
  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
unknown's avatar
unknown committed
1864
    theCompletionStatus = NdbConnection::CompletedFailure;
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
    return 0;
  }//if
}//NdbConnection::OpCompleteSuccess()

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

Remark:		Get global checkpoint identity of the transaction
*******************************************************************************/
int
NdbConnection::getGCI()
{
unknown's avatar
unknown committed
1877
  if (theCommitStatus == NdbConnection::Committed) {
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
    return theGlobalCheckpointId;
  }//if
  return 0;
}//NdbConnection::getGCI()

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

Remark:        Get the transaction identity. 
*******************************************************************************/
Uint64
NdbConnection::getTransactionId()
{
  return theTransactionId;
}//NdbConnection::getTransactionId()

1894
NdbConnection::CommitStatusType
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
NdbConnection::commitStatus()
{
  return theCommitStatus;
}//NdbConnection::commitStatus()

int 
NdbConnection::getNdbErrorLine()
{
  return theErrorLine;
}

NdbOperation*
NdbConnection::getNdbErrorOperation()
{
  return theErrorOperation;
}//NdbConnection::getNdbErrorOperation()

const NdbOperation * 
NdbConnection::getNextCompletedOperation(const NdbOperation * current) const {
  if(current == 0)
    return theCompletedFirstOp;
  return current->theNext;
}

#ifdef VM_TRACE
#define CASE(x) case x: ndbout << " " << #x; break
void
NdbConnection::printState()
{
  ndbout << "con=" << hex << this << dec;
  ndbout << " node=" << getConnectedNodeId();
  switch (theStatus) {
  CASE(NotConnected);
  CASE(Connecting);
  CASE(Connected);
  CASE(DisConnecting);
  CASE(ConnectFailure);
unknown's avatar
unknown committed
1932
  default: ndbout << (Uint32) theStatus;
1933 1934 1935 1936 1937 1938
  }
  switch (theListState) {
  CASE(NotInList);
  CASE(InPreparedList);
  CASE(InSendList);
  CASE(InCompletedList);
unknown's avatar
unknown committed
1939
  default: ndbout << (Uint32) theListState;
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
  }
  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);
unknown's avatar
unknown committed
1952
  default: ndbout << (Uint32) theSendStatus;
1953 1954 1955 1956 1957 1958 1959
  }
  switch (theCommitStatus) {
  CASE(NotStarted);
  CASE(Started);
  CASE(Committed);
  CASE(Aborted);
  CASE(NeedAbort);
unknown's avatar
unknown committed
1960
  default: ndbout << (Uint32) theCommitStatus;
1961 1962 1963 1964 1965 1966
  }
  switch (theCompletionStatus) {
  CASE(NotCompleted);
  CASE(CompletedSuccess);
  CASE(CompletedFailure);
  CASE(DefinitionFailure);
unknown's avatar
unknown committed
1967
  default: ndbout << (Uint32) theCompletionStatus;
1968 1969 1970 1971 1972
  }
  ndbout << endl;
}
#undef CASE
#endif
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

int
NdbConnection::report_node_failure(Uint32 id){
  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;
  while(tmp != 0)
  {
    if(tmp->theReceiver.m_expected_result_length == len && 
       tmp->theReceiver.m_received_result_length == 0)
    {
      tNoComp++;
      tmp->theError.code = 4119;
    }
    tmp = tmp->next();
  }
  theNoOfOpCompleted = tNoComp;
  if(tNoComp == tNoSent)
  {
    theError.code = 4119;
    theCompletionStatus = NdbConnection::CompletedFailure;    
    return 1;
  }
  return 0;
}