DbtcMain.cpp 431 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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 */

#define DBTC_C

#include "Dbtc.hpp"
#include "md5_hash.hpp"
#include <RefConvert.hpp>
#include <ndb_limits.h>
unknown's avatar
unknown committed
23
#include <my_sys.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

#include <signaldata/EventReport.hpp>
#include <signaldata/TcKeyReq.hpp>
#include <signaldata/TcKeyConf.hpp>
#include <signaldata/TcKeyRef.hpp>
#include <signaldata/KeyInfo.hpp>
#include <signaldata/AttrInfo.hpp>
#include <signaldata/TransIdAI.hpp>
#include <signaldata/TcRollbackRep.hpp>
#include <signaldata/NodeFailRep.hpp>
#include <signaldata/ReadNodesConf.hpp>
#include <signaldata/NFCompleteRep.hpp>
#include <signaldata/LqhKey.hpp>
#include <signaldata/TcCommit.hpp>
#include <signaldata/TcContinueB.hpp>
#include <signaldata/TcKeyFailConf.hpp>
#include <signaldata/AbortAll.hpp>
#include <signaldata/ScanFrag.hpp>
#include <signaldata/ScanTab.hpp>
#include <signaldata/PrepDropTab.hpp>
#include <signaldata/DropTab.hpp>
#include <signaldata/AlterTab.hpp>
#include <signaldata/CreateTrig.hpp>
#include <signaldata/DropTrig.hpp>
#include <signaldata/FireTrigOrd.hpp>
#include <signaldata/TrigAttrInfo.hpp>
#include <signaldata/CreateIndx.hpp>
#include <signaldata/DropIndx.hpp>
#include <signaldata/AlterIndx.hpp>
#include <signaldata/ScanTab.hpp>
#include <signaldata/SystemError.hpp>
#include <signaldata/DumpStateOrd.hpp>
#include <signaldata/DisconnectRep.hpp>
#include <signaldata/TcHbRep.hpp>

#include <signaldata/PrepDropTab.hpp>
#include <signaldata/DropTab.hpp>
#include <signaldata/TcIndx.hpp>
#include <signaldata/IndxKeyInfo.hpp>
#include <signaldata/IndxAttrInfo.hpp>
#include <signaldata/PackedSignal.hpp>
#include <AttributeHeader.hpp>
#include <signaldata/DictTabInfo.hpp>
unknown's avatar
unknown committed
67 68
#include <AttributeDescriptor.hpp>
#include <SectionReader.hpp>
69
#include <KeyDescriptor.hpp>
70 71

#include <NdbOut.hpp>
unknown's avatar
unknown committed
72
#include <DebuggerNames.hpp>
73 74 75 76 77 78 79 80 81 82 83

// Use DEBUG to print messages that should be
// seen only when we debug the product
#ifdef VM_TRACE
#define DEBUG(x) ndbout << "DBTC: "<< x << endl;
#else
#define DEBUG(x)
#endif
  
#define INTERNAL_TRIGGER_TCKEYREQ_JBA 0

unknown's avatar
unknown committed
84 85 86
#ifdef VM_TRACE
NdbOut &
operator<<(NdbOut& out, Dbtc::ConnectionState state){
unknown's avatar
unknown committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  switch(state){
  case Dbtc::CS_CONNECTED: out << "CS_CONNECTED"; break;
  case Dbtc::CS_DISCONNECTED: out << "CS_DISCONNECTED"; break;
  case Dbtc::CS_STARTED: out << "CS_STARTED"; break;
  case Dbtc::CS_RECEIVING: out << "CS_RECEIVING"; break;
  case Dbtc::CS_PREPARED: out << "CS_PREPARED"; break;
  case Dbtc::CS_START_PREPARING: out << "CS_START_PREPARING"; break;
  case Dbtc::CS_REC_PREPARING: out << "CS_REC_PREPARING"; break;
  case Dbtc::CS_RESTART: out << "CS_RESTART"; break;
  case Dbtc::CS_ABORTING: out << "CS_ABORTING"; break;
  case Dbtc::CS_COMPLETING: out << "CS_COMPLETING"; break;
  case Dbtc::CS_COMPLETE_SENT: out << "CS_COMPLETE_SENT"; break;
  case Dbtc::CS_PREPARE_TO_COMMIT: out << "CS_PREPARE_TO_COMMIT"; break;
  case Dbtc::CS_COMMIT_SENT: out << "CS_COMMIT_SENT"; break;
  case Dbtc::CS_START_COMMITTING: out << "CS_START_COMMITTING"; break;
  case Dbtc::CS_COMMITTING: out << "CS_COMMITTING"; break;
  case Dbtc::CS_REC_COMMITTING: out << "CS_REC_COMMITTING"; break;
  case Dbtc::CS_WAIT_ABORT_CONF: out << "CS_WAIT_ABORT_CONF"; break;
  case Dbtc::CS_WAIT_COMPLETE_CONF: out << "CS_WAIT_COMPLETE_CONF"; break;
  case Dbtc::CS_WAIT_COMMIT_CONF: out << "CS_WAIT_COMMIT_CONF"; break;
  case Dbtc::CS_FAIL_ABORTING: out << "CS_FAIL_ABORTING"; break;
  case Dbtc::CS_FAIL_ABORTED: out << "CS_FAIL_ABORTED"; break;
  case Dbtc::CS_FAIL_PREPARED: out << "CS_FAIL_PREPARED"; break;
  case Dbtc::CS_FAIL_COMMITTING: out << "CS_FAIL_COMMITTING"; break;
  case Dbtc::CS_FAIL_COMMITTED: out << "CS_FAIL_COMMITTED"; break;
  case Dbtc::CS_FAIL_COMPLETED: out << "CS_FAIL_COMPLETED"; break;
  case Dbtc::CS_START_SCAN: out << "CS_START_SCAN"; break;
  default:
    out << "Unknown: " << (int)state; break;
  }
unknown's avatar
unknown committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  return out;
}
NdbOut &
operator<<(NdbOut& out, Dbtc::OperationState state){
  out << (int)state;
  return out;
}
NdbOut &
operator<<(NdbOut& out, Dbtc::AbortState state){
  out << (int)state;
  return out;
}
NdbOut &
operator<<(NdbOut& out, Dbtc::ReturnSignal state){
  out << (int)state;
  return out;
}
NdbOut &
operator<<(NdbOut& out, Dbtc::ScanRecord::ScanState state){
  out << (int)state;
  return out;
}
NdbOut &
operator<<(NdbOut& out, Dbtc::ScanFragRec::ScanFragState state){
  out << (int)state;
  return out;
}
#endif

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
void
Dbtc::updateBuddyTimer(ApiConnectRecordPtr apiPtr)
{
  if (apiPtr.p->buddyPtr != RNIL) {
    jam();
    ApiConnectRecordPtr buddyApiPtr;
    buddyApiPtr.i = apiPtr.p->buddyPtr;
    ptrCheckGuard(buddyApiPtr, capiConnectFilesize, apiConnectRecord);
    if (getApiConTimer(buddyApiPtr.i) != 0) {
      if ((apiPtr.p->transid[0] == buddyApiPtr.p->transid[0]) &&
          (apiPtr.p->transid[1] == buddyApiPtr.p->transid[1])) {
        jam();
        setApiConTimer(buddyApiPtr.i, ctcTimer, __LINE__);
      } else {
        jam();
        // Not a buddy anymore since not the same transid
        apiPtr.p->buddyPtr = RNIL;
      }//if
    }//if
  }//if
}

void Dbtc::execCONTINUEB(Signal* signal) 
{
  UintR tcase;

  jamEntry();
  tcase = signal->theData[0];
  UintR Tdata0 = signal->theData[1];
  UintR Tdata1 = signal->theData[2];
176
  UintR Tdata2 = signal->theData[3];
177 178 179
  switch (tcase) {
  case TcContinueB::ZRETURN_FROM_QUEUED_DELIVERY:
    jam();
unknown's avatar
unknown committed
180
    ndbrequire(false);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    return;
  case TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER:
    jam();
    tcNodeFailptr.i = Tdata0;
    ptrCheckGuard(tcNodeFailptr, 1, tcFailRecord);
    completeTransAtTakeOverLab(signal, Tdata1);
    return;
  case TcContinueB::ZCONTINUE_TIME_OUT_CONTROL:
    jam();
    timeOutLoopStartLab(signal, Tdata0);
    return;
  case TcContinueB::ZNODE_TAKE_OVER_COMPLETED:
    jam();
    tnodeid = Tdata0;
    tcNodeFailptr.i = 0;
    ptrAss(tcNodeFailptr, tcFailRecord);
    nodeTakeOverCompletedLab(signal);
    return;
  case TcContinueB::ZINITIALISE_RECORDS:
    jam();
201
    initialiseRecordsLab(signal, Tdata0, Tdata2, signal->theData[4]);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    return;
  case TcContinueB::ZSEND_COMMIT_LOOP:
    jam();
    apiConnectptr.i = Tdata0;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    tcConnectptr.i = Tdata1;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    commit020Lab(signal);
    return;
  case TcContinueB::ZSEND_COMPLETE_LOOP:
    jam();
    apiConnectptr.i = Tdata0;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    tcConnectptr.i = Tdata1;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    complete010Lab(signal);
    return;
  case TcContinueB::ZHANDLE_FAILED_API_NODE:
    jam();
    handleFailedApiNode(signal, Tdata0, Tdata1);
    return;
  case TcContinueB::ZTRANS_EVENT_REP:
    jam();
    /* -------------------------------------------------------------------- */
    // Report information about transaction activity once per second.
    /* -------------------------------------------------------------------- */
228 229 230 231 232 233 234 235
    if (c_counters.c_trans_status == TransCounters::Timer){
      Uint32 len = c_counters.report(signal);
      sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, len, JBB);
      
      c_counters.reset();
      signal->theData[0] = TcContinueB::ZTRANS_EVENT_REP;
      sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 5000, 1);
    }
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    return;
  case TcContinueB::ZCONTINUE_TIME_OUT_FRAG_CONTROL:
    jam();
    timeOutLoopStartFragLab(signal, Tdata0);
    return;
  case TcContinueB::ZABORT_BREAK:
    jam();
    tcConnectptr.i = Tdata0;
    apiConnectptr.i = Tdata1;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    apiConnectptr.p->counter--;
    abort015Lab(signal);
    return;
  case TcContinueB::ZABORT_TIMEOUT_BREAK:
    jam();
    tcConnectptr.i = Tdata0;
    apiConnectptr.i = Tdata1;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
unknown's avatar
unknown committed
254
    apiConnectptr.p->counter--;
255 256 257 258 259 260 261 262 263 264 265 266 267 268
    sendAbortedAfterTimeout(signal, 1);
    return;
  case TcContinueB::ZHANDLE_FAILED_API_NODE_REMOVE_MARKERS:
    jam();
    removeMarkerForFailedAPI(signal, Tdata0, Tdata1);
    return;
  case TcContinueB::ZWAIT_ABORT_ALL:
    jam();
    checkAbortAllTimeout(signal, Tdata0);
    return;
  case TcContinueB::ZCHECK_SCAN_ACTIVE_FAILED_LQH:
    jam();
    checkScanActiveInFailedLqh(signal, Tdata0, Tdata1);
    return;
unknown's avatar
unknown committed
269 270 271 272
  case TcContinueB::ZNF_CHECK_TRANSACTIONS:
    jam();
    nodeFailCheckTransactions(signal, Tdata0, Tdata1);
    return;
273 274 275 276 277 278 279 280 281 282 283 284
  case TcContinueB::CHECK_WAIT_DROP_TAB_FAILED_LQH:
    jam();
    checkWaitDropTabFailedLqh(signal, Tdata0, Tdata1);
    return;
  case TcContinueB::TRIGGER_PENDING:
    jam();
    ApiConnectRecordPtr transPtr;
    transPtr.i = Tdata0;
    ptrCheckGuard(transPtr, capiConnectFilesize, apiConnectRecord);
    transPtr.p->triggerPending = false;
    executeTriggers(signal, &transPtr);
    return;
285 286 287 288 289 290
  case TcContinueB::DelayTCKEYCONF:
    jam();
    apiConnectptr.i = Tdata0;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    sendtckeyconf(signal, Tdata1);
    return;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  default:
    ndbrequire(false);
  }//switch
}

void Dbtc::execDIGETNODESREF(Signal* signal) 
{
  jamEntry();
  terrorCode = signal->theData[1];
  releaseAtErrorLab(signal);
}

void Dbtc::execINCL_NODEREQ(Signal* signal) 
{
  jamEntry();
  tblockref = signal->theData[0];
  hostptr.i = signal->theData[1];
  ptrCheckGuard(hostptr, chostFilesize, hostRecord);
  hostptr.p->hostStatus = HS_ALIVE;
  signal->theData[0] = cownref;
unknown's avatar
unknown committed
311
  c_alive_nodes.set(hostptr.i);
312 313 314 315 316 317 318 319 320 321 322 323
  sendSignal(tblockref, GSN_INCL_NODECONF, signal, 1, JBB);
}

void Dbtc::execREAD_NODESREF(Signal* signal) 
{
  jamEntry();
  ndbrequire(false);
}

void Dbtc::execTC_SCHVERREQ(Signal* signal) 
{
  jamEntry();
unknown's avatar
unknown committed
324 325 326 327
  if (! assembleFragments(signal)) {
    jam();
    return;
  }
328 329 330 331 332 333 334
  tabptr.i = signal->theData[0];
  ptrCheckGuard(tabptr, ctabrecFilesize, tableRecord);
  tabptr.p->currentSchemaVersion = signal->theData[1];
  tabptr.p->storedTable = (bool)signal->theData[2];
  BlockReference retRef = signal->theData[3];
  tabptr.p->tableType = (Uint8)signal->theData[4];
  BlockReference retPtr = signal->theData[5];
unknown's avatar
unknown committed
335 336
  Uint32 noOfKeyAttr = signal->theData[6];
  ndbrequire(noOfKeyAttr <= MAX_ATTRIBUTES_IN_INDEX);
337 338 339

  const KeyDescriptor* desc = g_key_descriptor_pool.getPtr(tabptr.i);
  ndbrequire(noOfKeyAttr == desc->noOfKeyAttr);
340 341 342 343

  ndbrequire(tabptr.p->enabled == false);
  tabptr.p->enabled = true;
  tabptr.p->dropping = false;
344 345 346
  tabptr.p->noOfKeyAttr = desc->noOfKeyAttr;
  tabptr.p->hasCharAttr = desc->hasCharAttr;
  tabptr.p->noOfDistrKeys = desc->noOfDistrKeys;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  
  signal->theData[0] = tabptr.i;
  signal->theData[1] = retPtr;
  sendSignal(retRef, GSN_TC_SCHVERCONF, signal, 2, JBB);
}//Dbtc::execTC_SCHVERREQ()

void
Dbtc::execPREP_DROP_TAB_REQ(Signal* signal)
{
  jamEntry();
  
  PrepDropTabReq* req = (PrepDropTabReq*)signal->getDataPtr();
  
  TableRecordPtr tabPtr;
  tabPtr.i = req->tableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
  
  Uint32 senderRef = req->senderRef;
  Uint32 senderData = req->senderData;
  
  if(!tabPtr.p->enabled){
    jam();
    PrepDropTabRef* ref = (PrepDropTabRef*)signal->getDataPtrSend();
    ref->senderRef = reference();
    ref->senderData = senderData;
    ref->tableId = tabPtr.i;
    ref->errorCode = PrepDropTabRef::NoSuchTable;
    sendSignal(senderRef, GSN_PREP_DROP_TAB_REF, signal,
	       PrepDropTabRef::SignalLength, JBB);
    return;
  }

  if(tabPtr.p->dropping){
    jam();
    PrepDropTabRef* ref = (PrepDropTabRef*)signal->getDataPtrSend();
    ref->senderRef = reference();
    ref->senderData = senderData;
    ref->tableId = tabPtr.i;
    ref->errorCode = PrepDropTabRef::DropInProgress;
    sendSignal(senderRef, GSN_PREP_DROP_TAB_REF, signal,
	       PrepDropTabRef::SignalLength, JBB);
    return;
  }
  
  tabPtr.p->dropping = true;
  tabPtr.p->dropTable.senderRef = senderRef;
  tabPtr.p->dropTable.senderData = senderData;

  {
    WaitDropTabReq * req = (WaitDropTabReq*)signal->getDataPtrSend();
    req->tableId = tabPtr.i;
    req->senderRef = reference();
    
    HostRecordPtr hostPtr;
    tabPtr.p->dropTable.waitDropTabCount.clearWaitingFor();
    for (hostPtr.i = 1; hostPtr.i < MAX_NDB_NODES; hostPtr.i++) {
      jam();
      ptrAss(hostPtr, hostRecord);
      if (hostPtr.p->hostStatus == HS_ALIVE) {
	jam();
	tabPtr.p->dropTable.waitDropTabCount.setWaitingFor(hostPtr.i);
	sendSignal(calcLqhBlockRef(hostPtr.i), GSN_WAIT_DROP_TAB_REQ,
		   signal, WaitDropTabReq::SignalLength, JBB);
      }//for
    }//if
    
    ndbrequire(tabPtr.p->dropTable.waitDropTabCount.done() != true);
  }
}

void
Dbtc::execWAIT_DROP_TAB_CONF(Signal* signal)
{
  jamEntry();
  WaitDropTabConf * conf = (WaitDropTabConf*)signal->getDataPtr();

  TableRecordPtr tabPtr;
  tabPtr.i = conf->tableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
  
  ndbrequire(tabPtr.p->dropping == true);
  Uint32 nodeId = refToNode(conf->senderRef);
  tabPtr.p->dropTable.waitDropTabCount.clearWaitingFor(nodeId);
  
  if(!tabPtr.p->dropTable.waitDropTabCount.done()){
    jam();
    return;
  }
  
  {
    PrepDropTabConf* conf = (PrepDropTabConf*)signal->getDataPtrSend();
    conf->tableId = tabPtr.i;
    conf->senderRef = reference();
    conf->senderData = tabPtr.p->dropTable.senderData;
    sendSignal(tabPtr.p->dropTable.senderRef, GSN_PREP_DROP_TAB_CONF, signal,
	       PrepDropTabConf::SignalLength, JBB);
    tabPtr.p->dropTable.senderRef = 0;
  }
}

unknown's avatar
unknown committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
void
Dbtc::execWAIT_DROP_TAB_REF(Signal* signal)
{
  jamEntry();
  WaitDropTabRef * ref = (WaitDropTabRef*)signal->getDataPtr();

  TableRecordPtr tabPtr;
  tabPtr.i = ref->tableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
  
  ndbrequire(tabPtr.p->dropping == true);
  Uint32 nodeId = refToNode(ref->senderRef);
  tabPtr.p->dropTable.waitDropTabCount.clearWaitingFor(nodeId);
  
  ndbrequire(ref->errorCode == WaitDropTabRef::NoSuchTable ||
	     ref->errorCode == WaitDropTabRef::NF_FakeErrorREF);
  
  if(!tabPtr.p->dropTable.waitDropTabCount.done()){
    jam();
    return;
  }
  
  {
    PrepDropTabConf* conf = (PrepDropTabConf*)signal->getDataPtrSend();
    conf->tableId = tabPtr.i;
    conf->senderRef = reference();
    conf->senderData = tabPtr.p->dropTable.senderData;
    sendSignal(tabPtr.p->dropTable.senderRef, GSN_PREP_DROP_TAB_CONF, signal,
	       PrepDropTabConf::SignalLength, JBB);
    tabPtr.p->dropTable.senderRef = 0;
  }
}  

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
void
Dbtc::checkWaitDropTabFailedLqh(Signal* signal, Uint32 nodeId, Uint32 tableId)
{
  
  TableRecordPtr tabPtr;
  tabPtr.i = tableId;

  WaitDropTabConf * conf = (WaitDropTabConf*)signal->getDataPtr();
  conf->tableId = tableId;

  const Uint32 RT_BREAK = 16;
  for(Uint32 i = 0; i<RT_BREAK && tabPtr.i < ctabrecFilesize; i++, tabPtr.i++){
    jam();
    ptrAss(tabPtr, tableRecord);
    if(tabPtr.p->enabled && tabPtr.p->dropping){
      if(tabPtr.p->dropTable.waitDropTabCount.isWaitingFor(nodeId)){
        jam();
	conf->senderRef = calcLqhBlockRef(nodeId);
	execWAIT_DROP_TAB_CONF(signal);
	tabPtr.i++;
	break;
      }
    }
  }
  
  if(tabPtr.i == ctabrecFilesize){
    /**
     * Finished
     */
    jam();
unknown's avatar
unknown committed
510
    checkNodeFailComplete(signal, nodeId, HostRecord::NF_CHECK_DROP_TAB);
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    return;
  }
  
  signal->theData[0] = TcContinueB::CHECK_WAIT_DROP_TAB_FAILED_LQH;
  signal->theData[1] = nodeId;
  signal->theData[2] = tabPtr.i;
  sendSignal(reference(), GSN_CONTINUEB, signal, 3, JBB);
}

void
Dbtc::execDROP_TAB_REQ(Signal* signal)
{
  jamEntry();

  DropTabReq* req = (DropTabReq*)signal->getDataPtr();
  
  TableRecordPtr tabPtr;
  tabPtr.i = req->tableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
  
  Uint32 senderRef = req->senderRef;
  Uint32 senderData = req->senderData;
  DropTabReq::RequestType rt = (DropTabReq::RequestType)req->requestType;
  
  if(!tabPtr.p->enabled && rt == DropTabReq::OnlineDropTab){
    jam();
    DropTabRef* ref = (DropTabRef*)signal->getDataPtrSend();
    ref->senderRef = reference();
    ref->senderData = senderData;
    ref->tableId = tabPtr.i;
    ref->errorCode = DropTabRef::NoSuchTable;
    sendSignal(senderRef, GSN_DROP_TAB_REF, signal,
	       DropTabRef::SignalLength, JBB);
    return;
  }

  if(!tabPtr.p->dropping && rt == DropTabReq::OnlineDropTab){
    jam();
    DropTabRef* ref = (DropTabRef*)signal->getDataPtrSend();
    ref->senderRef = reference();
    ref->senderData = senderData;
    ref->tableId = tabPtr.i;
    ref->errorCode = DropTabRef::DropWoPrep;
    sendSignal(senderRef, GSN_DROP_TAB_REF, signal,
	       DropTabRef::SignalLength, JBB);
    return;
  }
  
  tabPtr.p->enabled = false;
  tabPtr.p->dropping = false;
  
  DropTabConf * conf = (DropTabConf*)signal->getDataPtrSend();
  conf->tableId = tabPtr.i;
  conf->senderRef = reference();
  conf->senderData = senderData;
  sendSignal(senderRef, GSN_DROP_TAB_CONF, signal,
	     PrepDropTabConf::SignalLength, JBB);
}

void Dbtc::execALTER_TAB_REQ(Signal * signal)
{
  AlterTabReq* const req = (AlterTabReq*)signal->getDataPtr();
  const Uint32 senderRef = req->senderRef;
  const Uint32 senderData = req->senderData;
  const Uint32 changeMask = req->changeMask;
  const Uint32 tableId = req->tableId;
  const Uint32 tableVersion = req->tableVersion;
  const Uint32 gci = req->gci;
  AlterTabReq::RequestType requestType = 
    (AlterTabReq::RequestType) req->requestType;

  TableRecordPtr tabPtr;
  tabPtr.i = req->tableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
  tabPtr.p->currentSchemaVersion = tableVersion;

  // Request handled successfully 
  AlterTabConf * conf = (AlterTabConf*)signal->getDataPtrSend();
  conf->senderRef = reference();
  conf->senderData = senderData;
  conf->changeMask = changeMask;
  conf->tableId = tableId;
  conf->tableVersion = tableVersion;
  conf->gci = gci;
  conf->requestType = requestType;
  sendSignal(senderRef, GSN_ALTER_TAB_CONF, signal, 
	     AlterTabConf::SignalLength, JBB);
}

/* ***************************************************************************/
/*                                START / RESTART                            */
/* ***************************************************************************/
603
void Dbtc::execREAD_CONFIG_REQ(Signal* signal) 
604
{
605 606 607 608 609
  const ReadConfigReq * req = (ReadConfigReq*)signal->getDataPtr();
  Uint32 ref = req->senderRef;
  Uint32 senderData = req->senderData;
  ndbrequire(req->noOfParameters == 0);
  
610
  jamEntry();
611 612 613 614 615
  
  const ndb_mgm_configuration_iterator * p = 
    theConfiguration.getOwnConfigIterator();
  ndbrequire(p != 0);
  
616 617
  initData();
  
618 619 620 621 622 623 624 625 626 627 628
  UintR apiConnect;
  UintR tcConnect;
  UintR tables;
  UintR localScan;
  UintR tcScan;

  ndbrequire(!ndb_mgm_get_int_parameter(p, CFG_TC_API_CONNECT, &apiConnect));
  ndbrequire(!ndb_mgm_get_int_parameter(p, CFG_TC_TC_CONNECT, &tcConnect));
  ndbrequire(!ndb_mgm_get_int_parameter(p, CFG_TC_TABLE, &tables));
  ndbrequire(!ndb_mgm_get_int_parameter(p, CFG_TC_LOCAL_SCAN, &localScan));
  ndbrequire(!ndb_mgm_get_int_parameter(p, CFG_TC_SCAN, &tcScan));
629 630 631 632 633 634 635 636 637

  ccacheFilesize = (apiConnect/3) + 1;
  capiConnectFilesize = apiConnect;
  ctcConnectFilesize  = tcConnect;
  ctabrecFilesize     = tables;
  cscanrecFileSize = tcScan;
  cscanFragrecFileSize = localScan;

  initRecords();
638
  initialiseRecordsLab(signal, 0, ref, senderData);
639

640 641 642 643 644 645 646 647 648 649 650 651 652 653
  Uint32 val = 3000;
  ndb_mgm_get_int_parameter(p, CFG_DB_TRANSACTION_DEADLOCK_TIMEOUT, &val);
  set_timeout_value(val);

  val = 3000;
  ndb_mgm_get_int_parameter(p, CFG_DB_TRANSACTION_INACTIVE_TIMEOUT, &val);
  set_appl_timeout_value(val);

  val = 1;
  //ndb_mgm_get_int_parameter(p, CFG_DB_PARALLEL_TRANSACTION_TAKEOVER, &val);
  set_no_parallel_takeover(val);

  ctimeOutCheckDelay = 50; // 500ms
}//Dbtc::execSIZEALT_REP()
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

void Dbtc::execSTTOR(Signal* signal) 
{
  Uint16 tphase;

  jamEntry();
                                                     /* START CASE */
  tphase = signal->theData[1];
  csignalKey = signal->theData[6];
  switch (tphase) {
  case ZSPH1:
    jam();
    startphase1x010Lab(signal);
    return;
  default:
    jam();
    sttorryLab(signal); /* START PHASE 255 */
    return;
  }//switch
}//Dbtc::execSTTOR()

void Dbtc::sttorryLab(Signal* signal) 
{
  signal->theData[0] = csignalKey;
  signal->theData[1] = 3;    /* BLOCK CATEGORY */
  signal->theData[2] = 2;    /* SIGNAL VERSION NUMBER */
  signal->theData[3] = ZSPH1;
  signal->theData[4] = 255;
  sendSignal(NDBCNTR_REF, GSN_STTORRY, signal, 5, JBB);
}//Dbtc::sttorryLab()

/* ***************************************************************************/
/*                          INTERNAL  START / RESTART                        */
/*****************************************************************************/
void Dbtc::execNDB_STTOR(Signal* signal) 
{
  Uint16 tndbstartphase;
  Uint16 tstarttype;

  jamEntry();
  tusersblkref = signal->theData[0];
  tnodeid = signal->theData[1];
  tndbstartphase = signal->theData[2];   /* START PHASE      */
  tstarttype = signal->theData[3];       /* START TYPE       */
  switch (tndbstartphase) {
  case ZINTSPH1:
    jam();
    intstartphase1x010Lab(signal);
    return;
  case ZINTSPH2:
    jam();
    intstartphase2x010Lab(signal);
    return;
  case ZINTSPH3:
    jam();
    intstartphase3x010Lab(signal);      /* SEIZE CONNECT RECORD IN EACH LQH*/
// Start transaction event reporting.
711 712
    c_counters.c_trans_status = TransCounters::Timer;
    c_counters.reset();
713
    signal->theData[0] = TcContinueB::ZTRANS_EVENT_REP;
714
    sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 10, 1);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    return;
  case ZINTSPH6:
    jam();
    csystemStart = SSS_TRUE;
    break;
  default:
    jam();
    break;
  }//switch
  ndbsttorry010Lab(signal);
  return;
}//Dbtc::execNDB_STTOR()

void Dbtc::ndbsttorry010Lab(Signal* signal) 
{
  signal->theData[0] = cownref;
  sendSignal(cndbcntrblockref, GSN_NDB_STTORRY, signal, 1, JBB);
}//Dbtc::ndbsttorry010Lab()

void
Dbtc::set_timeout_value(Uint32 timeOut)
{
  timeOut = timeOut / 10;
  if (timeOut < 2) {
    jam();
    timeOut = 100;
  }//if
  ctimeOutValue = timeOut;
}

void
Dbtc::set_appl_timeout_value(Uint32 timeOut)
{
unknown's avatar
unknown committed
748 749 750 751 752 753 754 755
  if (timeOut)
  {
    timeOut /= 10;
    if (timeOut < ctimeOutValue) {
      jam();
      c_appl_timeout_value = ctimeOutValue;
    }//if
  }
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  c_appl_timeout_value = timeOut;
}

void
Dbtc::set_no_parallel_takeover(Uint32 noParallelTakeOver)
{
  if (noParallelTakeOver == 0) {
    jam();
    noParallelTakeOver = 1;
  } else if (noParallelTakeOver > MAX_NDB_NODES) {
    jam();
    noParallelTakeOver = MAX_NDB_NODES;
  }//if
  cnoParallelTakeOver = noParallelTakeOver;
}

/* ***************************************************************************/
/*                        S T A R T P H A S E 1 X                            */
/*                     INITIALISE BLOCKREF AND BLOCKNUMBERS                  */
/* ***************************************************************************/
void Dbtc::startphase1x010Lab(Signal* signal) 
{
  csystemStart = SSS_FALSE;
  ctimeOutCheckCounter = 0;
  ctimeOutCheckFragCounter = 0;
  ctimeOutMissedHeartbeats = 0;
  ctimeOutCheckHeartbeat = 0;
  ctimeOutCheckLastHeartbeat = 0;
  ctimeOutCheckActive = TOCS_FALSE;
  ctimeOutCheckFragActive = TOCS_FALSE;
  sttorryLab(signal);
}//Dbtc::startphase1x010Lab()

/*****************************************************************************/
/*                        I N T S T A R T P H A S E 1 X                      */
/*                         INITIALISE ALL RECORDS.                           */
/*****************************************************************************/
void Dbtc::intstartphase1x010Lab(Signal* signal) 
{
  cownNodeid = tnodeid;
  cownref =          calcTcBlockRef(cownNodeid);
  clqhblockref =     calcLqhBlockRef(cownNodeid);
  cdihblockref =     calcDihBlockRef(cownNodeid);
  cdictblockref =    calcDictBlockRef(cownNodeid);
  cndbcntrblockref = calcNdbCntrBlockRef(cownNodeid);
  cerrorBlockref   = calcNdbCntrBlockRef(cownNodeid);
  coperationsize = 0;
  cfailure_nr = 0;
  ndbsttorry010Lab(signal);
}//Dbtc::intstartphase1x010Lab()

/*****************************************************************************/
/*                         I N T S T A R T P H A S E 2 X                     */
/*                          SET-UP LOCAL CONNECTIONS.                        */
/*****************************************************************************/
void Dbtc::intstartphase2x010Lab(Signal* signal) 
{
  tcConnectptr.i = cfirstfreeTcConnect;
  intstartphase2x020Lab(signal);
}//Dbtc::intstartphase2x010Lab()

void Dbtc::intstartphase2x020Lab(Signal* signal) 
{
  if (tcConnectptr.i == RNIL) {
    jam();
    ndbsttorry010Lab(signal);
    return;
  }//if
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  tcConnectptr.p->tcConnectstate = OS_CONNECTING_DICT;
/* ****************** */
/*     DISEIZEREQ   < */
/* ****************** */
  signal->theData[0] = tcConnectptr.i;
  signal->theData[1] = cownref;
  sendSignal(cdihblockref, GSN_DISEIZEREQ, signal, 2, JBB);
}//Dbtc::intstartphase2x020Lab()

void Dbtc::execDISEIZECONF(Signal* signal) 
{
  jamEntry();
  tcConnectptr.i = signal->theData[0];
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  tcConnectptr.p->dihConnectptr = signal->theData[1];
  tcConnectptr.i = tcConnectptr.p->nextTcConnect;
  intstartphase2x020Lab(signal);
}//Dbtc::execDISEIZECONF()

/*****************************************************************************/
/*                         I N T S T A R T P H A S E 3 X                     */
/*                        PREPARE DISTRIBUTED CONNECTIONS                    */
/*****************************************************************************/
void Dbtc::intstartphase3x010Lab(Signal* signal) 
{
  signal->theData[0] = cownref;
  sendSignal(cndbcntrblockref, GSN_READ_NODESREQ, signal, 1, JBB);
}//Dbtc::intstartphase3x010Lab()

void Dbtc::execREAD_NODESCONF(Signal* signal) 
{
  UintR guard0;

  jamEntry();

  ReadNodesConf * const readNodes = (ReadNodesConf *)&signal->theData[0];

  csystemnodes  = readNodes->noOfNodes;
  cmasterNodeId = readNodes->masterNodeId;

  con_lineNodes = 0;
  arrGuard(csystemnodes, MAX_NDB_NODES);
  guard0 = csystemnodes - 1;
  arrGuard(guard0, MAX_NDB_NODES);       // Check not zero nodes

  for (unsigned i = 1; i < MAX_NDB_NODES; i++) {
    jam();
    if (NodeBitmask::get(readNodes->allNodes, i)) {
      hostptr.i = i;
      ptrCheckGuard(hostptr, chostFilesize, hostRecord);
875

876 877 878 879 880 881 882
      if (NodeBitmask::get(readNodes->inactiveNodes, i)) {
        jam();
        hostptr.p->hostStatus = HS_DEAD;
      } else {
        jam();
        con_lineNodes++;
        hostptr.p->hostStatus = HS_ALIVE;
unknown's avatar
unknown committed
883
	c_alive_nodes.set(i);
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
      }//if
    }//if
  }//for
  ndbsttorry010Lab(signal);
}//Dbtc::execREAD_NODESCONF()

/*****************************************************************************/
/*                     A P I _ F A I L R E Q                                 */
// An API node has failed for some reason. We need to disconnect all API 
// connections to the API node. This also includes 
/*****************************************************************************/
void Dbtc::execAPI_FAILREQ(Signal* signal)
{
  /***************************************************************************
   * Set the block reference to return API_FAILCONF to. Set the number of api 
   * connects currently closing to one to indicate that we are still in the 
   * process of going through the api connect records. Thus checking for zero 
   * can only be true after all api connect records have been checked.
   **************************************************************************/
  jamEntry();  
  capiFailRef = signal->theData[1];
  arrGuard(signal->theData[0], MAX_NODES);
  capiConnectClosing[signal->theData[0]] = 1;
  handleFailedApiNode(signal, signal->theData[0], (UintR)0);
}

void
Dbtc::handleFailedApiNode(Signal* signal, 
                          UintR TapiFailedNode, 
                          UintR TapiConnectPtr)
{
  UintR TloopCount = 0;
  arrGuard(TapiFailedNode, MAX_NODES);
  apiConnectptr.i = TapiConnectPtr;
  do {
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    const UintR TapiNode = refToNode(apiConnectptr.p->ndbapiBlockref);
    if (TapiNode == TapiFailedNode) {
#ifdef VM_TRACE
      if (apiConnectptr.p->apiFailState != ZFALSE) {
        ndbout << "Error in previous API fail handling discovered" << endl
	       << "  apiConnectptr.i = " << apiConnectptr.i << endl
	       << "  apiConnectstate = " << apiConnectptr.p->apiConnectstate 
	       << endl
	       << "  ndbapiBlockref = " << hex
	       << apiConnectptr.p->ndbapiBlockref << endl
	       << "  apiNode = " << refToNode(apiConnectptr.p->ndbapiBlockref) 
	       << endl;
	if (apiConnectptr.p->lastTcConnect != RNIL){	  
	  jam();
	  tcConnectptr.i = apiConnectptr.p->lastTcConnect;
	  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
	  ndbout << "  tcConnectptr.i = " << tcConnectptr.i << endl
		 << "  tcConnectstate = " << tcConnectptr.p->tcConnectstate 
		 << endl;
	}
      }//if
#endif
      
      apiConnectptr.p->returnsignal = RS_NO_RETURN;
      /***********************************************************************/
      // The connected node is the failed node.
      /**********************************************************************/
      switch(apiConnectptr.p->apiConnectstate) {
      case CS_DISCONNECTED:
        /*********************************************************************/
        // These states do not need any special handling. 
        // Simply continue with the next.
        /*********************************************************************/
        jam();
        break;
      case CS_ABORTING:
        /*********************************************************************/
        // This could actually mean that the API connection is already 
        // ready to release if the abortState is IDLE.
        /*********************************************************************/
        if (apiConnectptr.p->abortState == AS_IDLE) {
          jam();
          releaseApiCon(signal, apiConnectptr.i);
        } else {
          jam();
          capiConnectClosing[TapiFailedNode]++;
          apiConnectptr.p->apiFailState = ZTRUE;
        }//if
        break;
      case CS_WAIT_ABORT_CONF:
      case CS_WAIT_COMMIT_CONF:
      case CS_START_COMMITTING:
      case CS_PREPARE_TO_COMMIT:
      case CS_COMMITTING:
      case CS_COMMIT_SENT:
        /*********************************************************************/
        // These states indicate that an abort process or commit process is 
        // already ongoing. We will set a state in the api record indicating 
        // that the API node has failed.
        // Also we will increase the number of outstanding api records to 
        // wait for before we can respond with API_FAILCONF.
        /*********************************************************************/
        jam();
        capiConnectClosing[TapiFailedNode]++;
        apiConnectptr.p->apiFailState = ZTRUE;
        break;
      case CS_START_SCAN:
        /*********************************************************************/
        // The api record was performing a scan operation. We need to check 
        // on the scan state. Since completing a scan process might involve
        // sending several signals we will increase the loop count by 64.
        /*********************************************************************/
        jam();
unknown's avatar
unknown committed
993 994 995 996 997 998 999

	apiConnectptr.p->apiFailState = ZTRUE;
	capiConnectClosing[TapiFailedNode]++;

	ScanRecordPtr scanPtr;
	scanPtr.i = apiConnectptr.p->apiScanRec;
	ptrCheckGuard(scanPtr, cscanrecFileSize, scanRecord);
unknown's avatar
unknown committed
1000
	close_scan_req(signal, scanPtr, true);
unknown's avatar
unknown committed
1001
	
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
        TloopCount += 64;
        break;
      case CS_CONNECTED:
        /*********************************************************************/
        // The api record is connected to failed node. We need to release the 
        // connection and set it in a disconnected state.
        /*********************************************************************/
        jam();
        releaseApiCon(signal, apiConnectptr.i);
        break;
      case CS_REC_COMMITTING:
      case CS_RECEIVING:
      case CS_STARTED:
        /*********************************************************************/
        // The api record was in the process of performing a transaction but 
        // had not yet sent all information. 
        // We need to initiate an ABORT since the API will not provide any 
        // more information. 
        // Since the abort can send many signals we will insert a real-time
        // break after checking this record.
        /*********************************************************************/
        jam();
        apiConnectptr.p->apiFailState = ZTRUE;
        capiConnectClosing[TapiFailedNode]++;
        abort010Lab(signal);
        TloopCount = 256;
        break;
      case CS_PREPARED:
        jam();
      case CS_REC_PREPARING:
        jam();
      case CS_START_PREPARING:
        jam();
        /*********************************************************************/
        // Not implemented yet.
        /*********************************************************************/
1038
        systemErrorLab(signal, __LINE__);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
        break;
      case CS_RESTART:
        jam();
      case CS_COMPLETING:
        jam();
      case CS_COMPLETE_SENT:
        jam();
      case CS_WAIT_COMPLETE_CONF:
        jam();
      case CS_FAIL_ABORTING:
        jam();
      case CS_FAIL_ABORTED:
        jam();
      case CS_FAIL_PREPARED:
        jam();
      case CS_FAIL_COMMITTING:
        jam();
      case CS_FAIL_COMMITTED:
        /*********************************************************************/
        // These states are only valid on copy and fail API connections.
        /*********************************************************************/
      default:
        jam();
1062
        systemErrorLab(signal, __LINE__);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
        break;
      }//switch
    } else {
      jam();
    }//if
    apiConnectptr.i++;
    if (apiConnectptr.i > ((capiConnectFilesize / 3) - 1)) {
      jam();
      /**
       * Finished with scanning connection record
       *
       * Now scan markers
       */
unknown's avatar
unknown committed
1076
      removeMarkerForFailedAPI(signal, TapiFailedNode, 0);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
      return;
    }//if
  } while (TloopCount++ < 256);
  signal->theData[0] = TcContinueB::ZHANDLE_FAILED_API_NODE;
  signal->theData[1] = TapiFailedNode;
  signal->theData[2] = apiConnectptr.i;
  sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
}//Dbtc::handleFailedApiNode()

void
Dbtc::removeMarkerForFailedAPI(Signal* signal, 
                               Uint32 nodeId, 
                               Uint32 startBucket)
{
unknown's avatar
unknown committed
1091 1092 1093 1094
  TcFailRecordPtr node_fail_ptr;
  node_fail_ptr.i = 0;
  ptrAss(node_fail_ptr, tcFailRecord);
  if(node_fail_ptr.p->failStatus != FS_IDLE) {
1095
    jam();
unknown's avatar
unknown committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    DEBUG("Restarting removeMarkerForFailedAPI");
    /**
     * TC take-over in progress
     *   needs to restart as this
     *   creates new markers
     */
    signal->theData[0] = TcContinueB::ZHANDLE_FAILED_API_NODE_REMOVE_MARKERS;
    signal->theData[1] = nodeId;
    signal->theData[2] = 0;
    sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 500, 3);
    return;
1107
  }
unknown's avatar
unknown committed
1108 1109 1110

  CommitAckMarkerIterator iter;
  m_commitAckMarkerHash.next(startBucket, iter);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  
  const Uint32 RT_BREAK = 256;
  for(Uint32 i = 0; i<RT_BREAK || iter.bucket == startBucket; i++){ 
    jam();
    
    if(iter.curr.i == RNIL){
      jam();
      /**
       * Done with iteration
       */
      capiConnectClosing[nodeId]--;
      if (capiConnectClosing[nodeId] == 0) {
        jam();
        /********************************************************************/
        // No outstanding ABORT or COMMIT's of this failed API node. 
        // We can respond with API_FAILCONF
        /********************************************************************/
        signal->theData[0] = nodeId;
        signal->theData[1] = cownref;
        sendSignal(capiFailRef, GSN_API_FAILCONF, signal, 2, JBB);
      }
      return;
    }
    
    if(iter.curr.p->apiNodeId == nodeId){
      jam();
      
      /**
       * Check so that the record is not still in use
       *
       */
      ApiConnectRecordPtr apiConnectPtr;
      apiConnectPtr.i = iter.curr.p->apiConnectPtr;
      ptrCheckGuard(apiConnectPtr, capiConnectFilesize, apiConnectRecord);
      if(apiConnectPtr.p->commitAckMarker == iter.curr.i){
	jam();
        /**
         * The record is still active
         *
         * Don't remove it, but continueb instead
         */
unknown's avatar
unknown committed
1152
	break;
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 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 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
      }
      sendRemoveMarkers(signal, iter.curr.p);
      m_commitAckMarkerHash.release(iter.curr);
      
      break;
    } 
    m_commitAckMarkerHash.next(iter);
  }
  
  signal->theData[0] = TcContinueB::ZHANDLE_FAILED_API_NODE_REMOVE_MARKERS;
  signal->theData[1] = nodeId;
  signal->theData[2] = iter.bucket;
  sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
}

void Dbtc::handleApiFailState(Signal* signal, UintR TapiConnectptr)
{
  ApiConnectRecordPtr TlocalApiConnectptr;
  UintR TfailedApiNode;

  TlocalApiConnectptr.i = TapiConnectptr;
  ptrCheckGuard(TlocalApiConnectptr, capiConnectFilesize, apiConnectRecord);
  TfailedApiNode = refToNode(TlocalApiConnectptr.p->ndbapiBlockref);
  arrGuard(TfailedApiNode, MAX_NODES);
  capiConnectClosing[TfailedApiNode]--;
  releaseApiCon(signal, TapiConnectptr);
  TlocalApiConnectptr.p->apiFailState = ZFALSE;
  if (capiConnectClosing[TfailedApiNode] == 0) {
    jam();
    signal->theData[0] = TfailedApiNode;
    signal->theData[1] = cownref;
    sendSignal(capiFailRef, GSN_API_FAILCONF, signal, 2, JBB);
  }//if
}//Dbtc::handleApiFailState()

/****************************************************************************
 *                                T C S E I Z E R E Q
 * THE APPLICATION SENDS A REQUEST TO SEIZE A CONNECT RECORD TO CARRY OUT A 
 * TRANSACTION
 * TC BLOCK TAKE OUT A CONNECT RECORD FROM THE FREE LIST AND ESTABLISHES ALL 
 * NECESSARY CONNECTION BEFORE REPLYING TO THE APPLICATION BLOCK   
 ****************************************************************************/
void Dbtc::execTCSEIZEREQ(Signal* signal) 
{
  UintR tapiPointer;
  BlockReference tapiBlockref;       /* SENDER BLOCK REFERENCE*/
  
  jamEntry();
  tapiPointer = signal->theData[0]; /* REQUEST SENDERS CONNECT RECORD POINTER*/
  tapiBlockref = signal->theData[1]; /* SENDERS BLOCK REFERENCE*/
  
  const NodeState::StartLevel sl = 
    (NodeState::StartLevel)getNodeState().startLevel;

  const NodeId senderNodeId = refToNode(tapiBlockref);
  const bool local = senderNodeId == getOwnNodeId() || senderNodeId == 0;
  
  if(!(senderNodeId == getNodeState().getSingleUserApi()) &&
      !getNodeState().getSingleUserMode()) {
    if(!(sl==NodeState::SL_SINGLEUSER && 
	 senderNodeId == getNodeState().getSingleUserApi())) {
      if (!(sl == NodeState::SL_STARTED ||
	    (sl == NodeState::SL_STARTING && local == true))) {
	jam();
	
	Uint32 errCode;
	if(!(sl == NodeState::SL_SINGLEUSER && local))
	  {
	    switch(sl){
	    case NodeState::SL_STARTING:
	      errCode = ZSYSTEM_NOT_STARTED_ERROR;
	      break;
	    case NodeState::SL_STOPPING_1:
	    case NodeState::SL_STOPPING_2:
	    case NodeState::SL_STOPPING_3:
	    case NodeState::SL_STOPPING_4:
	      if(getNodeState().stopping.systemShutdown)
		errCode = ZCLUSTER_SHUTDOWN_IN_PROGRESS;
	      else
		errCode = ZNODE_SHUTDOWN_IN_PROGRESS;
	      break;
	    case NodeState::SL_SINGLEUSER:
	      errCode = ZCLUSTER_IN_SINGLEUSER_MODE;
	      break;
	    default:
	      errCode = ZWRONG_STATE;
	      break;
	    }
	    signal->theData[0] = tapiPointer;
	    signal->theData[1] = errCode;
	    sendSignal(tapiBlockref, GSN_TCSEIZEREF, signal, 2, JBB);
	    return;
	  }//if (!(sl == SL_SINGLEUSER))
      } //if
    }
  } 
  
  seizeApiConnect(signal);
  if (terrorCode == ZOK) {
    jam();
    apiConnectptr.p->ndbapiConnect = tapiPointer;
    apiConnectptr.p->ndbapiBlockref = tapiBlockref;
    signal->theData[0] = apiConnectptr.p->ndbapiConnect;
    signal->theData[1] = apiConnectptr.i;
    sendSignal(tapiBlockref, GSN_TCSEIZECONF, signal, 2, JBB);
    return;
  }

  signal->theData[0] = tapiPointer;
  signal->theData[1] = terrorCode;
  sendSignal(tapiBlockref, GSN_TCSEIZEREF, signal, 2, JBB);
}//Dbtc::execTCSEIZEREQ()

/****************************************************************************/
/*                    T C R E L E A S E Q                                   */
/*                  REQUEST TO RELEASE A CONNECT RECORD                     */
/****************************************************************************/
void Dbtc::execTCRELEASEREQ(Signal* signal) 
{
  UintR tapiPointer;
  BlockReference tapiBlockref;     /* SENDER BLOCK REFERENCE*/

  jamEntry();
  tapiPointer = signal->theData[0]; /* REQUEST SENDERS CONNECT RECORD POINTER*/
  tapiBlockref = signal->theData[1];/* SENDERS BLOCK REFERENCE*/
  tuserpointer = signal->theData[2];
  if (tapiPointer >= capiConnectFilesize) {
    jam();
    signal->theData[0] = tuserpointer;
    signal->theData[1] = ZINVALID_CONNECTION;
unknown's avatar
unknown committed
1283 1284
    signal->theData[2] = __LINE__;
    sendSignal(tapiBlockref, GSN_TCRELEASEREF, signal, 3, JBB);
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    return;
  } else {
    jam();
    apiConnectptr.i = tapiPointer;
  }//if
  ptrAss(apiConnectptr, apiConnectRecord);
  if (apiConnectptr.p->apiConnectstate == CS_DISCONNECTED) {
    jam();
    signal->theData[0] = tuserpointer;
    sendSignal(tapiBlockref, GSN_TCRELEASECONF, signal, 1, JBB);
  } else {
    if (tapiBlockref == apiConnectptr.p->ndbapiBlockref) {
unknown's avatar
unknown committed
1297 1298
      if (apiConnectptr.p->apiConnectstate == CS_CONNECTED ||
	  (apiConnectptr.p->apiConnectstate == CS_ABORTING &&
unknown's avatar
unknown committed
1299 1300 1301 1302
	   apiConnectptr.p->abortState == AS_IDLE) ||
	  (apiConnectptr.p->apiConnectstate == CS_STARTED &&
	   apiConnectptr.p->firstTcConnect == RNIL))
      {
1303 1304 1305
        jam();                                   /* JUST REPLY OK */
        releaseApiCon(signal, apiConnectptr.i);
        signal->theData[0] = tuserpointer;
unknown's avatar
unknown committed
1306
        sendSignal(tapiBlockref,
1307 1308 1309 1310 1311
                   GSN_TCRELEASECONF, signal, 1, JBB);
      } else {
        jam();
        signal->theData[0] = tuserpointer;
        signal->theData[1] = ZINVALID_CONNECTION;
unknown's avatar
unknown committed
1312 1313
	signal->theData[2] = __LINE__;
	signal->theData[3] = apiConnectptr.p->apiConnectstate;
unknown's avatar
unknown committed
1314
        sendSignal(tapiBlockref,
unknown's avatar
unknown committed
1315
                   GSN_TCRELEASEREF, signal, 4, JBB);
1316 1317 1318 1319 1320
      }
    } else {
      jam();
      signal->theData[0] = tuserpointer;
      signal->theData[1] = ZINVALID_CONNECTION;
unknown's avatar
unknown committed
1321 1322 1323 1324
      signal->theData[2] = __LINE__;
      signal->theData[3] = tapiBlockref;      
      signal->theData[4] = apiConnectptr.p->ndbapiBlockref;      
      sendSignal(tapiBlockref, GSN_TCRELEASEREF, signal, 5, JBB);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    }//if
  }//if
}//Dbtc::execTCRELEASEREQ()

/****************************************************************************/
// Error Handling for TCKEYREQ messages
/****************************************************************************/
void Dbtc::signalErrorRefuseLab(Signal* signal) 
{
  ptrGuard(apiConnectptr);
  if (apiConnectptr.p->apiConnectstate != CS_DISCONNECTED) {
    jam();
    apiConnectptr.p->abortState = AS_IDLE;
    apiConnectptr.p->apiConnectstate = CS_ABORTING;
  }//if
  sendSignalErrorRefuseLab(signal);
}//Dbtc::signalErrorRefuseLab()

void Dbtc::sendSignalErrorRefuseLab(Signal* signal) 
{
  ndbassert(false);
  ptrGuard(apiConnectptr);
  if (apiConnectptr.p->apiConnectstate != CS_DISCONNECTED) {
    jam();
unknown's avatar
unknown committed
1349
    ndbrequire(false);
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
    signal->theData[0] = apiConnectptr.p->ndbapiConnect;
    signal->theData[1] = signal->theData[ttransid_ptr];
    signal->theData[2] = signal->theData[ttransid_ptr + 1];
    signal->theData[3] = ZSIGNAL_ERROR;
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_TCROLLBACKREP, 
	       signal, 4, JBB);
  }
}//Dbtc::sendSignalErrorRefuseLab()

void Dbtc::abortBeginErrorLab(Signal* signal) 
{
  apiConnectptr.p->transid[0] = signal->theData[ttransid_ptr];
  apiConnectptr.p->transid[1] = signal->theData[ttransid_ptr + 1];
  abortErrorLab(signal);
}//Dbtc::abortBeginErrorLab()

void Dbtc::printState(Signal* signal, int place) 
{
#ifdef VM_TRACE // Change to if 0 to disable these printouts
  ndbout << "-- Dbtc::printState -- " << endl;
  ndbout << "Received from place = " << place
	 << " apiConnectptr.i = " << apiConnectptr.i
	 << " apiConnectstate = " << apiConnectptr.p->apiConnectstate << endl;
  ndbout << "ctcTimer = " << ctcTimer
	 << " ndbapiBlockref = " << hex <<apiConnectptr.p->ndbapiBlockref
	 << " Transid = " << apiConnectptr.p->transid[0]
	 << " " << apiConnectptr.p->transid[1] << endl;
  ndbout << " apiTimer = " << getApiConTimer(apiConnectptr.i)
	 << " counter = " << apiConnectptr.p->counter
	 << " lqhkeyconfrec = " << apiConnectptr.p->lqhkeyconfrec
	 << " lqhkeyreqrec = " << apiConnectptr.p->lqhkeyreqrec << endl;
unknown's avatar
unknown committed
1381
  ndbout << "abortState = " << apiConnectptr.p->abortState 
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	 << " apiScanRec = " << apiConnectptr.p->apiScanRec
	 << " returncode = " << apiConnectptr.p->returncode << endl;
  ndbout << "tckeyrec = " << apiConnectptr.p->tckeyrec
	 << " returnsignal = " << apiConnectptr.p->returnsignal
	 << " apiFailState = " << apiConnectptr.p->apiFailState << endl;
  if (apiConnectptr.p->cachePtr != RNIL) {
    jam();
    CacheRecord *localCacheRecord = cacheRecord;
    UintR TcacheFilesize = ccacheFilesize;
    UintR TcachePtr = apiConnectptr.p->cachePtr;
    if (TcachePtr < TcacheFilesize) {
      jam();
      CacheRecord * const regCachePtr = &localCacheRecord[TcachePtr];
      ndbout << "currReclenAi = " << regCachePtr->currReclenAi
	     << " attrlength = " << regCachePtr->attrlength
	     << " tableref = " << regCachePtr->tableref
	     << " keylen = " << regCachePtr->keylen << endl;
    } else {
      jam();
1401
      systemErrorLab(signal, __LINE__);
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
    }//if
  }//if
#endif
  return;
}//Dbtc::printState()

void
Dbtc::TCKEY_abort(Signal* signal, int place)
{
  switch (place) {
  case 0:
    jam();
    terrorCode = ZSTATE_ERROR;
    apiConnectptr.p->firstTcConnect = RNIL;
    printState(signal, 4);
    abortBeginErrorLab(signal);
    return;
  case 1:
    jam();
    printState(signal, 3);
    sendSignalErrorRefuseLab(signal);
    return;
  case 2:{
    printState(signal, 6);
    const TcKeyReq * const tcKeyReq = (TcKeyReq *)&signal->theData[0];
    const Uint32 t1 = tcKeyReq->transId1;
    const Uint32 t2 = tcKeyReq->transId2;
    signal->theData[0] = apiConnectptr.p->ndbapiConnect;
    signal->theData[1] = t1;
    signal->theData[2] = t2;
    signal->theData[3] = ZABORT_ERROR;
unknown's avatar
unknown committed
1433
    ndbrequire(false);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_TCROLLBACKREP, 
	       signal, 4, JBB);
    return;
  }
  case 3:
    jam();
    printState(signal, 7);
    noFreeConnectionErrorLab(signal);
    return;
  case 4:
    jam();
    terrorCode = ZERO_KEYLEN_ERROR;
    releaseAtErrorLab(signal);
    return;
  case 5:
    jam();
    terrorCode = ZNO_AI_WITH_UPDATE;
    releaseAtErrorLab(signal);
    return;
  case 6:
    jam();
1455
    warningHandlerLab(signal, __LINE__);
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    return;

  case 7:
    jam();
    tabStateErrorLab(signal);
    return;

  case 8:
    jam();
    wrongSchemaVersionErrorLab(signal);
    return;

  case 9:
    jam();
    terrorCode = ZSTATE_ERROR;
    releaseAtErrorLab(signal);
    return;

  case 10:
    jam();
1476
    systemErrorLab(signal, __LINE__);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
    return;

  case 11:
    jam();
    terrorCode = ZMORE_AI_IN_TCKEYREQ_ERROR;
    releaseAtErrorLab(signal);
    return;

  case 12:
    jam();
    terrorCode = ZSIMPLE_READ_WITHOUT_AI;
    releaseAtErrorLab(signal);
    return;

  case 13:
    jam();
    switch (tcConnectptr.p->tcConnectstate) {
    case OS_WAIT_KEYINFO:
      jam();
      printState(signal, 8);
      terrorCode = ZSTATE_ERROR;
      abortErrorLab(signal);
      return;
    default:
      jam();
      /********************************************************************/
      /*       MISMATCH BETWEEN STATE ON API CONNECTION AND THIS          */
      /*       PARTICULAR TC CONNECT RECORD. THIS MUST BE CAUSED BY NDB   */
      /*       INTERNAL ERROR.                                            */
      /********************************************************************/
1507
      systemErrorLab(signal, __LINE__);
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
      return;
    }//switch
    return;

  case 15:
    jam();
    terrorCode = ZSCAN_NODE_ERROR;
    releaseAtErrorLab(signal);
    return;

  case 16:
    jam();
1520
    systemErrorLab(signal, __LINE__);
1521 1522 1523 1524
    return;

  case 17:
    jam();
1525
    systemErrorLab(signal, __LINE__);
1526 1527 1528 1529
    return;

  case 18:
    jam();
1530
    warningHandlerLab(signal, __LINE__);
1531 1532 1533 1534 1535 1536 1537 1538
    return;

  case 19:
    jam();
    return;

  case 20:
    jam();
1539
    warningHandlerLab(signal, __LINE__);
1540 1541 1542 1543
    return;

  case 21:
    jam();
1544
    systemErrorLab(signal, __LINE__);
1545 1546 1547 1548
    return;

  case 22:
    jam();
1549
    systemErrorLab(signal, __LINE__);
1550 1551 1552 1553
    return;

  case 23:
    jam();
1554
    systemErrorLab(signal, __LINE__);
1555 1556 1557 1558 1559 1560 1561 1562 1563
    return;

  case 24:
    jam();
    seizeAttrbuferrorLab(signal);
    return;

  case 25:
    jam();
1564
    warningHandlerLab(signal, __LINE__);
1565 1566 1567 1568 1569 1570 1571
    return;

  case 26:
    jam();
    return;

  case 27:
1572
    systemErrorLab(signal, __LINE__);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    jam();
    return;

  case 28:
    jam();
    // NOT USED
    return;

  case 29:
    jam();
1583
    systemErrorLab(signal, __LINE__);
1584 1585 1586 1587
    return;

  case 30:
    jam();
1588
    systemErrorLab(signal, __LINE__);
1589 1590 1591 1592
    return;

  case 31:
    jam();
1593
    systemErrorLab(signal, __LINE__);
1594 1595 1596 1597
    return;

  case 32:
    jam();
1598
    systemErrorLab(signal, __LINE__);
1599 1600 1601 1602
    return;

  case 33:
    jam();
1603
    systemErrorLab(signal, __LINE__);
1604 1605 1606 1607
    return;

  case 34:
    jam();
1608
    systemErrorLab(signal, __LINE__);
1609 1610 1611 1612
    return;

  case 35:
    jam();
1613
    systemErrorLab(signal, __LINE__);
1614 1615 1616 1617
    return;

  case 36:
    jam();
1618
    systemErrorLab(signal, __LINE__);
1619 1620 1621 1622
    return;

  case 37:
    jam();
1623
    systemErrorLab(signal, __LINE__);
1624 1625 1626 1627
    return;

  case 38:
    jam();
1628
    systemErrorLab(signal, __LINE__);
1629 1630 1631 1632
    return;

  case 39:
    jam();
1633
    systemErrorLab(signal, __LINE__);
1634 1635 1636 1637
    return;

  case 40:
    jam();
1638
    systemErrorLab(signal, __LINE__);
1639 1640 1641 1642
    return;

  case 41:
    jam();
1643
    systemErrorLab(signal, __LINE__);
1644 1645 1646 1647
    return;

  case 42:
    jam();
1648
    systemErrorLab(signal, __LINE__);
1649 1650 1651 1652
    return;

  case 43:
    jam();
1653
    systemErrorLab(signal, __LINE__);
1654 1655 1656 1657
    return;

  case 44:
    jam();
1658
    systemErrorLab(signal, __LINE__);
1659 1660 1661 1662
    return;

  case 45:
    jam();
1663
    systemErrorLab(signal, __LINE__);
1664 1665 1666 1667
    return;

  case 46:
    jam();
1668
    systemErrorLab(signal, __LINE__);
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
    return;

  case 47:
    jam();
    terrorCode = apiConnectptr.p->returncode;
    releaseAtErrorLab(signal);
    return;

  case 48:
    jam();
    terrorCode = ZCOMMIT_TYPE_ERROR;
    releaseAtErrorLab(signal);
    return;

  case 49:
    jam();
    abortErrorLab(signal);
    return;

  case 50:
    jam();
1690
    systemErrorLab(signal, __LINE__);
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
    return;

  case 51:
    jam();
    abortErrorLab(signal);
    return;

  case 52:
    jam();
    abortErrorLab(signal);
    return;

  case 53:
    jam();
    abortErrorLab(signal);
    return;

  case 54:
    jam();
    abortErrorLab(signal);
    return;

  case 55:
    jam();
    printState(signal, 5);
    sendSignalErrorRefuseLab(signal);
    return;
    
  case 56:{
    jam();
    terrorCode = ZNO_FREE_TC_MARKER;
    abortErrorLab(signal);
    return;
  }
  case 57:{
    jam();
    /**
     * Initialize object before starting error handling
     */
    initApiConnectRec(signal, apiConnectptr.p, true);
    switch(getNodeState().startLevel){
    case NodeState::SL_STOPPING_2:
    case NodeState::SL_STOPPING_3:
    case NodeState::SL_STOPPING_4:
      if(getNodeState().stopping.systemShutdown)
	terrorCode  = ZCLUSTER_SHUTDOWN_IN_PROGRESS;
      else
	terrorCode = ZNODE_SHUTDOWN_IN_PROGRESS;
      break;
    case NodeState::SL_SINGLEUSER:
      terrorCode  = ZCLUSTER_IN_SINGLEUSER_MODE;
      break;
    default:
      terrorCode = ZWRONG_STATE;
      break;
    }
    abortErrorLab(signal);
    return;
  }

  case 58:{
    jam();
    releaseAtErrorLab(signal);
    return;
  }

unknown's avatar
unknown committed
1757 1758
  case 59:{
    jam();
1759 1760
    terrorCode = ZABORTINPROGRESS;
    abortErrorLab(signal);
unknown's avatar
unknown committed
1761 1762 1763
    return;
  }
    
1764 1765
  default:
    jam();
1766
    systemErrorLab(signal, __LINE__);
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
    return;
  }//switch
}

void Dbtc::execKEYINFO(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;
  jamEntry();
  apiConnectptr.i = signal->theData[0];
  tmaxData = 20;
  if (apiConnectptr.i >= capiConnectFilesize) {
    jam();
1779
    warningHandlerLab(signal, __LINE__);
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
    return;
  }//if
  ptrAss(apiConnectptr, apiConnectRecord);
  ttransid_ptr = 1;
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[1];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[2];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    jam();
    printState(signal, 10);
    sendSignalErrorRefuseLab(signal);
    return;
  }//if
  switch (apiConnectptr.p->apiConnectstate) {
  case CS_RECEIVING:
  case CS_REC_COMMITTING:
1796
  case CS_START_SCAN:
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
    jam();
    /*empty*/;
    break;
                /* OK */
  case CS_ABORTING:
    jam();
    return;     /* IGNORE */
  case CS_CONNECTED:
    jam();
    /****************************************************************>*/
    /*       MOST LIKELY CAUSED BY A MISSED SIGNAL. SEND REFUSE AND   */
    /*       SET STATE TO ABORTING.                                   */
    /****************************************************************>*/
    printState(signal, 11);
    signalErrorRefuseLab(signal);
    return;
  case CS_STARTED:
    jam();
    /****************************************************************>*/
    /*       MOST LIKELY CAUSED BY A MISSED SIGNAL. SEND REFUSE AND   */
    /*       SET STATE TO ABORTING. SINCE A TRANSACTION WAS STARTED   */
    /*       WE ALSO NEED TO ABORT THIS TRANSACTION.                  */
    /****************************************************************>*/
    terrorCode = ZSIGNAL_ERROR;
    printState(signal, 2);
    abortErrorLab(signal);
    return;
  default:
    jam();
1826
    warningHandlerLab(signal, __LINE__);
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
    return;
  }//switch

  CacheRecord *localCacheRecord = cacheRecord;
  UintR TcacheFilesize = ccacheFilesize;
  UintR TcachePtr = apiConnectptr.p->cachePtr;
  UintR TtcTimer = ctcTimer;
  CacheRecord * const regCachePtr = &localCacheRecord[TcachePtr];
  if (TcachePtr >= TcacheFilesize) {
    TCKEY_abort(signal, 42);
    return;
  }//if
  setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);
  cachePtr.i = TcachePtr;
  cachePtr.p = regCachePtr;

  tcConnectptr.i = apiConnectptr.p->lastTcConnect;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  switch (tcConnectptr.p->tcConnectstate) {
  case OS_WAIT_KEYINFO:
    jam();
    tckeyreq020Lab(signal);
    return;
1850 1851
  case OS_WAIT_SCAN:
    break;
1852 1853 1854 1855 1856 1857
  default:
    jam();
    terrorCode = ZSTATE_ERROR;
    abortErrorLab(signal);
    return;
  }//switch
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896

  UintR TdataPos = 0;
  UintR TkeyLen = regCachePtr->keylen;
  UintR Tlen = regCachePtr->save1;

  do {
    if (cfirstfreeDatabuf == RNIL) {
      jam();
      seizeDatabuferrorLab(signal);
      return;
    }//if
    linkKeybuf(signal);
    arrGuard(TdataPos, 19);
    databufptr.p->data[0] = signal->theData[TdataPos + 3];
    databufptr.p->data[1] = signal->theData[TdataPos + 4];
    databufptr.p->data[2] = signal->theData[TdataPos + 5];
    databufptr.p->data[3] = signal->theData[TdataPos + 6];
    Tlen = Tlen + 4;
    TdataPos = TdataPos + 4;
    if (Tlen < TkeyLen) {
      jam();
      if (TdataPos >= tmaxData) {
        jam();
	/*----------------------------------------------------*/
	/** EXIT AND WAIT FOR SIGNAL KEYINFO OR KEYINFO9     **/
	/** WHEN EITHER OF THE SIGNALS IS RECEIVED A JUMP    **/
	/** TO LABEL "KEYINFO_LABEL" IS DONE. THEN THE       **/
	/** PROGRAM RETURNS TO LABEL TCKEYREQ020             **/
	/*----------------------------------------------------*/
        setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
        regCachePtr->save1 = Tlen;
        return;
      }//if
    } else {
      jam();
      return;
    }//if
  } while (1);
  return;
1897 1898 1899 1900 1901 1902 1903 1904
}//Dbtc::execKEYINFO()

/*---------------------------------------------------------------------------*/
/*                                                                           */
/* MORE THAN FOUR WORDS OF KEY DATA. WE NEED TO PACK THIS IN KEYINFO SIGNALS.*/
/* WE WILL ALWAYS PACK 4 WORDS AT A TIME.                                    */
/*---------------------------------------------------------------------------*/
void Dbtc::packKeyData000Lab(Signal* signal,
1905 1906
                             BlockReference TBRef,
			     Uint32 totalLen) 
1907 1908 1909 1910
{
  CacheRecord * const regCachePtr = cachePtr.p;

  jam();
1911
  Uint32 len = 0;
1912
  databufptr.i = regCachePtr->firstKeybuf;
1913 1914 1915 1916 1917 1918
  signal->theData[0] = tcConnectptr.i;
  signal->theData[1] = apiConnectptr.p->transid[0];
  signal->theData[2] = apiConnectptr.p->transid[1];
  Uint32 * dst = signal->theData+3;
  ptrCheckGuard(databufptr, cdatabufFilesize, databufRecord);
  
1919 1920
  do {
    jam();
1921 1922 1923 1924 1925 1926 1927
    databufptr.i = databufptr.p->nextDatabuf;
    dst[len + 0] = databufptr.p->data[0];
    dst[len + 1] = databufptr.p->data[1];
    dst[len + 2] = databufptr.p->data[2];
    dst[len + 3] = databufptr.p->data[3];
    len += 4;
    if (totalLen <= 4) {
1928 1929 1930 1931 1932 1933
      jam();
      /*---------------------------------------------------------------------*/
      /*       LAST PACK OF KEY DATA HAVE BEEN SENT                          */
      /*---------------------------------------------------------------------*/
      /*       THERE WERE UNSENT INFORMATION, SEND IT.                       */
      /*---------------------------------------------------------------------*/
1934
      sendSignal(TBRef, GSN_KEYINFO, signal, 3 + len, JBB);
1935
      return;
1936 1937 1938 1939 1940 1941 1942
    } else if(len == KeyInfo::DataLength){
      jam();
      len = 0;
      sendSignal(TBRef, GSN_KEYINFO, signal, 3 + KeyInfo::DataLength, JBB);
    }
    totalLen -= 4;
    ptrCheckGuard(databufptr, cdatabufFilesize, databufRecord);
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 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 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
  } while (1);
}//Dbtc::packKeyData000Lab()

void Dbtc::tckeyreq020Lab(Signal* signal) 
{
  CacheRecord * const regCachePtr = cachePtr.p;
  UintR TdataPos = 0;
  UintR TkeyLen = regCachePtr->keylen;
  UintR Tlen = regCachePtr->save1;

  do {
    if (cfirstfreeDatabuf == RNIL) {
      jam();
      seizeDatabuferrorLab(signal);
      return;
    }//if
    linkKeybuf(signal);
    arrGuard(TdataPos, 19);
    databufptr.p->data[0] = signal->theData[TdataPos + 3];
    databufptr.p->data[1] = signal->theData[TdataPos + 4];
    databufptr.p->data[2] = signal->theData[TdataPos + 5];
    databufptr.p->data[3] = signal->theData[TdataPos + 6];
    Tlen = Tlen + 4;
    TdataPos = TdataPos + 4;
    if (Tlen < TkeyLen) {
      jam();
      if (TdataPos >= tmaxData) {
        jam();
	/*----------------------------------------------------*/
	/** EXIT AND WAIT FOR SIGNAL KEYINFO OR KEYINFO9     **/
	/** WHEN EITHER OF THE SIGNALS IS RECEIVED A JUMP    **/
	/** TO LABEL "KEYINFO_LABEL" IS DONE. THEN THE       **/
	/** PROGRAM RETURNS TO LABEL TCKEYREQ020             **/
	/*----------------------------------------------------*/
        setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
        regCachePtr->save1 = Tlen;
        tcConnectptr.p->tcConnectstate = OS_WAIT_KEYINFO;
        return;
      }//if
    } else {
      jam();
      tckeyreq050Lab(signal);
      return;
    }//if
  } while (1);
  return;
}//Dbtc::tckeyreq020Lab()

/* ------------------------------------------------------------------------- */
/* -------        SAVE ATTRIBUTE INFORMATION IN OPERATION RECORD     ------- */
/* ------------------------------------------------------------------------- */
void Dbtc::saveAttrbuf(Signal* signal) 
{
  CacheRecord * const regCachePtr = cachePtr.p;
  UintR TfirstfreeAttrbuf = cfirstfreeAttrbuf;
  UintR TattrbufFilesize = cattrbufFilesize;
  UintR TTcfirstAttrbuf = regCachePtr->firstAttrbuf;
  UintR Tlen = signal->length() - 3;
  AttrbufRecord *localAttrbufRecord = attrbufRecord;

  AttrbufRecord * const regAttrPtr = &localAttrbufRecord[TfirstfreeAttrbuf];
  if (TfirstfreeAttrbuf >= TattrbufFilesize) {
    TCKEY_abort(signal, 21);
    return;
  }//if
  UintR Tnext = regAttrPtr->attrbuf[ZINBUF_NEXT];
  if (TTcfirstAttrbuf == RNIL) {
    jam();
    regCachePtr->firstAttrbuf = TfirstfreeAttrbuf;
  } else {
    AttrbufRecordPtr saAttrbufptr;

    saAttrbufptr.i = regCachePtr->lastAttrbuf;
    jam();
    if (saAttrbufptr.i >= TattrbufFilesize) {
      TCKEY_abort(signal, 22);
      return;
    }//if
    saAttrbufptr.p = &localAttrbufRecord[saAttrbufptr.i];
    saAttrbufptr.p->attrbuf[ZINBUF_NEXT] = TfirstfreeAttrbuf;
  }//if

  cfirstfreeAttrbuf = Tnext;
  regAttrPtr->attrbuf[ZINBUF_NEXT] = RNIL;
  regCachePtr->lastAttrbuf = TfirstfreeAttrbuf;
  regAttrPtr->attrbuf[ZINBUF_DATA_LEN] = Tlen;

  UintR Tdata1 = signal->theData[3];
  UintR Tdata2 = signal->theData[4];
  UintR Tdata3 = signal->theData[5];
  UintR Tdata4 = signal->theData[6];
  UintR Tdata5 = signal->theData[7];
  UintR Tdata6 = signal->theData[8];
  UintR Tdata7 = signal->theData[9];
  UintR Tdata8 = signal->theData[10];

  regAttrPtr->attrbuf[0] = Tdata1;
  regAttrPtr->attrbuf[1] = Tdata2;
  regAttrPtr->attrbuf[2] = Tdata3;
  regAttrPtr->attrbuf[3] = Tdata4;
  regAttrPtr->attrbuf[4] = Tdata5;
  regAttrPtr->attrbuf[5] = Tdata6;
  regAttrPtr->attrbuf[6] = Tdata7;
  regAttrPtr->attrbuf[7] = Tdata8;

  if (Tlen > 8) {

    Tdata1 = signal->theData[11];
    Tdata2 = signal->theData[12];
    Tdata3 = signal->theData[13];
    Tdata4 = signal->theData[14];
    Tdata5 = signal->theData[15];
    Tdata6 = signal->theData[16];
    Tdata7 = signal->theData[17];

    regAttrPtr->attrbuf[8] = Tdata1;
    regAttrPtr->attrbuf[9] = Tdata2;
    regAttrPtr->attrbuf[10] = Tdata3;
    regAttrPtr->attrbuf[11] = Tdata4;
    regAttrPtr->attrbuf[12] = Tdata5;
    regAttrPtr->attrbuf[13] = Tdata6;
    regAttrPtr->attrbuf[14] = Tdata7;
    jam();
    if (Tlen > 15) {

      Tdata1 = signal->theData[18];
      Tdata2 = signal->theData[19];
      Tdata3 = signal->theData[20];
      Tdata4 = signal->theData[21];
      Tdata5 = signal->theData[22];
      Tdata6 = signal->theData[23];
      Tdata7 = signal->theData[24];

      jam();
      regAttrPtr->attrbuf[15] = Tdata1;
      regAttrPtr->attrbuf[16] = Tdata2;
      regAttrPtr->attrbuf[17] = Tdata3;
      regAttrPtr->attrbuf[18] = Tdata4;
      regAttrPtr->attrbuf[19] = Tdata5;
      regAttrPtr->attrbuf[20] = Tdata6;
      regAttrPtr->attrbuf[21] = Tdata7;
    }//if
  }//if
}//Dbtc::saveAttrbuf()

void Dbtc::execATTRINFO(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;
  UintR Tdata1 = signal->theData[0];
  UintR Tlength = signal->length();
  UintR TapiConnectFilesize = capiConnectFilesize;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  jamEntry();
  apiConnectptr.i = Tdata1;
  ttransid_ptr = 1;
  if (Tdata1 >= TapiConnectFilesize) {
    DEBUG("Drop ATTRINFO, wrong apiConnectptr");
    TCKEY_abort(signal, 18);
    return;
  }//if

  UintR Tdata2 = signal->theData[1];
  UintR Tdata3 = signal->theData[2];
  ApiConnectRecord * const regApiPtr = &localApiConnectRecord[Tdata1];
  compare_transid1 = regApiPtr->transid[0] ^ Tdata2;
  compare_transid2 = regApiPtr->transid[1] ^ Tdata3;
  apiConnectptr.p = regApiPtr;
  compare_transid1 = compare_transid1 | compare_transid2;

  if (compare_transid1 != 0) {
    DEBUG("Drop ATTRINFO, wrong transid, lenght="<<Tlength
	  << " transid("<<hex<<Tdata2<<", "<<Tdata3);
    TCKEY_abort(signal, 19);
    return;
  }//if
  if (Tlength < 4) {
    DEBUG("Drop ATTRINFO, wrong length = " << Tlength);
    TCKEY_abort(signal, 20);
    return;
  }
  Tlength -= 3;
  UintR TcompREC_COMMIT = (regApiPtr->apiConnectstate == CS_REC_COMMITTING);
  UintR TcompRECEIVING = (regApiPtr->apiConnectstate == CS_RECEIVING);
  UintR TcompBOTH = TcompREC_COMMIT | TcompRECEIVING;

  if (TcompBOTH) {
    jam();
    if (ERROR_INSERTED(8015)) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
    if (ERROR_INSERTED(8016)) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
    CacheRecord *localCacheRecord = cacheRecord;
    UintR TcacheFilesize = ccacheFilesize;
    UintR TcachePtr = regApiPtr->cachePtr;
    UintR TtcTimer = ctcTimer;
    CacheRecord * const regCachePtr = &localCacheRecord[TcachePtr];
    if (TcachePtr >= TcacheFilesize) {
      TCKEY_abort(signal, 43);
      return;
    }//if
    UintR TfirstfreeAttrbuf = cfirstfreeAttrbuf;
    UintR TcurrReclenAi = regCachePtr->currReclenAi;
    UintR TattrLen = regCachePtr->attrlength;

    setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);
    cachePtr.i = TcachePtr;
    cachePtr.p = regCachePtr;
    TcurrReclenAi = TcurrReclenAi + Tlength;
    regCachePtr->currReclenAi = TcurrReclenAi;
    int TattrlengthRemain = TattrLen - TcurrReclenAi;

    if (TfirstfreeAttrbuf == RNIL) {
      DEBUG("No more attrinfo buffers");
      TCKEY_abort(signal, 24);
      return;
    }//if
    saveAttrbuf(signal);
    if (TattrlengthRemain == 0) {
      /****************************************************************>*/
      /* HERE WE HAVE FOUND THAT THE LAST SIGNAL BELONGING TO THIS       */
      /* OPERATION HAVE BEEN RECEIVED. THIS MEANS THAT WE CAN NOW REUSE */
      /* THE API CONNECT RECORD. HOWEVER IF PREPARE OR COMMIT HAVE BEEN */
      /* RECEIVED THEN IT IS NOT ALLOWED TO RECEIVE ANY FURTHER          */
      /* OPERATIONS.                                                     */
      /****************************************************************>*/
      UintR TlastConnect = regApiPtr->lastTcConnect;
      if (TcompRECEIVING) {
        jam();
        regApiPtr->apiConnectstate = CS_STARTED;
      } else {
        jam();
        regApiPtr->apiConnectstate = CS_START_COMMITTING;
      }//if
      tcConnectptr.i = TlastConnect;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      attrinfoDihReceivedLab(signal);
    } else if (TattrlengthRemain < 0) {
      jam();
      DEBUG("ATTRINFO wrong total length="<<Tlength
	    <<", TattrlengthRemain="<<TattrlengthRemain
	    <<", TattrLen="<<TattrLen
	    <<", TcurrReclenAi="<<TcurrReclenAi);
      tcConnectptr.i = regApiPtr->lastTcConnect;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      aiErrorLab(signal);
    }//if
    return;
  } else if (regApiPtr->apiConnectstate == CS_START_SCAN) {
    jam();
    scanAttrinfoLab(signal, Tlength);
    return;
  } else {
    switch (regApiPtr->apiConnectstate) {
    case CS_ABORTING:
      jam();
      /* JUST IGNORE THE SIGNAL*/
      // DEBUG("Drop ATTRINFO, CS_ABORTING"); 
      return;
    case CS_CONNECTED:
      jam();
      /* MOST LIKELY CAUSED BY A MISSED SIGNAL.*/
      // DEBUG("Drop ATTRINFO, CS_CONNECTED"); 
      return;
    case CS_STARTED:
      jam();
      /****************************************************************>*/
      /*       MOST LIKELY CAUSED BY A MISSED SIGNAL. SEND REFUSE AND   */
      /*       SET STATE TO ABORTING. SINCE A TRANSACTION WAS STARTED   */
      /*       WE ALSO NEED TO ABORT THIS TRANSACTION.                  */
      /****************************************************************>*/
      terrorCode = ZSIGNAL_ERROR;
      printState(signal, 1);
      abortErrorLab(signal);
      return;
    default:
      jam();
      /****************************************************************>*/
      /*       SIGNAL RECEIVED IN AN UNEXPECTED STATE. WE IGNORE SIGNAL */
      /*       SINCE WE DO NOT REALLY KNOW WHERE THE ERROR OCCURRED.    */
      /****************************************************************>*/
      DEBUG("Drop ATTRINFO, illegal state="<<regApiPtr->apiConnectstate); 
      printState(signal, 9);
      return;
    }//switch
  }//if
}//Dbtc::execATTRINFO()

/* *********************************************************************>> */
/*                                                                        */
/*       MODULE: HASH MODULE                                              */
/*       DESCRIPTION: CONTAINS THE HASH VALUE CALCULATION                 */
/* *********************************************************************> */
void Dbtc::hash(Signal* signal) 
{
  DatabufRecordPtr locDatabufptr;
  UintR ti;
  UintR  Tdata0;
  UintR  Tdata1;
  UintR  Tdata2;
  UintR  Tdata3;
  UintR*  Tdata32;
2249
  
2250
  CacheRecord * const regCachePtr = cachePtr.p;
2251 2252
  Tdata32 = signal->theData;
  
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
  Tdata0 = regCachePtr->keydata[0];
  Tdata1 = regCachePtr->keydata[1];
  Tdata2 = regCachePtr->keydata[2];
  Tdata3 = regCachePtr->keydata[3];
  Tdata32[0] = Tdata0;
  Tdata32[1] = Tdata1;
  Tdata32[2] = Tdata2;
  Tdata32[3] = Tdata3;
  if (regCachePtr->keylen > 4) {
    locDatabufptr.i = regCachePtr->firstKeybuf;
    ti = 4;
    while (locDatabufptr.i != RNIL) {
      ptrCheckGuard(locDatabufptr, cdatabufFilesize, databufRecord);
      Tdata0 = locDatabufptr.p->data[0];
      Tdata1 = locDatabufptr.p->data[1];
      Tdata2 = locDatabufptr.p->data[2];
      Tdata3 = locDatabufptr.p->data[3];
      Tdata32[ti    ] = Tdata0;
      Tdata32[ti + 1] = Tdata1;
      Tdata32[ti + 2] = Tdata2;
      Tdata32[ti + 3] = Tdata3;
      locDatabufptr.i = locDatabufptr.p->nextDatabuf;
      ti += 4;
    }//while
  }//if
unknown's avatar
unknown committed
2278 2279

  UintR keylen = (UintR)regCachePtr->keylen;
2280 2281
  Uint32 distKey = regCachePtr->distributionKeyIndicator;
  
unknown's avatar
unknown committed
2282
  Uint32 tmp[4];
2283 2284 2285 2286 2287 2288 2289 2290 2291
  if(!regCachePtr->m_special_hash)
  {
    md5_hash(tmp, (Uint64*)&Tdata32[0], keylen);
  }
  else
  {
    handle_special_hash(tmp, Tdata32, keylen, regCachePtr->tableref, !distKey);
  }
  
unknown's avatar
unknown committed
2292
  thashValue = tmp[0];
2293
  if (distKey){
2294
    jam();
unknown's avatar
unknown committed
2295
    tdistrHashValue = regCachePtr->distributionKey;
2296 2297
  } else {
    jam();
unknown's avatar
unknown committed
2298
    tdistrHashValue = tmp[1];
2299 2300 2301
  }//if
}//Dbtc::hash()

unknown's avatar
unknown committed
2302
bool
2303 2304 2305 2306
Dbtc::handle_special_hash(Uint32 dstHash[4], Uint32* src, Uint32 srcLen, 
			  Uint32 tabPtrI,
			  bool distr)
{
2307
  Uint64 Tmp[MAX_KEY_SIZE_IN_WORDS * MAX_XFRM_MULTIPLY];
2308 2309
  const TableRecord* tabPtrP = &tableRecord[tabPtrI];
  const bool hasCharAttr = tabPtrP->hasCharAttr;
2310
  const bool hasDistKeys = tabPtrP->noOfDistrKeys > 0;
2311 2312
  
  Uint32 *dst = (Uint32*)Tmp;
unknown's avatar
unknown committed
2313
  Uint32 dstPos = 0;
2314
  Uint32 keyPartLen[MAX_ATTRIBUTES_IN_INDEX];
2315 2316 2317 2318 2319
  Uint32 * keyPartLenPtr;
  if(hasCharAttr)
  {
    keyPartLenPtr = keyPartLen;
    dstPos = xfrm_key(tabPtrI, src, dst, sizeof(Tmp) >> 2, keyPartLenPtr);
2320 2321 2322 2323
    if (unlikely(dstPos == 0))
    {
      goto error;
    }
2324 2325 2326 2327 2328
  } 
  else 
  {
    dst = src;
    dstPos = srcLen;
2329
    keyPartLenPtr = 0;
2330 2331 2332 2333
  }
  
  md5_hash(dstHash, (Uint64*)dst, dstPos);
  
2334
  if(distr && hasDistKeys)
2335 2336
  {
    jam();
2337
    
2338
    Uint32 tmp[4];
2339 2340
    Uint32 len = create_distr_key(tabPtrI, dst, keyPartLenPtr);
    md5_hash(tmp, (Uint64*)dst, len);
2341
    dstHash[1] = tmp[1];
unknown's avatar
unknown committed
2342
  }
unknown's avatar
unknown committed
2343
  return true;  // success
2344 2345 2346 2347

error:
  terrorCode = ZINVALID_KEY;
  return false;
unknown's avatar
unknown committed
2348 2349
}

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
/*
INIT_API_CONNECT_REC
---------------------------
*/
/* ========================================================================= */
/* =======                       INIT_API_CONNECT_REC                ======= */
/*                                                                           */
/* ========================================================================= */
void Dbtc::initApiConnectRec(Signal* signal,
                             ApiConnectRecord * const regApiPtr,
			     bool releaseIndexOperations) 
{
  const TcKeyReq * const tcKeyReq = (TcKeyReq *)&signal->theData[0];
  UintR TfailureNr = cfailure_nr;
2364
  UintR TtransCount = c_counters.ctransCount;
2365 2366 2367
  UintR Ttransid0 = tcKeyReq->transId1;
  UintR Ttransid1 = tcKeyReq->transId2;

2368 2369
  regApiPtr->m_exec_flag = 0;
  regApiPtr->returncode = 0;
2370
  regApiPtr->returnsignal = RS_TCKEYCONF;
unknown's avatar
unknown committed
2371
  ndbassert(regApiPtr->firstTcConnect == RNIL);
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
  regApiPtr->firstTcConnect = RNIL;
  regApiPtr->lastTcConnect = RNIL;
  regApiPtr->globalcheckpointid = 0;
  regApiPtr->lqhkeyconfrec = 0;
  regApiPtr->lqhkeyreqrec = 0;
  regApiPtr->tckeyrec = 0;
  regApiPtr->tcindxrec = 0;
  regApiPtr->failureNr = TfailureNr;
  regApiPtr->transid[0] = Ttransid0;
  regApiPtr->transid[1] = Ttransid1;
  regApiPtr->commitAckMarker = RNIL;
  regApiPtr->buddyPtr = RNIL;
  regApiPtr->currSavePointId = 0;
unknown's avatar
unknown committed
2385
  regApiPtr->m_transaction_nodes.clear();
2386 2387 2388 2389 2390 2391 2392 2393
  // Trigger data
  releaseFiredTriggerData(&regApiPtr->theFiredTriggers),
  // Index data
  regApiPtr->indexOpReturn = false;
  regApiPtr->noIndexOp = 0;
  if(releaseIndexOperations)
    releaseAllSeizedIndexOperations(regApiPtr);

2394
  c_counters.ctransCount = TtransCount + 1;
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
}//Dbtc::initApiConnectRec()

int
Dbtc::seizeTcRecord(Signal* signal)
{
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  UintR TfirstfreeTcConnect = cfirstfreeTcConnect;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  tcConnectptr.i = TfirstfreeTcConnect;
  if (TfirstfreeTcConnect >= TtcConnectFilesize) {
    int place = 3;
    if (TfirstfreeTcConnect != RNIL) {
      place = 10;
    }//if
    TCKEY_abort(signal, place);
    return 1;
  }//if
  //--------------------------------------------------------------------------
  // Optimised version of ptrAss(tcConnectptr, tcConnectRecord)
  //--------------------------------------------------------------------------
  TcConnectRecord * const regTcPtr = 
                           &localTcConnectRecord[TfirstfreeTcConnect];

2419
  UintR TconcurrentOp = c_counters.cconcurrentOp;
2420 2421 2422 2423 2424 2425 2426
  UintR TlastTcConnect = regApiPtr->lastTcConnect;
  UintR TtcConnectptrIndex = tcConnectptr.i;
  TcConnectRecordPtr tmpTcConnectptr;

  cfirstfreeTcConnect = regTcPtr->nextTcConnect;
  tcConnectptr.p = regTcPtr;

2427
  c_counters.cconcurrentOp = TconcurrentOp + 1;
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
  regTcPtr->prevTcConnect = TlastTcConnect;
  regTcPtr->nextTcConnect = RNIL;
  regTcPtr->accumulatingTriggerData.i = RNIL;  
  regTcPtr->accumulatingTriggerData.p = NULL;  
  regTcPtr->noFiredTriggers = 0;
  regTcPtr->noReceivedTriggers = 0;
  regTcPtr->triggerExecutionCount = 0;
  regTcPtr->triggeringOperation = RNIL;
  regTcPtr->isIndexOp = false;
  regTcPtr->indexOp = RNIL;
  regTcPtr->currentIndexId = RNIL;

  regApiPtr->lastTcConnect = TtcConnectptrIndex;

  if (TlastTcConnect == RNIL) {
    jam();
    regApiPtr->firstTcConnect = TtcConnectptrIndex;
  } else {
    tmpTcConnectptr.i = TlastTcConnect;
    jam();
    ptrCheckGuard(tmpTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
    tmpTcConnectptr.p->nextTcConnect = TtcConnectptrIndex;
  }//if
  return 0;
}//Dbtc::seizeTcRecord()

int
Dbtc::seizeCacheRecord(Signal* signal)
{
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  UintR TfirstfreeCacheRec = cfirstfreeCacheRec;
  UintR TcacheFilesize = ccacheFilesize;
  CacheRecord *localCacheRecord = cacheRecord;
  if (TfirstfreeCacheRec >= TcacheFilesize) {
    TCKEY_abort(signal, 41);
    return 1;
  }//if
  CacheRecord * const regCachePtr = &localCacheRecord[TfirstfreeCacheRec];

  regApiPtr->cachePtr = TfirstfreeCacheRec;
  cfirstfreeCacheRec = regCachePtr->nextCacheRec;
  cachePtr.i = TfirstfreeCacheRec;
  cachePtr.p = regCachePtr;

#ifdef VM_TRACE
  // This is a good place to check that resources have 
  // been properly released from CacheRecord
  ndbrequire(regCachePtr->firstKeybuf == RNIL);
  ndbrequire(regCachePtr->lastKeybuf == RNIL);
#endif
  regCachePtr->firstKeybuf = RNIL;
  regCachePtr->lastKeybuf = RNIL;
  regCachePtr->firstAttrbuf = RNIL;
  regCachePtr->lastAttrbuf = RNIL;
  regCachePtr->currReclenAi = 0;
  return 0;
}//Dbtc::seizeCacheRecord()  

/*****************************************************************************/
/*                               T C K E Y R E Q                             */
/* AFTER HAVING ESTABLISHED THE CONNECT, THE APPLICATION BLOCK SENDS AN      */
/* OPERATION REQUEST TO TC. ALL NECESSARY INFORMATION TO CARRY OUT REQUEST   */
/* IS FURNISHED IN PARAMETERS. TC STORES THIS INFORMATION AND ENQUIRES       */
/* FROM DIH ABOUT THE NODES WHICH MAY HAVE THE REQUESTED DATA                */
/*****************************************************************************/
void Dbtc::execTCKEYREQ(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;
  UintR titcLenAiInTckeyreq;
  UintR TkeyLength;
  const TcKeyReq * const tcKeyReq = (TcKeyReq *)signal->getDataPtr();
  UintR Treqinfo;

  jamEntry();
  /*-------------------------------------------------------------------------
   * Common error routines are used for several signals, they need to know 
   * where to find the transaction identifier in the signal.
   *-------------------------------------------------------------------------*/
  const UintR TapiIndex = tcKeyReq->apiConnectPtr;
  const UintR TapiMaxIndex = capiConnectFilesize;
  const UintR TtabIndex = tcKeyReq->tableId;
  const UintR TtabMaxIndex = ctabrecFilesize;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  ttransid_ptr = 6; 
  apiConnectptr.i = TapiIndex;
  if (TapiIndex >= TapiMaxIndex) {
    TCKEY_abort(signal, 6);
    return;
  }//if
  if (TtabIndex >= TtabMaxIndex) {
    TCKEY_abort(signal, 7);
    return;
  }//if

  Treqinfo = tcKeyReq->requestInfo;
  //--------------------------------------------------------------------------
  // Optimised version of ptrAss(tabptr, tableRecord)
  // Optimised version of ptrAss(apiConnectptr, apiConnectRecord)
  //--------------------------------------------------------------------------
  ApiConnectRecord * const regApiPtr = &localApiConnectRecord[TapiIndex];
  apiConnectptr.p = regApiPtr;

  Uint32 TstartFlag = tcKeyReq->getStartFlag(Treqinfo);
unknown's avatar
unknown committed
2532 2533
  Uint32 TexecFlag = TcKeyReq::getExecuteFlag(Treqinfo);

2534 2535 2536
  bool isIndexOp = regApiPtr->isIndexOp;
  bool isIndexOpReturn = regApiPtr->indexOpReturn;
  regApiPtr->isIndexOp = false; // Reset marker
2537
  regApiPtr->m_exec_flag |= TexecFlag;
2538 2539 2540 2541 2542 2543 2544 2545
  switch (regApiPtr->apiConnectstate) {
  case CS_CONNECTED:{
    if (TstartFlag == 1 && getAllowStartTransaction() == true){
      //---------------------------------------------------------------------
      // Initialise API connect record if transaction is started.
      //---------------------------------------------------------------------
      jam();
      initApiConnectRec(signal, regApiPtr);
2546
      regApiPtr->m_exec_flag = TexecFlag;
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
    } else {
      if(getAllowStartTransaction() == true){
	/*------------------------------------------------------------------
	 * WE EXPECTED A START TRANSACTION. SINCE NO OPERATIONS HAVE BEEN 
	 * RECEIVED WE INDICATE THIS BY SETTING FIRST_TC_CONNECT TO RNIL TO 
	 * ENSURE PROPER OPERATION OF THE COMMON ABORT HANDLING.
	 *-----------------------------------------------------------------*/
	TCKEY_abort(signal, 0);
	return;
      } else {
	/**
	 * getAllowStartTransaction() == false
	 */
	TCKEY_abort(signal, 57);
	return;
      }//if
    }
  }
  break;
  case CS_STARTED:
unknown's avatar
unknown committed
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
    if(TstartFlag == 1 && regApiPtr->firstTcConnect == RNIL)
    {
      /**
       * If last operation in last transaction was a simple/dirty read
       *  it does not have to be committed or rollbacked hence,
       *  the state will be CS_STARTED
       */
      jam();
      initApiConnectRec(signal, regApiPtr);
      regApiPtr->m_exec_flag = TexecFlag;
    } else { 
      //----------------------------------------------------------------------
      // Transaction is started already. 
      // Check that the operation is on the same transaction.
      //-----------------------------------------------------------------------
      compare_transid1 = regApiPtr->transid[0] ^ tcKeyReq->transId1;
      compare_transid2 = regApiPtr->transid[1] ^ tcKeyReq->transId2;
      jam();
      compare_transid1 = compare_transid1 | compare_transid2;
      if (compare_transid1 != 0) {
	TCKEY_abort(signal, 1);
	return;
      }//if
    }
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
    break;
  case CS_ABORTING:
    if (regApiPtr->abortState == AS_IDLE) {
      if (TstartFlag == 1) {
	//--------------------------------------------------------------------
	// Previous transaction had been aborted and the abort was completed. 
	// It is then OK to start a new transaction again.
	//--------------------------------------------------------------------
        jam();
        initApiConnectRec(signal, regApiPtr);
2601
	regApiPtr->m_exec_flag = TexecFlag;
unknown's avatar
unknown committed
2602 2603 2604 2605
      } else if(TexecFlag) {
	TCKEY_abort(signal, 59);
	return;
      } else { 
2606 2607 2608 2609 2610
	//--------------------------------------------------------------------
	// The current transaction was aborted successfully. 
	// We will not do anything before we receive an operation 
	// with a start indicator. We will ignore this signal.
	//--------------------------------------------------------------------
unknown's avatar
unknown committed
2611 2612
	jam();
	DEBUG("Drop TCKEYREQ - apiConnectState=CS_ABORTING, ==AS_IDLE");
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
        return;
      }//if
    } else {
      //----------------------------------------------------------------------
      // Previous transaction is still aborting
      //----------------------------------------------------------------------
      jam();
      if (TstartFlag == 1) {
	//--------------------------------------------------------------------
	// If a new transaction tries to start while the old is 
	// still aborting, we will report this to the starting API.
	//--------------------------------------------------------------------
        TCKEY_abort(signal, 2);
        return;
unknown's avatar
unknown committed
2627 2628 2629 2630
      } else if(TexecFlag) {
        TCKEY_abort(signal, 59);
        return;
      }
2631 2632 2633
      //----------------------------------------------------------------------
      // Ignore signals without start indicator set when aborting transaction.
      //----------------------------------------------------------------------
unknown's avatar
unknown committed
2634
      DEBUG("Drop TCKEYREQ - apiConnectState=CS_ABORTING, !=AS_IDLE");
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
      return;
    }//if
    break;
  case CS_START_COMMITTING:
    jam();
    if(isIndexOpReturn || TcKeyReq::getExecutingTrigger(Treqinfo)){
      break;
    }
  default:
    jam();
    /*----------------------------------------------------------------------
     * IN THIS CASE THE NDBAPI IS AN UNTRUSTED ENTITY THAT HAS SENT A SIGNAL 
     * WHEN IT WAS NOT EXPECTED TO. 
     * WE MIGHT BE IN A PROCESS TO RECEIVE, PREPARE, 
     * COMMIT OR COMPLETE AND OBVIOUSLY THIS IS NOT A DESIRED EVENT.
     * WE WILL ALWAYS COMPLETE THE ABORT HANDLING BEFORE WE ALLOW 
     * ANYTHING TO HAPPEN ON THIS CONNECTION AGAIN. 
     * THUS THERE IS NO ACTION FROM THE API THAT CAN SPEED UP THIS PROCESS.
     *---------------------------------------------------------------------*/
    TCKEY_abort(signal, 55);
    return;
  }//switch
  
  TableRecordPtr localTabptr;
  localTabptr.i = TtabIndex;
  localTabptr.p = &tableRecord[TtabIndex];
  if (localTabptr.p->checkTable(tcKeyReq->tableSchemaVersion)) {
    ;
  } else {
    /*-----------------------------------------------------------------------*/
    /* THE API IS WORKING WITH AN OLD SCHEMA VERSION. IT NEEDS REPLACEMENT.  */
    /* COULD ALSO BE THAT THE TABLE IS NOT DEFINED.                          */
    /*-----------------------------------------------------------------------*/
    TCKEY_abort(signal, 8);
    return;
  }//if
  
  //-------------------------------------------------------------------------
  // Error Insertion for testing purposes. Test to see what happens when no
  // more TC records available.
  //-------------------------------------------------------------------------
  if (ERROR_INSERTED(8032)) {
    TCKEY_abort(signal, 3);
    return;
  }//if
  
  if (seizeTcRecord(signal) != 0) {
    return;
  }//if
  
  if (seizeCacheRecord(signal) != 0) {
    return;
  }//if
  
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  CacheRecord * const regCachePtr = cachePtr.p;

  /*
    INIT_TC_CONNECT_REC 
    -------------------------
  */
  /* ---------------------------------------------------------------------- */
  /* -------     INIT OPERATION RECORD WITH SIGNAL DATA AND RNILS   ------- */
  /*                                                                        */
  /* ---------------------------------------------------------------------- */

  UintR TapiVersionNo = tcKeyReq->getAPIVersion(tcKeyReq->attrLen);
  UintR Tlqhkeyreqrec = regApiPtr->lqhkeyreqrec;
  regApiPtr->lqhkeyreqrec = Tlqhkeyreqrec + 1;
  regCachePtr->apiVersionNo = TapiVersionNo;

  UintR TapiConnectptrIndex = apiConnectptr.i;
  UintR TsenderData = tcKeyReq->senderData;
  UintR TattrLen = tcKeyReq->getAttrinfoLen(tcKeyReq->attrLen);
2709
  UintR TattrinfoCount = c_counters.cattrinfoCount;
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723

  regTcPtr->apiConnect = TapiConnectptrIndex;
  regTcPtr->clientData = TsenderData;
  regTcPtr->commitAckMarker = RNIL;
  regTcPtr->isIndexOp = isIndexOp;
  regTcPtr->indexOp = regApiPtr->executingIndexOp;
  regTcPtr->savePointId = regApiPtr->currSavePointId;
  regApiPtr->executingIndexOp = RNIL;

  if (TcKeyReq::getExecutingTrigger(Treqinfo)) {
    // Save the TcOperationPtr for fireing operation
    regTcPtr->triggeringOperation = TsenderData;
  }

unknown's avatar
unknown committed
2724
  if (TexecFlag){
2725 2726 2727 2728 2729
    Uint32 currSPId = regApiPtr->currSavePointId;
    regApiPtr->currSavePointId = ++currSPId;
  }

  regCachePtr->attrlength = TattrLen;
2730
  c_counters.cattrinfoCount = TattrinfoCount + TattrLen;
2731 2732 2733 2734 2735 2736 2737 2738

  UintR TtabptrIndex = localTabptr.i;
  UintR TtableSchemaVersion = tcKeyReq->tableSchemaVersion;
  Uint8 TOperationType = tcKeyReq->getOperationType(Treqinfo);
  regCachePtr->tableref = TtabptrIndex;
  regCachePtr->schemaVersion = TtableSchemaVersion;
  regTcPtr->operation = TOperationType;

unknown's avatar
unknown committed
2739
  Uint8 TSimpleFlag         = tcKeyReq->getSimpleFlag(Treqinfo);
2740 2741 2742
  Uint8 TDirtyFlag          = tcKeyReq->getDirtyFlag(Treqinfo);
  Uint8 TInterpretedFlag    = tcKeyReq->getInterpretedFlag(Treqinfo);
  Uint8 TDistrKeyFlag       = tcKeyReq->getDistributionKeyFlag(Treqinfo);
unknown's avatar
unknown committed
2743
  Uint8 TexecuteFlag        = TexecFlag;
2744
  
unknown's avatar
unknown committed
2745
  regCachePtr->opSimple = TSimpleFlag;
2746
  regCachePtr->opExec   = TInterpretedFlag;
unknown's avatar
unknown committed
2747
  regTcPtr->dirtyOp  = TDirtyFlag;
unknown's avatar
unknown committed
2748
  regCachePtr->distributionKeyIndicator = TDistrKeyFlag;
2749 2750 2751 2752 2753 2754 2755 2756

  //-------------------------------------------------------------
  // The next step is to read the upto three conditional words.
  //-------------------------------------------------------------
  Uint32 TkeyIndex;
  Uint32* TOptionalDataPtr = (Uint32*)&tcKeyReq->scanInfo;
  {
    Uint32  TDistrGHIndex    = tcKeyReq->getScanIndFlag(Treqinfo);
unknown's avatar
unknown committed
2757
    Uint32  TDistrKeyIndex   = TDistrGHIndex;
unknown's avatar
unknown committed
2758 2759

    Uint32 TscanInfo = tcKeyReq->getTakeOverScanInfo(TOptionalDataPtr[0]);
2760 2761 2762 2763

    regCachePtr->scanTakeOverInd = TDistrGHIndex;
    regCachePtr->scanInfo = TscanInfo;

unknown's avatar
unknown committed
2764
    regCachePtr->distributionKey = TOptionalDataPtr[TDistrKeyIndex];
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

    TkeyIndex = TDistrKeyIndex + TDistrKeyFlag;
  }
  Uint32* TkeyDataPtr = &TOptionalDataPtr[TkeyIndex];

  UintR Tdata1 = TkeyDataPtr[0];
  UintR Tdata2 = TkeyDataPtr[1];
  UintR Tdata3 = TkeyDataPtr[2];
  UintR Tdata4 = TkeyDataPtr[3];
  UintR Tdata5;

  regCachePtr->keydata[0] = Tdata1;
  regCachePtr->keydata[1] = Tdata2;
  regCachePtr->keydata[2] = Tdata3;
  regCachePtr->keydata[3] = Tdata4;

  TkeyLength = tcKeyReq->getKeyLength(Treqinfo);
  Uint32 TAIDataIndex;
  if (TkeyLength > 8) {
    TAIDataIndex = TkeyIndex + 8;
  } else {
    if (TkeyLength == 0) {
      TCKEY_abort(signal, 4);
      return;
    }//if
    TAIDataIndex = TkeyIndex + TkeyLength;
  }//if
  Uint32* TAIDataPtr = &TOptionalDataPtr[TAIDataIndex];

  titcLenAiInTckeyreq = tcKeyReq->getAIInTcKeyReq(Treqinfo);
  regCachePtr->keylen = TkeyLength;
  regCachePtr->lenAiInTckeyreq = titcLenAiInTckeyreq;
  regCachePtr->currReclenAi = titcLenAiInTckeyreq;
2798 2799
  regCachePtr->m_special_hash = 
    localTabptr.p->hasCharAttr | (localTabptr.p->noOfDistrKeys > 0);
2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
  Tdata1 = TAIDataPtr[0];
  Tdata2 = TAIDataPtr[1];
  Tdata3 = TAIDataPtr[2];
  Tdata4 = TAIDataPtr[3];
  Tdata5 = TAIDataPtr[4];

  regCachePtr->attrinfo0     = Tdata1;
  regCachePtr->attrinfo15[0] = Tdata2;
  regCachePtr->attrinfo15[1] = Tdata3;
  regCachePtr->attrinfo15[2] = Tdata4;
  regCachePtr->attrinfo15[3] = Tdata5;

  if (TOperationType == ZREAD) {
unknown's avatar
unknown committed
2813
    Uint32 TreadCount = c_counters.creadCount;
2814 2815
    jam();
    regCachePtr->opLock = 0;
2816
    c_counters.creadCount = TreadCount + 1;
2817
  } else if(TOperationType == ZREAD_EX){
unknown's avatar
unknown committed
2818
    Uint32 TreadCount = c_counters.creadCount;
2819 2820 2821 2822
    jam();
    TOperationType = ZREAD;
    regTcPtr->operation = ZREAD;
    regCachePtr->opLock = ZUPDATE;
2823
    c_counters.creadCount = TreadCount + 1;
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
  } else {
    if(regApiPtr->commitAckMarker == RNIL){
      jam();
      CommitAckMarkerPtr tmp;
      if(!m_commitAckMarkerHash.seize(tmp)){
        TCKEY_abort(signal, 56);
        return;
      } else {
        regTcPtr->commitAckMarker = tmp.i;
        regApiPtr->commitAckMarker = tmp.i;
        tmp.p->transid1      = tcKeyReq->transId1;
        tmp.p->transid2      = tcKeyReq->transId2;
        tmp.p->apiNodeId     = refToNode(regApiPtr->ndbapiBlockref);
        tmp.p->apiConnectPtr = TapiIndex;
        tmp.p->noOfLqhs      = 0;
        m_commitAckMarkerHash.add(tmp);
      }
    }
    
2843
    UintR TwriteCount = c_counters.cwriteCount;
2844 2845 2846 2847 2848 2849 2850
    UintR Toperationsize = coperationsize;
    /* -------------------------------------------------------------------- 
     *   THIS IS A TEMPORARY TABLE, DON'T UPDATE coperationsize. 
     *   THIS VARIABLE CONTROLS THE INTERVAL BETWEEN LCP'S AND 
     *   TEMP TABLES DON'T PARTICIPATE.
     * -------------------------------------------------------------------- */
    if (localTabptr.p->storedTable) {
unknown's avatar
unknown committed
2851
      coperationsize = ((Toperationsize + TattrLen) + TkeyLength) + 17;
2852
    }
2853
    c_counters.cwriteCount = TwriteCount + 1;
2854 2855 2856
    switch (TOperationType) {
    case ZUPDATE:
      jam();
unknown's avatar
unknown committed
2857
      if (TattrLen == 0) {
unknown's avatar
unknown committed
2858 2859
        //TCKEY_abort(signal, 5);
        //return;
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
      }//if
      /*---------------------------------------------------------------------*/
      // The missing break is intentional since we also want to set the opLock 
      // variable also for updates
      /*---------------------------------------------------------------------*/
    case ZINSERT:
    case ZDELETE:
      jam();      
      regCachePtr->opLock = TOperationType;
      break;
    case ZWRITE:
      jam();
      // A write operation is originally an insert operation.
      regCachePtr->opLock = ZINSERT;  
      break;
    default:
      TCKEY_abort(signal, 9);
      return;
    }//switch
  }//if
  
  Uint32 TabortOption = tcKeyReq->getAbortOption(Treqinfo);
  regTcPtr->m_execAbortOption = TabortOption;
  
  /*-------------------------------------------------------------------------
   * Check error handling per operation
   * If CommitFlag is set state accordingly and check for early abort
   *------------------------------------------------------------------------*/
  if (tcKeyReq->getCommitFlag(Treqinfo) == 1) {
    ndbrequire(TexecuteFlag);
    regApiPtr->apiConnectstate = CS_REC_COMMITTING;
  } else {
    /* ---------------------------------------------------------------------
     *       PREPARE TRANSACTION IS NOT IMPLEMENTED YET.
     * ---------------------------------------------------------------------
     *       ELSIF (TREQINFO => 3) (*) 1 = 1 THEN                
     * IF PREPARE TRANSACTION THEN
     *   API_CONNECTPTR:API_CONNECTSTATE = REC_PREPARING
     * SET STATE TO PREPARING
     * --------------------------------------------------------------------- */
    if (regApiPtr->apiConnectstate == CS_START_COMMITTING) {
      jam();
      // Trigger execution at commit
      regApiPtr->apiConnectstate = CS_REC_COMMITTING;
    } else {
      jam();
      regApiPtr->apiConnectstate = CS_RECEIVING;
    }//if
  }//if
  if (TkeyLength <= 4) {
    tckeyreq050Lab(signal);
    return;
  } else {
    if (cfirstfreeDatabuf != RNIL) {
      jam();
      linkKeybuf(signal);
      Tdata1 = TkeyDataPtr[4];
      Tdata2 = TkeyDataPtr[5];
      Tdata3 = TkeyDataPtr[6];
      Tdata4 = TkeyDataPtr[7];

      DatabufRecord * const regDataPtr = databufptr.p;
      regDataPtr->data[0] = Tdata1;
      regDataPtr->data[1] = Tdata2;
      regDataPtr->data[2] = Tdata3;
      regDataPtr->data[3] = Tdata4;
    } else {
      jam();
      seizeDatabuferrorLab(signal);
      return;
    }//if
    if (TkeyLength <= 8) {
      jam();
      tckeyreq050Lab(signal);
      return;
    } else {
      jam();
      /* --------------------------------------------------------------------
       * THE TCKEYREQ DIDN'T CONTAIN ALL KEY DATA, 
       * SAVE STATE AND WAIT FOR KEYINFO 
       * --------------------------------------------------------------------*/
      setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
      regCachePtr->save1 = 8;
      regTcPtr->tcConnectstate = OS_WAIT_KEYINFO;
      return;
    }//if
  }//if
  return;
}//Dbtc::execTCKEYREQ()

void Dbtc::tckeyreq050Lab(Signal* signal) 
{
  UintR tnoOfBackup;
  UintR tnoOfStandby;
  UintR tnodeinfo;

2956 2957
  terrorCode = 0;

2958
  hash(signal); /* NOW IT IS TIME TO CALCULATE THE HASH VALUE*/
2959 2960 2961 2962 2963 2964
  
  if (unlikely(terrorCode))
  {
    releaseAtErrorLab(signal);
    return;
  }
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993

  CacheRecord * const regCachePtr = cachePtr.p;
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;

  UintR TtcTimer = ctcTimer;
  UintR ThashValue = thashValue;
  UintR TdistrHashValue = tdistrHashValue;
  UintR TdihConnectptr = regTcPtr->dihConnectptr;
  UintR Ttableref = regCachePtr->tableref;
  
  TableRecordPtr localTabptr;
  localTabptr.i = Ttableref;
  localTabptr.p = &tableRecord[localTabptr.i];
  Uint32 schemaVersion = regCachePtr->schemaVersion;
  if(localTabptr.p->checkTable(schemaVersion)){
    ;
  } else {
    terrorCode = localTabptr.p->getErrorCode(schemaVersion);
    TCKEY_abort(signal, 58);
    return;
  }
  
  setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);
  regCachePtr->hashValue = ThashValue;

  signal->theData[0] = TdihConnectptr;
  signal->theData[1] = Ttableref;
  signal->theData[2] = TdistrHashValue;
unknown's avatar
unknown committed
2994 2995 2996 2997
  signal->theData[3] = 0;
  signal->theData[4] = 0;
  signal->theData[5] = 0;
  signal->theData[6] = 0;
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013

  /*-------------------------------------------------------------*/
  /* FOR EFFICIENCY REASONS WE AVOID THE SIGNAL SENDING HERE AND */
  /* PROCEED IMMEDIATELY TO DIH. IN MULTI-THREADED VERSIONS WE   */
  /* HAVE TO INSERT A MUTEX ON DIH TO ENSURE PROPER OPERATION.   */
  /* SINCE THIS SIGNAL AND DIVERIFYREQ ARE THE ONLY SIGNALS SENT */
  /* TO DIH IN TRAFFIC IT SHOULD BE OK (3% OF THE EXECUTION TIME */
  /* IS SPENT IN DIH AND EVEN LESS IN REPLICATED NDB.            */
  /*-------------------------------------------------------------*/
  EXECUTE_DIRECT(DBDIH, GSN_DIGETNODESREQ, signal, 3);
  UintR TerrorIndicator = signal->theData[0];
  jamEntry();
  if (TerrorIndicator != 0) {
    execDIGETNODESREF(signal);
    return;
  }
3014 3015 3016 3017 3018 3019 3020 3021 3022
  
  if(ERROR_INSERTED(8050) && signal->theData[3] != getOwnNodeId())
  {
    ndbassert(false);
    signal->theData[1] = 626;
    execDIGETNODESREF(signal);
    return;
  }
  
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
  /****************>>*/
  /* DIGETNODESCONF >*/
  /* ***************>*/

  UintR Tdata1 = signal->theData[1];
  UintR Tdata2 = signal->theData[2];
  UintR Tdata3 = signal->theData[3];
  UintR Tdata4 = signal->theData[4];
  UintR Tdata5 = signal->theData[5];
  UintR Tdata6 = signal->theData[6];

  regCachePtr->fragmentid = Tdata1;
  tnodeinfo = Tdata2;

  regTcPtr->tcNodedata[0] = Tdata3;
  regTcPtr->tcNodedata[1] = Tdata4;
  regTcPtr->tcNodedata[2] = Tdata5;
  regTcPtr->tcNodedata[3] = Tdata6;

  Uint8 Toperation = regTcPtr->operation;
unknown's avatar
unknown committed
3043
  Uint8 Tdirty = regTcPtr->dirtyOp;
3044 3045 3046
  tnoOfBackup = tnodeinfo & 3;
  tnoOfStandby = (tnodeinfo >> 8) & 3;
 
unknown's avatar
unknown committed
3047
  regCachePtr->fragmentDistributionKey = (tnodeinfo >> 16) & 255;
3048
  if (Toperation == ZREAD) {
unknown's avatar
unknown committed
3049
    if (Tdirty == 1) {
3050 3051 3052 3053 3054 3055 3056
      jam();
      /*-------------------------------------------------------------*/
      /*       A SIMPLE READ CAN SELECT ANY OF THE PRIMARY AND       */
      /*       BACKUP NODES TO READ. WE WILL TRY TO SELECT THIS      */
      /*       NODE IF POSSIBLE TO AVOID UNNECESSARY COMMUNICATION   */
      /*       WITH SIMPLE READS.                                    */
      /*-------------------------------------------------------------*/
3057
      arrGuard(tnoOfBackup, MAX_REPLICAS);
3058
      UintR Tindex;
unknown's avatar
unknown committed
3059
      UintR TownNode = cownNodeid;
3060 3061 3062 3063 3064 3065 3066 3067
      for (Tindex = 1; Tindex <= tnoOfBackup; Tindex++) {
        UintR Tnode = regTcPtr->tcNodedata[Tindex];
        jam();
        if (Tnode == TownNode) {
          jam();
          regTcPtr->tcNodedata[0] = Tnode;
        }//if
      }//for
3068
      if(ERROR_INSERTED(8048) || ERROR_INSERTED(8049))
unknown's avatar
unknown committed
3069 3070 3071 3072 3073 3074 3075 3076
      {
	for (Tindex = 0; Tindex <= tnoOfBackup; Tindex++) 
	{
	  UintR Tnode = regTcPtr->tcNodedata[Tindex];
	  jam();
	  if (Tnode != TownNode) {
	    jam();
	    regTcPtr->tcNodedata[0] = Tnode;
3077
	    ndbout_c("Choosing %d", Tnode);
unknown's avatar
unknown committed
3078 3079 3080
	  }//if
	}//for
      }
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
    }//if
    jam();
    regTcPtr->lastReplicaNo = 0;
    regTcPtr->noOfNodes = 1;
  } else {
    UintR TlastReplicaNo;
    jam();
    TlastReplicaNo = tnoOfBackup + tnoOfStandby;
    regTcPtr->lastReplicaNo = (Uint8)TlastReplicaNo;
    regTcPtr->noOfNodes = (Uint8)(TlastReplicaNo + 1);
  }//if
  if (regCachePtr->lenAiInTckeyreq == regCachePtr->attrlength) {
    /****************************************************************>*/
    /* HERE WE HAVE FOUND THAT THE LAST SIGNAL BELONGING TO THIS      */
    /* OPERATION HAVE BEEN RECEIVED. THIS MEANS THAT WE CAN NOW REUSE */
    /* THE API CONNECT RECORD. HOWEVER IF PREPARE OR COMMIT HAVE BEEN */
    /* RECEIVED THEN IT IS NOT ALLOWED TO RECEIVE ANY FURTHER         */
    /* OPERATIONS. WE KNOW THAT WE WILL WAIT FOR DICT NEXT. IT IS NOT */
    /* POSSIBLE FOR THE TC CONNECTION TO BE READY YET.                */
    /****************************************************************>*/
    switch (regApiPtr->apiConnectstate) {
    case CS_RECEIVING:
      jam();
      regApiPtr->apiConnectstate = CS_STARTED;
      break;
    case CS_REC_COMMITTING:
      jam();
      regApiPtr->apiConnectstate = CS_START_COMMITTING;
      break;
    default:
      jam();
3112
      systemErrorLab(signal, __LINE__);
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
      return;
    }//switch
    attrinfoDihReceivedLab(signal);
    return;
  } else {
    if (regCachePtr->lenAiInTckeyreq < regCachePtr->attrlength) {
      TtcTimer = ctcTimer;
      jam();
      setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);
      regTcPtr->tcConnectstate = OS_WAIT_ATTR;
      return;
    } else {
      TCKEY_abort(signal, 11);
      return;
    }//if
  }//if
  return;
}//Dbtc::tckeyreq050Lab()

void Dbtc::attrinfoDihReceivedLab(Signal* signal) 
{
  CacheRecord * const regCachePtr = cachePtr.p;
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  Uint16 Tnode = regTcPtr->tcNodedata[0];

  TableRecordPtr localTabptr;
  localTabptr.i = regCachePtr->tableref;
  localTabptr.p = &tableRecord[localTabptr.i];

  if(localTabptr.p->checkTable(regCachePtr->schemaVersion)){
    ;
  } else {
    terrorCode = localTabptr.p->getErrorCode(regCachePtr->schemaVersion);
    TCKEY_abort(signal, 58);
    return;
  }
  arrGuard(Tnode, MAX_NDB_NODES);
  packLqhkeyreq(signal, calcLqhBlockRef(Tnode));
}//Dbtc::attrinfoDihReceivedLab()

void Dbtc::packLqhkeyreq(Signal* signal,
                         BlockReference TBRef) 
{
  CacheRecord * const regCachePtr = cachePtr.p;
  UintR Tkeylen = regCachePtr->keylen;
  UintR TfirstAttrbuf = regCachePtr->firstAttrbuf;
  sendlqhkeyreq(signal, TBRef);
  if (Tkeylen > 4) {
3161 3162
    packKeyData000Lab(signal, TBRef, Tkeylen - 4);
    releaseKeys();
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
  }//if
  packLqhkeyreq040Lab(signal,
                      TfirstAttrbuf,
                      TBRef);
}//Dbtc::packLqhkeyreq()

void Dbtc::sendlqhkeyreq(Signal* signal,
                         BlockReference TBRef) 
{
  UintR tslrAttrLen;
  UintR Tdata10;
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  CacheRecord * const regCachePtr = cachePtr.p;
unknown's avatar
unknown committed
3177
  UintR sig0, sig1, sig2, sig3, sig4, sig5, sig6;
3178 3179
#ifdef ERROR_INSERT
  if (ERROR_INSERTED(8002)) {
3180
    systemErrorLab(signal, __LINE__);
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
  }//if
  if (ERROR_INSERTED(8007)) {
    if (apiConnectptr.p->apiConnectstate == CS_STARTED) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8008)) {
    if (apiConnectptr.p->apiConnectstate == CS_START_COMMITTING) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8009)) {
    if (apiConnectptr.p->apiConnectstate == CS_STARTED) {
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8010)) {
    if (apiConnectptr.p->apiConnectstate == CS_START_COMMITTING) {
      return;
    }//if
  }//if
#endif

  tslrAttrLen = 0;
  LqhKeyReq::setAttrLen(tslrAttrLen, regCachePtr->attrlength);
  /* ---------------------------------------------------------------------- */
  // Bit16 == 0 since StoredProcedures are not yet supported.
  /* ---------------------------------------------------------------------- */
unknown's avatar
unknown committed
3211
  LqhKeyReq::setDistributionKey(tslrAttrLen, regCachePtr->fragmentDistributionKey);
3212 3213 3214
  LqhKeyReq::setScanTakeOverFlag(tslrAttrLen, regCachePtr->scanTakeOverInd);

  Tdata10 = 0;
unknown's avatar
unknown committed
3215 3216 3217
  sig0 = regCachePtr->opSimple;
  sig1 = regTcPtr->operation;
  bool simpleRead = (sig1 == ZREAD && sig0 == ZTRUE);
3218 3219 3220 3221 3222 3223 3224 3225 3226
  LqhKeyReq::setKeyLen(Tdata10, regCachePtr->keylen);
  LqhKeyReq::setLastReplicaNo(Tdata10, regTcPtr->lastReplicaNo);
  LqhKeyReq::setLockType(Tdata10, regCachePtr->opLock);
  /* ---------------------------------------------------------------------- */
  // Indicate Application Reference is present in bit 15
  /* ---------------------------------------------------------------------- */
  LqhKeyReq::setApplicationAddressFlag(Tdata10, 1);
  LqhKeyReq::setDirtyFlag(Tdata10, regTcPtr->dirtyOp);
  LqhKeyReq::setInterpretedFlag(Tdata10, regCachePtr->opExec);
unknown's avatar
unknown committed
3227 3228
  LqhKeyReq::setSimpleFlag(Tdata10, sig0);
  LqhKeyReq::setOperation(Tdata10, sig1);
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
  /* ----------------------------------------------------------------------- 
   * Sequential Number of first LQH = 0, bit 22-23                           
   * IF ATTRIBUTE INFORMATION IS SENT IN TCKEYREQ,
   * IT IS ALSO SENT IN LQHKEYREQ
   * ----------------------------------------------------------------------- */
  LqhKeyReq::setAIInLqhKeyReq(Tdata10, regCachePtr->lenAiInTckeyreq);
  /* -----------------------------------------------------------------------
   * Bit 27 == 0 since TC record is the same as the client record.
   * Bit 28 == 0 since readLenAi can only be set after reading in LQH.
   * ----------------------------------------------------------------------- */
  //LqhKeyReq::setAPIVersion(Tdata10, regCachePtr->apiVersionNo);
  Uint32 commitAckMarker = regTcPtr->commitAckMarker;
unknown's avatar
unknown committed
3241
  const Uint32 noOfLqhs = regTcPtr->noOfNodes;
3242 3243 3244 3245
  if(commitAckMarker != RNIL){
    jam();
    LqhKeyReq::setMarkerFlag(Tdata10, 1);

unknown's avatar
unknown committed
3246
    CommitAckMarker * tmp = m_commitAckMarkerHash.getPtr(commitAckMarker);
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275
    
    /**
     * Populate LQH array
     */
    tmp->noOfLqhs = noOfLqhs;
    for(Uint32 i = 0; i<noOfLqhs; i++){
      tmp->lqhNodeId[i] = regTcPtr->tcNodedata[i];
    }
  }
  
  /* ************************************************************> */
  /* NO READ LENGTH SENT FROM TC. SEQUENTIAL NUMBER IS 1 AND IT    */
  /* IS SENT TO A PRIMARY NODE.                                    */
  /* ************************************************************> */

  LqhKeyReq * const lqhKeyReq = (LqhKeyReq *)signal->getDataPtrSend();

  sig0 = tcConnectptr.i;
  sig2 = regCachePtr->hashValue;
  sig4 = cownref;
  sig5 = regTcPtr->savePointId;

  lqhKeyReq->clientConnectPtr = sig0;
  lqhKeyReq->attrLen = tslrAttrLen;
  lqhKeyReq->hashValue = sig2;
  lqhKeyReq->requestInfo = Tdata10;
  lqhKeyReq->tcBlockref = sig4;
  lqhKeyReq->savePointId = sig5;

3276
  sig0 = regCachePtr->tableref + ((regCachePtr->schemaVersion << 16) & 0xFFFF0000);
3277 3278 3279 3280 3281 3282 3283
  sig1 = regCachePtr->fragmentid + (regTcPtr->tcNodedata[1] << 16);
  sig2 = regApiPtr->transid[0];
  sig3 = regApiPtr->transid[1];
  sig4 = regApiPtr->ndbapiBlockref;
  sig5 = regTcPtr->clientData;
  sig6 = regCachePtr->scanInfo;

unknown's avatar
unknown committed
3284 3285 3286 3287 3288 3289 3290 3291
  if (! simpleRead)
  {
    regApiPtr->m_transaction_nodes.set(regTcPtr->tcNodedata[0]);
    regApiPtr->m_transaction_nodes.set(regTcPtr->tcNodedata[1]);
    regApiPtr->m_transaction_nodes.set(regTcPtr->tcNodedata[2]);
    regApiPtr->m_transaction_nodes.set(regTcPtr->tcNodedata[3]);  
  }
  
3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388
  lqhKeyReq->tableSchemaVersion = sig0;
  lqhKeyReq->fragmentData = sig1;
  lqhKeyReq->transId1 = sig2;
  lqhKeyReq->transId2 = sig3;
  lqhKeyReq->scanInfo = sig6;

  lqhKeyReq->variableData[0] = sig4;
  lqhKeyReq->variableData[1] = sig5;

  UintR nextPos = 2;

  if (regTcPtr->lastReplicaNo > 1) {
    sig0 = (UintR)regTcPtr->tcNodedata[2] +
           (UintR)(regTcPtr->tcNodedata[3] << 16);
    lqhKeyReq->variableData[nextPos] = sig0;
    nextPos++;
  }//if

  sig0 = regCachePtr->keydata[0];
  sig1 = regCachePtr->keydata[1];
  sig2 = regCachePtr->keydata[2];
  sig3 = regCachePtr->keydata[3];
  UintR Tkeylen = regCachePtr->keylen;

  lqhKeyReq->variableData[nextPos + 0] = sig0;
  lqhKeyReq->variableData[nextPos + 1] = sig1;
  lqhKeyReq->variableData[nextPos + 2] = sig2;
  lqhKeyReq->variableData[nextPos + 3] = sig3;

  if (Tkeylen < 4) {
    nextPos += Tkeylen;
  } else {
    nextPos += 4;
  }//if

  sig0 = regCachePtr->attrinfo0;
  sig1 = regCachePtr->attrinfo15[0];
  sig2 = regCachePtr->attrinfo15[1];
  sig3 = regCachePtr->attrinfo15[2];
  sig4 = regCachePtr->attrinfo15[3];
  UintR TlenAi = regCachePtr->lenAiInTckeyreq;

  lqhKeyReq->variableData[nextPos + 0] = sig0;
  lqhKeyReq->variableData[nextPos + 1] = sig1;
  lqhKeyReq->variableData[nextPos + 2] = sig2;
  lqhKeyReq->variableData[nextPos + 3] = sig3;
  lqhKeyReq->variableData[nextPos + 4] = sig4;

  nextPos += TlenAi;

  // Reset trigger count
  regTcPtr->accumulatingTriggerData.i = RNIL;  
  regTcPtr->accumulatingTriggerData.p = NULL;  
  regTcPtr->noFiredTriggers = 0;
  regTcPtr->triggerExecutionCount = 0;

  sendSignal(TBRef, GSN_LQHKEYREQ, signal, 
             nextPos + LqhKeyReq::FixedSignalLength, JBB);
}//Dbtc::sendlqhkeyreq()

void Dbtc::packLqhkeyreq040Lab(Signal* signal,
                               UintR anAttrBufIndex,
                               BlockReference TBRef) 
{
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  CacheRecord * const regCachePtr = cachePtr.p;
#ifdef ERROR_INSERT
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  if (ERROR_INSERTED(8009)) {
    if (regApiPtr->apiConnectstate == CS_STARTED) {
      attrbufptr.i = RNIL;
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8010)) {
    if (regApiPtr->apiConnectstate == CS_START_COMMITTING) {
      attrbufptr.i = RNIL;
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
#endif

  UintR TattrbufFilesize = cattrbufFilesize;
  AttrbufRecord *localAttrbufRecord = attrbufRecord;
  while (1) {
    if (anAttrBufIndex == RNIL) {
      UintR TtcTimer = ctcTimer;
      UintR Tread = (regTcPtr->operation == ZREAD);
      UintR Tsimple = (regCachePtr->opSimple == ZTRUE);
      UintR Tboth = Tread & Tsimple;
      setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);
      jam();
      /*--------------------------------------------------------------------
       *   WE HAVE SENT ALL THE SIGNALS OF THIS OPERATION. SET STATE AND EXIT.
       *---------------------------------------------------------------------*/
unknown's avatar
unknown committed
3389
      releaseAttrinfo();
3390 3391
      if (Tboth) {
        jam();
unknown's avatar
unknown committed
3392
        releaseSimpleRead(signal, apiConnectptr, tcConnectptr.p);
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
        return;
      }//if
      regTcPtr->tcConnectstate = OS_OPERATING;
      return;
    }//if
    if (anAttrBufIndex < TattrbufFilesize) {
      AttrbufRecord * const regAttrPtr = &localAttrbufRecord[anAttrBufIndex];
      anAttrBufIndex = regAttrPtr->attrbuf[ZINBUF_NEXT];
      sendAttrinfo(signal,
                   tcConnectptr.i,
                   regAttrPtr,
                   TBRef);
    } else {
      TCKEY_abort(signal, 17);
      return;
    }//if
  }//while
}//Dbtc::packLqhkeyreq040Lab()

/* ========================================================================= */
/* -------      RELEASE ALL ATTRINFO RECORDS IN AN OPERATION RECORD  ------- */
/* ========================================================================= */
unknown's avatar
unknown committed
3415
void Dbtc::releaseAttrinfo() 
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446
{
  UintR Tmp;
  AttrbufRecordPtr Tattrbufptr;
  CacheRecord * const regCachePtr = cachePtr.p;
  UintR TattrbufFilesize = cattrbufFilesize;
  UintR TfirstfreeAttrbuf = cfirstfreeAttrbuf;
  Tattrbufptr.i = regCachePtr->firstAttrbuf;
  AttrbufRecord *localAttrbufRecord = attrbufRecord;

  while (Tattrbufptr.i < TattrbufFilesize) {
    Tattrbufptr.p = &localAttrbufRecord[Tattrbufptr.i];
    Tmp = Tattrbufptr.p->attrbuf[ZINBUF_NEXT];
    Tattrbufptr.p->attrbuf[ZINBUF_NEXT] = TfirstfreeAttrbuf;
    TfirstfreeAttrbuf = Tattrbufptr.i;
    Tattrbufptr.i = Tmp;
    jam();
  }//while
  if (Tattrbufptr.i == RNIL) {
//---------------------------------------------------
// Now we will release the cache record at the same
// time as releasing the attrinfo records.
//---------------------------------------------------
    ApiConnectRecord * const regApiPtr = apiConnectptr.p;
    UintR TfirstfreeCacheRec = cfirstfreeCacheRec;
    UintR TCacheIndex = cachePtr.i;
    cfirstfreeAttrbuf = TfirstfreeAttrbuf;
    regCachePtr->nextCacheRec = TfirstfreeCacheRec;
    cfirstfreeCacheRec = TCacheIndex;
    regApiPtr->cachePtr = RNIL;
    return;
  }//if
3447
  systemErrorLab(0, __LINE__);
3448 3449 3450 3451 3452 3453
  return;
}//Dbtc::releaseAttrinfo()

/* ========================================================================= */
/* -------   RELEASE ALL RECORDS CONNECTED TO A SIMPLE OPERATION     ------- */
/* ========================================================================= */
unknown's avatar
unknown committed
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
void Dbtc::releaseSimpleRead(Signal* signal, 
			     ApiConnectRecordPtr regApiPtr,
			     TcConnectRecord* regTcPtr) 
{
  Uint32 Ttckeyrec = regApiPtr.p->tckeyrec;
  Uint32 TclientData = regTcPtr->clientData;
  Uint32 Tnode = regTcPtr->tcNodedata[0];
  Uint32 Tlqhkeyreqrec = regApiPtr.p->lqhkeyreqrec;
  Uint32 TsimpleReadCount = c_counters.csimpleReadCount;
  ConnectionState state = regApiPtr.p->apiConnectstate;
  
  regApiPtr.p->tcSendArray[Ttckeyrec] = TclientData;
  regApiPtr.p->tcSendArray[Ttckeyrec + 1] = TcKeyConf::SimpleReadBit | Tnode;
  regApiPtr.p->tckeyrec = Ttckeyrec + 2;
  
3469
  unlinkReadyTcCon(signal);
unknown's avatar
unknown committed
3470
  releaseTcCon();
3471 3472 3473 3474 3475

  /**
   * No LQHKEYCONF in Simple/Dirty read
   * Therefore decrese no LQHKEYCONF(REF) we are waiting for
   */
3476
  c_counters.csimpleReadCount = TsimpleReadCount + 1;
unknown's avatar
unknown committed
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
  regApiPtr.p->lqhkeyreqrec = --Tlqhkeyreqrec;
  
  if(Tlqhkeyreqrec == 0)
  {
    /**
     * Special case of lqhKeyConf_checkTransactionState:
     * - commit with zero operations: handle only for simple read
     */
    sendtckeyconf(signal, state == CS_START_COMMITTING);
    regApiPtr.p->apiConnectstate = 
      (state == CS_START_COMMITTING ? CS_CONNECTED : state);
    setApiConTimer(regApiPtr.i, 0, __LINE__);
3489 3490

    return;
unknown's avatar
unknown committed
3491 3492
  }
  
3493
  /**
unknown's avatar
unknown committed
3494
   * Emulate LQHKEYCONF
3495
   */
unknown's avatar
unknown committed
3496
  lqhKeyConf_checkTransactionState(signal, regApiPtr.p);
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
}//Dbtc::releaseSimpleRead()

/* ------------------------------------------------------------------------- */
/* -------        CHECK IF ALL TC CONNECTIONS ARE COMPLETED          ------- */
/* ------------------------------------------------------------------------- */
void Dbtc::unlinkReadyTcCon(Signal* signal) 
{
  TcConnectRecordPtr urtTcConnectptr;

  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  if (regTcPtr->prevTcConnect != RNIL) {
    jam();
    urtTcConnectptr.i = regTcPtr->prevTcConnect;
    ptrCheckGuard(urtTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
    urtTcConnectptr.p->nextTcConnect = regTcPtr->nextTcConnect;
  } else {
    jam();
    regApiPtr->firstTcConnect = regTcPtr->nextTcConnect;
  }//if
  if (regTcPtr->nextTcConnect != RNIL) {
    jam();
    urtTcConnectptr.i = regTcPtr->nextTcConnect;
    ptrCheckGuard(urtTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
    urtTcConnectptr.p->prevTcConnect = regTcPtr->prevTcConnect;
  } else {
    jam();
    regApiPtr->lastTcConnect = tcConnectptr.p->prevTcConnect;
  }//if
}//Dbtc::unlinkReadyTcCon()

unknown's avatar
unknown committed
3530
void Dbtc::releaseTcCon() 
3531 3532 3533
{
  TcConnectRecord * const regTcPtr = tcConnectptr.p;
  UintR TfirstfreeTcConnect = cfirstfreeTcConnect;
3534
  UintR TconcurrentOp = c_counters.cconcurrentOp;
3535 3536 3537 3538 3539 3540 3541 3542
  UintR TtcConnectptrIndex = tcConnectptr.i;

  regTcPtr->tcConnectstate = OS_CONNECTED;
  regTcPtr->nextTcConnect = TfirstfreeTcConnect;
  regTcPtr->apiConnect = RNIL;
  regTcPtr->isIndexOp = false;
  regTcPtr->indexOp = RNIL;
  cfirstfreeTcConnect = TtcConnectptrIndex;
3543
  c_counters.cconcurrentOp = TconcurrentOp - 1;
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
}//Dbtc::releaseTcCon()

void Dbtc::execPACKED_SIGNAL(Signal* signal) 
{
  LqhKeyConf * const lqhKeyConf = (LqhKeyConf *)signal->getDataPtr();

  UintR Ti;
  UintR Tstep = 0;
  UintR Tlength;
  UintR TpackedData[28];
  UintR Tdata1, Tdata2, Tdata3, Tdata4;

  jamEntry();
  Tlength = signal->length();
  if (Tlength > 25) {
    jam();
3560
    systemErrorLab(signal, __LINE__);
3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
    return;
  }//if
  Uint32* TpackDataPtr;
  for (Ti = 0; Ti < Tlength; Ti += 4) {
    Uint32* TsigDataPtr = &signal->theData[Ti];
    Tdata1 = TsigDataPtr[0];
    Tdata2 = TsigDataPtr[1];
    Tdata3 = TsigDataPtr[2];
    Tdata4 = TsigDataPtr[3];

    TpackDataPtr = &TpackedData[Ti];
    TpackDataPtr[0] = Tdata1;
    TpackDataPtr[1] = Tdata2;
    TpackDataPtr[2] = Tdata3;
    TpackDataPtr[3] = Tdata4;
  }//for
  while (Tlength > Tstep) {

    TpackDataPtr = &TpackedData[Tstep];
    Tdata1 = TpackDataPtr[0];
    Tdata2 = TpackDataPtr[1];
    Tdata3 = TpackDataPtr[2];

    lqhKeyConf->connectPtr = Tdata1 & 0x0FFFFFFF;
    lqhKeyConf->opPtr = Tdata2;
    lqhKeyConf->userRef = Tdata3;

    switch (Tdata1 >> 28) {
    case ZCOMMITTED:
      signal->header.theLength = 3;
      execCOMMITTED(signal);
      Tstep += 3;
      break;
    case ZCOMPLETED:
      signal->header.theLength = 3;
      execCOMPLETED(signal);
      Tstep += 3;
      break;
    case ZLQHKEYCONF:
      jam();
      Tdata1 = TpackDataPtr[3];
      Tdata2 = TpackDataPtr[4];
      Tdata3 = TpackDataPtr[5];
      Tdata4 = TpackDataPtr[6];

      lqhKeyConf->readLen = Tdata1;
      lqhKeyConf->transId1 = Tdata2;
      lqhKeyConf->transId2 = Tdata3;
      lqhKeyConf->noFiredTriggers = Tdata4;
      signal->header.theLength = LqhKeyConf::SignalLength;
      execLQHKEYCONF(signal);
      Tstep += LqhKeyConf::SignalLength;
      break;
    default:
3615
      systemErrorLab(signal, __LINE__);
3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
      return;
    }//switch
  }//while
  return;
}//Dbtc::execPACKED_SIGNAL()

void Dbtc::execLQHKEYCONF(Signal* signal) 
{
  const LqhKeyConf * const lqhKeyConf = (LqhKeyConf *)signal->getDataPtr();
  UintR compare_transid1, compare_transid2;
  BlockReference tlastLqhBlockref;
  UintR tlastLqhConnect;
  UintR treadlenAi;
  UintR TtcConnectptrIndex;
  UintR TtcConnectFilesize = ctcConnectFilesize;

  tlastLqhConnect = lqhKeyConf->connectPtr;
  TtcConnectptrIndex = lqhKeyConf->opPtr;
  tlastLqhBlockref = lqhKeyConf->userRef;
  treadlenAi = lqhKeyConf->readLen;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;

  /*------------------------------------------------------------------------
   * NUMBER OF EXTERNAL TRIGGERS FIRED IN DATA[6] 
   * OPERATION IS NOW COMPLETED. CHECK FOR CORRECT OPERATION POINTER  
   * TO ENSURE NO CRASHES BECAUSE OF ERRONEUS NODES. CHECK STATE OF   
   * OPERATION. THEN SET OPERATION STATE AND RETRIEVE ALL POINTERS    
   * OF THIS OPERATION. PUT COMPLETED OPERATION IN LIST OF COMPLETED  
   * OPERATIONS ON THE LQH CONNECT RECORD.                          
   *------------------------------------------------------------------------
   * THIS SIGNAL ALWAYS ARRIVE BEFORE THE ABORTED SIGNAL ARRIVES SINCE IT USES
   * THE SAME PATH BACK TO TC AS THE ABORTED SIGNAL DO. WE DO HOWEVER HAVE A 
   * PROBLEM  WHEN WE ENCOUNTER A TIME-OUT WAITING FOR THE ABORTED SIGNAL.  
   * THEN THIS SIGNAL MIGHT ARRIVE WHEN THE TC CONNECT RECORD HAVE BEEN REUSED
   * BY OTHER TRANSACTION THUS WE CHECK THE TRANSACTION ID OF THE SIGNAL 
   * BEFORE ACCEPTING THIS SIGNAL.
   * Due to packing of LQHKEYCONF the ABORTED signal can now arrive before 
   * this. 
   * This is more reason to ignore the signal if not all states are correct.
   *------------------------------------------------------------------------*/
  if (TtcConnectptrIndex >= TtcConnectFilesize) {
    TCKEY_abort(signal, 25);
    return;
  }//if
  TcConnectRecord* const regTcPtr = &localTcConnectRecord[TtcConnectptrIndex];
  OperationState TtcConnectstate = regTcPtr->tcConnectstate;
  tcConnectptr.i = TtcConnectptrIndex;
  tcConnectptr.p = regTcPtr;
  if (TtcConnectstate != OS_OPERATING) {
    warningReport(signal, 23);
    return;
  }//if
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;
  UintR TapiConnectptrIndex = regTcPtr->apiConnect;
  UintR TapiConnectFilesize = capiConnectFilesize;
  UintR Ttrans1 = lqhKeyConf->transId1;
  UintR Ttrans2 = lqhKeyConf->transId2;
3673
  Uint32 noFired = lqhKeyConf->noFiredTriggers;
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692

  if (TapiConnectptrIndex >= TapiConnectFilesize) {
    TCKEY_abort(signal, 29);
    return;
  }//if
  ApiConnectRecord * const regApiPtr = 
                            &localApiConnectRecord[TapiConnectptrIndex];
  apiConnectptr.i = TapiConnectptrIndex;
  apiConnectptr.p = regApiPtr;
  compare_transid1 = regApiPtr->transid[0] ^ Ttrans1;
  compare_transid2 = regApiPtr->transid[1] ^ Ttrans2;
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    warningReport(signal, 24);
    return;
  }//if

#ifdef ERROR_INSERT
  if (ERROR_INSERTED(8029)) {
3693
    systemErrorLab(signal, __LINE__);
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
  }//if
  if (ERROR_INSERTED(8003)) {
    if (regApiPtr->apiConnectstate == CS_STARTED) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8004)) {
    if (regApiPtr->apiConnectstate == CS_RECEIVING) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8005)) {
    if (regApiPtr->apiConnectstate == CS_REC_COMMITTING) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8006)) {
    if (regApiPtr->apiConnectstate == CS_START_COMMITTING) {
      CLEAR_ERROR_INSERT_VALUE;
      return;
    }//if
  }//if
  if (ERROR_INSERTED(8023)) {
    SET_ERROR_INSERT_VALUE(8024);
    return;
  }//if
#endif
  UintR TtcTimer = ctcTimer;
  regTcPtr->lastLqhCon = tlastLqhConnect;
  regTcPtr->lastLqhNodeId = refToNode(tlastLqhBlockref);
3727
  regTcPtr->noFiredTriggers = noFired;
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749

  UintR Ttckeyrec = (UintR)regApiPtr->tckeyrec;
  UintR TclientData = regTcPtr->clientData;
  UintR TdirtyOp = regTcPtr->dirtyOp;
  ConnectionState TapiConnectstate = regApiPtr->apiConnectstate;
  if (Ttckeyrec > (ZTCOPCONF_SIZE - 2)) {
    TCKEY_abort(signal, 30);
    return;
  }
  if (TapiConnectstate == CS_ABORTING) {
    warningReport(signal, 27);
    return;
  }//if

  setApiConTimer(apiConnectptr.i, TtcTimer, __LINE__);

  if (regTcPtr->isIndexOp) {
    jam();
    // This was an internal TCKEYREQ
    // will be returned unpacked
    regTcPtr->attrInfoLen = treadlenAi;
  } else {
3750
    if (noFired == 0 && regTcPtr->triggeringOperation == RNIL) {
3751 3752
      jam();
      /*
3753 3754 3755 3756 3757 3758 3759
       * Skip counting triggering operations the first round
       * since they will enter execLQHKEYCONF a second time
       * Skip counting internally generated TcKeyReq
       */
      regApiPtr->tcSendArray[Ttckeyrec] = TclientData;
      regApiPtr->tcSendArray[Ttckeyrec + 1] = treadlenAi;
      regApiPtr->tckeyrec = Ttckeyrec + 2;
3760 3761 3762 3763 3764 3765 3766 3767 3768
    }//if
  }//if
  if (TdirtyOp == ZTRUE) {
    UintR Tlqhkeyreqrec = regApiPtr->lqhkeyreqrec;
    jam();
    releaseDirtyWrite(signal);
    regApiPtr->lqhkeyreqrec = Tlqhkeyreqrec - 1;
  } else {
    jam();
3769
    if (noFired == 0) {
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
      jam();
      // No triggers to execute
      UintR Tlqhkeyconfrec = regApiPtr->lqhkeyconfrec;
      regApiPtr->lqhkeyconfrec = Tlqhkeyconfrec + 1;
      regTcPtr->tcConnectstate = OS_PREPARED;
    }
  }//if

  /**
   * And now decide what to do next
   */
  if (regTcPtr->triggeringOperation != RNIL) {
    jam();
    // This operation was created by a trigger execting operation
    // Restart it if we have executed all it's triggers
    TcConnectRecordPtr opPtr;

    opPtr.i = regTcPtr->triggeringOperation;
    ptrCheckGuard(opPtr, ctcConnectFilesize, localTcConnectRecord);
    opPtr.p->triggerExecutionCount--;
    if (opPtr.p->triggerExecutionCount == 0) {
      /*
      We have completed current trigger execution
      Continue triggering operation
      */
      jam();
      continueTriggeringOp(signal, opPtr.p);
    }
3798
  } else if (noFired == 0) {
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809
    // This operation did not fire any triggers, finish operation
    jam();
    if (regTcPtr->isIndexOp) {
      jam();
      setupIndexOpReturn(regApiPtr, regTcPtr);
    }
    lqhKeyConf_checkTransactionState(signal, regApiPtr);
  } else {
    // We have fired triggers
    jam();
    saveTriggeringOpState(signal, regTcPtr);
3810
    if (regTcPtr->noReceivedTriggers == noFired) {
3811
      ApiConnectRecordPtr transPtr;
3812
      
3813 3814 3815 3816 3817 3818
      // We have received all data
      jam();
      transPtr.i = TapiConnectptrIndex;
      transPtr.p = regApiPtr;
      executeTriggers(signal, &transPtr);
    }
3819
    // else wait for more trigger data
3820 3821
  }
}//Dbtc::execLQHKEYCONF()
3822
 
3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
void Dbtc::setupIndexOpReturn(ApiConnectRecord* regApiPtr,
			      TcConnectRecord* regTcPtr) 
{
  regApiPtr->indexOpReturn = true;
  regApiPtr->indexOp = regTcPtr->indexOp;
  regApiPtr->clientData = regTcPtr->clientData;
  regApiPtr->attrInfoLen = regTcPtr->attrInfoLen;
}

/**
 * lqhKeyConf_checkTransactionState
 *
 * This functions checks state variables, and
 *   decides if it should wait for more LQHKEYCONF signals
 *   or if it should start commiting
 */
void
Dbtc::lqhKeyConf_checkTransactionState(Signal * signal,
				       ApiConnectRecord * const apiConnectPtrP)
{
/*---------------------------------------------------------------*/
/* IF THE COMMIT FLAG IS SET IN SIGNAL TCKEYREQ THEN DBTC HAS TO */
/* SEND TCKEYCONF FOR ALL OPERATIONS EXCEPT THE LAST ONE. WHEN   */
/* THE TRANSACTION THEN IS COMMITTED TCKEYCONF IS SENT FOR THE   */
/* WHOLE TRANSACTION                                             */
/* IF THE COMMIT FLAG IS NOT RECECIVED DBTC WILL SEND TCKEYCONF  */
/* FOR ALL OPERATIONS, AND THEN WAIT FOR THE API TO CONCLUDE THE */
/* TRANSACTION                                                   */
/*---------------------------------------------------------------*/
  ConnectionState TapiConnectstate = apiConnectPtrP->apiConnectstate;
  UintR Tlqhkeyconfrec = apiConnectPtrP->lqhkeyconfrec;
  UintR Tlqhkeyreqrec = apiConnectPtrP->lqhkeyreqrec;
  int TnoOfOutStanding = Tlqhkeyreqrec - Tlqhkeyconfrec;

  switch (TapiConnectstate) {
  case CS_START_COMMITTING:
    if (TnoOfOutStanding == 0) {
      jam();
      diverify010Lab(signal);
      return;
    } else if (TnoOfOutStanding > 0) {
      if (apiConnectPtrP->tckeyrec == ZTCOPCONF_SIZE) {
        jam();
        sendtckeyconf(signal, 0);
        return;
      } else if (apiConnectPtrP->indexOpReturn) {
	jam();
        sendtckeyconf(signal, 0);
        return;
      }//if
      jam();
      return;
    } else {
      TCKEY_abort(signal, 44);
      return;
    }//if
    return;
  case CS_STARTED:
  case CS_RECEIVING:
    if (TnoOfOutStanding == 0) {
      jam();
unknown's avatar
unknown committed
3884
      sendtckeyconf(signal, 2);
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
      return;
    } else {
      if (apiConnectPtrP->tckeyrec == ZTCOPCONF_SIZE) {
        jam();
        sendtckeyconf(signal, 0);
        return;
      } else if (apiConnectPtrP->indexOpReturn) {
	jam();
        sendtckeyconf(signal, 0);
        return;
	}//if
      jam();
    }//if
    return;
  case CS_REC_COMMITTING:
    if (TnoOfOutStanding > 0) {
      if (apiConnectPtrP->tckeyrec == ZTCOPCONF_SIZE) {
        jam();
        sendtckeyconf(signal, 0);
        return;
      } else if (apiConnectPtrP->indexOpReturn) {
        jam();
        sendtckeyconf(signal, 0);
        return;
      }//if
      jam();
      return;
    }//if
    TCKEY_abort(signal, 45);
    return;
  case CS_CONNECTED:
    jam();
/*---------------------------------------------------------------*/
/*       WE HAVE CONCLUDED THE TRANSACTION SINCE IT WAS ONLY     */
/*       CONSISTING OF DIRTY WRITES AND ALL OF THOSE WERE        */
/*       COMPLETED. ENSURE TCKEYREC IS ZERO TO PREVENT ERRORS.   */
/*---------------------------------------------------------------*/
    apiConnectPtrP->tckeyrec = 0;
    return;
  default:
    TCKEY_abort(signal, 46);
    return;
  }//switch
}//Dbtc::lqhKeyConf_checkTransactionState()

void Dbtc::sendtckeyconf(Signal* signal, UintR TcommitFlag) 
{
3932 3933 3934 3935 3936 3937 3938 3939 3940
  if(ERROR_INSERTED(8049)){
    CLEAR_ERROR_INSERT_VALUE;
    signal->theData[0] = TcContinueB::DelayTCKEYCONF;
    signal->theData[1] = apiConnectptr.i;
    signal->theData[2] = TcommitFlag;
    sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 3000, 3);
    return;
  }
  
3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951
  HostRecordPtr localHostptr;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  const UintR TopWords = (UintR)regApiPtr->tckeyrec;
  localHostptr.i = refToNode(regApiPtr->ndbapiBlockref);
  const Uint32 type = getNodeInfo(localHostptr.i).m_type;
  const bool is_api = (type >= NodeInfo::API && type <= NodeInfo::REP);
  const BlockNumber TblockNum = refToBlock(regApiPtr->ndbapiBlockref);
  const Uint32 Tmarker = (regApiPtr->commitAckMarker == RNIL) ? 0 : 1;
  ptrAss(localHostptr, hostRecord);
  UintR TcurrLen = localHostptr.p->noOfWordsTCKEYCONF;
  UintR confInfo = 0;
unknown's avatar
unknown committed
3952
  TcKeyConf::setCommitFlag(confInfo, TcommitFlag == 1);
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
  TcKeyConf::setMarkerFlag(confInfo, Tmarker);
  const UintR TpacketLen = 6 + TopWords;
  regApiPtr->tckeyrec = 0;

  if (regApiPtr->indexOpReturn) {
    jam();
    // Return internally generated TCKEY
    TcKeyConf * const tcKeyConf = (TcKeyConf *)signal->getDataPtrSend();
    TcKeyConf::setNoOfOperations(confInfo, 1);
    tcKeyConf->apiConnectPtr = regApiPtr->indexOp;
    tcKeyConf->gci = regApiPtr->globalcheckpointid;
    tcKeyConf->confInfo = confInfo;
    tcKeyConf->transId1 = regApiPtr->transid[0];
    tcKeyConf->transId2 = regApiPtr->transid[1];
    tcKeyConf->operations[0].apiOperationPtr = regApiPtr->clientData;
    tcKeyConf->operations[0].attrInfoLen = regApiPtr->attrInfoLen;
    Uint32 sigLen = TcKeyConf::StaticLength + TcKeyConf::OperationLength;
    EXECUTE_DIRECT(DBTC, GSN_TCKEYCONF, signal, sigLen);
    regApiPtr->indexOpReturn = false;
    if (TopWords == 0) {
      jam();
      return; // No queued TcKeyConf
    }//if
  }//if
unknown's avatar
unknown committed
3977 3978 3979 3980
  if(TcommitFlag){
    jam();
    regApiPtr->m_exec_flag = 0;
  }
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177
  TcKeyConf::setNoOfOperations(confInfo, (TopWords >> 1));
  if ((TpacketLen > 25) || !is_api){
    TcKeyConf * const tcKeyConf = (TcKeyConf *)signal->getDataPtrSend();
    
    jam();
    tcKeyConf->apiConnectPtr = regApiPtr->ndbapiConnect;
    tcKeyConf->gci = regApiPtr->globalcheckpointid;;
    tcKeyConf->confInfo = confInfo;
    tcKeyConf->transId1 = regApiPtr->transid[0];
    tcKeyConf->transId2 = regApiPtr->transid[1];
    copyFromToLen(&regApiPtr->tcSendArray[0],
		  (UintR*)&tcKeyConf->operations,
		  (UintR)ZTCOPCONF_SIZE);
    sendSignal(regApiPtr->ndbapiBlockref,
	       GSN_TCKEYCONF, signal, (TpacketLen - 1), JBB);
    return;
  } else if (((TcurrLen + TpacketLen) > 25) && (TcurrLen > 0)) {
    jam();
    sendPackedTCKEYCONF(signal, localHostptr.p, localHostptr.i);
    TcurrLen = 0;
  } else {
    jam();
    updatePackedList(signal, localHostptr.p, localHostptr.i);
  }//if
  // -------------------------------------------------------------------------
  // The header contains the block reference of receiver plus the real signal
  // length - 3, since we have the real signal length plus one additional word
  // for the header we have to do - 4.
  // -------------------------------------------------------------------------
  UintR Tpack0 = (TblockNum << 16) + (TpacketLen - 4);
  UintR Tpack1 = regApiPtr->ndbapiConnect;
  UintR Tpack2 = regApiPtr->globalcheckpointid;
  UintR Tpack3 = confInfo;
  UintR Tpack4 = regApiPtr->transid[0];
  UintR Tpack5 = regApiPtr->transid[1];
  
  localHostptr.p->noOfWordsTCKEYCONF = TcurrLen + TpacketLen;
  
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 0] = Tpack0;
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 1] = Tpack1;
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 2] = Tpack2;
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 3] = Tpack3;
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 4] = Tpack4;
  localHostptr.p->packedWordsTCKEYCONF[TcurrLen + 5] = Tpack5;
  
  UintR Ti;
  for (Ti = 6; Ti < TpacketLen; Ti++) {
    localHostptr.p->packedWordsTCKEYCONF[TcurrLen + Ti] = 
      regApiPtr->tcSendArray[Ti - 6];
  }//for
}//Dbtc::sendtckeyconf()

void Dbtc::copyFromToLen(UintR* sourceBuffer, UintR* destBuffer, UintR Tlen)
{
  UintR Tindex = 0;
  UintR Ti;
  while (Tlen >= 4) {
    UintR Tdata0 = sourceBuffer[Tindex + 0];
    UintR Tdata1 = sourceBuffer[Tindex + 1];
    UintR Tdata2 = sourceBuffer[Tindex + 2];
    UintR Tdata3 = sourceBuffer[Tindex + 3];
    Tlen -= 4;
    destBuffer[Tindex + 0] = Tdata0;
    destBuffer[Tindex + 1] = Tdata1;
    destBuffer[Tindex + 2] = Tdata2;
    destBuffer[Tindex + 3] = Tdata3;
    Tindex += 4;
  }//while
  for (Ti = 0; Ti < Tlen; Ti++, Tindex++) {
    destBuffer[Tindex] = sourceBuffer[Tindex];
  }//for
}//Dbtc::copyFromToLen()

void Dbtc::execSEND_PACKED(Signal* signal) 
{
  HostRecordPtr Thostptr;
  HostRecord *localHostRecord = hostRecord;
  UintR i;
  UintR TpackedListIndex = cpackedListIndex;
  jamEntry();
  for (i = 0; i < TpackedListIndex; i++) {
    Thostptr.i = cpackedList[i];
    ptrAss(Thostptr, localHostRecord);
    arrGuard(Thostptr.i - 1, MAX_NODES - 1);
    UintR TnoOfPackedWordsLqh = Thostptr.p->noOfPackedWordsLqh;
    UintR TnoOfWordsTCKEYCONF = Thostptr.p->noOfWordsTCKEYCONF;
    UintR TnoOfWordsTCINDXCONF = Thostptr.p->noOfWordsTCINDXCONF;
    jam();
    if (TnoOfPackedWordsLqh > 0) {
      jam();
      sendPackedSignalLqh(signal, Thostptr.p);
    }//if
    if (TnoOfWordsTCKEYCONF > 0) {
      jam();
      sendPackedTCKEYCONF(signal, Thostptr.p, (Uint32)Thostptr.i);
    }//if
    if (TnoOfWordsTCINDXCONF > 0) {
      jam();
      sendPackedTCINDXCONF(signal, Thostptr.p, (Uint32)Thostptr.i);
    }//if
    Thostptr.p->inPackedList = false;
  }//for
  cpackedListIndex = 0;
  return;
}//Dbtc::execSEND_PACKED()

void 
Dbtc::updatePackedList(Signal* signal, HostRecord* ahostptr, Uint16 ahostIndex)
{
  if (ahostptr->inPackedList == false) {
    UintR TpackedListIndex = cpackedListIndex;
    jam();
    ahostptr->inPackedList = true;
    cpackedList[TpackedListIndex] = ahostIndex;
    cpackedListIndex = TpackedListIndex + 1;
  }//if
}//Dbtc::updatePackedList()

void Dbtc::sendPackedSignalLqh(Signal* signal, HostRecord * ahostptr)
{
  UintR Tj;
  UintR TnoOfWords = ahostptr->noOfPackedWordsLqh;
  for (Tj = 0; Tj < TnoOfWords; Tj += 4) {
    UintR sig0 = ahostptr->packedWordsLqh[Tj + 0];
    UintR sig1 = ahostptr->packedWordsLqh[Tj + 1];
    UintR sig2 = ahostptr->packedWordsLqh[Tj + 2];
    UintR sig3 = ahostptr->packedWordsLqh[Tj + 3];
    signal->theData[Tj + 0] = sig0;
    signal->theData[Tj + 1] = sig1;
    signal->theData[Tj + 2] = sig2;
    signal->theData[Tj + 3] = sig3;
  }//for
  ahostptr->noOfPackedWordsLqh = 0;
  sendSignal(ahostptr->hostLqhBlockRef,
             GSN_PACKED_SIGNAL,
             signal,
             TnoOfWords,
             JBB);
}//Dbtc::sendPackedSignalLqh()

void Dbtc::sendPackedTCKEYCONF(Signal* signal,
                               HostRecord * ahostptr,
                               UintR hostId)
{
  UintR Tj;
  UintR TnoOfWords = ahostptr->noOfWordsTCKEYCONF;
  BlockReference TBref = numberToRef(API_PACKED, hostId);
  for (Tj = 0; Tj < ahostptr->noOfWordsTCKEYCONF; Tj += 4) {
    UintR sig0 = ahostptr->packedWordsTCKEYCONF[Tj + 0];
    UintR sig1 = ahostptr->packedWordsTCKEYCONF[Tj + 1];
    UintR sig2 = ahostptr->packedWordsTCKEYCONF[Tj + 2];
    UintR sig3 = ahostptr->packedWordsTCKEYCONF[Tj + 3];
    signal->theData[Tj + 0] = sig0;
    signal->theData[Tj + 1] = sig1;
    signal->theData[Tj + 2] = sig2;
    signal->theData[Tj + 3] = sig3;
  }//for
  ahostptr->noOfWordsTCKEYCONF = 0;
  sendSignal(TBref, GSN_TCKEYCONF, signal, TnoOfWords, JBB);
}//Dbtc::sendPackedTCKEYCONF()

void Dbtc::sendPackedTCINDXCONF(Signal* signal,
                                HostRecord * ahostptr,
                                UintR hostId)
{
  UintR Tj;
  UintR TnoOfWords = ahostptr->noOfWordsTCINDXCONF;
  BlockReference TBref = numberToRef(API_PACKED, hostId);
  for (Tj = 0; Tj < ahostptr->noOfWordsTCINDXCONF; Tj += 4) {
    UintR sig0 = ahostptr->packedWordsTCINDXCONF[Tj + 0];
    UintR sig1 = ahostptr->packedWordsTCINDXCONF[Tj + 1];
    UintR sig2 = ahostptr->packedWordsTCINDXCONF[Tj + 2];
    UintR sig3 = ahostptr->packedWordsTCINDXCONF[Tj + 3];
    signal->theData[Tj + 0] = sig0;
    signal->theData[Tj + 1] = sig1;
    signal->theData[Tj + 2] = sig2;
    signal->theData[Tj + 3] = sig3;
  }//for
  ahostptr->noOfWordsTCINDXCONF = 0;
  sendSignal(TBref, GSN_TCINDXCONF, signal, TnoOfWords, JBB);
}//Dbtc::sendPackedTCINDXCONF()

/*
4.3.11 DIVERIFY 
---------------
*/
/*****************************************************************************/
/*                               D I V E R I F Y                             */
/*                                                                           */
/*****************************************************************************/
void Dbtc::diverify010Lab(Signal* signal) 
{
  UintR TfirstfreeApiConnectCopy = cfirstfreeApiConnectCopy;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  signal->theData[0] = apiConnectptr.i;
  if (ERROR_INSERTED(8022)) {
    jam();
4178
    systemErrorLab(signal, __LINE__);
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491
  }//if
  if (TfirstfreeApiConnectCopy != RNIL) {
    seizeApiConnectCopy(signal);
    regApiPtr->apiConnectstate = CS_PREPARE_TO_COMMIT;
    /*-----------------------------------------------------------------------
     * WE COME HERE ONLY IF THE TRANSACTION IS PREPARED ON ALL TC CONNECTIONS.
     * THUS WE CAN START THE COMMIT PHASE BY SENDING DIVERIFY ON ALL TC     
     * CONNECTIONS AND THEN WHEN ALL DIVERIFYCONF HAVE BEEN RECEIVED THE 
     * COMMIT MESSAGE CAN BE SENT TO ALL INVOLVED PARTS.
     *-----------------------------------------------------------------------*/
    EXECUTE_DIRECT(DBDIH, GSN_DIVERIFYREQ, signal, 1);
    if (signal->theData[2] == 0) {
      execDIVERIFYCONF(signal);
    }
    return;
  } else {
    /*-----------------------------------------------------------------------
     * There were no free copy connections available. We must abort the 
     * transaction since otherwise we will have a problem with the report 
     * to the application.
     * This should more or less not happen but if it happens we do not want to
     * crash and we do not want to create code to handle it properly since 
     * it is difficult to test it and will be complex to handle a problem 
     * more or less not occurring.
     *-----------------------------------------------------------------------*/
    terrorCode = ZSEIZE_API_COPY_ERROR;
    abortErrorLab(signal);
    return;
  }//if
}//Dbtc::diverify010Lab()

/* ------------------------------------------------------------------------- */
/* -------                  SEIZE_API_CONNECT                        ------- */
/*                  SEIZE CONNECT RECORD FOR A REQUEST                       */
/* ------------------------------------------------------------------------- */
void Dbtc::seizeApiConnectCopy(Signal* signal) 
{
  ApiConnectRecordPtr locApiConnectptr;

  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;
  UintR TapiConnectFilesize = capiConnectFilesize;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;

  locApiConnectptr.i = cfirstfreeApiConnectCopy;
  ptrCheckGuard(locApiConnectptr, TapiConnectFilesize, localApiConnectRecord);
  cfirstfreeApiConnectCopy = locApiConnectptr.p->nextApiConnect;
  locApiConnectptr.p->nextApiConnect = RNIL;
  regApiPtr->apiCopyRecord = locApiConnectptr.i;
  regApiPtr->triggerPending = false;
  regApiPtr->isIndexOp = false;
}//Dbtc::seizeApiConnectCopy()

void Dbtc::execDIVERIFYCONF(Signal* signal) 
{
  UintR TapiConnectptrIndex = signal->theData[0];
  UintR TapiConnectFilesize = capiConnectFilesize;
  UintR Tgci = signal->theData[1];
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  jamEntry();
  if (ERROR_INSERTED(8017)) {
    CLEAR_ERROR_INSERT_VALUE;
    return;
  }//if
  if (TapiConnectptrIndex >= TapiConnectFilesize) {
    TCKEY_abort(signal, 31);
    return;
  }//if
  ApiConnectRecord * const regApiPtr = 
                            &localApiConnectRecord[TapiConnectptrIndex];
  ConnectionState TapiConnectstate = regApiPtr->apiConnectstate;
  UintR TApifailureNr = regApiPtr->failureNr;
  UintR Tfailure_nr = cfailure_nr;
  apiConnectptr.i = TapiConnectptrIndex;
  apiConnectptr.p = regApiPtr;
  if (TapiConnectstate != CS_PREPARE_TO_COMMIT) {
    TCKEY_abort(signal, 32);
    return;
  }//if
  /*--------------------------------------------------------------------------
   * THIS IS THE COMMIT POINT. IF WE ARRIVE HERE THE TRANSACTION IS COMMITTED 
   * UNLESS EVERYTHING CRASHES BEFORE WE HAVE BEEN ABLE TO REPORT THE COMMIT 
   * DECISION. THERE IS NO TURNING BACK FROM THIS DECISION FROM HERE ON.     
   * WE WILL INSERT THE TRANSACTION INTO ITS PROPER QUEUE OF 
   * TRANSACTIONS FOR ITS GLOBAL CHECKPOINT.              
   *-------------------------------------------------------------------------*/
  if (TApifailureNr != Tfailure_nr) {
    DIVER_node_fail_handling(signal, Tgci);
    return;
  }//if
  commitGciHandling(signal, Tgci);

  /**************************************************************************
   *                                 C O M M I T                      
   * THE TRANSACTION HAVE NOW BEEN VERIFIED AND NOW THE COMMIT PHASE CAN START 
   **************************************************************************/

  UintR TtcConnectptrIndex = regApiPtr->firstTcConnect;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;

  regApiPtr->counter = regApiPtr->lqhkeyconfrec;
  regApiPtr->apiConnectstate = CS_COMMITTING;
  if (TtcConnectptrIndex >= TtcConnectFilesize) {
    TCKEY_abort(signal, 33);
    return;
  }//if
  TcConnectRecord* const regTcPtr = &localTcConnectRecord[TtcConnectptrIndex];
  tcConnectptr.i = TtcConnectptrIndex;
  tcConnectptr.p = regTcPtr;
  commit020Lab(signal);
}//Dbtc::execDIVERIFYCONF()

/*--------------------------------------------------------------------------*/
/*                             COMMIT_GCI_HANDLING                          */
/*       SET UP GLOBAL CHECKPOINT DATA STRUCTURE AT THE COMMIT POINT.       */
/*--------------------------------------------------------------------------*/
void Dbtc::commitGciHandling(Signal* signal, UintR Tgci) 
{
  GcpRecordPtr localGcpPointer;

  UintR TgcpFilesize = cgcpFilesize;
  UintR Tfirstgcp = cfirstgcp;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  GcpRecord *localGcpRecord = gcpRecord;

  regApiPtr->globalcheckpointid = Tgci;
  if (Tfirstgcp != RNIL) {
    /* IF THIS GLOBAL CHECKPOINT ALREADY EXISTS */
    localGcpPointer.i = Tfirstgcp;
    ptrCheckGuard(localGcpPointer, TgcpFilesize, localGcpRecord);
    do {
      if (regApiPtr->globalcheckpointid == localGcpPointer.p->gcpId) {
        jam();
        gcpPtr.i = localGcpPointer.i;
        gcpPtr.p = localGcpPointer.p;
        linkApiToGcp(signal);
        return;
      } else {
        localGcpPointer.i = localGcpPointer.p->nextGcp;
        jam();
        if (localGcpPointer.i != RNIL) {
          jam();
          ptrCheckGuard(localGcpPointer, TgcpFilesize, localGcpRecord);
          continue;
        }//if
      }//if
      seizeGcp(signal);
      linkApiToGcp(signal);
      return;
    } while (1);
  } else {
    jam();
    seizeGcp(signal);
    linkApiToGcp(signal);
  }//if
}//Dbtc::commitGciHandling()

/* --------------------------------------------------------------------------*/
/* -LINK AN API CONNECT RECORD IN STATE PREPARED INTO THE LIST WITH GLOBAL - */
/* CHECKPOINTS. WHEN THE TRANSACTION I COMPLETED THE API CONNECT RECORD IS   */
/* LINKED OUT OF THE LIST.                                                   */
/*---------------------------------------------------------------------------*/
void Dbtc::linkApiToGcp(Signal* signal) 
{
  ApiConnectRecordPtr localApiConnectptr;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  GcpRecord * const regGcpPtr = gcpPtr.p;
  UintR TapiConnectptrIndex = apiConnectptr.i;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  regApiPtr->nextGcpConnect = RNIL;
  if (regGcpPtr->firstApiConnect == RNIL) {
    regGcpPtr->firstApiConnect = TapiConnectptrIndex;
    jam();
  } else {
    UintR TapiConnectFilesize = capiConnectFilesize;
    localApiConnectptr.i = regGcpPtr->lastApiConnect;
    jam();
    ptrCheckGuard(localApiConnectptr,
                  TapiConnectFilesize, localApiConnectRecord);
    localApiConnectptr.p->nextGcpConnect = TapiConnectptrIndex;
  }//if
  UintR TlastApiConnect = regGcpPtr->lastApiConnect;
  regApiPtr->gcpPointer = gcpPtr.i;
  regApiPtr->prevGcpConnect = TlastApiConnect;
  regGcpPtr->lastApiConnect = TapiConnectptrIndex;
}//Dbtc::linkApiToGcp()

void Dbtc::seizeGcp(Signal* signal) 
{
  GcpRecordPtr tmpGcpPointer;
  GcpRecordPtr localGcpPointer;

  UintR Tfirstgcp = cfirstgcp;
  UintR Tglobalcheckpointid = apiConnectptr.p->globalcheckpointid;
  UintR TgcpFilesize = cgcpFilesize;
  GcpRecord *localGcpRecord = gcpRecord;

  localGcpPointer.i = cfirstfreeGcp;
  ptrCheckGuard(localGcpPointer, TgcpFilesize, localGcpRecord);
  UintR TfirstfreeGcp = localGcpPointer.p->nextGcp;
  localGcpPointer.p->gcpId = Tglobalcheckpointid;
  localGcpPointer.p->nextGcp = RNIL;
  localGcpPointer.p->firstApiConnect = RNIL;
  localGcpPointer.p->lastApiConnect = RNIL;
  localGcpPointer.p->gcpNomoretransRec = ZFALSE;
  cfirstfreeGcp = TfirstfreeGcp;

  if (Tfirstgcp == RNIL) {
    jam();
    cfirstgcp = localGcpPointer.i;
  } else {
    tmpGcpPointer.i = clastgcp;
    jam();
    ptrCheckGuard(tmpGcpPointer, TgcpFilesize, localGcpRecord);
    tmpGcpPointer.p->nextGcp = localGcpPointer.i;
  }//if
  clastgcp = localGcpPointer.i;
  gcpPtr = localGcpPointer;
}//Dbtc::seizeGcp()

/*---------------------------------------------------------------------------*/
// Send COMMIT messages to all LQH operations involved in the transaction.
/*---------------------------------------------------------------------------*/
void Dbtc::commit020Lab(Signal* signal) 
{
  TcConnectRecordPtr localTcConnectptr;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;

  localTcConnectptr.p = tcConnectptr.p;
  setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
  UintR Tcount = 0;
  do {
    /*-----------------------------------------------------------------------
     * WE ARE NOW READY TO RELEASE ALL OPERATIONS ON THE LQH                  
     *-----------------------------------------------------------------------*/
    /* *********< */
    /*  COMMIT  < */
    /* *********< */
    localTcConnectptr.i = localTcConnectptr.p->nextTcConnect;
    localTcConnectptr.p->tcConnectstate = OS_COMMITTING;
    sendCommitLqh(signal, localTcConnectptr.p);

    if (localTcConnectptr.i != RNIL) {
      Tcount = Tcount + 1;
      if (Tcount < 16) {
        ptrCheckGuard(localTcConnectptr,
                      TtcConnectFilesize, localTcConnectRecord);
        jam();
        continue;
      } else {
        jam();
        if (ERROR_INSERTED(8014)) {
          CLEAR_ERROR_INSERT_VALUE;
          return;
        }//if
        signal->theData[0] = TcContinueB::ZSEND_COMMIT_LOOP;
        signal->theData[1] = apiConnectptr.i;
        signal->theData[2] = localTcConnectptr.i;
        sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
        return;
      }//if
    } else {
      jam();
      regApiPtr->apiConnectstate = CS_COMMIT_SENT;
      return;
    }//if
  } while (1);
}//Dbtc::commit020Lab()

void Dbtc::sendCommitLqh(Signal* signal,
                         TcConnectRecord * const regTcPtr)
{
  HostRecordPtr Thostptr;
  UintR ThostFilesize = chostFilesize;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  Thostptr.i = regTcPtr->lastLqhNodeId;
  ptrCheckGuard(Thostptr, ThostFilesize, hostRecord);
  if (Thostptr.p->noOfPackedWordsLqh > 21) {
    jam();
    sendPackedSignalLqh(signal, Thostptr.p);
  } else {
    jam();
    updatePackedList(signal, Thostptr.p, Thostptr.i);
  }//if
  UintR Tindex = Thostptr.p->noOfPackedWordsLqh;
  UintR* TDataPtr = &Thostptr.p->packedWordsLqh[Tindex];
  UintR Tdata1 = regTcPtr->lastLqhCon;
  UintR Tdata2 = regApiPtr->globalcheckpointid;
  UintR Tdata3 = regApiPtr->transid[0];
  UintR Tdata4 = regApiPtr->transid[1];

  TDataPtr[0] = Tdata1 | (ZCOMMIT << 28);
  TDataPtr[1] = Tdata2;
  TDataPtr[2] = Tdata3;
  TDataPtr[3] = Tdata4;
  Thostptr.p->noOfPackedWordsLqh = Tindex + 4;
}//Dbtc::sendCommitLqh()

void
Dbtc::DIVER_node_fail_handling(Signal* signal, UintR Tgci)
{
  /*------------------------------------------------------------------------
   * AT LEAST ONE NODE HAS FAILED DURING THE TRANSACTION. WE NEED TO CHECK IF  
   * THIS IS SO SERIOUS THAT WE NEED TO ABORT THE TRANSACTION. IN BOTH THE     
   * ABORT AND THE COMMIT CASES WE NEED  TO SET-UP THE DATA FOR THE           
   * ABORT/COMMIT/COMPLETE  HANDLING AS ALSO USED BY TAKE OVER FUNCTIONALITY. 
   *------------------------------------------------------------------------*/
  tabortInd = ZFALSE;
  setupFailData(signal);
unknown's avatar
unknown committed
4492
  if (false && tabortInd == ZFALSE) {
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
    jam();
    commitGciHandling(signal, Tgci);
    toCommitHandlingLab(signal);
  } else {
    jam();
    apiConnectptr.p->returnsignal = RS_TCROLLBACKREP;
    apiConnectptr.p->returncode = ZNODEFAIL_BEFORE_COMMIT;
    toAbortHandlingLab(signal);
  }//if
  return;
}//Dbtc::DIVER_node_fail_handling()


/* ------------------------------------------------------------------------- */
/* -------                       ENTER COMMITTED                     ------- */
/*                                                                           */
/* ------------------------------------------------------------------------- */
void Dbtc::execCOMMITTED(Signal* signal) 
{
  TcConnectRecordPtr localTcConnectptr;
  ApiConnectRecordPtr localApiConnectptr;

  UintR TtcConnectFilesize = ctcConnectFilesize;
  UintR TapiConnectFilesize = capiConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

#ifdef ERROR_INSERT
  if (ERROR_INSERTED(8018)) {
    CLEAR_ERROR_INSERT_VALUE;
    return;
  }//if
  if (ERROR_INSERTED(8030)) {
4526
    systemErrorLab(signal, __LINE__);
4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
  }//if
  if (ERROR_INSERTED(8025)) {
    SET_ERROR_INSERT_VALUE(8026);
    return;
  }//if
  if (ERROR_INSERTED(8041)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_COMMITTED, signal, 2000, 3);
    return;
  }//if
  if (ERROR_INSERTED(8042)) {
    SET_ERROR_INSERT_VALUE(8046);
    sendSignalWithDelay(cownref, GSN_COMMITTED, signal, 2000, 4);
    return;
  }//if
#endif
  localTcConnectptr.i = signal->theData[0];
  jamEntry();
  ptrCheckGuard(localTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
  localApiConnectptr.i = localTcConnectptr.p->apiConnect;
  if (localTcConnectptr.p->tcConnectstate != OS_COMMITTING) {
    warningReport(signal, 4);
    return;
  }//if
  ptrCheckGuard(localApiConnectptr, TapiConnectFilesize, 
		localApiConnectRecord);
  UintR Tcounter = localApiConnectptr.p->counter - 1;
  ConnectionState TapiConnectstate = localApiConnectptr.p->apiConnectstate;
  UintR Tdata1 = localApiConnectptr.p->transid[0] - signal->theData[1];
  UintR Tdata2 = localApiConnectptr.p->transid[1] - signal->theData[2];
  Tdata1 = Tdata1 | Tdata2;
  bool TcheckCondition = 
    (TapiConnectstate != CS_COMMIT_SENT) || (Tcounter != 0);

  setApiConTimer(localApiConnectptr.i, ctcTimer, __LINE__);
  localApiConnectptr.p->counter = Tcounter;
  localTcConnectptr.p->tcConnectstate = OS_COMMITTED;
  if (Tdata1 != 0) {
    warningReport(signal, 5);
    return;
  }//if
  if (TcheckCondition) {
    jam();
    /*-------------------------------------------------------*/
    // We have not sent all COMMIT requests yet. We could be
    // in the state that all sent are COMMITTED but we are
    // still waiting for a CONTINUEB to send the rest of the
    // COMMIT requests.
    /*-------------------------------------------------------*/
    return;
  }//if
  if (ERROR_INSERTED(8020)) {
    jam();
4580
    systemErrorLab(signal, __LINE__);
4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626
  }//if
  /*-------------------------------------------------------*/
  /* THE ENTIRE TRANSACTION IS NOW COMMITED                */
  /* NOW WE NEED TO SEND THE RESPONSE TO THE APPLICATION.  */
  /* THE APPLICATION CAN THEN REUSE THE API CONNECTION AND */
  /* THEREFORE WE NEED TO MOVE THE API CONNECTION TO A     */
  /* NEW API CONNECT RECORD.                               */
  /*-------------------------------------------------------*/

  apiConnectptr = localApiConnectptr;
  sendApiCommit(signal);

  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  localTcConnectptr.i = regApiPtr->firstTcConnect;
  UintR Tlqhkeyconfrec = regApiPtr->lqhkeyconfrec;
  ptrCheckGuard(localTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
  regApiPtr->counter = Tlqhkeyconfrec;

  tcConnectptr = localTcConnectptr;
  complete010Lab(signal);
  return;

}//Dbtc::execCOMMITTED()

/*-------------------------------------------------------*/
/*                       SEND_API_COMMIT                 */
/*       SEND COMMIT DECISION TO THE API.                */
/*-------------------------------------------------------*/
void Dbtc::sendApiCommit(Signal* signal) 
{
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;

  if (regApiPtr->returnsignal == RS_TCKEYCONF) {
    sendtckeyconf(signal, 1);
  } else if (regApiPtr->returnsignal == RS_TC_COMMITCONF) {
    jam();
    TcCommitConf * const commitConf = (TcCommitConf *)&signal->theData[0];
    if(regApiPtr->commitAckMarker == RNIL){
      jam();
      commitConf->apiConnectPtr = regApiPtr->ndbapiConnect;
    } else {
      jam();
      commitConf->apiConnectPtr = regApiPtr->ndbapiConnect | 1;
    }
    commitConf->transId1 = regApiPtr->transid[0];
    commitConf->transId2 = regApiPtr->transid[1];
unknown's avatar
unknown committed
4627 4628 4629
    commitConf->gci = regApiPtr->globalcheckpointid;
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TC_COMMITCONF, signal, 
	       TcCommitConf::SignalLength, JBB);
4630 4631 4632 4633 4634 4635 4636
  } else if (regApiPtr->returnsignal == RS_NO_RETURN) {
    jam();
  } else {
    TCKEY_abort(signal, 37);
    return;
  }//if
  UintR TapiConnectFilesize = capiConnectFilesize;
4637
  UintR TcommitCount = c_counters.ccommitCount;
4638 4639 4640 4641 4642 4643 4644
  UintR TapiIndex = apiConnectptr.i;
  UintR TnewApiIndex = regApiPtr->apiCopyRecord;
  UintR TapiFailState = regApiPtr->apiFailState;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  tmpApiConnectptr.p = apiConnectptr.p;
  tmpApiConnectptr.i = TapiIndex;
4645
  c_counters.ccommitCount = TcommitCount + 1;
4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675
  apiConnectptr.i = TnewApiIndex;
  ptrCheckGuard(apiConnectptr, TapiConnectFilesize, localApiConnectRecord);
  copyApi(signal);
  if (TapiFailState != ZTRUE) {
    return;
  } else {
    jam();
    handleApiFailState(signal, tmpApiConnectptr.i);
    return;
  }//if
}//Dbtc::sendApiCommit()

/* ========================================================================= */
/* =======                  COPY_API                                 ======= */
/*   COPY API RECORD ALSO RESET THE OLD API RECORD SO THAT IT                */
/*   IS PREPARED TO RECEIVE A NEW TRANSACTION.                               */
/*===========================================================================*/
void Dbtc::copyApi(Signal* signal) 
{
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  ApiConnectRecord * const regTmpApiPtr = tmpApiConnectptr.p;

  UintR TndbapiConnect = regTmpApiPtr->ndbapiConnect;
  UintR TfirstTcConnect = regTmpApiPtr->firstTcConnect;
  UintR Ttransid1 = regTmpApiPtr->transid[0];
  UintR Ttransid2 = regTmpApiPtr->transid[1];
  UintR Tlqhkeyconfrec = regTmpApiPtr->lqhkeyconfrec;
  UintR TgcpPointer = regTmpApiPtr->gcpPointer;
  UintR TgcpFilesize = cgcpFilesize;
  UintR TcommitAckMarker = regTmpApiPtr->commitAckMarker;
unknown's avatar
unknown committed
4676
  NdbNodeBitmask Tnodes = regTmpApiPtr->m_transaction_nodes;
4677 4678 4679 4680 4681 4682 4683 4684 4685 4686
  GcpRecord *localGcpRecord = gcpRecord;

  regApiPtr->ndbapiBlockref = regTmpApiPtr->ndbapiBlockref;
  regApiPtr->ndbapiConnect = TndbapiConnect;
  regApiPtr->firstTcConnect = TfirstTcConnect;
  regApiPtr->apiConnectstate = CS_COMPLETING;
  regApiPtr->transid[0] = Ttransid1;
  regApiPtr->transid[1] = Ttransid2;
  regApiPtr->lqhkeyconfrec = Tlqhkeyconfrec;
  regApiPtr->commitAckMarker = TcommitAckMarker;
unknown's avatar
unknown committed
4687
  regApiPtr->m_transaction_nodes = Tnodes;
4688 4689 4690 4691 4692 4693 4694 4695

  gcpPtr.i = TgcpPointer;
  ptrCheckGuard(gcpPtr, TgcpFilesize, localGcpRecord);
  unlinkApiConnect(signal);
  linkApiToGcp(signal);
  setApiConTimer(tmpApiConnectptr.i, 0, __LINE__);
  regTmpApiPtr->apiConnectstate = CS_CONNECTED;
  regTmpApiPtr->commitAckMarker = RNIL;
unknown's avatar
unknown committed
4696 4697
  regTmpApiPtr->firstTcConnect = RNIL;
  regTmpApiPtr->lastTcConnect = RNIL;
unknown's avatar
unknown committed
4698
  regTmpApiPtr->m_transaction_nodes.clear();
4699
  releaseAllSeizedIndexOperations(regTmpApiPtr);
4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
}//Dbtc::copyApi()

void Dbtc::unlinkApiConnect(Signal* signal) 
{
  ApiConnectRecordPtr localApiConnectptr;
  ApiConnectRecord * const regTmpApiPtr = tmpApiConnectptr.p;
  UintR TapiConnectFilesize = capiConnectFilesize;
  UintR TprevGcpConnect = regTmpApiPtr->prevGcpConnect;
  UintR TnextGcpConnect = regTmpApiPtr->nextGcpConnect;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

  if (TprevGcpConnect == RNIL) {
    gcpPtr.p->firstApiConnect = TnextGcpConnect;
    jam();
  } else {
    localApiConnectptr.i = TprevGcpConnect;
    jam();
    ptrCheckGuard(localApiConnectptr,
                  TapiConnectFilesize, localApiConnectRecord);
    localApiConnectptr.p->nextGcpConnect = TnextGcpConnect;
  }//if
  if (TnextGcpConnect == RNIL) {
    gcpPtr.p->lastApiConnect = TprevGcpConnect;
    jam();
  } else {
    localApiConnectptr.i = TnextGcpConnect;
    jam();
    ptrCheckGuard(localApiConnectptr,
                  TapiConnectFilesize, localApiConnectRecord);
    localApiConnectptr.p->prevGcpConnect = TprevGcpConnect;
  }//if
}//Dbtc::unlinkApiConnect()

void Dbtc::complete010Lab(Signal* signal) 
{
  TcConnectRecordPtr localTcConnectptr;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;

  localTcConnectptr.p = tcConnectptr.p;
  setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
  UintR TapiConnectptrIndex = apiConnectptr.i;
  UintR Tcount = 0;
  do {
    localTcConnectptr.p->apiConnect = TapiConnectptrIndex;
    localTcConnectptr.p->tcConnectstate = OS_COMPLETING;

    /* ************ */
    /*  COMPLETE  < */
    /* ************ */
    const Uint32 nextTcConnect = localTcConnectptr.p->nextTcConnect;
    sendCompleteLqh(signal, localTcConnectptr.p);
    localTcConnectptr.i = nextTcConnect;
    if (localTcConnectptr.i != RNIL) {
      Tcount++;
      if (Tcount < 16) {
        ptrCheckGuard(localTcConnectptr,
                      TtcConnectFilesize, localTcConnectRecord);
        jam();
        continue;
      } else {
        jam();
        if (ERROR_INSERTED(8013)) {
          CLEAR_ERROR_INSERT_VALUE;
          return;
        }//if
        signal->theData[0] = TcContinueB::ZSEND_COMPLETE_LOOP;
        signal->theData[1] = apiConnectptr.i;
        signal->theData[2] = localTcConnectptr.i;
        sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
        return;
      }//if
    } else {
      jam();
      regApiPtr->apiConnectstate = CS_COMPLETE_SENT;
      return;
    }//if
  } while (1);
}//Dbtc::complete010Lab()

void Dbtc::sendCompleteLqh(Signal* signal,
                           TcConnectRecord * const regTcPtr)
{
  HostRecordPtr Thostptr;
  UintR ThostFilesize = chostFilesize;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  Thostptr.i = regTcPtr->lastLqhNodeId;
  ptrCheckGuard(Thostptr, ThostFilesize, hostRecord);
  if (Thostptr.p->noOfPackedWordsLqh > 22) {
    jam();
    sendPackedSignalLqh(signal, Thostptr.p);
  } else {
    jam();
    updatePackedList(signal, Thostptr.p, Thostptr.i);
  }//if

  UintR Tindex = Thostptr.p->noOfPackedWordsLqh;
  UintR* TDataPtr = &Thostptr.p->packedWordsLqh[Tindex];
  UintR Tdata1 = regTcPtr->lastLqhCon | (ZCOMPLETE << 28);
  UintR Tdata2 = regApiPtr->transid[0];
  UintR Tdata3 = regApiPtr->transid[1];
  
  TDataPtr[0] = Tdata1;
  TDataPtr[1] = Tdata2;
  TDataPtr[2] = Tdata3;
  Thostptr.p->noOfPackedWordsLqh = Tindex + 3;
}//Dbtc::sendCompleteLqh()

void
Dbtc::execTC_COMMIT_ACK(Signal* signal){
  jamEntry();

  CommitAckMarker key;
  key.transid1 = signal->theData[0];
  key.transid2 = signal->theData[1];

  CommitAckMarkerPtr removedMarker;
  m_commitAckMarkerHash.release(removedMarker, key);
  if (removedMarker.i == RNIL) {
    jam();
4821
    warningHandlerLab(signal, __LINE__);
4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882
    return;
  }//if
  sendRemoveMarkers(signal, removedMarker.p);
}

void
Dbtc::sendRemoveMarkers(Signal* signal, const CommitAckMarker * marker){
  jam();
  const Uint32 noOfLqhs = marker->noOfLqhs;
  const Uint32 transId1 = marker->transid1;
  const Uint32 transId2 = marker->transid2;
  
  for(Uint32 i = 0; i<noOfLqhs; i++){
    jam();
    const NodeId nodeId = marker->lqhNodeId[i];
    sendRemoveMarker(signal, nodeId, transId1, transId2);
  }
}

void
Dbtc::sendRemoveMarker(Signal* signal, 
                       NodeId nodeId,
                       Uint32 transid1, 
                       Uint32 transid2){
  /**
   * Seize host ptr
   */
  HostRecordPtr hostPtr;
  const UintR ThostFilesize = chostFilesize;
  hostPtr.i = nodeId;
  ptrCheckGuard(hostPtr, ThostFilesize, hostRecord);

  if (hostPtr.p->noOfPackedWordsLqh > (25 - 3)){
    jam();
    sendPackedSignalLqh(signal, hostPtr.p);
  } else {
    jam();
    updatePackedList(signal, hostPtr.p, hostPtr.i);
  }//if
  
  UintR  numWord = hostPtr.p->noOfPackedWordsLqh;
  UintR* dataPtr = &hostPtr.p->packedWordsLqh[numWord];

  dataPtr[0] = (ZREMOVE_MARKER << 28);
  dataPtr[1] = transid1;
  dataPtr[2] = transid2;
  hostPtr.p->noOfPackedWordsLqh = numWord + 3;
}

void Dbtc::execCOMPLETED(Signal* signal) 
{
  TcConnectRecordPtr localTcConnectptr;
  ApiConnectRecordPtr localApiConnectptr;

  UintR TtcConnectFilesize = ctcConnectFilesize;
  UintR TapiConnectFilesize = capiConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;

#ifdef ERROR_INSERT
  if (ERROR_INSERTED(8031)) {
4883
    systemErrorLab(signal, __LINE__);
4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941
  }//if
  if (ERROR_INSERTED(8019)) {
    CLEAR_ERROR_INSERT_VALUE;
    return;
  }//if
  if (ERROR_INSERTED(8027)) {
    SET_ERROR_INSERT_VALUE(8028);
    return;
  }//if
  if (ERROR_INSERTED(8043)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_COMPLETED, signal, 2000, 3);
    return;
  }//if
  if (ERROR_INSERTED(8044)) {
    SET_ERROR_INSERT_VALUE(8047);
    sendSignalWithDelay(cownref, GSN_COMPLETED, signal, 2000, 3);
    return;
  }//if
#endif
  localTcConnectptr.i = signal->theData[0];
  jamEntry();
  ptrCheckGuard(localTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
  bool Tcond1 = (localTcConnectptr.p->tcConnectstate != OS_COMPLETING);
  localApiConnectptr.i = localTcConnectptr.p->apiConnect;
  if (Tcond1) {
    warningReport(signal, 6);
    return;
  }//if
  ptrCheckGuard(localApiConnectptr, TapiConnectFilesize, 
		localApiConnectRecord);
  UintR Tdata1 = localApiConnectptr.p->transid[0] - signal->theData[1];
  UintR Tdata2 = localApiConnectptr.p->transid[1] - signal->theData[2];
  UintR Tcounter = localApiConnectptr.p->counter - 1;
  ConnectionState TapiConnectstate = localApiConnectptr.p->apiConnectstate;
  Tdata1 = Tdata1 | Tdata2;
  bool TcheckCondition = 
    (TapiConnectstate != CS_COMPLETE_SENT) || (Tcounter != 0);
  if (Tdata1 != 0) {
    warningReport(signal, 7);
    return;
  }//if
  setApiConTimer(localApiConnectptr.i, ctcTimer, __LINE__);
  localApiConnectptr.p->counter = Tcounter;
  localTcConnectptr.p->tcConnectstate = OS_COMPLETED;
  localTcConnectptr.p->noOfNodes = 0; // == releaseNodes(signal)
  if (TcheckCondition) {
    jam();
    /*-------------------------------------------------------*/
    // We have not sent all COMPLETE requests yet. We could be
    // in the state that all sent are COMPLETED but we are
    // still waiting for a CONTINUEB to send the rest of the
    // COMPLETE requests.
    /*-------------------------------------------------------*/
    return;
  }//if
  if (ERROR_INSERTED(8021)) {
    jam();
4942
    systemErrorLab(signal, __LINE__);
4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
  }//if
  apiConnectptr = localApiConnectptr;
  releaseTransResources(signal);
}//Dbtc::execCOMPLETED()

/*---------------------------------------------------------------------------*/
/*                               RELEASE_TRANS_RESOURCES                     */
/*       RELEASE ALL RESOURCES THAT ARE CONNECTED TO THIS TRANSACTION.       */
/*---------------------------------------------------------------------------*/
void Dbtc::releaseTransResources(Signal* signal) 
{
  TcConnectRecordPtr localTcConnectptr;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
unknown's avatar
unknown committed
4957
  apiConnectptr.p->m_transaction_nodes.clear();
4958 4959 4960 4961 4962 4963 4964 4965
  localTcConnectptr.i = apiConnectptr.p->firstTcConnect;
  do {
    jam();
    ptrCheckGuard(localTcConnectptr, TtcConnectFilesize, localTcConnectRecord);
    UintR rtrTcConnectptrIndex = localTcConnectptr.p->nextTcConnect;
    tcConnectptr.i = localTcConnectptr.i;
    tcConnectptr.p = localTcConnectptr.p;
    localTcConnectptr.i = rtrTcConnectptrIndex;
unknown's avatar
unknown committed
4966
    releaseTcCon();
4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019
  } while (localTcConnectptr.i != RNIL);
  handleGcp(signal);
  releaseFiredTriggerData(&apiConnectptr.p->theFiredTriggers);
  releaseAllSeizedIndexOperations(apiConnectptr.p);
  releaseApiConCopy(signal);
}//Dbtc::releaseTransResources()

/* *********************************************************************>> */
/*       MODULE: HANDLE_GCP                                                */
/*       DESCRIPTION: HANDLES GLOBAL CHECKPOINT HANDLING AT THE COMPLETION */
/*       OF THE COMMIT PHASE AND THE ABORT PHASE. WE MUST ENSURE THAT TC   */
/*       SENDS GCP_TCFINISHED WHEN ALL TRANSACTIONS BELONGING TO A CERTAIN */
/*       GLOBAL CHECKPOINT HAVE COMPLETED.                                 */
/* *********************************************************************>> */
void Dbtc::handleGcp(Signal* signal) 
{
  GcpRecord *localGcpRecord = gcpRecord;
  GcpRecordPtr localGcpPtr;
  UintR TapiConnectptrIndex = apiConnectptr.i;
  UintR TgcpFilesize = cgcpFilesize;
  localGcpPtr.i = apiConnectptr.p->gcpPointer;
  tmpApiConnectptr.i = TapiConnectptrIndex;
  tmpApiConnectptr.p = apiConnectptr.p;
  ptrCheckGuard(localGcpPtr, TgcpFilesize, localGcpRecord);
  gcpPtr.i = localGcpPtr.i;
  gcpPtr.p = localGcpPtr.p;
  unlinkApiConnect(signal);
  if (localGcpPtr.p->firstApiConnect == RNIL) {
    if (localGcpPtr.p->gcpNomoretransRec == ZTRUE) {
      jam();
      tcheckGcpId = localGcpPtr.p->gcpId;
      gcpTcfinished(signal);
      unlinkGcp(signal);
    }//if
  }//if
}//Dbtc::handleGcp()

void Dbtc::releaseApiConCopy(Signal* signal) 
{
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  UintR TfirstfreeApiConnectCopyOld = cfirstfreeApiConnectCopy;
  cfirstfreeApiConnectCopy = apiConnectptr.i;
  regApiPtr->nextApiConnect = TfirstfreeApiConnectCopyOld;
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
  regApiPtr->apiConnectstate = CS_RESTART;
}//Dbtc::releaseApiConCopy()

/* ========================================================================= */
/* -------  RELEASE ALL RECORDS CONNECTED TO A DIRTY WRITE OPERATION ------- */
/* ========================================================================= */
void Dbtc::releaseDirtyWrite(Signal* signal) 
{
  unlinkReadyTcCon(signal);
unknown's avatar
unknown committed
5020
  releaseTcCon();
5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  if (regApiPtr->apiConnectstate == CS_START_COMMITTING) {
    if (regApiPtr->firstTcConnect == RNIL) {
      jam();
      regApiPtr->apiConnectstate = CS_CONNECTED;
      setApiConTimer(apiConnectptr.i, 0, __LINE__);
      sendtckeyconf(signal, 1);
    }//if
  }//if
}//Dbtc::releaseDirtyWrite()

/*****************************************************************************
 *                               L Q H K E Y R E F                          
 * WHEN LQHKEYREF IS RECEIVED DBTC WILL CHECK IF COMMIT FLAG WAS SENT FROM THE
 * APPLICATION. IF SO, THE WHOLE TRANSACTION WILL BE ROLLED BACK AND SIGNAL  
 * TCROLLBACKREP WILL BE SENT TO THE API.                                    
 *                                                                          
 * OTHERWISE TC WILL CHECK THE ERRORCODE. IF THE ERRORCODE IS INDICATING THAT
 * THE "ROW IS NOT FOUND" FOR UPDATE/READ/DELETE OPERATIONS AND "ROW ALREADY 
 * EXISTS" FOR INSERT OPERATIONS, DBTC WILL RELEASE THE OPERATION AND THEN   
 * SEND RETURN SIGNAL TCKEYREF TO THE USER. THE USER THEN HAVE TO SEND     
 * SIGNAL TC_COMMITREQ OR TC_ROLLBACKREQ TO CONCLUDE THE TRANSACTION. 
 * IF ANY TCKEYREQ WITH COMMIT IS RECEIVED AND API_CONNECTSTATE EQUALS 
 * "REC_LQHREFUSE",
 * THE OPERATION WILL BE TREATED AS AN OPERATION WITHOUT COMMIT. WHEN ANY    
 * OTHER FAULTCODE IS RECEIVED THE WHOLE TRANSACTION MUST BE ROLLED BACK     
 *****************************************************************************/
void Dbtc::execLQHKEYREF(Signal* signal) 
{
  const LqhKeyRef * const lqhKeyRef = (LqhKeyRef *)signal->getDataPtr();
  jamEntry();
5052
  
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071
  UintR compare_transid1, compare_transid2;
  UintR TtcConnectFilesize = ctcConnectFilesize;
  /*-------------------------------------------------------------------------
   *                                                                         
   * RELEASE NODE BUFFER(S) TO INDICATE THAT THIS OPERATION HAVE NO 
   * TRANSACTION PARTS ACTIVE ANYMORE. 
   * LQHKEYREF HAVE CLEARED ALL PARTS ON ITS PATH BACK TO TC.
   *-------------------------------------------------------------------------*/
  if (lqhKeyRef->connectPtr < TtcConnectFilesize) {
    /*-----------------------------------------------------------------------
     * WE HAVE TO CHECK THAT THE TRANSACTION IS STILL VALID. FIRST WE CHECK 
     * THAT THE LQH IS STILL CONNECTED TO A TC, IF THIS HOLDS TRUE THEN THE 
     * TC MUST BE CONNECTED TO AN API CONNECT RECORD. 
     * WE MUST ENSURE THAT THE TRANSACTION ID OF THIS API CONNECT 
     * RECORD IS STILL THE SAME AS THE ONE LQHKEYREF REFERS TO. 
     * IF NOT SIMPLY EXIT AND FORGET THE SIGNAL SINCE THE TRANSACTION IS    
     * ALREADY COMPLETED (ABORTED).
     *-----------------------------------------------------------------------*/
    tcConnectptr.i = lqhKeyRef->connectPtr;
5072
    Uint32 errCode = terrorCode = lqhKeyRef->errorCode;
5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086
    ptrAss(tcConnectptr, tcConnectRecord);
    TcConnectRecord * const regTcPtr = tcConnectptr.p;
    if (regTcPtr->tcConnectstate == OS_OPERATING) {
      apiConnectptr.i = regTcPtr->apiConnect;
      ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
      ApiConnectRecord * const regApiPtr = apiConnectptr.p;
      compare_transid1 = regApiPtr->transid[0] ^ lqhKeyRef->transId1;
      compare_transid2 = regApiPtr->transid[1] ^ lqhKeyRef->transId2;
      compare_transid1 = compare_transid1 | compare_transid2;
      if (compare_transid1 != 0) {
	warningReport(signal, 25);
	return;
      }//if

5087 5088 5089
      const ConnectionState state = regApiPtr->apiConnectstate;
      const Uint32 triggeringOp = regTcPtr->triggeringOperation;
      if (triggeringOp != RNIL) {
5090 5091 5092 5093 5094 5095
        jam();
	// This operation was created by a trigger execting operation
	TcConnectRecordPtr opPtr;
	TcConnectRecord *localTcConnectRecord = tcConnectRecord;
       
	const Uint32 currentIndexId = regTcPtr->currentIndexId;
5096
	ndbassert(currentIndexId != 0); // Only index triggers so far
5097
	
5098
	opPtr.i = triggeringOp;
5099
	ptrCheckGuard(opPtr, ctcConnectFilesize, localTcConnectRecord);
5100 5101 5102
	
	// The operation executed an index trigger
	const Uint32 opType = regTcPtr->operation;
5103 5104 5105
	if (errCode == ZALREADYEXIST)
	  errCode = terrorCode = ZNOTUNIQUE;
	else if (!(opType == ZDELETE && errCode == ZNOT_FOUND)) {
5106 5107 5108 5109 5110
	  jam();
	  /**
	   * "Normal path"
	   */
	  // fall-through
5111
	} else {
5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122
	  jam();
	  /** ZDELETE && NOT_FOUND */
	  TcIndexData* indexData = c_theIndexes.getPtr(currentIndexId);
	  if(indexData->indexState == IS_BUILDING && state != CS_ABORTING){
	    jam();
	    /**
	     * Ignore error
	     */
	    regApiPtr->lqhkeyconfrec++;
	    
	    unlinkReadyTcCon(signal);
unknown's avatar
unknown committed
5123
	    releaseTcCon();
5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136
	    
	    opPtr.p->triggerExecutionCount--;
	    if (opPtr.p->triggerExecutionCount == 0) {
	      /**
	       * We have completed current trigger execution
	       * Continue triggering operation
	       */
	      jam();
	      continueTriggeringOp(signal, opPtr.p);
	    }
	    return;
	  }
	}
5137
      }
5138
      
unknown's avatar
unknown committed
5139
      Uint32 marker = regTcPtr->commitAckMarker;
5140 5141
      markOperationAborted(regApiPtr, regTcPtr);
      
5142 5143 5144 5145 5146 5147 5148 5149
      if(regApiPtr->apiConnectstate == CS_ABORTING){
	/**
	 * We're already aborting' so don't send an "extra" TCKEYREF
	 */
	jam();
	return;
      }
      
5150 5151
      const Uint32 abort = regTcPtr->m_execAbortOption;
      if (abort == TcKeyReq::AbortOnError || triggeringOp != RNIL) {
5152 5153 5154 5155 5156 5157 5158
	/**
	 * No error is allowed on this operation
	 */
	TCKEY_abort(signal, 49);
	return;
      }//if

unknown's avatar
unknown committed
5159
      if (marker != RNIL){
5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178
	/**
	 * This was an insert/update/delete/write which failed
	 *   that contained the marker
	 * Currently unsupported to place new marker
	 */
	TCKEY_abort(signal, 49);
	return;
      }
      
      /* *************** */
      /*    TCKEYREF   < */
      /* *************** */
      TcKeyRef * const tcKeyRef = (TcKeyRef *) signal->getDataPtrSend();
      tcKeyRef->transId[0] = regApiPtr->transid[0];
      tcKeyRef->transId[1] = regApiPtr->transid[1];
      tcKeyRef->errorCode = terrorCode;
      bool isIndexOp = regTcPtr->isIndexOp;
      Uint32 indexOp = tcConnectptr.p->indexOp;
      Uint32 clientData = regTcPtr->clientData;
5179
      unlinkReadyTcCon(signal);   /* LINK TC CONNECT RECORD OUT OF  */
unknown's avatar
unknown committed
5180
      releaseTcCon();       /* RELEASE THE TC CONNECT RECORD  */
5181 5182 5183
      setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
      if (isIndexOp) {
        jam();
unknown's avatar
unknown committed
5184
	regApiPtr->lqhkeyreqrec--; // Compensate for extra during read
5185 5186
	tcKeyRef->connectPtr = indexOp;
	EXECUTE_DIRECT(DBTC, GSN_TCKEYREF, signal, TcKeyRef::SignalLength);
5187 5188
	apiConnectptr.i = regTcPtr->apiConnect;
	apiConnectptr.p = regApiPtr;
5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
      } else {
        jam();
	tcKeyRef->connectPtr = clientData;
	sendSignal(regApiPtr->ndbapiBlockref, 
		   GSN_TCKEYREF, signal, TcKeyRef::SignalLength, JBB);
      }//if
      
      /*---------------------------------------------------------------------
       * SINCE WE ARE NOT ABORTING WE NEED TO UPDATE THE COUNT OF HOW MANY 
       * LQHKEYREQ THAT HAVE RETURNED. 
       * IF NO MORE OUTSTANDING LQHKEYREQ'S THEN WE NEED TO 
       * TCKEYCONF (IF THERE IS ANYTHING TO SEND).          
       *---------------------------------------------------------------------*/
5202
      regApiPtr->lqhkeyreqrec--;
5203
      if (regApiPtr->lqhkeyconfrec == regApiPtr->lqhkeyreqrec) {
unknown's avatar
unknown committed
5204 5205
	if (regApiPtr->apiConnectstate == CS_START_COMMITTING) {
	  if(regApiPtr->lqhkeyconfrec) {
5206
	    jam();
unknown's avatar
unknown committed
5207 5208 5209 5210 5211
	    diverify010Lab(signal);
	  } else {
	    jam();
	    sendtckeyconf(signal, 1);
	    regApiPtr->apiConnectstate = CS_CONNECTED;
5212 5213
	  }
	  return;
unknown's avatar
unknown committed
5214
	} else if (regApiPtr->tckeyrec > 0 || regApiPtr->m_exec_flag) {
5215
	  jam();
unknown's avatar
unknown committed
5216
	  sendtckeyconf(signal, 2);
5217
	  return;
unknown's avatar
unknown committed
5218
	}
5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
      }//if
      return;
      
    } else {
      warningReport(signal, 26);
    }//if
  } else {
    errorReport(signal, 6);
  }//if
  return;
}//Dbtc::execLQHKEYREF()

void Dbtc::clearCommitAckMarker(ApiConnectRecord * const regApiPtr,
				TcConnectRecord * const regTcPtr)
{
  const Uint32 commitAckMarker = regTcPtr->commitAckMarker;
  if (regApiPtr->commitAckMarker == RNIL)
    ndbassert(commitAckMarker == RNIL);
  if (commitAckMarker != RNIL)
    ndbassert(regApiPtr->commitAckMarker != RNIL);
  if(commitAckMarker != RNIL){
    jam();
    m_commitAckMarkerHash.release(commitAckMarker);
    regTcPtr->commitAckMarker = RNIL;
    regApiPtr->commitAckMarker = RNIL;
  }
}

void Dbtc::markOperationAborted(ApiConnectRecord * const regApiPtr,
				TcConnectRecord * const regTcPtr)
{
  /*------------------------------------------------------------------------
   * RELEASE NODES TO INDICATE THAT THE OPERATION IS ALREADY ABORTED IN THE 
   * LQH'S ALSO SET STATE TO ABORTING TO INDICATE THE ABORT IS 
   * ALREADY COMPLETED.  
   *------------------------------------------------------------------------*/
  regTcPtr->noOfNodes = 0; // == releaseNodes(signal)
  regTcPtr->tcConnectstate = OS_ABORTING;
  clearCommitAckMarker(regApiPtr, regTcPtr);
}

/*--------------------------------------*/
/* EXIT AND WAIT FOR SIGNAL TCOMMITREQ  */
/* OR TCROLLBACKREQ FROM THE USER TO    */
/* CONTINUE THE TRANSACTION             */
/*--------------------------------------*/
void Dbtc::execTC_COMMITREQ(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  apiConnectptr.i = signal->theData[0];
  if (apiConnectptr.i < capiConnectFilesize) {
    ptrAss(apiConnectptr, apiConnectRecord);
    compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[1];
    compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[2];
    compare_transid1 = compare_transid1 | compare_transid2;
    if (compare_transid1 != 0) {
      jam();
      return;
    }//if
    
    ApiConnectRecord * const regApiPtr = apiConnectptr.p;

    const Uint32 apiConnectPtr = regApiPtr->ndbapiConnect;
    const Uint32 apiBlockRef   = regApiPtr->ndbapiBlockref;
    const Uint32 transId1      = regApiPtr->transid[0];
    const Uint32 transId2      = regApiPtr->transid[1];
    Uint32 errorCode           = 0;
5288 5289

    regApiPtr->m_exec_flag = 1;
5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
    switch (regApiPtr->apiConnectstate) {
    case CS_STARTED:
      tcConnectptr.i = regApiPtr->firstTcConnect;
      if (tcConnectptr.i != RNIL) {
        ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
        if (regApiPtr->lqhkeyconfrec == regApiPtr->lqhkeyreqrec) {
          jam();
          /*******************************************************************/
          // The proper case where the application is waiting for commit or 
          // abort order.
          // Start the commit order.
          /*******************************************************************/
          regApiPtr->returnsignal = RS_TC_COMMITCONF;
          setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
          diverify010Lab(signal);
          return;
        } else {
          jam();
          /*******************************************************************/
          // The transaction is started but not all operations are completed. 
          // It is not possible to commit the transaction in this state. 
          // We will abort it instead.
          /*******************************************************************/
          regApiPtr->returnsignal = RS_NO_RETURN;
          errorCode = ZTRANS_STATUS_ERROR;
5315
          abort010Lab(signal);
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
        }//if
      } else {
        jam();
        /**
         * No operations, accept commit
         */
        TcCommitConf * const commitConf = (TcCommitConf *)&signal->theData[0];
        commitConf->apiConnectPtr = apiConnectPtr;
        commitConf->transId1 = transId1;
        commitConf->transId2 = transId2;
unknown's avatar
unknown committed
5326 5327 5328
        commitConf->gci = 0;
        sendSignal(apiBlockRef, GSN_TC_COMMITCONF, signal, 
		   TcCommitConf::SignalLength, JBB);
5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342
        
        regApiPtr->returnsignal = RS_NO_RETURN;
        releaseAbortResources(signal);
        return;
      }//if
      break;
    case CS_RECEIVING:
      jam();
      /***********************************************************************/
      // A transaction is still receiving data. We cannot commit an unfinished 
      // transaction. We will abort it instead.
      /***********************************************************************/
      regApiPtr->returnsignal = RS_NO_RETURN;
      errorCode = ZPREPAREINPROGRESS;
5343
      abort010Lab(signal);
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361
      break;
      
    case CS_START_COMMITTING:
    case CS_COMMITTING:
    case CS_COMMIT_SENT:
    case CS_COMPLETING:
    case CS_COMPLETE_SENT:
    case CS_REC_COMMITTING:
    case CS_PREPARE_TO_COMMIT:
      jam();
      /***********************************************************************/
      // The transaction is already performing a commit but it is not concluded
      // yet.
      /***********************************************************************/
      errorCode = ZCOMMITINPROGRESS;
      break;
    case CS_ABORTING:
      jam();
unknown's avatar
unknown committed
5362 5363
      errorCode = regApiPtr->returncode ? 
	regApiPtr->returncode : ZABORTINPROGRESS;
5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382
      break;
    case CS_START_SCAN:
      jam();
      /***********************************************************************/
      // The transaction is a scan. Scans cannot commit
      /***********************************************************************/
      errorCode = ZSCANINPROGRESS;
      break;
    case CS_PREPARED:
      jam();
      return;
    case CS_START_PREPARING:
      jam();
      return;
    case CS_REC_PREPARING:
      jam();
      return;
      break;
    default:
5383
      warningHandlerLab(signal, __LINE__);
5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395
      return;
    }//switch
    TcCommitRef * const commitRef = (TcCommitRef*)&signal->theData[0];
    commitRef->apiConnectPtr = apiConnectPtr;
    commitRef->transId1 = transId1;
    commitRef->transId2 = transId2;
    commitRef->errorCode = errorCode;
    sendSignal(apiBlockRef, GSN_TC_COMMITREF, signal, 
	       TcCommitRef::SignalLength, JBB);
    return;
  } else /** apiConnectptr.i < capiConnectFilesize */ {
    jam();
5396
    warningHandlerLab(signal, __LINE__);
5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418
    return;
  }
}//Dbtc::execTC_COMMITREQ()

void Dbtc::execTCROLLBACKREQ(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  apiConnectptr.i = signal->theData[0];
  if (apiConnectptr.i >= capiConnectFilesize) {
    goto TC_ROLL_warning;
  }//if
  ptrAss(apiConnectptr, apiConnectRecord);
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[1];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[2];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    jam();
    return;
  }//if

unknown's avatar
unknown committed
5419
  apiConnectptr.p->m_exec_flag = 1;
5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454
  switch (apiConnectptr.p->apiConnectstate) {
  case CS_STARTED:
  case CS_RECEIVING:
    jam();
    apiConnectptr.p->returnsignal = RS_TCROLLBACKCONF;
    abort010Lab(signal);
    return;
  case CS_CONNECTED:
    jam();
    signal->theData[0] = apiConnectptr.p->ndbapiConnect;
    signal->theData[1] = apiConnectptr.p->transid[0];
    signal->theData[2] = apiConnectptr.p->transid[1];
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_TCROLLBACKCONF, 
	       signal, 3, JBB);
    break;
  case CS_START_SCAN:
  case CS_PREPARE_TO_COMMIT:
  case CS_COMMITTING:
  case CS_COMMIT_SENT:
  case CS_COMPLETING:
  case CS_COMPLETE_SENT:
  case CS_WAIT_COMMIT_CONF:
  case CS_WAIT_COMPLETE_CONF:
  case CS_RESTART:
  case CS_DISCONNECTED:
  case CS_START_COMMITTING:
  case CS_REC_COMMITTING:
    jam();
    /* ***************< */
    /* TC_ROLLBACKREF < */
    /* ***************< */
    signal->theData[0] = apiConnectptr.p->ndbapiConnect;
    signal->theData[1] = apiConnectptr.p->transid[0];
    signal->theData[2] = apiConnectptr.p->transid[1];
    signal->theData[3] = ZROLLBACKNOTALLOWED;
5455
    signal->theData[4] = apiConnectptr.p->apiConnectstate;
5456
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_TCROLLBACKREF, 
5457
	       signal, 5, JBB);
5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491
    break;
                                                 /* SEND A REFUSAL SIGNAL*/
  case CS_ABORTING:
    jam();
    if (apiConnectptr.p->abortState == AS_IDLE) {
      jam();
      signal->theData[0] = apiConnectptr.p->ndbapiConnect;
      signal->theData[1] = apiConnectptr.p->transid[0];
      signal->theData[2] = apiConnectptr.p->transid[1];
      sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_TCROLLBACKCONF, 
		 signal, 3, JBB);
    } else {
      jam();
      apiConnectptr.p->returnsignal = RS_TCROLLBACKCONF;
    }//if
    break;
  case CS_WAIT_ABORT_CONF:
    jam();
    apiConnectptr.p->returnsignal = RS_TCROLLBACKCONF;
    break;
  case CS_START_PREPARING:
    jam();
  case CS_PREPARED:
    jam();
  case CS_REC_PREPARING:
    jam();
  default:
    goto TC_ROLL_system_error;
    break;
  }//switch
  return;

TC_ROLL_warning:
  jam();
5492
  warningHandlerLab(signal, __LINE__);
5493 5494 5495 5496
  return;

TC_ROLL_system_error:
  jam();
5497
  systemErrorLab(signal, __LINE__);
5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737
  return;
}//Dbtc::execTCROLLBACKREQ()

void Dbtc::execTC_HBREP(Signal* signal) 
{
  const TcHbRep * const tcHbRep = 
    (TcHbRep *)signal->getDataPtr();

  jamEntry();
  apiConnectptr.i = tcHbRep->apiConnectPtr;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);

  if (apiConnectptr.p->transid[0] == tcHbRep->transId1 &&
      apiConnectptr.p->transid[1] == tcHbRep->transId2){

    if (getApiConTimer(apiConnectptr.i) != 0){
      setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
    } else {
      DEBUG("TCHBREP received when timer was off apiConnectptr.i=" 
	    << apiConnectptr.i);
    }
  }
}//Dbtc::execTCHBREP()

/*
4.3.15 ABORT 
-----------
*/
/*****************************************************************************/
/*                                  A B O R T                                */
/*                                                                           */
/*****************************************************************************/
void Dbtc::warningReport(Signal* signal, int place)
{
  switch (place) {
  case 0:
    jam();
#ifdef ABORT_TRACE
    ndbout << "ABORTED to not active TC record" << endl;
#endif
    break;
  case 1:
    jam();
#ifdef ABORT_TRACE
    ndbout << "ABORTED to TC record active with new transaction" << endl;
#endif
    break;
  case 2:
    jam();
#ifdef ABORT_TRACE
    ndbout << "ABORTED to active TC record not expecting ABORTED" << endl;
#endif
    break;
  case 3:
    jam();
#ifdef ABORT_TRACE
    ndbout << "ABORTED to TC rec active with trans but wrong node" << endl;
    ndbout << "This is ok when aborting in node failure situations" << endl;
#endif
    break;
  case 4:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITTED in wrong state in Dbtc" << endl;
#endif
    break;
  case 5:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITTED with wrong transid in Dbtc" << endl;
#endif
    break;
  case 6:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETED in wrong state in Dbtc" << endl;
#endif
    break;
  case 7:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETED with wrong transid in Dbtc" << endl;
#endif
    break;
  case 8:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITCONF with tc-rec in wrong state in Dbtc" << endl;
#endif
    break;
  case 9:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITCONF with api-rec in wrong state in Dbtc" <<endl;
#endif
    break;
  case 10:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITCONF with wrong transid in Dbtc" << endl;
#endif
    break;
  case 11:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMMITCONF from wrong nodeid in Dbtc" << endl;
#endif
    break;
  case 12:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETECONF, tc-rec in wrong state in Dbtc" << endl;
#endif
    break;
  case 13:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETECONF, api-rec in wrong state in Dbtc" << endl;
#endif
    break;
  case 14:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETECONF with wrong transid in Dbtc" << endl;
#endif
    break;
  case 15:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received COMPLETECONF from wrong nodeid in Dbtc" << endl;
#endif
    break;
  case 16:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received ABORTCONF, tc-rec in wrong state in Dbtc" << endl;
#endif
    break;
  case 17:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received ABORTCONF, api-rec in wrong state in Dbtc" << endl;
#endif
    break;
  case 18:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received ABORTCONF with wrong transid in Dbtc" << endl;
#endif
    break;
  case 19:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received ABORTCONF from wrong nodeid in Dbtc" << endl;
#endif
    break;
  case 20:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Time-out waiting for ABORTCONF in Dbtc" << endl;
#endif
    break;
  case 21:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Time-out waiting for COMMITCONF in Dbtc" << endl;
#endif
    break;
  case 22:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Time-out waiting for COMPLETECONF in Dbtc" << endl;
#endif
    break;
  case 23:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received LQHKEYCONF in wrong tc-state in Dbtc" << endl;
#endif
    break;
  case 24:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received LQHKEYREF to wrong transid in Dbtc" << endl;
#endif
    break;
  case 25:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received LQHKEYREF in wrong state in Dbtc" << endl;
#endif
    break;
  case 26:
    jam();
#ifdef ABORT_TRACE
    ndbout << "Received LQHKEYCONF to wrong transid in Dbtc" << endl;
#endif
    break;
  case 27:
    jam();
    // printState(signal, 27);
#ifdef ABORT_TRACE
    ndbout << "Received LQHKEYCONF in wrong api-state in Dbtc" << endl;
#endif
    break;
  default:
    jam();
    break;
  }//switch
  return;
}//Dbtc::warningReport()

void Dbtc::errorReport(Signal* signal, int place)
{
  switch (place) {
  case 0:
    jam();
    break;
  case 1:
    jam();
    break;
  case 2:
    jam();
    break;
  case 3:
    jam();
    break;
  case 4:
    jam();
    break;
  case 5:
    jam();
    break;
  case 6:
    jam();
    break;
  default:
    jam();
    break;
  }//switch
5738
  systemErrorLab(signal, __LINE__);
5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
  return;
}//Dbtc::errorReport()

/* ------------------------------------------------------------------------- */
/* -------                       ENTER ABORTED                       ------- */
/*                                                                           */
/*-------------------------------------------------------------------------- */
void Dbtc::execABORTED(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  tcConnectptr.i = signal->theData[0];
  UintR Tnodeid = signal->theData[3];
  UintR TlastLqhInd = signal->theData[4];

  if (ERROR_INSERTED(8040)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_ABORTED, signal, 2000, 5);
    return;
  }//if
  /*------------------------------------------------------------------------
   *    ONE PARTICIPANT IN THE TRANSACTION HAS REPORTED THAT IT IS ABORTED.
   *------------------------------------------------------------------------*/
  if (tcConnectptr.i >= ctcConnectFilesize) {
    errorReport(signal, 0);
    return;
  }//if
  /*-------------------------------------------------------------------------
   *     WE HAVE TO CHECK THAT THIS IS NOT AN OLD SIGNAL BELONGING TO A     
   *     TRANSACTION ALREADY ABORTED. THIS CAN HAPPEN WHEN TIME-OUT OCCURS  
   *     IN TC WAITING FOR ABORTED.                                         
   *-------------------------------------------------------------------------*/
  ptrAss(tcConnectptr, tcConnectRecord);
  if (tcConnectptr.p->tcConnectstate != OS_ABORT_SENT) {
    warningReport(signal, 2);
    return;
    /*-----------------------------------------------------------------------*/
    // ABORTED reported on an operation not expecting ABORT.
    /*-----------------------------------------------------------------------*/
  }//if
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  if (apiConnectptr.i >= capiConnectFilesize) {
    warningReport(signal, 0);
    return;
  }//if
  ptrAss(apiConnectptr, apiConnectRecord);
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[1];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[2];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    warningReport(signal, 1);
    return;
  }//if
  if (ERROR_INSERTED(8024)) {
    jam();
5795
    systemErrorLab(signal, __LINE__);
5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877
  }//if

  /**
   * Release marker
   */
  clearCommitAckMarker(apiConnectptr.p, tcConnectptr.p);

  Uint32 i;
  Uint32 Tfound = 0;
  for (i = 0; i < tcConnectptr.p->noOfNodes; i++) {
    jam();
    if (tcConnectptr.p->tcNodedata[i] == Tnodeid) {
      /*---------------------------------------------------------------------
       * We have received ABORTED from one of the participants in this 
       * operation in this aborted transaction.
       * Record all nodes that have completed abort.
       * If last indicator is set it means that no more replica has 
       * heard of the operation and are thus also aborted.
       *---------------------------------------------------------------------*/
      jam();
      Tfound = 1;
      clearTcNodeData(signal, TlastLqhInd, i);
    }//if
  }//for
  if (Tfound == 0) {
    warningReport(signal, 3);
    return;
  }
  for (i = 0; i < tcConnectptr.p->noOfNodes; i++) {
    if (tcConnectptr.p->tcNodedata[i] != 0) {
      /*--------------------------------------------------------------------
       * There are still outstanding ABORTED's to wait for.
       *--------------------------------------------------------------------*/
      jam();
      return;
    }//if
  }//for
  tcConnectptr.p->noOfNodes = 0;
  tcConnectptr.p->tcConnectstate = OS_ABORTING;
  setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
  apiConnectptr.p->counter--;
  if (apiConnectptr.p->counter > 0) {
    jam();
    /*----------------------------------------------------------------------
     *       WE ARE STILL WAITING FOR MORE PARTICIPANTS TO SEND ABORTED.    
     *----------------------------------------------------------------------*/
    return;
  }//if
  /*------------------------------------------------------------------------*/
  /*                                                                        */
  /*     WE HAVE NOW COMPLETED THE ABORT PROCESS. WE HAVE RECEIVED ABORTED  */
  /*     FROM ALL PARTICIPANTS IN THE TRANSACTION. WE CAN NOW RELEASE ALL   */
  /*     RESOURCES CONNECTED TO THE TRANSACTION AND SEND THE ABORT RESPONSE */
  /*------------------------------------------------------------------------*/
  releaseAbortResources(signal);
}//Dbtc::execABORTED()

void Dbtc::clearTcNodeData(Signal* signal, 
                           UintR TLastLqhIndicator,
                           UintR Tstart) 
{
  UintR Ti;
  if (TLastLqhIndicator == ZTRUE) {
    for (Ti = Tstart ; Ti < tcConnectptr.p->noOfNodes; Ti++) {
      jam();
      tcConnectptr.p->tcNodedata[Ti] = 0;
    }//for
  } else {
    jam();
    tcConnectptr.p->tcNodedata[Tstart] = 0;
  }//for
}//clearTcNodeData()

void Dbtc::abortErrorLab(Signal* signal) 
{
  ptrGuard(apiConnectptr);
  ApiConnectRecord * transP = apiConnectptr.p;
  if (transP->apiConnectstate == CS_ABORTING && transP->abortState != AS_IDLE){
    jam();
    return;
  }
  transP->returnsignal = RS_TCROLLBACKREP;
5878 5879 5880 5881
  if(transP->returncode == 0){
    jam();
    transP->returncode = terrorCode;
  }
5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901
  abort010Lab(signal);
}//Dbtc::abortErrorLab()

void Dbtc::abort010Lab(Signal* signal) 
{
  ApiConnectRecord * transP = apiConnectptr.p;
  if (transP->apiConnectstate == CS_ABORTING && transP->abortState != AS_IDLE){
    jam();
    return;
  }
  transP->apiConnectstate = CS_ABORTING;
  /*------------------------------------------------------------------------*/
  /*     AN ABORT DECISION HAS BEEN TAKEN FOR SOME REASON. WE NEED TO ABORT */
  /*     ALL PARTICIPANTS IN THE TRANSACTION.                               */
  /*------------------------------------------------------------------------*/
  transP->abortState = AS_ACTIVE;
  transP->counter = 0;

  if (transP->firstTcConnect == RNIL) {
    jam();
unknown's avatar
unknown committed
5902 5903 5904
    /*--------------------------------------------------------------------*/
    /* WE HAVE NO PARTICIPANTS IN THE TRANSACTION.                        */
    /*--------------------------------------------------------------------*/
5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967
    releaseAbortResources(signal);
    return;
  }//if
  tcConnectptr.i = transP->firstTcConnect;
  abort015Lab(signal);
}//Dbtc::abort010Lab()

/*--------------------------------------------------------------------------*/
/*                                                                          */
/*       WE WILL ABORT ONE NODE PER OPERATION AT A TIME. THIS IS TO KEEP    */
/*       ERROR HANDLING OF THIS PROCESS FAIRLY SIMPLE AND TRACTABLE.        */
/*       EVEN IF NO NODE OF THIS PARTICULAR NODE NUMBER NEEDS ABORTION WE   */
/*       MUST ENSURE THAT ALL NODES ARE CHECKED. THUS A FAULTY NODE DOES    */
/*       NOT MEAN THAT ALL NODES IN AN OPERATION IS ABORTED. FOR THIS REASON*/
/*       WE SET THE TCONTINUE_ABORT TO TRUE WHEN A FAULTY NODE IS DETECTED. */
/*--------------------------------------------------------------------------*/
void Dbtc::abort015Lab(Signal* signal) 
{
  Uint32 TloopCount = 0;
ABORT020:
  jam();
  TloopCount++;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  switch (tcConnectptr.p->tcConnectstate) {
  case OS_WAIT_DIH:
  case OS_WAIT_KEYINFO:
  case OS_WAIT_ATTR:
    jam();
    /*----------------------------------------------------------------------*/
    /* WE ARE STILL WAITING FOR MORE KEYINFO/ATTRINFO. WE HAVE NOT CONTACTED*/
    /* ANY LQH YET AND SO WE CAN SIMPLY SET STATE TO ABORTING.              */
    /*----------------------------------------------------------------------*/
    tcConnectptr.p->noOfNodes = 0; // == releaseAbort(signal)
    tcConnectptr.p->tcConnectstate = OS_ABORTING;
    break;
  case OS_CONNECTED:
    jam();
    /*-----------------------------------------------------------------------
     *   WE ARE STILL IN THE INITIAL PHASE OF THIS OPERATION. 
     *   NEED NOT BOTHER ABOUT ANY LQH ABORTS.
     *-----------------------------------------------------------------------*/
    tcConnectptr.p->noOfNodes = 0; // == releaseAbort(signal)
    tcConnectptr.p->tcConnectstate = OS_ABORTING;
    break;
  case OS_PREPARED:
    jam();
  case OS_OPERATING:
    jam();
    /*----------------------------------------------------------------------
     * WE HAVE SENT LQHKEYREQ AND ARE IN SOME STATE OF EITHER STILL       
     * SENDING THE OPERATION, WAITING FOR REPLIES, WAITING FOR MORE       
     * ATTRINFO OR OPERATION IS PREPARED. WE NEED TO ABORT ALL LQH'S.     
     *----------------------------------------------------------------------*/
    releaseAndAbort(signal);
    tcConnectptr.p->tcConnectstate = OS_ABORT_SENT;
    TloopCount += 127;
    break;
  case OS_ABORTING:
    jam();
    break;
  case OS_ABORT_SENT:
    jam();
    DEBUG("ABORT_SENT state in abort015Lab(), not expected");
5968
    systemErrorLab(signal, __LINE__);
5969 5970 5971 5972
    return;
  default:
    jam();
    DEBUG("tcConnectstate = " << tcConnectptr.p->tcConnectstate);
5973
    systemErrorLab(signal, __LINE__);
5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019
    return;
  }//switch

  if (tcConnectptr.p->nextTcConnect != RNIL) {
    jam();
    tcConnectptr.i = tcConnectptr.p->nextTcConnect;
    if (TloopCount < 1024) {
      goto ABORT020;
    } else {
      jam();
      /*---------------------------------------------------------------------
       * Reset timer to avoid time-out in real-time break.
       * Increase counter to ensure that we don't think that all ABORTED have
       * been received before all have been sent.
       *---------------------------------------------------------------------*/
      apiConnectptr.p->counter++;
      setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
      signal->theData[0] = TcContinueB::ZABORT_BREAK;
      signal->theData[1] = tcConnectptr.i;
      signal->theData[2] = apiConnectptr.i;
      sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
      return;
    }//if
  }//if
  if (apiConnectptr.p->counter > 0) {
    jam();
    setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
    return;
  }//if
  /*-----------------------------------------------------------------------
   *    WE HAVE NOW COMPLETED THE ABORT PROCESS. WE HAVE RECEIVED ABORTED 
   *    FROM ALL PARTICIPANTS IN THE TRANSACTION. WE CAN NOW RELEASE ALL  
   *    RESOURCES CONNECTED TO THE TRANSACTION AND SEND THE ABORT RESPONSE
   *------------------------------------------------------------------------*/
  releaseAbortResources(signal);
}//Dbtc::abort015Lab()

/*--------------------------------------------------------------------------*/
/*       RELEASE KEY AND ATTRINFO OBJECTS AND SEND ABORT TO THE LQH BLOCK.  */
/*--------------------------------------------------------------------------*/
int Dbtc::releaseAndAbort(Signal* signal) 
{
  HostRecordPtr localHostptr;
  UintR TnoLoops = tcConnectptr.p->noOfNodes;
  
  apiConnectptr.p->counter++;
6020
  bool prevAlive = false;
6021 6022 6023 6024 6025
  for (Uint32 Ti = 0; Ti < TnoLoops ; Ti++) {
    localHostptr.i = tcConnectptr.p->tcNodedata[Ti];
    ptrCheckGuard(localHostptr, chostFilesize, hostRecord);
    if (localHostptr.p->hostStatus == HS_ALIVE) {
      jam();
6026 6027 6028 6029 6030
      if (prevAlive) {
        // if previous is alive, its LQH forwards abort to this node
        jam();
        continue;
      }
6031 6032 6033 6034 6035 6036 6037 6038 6039
      /* ************< */
      /*    ABORT    < */
      /* ************< */
      tblockref = calcLqhBlockRef(localHostptr.i);
      signal->theData[0] = tcConnectptr.i;
      signal->theData[1] = cownref;
      signal->theData[2] = apiConnectptr.p->transid[0];
      signal->theData[3] = apiConnectptr.p->transid[1];
      sendSignal(tblockref, GSN_ABORT, signal, 4, JBB);
6040
      prevAlive = true;
6041 6042 6043 6044 6045
    } else {
      jam();
      signal->theData[0] = tcConnectptr.i;
      signal->theData[1] = apiConnectptr.p->transid[0];
      signal->theData[2] = apiConnectptr.p->transid[1];
unknown's avatar
unknown committed
6046
      signal->theData[3] = localHostptr.i;
6047 6048
      signal->theData[4] = ZFALSE;
      sendSignal(cownref, GSN_ABORTED, signal, 5, JBB);
6049
      prevAlive = false;
6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088
    }//if
  }//for
  return 1;
}//Dbtc::releaseAndAbort()

/* ------------------------------------------------------------------------- */
/* -------                       ENTER TIME_SIGNAL                   ------- */
/*                                                                           */
/* ------------------------------------------------------------------------- */
void Dbtc::execTIME_SIGNAL(Signal* signal) 
{
  
  jamEntry();
  ctcTimer++;
  if (csystemStart != SSS_TRUE) {
    jam();
    return;
  }//if
  checkStartTimeout(signal);
  checkStartFragTimeout(signal);
}//Dbtc::execTIME_SIGNAL()

/*------------------------------------------------*/
/* Start timeout handling if not already going on */
/*------------------------------------------------*/
void Dbtc::checkStartTimeout(Signal* signal)
{
  ctimeOutCheckCounter++;
  if (ctimeOutCheckActive == TOCS_TRUE) {
    jam();
    // Check heartbeat of timeout loop
    if(ctimeOutCheckHeartbeat > ctimeOutCheckLastHeartbeat){
      jam();
      ctimeOutMissedHeartbeats = 0;      
    }else{
      jam();
      ctimeOutMissedHeartbeats++;
      if (ctimeOutMissedHeartbeats > 100){
	jam();
6089
	systemErrorLab(signal, __LINE__);
6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137
      }
    }
    ctimeOutCheckLastHeartbeat = ctimeOutCheckHeartbeat;
    return;
  }//if
  if (ctimeOutCheckCounter < ctimeOutCheckDelay) {
    jam();
    /*------------------------------------------------------------------*/
    /*                                                                  */
    /*       NO TIME-OUT CHECKED THIS TIME. WAIT MORE.                  */
    /*------------------------------------------------------------------*/
    return;
  }//if
  ctimeOutCheckActive = TOCS_TRUE;
  ctimeOutCheckCounter = 0;
  timeOutLoopStartLab(signal, 0); // 0 is first api connect record
  return;
}//Dbtc::execTIME_SIGNAL()

/*----------------------------------------------------------------*/
/* Start fragment (scan) timeout handling if not already going on */
/*----------------------------------------------------------------*/
void Dbtc::checkStartFragTimeout(Signal* signal)
{
  ctimeOutCheckFragCounter++;
  if (ctimeOutCheckFragActive == TOCS_TRUE) {
    jam();
    return;
  }//if
  if (ctimeOutCheckFragCounter < ctimeOutCheckDelay) {
    jam();
    /*------------------------------------------------------------------*/
    /*       NO TIME-OUT CHECKED THIS TIME. WAIT MORE.                  */
    /*------------------------------------------------------------------*/
    return;
  }//if

  // Go through the fragment records and look for timeout in a scan.
  ctimeOutCheckFragActive = TOCS_TRUE;
  ctimeOutCheckFragCounter = 0;
  timeOutLoopStartFragLab(signal, 0); // 0 means first scan record
}//checkStartFragTimeout()

/*------------------------------------------------------------------*/
/*       IT IS NOW TIME TO CHECK WHETHER ANY TRANSACTIONS HAVE      */
/*       BEEN DELAYED FOR SO LONG THAT WE ARE FORCED TO PERFORM     */
/*       SOME ACTION, EITHER ABORT OR RESEND OR REMOVE A NODE FROM  */
/*       THE WAITING PART OF A PROTOCOL.                            */
6138 6139 6140 6141 6142 6143 6144 6145 6146 6147
/*
The algorithm used here is to check 1024 transactions at a time before
doing a real-time break.
To avoid aborting both transactions in a deadlock detected by time-out
we insert a random extra time-out of upto 630 ms by using the lowest
six bits of the api connect reference.
We spread it out from 0 to 630 ms if base time-out is larger than 3 sec,
we spread it out from 0 to 70 ms if base time-out is smaller than 300 msec,
and otherwise we spread it out 310 ms.
*/
6148
/*------------------------------------------------------------------*/
6149
void Dbtc::timeOutLoopStartLab(Signal* signal, Uint32 api_con_ptr) 
6150
{
6151 6152 6153 6154
  Uint32 end_ptr, time_passed, time_out_value, mask_value;
  const Uint32 api_con_sz= capiConnectFilesize;
  const Uint32 tc_timer= ctcTimer;
  const Uint32 time_out_param= ctimeOutValue;
6155

6156
  ctimeOutCheckHeartbeat = tc_timer;
6157

6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171
  if (api_con_ptr + 1024 < api_con_sz) {
    jam();
    end_ptr= api_con_ptr + 1024;
  } else {
    jam();
    end_ptr= api_con_sz;
  }
  if (time_out_param > 300) {
    jam();
    mask_value= 63;
  } else if (time_out_param < 30) {
    jam();
    mask_value= 7;
  } else {
6172
    jam();
6173 6174 6175 6176 6177 6178
    mask_value= 31;
  }
  for ( ; api_con_ptr < end_ptr; api_con_ptr++) {
    Uint32 api_timer= getApiConTimer(api_con_ptr);
    jam();
    if (api_timer != 0) {
6179
      time_out_value= time_out_param + (api_con_ptr & mask_value);
6180
      time_passed= tc_timer - api_timer;
unknown's avatar
unknown committed
6181 6182
      if (time_passed > time_out_value) 
      {
6183
        jam();
unknown's avatar
unknown committed
6184 6185 6186
        timeOutFoundLab(signal, api_con_ptr, ZTIME_OUT_ERROR);
	api_con_ptr++;
	break;
6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203
      }
    }
  }
  if (api_con_ptr == api_con_sz) {
    jam();
    /*------------------------------------------------------------------*/
    /*                                                                  */
    /*       WE HAVE NOW CHECKED ALL TRANSACTIONS FOR TIME-OUT AND ALSO */
    /*       STARTED TIME-OUT HANDLING OF THOSE WE FOUND. WE ARE NOW    */
    /*       READY AND CAN WAIT FOR THE NEXT TIME-OUT CHECK.            */
    /*------------------------------------------------------------------*/
    ctimeOutCheckActive = TOCS_FALSE;
  } else {
    jam();
    sendContinueTimeOutControl(signal, api_con_ptr);
  }
  return;
6204 6205
}//Dbtc::timeOutLoopStartLab()

unknown's avatar
unknown committed
6206
void Dbtc::timeOutFoundLab(Signal* signal, Uint32 TapiConPtr, Uint32 errCode) 
6207 6208 6209 6210 6211 6212 6213 6214
{
  apiConnectptr.i = TapiConPtr;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  /*------------------------------------------------------------------*/
  /*                                                                  */
  /*       THIS TRANSACTION HAVE EXPERIENCED A TIME-OUT AND WE NEED TO*/
  /*       FIND OUT WHAT WE NEED TO DO BASED ON THE STATE INFORMATION.*/
  /*------------------------------------------------------------------*/
unknown's avatar
unknown committed
6215 6216 6217
  DEBUG("[ H'" << hex << apiConnectptr.p->transid[0] 
	<< " H'" << apiConnectptr.p->transid[1] << "] " << dec 
	<< "Time-out in state = " << apiConnectptr.p->apiConnectstate
6218
	<< " apiConnectptr.i = " << apiConnectptr.i 
unknown's avatar
unknown committed
6219
	<< " - exec: " << apiConnectptr.p->m_exec_flag
unknown's avatar
unknown committed
6220 6221
	<< " - place: " << c_apiConTimer_line[apiConnectptr.i]
	<< " code: " << errCode);
6222 6223 6224 6225 6226 6227 6228 6229 6230
  switch (apiConnectptr.p->apiConnectstate) {
  case CS_STARTED:
    if(apiConnectptr.p->lqhkeyreqrec == apiConnectptr.p->lqhkeyconfrec){
      jam();
      /*
      We are waiting for application to continue the transaction. In this
      particular state we will use the application timeout parameter rather
      than the shorter Deadlock detection timeout.
      */
unknown's avatar
unknown committed
6231 6232
      if (c_appl_timeout_value == 0 ||
          (ctcTimer - getApiConTimer(apiConnectptr.i)) <= c_appl_timeout_value) {
6233 6234 6235 6236
        jam();
        return;
      }//if
    }
6237
    apiConnectptr.p->returnsignal = RS_TCROLLBACKREP;      
unknown's avatar
unknown committed
6238
    apiConnectptr.p->returncode = errCode;
6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250
    abort010Lab(signal);
    return;
  case CS_RECEIVING:
  case CS_REC_COMMITTING:
  case CS_START_COMMITTING:
    jam();
    /*------------------------------------------------------------------*/
    /*       WE ARE STILL IN THE PREPARE PHASE AND THE TRANSACTION HAS  */
    /*       NOT YET REACHED ITS COMMIT POINT. THUS IT IS NOW OK TO     */
    /*       START ABORTING THE TRANSACTION. ALSO START CHECKING THE    */
    /*       REMAINING TRANSACTIONS.                                    */
    /*------------------------------------------------------------------*/
unknown's avatar
unknown committed
6251
    terrorCode = errCode;
6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277
    abortErrorLab(signal);
    return;
  case CS_COMMITTING:
    jam();
    /*------------------------------------------------------------------*/
    // We are simply waiting for a signal in the job buffer. Only extreme
    // conditions should get us here. We ignore it.
    /*------------------------------------------------------------------*/
  case CS_COMPLETING:
    jam();
    /*------------------------------------------------------------------*/
    // We are simply waiting for a signal in the job buffer. Only extreme
    // conditions should get us here. We ignore it.
    /*------------------------------------------------------------------*/
  case CS_PREPARE_TO_COMMIT:
    jam();
    /*------------------------------------------------------------------*/
    /*       WE ARE WAITING FOR DIH TO COMMIT THE TRANSACTION. WE SIMPLY*/
    /*       KEEP WAITING SINCE THERE IS NO BETTER IDEA ON WHAT TO DO.  */
    /*       IF IT IS BLOCKED THEN NO TRANSACTION WILL PASS THIS GATE.  */
    // To ensure against strange bugs we crash the system if we have passed
    // time-out period by a factor of 10 and it is also at least 5 seconds.
    /*------------------------------------------------------------------*/
    if (((ctcTimer - getApiConTimer(apiConnectptr.i)) > (10 * ctimeOutValue)) &&
        ((ctcTimer - getApiConTimer(apiConnectptr.i)) > 500)) {
        jam();
6278
        systemErrorLab(signal, __LINE__);
6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315
    }//if
    break;
  case CS_COMMIT_SENT:
    jam();
    /*------------------------------------------------------------------*/
    /*       WE HAVE SENT COMMIT TO A NUMBER OF NODES. WE ARE CURRENTLY */
    /*       WAITING FOR THEIR REPLY. WITH NODE RECOVERY SUPPORTED WE   */
    /*       WILL CHECK FOR CRASHED NODES AND RESEND THE COMMIT SIGNAL  */
    /*       TO THOSE NODES THAT HAVE MISSED THE COMMIT SIGNAL DUE TO   */
    /*       A NODE FAILURE.                                            */
    /*------------------------------------------------------------------*/
    tabortInd = ZCOMMIT_SETUP;
    setupFailData(signal);
    toCommitHandlingLab(signal);
    return;
  case CS_COMPLETE_SENT:
    jam();
    /*--------------------------------------------------------------------*/
    /*       WE HAVE SENT COMPLETE TO A NUMBER OF NODES. WE ARE CURRENTLY */
    /*       WAITING FOR THEIR REPLY. WITH NODE RECOVERY SUPPORTED WE     */
    /*       WILL CHECK FOR CRASHED NODES AND RESEND THE COMPLETE SIGNAL  */
    /*       TO THOSE NODES THAT HAVE MISSED THE COMPLETE SIGNAL DUE TO   */
    /*       A NODE FAILURE.                                              */
    /*--------------------------------------------------------------------*/
    tabortInd = ZCOMMIT_SETUP;
    setupFailData(signal);
    toCompleteHandlingLab(signal);
    return;
  case CS_ABORTING:
    jam();
    /*------------------------------------------------------------------*/
    /*       TIME-OUT DURING ABORT. WE NEED TO SEND ABORTED FOR ALL     */
    /*       NODES THAT HAVE FAILED BEFORE SENDING ABORTED.             */
    /*------------------------------------------------------------------*/
    tcConnectptr.i = apiConnectptr.p->firstTcConnect;
    sendAbortedAfterTimeout(signal, 0);
    break;
unknown's avatar
unknown committed
6316
  case CS_START_SCAN:{
6317
    jam();
unknown's avatar
unknown committed
6318 6319 6320 6321
    ScanRecordPtr scanPtr;
    scanPtr.i = apiConnectptr.p->apiScanRec;
    ptrCheckGuard(scanPtr, cscanrecFileSize, scanRecord);
    scanError(signal, scanPtr, ZSCANTIME_OUT_ERROR);
6322
    break;
unknown's avatar
unknown committed
6323
  }
6324 6325 6326 6327
  case CS_WAIT_ABORT_CONF:
    jam();
    tcConnectptr.i = apiConnectptr.p->currentTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
6328
    arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353
    hostptr.i = tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo];
    ptrCheckGuard(hostptr, chostFilesize, hostRecord);
    if (hostptr.p->hostStatus == HS_ALIVE) {
      /*------------------------------------------------------------------*/
      // Time-out waiting for ABORTCONF. We will resend the ABORTREQ just in
      // case.
      /*------------------------------------------------------------------*/
      warningReport(signal, 20);
      apiConnectptr.p->timeOutCounter++;
      if (apiConnectptr.p->timeOutCounter > 3) {
	/*------------------------------------------------------------------*/
	// 100 time-outs are not acceptable. We will shoot down the node
	// not responding.
	/*------------------------------------------------------------------*/
        reportNodeFailed(signal, hostptr.i);
      }//if
      apiConnectptr.p->currentReplicaNo++;
    }//if
    tcurrentReplicaNo = (Uint8)Z8NIL;
    toAbortHandlingLab(signal);
    return;
  case CS_WAIT_COMMIT_CONF:
    jam();
    tcConnectptr.i = apiConnectptr.p->currentTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
6354
    arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379
    hostptr.i = tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo];
    ptrCheckGuard(hostptr, chostFilesize, hostRecord);
    if (hostptr.p->hostStatus == HS_ALIVE) {
      /*------------------------------------------------------------------*/
      // Time-out waiting for COMMITCONF. We will resend the COMMITREQ just in
      // case.
      /*------------------------------------------------------------------*/
      warningReport(signal, 21);
      apiConnectptr.p->timeOutCounter++;
      if (apiConnectptr.p->timeOutCounter > 3) {
	/*------------------------------------------------------------------*/
	// 100 time-outs are not acceptable. We will shoot down the node
	// not responding.
	/*------------------------------------------------------------------*/
        reportNodeFailed(signal, hostptr.i);
      }//if
      apiConnectptr.p->currentReplicaNo++;
    }//if
    tcurrentReplicaNo = (Uint8)Z8NIL;
    toCommitHandlingLab(signal);
    return;
  case CS_WAIT_COMPLETE_CONF:
    jam();
    tcConnectptr.i = apiConnectptr.p->currentTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
6380
    arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425
    hostptr.i = tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo];
    ptrCheckGuard(hostptr, chostFilesize, hostRecord);
    if (hostptr.p->hostStatus == HS_ALIVE) {
      /*------------------------------------------------------------------*/
      // Time-out waiting for COMPLETECONF. We will resend the COMPLETEREQ
      // just in case.
      /*------------------------------------------------------------------*/
      warningReport(signal, 22);
      apiConnectptr.p->timeOutCounter++;
      if (apiConnectptr.p->timeOutCounter > 100) {
	/*------------------------------------------------------------------*/
	// 100 time-outs are not acceptable. We will shoot down the node
	// not responding.
	/*------------------------------------------------------------------*/
        reportNodeFailed(signal, hostptr.i);
      }//if
      apiConnectptr.p->currentReplicaNo++;
    }//if
    tcurrentReplicaNo = (Uint8)Z8NIL;
    toCompleteHandlingLab(signal);
    return;
  case CS_FAIL_PREPARED:
    jam();
  case CS_FAIL_COMMITTING:
    jam();
  case CS_FAIL_COMMITTED:
    jam();
  case CS_REC_PREPARING:
    jam();
  case CS_START_PREPARING:
    jam();
  case CS_PREPARED:
    jam();
  case CS_RESTART:
    jam();
  case CS_FAIL_ABORTED:
    jam();
  case CS_DISCONNECTED:
    jam();
  default:
    jam();
    /*------------------------------------------------------------------*/
    /*       AN IMPOSSIBLE STATE IS SET. CRASH THE SYSTEM.              */
    /*------------------------------------------------------------------*/
    DEBUG("State = " << apiConnectptr.p->apiConnectstate);
6426
    systemErrorLab(signal, __LINE__);
6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444
    return;
  }//switch
  return;
}//Dbtc::timeOutFoundLab()

void Dbtc::sendAbortedAfterTimeout(Signal* signal, int Tcheck)
{
  ApiConnectRecord * transP = apiConnectptr.p;
  if(transP->abortState == AS_IDLE){
    jam();
    warningEvent("TC: %d: %d state=%d abort==IDLE place: %d fop=%d t: %d", 
		 __LINE__,
		 apiConnectptr.i, 
		 transP->apiConnectstate,
		 c_apiConTimer_line[apiConnectptr.i],
		 transP->firstTcConnect,
		 c_apiConTimer[apiConnectptr.i]
		 );
6445 6446 6447 6448 6449 6450 6451 6452 6453
    ndbout_c("TC: %d: %d state=%d abort==IDLE place: %d fop=%d t: %d", 
	     __LINE__,
	     apiConnectptr.i, 
	     transP->apiConnectstate,
	     c_apiConTimer_line[apiConnectptr.i],
	     transP->firstTcConnect,
	     c_apiConTimer[apiConnectptr.i]
	     );
    ndbrequire(false);
6454 6455 6456 6457
    setApiConTimer(apiConnectptr.i, 0, __LINE__);
    return;
  }
  
unknown's avatar
unknown committed
6458
  bool found = false;
6459
  OperationState tmp[16];
6460
  
6461 6462 6463 6464 6465
  Uint32 TloopCount = 0;
  do {
    jam();
    if (tcConnectptr.i == RNIL) {
      jam();
unknown's avatar
unknown committed
6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490

#ifdef VM_TRACE
      ndbout_c("found: %d Tcheck: %d apiConnectptr.p->counter: %d",
	       found, Tcheck, apiConnectptr.p->counter);
#endif
      if (found || apiConnectptr.p->counter)
      {
	jam();
	/**
	 * We sent atleast one ABORT/ABORTED
	 *   or ZABORT_TIMEOUT_BREAK is in job buffer
	 *   wait for reception...
	 */
	return;
      }
      
      if (Tcheck == 1)
      {
	jam();
	releaseAbortResources(signal);
	return;
      }
      
      if (Tcheck == 0)
      {
6491 6492 6493 6494 6495 6496 6497 6498
        jam();
	/*------------------------------------------------------------------
	 * All nodes had already reported ABORTED for all tcConnect records.
	 * Crash since it is an error situation that we then received a 
	 * time-out.
	 *------------------------------------------------------------------*/
	char buf[96]; buf[0] = 0;
	char buf2[96];
unknown's avatar
unknown committed
6499 6500 6501 6502 6503
	BaseString::snprintf(buf, sizeof(buf), "TC %d: %d counter: %d ops:",
			     __LINE__, apiConnectptr.i,
			     apiConnectptr.p->counter);
	for(Uint32 i = 0; i<TloopCount; i++)
	{
6504 6505
	  BaseString::snprintf(buf2, sizeof(buf2), "%s %d", buf, tmp[i]);
	  BaseString::snprintf(buf, sizeof(buf), buf2);
6506 6507
	}
	warningEvent(buf);
6508 6509
	ndbout_c(buf);
	ndbrequire(false);
unknown's avatar
unknown committed
6510
	releaseAbortResources(signal);
unknown's avatar
unknown committed
6511
	return;
unknown's avatar
unknown committed
6512
      }
unknown's avatar
unknown committed
6513
      
6514 6515 6516 6517 6518 6519 6520 6521 6522 6523
      return;
    }//if
    TloopCount++;
    if (TloopCount >= 1024) {
      jam();
      /*------------------------------------------------------------------*/
      // Insert a real-time break for large transactions to avoid blowing
      // away the job buffer.
      /*------------------------------------------------------------------*/
      setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
unknown's avatar
unknown committed
6524
      apiConnectptr.p->counter++;
6525 6526 6527
      signal->theData[0] = TcContinueB::ZABORT_TIMEOUT_BREAK;
      signal->theData[1] = tcConnectptr.i;
      signal->theData[2] = apiConnectptr.i;      
unknown's avatar
unknown committed
6528 6529 6530 6531 6532 6533 6534 6535 6536 6537
      if (ERROR_INSERTED(8050))
      {
	ndbout_c("sending ZABORT_TIMEOUT_BREAK delayed (%d %d)", 
		 Tcheck, apiConnectptr.p->counter);
	sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 2000, 3);
      }
      else
      {
	sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
      }
6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554
      return;
    }//if
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    if(TloopCount < 16){
      jam();
      tmp[TloopCount-1] = tcConnectptr.p->tcConnectstate;
    }

    if (tcConnectptr.p->tcConnectstate == OS_ABORT_SENT) {
      jam();
      /*------------------------------------------------------------------*/
      // We have sent an ABORT signal to this node but not yet received any
      // reply. We have to send an ABORTED signal on our own in some cases.
      // If the node is declared as up and running and still do not respond
      // in time to the ABORT signal we will declare it as dead.
      /*------------------------------------------------------------------*/
      UintR Ti = 0;
6555
      arrGuard(tcConnectptr.p->noOfNodes, MAX_REPLICAS+1);
6556 6557 6558 6559
      for (Ti = 0; Ti < tcConnectptr.p->noOfNodes; Ti++) {
        jam();
        if (tcConnectptr.p->tcNodedata[Ti] != 0) {
          TloopCount += 31;
unknown's avatar
unknown committed
6560
	  found = true;
6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
          hostptr.i = tcConnectptr.p->tcNodedata[Ti];
          ptrCheckGuard(hostptr, chostFilesize, hostRecord);
          if (hostptr.p->hostStatus == HS_ALIVE) {
            jam();
	    /*---------------------------------------------------------------
	     * A backup replica has not sent ABORTED. 
	     * Could be that a node before him has crashed. 
	     * Send an ABORT signal specifically to this node.
	     * We will not send to any more nodes after this 
	     * to avoid race problems.
	     * To also ensure that we use this message also as a heartbeat 
	     * we will move this node to the primary replica seat. 
	     * The primary replica and any failed node after it will 
	     * be removed from the node list. Update also number of nodes. 
	     * Finally break the loop to ensure we don't mess
	     * things up by executing another loop. 
	     * We also update the timer to ensure we don't get time-out 
	     * too early.
	     *--------------------------------------------------------------*/
            BlockReference TBRef = calcLqhBlockRef(hostptr.i);
            signal->theData[0] = tcConnectptr.i;
            signal->theData[1] = cownref;
            signal->theData[2] = apiConnectptr.p->transid[0];
            signal->theData[3] = apiConnectptr.p->transid[1];
            sendSignal(TBRef, GSN_ABORT, signal, 4, JBB);
            setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
            break;
          } else {
            jam();
	    /*--------------------------------------------------------------
	     * The node we are waiting for is dead. We will send ABORTED to
	     * ourselves vicarious for the failed node.
	     *--------------------------------------------------------------*/
            setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
            signal->theData[0] = tcConnectptr.i;
            signal->theData[1] = apiConnectptr.p->transid[0];
            signal->theData[2] = apiConnectptr.p->transid[1];
            signal->theData[3] = hostptr.i;
            signal->theData[4] = ZFALSE;
            sendSignal(cownref, GSN_ABORTED, signal, 5, JBB);
          }//if
        }//if
      }//for
    }//if
    tcConnectptr.i = tcConnectptr.p->nextTcConnect;
  } while (1);
}//Dbtc::sendAbortedAfterTimeout()

void Dbtc::reportNodeFailed(Signal* signal, Uint32 nodeId)
{
  DisconnectRep * const rep = (DisconnectRep *)&signal->theData[0];
  rep->nodeId = nodeId;
  rep->err = DisconnectRep::TcReportNodeFailed;
  sendSignal(QMGR_REF, GSN_DISCONNECT_REP, signal, 
	     DisconnectRep::SignalLength, JBB);
}//Dbtc::reportNodeFailed()

/*-------------------------------------------------*/
/*      Timeout-loop for scanned fragments.        */
/*-------------------------------------------------*/
void Dbtc::timeOutLoopStartFragLab(Signal* signal, Uint32 TscanConPtr)
{
  ScanFragRecPtr timeOutPtr[8];
  UintR tfragTimer[8];
  UintR texpiredTime[8];
  UintR TloopCount = 0;
  Uint32 TtcTimer = ctcTimer;
unknown's avatar
unknown committed
6628

6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639
  while ((TscanConPtr + 8) < cscanFragrecFileSize) {
    jam();
    timeOutPtr[0].i  = TscanConPtr + 0;
    timeOutPtr[1].i  = TscanConPtr + 1;
    timeOutPtr[2].i  = TscanConPtr + 2;
    timeOutPtr[3].i  = TscanConPtr + 3;
    timeOutPtr[4].i  = TscanConPtr + 4;
    timeOutPtr[5].i  = TscanConPtr + 5;
    timeOutPtr[6].i  = TscanConPtr + 6;
    timeOutPtr[7].i  = TscanConPtr + 7;
    
unknown's avatar
unknown committed
6640 6641 6642 6643 6644 6645 6646 6647
    c_scan_frag_pool.getPtrForce(timeOutPtr[0]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[1]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[2]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[3]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[4]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[5]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[6]);
    c_scan_frag_pool.getPtrForce(timeOutPtr[7]);
6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698

    tfragTimer[0] = timeOutPtr[0].p->scanFragTimer;
    tfragTimer[1] = timeOutPtr[1].p->scanFragTimer;
    tfragTimer[2] = timeOutPtr[2].p->scanFragTimer;
    tfragTimer[3] = timeOutPtr[3].p->scanFragTimer;
    tfragTimer[4] = timeOutPtr[4].p->scanFragTimer;
    tfragTimer[5] = timeOutPtr[5].p->scanFragTimer;
    tfragTimer[6] = timeOutPtr[6].p->scanFragTimer;
    tfragTimer[7] = timeOutPtr[7].p->scanFragTimer;

    texpiredTime[0] = TtcTimer - tfragTimer[0];
    texpiredTime[1] = TtcTimer - tfragTimer[1];
    texpiredTime[2] = TtcTimer - tfragTimer[2];
    texpiredTime[3] = TtcTimer - tfragTimer[3];
    texpiredTime[4] = TtcTimer - tfragTimer[4];
    texpiredTime[5] = TtcTimer - tfragTimer[5];
    texpiredTime[6] = TtcTimer - tfragTimer[6];
    texpiredTime[7] = TtcTimer - tfragTimer[7];
    
    for (Uint32 Ti = 0; Ti < 8; Ti++) {
      jam();
      if (tfragTimer[Ti] != 0) {

        if (texpiredTime[Ti] > ctimeOutValue) {
	  jam();
	  DEBUG("Fragment timeout found:"<<
		" ctimeOutValue=" <<ctimeOutValue
		<<", texpiredTime="<<texpiredTime[Ti]<<endl
		<<"      tfragTimer="<<tfragTimer[Ti]
		<<", ctcTimer="<<ctcTimer);
          timeOutFoundFragLab(signal, TscanConPtr + Ti);
          return;
        }//if
      }//if
    }//for
    TscanConPtr += 8;
    /*----------------------------------------------------------------*/
    /* We split the process up checking 1024 fragmentrecords at a time*/
    /* to maintain real time behaviour.                               */
    /*----------------------------------------------------------------*/
    if (TloopCount++ > 128 ) {
      jam();
      signal->theData[0] = TcContinueB::ZCONTINUE_TIME_OUT_FRAG_CONTROL;
      signal->theData[1] = TscanConPtr;
      sendSignal(cownref, GSN_CONTINUEB, signal, 2, JBB);
      return;
    }//if
  }//while
  for ( ; TscanConPtr < cscanFragrecFileSize; TscanConPtr++){
    jam();
    timeOutPtr[0].i = TscanConPtr;
unknown's avatar
unknown committed
6699
    c_scan_frag_pool.getPtrForce(timeOutPtr[0]);
6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714
    if (timeOutPtr[0].p->scanFragTimer != 0) {
      texpiredTime[0] = ctcTimer - timeOutPtr[0].p->scanFragTimer;
      if (texpiredTime[0] > ctimeOutValue) {
        jam();
	DEBUG("Fragment timeout found:"<<
	      " ctimeOutValue=" <<ctimeOutValue
	      <<", texpiredTime="<<texpiredTime[0]<<endl
		<<"      tfragTimer="<<tfragTimer[0]
		<<", ctcTimer="<<ctcTimer);
        timeOutFoundFragLab(signal, TscanConPtr);
        return;
      }//if
    }//if
  }//for  
  ctimeOutCheckFragActive = TOCS_FALSE;
unknown's avatar
unknown committed
6715

6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727
  return;
}//timeOutLoopStartFragLab()

/*--------------------------------------------------------------------------*/
/*Handle the heartbeat signal from LQH in a scan process                    */
// (Set timer on fragrec.)
/*--------------------------------------------------------------------------*/
void Dbtc::execSCAN_HBREP(Signal* signal)
{
  jamEntry();

  scanFragptr.i = signal->theData[0];  
unknown's avatar
unknown committed
6728
  c_scan_frag_pool.getPtr(scanFragptr);
6729 6730 6731 6732 6733
  switch (scanFragptr.p->scanFragState){
  case ScanFragRec::LQH_ACTIVE:
    break;
  default:
    DEBUG("execSCAN_HBREP: scanFragState="<<scanFragptr.p->scanFragState);
6734
    systemErrorLab(signal, __LINE__);
6735 6736 6737
    break;
  }

unknown's avatar
unknown committed
6738
  ScanRecordPtr scanptr;
6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768
  scanptr.i = scanFragptr.p->scanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);

  apiConnectptr.i = scanptr.p->scanApiRec;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);

  if (!(apiConnectptr.p->transid[0] == signal->theData[1] &&
	apiConnectptr.p->transid[1] == signal->theData[2])){
    jam();
    /**
     * Send signal back to sender so that the crash occurs there
     */
    // Save original transid
    signal->theData[3] = signal->theData[0];
    signal->theData[4] = signal->theData[1];
    // Set transid to illegal values
    signal->theData[1] = RNIL;
    signal->theData[2] = RNIL;
    
    sendSignal(signal->senderBlockRef(), GSN_SCAN_HBREP, signal, 5, JBA);
    DEBUG("SCAN_HBREP with wrong transid("
	  <<signal->theData[3]<<", "<<signal->theData[4]<<")");
    return;
  }//if

  // Update timer on ScanFragRec
  if (scanFragptr.p->scanFragTimer != 0){
    updateBuddyTimer(apiConnectptr);
    scanFragptr.p->startFragTimer(ctcTimer);
  } else {
unknown's avatar
unknown committed
6769
    ndbassert(false);
6770 6771 6772 6773 6774 6775 6776 6777 6778 6779
    DEBUG("SCAN_HBREP when scanFragTimer was turned off");
  }
}//execSCAN_HBREP()

/*--------------------------------------------------------------------------*/
/*      Timeout has occured on a fragment which means a scan has timed out. */
/*      If this is true we have an error in LQH/ACC.                        */
/*--------------------------------------------------------------------------*/
void Dbtc::timeOutFoundFragLab(Signal* signal, UintR TscanConPtr)
{
unknown's avatar
unknown committed
6780 6781 6782
  ScanFragRecPtr ptr;
  c_scan_frag_pool.getPtr(ptr, TscanConPtr);
  DEBUG(TscanConPtr << " timeOutFoundFragLab: scanFragState = "<< ptr.p->scanFragState);
6783 6784 6785 6786 6787

  /*-------------------------------------------------------------------------*/
  // The scan fragment has expired its timeout. Check its state to decide
  // what to do.
  /*-------------------------------------------------------------------------*/
unknown's avatar
unknown committed
6788
  switch (ptr.p->scanFragState) {
6789 6790
  case ScanFragRec::WAIT_GET_PRIMCONF:
    jam();
unknown's avatar
unknown committed
6791
    ndbrequire(false);
6792
    break;
unknown's avatar
unknown committed
6793
  case ScanFragRec::LQH_ACTIVE:{
6794
    jam();
unknown's avatar
unknown committed
6795

6796 6797 6798
    /**  
     * The LQH expired it's timeout, try to close it
     */    
unknown's avatar
unknown committed
6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812
    Uint32 nodeId = refToNode(ptr.p->lqhBlockref);
    Uint32 connectCount = getNodeInfo(nodeId).m_connectCount;
    ScanRecordPtr scanptr;
    scanptr.i = ptr.p->scanRec;
    ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);

    if(connectCount != ptr.p->m_connectCount){
      jam();
      /**
       * The node has died
       */
      ptr.p->scanFragState = ScanFragRec::COMPLETED;
      ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
      
unknown's avatar
unknown committed
6813
      run.release(ptr);
unknown's avatar
unknown committed
6814 6815
      ptr.p->stopFragTimer();
    }
6816
    
unknown's avatar
unknown committed
6817 6818 6819
    scanError(signal, scanptr, ZSCAN_FRAG_LQH_ERROR);
    break;
  }
6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
  case ScanFragRec::DELIVERED:
    jam();
  case ScanFragRec::IDLE:
    jam();
  case ScanFragRec::QUEUED_FOR_DELIVERY:
    jam();
    /*-----------------------------------------------------------------------
     * Should never occur. We will simply report set the timer to zero and
     * continue. In a debug version we should crash here but not in a release
     * version. In a release version we will simply set the time-out to zero.
     *-----------------------------------------------------------------------*/
#ifdef VM_TRACE
6832
    systemErrorLab(signal, __LINE__);
6833 6834 6835 6836 6837 6838 6839 6840
#endif
    scanFragptr.p->stopFragTimer();
    break;
  default:
    jam();
    /*-----------------------------------------------------------------------
     * Non-existent state. Crash.
     *-----------------------------------------------------------------------*/
6841
    systemErrorLab(signal, __LINE__);
6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932
    break;
  }//switch
  
  signal->theData[0] = TcContinueB::ZCONTINUE_TIME_OUT_FRAG_CONTROL;
  signal->theData[1] = TscanConPtr + 1;
  sendSignal(cownref, GSN_CONTINUEB, signal, 2, JBB);
  return;  
}//timeOutFoundFragLab()


/*
  4.3.16 GCP_NOMORETRANS  
  ----------------------
*/
/*****************************************************************************
 *                         G C P _ N O M O R E T R A N S                    
 *                                                                           
 *  WHEN DBTC RECEIVES SIGNAL GCP_NOMORETRANS A CHECK IS DONE TO FIND OUT IF  
 *  THERE ARE ANY GLOBAL CHECKPOINTS GOING ON - CFIRSTGCP /= RNIL. DBTC THEN 
 *  SEARCHES THE GCP_RECORD FILE TO FIND OUT IF THERE ARE ANY TRANSACTIONS NOT
 *  CONCLUDED WITH THIS SPECIFIC CHECKPOINT - GCP_PTR:GCP_ID = TCHECK_GCP_ID.
 *  FOR EACH TRANSACTION WHERE API_CONNECTSTATE EQUALS PREPARED, COMMITTING,
 *  COMMITTED OR COMPLETING SIGNAL CONTINUEB IS SENT WITH A DELAY OF 100 MS, 
 *  THE COUNTER GCP_PTR:OUTSTANDINGAPI IS INCREASED. WHEN CONTINUEB IS RECEIVED
 *  THE COUNTER IS DECREASED AND A CHECK IS DONE TO FIND OUT IF ALL 
 *  TRANSACTIONS ARE CONCLUDED. IF SO, SIGNAL GCP_TCFINISHED IS SENT.       
 *****************************************************************************/
void Dbtc::execGCP_NOMORETRANS(Signal* signal) 
{
  jamEntry();
  tcheckGcpId = signal->theData[1];
  if (cfirstgcp != RNIL) {
    jam();
                                      /* A GLOBAL CHECKPOINT IS GOING ON */
    gcpPtr.i = cfirstgcp;             /* SET POINTER TO FIRST GCP IN QUEUE*/
    ptrCheckGuard(gcpPtr, cgcpFilesize, gcpRecord);
    if (gcpPtr.p->gcpId == tcheckGcpId) {
      jam();
      if (gcpPtr.p->firstApiConnect != RNIL) {
        jam();
        gcpPtr.p->gcpNomoretransRec = ZTRUE;
      } else {
        jam();
        gcpTcfinished(signal);
        unlinkGcp(signal);
      }//if
    } else {
      jam();
      /*------------------------------------------------------------*/
      /*       IF IT IS NOT THE FIRST THEN THERE SHOULD BE NO       */
      /*       RECORD FOR THIS GLOBAL CHECKPOINT. WE ALWAYS REMOVE  */
      /*       THE GLOBAL CHECKPOINTS IN ORDER.                     */
      /*------------------------------------------------------------*/
      gcpTcfinished(signal);
    }//if
  } else {
    jam();
    gcpTcfinished(signal);
  }//if
  return;
}//Dbtc::execGCP_NOMORETRANS()

/*****************************************************************************/
/*                                                                           */
/*                            TAKE OVER MODULE                               */
/*                                                                           */
/*****************************************************************************/
/*                                                                           */
/*    THIS PART OF TC TAKES OVER THE COMMIT/ABORT OF TRANSACTIONS WHERE THE  */
/*    NODE ACTING AS TC HAVE FAILED. IT STARTS BY QUERYING ALL NODES ABOUT   */
/*    ANY OPERATIONS PARTICIPATING IN A TRANSACTION WHERE THE TC NODE HAVE   */
/*    FAILED.                                                                */
/*                                                                           */
/*    AFTER RECEIVING INFORMATION FROM ALL NODES ABOUT OPERATION STATUS THIS */
/*    CODE WILL ENSURE THAT ALL AFFECTED TRANSACTIONS ARE PROPERLY ABORTED OR*/
/*    COMMITTED. THE ORIGINATING APPLICATION NODE WILL ALSO BE CONTACTED.    */
/*    IF THE ORIGINATING APPLICATION ALSO FAILED THEN THERE IS CURRENTLY NO  */
/*    WAY TO FIND OUT WHETHER A TRANSACTION WAS PERFORMED OR NOT.            */
/*****************************************************************************/
void Dbtc::execNODE_FAILREP(Signal* signal) 
{
  HostRecordPtr tmpHostptr;
  jamEntry();

  NodeFailRep * const nodeFail = (NodeFailRep *)&signal->theData[0];

  cfailure_nr = nodeFail->failNo;
  const Uint32 tnoOfNodes  = nodeFail->noOfNodes;
  const Uint32 tnewMasterId = nodeFail->masterNodeId;
  
  arrGuard(tnoOfNodes, MAX_NDB_NODES);
unknown's avatar
unknown committed
6933
  Uint32 i;
6934
  int index = 0;
unknown's avatar
unknown committed
6935 6936 6937 6938
  for (i = 1; i< MAX_NDB_NODES; i++) 
  {
    if(NodeBitmask::get(nodeFail->theNodes, i))
    {
6939 6940 6941 6942 6943
      cdata[index] = i;
      index++;
    }//if
  }//for

unknown's avatar
unknown committed
6944 6945
  cmasterNodeId = tnewMasterId;
  
6946 6947
  tcNodeFailptr.i = 0;
  ptrAss(tcNodeFailptr, tcFailRecord);
unknown's avatar
unknown committed
6948 6949
  for (i = 0; i < tnoOfNodes; i++) 
  {
6950
    jam();
unknown's avatar
unknown committed
6951
    hostptr.i = cdata[i];
6952
    ptrCheckGuard(hostptr, chostFilesize, hostRecord);
unknown's avatar
unknown committed
6953
    
6954 6955 6956 6957 6958
    /*------------------------------------------------------------*/
    /*       SET STATUS OF THE FAILED NODE TO DEAD SINCE IT HAS   */
    /*       FAILED.                                              */
    /*------------------------------------------------------------*/
    hostptr.p->hostStatus = HS_DEAD;
unknown's avatar
unknown committed
6959 6960
    hostptr.p->m_nf_bits = HostRecord::NF_NODE_FAIL_BITS;
    c_alive_nodes.clear(hostptr.i);
6961

unknown's avatar
unknown committed
6962 6963
    if (tcNodeFailptr.p->failStatus == FS_LISTENING) 
    {
6964 6965 6966 6967 6968
      jam();
      /*------------------------------------------------------------*/
      /*       THE CURRENT TAKE OVER CAN BE AFFECTED BY THIS NODE   */
      /*       FAILURE.                                             */
      /*------------------------------------------------------------*/
unknown's avatar
unknown committed
6969 6970
      if (hostptr.p->lqhTransStatus == LTS_ACTIVE) 
      {
6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981
	jam();
	/*------------------------------------------------------------*/
	/*       WE WERE WAITING FOR THE FAILED NODE IN THE TAKE OVER */
	/*       PROTOCOL FOR TC.                                     */
	/*------------------------------------------------------------*/
	signal->theData[0] = TcContinueB::ZNODE_TAKE_OVER_COMPLETED;
	signal->theData[1] = hostptr.i;
	sendSignal(cownref, GSN_CONTINUEB, signal, 2, JBB);
      }//if
    }//if
    
unknown's avatar
unknown committed
6982 6983
    if (getOwnNodeId() != tnewMasterId)
    {
6984
      jam();
unknown's avatar
unknown committed
6985 6986 6987 6988 6989 6990 6991
      /**
       * Only master does takeover currently
       */
      hostptr.p->m_nf_bits &= ~HostRecord::NF_TAKEOVER;
    }
    else
    {
6992
      jam();
unknown's avatar
unknown committed
6993 6994 6995 6996
      signal->theData[0] = hostptr.i;
      sendSignal(cownref, GSN_TAKE_OVERTCREQ, signal, 1, JBB);
    }

6997 6998
    checkScanActiveInFailedLqh(signal, 0, hostptr.i);
    checkWaitDropTabFailedLqh(signal, hostptr.i, 0); // nodeid, tableid
unknown's avatar
unknown committed
6999
    nodeFailCheckTransactions(signal, 0, hostptr.i);
unknown's avatar
unknown committed
7000
  }
7001 7002
}//Dbtc::execNODE_FAILREP()

unknown's avatar
unknown committed
7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021
void
Dbtc::checkNodeFailComplete(Signal* signal, 
			    Uint32 failedNodeId,
			    Uint32 bit)
{
  hostptr.i = failedNodeId;
  ptrCheckGuard(hostptr, chostFilesize, hostRecord);
  hostptr.p->m_nf_bits &= ~bit;
  if (hostptr.p->m_nf_bits == 0)
  {
    NFCompleteRep * const nfRep = (NFCompleteRep *)&signal->theData[0];
    nfRep->blockNo      = DBTC;
    nfRep->nodeId       = cownNodeid;
    nfRep->failedNodeId = hostptr.i;
    sendSignal(cdihblockref, GSN_NF_COMPLETEREP, signal, 
	       NFCompleteRep::SignalLength, JBB);
  }
}

7022
void Dbtc::checkScanActiveInFailedLqh(Signal* signal, 
unknown's avatar
unknown committed
7023 7024
				      Uint32 scanPtrI, 
				      Uint32 failedNodeId){
7025

unknown's avatar
unknown committed
7026
  ScanRecordPtr scanptr;
7027 7028 7029
  for (scanptr.i = scanPtrI; scanptr.i < cscanrecFileSize; scanptr.i++) {
    jam();
    ptrAss(scanptr, scanRecord);
7030
    bool found = false;
7031
    if (scanptr.p->scanState != ScanRecord::IDLE){
unknown's avatar
unknown committed
7032 7033 7034
      jam();
      ScanFragRecPtr ptr;
      ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
7035
      
unknown's avatar
unknown committed
7036 7037 7038 7039 7040 7041 7042 7043
      for(run.first(ptr); !ptr.isNull(); ){
	jam();
	ScanFragRecPtr curr = ptr;
	run.next(ptr);
	if (curr.p->scanFragState == ScanFragRec::LQH_ACTIVE && 
	    refToNode(curr.p->lqhBlockref) == failedNodeId){
	  jam();
	  
unknown's avatar
unknown committed
7044
	  run.release(curr);
unknown's avatar
unknown committed
7045 7046 7047 7048 7049 7050
	  curr.p->scanFragState = ScanFragRec::COMPLETED;
	  curr.p->stopFragTimer();
	  found = true;
	}
      }
    }
7051 7052 7053 7054 7055
    if(found){
      jam();
      scanError(signal, scanptr, ZSCAN_LQH_ERROR);	  
    }

7056 7057 7058 7059 7060 7061 7062
    // Send CONTINUEB to continue later
    signal->theData[0] = TcContinueB::ZCHECK_SCAN_ACTIVE_FAILED_LQH;
    signal->theData[1] = scanptr.i + 1; // Check next scanptr
    signal->theData[2] = failedNodeId;
    sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
    return;
  }//for
unknown's avatar
unknown committed
7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073

  checkNodeFailComplete(signal, failedNodeId, HostRecord::NF_CHECK_SCAN);
}

void
Dbtc::nodeFailCheckTransactions(Signal* signal, 
				Uint32 transPtrI, 
				Uint32 failedNodeId)
{
  jam();
  Ptr<ApiConnectRecord> transPtr;
unknown's avatar
unknown committed
7074 7075
  Uint32 TtcTimer = ctcTimer;
  Uint32 TapplTimeout = c_appl_timeout_value;
unknown's avatar
unknown committed
7076 7077 7078 7079 7080 7081
  for (transPtr.i = transPtrI; transPtr.i < capiConnectFilesize; transPtr.i++)
  {
    ptrCheckGuard(transPtr, capiConnectFilesize, apiConnectRecord); 
    if (transPtr.p->m_transaction_nodes.get(failedNodeId))
    {
      jam();
unknown's avatar
unknown committed
7082

unknown's avatar
unknown committed
7083 7084
      // Force timeout regardless of state      
      c_appl_timeout_value = 1;
unknown's avatar
unknown committed
7085
      setApiConTimer(transPtr.i, TtcTimer - 2, __LINE__);
unknown's avatar
unknown committed
7086
      timeOutFoundLab(signal, transPtr.i, ZNODEFAIL_BEFORE_COMMIT);
unknown's avatar
unknown committed
7087
      c_appl_timeout_value = TapplTimeout;
unknown's avatar
unknown committed
7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099
    }
    
    // Send CONTINUEB to continue later
    signal->theData[0] = TcContinueB::ZNF_CHECK_TRANSACTIONS;
    signal->theData[1] = transPtr.i + 1; // Check next
    signal->theData[2] = failedNodeId;
    sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
    return;
  }

  checkNodeFailComplete(signal, failedNodeId, 
			HostRecord::NF_CHECK_TRANSACTION);
7100 7101
}

unknown's avatar
unknown committed
7102

unknown's avatar
unknown committed
7103 7104 7105 7106 7107 7108
void
Dbtc::checkScanFragList(Signal* signal,
			Uint32 failedNodeId,
			ScanRecord * scanP, 
			ScanFragList::Head & head){
  
unknown's avatar
unknown committed
7109
  DEBUG("checkScanActiveInFailedLqh: scanFragError");
unknown's avatar
unknown committed
7110 7111
}

7112 7113 7114 7115 7116 7117
void Dbtc::execTAKE_OVERTCCONF(Signal* signal) 
{
  jamEntry();
  tfailedNodeId = signal->theData[0];
  hostptr.i = tfailedNodeId;
  ptrCheckGuard(hostptr, chostFilesize, hostRecord);
unknown's avatar
unknown committed
7118 7119 7120

  if (signal->getSendersBlockRef() != reference())
  {
7121 7122
    jam();
    return;
unknown's avatar
unknown committed
7123 7124 7125
  }
  
  checkNodeFailComplete(signal, hostptr.i, HostRecord::NF_TAKEOVER);
7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351
}//Dbtc::execTAKE_OVERTCCONF()

void Dbtc::execTAKE_OVERTCREQ(Signal* signal) 
{
  jamEntry();
  tfailedNodeId = signal->theData[0];
  tcNodeFailptr.i = 0;
  ptrAss(tcNodeFailptr, tcFailRecord);
  if (tcNodeFailptr.p->failStatus != FS_IDLE) {
    jam();
    /*------------------------------------------------------------*/
    /*       WE CAN CURRENTLY ONLY HANDLE ONE TAKE OVER AT A TIME */
    /*------------------------------------------------------------*/
    /*       IF MORE THAN ONE TAKE OVER IS REQUESTED WE WILL      */
    /*       QUEUE THE TAKE OVER AND START IT AS SOON AS THE      */
    /*       PREVIOUS ARE COMPLETED.                              */
    /*------------------------------------------------------------*/
    arrGuard(tcNodeFailptr.p->queueIndex, MAX_NDB_NODES);
    tcNodeFailptr.p->queueList[tcNodeFailptr.p->queueIndex] = tfailedNodeId;
    tcNodeFailptr.p->queueIndex = tcNodeFailptr.p->queueIndex + 1;
    return;
  }//if
  startTakeOverLab(signal);
}//Dbtc::execTAKE_OVERTCREQ()

/*------------------------------------------------------------*/
/*       INITIALISE THE HASH TABLES FOR STORING TRANSACTIONS  */
/*       AND OPERATIONS DURING TC TAKE OVER.                  */
/*------------------------------------------------------------*/
void Dbtc::startTakeOverLab(Signal* signal) 
{
  for (tindex = 0; tindex <= 511; tindex++) {
    ctransidFailHash[tindex] = RNIL;
  }//for
  for (tindex = 0; tindex <= 1023; tindex++) {
    ctcConnectFailHash[tindex] = RNIL;
  }//for
  tcNodeFailptr.p->failStatus = FS_LISTENING;
  tcNodeFailptr.p->takeOverNode = tfailedNodeId;
  for (hostptr.i = 1; hostptr.i < MAX_NDB_NODES; hostptr.i++) {
    jam();
    ptrAss(hostptr, hostRecord);
    if (hostptr.p->hostStatus == HS_ALIVE) {
      jam();
      tblockref = calcLqhBlockRef(hostptr.i);
      hostptr.p->lqhTransStatus = LTS_ACTIVE;
      signal->theData[0] = tcNodeFailptr.i;
      signal->theData[1] = cownref;
      signal->theData[2] = tfailedNodeId;
      sendSignal(tblockref, GSN_LQH_TRANSREQ, signal, 3, JBB);
    }//if
  }//for
}//Dbtc::startTakeOverLab()

/*------------------------------------------------------------*/
/*       A REPORT OF AN OPERATION WHERE TC FAILED HAS ARRIVED.*/
/*------------------------------------------------------------*/
void Dbtc::execLQH_TRANSCONF(Signal* signal) 
{
  jamEntry();
  LqhTransConf * const lqhTransConf = (LqhTransConf *)&signal->theData[0];
  
  tcNodeFailptr.i = lqhTransConf->tcRef;
  ptrCheckGuard(tcNodeFailptr, 1, tcFailRecord);
  tnodeid = lqhTransConf->lqhNodeId;
  ttransStatus = (LqhTransConf::OperationStatus)lqhTransConf->operationStatus;
  ttransid1    = lqhTransConf->transId1;
  ttransid2    = lqhTransConf->transId2;
  ttcOprec     = lqhTransConf->oldTcOpRec;
  treqinfo     = lqhTransConf->requestInfo;
  tgci         = lqhTransConf->gci;
  cnodes[0]    = lqhTransConf->nextNodeId1;
  cnodes[1]    = lqhTransConf->nextNodeId2;
  cnodes[2]    = lqhTransConf->nextNodeId3;
  const Uint32 ref = tapplRef = lqhTransConf->apiRef;
  tapplOprec   = lqhTransConf->apiOpRec;
  const Uint32 tableId = lqhTransConf->tableId;

  if (ttransStatus == LqhTransConf::LastTransConf){
    jam();
    /*------------------------------------------------------------*/
    /*       A NODE HAS REPORTED COMPLETION OF TAKE OVER REPORTING*/
    /*------------------------------------------------------------*/
    nodeTakeOverCompletedLab(signal);
    return;
  }//if
  if (ttransStatus == LqhTransConf::Marker){
    jam();
    treqinfo = 0;
    LqhTransConf::setMarkerFlag(treqinfo, 1);
  } else {
    TableRecordPtr tabPtr;
    tabPtr.i = tableId;
    ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);
    switch((DictTabInfo::TableType)tabPtr.p->tableType){
    case DictTabInfo::SystemTable:
    case DictTabInfo::UserTable:
      break;
    default:
      tapplRef = 0;
      tapplOprec = 0;
    }
  }

  findApiConnectFail(signal);

  if(apiConnectptr.p->ndbapiBlockref == 0 && tapplRef != 0){
    apiConnectptr.p->ndbapiBlockref = ref;
    apiConnectptr.p->ndbapiConnect = tapplOprec;
  }
  
  if (ttransStatus != LqhTransConf::Marker){
    jam();
    findTcConnectFail(signal);
  }
}//Dbtc::execLQH_TRANSCONF()

/*------------------------------------------------------------*/
/*       A NODE HAS REPORTED COMPLETION OF TAKE OVER REPORTING*/
/*------------------------------------------------------------*/
void Dbtc::nodeTakeOverCompletedLab(Signal* signal) 
{
  Uint32 guard0;

  hostptr.i = tnodeid;
  ptrCheckGuard(hostptr, chostFilesize, hostRecord);
  hostptr.p->lqhTransStatus = LTS_IDLE;
  for (hostptr.i = 1; hostptr.i < MAX_NDB_NODES; hostptr.i++) {
    jam();
    ptrAss(hostptr, hostRecord);
    if (hostptr.p->hostStatus == HS_ALIVE) {
      if (hostptr.p->lqhTransStatus == LTS_ACTIVE) {
        jam();
	/*------------------------------------------------------------*/
	/*       NOT ALL NODES ARE COMPLETED WITH REPORTING IN THE    */
	/*       TAKE OVER.                                           */
	/*------------------------------------------------------------*/
        return;
      }//if
    }//if
  }//for
  /*------------------------------------------------------------*/
  /*       ALL NODES HAVE REPORTED ON THE STATUS OF THE VARIOUS */
  /*       OPERATIONS THAT WAS CONTROLLED BY THE FAILED TC. WE  */
  /*       ARE NOW IN A POSITION TO COMPLETE ALL OF THOSE       */
  /*       TRANSACTIONS EITHER IN A SUCCESSFUL WAY OR IN AN     */
  /*       UNSUCCESSFUL WAY. WE WILL ALSO REPORT THIS CONCLUSION*/
  /*       TO THE APPLICATION IF THAT IS STILL ALIVE.           */
  /*------------------------------------------------------------*/
  tcNodeFailptr.p->currentHashIndexTakeOver = 0;
  tcNodeFailptr.p->completedTakeOver = 0;
  tcNodeFailptr.p->failStatus = FS_COMPLETING;
  guard0 = cnoParallelTakeOver - 1;
  /*------------------------------------------------------------*/
  /*       WE WILL COMPLETE THE TRANSACTIONS BY STARTING A      */
  /*       NUMBER OF PARALLEL ACTIVITIES. EACH ACTIVITY WILL    */
  /*       COMPLETE ONE TRANSACTION AT A TIME AND IN THAT       */
  /*       TRANSACTION IT WILL COMPLETE ONE OPERATION AT A TIME.*/
  /*       WHEN ALL ACTIVITIES ARE COMPLETED THEN THE TAKE OVER */
  /*       IS COMPLETED.                                        */
  /*------------------------------------------------------------*/
  arrGuard(guard0, MAX_NDB_NODES);
  for (tindex = 0; tindex <= guard0; tindex++) {
    jam();
    tcNodeFailptr.p->takeOverProcState[tindex] = ZTAKE_OVER_ACTIVE;
    signal->theData[0] = TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER;
    signal->theData[1] = tcNodeFailptr.i;
    signal->theData[2] = tindex;
    sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
  }//for
}//Dbtc::nodeTakeOverCompletedLab()

/*------------------------------------------------------------*/
/*       COMPLETE A NEW TRANSACTION FROM THE HASH TABLE OF    */
/*       TRANSACTIONS TO COMPLETE.                            */
/*------------------------------------------------------------*/
void Dbtc::completeTransAtTakeOverLab(Signal* signal, UintR TtakeOverInd) 
{
  jam();
  while (tcNodeFailptr.p->currentHashIndexTakeOver < 512){
    jam();
    apiConnectptr.i = 
        ctransidFailHash[tcNodeFailptr.p->currentHashIndexTakeOver];
    if (apiConnectptr.i != RNIL) {
      jam();
      /*------------------------------------------------------------*/
      /*       WE HAVE FOUND A TRANSACTION THAT NEEDS TO BE         */
      /*       COMPLETED. REMOVE IT FROM THE HASH TABLE SUCH THAT   */
      /*       NOT ANOTHER ACTIVITY ALSO TRIES TO COMPLETE THIS     */
      /*       TRANSACTION.                                         */
      /*------------------------------------------------------------*/
      ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
      ctransidFailHash[tcNodeFailptr.p->currentHashIndexTakeOver] = 
        apiConnectptr.p->nextApiConnect;

      completeTransAtTakeOverDoOne(signal, TtakeOverInd);
      // One transaction taken care of, return from this function
      // and wait for the next CONTINUEB to continue processing
      break;

    } else {
      if (tcNodeFailptr.p->currentHashIndexTakeOver < 511){
        jam();      
        tcNodeFailptr.p->currentHashIndexTakeOver++;
      } else {
        jam();
        completeTransAtTakeOverDoLast(signal, TtakeOverInd); 
        tcNodeFailptr.p->currentHashIndexTakeOver++;
      }//if
    }//if
  }//while
}//Dbtc::completeTransAtTakeOverLab()




void Dbtc::completeTransAtTakeOverDoLast(Signal* signal, UintR TtakeOverInd) 
{
  Uint32 guard0;
  /*------------------------------------------------------------*/
  /*       THERE ARE NO MORE TRANSACTIONS TO COMPLETE. THIS     */
  /*       ACTIVITY IS COMPLETED.                               */
  /*------------------------------------------------------------*/
  arrGuard(TtakeOverInd, MAX_NDB_NODES);
  if (tcNodeFailptr.p->takeOverProcState[TtakeOverInd] != ZTAKE_OVER_ACTIVE) {
    jam();
7352
    systemErrorLab(signal, __LINE__);
7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364
    return;
  }//if
  tcNodeFailptr.p->takeOverProcState[TtakeOverInd] = ZTAKE_OVER_IDLE;
  tcNodeFailptr.p->completedTakeOver++;

  if (tcNodeFailptr.p->completedTakeOver == cnoParallelTakeOver) {
    jam();
    /*------------------------------------------------------------*/
    /*       WE WERE THE LAST ACTIVITY THAT WAS COMPLETED. WE NEED*/
    /*       TO REPORT THE COMPLETION OF THE TAKE OVER TO ALL     */
    /*       NODES THAT ARE ALIVE.                                */
    /*------------------------------------------------------------*/
unknown's avatar
unknown committed
7365 7366 7367 7368
    NodeReceiverGroup rg(DBTC, c_alive_nodes);
    signal->theData[0] = tcNodeFailptr.p->takeOverNode;
    sendSignal(rg, GSN_TAKE_OVERTCCONF, signal, 1, JBB);
    
7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472
    if (tcNodeFailptr.p->queueIndex > 0) {
      jam();
      /*------------------------------------------------------------*/
      /*       THERE ARE MORE NODES TO TAKE OVER. WE NEED TO START  */
      /*       THE TAKE OVER.                                       */
      /*------------------------------------------------------------*/
      tfailedNodeId = tcNodeFailptr.p->queueList[0];
      guard0 = tcNodeFailptr.p->queueIndex - 1;
      arrGuard(guard0 + 1, MAX_NDB_NODES);
      for (tindex = 0; tindex <= guard0; tindex++) {
        jam();
        tcNodeFailptr.p->queueList[tindex] = 
          tcNodeFailptr.p->queueList[tindex + 1];
      }//for
      tcNodeFailptr.p->queueIndex--;
      startTakeOverLab(signal);
      return;
    } else {
      jam();
      tcNodeFailptr.p->failStatus = FS_IDLE;
    }//if
  }//if
  return;
}//Dbtc::completeTransAtTakeOverDoLast()

void Dbtc::completeTransAtTakeOverDoOne(Signal* signal, UintR TtakeOverInd)
{
  apiConnectptr.p->takeOverRec = (Uint8)tcNodeFailptr.i;
  apiConnectptr.p->takeOverInd = TtakeOverInd;

  switch (apiConnectptr.p->apiConnectstate) {
  case CS_FAIL_COMMITTED:
    jam();
    /*------------------------------------------------------------*/
    /*       ALL PARTS OF THE TRANSACTIONS REPORTED COMMITTED. WE */
    /*       HAVE THUS COMPLETED THE COMMIT PHASE. WE CAN REPORT  */
    /*       COMMITTED TO THE APPLICATION AND CONTINUE WITH THE   */
    /*       COMPLETE PHASE.                                      */
    /*------------------------------------------------------------*/
    sendTCKEY_FAILCONF(signal, apiConnectptr.p);
    tcConnectptr.i = apiConnectptr.p->firstTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    apiConnectptr.p->currentTcConnect = tcConnectptr.i;
    apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
    tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
    toCompleteHandlingLab(signal);
    return;
  case CS_FAIL_COMMITTING:
    jam();
    /*------------------------------------------------------------*/
    /*       AT LEAST ONE PART WAS ONLY PREPARED AND AT LEAST ONE */
    /*       PART WAS COMMITTED. COMPLETE THE COMMIT PHASE FIRST. */
    /*       THEN CONTINUE AS AFTER COMMITTED.                    */
    /*------------------------------------------------------------*/
    tcConnectptr.i = apiConnectptr.p->firstTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    apiConnectptr.p->currentTcConnect = tcConnectptr.i;
    apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
    tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
    toCommitHandlingLab(signal);
    return;
  case CS_FAIL_ABORTING:
  case CS_FAIL_PREPARED:
    jam();
    /*------------------------------------------------------------*/
    /*       WE WILL ABORT THE TRANSACTION IF IT IS IN A PREPARED */
    /*       STATE IN THIS VERSION. IN LATER VERSIONS WE WILL     */
    /*       HAVE TO ADD CODE FOR HANDLING OF PREPARED-TO-COMMIT  */
    /*       TRANSACTIONS. THESE ARE NOT ALLOWED TO ABORT UNTIL WE*/
    /*       HAVE HEARD FROM THE TRANSACTION COORDINATOR.         */
    /*                                                            */
    /*       IT IS POSSIBLE TO COMMIT TRANSACTIONS THAT ARE       */
    /*       PREPARED ACTUALLY. WE WILL LEAVE THIS PROBLEM UNTIL  */
    /*       LATER VERSIONS.                                      */
    /*------------------------------------------------------------*/
    tcConnectptr.i = apiConnectptr.p->firstTcConnect;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    apiConnectptr.p->currentTcConnect = tcConnectptr.i;
    apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
    tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
    toAbortHandlingLab(signal);
    return;
  case CS_FAIL_ABORTED:
    jam();
    sendTCKEY_FAILREF(signal, apiConnectptr.p);
    
    signal->theData[0] = TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER;
    signal->theData[1] = (UintR)apiConnectptr.p->takeOverRec;
    signal->theData[2] = apiConnectptr.p->takeOverInd;
    sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
    releaseTakeOver(signal);
    break;
  case CS_FAIL_COMPLETED:
    jam();
    sendTCKEY_FAILCONF(signal, apiConnectptr.p);
    
    signal->theData[0] = TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER;
    signal->theData[1] = (UintR)apiConnectptr.p->takeOverRec;
    signal->theData[2] = apiConnectptr.p->takeOverInd;
    sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
    releaseApiConnectFail(signal);
    break;
  default:
    jam();
7473
    systemErrorLab(signal, __LINE__);
7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492
    return;
  }//switch
}//Dbtc::completeTransAtTakeOverDoOne()

void 
Dbtc::sendTCKEY_FAILREF(Signal* signal, const ApiConnectRecord * regApiPtr){
  jam();

  const Uint32 ref = regApiPtr->ndbapiBlockref;
  if(ref != 0){
    signal->theData[0] = regApiPtr->ndbapiConnect;
    signal->theData[1] = regApiPtr->transid[0];
    signal->theData[2] = regApiPtr->transid[1];
  
    sendSignal(ref, GSN_TCKEY_FAILREF, signal, 3, JBB);
  }
}

void 
unknown's avatar
unknown committed
7493
Dbtc::sendTCKEY_FAILCONF(Signal* signal, ApiConnectRecord * regApiPtr){
7494 7495 7496
  jam();
  TcKeyFailConf * const failConf = (TcKeyFailConf *)&signal->theData[0];
  
unknown's avatar
unknown committed
7497 7498 7499 7500 7501 7502 7503 7504 7505
  const Uint32 ref = regApiPtr->ndbapiBlockref;
  const Uint32 marker = regApiPtr->commitAckMarker;
  if(ref != 0){
    failConf->apiConnectPtr = regApiPtr->ndbapiConnect | (marker != RNIL);
    failConf->transId1 = regApiPtr->transid[0];
    failConf->transId2 = regApiPtr->transid[1];
    
    sendSignal(regApiPtr->ndbapiBlockref,
	       GSN_TCKEY_FAILCONF, signal, TcKeyFailConf::SignalLength, JBB);
7506
  }
unknown's avatar
unknown committed
7507
  regApiPtr->commitAckMarker = RNIL;
7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549
}

/*------------------------------------------------------------*/
/*       THIS PART HANDLES THE ABORT PHASE IN THE CASE OF A   */
/*       NODE FAILURE BEFORE THE COMMIT DECISION.             */
/*------------------------------------------------------------*/
/*       ABORT REQUEST SUCCESSFULLY COMPLETED ON TNODEID      */
/*------------------------------------------------------------*/
void Dbtc::execABORTCONF(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  tcConnectptr.i = signal->theData[0];
  tnodeid = signal->theData[2];
  if (ERROR_INSERTED(8045)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_ABORTCONF, signal, 2000, 5);
    return;
  }//if
  if (tcConnectptr.i >= ctcConnectFilesize) {
    errorReport(signal, 5);
    return;
  }//if
  ptrAss(tcConnectptr, tcConnectRecord);
  if (tcConnectptr.p->tcConnectstate != OS_WAIT_ABORT_CONF) {
    warningReport(signal, 16);
    return;
  }//if
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  if (apiConnectptr.p->apiConnectstate != CS_WAIT_ABORT_CONF) {
    warningReport(signal, 17);
    return;
  }//if
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[3];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[4];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    warningReport(signal, 18);
    return;
  }//if
7550
  arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565
  if (tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo] !=
      tnodeid) {
    warningReport(signal, 19);
    return;
  }//if
  tcurrentReplicaNo = (Uint8)Z8NIL;
  tcConnectptr.p->tcConnectstate = OS_ABORTING;
  toAbortHandlingLab(signal);
}//Dbtc::execABORTCONF()

void Dbtc::toAbortHandlingLab(Signal* signal) 
{
  do {
    if (tcurrentReplicaNo != (Uint8)Z8NIL) {
      jam();
7566
      arrGuard(tcurrentReplicaNo, MAX_REPLICAS);
7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598
      const LqhTransConf::OperationStatus stat = 
	(LqhTransConf::OperationStatus)
	tcConnectptr.p->failData[tcurrentReplicaNo];
      switch(stat){
      case LqhTransConf::InvalidStatus:
      case LqhTransConf::Aborted:
        jam();
        /*empty*/;
        break;
      case LqhTransConf::Prepared:
        jam();
        hostptr.i = tcConnectptr.p->tcNodedata[tcurrentReplicaNo];
        ptrCheckGuard(hostptr, chostFilesize, hostRecord);
        if (hostptr.p->hostStatus == HS_ALIVE) {
          jam();
          tblockref = calcLqhBlockRef(hostptr.i);
          setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
          tcConnectptr.p->tcConnectstate = OS_WAIT_ABORT_CONF;
          apiConnectptr.p->apiConnectstate = CS_WAIT_ABORT_CONF;
          apiConnectptr.p->timeOutCounter = 0;
          signal->theData[0] = tcConnectptr.i;
          signal->theData[1] = cownref;
          signal->theData[2] = apiConnectptr.p->transid[0];
          signal->theData[3] = apiConnectptr.p->transid[1];
          signal->theData[4] = apiConnectptr.p->tcBlockref;
          signal->theData[5] = tcConnectptr.p->tcOprec;
          sendSignal(tblockref, GSN_ABORTREQ, signal, 6, JBB);
          return;
        }//if
        break;
      default:
        jam();
7599
        systemErrorLab(signal, __LINE__);
7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699
        return;
      }//switch
    }//if
    if (apiConnectptr.p->currentReplicaNo > 0) {
      jam();
      /*------------------------------------------------------------*/
      /*       THERE IS STILL ANOTHER REPLICA THAT NEEDS TO BE      */
      /*       ABORTED.                                             */
      /*------------------------------------------------------------*/
      apiConnectptr.p->currentReplicaNo--;
      tcurrentReplicaNo = apiConnectptr.p->currentReplicaNo;
    } else {
      /*------------------------------------------------------------*/
      /*       THE LAST REPLICA IN THIS OPERATION HAVE COMMITTED.   */
      /*------------------------------------------------------------*/
      tcConnectptr.i = tcConnectptr.p->nextTcConnect;
      if (tcConnectptr.i == RNIL) {
	/*------------------------------------------------------------*/
	/*       WE HAVE COMPLETED THE ABORT PHASE. WE CAN NOW REPORT */
	/*       THE ABORT STATUS TO THE APPLICATION AND CONTINUE     */
	/*       WITH THE NEXT TRANSACTION.                           */
	/*------------------------------------------------------------*/
        if (apiConnectptr.p->takeOverRec != (Uint8)Z8NIL) {
          jam();
	  sendTCKEY_FAILREF(signal, apiConnectptr.p);
	  const Uint32 marker = apiConnectptr.p->commitAckMarker;
          if(marker != RNIL){
	    jam();

            CommitAckMarkerPtr tmp;
            tmp.i = marker;
            tmp.p = m_commitAckMarkerHash.getPtr(tmp.i);
            
            m_commitAckMarkerHash.release(tmp);
            apiConnectptr.p->commitAckMarker = RNIL;
          }
          
	  /*------------------------------------------------------------*/
	  /*       WE HAVE COMPLETED THIS TRANSACTION NOW AND CAN       */
	  /*       CONTINUE THE PROCESS WITH THE NEXT TRANSACTION.      */
	  /*------------------------------------------------------------*/
          signal->theData[0] = TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER;
          signal->theData[1] = (UintR)apiConnectptr.p->takeOverRec;
          signal->theData[2] = apiConnectptr.p->takeOverInd;
          sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
          releaseTakeOver(signal);
        } else {
          jam();
          releaseAbortResources(signal);
        }//if
        return;
      }//if
      apiConnectptr.p->currentTcConnect = tcConnectptr.i;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
      tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
    }//if
  } while (1);
}//Dbtc::toAbortHandlingLab()

/*------------------------------------------------------------*/
/*       THIS PART HANDLES THE COMMIT PHASE IN THE CASE OF A  */
/*       NODE FAILURE IN THE MIDDLE OF THE COMMIT PHASE.      */
/*------------------------------------------------------------*/
/*       COMMIT REQUEST SUCCESSFULLY COMPLETED ON TNODEID     */
/*------------------------------------------------------------*/
void Dbtc::execCOMMITCONF(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  tcConnectptr.i = signal->theData[0];
  tnodeid = signal->theData[1];
  if (ERROR_INSERTED(8046)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_COMMITCONF, signal, 2000, 4);
    return;
  }//if
  if (tcConnectptr.i >= ctcConnectFilesize) {
    errorReport(signal, 4);
    return;
  }//if
  ptrAss(tcConnectptr, tcConnectRecord);
  if (tcConnectptr.p->tcConnectstate != OS_WAIT_COMMIT_CONF) {
    warningReport(signal, 8);
    return;
  }//if
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  if (apiConnectptr.p->apiConnectstate != CS_WAIT_COMMIT_CONF) {
    warningReport(signal, 9);
    return;
  }//if
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[2];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[3];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    warningReport(signal, 10);
    return;
  }//if
7700
  arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
7701 7702 7703 7704 7705 7706 7707
  if (tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo] !=
      tnodeid) {
    warningReport(signal, 11);
    return;
  }//if
  if (ERROR_INSERTED(8026)) {
    jam();
7708
    systemErrorLab(signal, __LINE__);
7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719
  }//if
  tcurrentReplicaNo = (Uint8)Z8NIL;
  tcConnectptr.p->tcConnectstate = OS_COMMITTED;
  toCommitHandlingLab(signal);
}//Dbtc::execCOMMITCONF()

void Dbtc::toCommitHandlingLab(Signal* signal) 
{
  do {
    if (tcurrentReplicaNo != (Uint8)Z8NIL) {
      jam();
7720
      arrGuard(tcurrentReplicaNo, MAX_REPLICAS);
7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757
      switch (tcConnectptr.p->failData[tcurrentReplicaNo]) {
      case LqhTransConf::InvalidStatus:
        jam();
        /*empty*/;
        break;
      case LqhTransConf::Committed:
        jam();
        /*empty*/;
        break;
      case LqhTransConf::Prepared:
        jam();
        /*------------------------------------------------------------*/
        /*       THE NODE WAS PREPARED AND IS WAITING FOR ABORT OR    */
        /*       COMMIT REQUEST FROM TC.                              */
        /*------------------------------------------------------------*/
        hostptr.i = tcConnectptr.p->tcNodedata[tcurrentReplicaNo];
        ptrCheckGuard(hostptr, chostFilesize, hostRecord);
        if (hostptr.p->hostStatus == HS_ALIVE) {
          jam();
          tblockref = calcLqhBlockRef(hostptr.i);
          setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
          apiConnectptr.p->apiConnectstate = CS_WAIT_COMMIT_CONF;
          apiConnectptr.p->timeOutCounter = 0;
          tcConnectptr.p->tcConnectstate = OS_WAIT_COMMIT_CONF;
          signal->theData[0] = tcConnectptr.i;
          signal->theData[1] = cownref;
          signal->theData[2] = apiConnectptr.p->globalcheckpointid;
          signal->theData[3] = apiConnectptr.p->transid[0];
          signal->theData[4] = apiConnectptr.p->transid[1];
          signal->theData[5] = apiConnectptr.p->tcBlockref;
          signal->theData[6] = tcConnectptr.p->tcOprec;
          sendSignal(tblockref, GSN_COMMITREQ, signal, 7, JBB);
          return;
        }//if
        break;
      default:
        jam();
7758
        systemErrorLab(signal, __LINE__);
7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844
        return;
        break;
      }//switch
    }//if
    if (apiConnectptr.p->currentReplicaNo > 0) {
      jam();
      /*------------------------------------------------------------*/
      /*       THERE IS STILL ANOTHER REPLICA THAT NEEDS TO BE      */
      /*       COMMITTED.                                           */
      /*------------------------------------------------------------*/
      apiConnectptr.p->currentReplicaNo--;
      tcurrentReplicaNo = apiConnectptr.p->currentReplicaNo;
    } else {
      /*------------------------------------------------------------*/
      /*       THE LAST REPLICA IN THIS OPERATION HAVE COMMITTED.   */
      /*------------------------------------------------------------*/
      tcConnectptr.i = tcConnectptr.p->nextTcConnect;
      if (tcConnectptr.i == RNIL) {
	/*------------------------------------------------------------*/
	/*       WE HAVE COMPLETED THE COMMIT PHASE. WE CAN NOW REPORT*/
	/*       THE COMMIT STATUS TO THE APPLICATION AND CONTINUE    */
	/*       WITH THE COMPLETE PHASE.                             */
	/*------------------------------------------------------------*/
        if (apiConnectptr.p->takeOverRec != (Uint8)Z8NIL) {
          jam();
	  sendTCKEY_FAILCONF(signal, apiConnectptr.p);
	} else {
          jam();
          sendApiCommit(signal);
        }//if
        apiConnectptr.p->currentTcConnect = apiConnectptr.p->firstTcConnect;
        tcConnectptr.i = apiConnectptr.p->firstTcConnect;
        ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
        tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
        apiConnectptr.p->currentReplicaNo = tcurrentReplicaNo;
        toCompleteHandlingLab(signal);
        return;
      }//if
      apiConnectptr.p->currentTcConnect = tcConnectptr.i;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
      tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
    }//if
  } while (1);
}//Dbtc::toCommitHandlingLab()

/*------------------------------------------------------------*/
/*       COMMON PART TO HANDLE COMPLETE PHASE WHEN ANY NODE   */
/*       HAVE FAILED.                                         */
/*------------------------------------------------------------*/
/*       THE NODE WITH TNODEID HAVE COMPLETED THE OPERATION   */
/*------------------------------------------------------------*/
void Dbtc::execCOMPLETECONF(Signal* signal) 
{
  UintR compare_transid1, compare_transid2;

  jamEntry();
  tcConnectptr.i = signal->theData[0];
  tnodeid = signal->theData[1];
  if (ERROR_INSERTED(8047)) {
    CLEAR_ERROR_INSERT_VALUE;
    sendSignalWithDelay(cownref, GSN_COMPLETECONF, signal, 2000, 4);
    return;
  }//if
  if (tcConnectptr.i >= ctcConnectFilesize) {
    errorReport(signal, 3);
    return;
  }//if
  ptrAss(tcConnectptr, tcConnectRecord);
  if (tcConnectptr.p->tcConnectstate != OS_WAIT_COMPLETE_CONF) {
    warningReport(signal, 12);
    return;
  }//if
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  if (apiConnectptr.p->apiConnectstate != CS_WAIT_COMPLETE_CONF) {
    warningReport(signal, 13);
    return;
  }//if
  compare_transid1 = apiConnectptr.p->transid[0] ^ signal->theData[2];
  compare_transid2 = apiConnectptr.p->transid[1] ^ signal->theData[3];
  compare_transid1 = compare_transid1 | compare_transid2;
  if (compare_transid1 != 0) {
    warningReport(signal, 14);
    return;
  }//if
7845
  arrGuard(apiConnectptr.p->currentReplicaNo, MAX_REPLICAS);
7846 7847 7848 7849 7850 7851 7852
  if (tcConnectptr.p->tcNodedata[apiConnectptr.p->currentReplicaNo] !=
      tnodeid) {
    warningReport(signal, 15);
    return;
  }//if
  if (ERROR_INSERTED(8028)) {
    jam();
7853
    systemErrorLab(signal, __LINE__);
7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864
  }//if
  tcConnectptr.p->tcConnectstate = OS_COMPLETED;
  tcurrentReplicaNo = (Uint8)Z8NIL;
  toCompleteHandlingLab(signal);
}//Dbtc::execCOMPLETECONF()

void Dbtc::toCompleteHandlingLab(Signal* signal) 
{
  do {
    if (tcurrentReplicaNo != (Uint8)Z8NIL) {
      jam();
7865
      arrGuard(tcurrentReplicaNo, MAX_REPLICAS);
7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967
      switch (tcConnectptr.p->failData[tcurrentReplicaNo]) {
      case LqhTransConf::InvalidStatus:
        jam();
        /*empty*/;
        break;
      default:
        jam();
        /*------------------------------------------------------------*/
        /*       THIS NODE DID NOT REPORT ANYTHING FOR THIS OPERATION */
        /*       IT MUST HAVE FAILED.                                 */
        /*------------------------------------------------------------*/
        /*------------------------------------------------------------*/
        /*       SEND COMPLETEREQ TO THE NEXT REPLICA.                */
        /*------------------------------------------------------------*/
        hostptr.i = tcConnectptr.p->tcNodedata[tcurrentReplicaNo];
        ptrCheckGuard(hostptr, chostFilesize, hostRecord);
        if (hostptr.p->hostStatus == HS_ALIVE) {
          jam();
          tblockref = calcLqhBlockRef(hostptr.i);
          setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
          tcConnectptr.p->tcConnectstate = OS_WAIT_COMPLETE_CONF;
          apiConnectptr.p->apiConnectstate = CS_WAIT_COMPLETE_CONF;
          apiConnectptr.p->timeOutCounter = 0;
          tcConnectptr.p->apiConnect = apiConnectptr.i;
          signal->theData[0] = tcConnectptr.i;
          signal->theData[1] = cownref;
          signal->theData[2] = apiConnectptr.p->transid[0];
          signal->theData[3] = apiConnectptr.p->transid[1];
          signal->theData[4] = apiConnectptr.p->tcBlockref;
          signal->theData[5] = tcConnectptr.p->tcOprec;
          sendSignal(tblockref, GSN_COMPLETEREQ, signal, 6, JBB);
          return;
        }//if
        break;
      }//switch
    }//if
    if (apiConnectptr.p->currentReplicaNo != 0) {
      jam();
      /*------------------------------------------------------------*/
      /*       THERE ARE STILL MORE REPLICAS IN THIS OPERATION. WE  */
      /*       NEED TO CONTINUE WITH THOSE REPLICAS.                */
      /*------------------------------------------------------------*/
      apiConnectptr.p->currentReplicaNo--;
      tcurrentReplicaNo = apiConnectptr.p->currentReplicaNo;
    } else {
      tcConnectptr.i = tcConnectptr.p->nextTcConnect;
      if (tcConnectptr.i == RNIL) {
        /*------------------------------------------------------------*/
        /*       WE HAVE COMPLETED THIS TRANSACTION NOW AND CAN       */
        /*       CONTINUE THE PROCESS WITH THE NEXT TRANSACTION.      */
        /*------------------------------------------------------------*/
        if (apiConnectptr.p->takeOverRec != (Uint8)Z8NIL) {
          jam();
          signal->theData[0] = TcContinueB::ZCOMPLETE_TRANS_AT_TAKE_OVER;
          signal->theData[1] = (UintR)apiConnectptr.p->takeOverRec;
          signal->theData[2] = apiConnectptr.p->takeOverInd;
          sendSignal(cownref, GSN_CONTINUEB, signal, 3, JBB);
          releaseTakeOver(signal);
        } else {
          jam();
          releaseTransResources(signal);
        }//if
        return;
      }//if
      /*------------------------------------------------------------*/
      /*       WE HAVE COMPLETED AN OPERATION AND THERE ARE MORE TO */
      /*       COMPLETE. TAKE THE NEXT OPERATION AND START WITH THE */
      /*       FIRST REPLICA SINCE IT IS THE COMPLETE PHASE.        */
      /*------------------------------------------------------------*/
      apiConnectptr.p->currentTcConnect = tcConnectptr.i;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
      apiConnectptr.p->currentReplicaNo = tcurrentReplicaNo;
    }//if
  } while (1);
}//Dbtc::toCompleteHandlingLab()

/*------------------------------------------------------------*/
/*                                                            */
/*       FIND THE API CONNECT RECORD FOR THIS TRANSACTION     */
/*       DURING TAKE OVER FROM A FAILED TC. IF NONE EXISTS    */
/*       YET THEN SEIZE A NEW API CONNECT RECORD AND LINK IT  */
/*       INTO THE HASH TABLE.                                 */
/*------------------------------------------------------------*/
void Dbtc::findApiConnectFail(Signal* signal) 
{
  ApiConnectRecordPtr fafPrevApiConnectptr;
  ApiConnectRecordPtr fafNextApiConnectptr;
  UintR tfafHashNumber;

  tfafHashNumber = ttransid1 & 511;
  fafPrevApiConnectptr.i = RNIL;
  ptrNull(fafPrevApiConnectptr);
  arrGuard(tfafHashNumber, 512);
  fafNextApiConnectptr.i = ctransidFailHash[tfafHashNumber];
  ptrCheck(fafNextApiConnectptr, capiConnectFilesize, apiConnectRecord);
FAF_LOOP:
  jam();
  if (fafNextApiConnectptr.i == RNIL) {
    jam();
    if (cfirstfreeApiConnectFail == RNIL) {
      jam();
7968
      systemErrorLab(signal, __LINE__);
7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011
      return;
    }//if
    seizeApiConnectFail(signal);
    if (fafPrevApiConnectptr.i == RNIL) {
      jam();
      ctransidFailHash[tfafHashNumber] = apiConnectptr.i;
    } else {
      jam();
      ptrGuard(fafPrevApiConnectptr);
      fafPrevApiConnectptr.p->nextApiConnect = apiConnectptr.i;
    }//if
    apiConnectptr.p->nextApiConnect = RNIL;
    initApiConnectFail(signal);
  } else {
    jam();
    fafPrevApiConnectptr.i = fafNextApiConnectptr.i;
    fafPrevApiConnectptr.p = fafNextApiConnectptr.p;
    apiConnectptr.i = fafNextApiConnectptr.i;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    fafNextApiConnectptr.i = apiConnectptr.p->nextApiConnect;
    ptrCheck(fafNextApiConnectptr, capiConnectFilesize, apiConnectRecord);
    if ((apiConnectptr.p->transid[1] != ttransid2) ||
        (apiConnectptr.p->transid[0] != ttransid1)) {
      goto FAF_LOOP;
    }//if
    updateApiStateFail(signal);
  }//if
}//Dbtc::findApiConnectFail()

/*----------------------------------------------------------*/
/*       FIND THE TC CONNECT AND IF NOT FOUND ALLOCATE A NEW  */
/*----------------------------------------------------------*/
void Dbtc::findTcConnectFail(Signal* signal) 
{
  UintR tftfHashNumber;

  tftfHashNumber = (ttransid1 ^ ttcOprec) & 1023;
  tcConnectptr.i = ctcConnectFailHash[tftfHashNumber];
  do {
    if (tcConnectptr.i == RNIL) {
      jam();
      if (cfirstfreeTcConnectFail == RNIL) {
        jam();
8012
        systemErrorLab(signal, __LINE__);
8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046
        return;
      }//if
      seizeTcConnectFail(signal);
      linkTcInConnectionlist(signal);
      tcConnectptr.p->nextTcFailHash = ctcConnectFailHash[tftfHashNumber];
      ctcConnectFailHash[tftfHashNumber] = tcConnectptr.i;
      initTcConnectFail(signal);
      return;
    } else {
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      if (tcConnectptr.p->tcOprec != ttcOprec) {
        jam();  /* FRAGMENTID = TC_OPREC HERE, LOOP ANOTHER TURN */
        tcConnectptr.i = tcConnectptr.p->nextTcFailHash;
      } else {
        updateTcStateFail(signal);
        return;
      }//if
    }//if
  } while (1);
}//Dbtc::findTcConnectFail()

/*----------------------------------------------------------*/
/*       INITIALISE AN API CONNECT FAIL RECORD              */
/*----------------------------------------------------------*/
void Dbtc::initApiConnectFail(Signal* signal) 
{
  apiConnectptr.p->transid[0] = ttransid1;
  apiConnectptr.p->transid[1] = ttransid2;
  apiConnectptr.p->firstTcConnect = RNIL;
  apiConnectptr.p->currSavePointId = 0;
  apiConnectptr.p->lastTcConnect = RNIL;
  tblockref = calcTcBlockRef(tcNodeFailptr.p->takeOverNode);

  apiConnectptr.p->tcBlockref = tblockref;
8047 8048
  apiConnectptr.p->ndbapiBlockref = 0;
  apiConnectptr.p->ndbapiConnect = 0;
8049
  apiConnectptr.p->buddyPtr = RNIL;
unknown's avatar
unknown committed
8050
  apiConnectptr.p->m_transaction_nodes.clear();
8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
  switch(ttransStatus){
  case LqhTransConf::Committed:
    jam();
    apiConnectptr.p->globalcheckpointid = tgci;
    apiConnectptr.p->apiConnectstate = CS_FAIL_COMMITTED;
    break;
  case LqhTransConf::Prepared:
    jam();
    apiConnectptr.p->apiConnectstate = CS_FAIL_PREPARED;
    break;
  case LqhTransConf::Aborted:
    jam();
    apiConnectptr.p->apiConnectstate = CS_FAIL_ABORTED;
    break;
  case LqhTransConf::Marker:
    jam();
    apiConnectptr.p->apiConnectstate = CS_FAIL_COMPLETED;
    break;
  default:
    jam();
8072
    systemErrorLab(signal, __LINE__);
8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154
  }//if
  apiConnectptr.p->commitAckMarker = RNIL;
  if(LqhTransConf::getMarkerFlag(treqinfo)){
    jam();
    CommitAckMarkerPtr tmp;
    m_commitAckMarkerHash.seize(tmp);
    
    ndbrequire(tmp.i != RNIL);
    
    apiConnectptr.p->commitAckMarker = tmp.i;
    tmp.p->transid1      = ttransid1;
    tmp.p->transid2      = ttransid2;
    tmp.p->apiNodeId     = refToNode(tapplRef);
    tmp.p->noOfLqhs      = 1;
    tmp.p->lqhNodeId[0]  = tnodeid;
    tmp.p->apiConnectPtr = apiConnectptr.i;
    m_commitAckMarkerHash.add(tmp);
  } 
}//Dbtc::initApiConnectFail()

/*------------------------------------------------------------*/
/*       INITIALISE AT TC CONNECT AT TAKE OVER WHEN ALLOCATING*/
/*       THE TC CONNECT RECORD.                               */
/*------------------------------------------------------------*/
void Dbtc::initTcConnectFail(Signal* signal) 
{
  tcConnectptr.p->apiConnect = apiConnectptr.i;
  tcConnectptr.p->tcOprec = ttcOprec;
  Uint32 treplicaNo = LqhTransConf::getReplicaNo(treqinfo);
  for (Uint32 i = 0; i < MAX_REPLICAS; i++) {
    tcConnectptr.p->failData[i] = LqhTransConf::InvalidStatus;
  }//for
  tcConnectptr.p->tcNodedata[treplicaNo] = tnodeid;
  tcConnectptr.p->failData[treplicaNo] = ttransStatus;
  tcConnectptr.p->lastReplicaNo = LqhTransConf::getLastReplicaNo(treqinfo);
  tcConnectptr.p->dirtyOp = LqhTransConf::getDirtyFlag(treqinfo);
  
}//Dbtc::initTcConnectFail()

/*----------------------------------------------------------*/
/*       INITIALISE TC NODE FAIL RECORD.                    */
/*----------------------------------------------------------*/
void Dbtc::initTcFail(Signal* signal) 
{
  tcNodeFailptr.i = 0;
  ptrAss(tcNodeFailptr, tcFailRecord);
  tcNodeFailptr.p->queueIndex = 0;
  tcNodeFailptr.p->failStatus = FS_IDLE;
}//Dbtc::initTcFail()

/*----------------------------------------------------------*/
/*               RELEASE_TAKE_OVER                          */
/*----------------------------------------------------------*/
void Dbtc::releaseTakeOver(Signal* signal) 
{
  TcConnectRecordPtr rtoNextTcConnectptr;

  rtoNextTcConnectptr.i = apiConnectptr.p->firstTcConnect;
  do {
    jam();
    tcConnectptr.i = rtoNextTcConnectptr.i;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    rtoNextTcConnectptr.i = tcConnectptr.p->nextTcConnect;
    releaseTcConnectFail(signal);
  } while (rtoNextTcConnectptr.i != RNIL);
  releaseApiConnectFail(signal);
}//Dbtc::releaseTakeOver()

/*---------------------------------------------------------------------------*/
/*                               SETUP_FAIL_DATA                             */
/* SETUP DATA TO REUSE TAKE OVER CODE FOR HANDLING ABORT/COMMIT IN NODE      */
/* FAILURE SITUATIONS.                                                       */
/*---------------------------------------------------------------------------*/
void Dbtc::setupFailData(Signal* signal) 
{
  tcConnectptr.i = apiConnectptr.p->firstTcConnect;
  do {
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    switch (tcConnectptr.p->tcConnectstate) {
    case OS_PREPARED:
    case OS_COMMITTING:
      jam();
8155
      arrGuard(tcConnectptr.p->lastReplicaNo, MAX_REPLICAS);
8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168
      for (tindex = 0; tindex <= tcConnectptr.p->lastReplicaNo; tindex++) {
	jam();
	/*-------------------------------------------------------------------
	 * KEYDATA IS USED TO KEEP AN INDICATION OF STATE IN LQH. 
	 * IN THIS CASE ALL LQH'S ARE PREPARED AND WAITING FOR 
	 * COMMIT/ABORT DECISION.                 
	 *------------------------------------------------------------------*/
	tcConnectptr.p->failData[tindex] = LqhTransConf::Prepared;
      }//for
      break;
    case OS_COMMITTED:
    case OS_COMPLETING:
      jam();
8169
      arrGuard(tcConnectptr.p->lastReplicaNo, MAX_REPLICAS);
8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181
      for (tindex = 0; tindex <= tcConnectptr.p->lastReplicaNo; tindex++) {
	jam();
	/*-------------------------------------------------------------------
	 * KEYDATA IS USED TO KEEP AN INDICATION OF STATE IN LQH. 
	 * IN THIS CASE ALL LQH'S ARE COMMITTED AND WAITING FOR 
	 * COMPLETE MESSAGE.                     
	 *------------------------------------------------------------------*/
	tcConnectptr.p->failData[tindex] = LqhTransConf::Committed;
      }//for
      break;
    case OS_COMPLETED:
      jam();
8182
      arrGuard(tcConnectptr.p->lastReplicaNo, MAX_REPLICAS);
8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193
      for (tindex = 0; tindex <= tcConnectptr.p->lastReplicaNo; tindex++) {
	jam();
	/*-------------------------------------------------------------------
	 * KEYDATA IS USED TO KEEP AN INDICATION OF STATE IN LQH. 
	 * IN THIS CASE ALL LQH'S ARE COMPLETED.
	 *-------------------------------------------------------------------*/
	tcConnectptr.p->failData[tindex] = LqhTransConf::InvalidStatus;
      }//for
      break;
    default:
      jam();
8194
      sendSystemError(signal, __LINE__);
8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280
      break;
    }//switch
    if (tabortInd != ZCOMMIT_SETUP) {
      jam();
      for (UintR Ti = 0; Ti <= tcConnectptr.p->lastReplicaNo; Ti++) {
        hostptr.i = tcConnectptr.p->tcNodedata[Ti];
        ptrCheckGuard(hostptr, chostFilesize, hostRecord);
        if (hostptr.p->hostStatus != HS_ALIVE) {
          jam();
	  /*-----------------------------------------------------------------
	   * FAILURE OF ANY INVOLVED NODE ALWAYS INVOKES AN ABORT DECISION. 
	   *-----------------------------------------------------------------*/
          tabortInd = ZTRUE;
        }//if
      }//for
    }//if
    tcConnectptr.p->tcConnectstate = OS_TAKE_OVER;
    tcConnectptr.p->tcOprec = tcConnectptr.i;
    tcConnectptr.i = tcConnectptr.p->nextTcConnect;
  } while (tcConnectptr.i != RNIL);
  apiConnectptr.p->tcBlockref = cownref;
  apiConnectptr.p->currentTcConnect = apiConnectptr.p->firstTcConnect;
  tcConnectptr.i = apiConnectptr.p->firstTcConnect;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  apiConnectptr.p->currentReplicaNo = tcConnectptr.p->lastReplicaNo;
  tcurrentReplicaNo = tcConnectptr.p->lastReplicaNo;
}//Dbtc::setupFailData()

/*----------------------------------------------------------*/
/*       UPDATE THE STATE OF THE API CONNECT FOR THIS PART.   */
/*----------------------------------------------------------*/
void Dbtc::updateApiStateFail(Signal* signal) 
{
  if(LqhTransConf::getMarkerFlag(treqinfo)){
    jam();
    const Uint32 marker = apiConnectptr.p->commitAckMarker;
    if(marker == RNIL){
      jam();

      CommitAckMarkerPtr tmp;
      m_commitAckMarkerHash.seize(tmp);
      ndbrequire(tmp.i != RNIL);
      
      apiConnectptr.p->commitAckMarker = tmp.i;
      tmp.p->transid1      = ttransid1;
      tmp.p->transid2      = ttransid2;
      tmp.p->apiNodeId     = refToNode(tapplRef);
      tmp.p->noOfLqhs      = 1;
      tmp.p->lqhNodeId[0]  = tnodeid;
      tmp.p->apiConnectPtr = apiConnectptr.i;
      m_commitAckMarkerHash.add(tmp);
    } else {
      jam();
      
      CommitAckMarkerPtr tmp;
      tmp.i = marker;
      tmp.p = m_commitAckMarkerHash.getPtr(marker);

      const Uint32 noOfLqhs = tmp.p->noOfLqhs;
      ndbrequire(noOfLqhs < MAX_REPLICAS);
      tmp.p->lqhNodeId[noOfLqhs] = tnodeid;
      tmp.p->noOfLqhs = (noOfLqhs + 1);
    }
  }

  switch (ttransStatus) {
  case LqhTransConf::Committed:
    jam();
    switch (apiConnectptr.p->apiConnectstate) {
    case CS_FAIL_COMMITTING:
    case CS_FAIL_COMMITTED:
      jam();
      ndbrequire(tgci == apiConnectptr.p->globalcheckpointid);
      break;
    case CS_FAIL_PREPARED:
      jam();
      apiConnectptr.p->apiConnectstate = CS_FAIL_COMMITTING;
      apiConnectptr.p->globalcheckpointid = tgci;
      break;
    case CS_FAIL_COMPLETED:
      jam();
      apiConnectptr.p->globalcheckpointid = tgci;
      apiConnectptr.p->apiConnectstate = CS_FAIL_COMMITTED;
      break;
    default:
      jam();
8281
      systemErrorLab(signal, __LINE__);
8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303
      break;
    }//switch
    break;
  case LqhTransConf::Prepared:
    jam();
    switch (apiConnectptr.p->apiConnectstate) {
    case CS_FAIL_COMMITTED:
      jam();
      apiConnectptr.p->apiConnectstate = CS_FAIL_COMMITTING;
      break;
    case CS_FAIL_ABORTED:
      jam();
      apiConnectptr.p->apiConnectstate = CS_FAIL_ABORTING;
      break;
    case CS_FAIL_COMMITTING:
    case CS_FAIL_PREPARED:
    case CS_FAIL_ABORTING:
      jam();
      /*empty*/;
      break;
    default:
      jam();
8304
      systemErrorLab(signal, __LINE__);
8305 8306 8307 8308 8309 8310 8311 8312 8313
      break;
    }//switch
    break;
  case LqhTransConf::Aborted:
    jam();
    switch (apiConnectptr.p->apiConnectstate) {
    case CS_FAIL_COMMITTING:
    case CS_FAIL_COMMITTED:
      jam();
8314
      systemErrorLab(signal, __LINE__);
8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326
      break;
    case CS_FAIL_PREPARED:
      jam();
      apiConnectptr.p->apiConnectstate = CS_FAIL_ABORTING;
      break;
    case CS_FAIL_ABORTING:
    case CS_FAIL_ABORTED:
      jam();
      /*empty*/;
      break;
    default:
      jam();
8327
      systemErrorLab(signal, __LINE__);
8328 8329 8330 8331 8332 8333 8334 8335
      break;
    }//switch
    break;
  case LqhTransConf::Marker:
    jam();
    break;
  default:
    jam();
8336
    systemErrorLab(signal, __LINE__);
8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453
    break;
  }//switch
}//Dbtc::updateApiStateFail()

/*------------------------------------------------------------*/
/*               UPDATE_TC_STATE_FAIL                         */
/*                                                            */
/*       WE NEED TO UPDATE THE STATUS OF TC_CONNECT RECORD AND*/
/*       WE ALSO NEED TO CHECK THAT THERE IS CONSISTENCY      */
/*       BETWEEN THE DIFFERENT REPLICAS.                      */
/*------------------------------------------------------------*/
void Dbtc::updateTcStateFail(Signal* signal) 
{
  const Uint8 treplicaNo     = LqhTransConf::getReplicaNo(treqinfo);
  const Uint8 tlastReplicaNo = LqhTransConf::getLastReplicaNo(treqinfo);
  const Uint8 tdirtyOp       = LqhTransConf::getDirtyFlag(treqinfo);

  TcConnectRecord * regTcPtr = tcConnectptr.p;
  
  ndbrequire(regTcPtr->apiConnect == apiConnectptr.i);
  ndbrequire(regTcPtr->failData[treplicaNo] == LqhTransConf::InvalidStatus);
  ndbrequire(regTcPtr->lastReplicaNo == tlastReplicaNo);
  ndbrequire(regTcPtr->dirtyOp == tdirtyOp);
  
  regTcPtr->tcNodedata[treplicaNo] = tnodeid;
  regTcPtr->failData[treplicaNo] = ttransStatus;  
}//Dbtc::updateTcStateFail()

void Dbtc::execTCGETOPSIZEREQ(Signal* signal) 
{
  jamEntry();
  CRASH_INSERTION(8000);

  UintR Tuserpointer = signal->theData[0];         /* DBDIH POINTER         */
  BlockReference Tusersblkref = signal->theData[1];/* DBDIH BLOCK REFERENCE */
  signal->theData[0] = Tuserpointer;
  signal->theData[1] = coperationsize;
  sendSignal(Tusersblkref, GSN_TCGETOPSIZECONF, signal, 2, JBB);
}//Dbtc::execTCGETOPSIZEREQ()

void Dbtc::execTC_CLOPSIZEREQ(Signal* signal) 
{
  jamEntry();
  CRASH_INSERTION(8001);

  tuserpointer = signal->theData[0];
  tusersblkref = signal->theData[1];
                                            /* DBDIH BLOCK REFERENCE         */
  coperationsize = 0;
  signal->theData[0] = tuserpointer;
  sendSignal(tusersblkref, GSN_TC_CLOPSIZECONF, signal, 1, JBB);
}//Dbtc::execTC_CLOPSIZEREQ()

/* ######################################################################### */
/* #######                        ERROR MODULE                       ####### */
/* ######################################################################### */
void Dbtc::tabStateErrorLab(Signal* signal) 
{
  terrorCode = ZSTATE_ERROR;
  releaseAtErrorLab(signal);
}//Dbtc::tabStateErrorLab()

void Dbtc::wrongSchemaVersionErrorLab(Signal* signal) 
{
  const TcKeyReq * const tcKeyReq = (TcKeyReq *)&signal->theData[0];

  TableRecordPtr tabPtr;
  tabPtr.i = tcKeyReq->tableId;
  const Uint32 schemVer = tcKeyReq->tableSchemaVersion;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);

  terrorCode = tabPtr.p->getErrorCode(schemVer);
  
  abortErrorLab(signal);
}//Dbtc::wrongSchemaVersionErrorLab()

void Dbtc::noFreeConnectionErrorLab(Signal* signal) 
{
  terrorCode = ZNO_FREE_TC_CONNECTION;
  abortErrorLab(signal);        /* RECORD. OTHERWISE GOTO ERRORHANDLING  */
}//Dbtc::noFreeConnectionErrorLab()

void Dbtc::aiErrorLab(Signal* signal) 
{
  terrorCode = ZLENGTH_ERROR;
  abortErrorLab(signal);
}//Dbtc::aiErrorLab()

void Dbtc::seizeAttrbuferrorLab(Signal* signal) 
{
  terrorCode = ZGET_ATTRBUF_ERROR;
  abortErrorLab(signal);
}//Dbtc::seizeAttrbuferrorLab()

void Dbtc::seizeDatabuferrorLab(Signal* signal) 
{
  terrorCode = ZGET_DATAREC_ERROR;
  releaseAtErrorLab(signal);
}//Dbtc::seizeDatabuferrorLab()

void Dbtc::releaseAtErrorLab(Signal* signal) 
{
  ptrGuard(tcConnectptr);
  tcConnectptr.p->tcConnectstate = OS_ABORTING;
  /*-------------------------------------------------------------------------*
   * A FAILURE OF THIS OPERATION HAS OCCURRED. THIS FAILURE WAS EITHER A 
   * FAULTY PARAMETER OR A RESOURCE THAT WAS NOT AVAILABLE. 
   * WE WILL ABORT THE ENTIRE TRANSACTION SINCE THIS IS THE SAFEST PATH 
   * TO HANDLE THIS PROBLEM.      
   * SINCE WE HAVE NOT YET CONTACTED ANY LQH WE SET NUMBER OF NODES TO ZERO
   * WE ALSO SET THE STATE TO ABORTING TO INDICATE THAT WE ARE NOT EXPECTING 
   * ANY SIGNALS.                              
   *-------------------------------------------------------------------------*/
  tcConnectptr.p->noOfNodes = 0;
  abortErrorLab(signal);
}//Dbtc::releaseAtErrorLab()

8454
void Dbtc::warningHandlerLab(Signal* signal, int line) 
8455 8456 8457 8458
{
  ndbassert(false);
}//Dbtc::warningHandlerLab()

8459
void Dbtc::systemErrorLab(Signal* signal, int line) 
8460
{
8461
  progError(line, NDBD_EXIT_NDBREQUIRE);
8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561
}//Dbtc::systemErrorLab()


/* ######################################################################### *
 * #######                        SCAN MODULE                        ####### *
 * ######################################################################### *

  The application orders a scan of a table.  We divide the scan into a scan on
  each fragment.  The scan uses the primary replicas since the scan might be   
  used for an update in a separate transaction. 

  Scans are always done as a separate transaction.  Locks from the scan
  can be overtaken by another transaction.  Scans can never lock the entire
  table.  Locks are released immediately after the read has been verified 
  by the application. There is not even an option to leave the locks. 
  The reason is that this would hurt real-time behaviour too much.

  -#  The first step in handling a scan of a table is to receive all signals
      defining the scan. If failures occur during this step we release all 
      resource and reply with SCAN_TABREF providing the error code. 
      If system load is too high, the request will not be allowed.   
   
  -#  The second step retrieves the number of fragments that exist in the 
      table. It also ensures that the table actually exist.  After this,
      the scan is ready to be parallelised.  The idea is that the receiving 
      process (hereafter called delivery process) will start up a number 
      of scan processes.  Each of these scan processes will 
      independently scan one fragment at a time.  The delivery
      process object is the scan record and the scan process object is 
      the scan fragment record plus the scan operation record.
   
  -#  The third step is thus performed in parallel. In the third step each 
      scan process retrieves the primary replica of the fragment it will 
      scan.  Then it starts the scan as soon as the load on that node permits. 
   
  The LQH returns either when it retrieved the maximum number of tuples or 
  when it has retrived at least one tuple and is hindered by a lock to
  retrieve the next tuple.  This is to ensure that a scan process never 
  can be involved in a deadlock situation.   
   
  When the scan process receives a number of tuples to report to the 
  application it checks the state of the delivery process. Only one delivery 
  at a time is handled by the application. Thus if the delivery process 
  has already sent a number of tuples to the application this set of tuples 
  are queued.
   
  When the application requests the next set of tuples it is immediately
  delivered if any are queued, otherwise it waits for the next scan
  process that is ready to deliver.  


  ERROR HANDLING
   
  As already mentioned it is rather easy to handle errors before the scan 
  processes have started.  In this case it is enough to release the resources 
  and send SCAN_TAB_REF.
   
  If an error occurs in any of the scan processes then we have to stop all 
  scan processes. We do however only stop the delivery process and ask 
  the api to order us to close the scan.  The reason is that we can easily 
  enter into difficult timing problems since the application and this 
  block is out of synch we will thus always start by report the error to
  the application and wait for a close request. This error report uses the 
  SCAN_TABREF signal with a special error code that the api must check for.   
   
   
  CLOSING AN ACTIVE SCAN
   
  The application can close a scan for several reasons before it is completed.
  One reason was mentioned above where an error in a scan process led to a  
  request to close the scan. Another reason could simply be that the 
  application found what it looked for and is thus not interested in the 
  rest of the scan.  

  IT COULD ALSO BE DEPENDENT ON INTERNAL ERRORS IN THE API. 
   
  When a close scan request is received, all scan processes are stopped and all
  resources belonging to those scan processes are released. Stopping the scan  
  processes most often includes communication with an LQH where the local scan 
  is controlled. Finally all resources belonging to the scan is released and 
  the SCAN_TABCONF is sent with an indication of that the scan is closed.   
   
   
  CLOSING A COMPLETED SCAN  
   
  When all scan processes are completed then a report is sent to the 
  application which indicates that no more tuples can be fetched. 
  The application will send a close scan and the same action as when 
  closing an active scan is performed. 
  In this case it will of course not find any active scan processes. 
  It will even find all scan processes already released.
   
  The reason for requiring the api to close the scan is the same as above. 
  It is to avoid any timing problems due to that the api and this block 
  is out of synch.
   
  * ######################################################################## */
void Dbtc::execSCAN_TABREQ(Signal* signal) 
{
  const ScanTabReq * const scanTabReq = (ScanTabReq *)&signal->theData[0];
8562
  const Uint32 ri = scanTabReq->requestInfo;
8563 8564
  const Uint32 aiLength = (scanTabReq->attrLenKeyLen & 0xFFFF);
  const Uint32 keyLen = scanTabReq->attrLenKeyLen >> 16;
8565
  const Uint32 schemaVersion = scanTabReq->tableSchemaVersion;
unknown's avatar
unknown committed
8566 8567
  const Uint32 transid1 = scanTabReq->transId1;
  const Uint32 transid2 = scanTabReq->transId2;
8568 8569 8570 8571
  const Uint32 tmpXX = scanTabReq->buddyConPtr;
  const Uint32 buddyPtr = (tmpXX == 0xFFFFFFFF ? RNIL : tmpXX);
  Uint32 currSavePointId = 0;
  
8572 8573
  Uint32 scanConcurrency = scanTabReq->getParallelism(ri);
  Uint32 noOprecPerFrag = ScanTabReq::getScanBatch(ri);
unknown's avatar
unknown committed
8574
  Uint32 scanParallel = scanConcurrency;
8575
  Uint32 errCode;
unknown's avatar
unknown committed
8576
  ScanRecordPtr scanptr;
8577

unknown's avatar
unknown committed
8578 8579 8580 8581 8582 8583
  jamEntry();

  SegmentedSectionPtr api_op_ptr;
  signal->getSection(api_op_ptr, 0);
  copy(&cdata[0], api_op_ptr);
  releaseSections(signal);
unknown's avatar
unknown committed
8584

8585 8586 8587
  apiConnectptr.i = scanTabReq->apiConnectPtr;
  tabptr.i = scanTabReq->tableId;

8588 8589
  if (apiConnectptr.i >= capiConnectFilesize)
  {
8590
    jam();
8591
    warningHandlerLab(signal, __LINE__);
8592 8593
    return;
  }//if
8594

8595
  ptrAss(apiConnectptr, apiConnectRecord);
unknown's avatar
unknown committed
8596
  ApiConnectRecord * transP = apiConnectptr.p;
8597

unknown's avatar
unknown committed
8598
  if (transP->apiConnectstate != CS_CONNECTED) {
8599 8600
    jam();
    // could be left over from TCKEYREQ rollback
unknown's avatar
unknown committed
8601 8602
    if (transP->apiConnectstate == CS_ABORTING &&
	transP->abortState == AS_IDLE) {
8603
      jam();
unknown's avatar
unknown committed
8604 8605 8606 8607
    } else if(transP->apiConnectstate == CS_STARTED && 
	      transP->firstTcConnect == RNIL){
      jam();
      // left over from simple/dirty read
8608 8609 8610
    } else {
      jam();
      errCode = ZSTATE_ERROR;
8611
      goto SCAN_TAB_error_no_state_change;
8612 8613
    }
  }
8614 8615 8616 8617 8618 8619 8620

  if(tabptr.i >= ctabrecFilesize)
  {
    errCode = ZUNKNOWN_TABLE_ERROR;
    goto SCAN_TAB_error;
  }

8621
  ptrAss(tabptr, tableRecord);
unknown's avatar
unknown committed
8622 8623 8624 8625 8626 8627 8628
  if ((aiLength == 0) ||
      (!tabptr.p->checkTable(schemaVersion)) ||
      (scanConcurrency == 0) ||
      (cfirstfreeTcConnect == RNIL) ||
      (cfirstfreeScanrec == RNIL)) {
    goto SCAN_error_check;
  }
8629 8630 8631 8632 8633 8634 8635 8636
  if (buddyPtr != RNIL) {
    jam();
    ApiConnectRecordPtr buddyApiPtr;
    buddyApiPtr.i = buddyPtr;
    ptrCheckGuard(buddyApiPtr, capiConnectFilesize, apiConnectRecord);
    if ((transid1 == buddyApiPtr.p->transid[0]) &&
	(transid2 == buddyApiPtr.p->transid[1])) {
      jam();
8637 8638
      
      if (buddyApiPtr.p->apiConnectstate == CS_ABORTING) {
unknown's avatar
unknown committed
8639
	// transaction has been aborted
8640
	jam();
unknown's avatar
unknown committed
8641
	errCode = buddyApiPtr.p->returncode;
8642 8643
	goto SCAN_TAB_error;
      }//if
8644 8645 8646 8647 8648 8649
      currSavePointId = buddyApiPtr.p->currSavePointId;
      buddyApiPtr.p->currSavePointId++;
    }
  }
  
  seizeTcConnect(signal);
unknown's avatar
unknown committed
8650
  tcConnectptr.p->apiConnect = apiConnectptr.i;
8651 8652
  tcConnectptr.p->tcConnectstate = OS_WAIT_SCAN;
  apiConnectptr.p->lastTcConnect = tcConnectptr.i;
unknown's avatar
unknown committed
8653 8654

  seizeCacheRecord(signal);
8655 8656
  cachePtr.p->keylen = keyLen;
  cachePtr.p->save1 = 0;
8657 8658
  cachePtr.p->distributionKey = scanTabReq->distributionKey;
  cachePtr.p->distributionKeyIndicator= ScanTabReq::getDistributionKeyFlag(ri);
unknown's avatar
unknown committed
8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670
  scanptr = seizeScanrec(signal);

  ndbrequire(transP->apiScanRec == RNIL);
  ndbrequire(scanptr.p->scanApiRec == RNIL);

  initScanrec(scanptr, scanTabReq, scanParallel, noOprecPerFrag);

  transP->apiScanRec = scanptr.i;
  transP->returncode = 0;
  transP->transid[0] = transid1;
  transP->transid[1] = transid2;
  transP->buddyPtr   = buddyPtr;
8671 8672

  // The scan is started
unknown's avatar
unknown committed
8673 8674
  transP->apiConnectstate = CS_START_SCAN;
  transP->currSavePointId = currSavePointId;
8675 8676 8677 8678 8679 8680 8681

  /**********************************************************
  * We start the timer on scanRec to be able to discover a 
  * timeout in the API the API now is in charge!
  ***********************************************************/
  setApiConTimer(apiConnectptr.i, ctcTimer, __LINE__);
  updateBuddyTimer(apiConnectptr);
unknown's avatar
unknown committed
8682

8683 8684 8685 8686 8687 8688 8689 8690
  /***********************************************************
   * WE HAVE NOW RECEIVED ALL REFERENCES TO SCAN OBJECTS IN 
   * THE API. WE ARE NOW READY TO RECEIVE THE ATTRIBUTE INFO 
   * IF ANY TO RECEIVE.
   **********************************************************/
  scanptr.p->scanState = ScanRecord::WAIT_AI;
  return;

unknown's avatar
unknown committed
8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712
 SCAN_error_check:
  if (aiLength == 0) {
    jam()
    errCode = ZSCAN_AI_LEN_ERROR;
    goto SCAN_TAB_error;
  }//if
  if (!tabptr.p->checkTable(schemaVersion)){
    jam();
    errCode = tabptr.p->getErrorCode(schemaVersion);
    goto SCAN_TAB_error;
  }//if  
  if (scanConcurrency == 0) {
    jam();
    errCode = ZNO_CONCURRENCY_ERROR;
    goto SCAN_TAB_error;
  }//if
  if (cfirstfreeTcConnect == RNIL) {
    jam();
    errCode = ZNO_FREE_TC_CONNECTION;
    goto SCAN_TAB_error;
  }//if
  ndbrequire(cfirstfreeScanrec == RNIL);
8713
  jam();
unknown's avatar
unknown committed
8714 8715 8716
  errCode = ZNO_SCANREC_ERROR;
  goto SCAN_TAB_error;
 
8717
SCAN_TAB_error:
8718
  jam();
8719 8720 8721 8722 8723 8724 8725 8726 8727 8728
  /**
   * Prepare for up coming ATTRINFO/KEYINFO
   */
  transP->apiConnectstate = CS_ABORTING;
  transP->abortState = AS_IDLE;
  transP->transid[0] = transid1;
  transP->transid[1] = transid2;
 
SCAN_TAB_error_no_state_change:
  
8729
  ScanTabRef * ref = (ScanTabRef*)&signal->theData[0];
unknown's avatar
unknown committed
8730
  ref->apiConnectPtr = transP->ndbapiConnect;
8731 8732 8733
  ref->transId1 = transid1;
  ref->transId2 = transid2;
  ref->errorCode  = errCode;
unknown's avatar
unknown committed
8734 8735
  ref->closeNeeded = 0;
  sendSignal(transP->ndbapiBlockref, GSN_SCAN_TABREF, 
8736 8737 8738 8739
	     signal, ScanTabRef::SignalLength, JBB);
  return;
}//Dbtc::execSCAN_TABREQ()

unknown's avatar
unknown committed
8740 8741
void Dbtc::initScanrec(ScanRecordPtr scanptr,
		       const ScanTabReq * scanTabReq,
8742 8743 8744
		       UintR scanParallel,
		       UintR noOprecPerFrag) 
{
8745
  const UintR ri = scanTabReq->requestInfo;
8746 8747
  scanptr.p->scanTcrec = tcConnectptr.i;
  scanptr.p->scanApiRec = apiConnectptr.i;
8748 8749
  scanptr.p->scanAiLength = scanTabReq->attrLenKeyLen & 0xFFFF;
  scanptr.p->scanKeyLen = scanTabReq->attrLenKeyLen >> 16;
8750 8751 8752
  scanptr.p->scanTableref = tabptr.i;
  scanptr.p->scanSchemaVersion = scanTabReq->tableSchemaVersion;
  scanptr.p->scanParallel = scanParallel;
unknown's avatar
unknown committed
8753 8754 8755
  scanptr.p->first_batch_size_rows = scanTabReq->first_batch_size;
  scanptr.p->batch_byte_size = scanTabReq->batch_byte_size;
  scanptr.p->batch_size_rows = noOprecPerFrag;
unknown's avatar
unknown committed
8756 8757 8758 8759 8760 8761 8762

  Uint32 tmp = 0;
  ScanFragReq::setLockMode(tmp, ScanTabReq::getLockMode(ri));
  ScanFragReq::setHoldLockFlag(tmp, ScanTabReq::getHoldLockFlag(ri));
  ScanFragReq::setKeyinfoFlag(tmp, ScanTabReq::getKeyinfoFlag(ri));
  ScanFragReq::setReadCommittedFlag(tmp,ScanTabReq::getReadCommittedFlag(ri));
  ScanFragReq::setRangeScanFlag(tmp, ScanTabReq::getRangeScanFlag(ri));
8763
  ScanFragReq::setDescendingFlag(tmp, ScanTabReq::getDescendingFlag(ri));
8764
  ScanFragReq::setTupScanFlag(tmp, ScanTabReq::getTupScanFlag(ri));
unknown's avatar
unknown committed
8765 8766 8767
  ScanFragReq::setAttrLen(tmp, scanTabReq->attrLenKeyLen & 0xFFFF);
  
  scanptr.p->scanRequestInfo = tmp;
8768
  scanptr.p->scanStoredProcId = scanTabReq->storedProcId;
unknown's avatar
unknown committed
8769 8770
  scanptr.p->scanState = ScanRecord::RUNNING;
  scanptr.p->m_queued_count = 0;
unknown's avatar
unknown committed
8771

unknown's avatar
unknown committed
8772 8773 8774 8775 8776 8777 8778 8779 8780
  ScanFragList list(c_scan_frag_pool, 
		    scanptr.p->m_running_scan_frags);
  for (Uint32 i = 0; i < scanParallel; i++) {
    jam();
    ScanFragRecPtr ptr;
    ndbrequire(list.seize(ptr));
    ptr.p->scanRec = scanptr.i;
    ptr.p->scanFragId = 0;
    ptr.p->m_apiPtr = cdata[i];
8781
  }//for
8782

unknown's avatar
unknown committed
8783
  (* (ScanTabReq::getRangeScanFlag(ri) ? 
8784 8785
      &c_counters.c_range_scan_count : 
      &c_counters.c_scan_count))++;
8786 8787 8788 8789 8790 8791 8792 8793 8794
}//Dbtc::initScanrec()

void Dbtc::scanTabRefLab(Signal* signal, Uint32 errCode) 
{
  ScanTabRef * ref = (ScanTabRef*)&signal->theData[0];
  ref->apiConnectPtr = apiConnectptr.p->ndbapiConnect;
  ref->transId1 = apiConnectptr.p->transid[0];
  ref->transId2 = apiConnectptr.p->transid[1];
  ref->errorCode  = errCode;
unknown's avatar
unknown committed
8795
  ref->closeNeeded = 0;
8796 8797 8798 8799 8800 8801 8802 8803 8804 8805
  sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_SCAN_TABREF, 
	     signal, ScanTabRef::SignalLength, JBB);
}//Dbtc::scanTabRefLab()

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*       RECEPTION OF ATTRINFO FOR SCAN TABLE REQUEST.                       */
/*---------------------------------------------------------------------------*/
void Dbtc::scanAttrinfoLab(Signal* signal, UintR Tlen) 
{
unknown's avatar
unknown committed
8806
  ScanRecordPtr scanptr;
8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835
  scanptr.i = apiConnectptr.p->apiScanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
  tcConnectptr.i = scanptr.p->scanTcrec;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  cachePtr.i = apiConnectptr.p->cachePtr;
  ptrCheckGuard(cachePtr, ccacheFilesize, cacheRecord);
  CacheRecord * const regCachePtr = cachePtr.p;
  ndbrequire(scanptr.p->scanState == ScanRecord::WAIT_AI);

  regCachePtr->currReclenAi = regCachePtr->currReclenAi + Tlen;
  if (regCachePtr->currReclenAi < scanptr.p->scanAiLength) {
    if (cfirstfreeAttrbuf == RNIL) {
      goto scanAttrinfo_attrbuf_error;
    }//if
    saveAttrbuf(signal);
  } else {
    if (regCachePtr->currReclenAi > scanptr.p->scanAiLength) {
      goto scanAttrinfo_len_error;
    } else {
      /* CURR_RECLEN_AI = SCAN_AI_LENGTH */
      if (cfirstfreeAttrbuf == RNIL) {
        goto scanAttrinfo_attrbuf2_error;
      }//if
      saveAttrbuf(signal);
      /**************************************************
       * WE HAVE NOW RECEIVED ALL INFORMATION CONCERNING 
       * THIS SCAN. WE ARE READY TO START THE ACTUAL 
       * EXECUTION OF THE SCAN QUERY
       **************************************************/
unknown's avatar
unknown committed
8836
      diFcountReqLab(signal, scanptr);
8837 8838 8839 8840 8841 8842 8843
      return;
    }//if
  }//if
  return;

scanAttrinfo_attrbuf_error:
  jam();
unknown's avatar
unknown committed
8844
  abortScanLab(signal, scanptr, ZGET_ATTRBUF_ERROR, true);
8845 8846 8847 8848
  return;

scanAttrinfo_attrbuf2_error:
  jam();
unknown's avatar
unknown committed
8849
  abortScanLab(signal, scanptr, ZGET_ATTRBUF_ERROR, true);
8850 8851 8852 8853
  return;

scanAttrinfo_len_error:
  jam();
unknown's avatar
unknown committed
8854
  abortScanLab(signal, scanptr, ZLENGTH_ERROR, true);
8855 8856 8857
  return;
}//Dbtc::scanAttrinfoLab()

unknown's avatar
unknown committed
8858
void Dbtc::diFcountReqLab(Signal* signal, ScanRecordPtr scanptr)
8859 8860 8861 8862 8863 8864 8865 8866 8867 8868
{
  /**
   * Check so that the table is not being dropped
   */
  TableRecordPtr tabPtr;
  tabPtr.i = scanptr.p->scanTableref;
  tabPtr.p = &tableRecord[tabPtr.i];
  if (tabPtr.p->checkTable(scanptr.p->scanSchemaVersion)){
    ;
  } else {
unknown's avatar
unknown committed
8869
    abortScanLab(signal, scanptr, 
unknown's avatar
unknown committed
8870 8871
		 tabPtr.p->getErrorCode(scanptr.p->scanSchemaVersion),
		 true);
8872 8873 8874
    return;
  }

8875
  scanptr.p->scanNextFragId = 0;
8876
  scanptr.p->m_booked_fragments_count= 0;
8877
  scanptr.p->scanState = ScanRecord::WAIT_FRAGMENT_COUNT;
8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911
  
  if(!cachePtr.p->distributionKeyIndicator)
  {
    jam();
    /*************************************************
     * THE FIRST STEP TO RECEIVE IS SUCCESSFULLY COMPLETED. 
     * WE MUST FIRST GET THE NUMBER OF  FRAGMENTS IN THE TABLE.
     ***************************************************/
    signal->theData[0] = tcConnectptr.p->dihConnectptr;
    signal->theData[1] = scanptr.p->scanTableref;
    sendSignal(cdihblockref, GSN_DI_FCOUNTREQ, signal, 2, JBB);
  }
  else 
  {
    signal->theData[0] = tcConnectptr.p->dihConnectptr;
    signal->theData[1] = tabPtr.i;
    signal->theData[2] = cachePtr.p->distributionKey;
    EXECUTE_DIRECT(DBDIH, GSN_DIGETNODESREQ, signal, 3);
    UintR TerrorIndicator = signal->theData[0];
    jamEntry();
    if (TerrorIndicator != 0) {
      signal->theData[0] = tcConnectptr.i;
      //signal->theData[1] Contains error
      execDI_FCOUNTREF(signal);
      return;
    }
    
    UintR Tdata1 = signal->theData[1];
    scanptr.p->scanNextFragId = Tdata1;

    signal->theData[0] = tcConnectptr.i;
    signal->theData[1] = 1; // Frag count
    execDI_FCOUNTCONF(signal);
  }
8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927
  return;
}//Dbtc::diFcountReqLab()

/********************************************************************
 * execDI_FCOUNTCONF
 *
 * WE HAVE ASKED DIH ABOUT THE NUMBER OF FRAGMENTS IN THIS TABLE. 
 * WE WILL NOW START A NUMBER OF PARALLEL SCAN PROCESSES. EACH OF 
 * THESE WILL SCAN ONE FRAGMENT AT A TIME. THEY WILL CONTINUE THIS 
 * UNTIL THERE ARE NO MORE FRAGMENTS TO SCAN OR UNTIL THE APPLICATION 
 * CLOSES THE SCAN.
 ********************************************************************/
void Dbtc::execDI_FCOUNTCONF(Signal* signal) 
{
  jamEntry();
  tcConnectptr.i = signal->theData[0];
8928
  Uint32 tfragCount = signal->theData[1];
8929 8930 8931
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
unknown's avatar
unknown committed
8932
  ScanRecordPtr scanptr;
8933 8934 8935 8936 8937
  scanptr.i = apiConnectptr.p->apiScanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
  ndbrequire(scanptr.p->scanState == ScanRecord::WAIT_FRAGMENT_COUNT);
  if (apiConnectptr.p->apiFailState == ZTRUE) {
    jam();
unknown's avatar
unknown committed
8938
    releaseScanResources(scanptr, true);
8939 8940 8941 8942 8943
    handleApiFailState(signal, apiConnectptr.i);
    return;
  }//if
  if (tfragCount == 0) {
    jam();
unknown's avatar
unknown committed
8944
    abortScanLab(signal, scanptr, ZNO_FRAGMENT_ERROR, true);
8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956
    return;
  }//if
  
  /**
   * Check so that the table is not being dropped
   */
  TableRecordPtr tabPtr;
  tabPtr.i = scanptr.p->scanTableref;
  tabPtr.p = &tableRecord[tabPtr.i];
  if (tabPtr.p->checkTable(scanptr.p->scanSchemaVersion)){
    ;
  } else {
unknown's avatar
unknown committed
8957
    abortScanLab(signal, scanptr,
unknown's avatar
unknown committed
8958 8959
		 tabPtr.p->getErrorCode(scanptr.p->scanSchemaVersion),
		 true);
8960 8961 8962
    return;
  }

unknown's avatar
unknown committed
8963
  scanptr.p->scanParallel = tfragCount;
8964
  scanptr.p->scanNoFrag = tfragCount;
unknown's avatar
unknown committed
8965 8966
  scanptr.p->scanState = ScanRecord::RUNNING;

8967
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
unknown's avatar
unknown committed
8968 8969 8970
  updateBuddyTimer(apiConnectptr);
  
  ScanFragRecPtr ptr;
8971 8972 8973
  ScanFragList list(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
  for (list.first(ptr); !ptr.isNull() && tfragCount; 
       list.next(ptr), tfragCount--){
unknown's avatar
unknown committed
8974 8975
    jam();

unknown's avatar
unknown committed
8976
    ptr.p->lqhBlockref = 0;
unknown's avatar
unknown committed
8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987
    ptr.p->startFragTimer(ctcTimer);
    ptr.p->scanFragId = scanptr.p->scanNextFragId++;
    ptr.p->scanFragState = ScanFragRec::WAIT_GET_PRIMCONF;
    ptr.p->startFragTimer(ctcTimer);

    signal->theData[0] = tcConnectptr.p->dihConnectptr;
    signal->theData[1] = ptr.i;
    signal->theData[2] = scanptr.p->scanTableref;
    signal->theData[3] = ptr.p->scanFragId;
    sendSignal(cdihblockref, GSN_DIGETPRIMREQ, signal, 4, JBB);
  }//for
8988 8989 8990 8991 8992 8993

  ScanFragList queued(c_scan_frag_pool, scanptr.p->m_queued_scan_frags);
  for (; !ptr.isNull();)
  {
    ptr.p->m_ops = 0;
    ptr.p->m_totalLen = 0;
8994
    ptr.p->m_scan_frag_conf_status = 1;
8995 8996 8997 8998 8999 9000 9001
    ptr.p->scanFragState = ScanFragRec::QUEUED_FOR_DELIVERY;
    ptr.p->stopFragTimer();

    ScanFragRecPtr tmp = ptr;
    list.next(ptr);
    list.remove(tmp);
    queued.add(tmp);
9002
    scanptr.p->m_queued_count++;
9003
  }
9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016
}//Dbtc::execDI_FCOUNTCONF()

/******************************************************
 * execDI_FCOUNTREF
 ******************************************************/
void Dbtc::execDI_FCOUNTREF(Signal* signal) 
{
  jamEntry();
  tcConnectptr.i = signal->theData[0];
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  const Uint32 errCode = signal->theData[1];
  apiConnectptr.i = tcConnectptr.p->apiConnect;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
unknown's avatar
unknown committed
9017
  ScanRecordPtr scanptr;
9018 9019 9020 9021 9022
  scanptr.i = apiConnectptr.p->apiScanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
  ndbrequire(scanptr.p->scanState == ScanRecord::WAIT_FRAGMENT_COUNT);
  if (apiConnectptr.p->apiFailState == ZTRUE) {
    jam();
unknown's avatar
unknown committed
9023
    releaseScanResources(scanptr, true);
9024 9025 9026
    handleApiFailState(signal, apiConnectptr.i);
    return;
  }//if
unknown's avatar
unknown committed
9027
  abortScanLab(signal, scanptr, errCode, true);
9028 9029
}//Dbtc::execDI_FCOUNTREF()

unknown's avatar
unknown committed
9030 9031
void Dbtc::abortScanLab(Signal* signal, ScanRecordPtr scanptr, Uint32 errCode,
			bool not_started) 
9032 9033
{
  scanTabRefLab(signal, errCode);
unknown's avatar
unknown committed
9034
  releaseScanResources(scanptr, not_started);
9035 9036
}//Dbtc::abortScanLab()

unknown's avatar
unknown committed
9037 9038
void Dbtc::releaseScanResources(ScanRecordPtr scanPtr,
				bool not_started)
9039 9040 9041 9042
{
  if (apiConnectptr.p->cachePtr != RNIL) {
    cachePtr.i = apiConnectptr.p->cachePtr;
    ptrCheckGuard(cachePtr, ccacheFilesize, cacheRecord);
9043
    releaseKeys();
unknown's avatar
unknown committed
9044
    releaseAttrinfo();
9045
  }//if
unknown's avatar
unknown committed
9046
  tcConnectptr.i = scanPtr.p->scanTcrec;
9047
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
unknown's avatar
unknown committed
9048 9049
  releaseTcCon();

unknown's avatar
unknown committed
9050 9051 9052 9053 9054 9055 9056 9057 9058
  if (not_started)
  {
    jam();
    ScanFragList run(c_scan_frag_pool, scanPtr.p->m_running_scan_frags);
    ScanFragList queue(c_scan_frag_pool, scanPtr.p->m_queued_scan_frags);
    run.release();
    queue.release();
  }
  
unknown's avatar
unknown committed
9059 9060 9061
  ndbrequire(scanPtr.p->m_running_scan_frags.isEmpty());
  ndbrequire(scanPtr.p->m_queued_scan_frags.isEmpty());
  ndbrequire(scanPtr.p->m_delivered_scan_frags.isEmpty());
unknown's avatar
unknown committed
9062 9063 9064 9065

  ndbassert(scanPtr.p->scanApiRec == apiConnectptr.i);
  ndbassert(apiConnectptr.p->apiScanRec == scanPtr.i);
  
unknown's avatar
unknown committed
9066 9067 9068 9069
  // link into free list
  scanPtr.p->nextScan = cfirstfreeScanrec;
  scanPtr.p->scanState = ScanRecord::IDLE;
  scanPtr.p->scanTcrec = RNIL;
unknown's avatar
unknown committed
9070
  scanPtr.p->scanApiRec = RNIL;
unknown's avatar
unknown committed
9071 9072
  cfirstfreeScanrec = scanPtr.i;
  
9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090
  apiConnectptr.p->apiScanRec = RNIL;
  apiConnectptr.p->apiConnectstate = CS_CONNECTED;
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
}//Dbtc::releaseScanResources()


/****************************************************************
 * execDIGETPRIMCONF
 *
 * WE HAVE RECEIVED THE PRIMARY NODE OF THIS FRAGMENT. 
 * WE ARE NOW READY TO ASK  FOR PERMISSION TO LOAD THIS 
 * SPECIFIC NODE WITH A SCAN OPERATION.
 ****************************************************************/
void Dbtc::execDIGETPRIMCONF(Signal* signal) 
{
  jamEntry();
  //  tcConnectptr.i in theData[0] is not used
  scanFragptr.i = signal->theData[1];
unknown's avatar
unknown committed
9091
  c_scan_frag_pool.getPtr(scanFragptr);
9092 9093 9094 9095 9096 9097

  tnodeid = signal->theData[2];
  arrGuard(tnodeid, MAX_NDB_NODES);

  ndbrequire(scanFragptr.p->scanFragState == ScanFragRec::WAIT_GET_PRIMCONF);
  scanFragptr.p->stopFragTimer();
unknown's avatar
unknown committed
9098 9099

  ScanRecordPtr scanptr;
9100 9101 9102
  scanptr.i = scanFragptr.p->scanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);

9103 9104 9105 9106 9107 9108
  /**
   * This must be false as select count(*) otherwise
   *   can "pass" committing on backup fragments and
   *   get incorrect row count
   */
  if(false && ScanFragReq::getReadCommittedFlag(scanptr.p->scanRequestInfo))
unknown's avatar
unknown committed
9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121
  {
    jam();
    Uint32 max = 3+signal->theData[6];
    Uint32 nodeid = getOwnNodeId();
    for(Uint32 i = 3; i<max; i++)
      if(signal->theData[i] ==  nodeid)
      {
	jam();
	tnodeid = nodeid;
	break;
      }
  }
  
9122 9123 9124 9125 9126 9127 9128 9129 9130 9131
  {
    /**
     * Check table
     */
    TableRecordPtr tabPtr;
    tabPtr.i = scanptr.p->scanTableref;
    ptrAss(tabPtr, tableRecord);
    Uint32 schemaVersion = scanptr.p->scanSchemaVersion;
    if(tabPtr.p->checkTable(schemaVersion) == false){
      jam();
unknown's avatar
unknown committed
9132 9133
      ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
      
unknown's avatar
unknown committed
9134
      run.release(scanFragptr);
unknown's avatar
unknown committed
9135
      scanError(signal, scanptr, tabPtr.p->getErrorCode(schemaVersion));
9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149
      return;
    }
  }
  
  tcConnectptr.i = scanptr.p->scanTcrec;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  apiConnectptr.i = scanptr.p->scanApiRec;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  cachePtr.i = apiConnectptr.p->cachePtr;
  ptrCheckGuard(cachePtr, ccacheFilesize, cacheRecord);
  switch (scanptr.p->scanState) {
  case ScanRecord::CLOSING_SCAN:
    jam();
    updateBuddyTimer(apiConnectptr);
unknown's avatar
unknown committed
9150 9151 9152
    {
      ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
      
unknown's avatar
unknown committed
9153
      run.release(scanFragptr);
unknown's avatar
unknown committed
9154 9155
    }
    close_scan_req_send_conf(signal, scanptr);
9156 9157 9158 9159 9160 9161
    return;
  default:
    jam();
    /*empty*/;
    break;
  }//switch
unknown's avatar
unknown committed
9162 9163
  Uint32 ref = calcLqhBlockRef(tnodeid);
  scanFragptr.p->lqhBlockref = ref;
9164
  scanFragptr.p->m_connectCount = getNodeInfo(tnodeid).m_connectCount;
unknown's avatar
unknown committed
9165
  sendScanFragReq(signal, scanptr.p, scanFragptr.p);
unknown's avatar
unknown committed
9166 9167
  if(ERROR_INSERTED(8035))
    globalTransporterRegistry.performSend();
9168 9169 9170 9171 9172 9173 9174
  attrbufptr.i = cachePtr.p->firstAttrbuf;
  while (attrbufptr.i != RNIL) {
    jam();
    ptrCheckGuard(attrbufptr, cattrbufFilesize, attrbufRecord);
    sendAttrinfo(signal,
                 scanFragptr.i,
                 attrbufptr.p,
unknown's avatar
unknown committed
9175
                 ref);
9176
    attrbufptr.i = attrbufptr.p->attrbuf[ZINBUF_NEXT];
unknown's avatar
unknown committed
9177 9178
    if(ERROR_INSERTED(8035))
      globalTransporterRegistry.performSend();
9179 9180 9181
  }//while
  scanFragptr.p->scanFragState = ScanFragRec::LQH_ACTIVE;
  scanFragptr.p->startFragTimer(ctcTimer);
unknown's avatar
unknown committed
9182
  updateBuddyTimer(apiConnectptr);
9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201
  /*********************************************
   * WE HAVE NOW STARTED A FRAGMENT SCAN. NOW 
   * WAIT FOR THE FIRST SCANNED RECORDS
   *********************************************/
}//Dbtc::execDIGETPRIMCONF

/***************************************************
 * execDIGETPRIMREF
 *
 * WE ARE NOW FORCED TO STOP THE SCAN. THIS ERROR 
 * IS NOT RECOVERABLE SINCE THERE IS A PROBLEM WITH 
 * FINDING A PRIMARY REPLICA OF A CERTAIN FRAGMENT.
 ***************************************************/
void Dbtc::execDIGETPRIMREF(Signal* signal) 
{
  jamEntry();
  // tcConnectptr.i in theData[0] is not used.
  scanFragptr.i = signal->theData[1];
  const Uint32 errCode = signal->theData[2];
unknown's avatar
unknown committed
9202
  c_scan_frag_pool.getPtr(scanFragptr);
9203
  ndbrequire(scanFragptr.p->scanFragState == ScanFragRec::WAIT_GET_PRIMCONF);
unknown's avatar
unknown committed
9204 9205 9206 9207 9208 9209 9210

  ScanRecordPtr scanptr;
  scanptr.i = scanFragptr.p->scanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);

  ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
  
unknown's avatar
unknown committed
9211
  run.release(scanFragptr);
unknown's avatar
unknown committed
9212 9213

  scanError(signal, scanptr, errCode);
9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229
}//Dbtc::execDIGETPRIMREF()

/**
 * Dbtc::execSCAN_FRAGREF
 *  Our attempt to scan a fragment was refused
 *  set error code and close all other fragment 
 *  scan's belonging to this scan
 */
void Dbtc::execSCAN_FRAGREF(Signal* signal) 
{
  const ScanFragRef * const ref = (ScanFragRef *)&signal->theData[0];
  
  jamEntry();
  const Uint32 errCode = ref->errorCode;

  scanFragptr.i = ref->senderData;
unknown's avatar
unknown committed
9230
  c_scan_frag_pool.getPtr(scanFragptr);
9231

unknown's avatar
unknown committed
9232
  ScanRecordPtr scanptr;
9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243
  scanptr.i = scanFragptr.p->scanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);

  apiConnectptr.i = scanptr.p->scanApiRec;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);

  Uint32 transid1 = apiConnectptr.p->transid[0] ^ ref->transId1;
  Uint32 transid2 = apiConnectptr.p->transid[1] ^ ref->transId2;
  transid1 = transid1 | transid2;
  if (transid1 != 0) {
    jam();
9244
    systemErrorLab(signal, __LINE__);
9245 9246 9247 9248 9249 9250 9251
  }//if

  /**
   * Set errorcode, close connection to this lqh fragment,
   * stop fragment timer and call scanFragError to start 
   * close of the other fragment scans
   */
unknown's avatar
unknown committed
9252 9253 9254 9255 9256
  ndbrequire(scanFragptr.p->scanFragState == ScanFragRec::LQH_ACTIVE);
  {
    scanFragptr.p->scanFragState = ScanFragRec::COMPLETED;
    ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
    
unknown's avatar
unknown committed
9257
    run.release(scanFragptr);
unknown's avatar
unknown committed
9258 9259 9260
    scanFragptr.p->stopFragTimer();
  }    
  scanError(signal, scanptr, errCode);
9261 9262 9263
}//Dbtc::execSCAN_FRAGREF()

/**
unknown's avatar
unknown committed
9264
 * Dbtc::scanError
9265 9266 9267
 *
 * Called when an error occurs during
 */ 
unknown's avatar
unknown committed
9268
void Dbtc::scanError(Signal* signal, ScanRecordPtr scanptr, Uint32 errorCode) 
9269 9270
{
  jam();
unknown's avatar
unknown committed
9271 9272 9273 9274
  ScanRecord* scanP = scanptr.p;
  
  DEBUG("scanError, errorCode = "<< errorCode << 
	", scanState = " << scanptr.p->scanState);
9275

unknown's avatar
unknown committed
9276 9277 9278 9279
  apiConnectptr.i = scanP->scanApiRec;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  ndbrequire(apiConnectptr.p->apiScanRec == scanptr.i);

unknown's avatar
unknown committed
9280 9281 9282 9283 9284
  if(scanP->scanState == ScanRecord::CLOSING_SCAN){
    jam();
    close_scan_req_send_conf(signal, scanptr);
    return;
  }
unknown's avatar
unknown committed
9285
  
unknown's avatar
unknown committed
9286 9287 9288 9289 9290 9291 9292 9293 9294
  ndbrequire(scanP->scanState == ScanRecord::RUNNING);
  
  /**
   * Close scan wo/ having received an order to do so
   */
  close_scan_req(signal, scanptr, false);
  
  const bool apiFail = (apiConnectptr.p->apiFailState == ZTRUE);
  if(apiFail){
9295 9296
    jam();
    return;
unknown's avatar
unknown committed
9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307
  }
  
  ScanTabRef * ref = (ScanTabRef*)&signal->theData[0];
  ref->apiConnectPtr = apiConnectptr.p->ndbapiConnect;
  ref->transId1 = apiConnectptr.p->transid[0];
  ref->transId2 = apiConnectptr.p->transid[1];
  ref->errorCode  = errorCode;
  ref->closeNeeded = 1;
  sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_SCAN_TABREF, 
	     signal, ScanTabRef::SignalLength, JBB);
}//Dbtc::scanError()
9308 9309 9310 9311 9312 9313 9314 9315 9316

/************************************************************
 * execSCAN_FRAGCONF
 *
 * A NUMBER OF OPERATIONS HAVE BEEN COMPLETED IN THIS 
 * FRAGMENT. TAKE CARE OF AND ISSUE FURTHER ACTIONS.
 ************************************************************/
void Dbtc::execSCAN_FRAGCONF(Signal* signal) 
{
unknown's avatar
unknown committed
9317
  Uint32 transid1, transid2, total_len;
9318 9319 9320 9321
  jamEntry();

  const ScanFragConf * const conf = (ScanFragConf*)&signal->theData[0];
  const Uint32 noCompletedOps = conf->completedOps;
unknown's avatar
unknown committed
9322
  const Uint32 status = conf->fragmentCompleted;
9323 9324

  scanFragptr.i = conf->senderData;
unknown's avatar
unknown committed
9325 9326
  c_scan_frag_pool.getPtr(scanFragptr);
  
unknown's avatar
unknown committed
9327
  ScanRecordPtr scanptr;
9328 9329
  scanptr.i = scanFragptr.p->scanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
unknown's avatar
unknown committed
9330
  
9331 9332 9333
  apiConnectptr.i = scanptr.p->scanApiRec;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);

unknown's avatar
unknown committed
9334 9335 9336
  transid1 = apiConnectptr.p->transid[0] ^ conf->transId1;
  transid2 = apiConnectptr.p->transid[1] ^ conf->transId2;
  total_len= conf->total_len;
9337 9338 9339
  transid1 = transid1 | transid2;
  if (transid1 != 0) {
    jam();
9340
    systemErrorLab(signal, __LINE__);
9341
  }//if
unknown's avatar
unknown committed
9342 9343 9344 9345
  
  ndbrequire(scanFragptr.p->scanFragState == ScanFragRec::LQH_ACTIVE);
  
  if(scanptr.p->scanState == ScanRecord::CLOSING_SCAN){
unknown's avatar
unknown committed
9346
    jam();
unknown's avatar
unknown committed
9347
    if(status == 0){
unknown's avatar
unknown committed
9348
      /**
unknown's avatar
unknown committed
9349
       * We have started closing = we sent a close -> ignore this
unknown's avatar
unknown committed
9350 9351 9352 9353 9354 9355
       */
      return;
    } else {
      jam();
      ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
      
unknown's avatar
unknown committed
9356
      run.release(scanFragptr);
unknown's avatar
unknown committed
9357 9358
      scanFragptr.p->stopFragTimer();
      scanFragptr.p->scanFragState = ScanFragRec::COMPLETED;
unknown's avatar
unknown committed
9359 9360
    }
    close_scan_req_send_conf(signal, scanptr);
9361
    return;
unknown's avatar
unknown committed
9362
  }
unknown's avatar
unknown committed
9363

unknown's avatar
unknown committed
9364
  if(noCompletedOps == 0 && status != 0 && 
9365
     scanptr.p->scanNextFragId+scanptr.p->m_booked_fragments_count < scanptr.p->scanNoFrag){
9366
    /**
unknown's avatar
unknown committed
9367
     * Start on next fragment
9368
     */
unknown's avatar
unknown committed
9369 9370
    scanFragptr.p->scanFragState = ScanFragRec::WAIT_GET_PRIMCONF; 
    scanFragptr.p->startFragTimer(ctcTimer);
9371

unknown's avatar
unknown committed
9372 9373 9374 9375 9376 9377 9378 9379
    tcConnectptr.i = scanptr.p->scanTcrec;
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    scanFragptr.p->scanFragId = scanptr.p->scanNextFragId++;
    signal->theData[0] = tcConnectptr.p->dihConnectptr;
    signal->theData[1] = scanFragptr.i;
    signal->theData[2] = scanptr.p->scanTableref;
    signal->theData[3] = scanFragptr.p->scanFragId;
    sendSignal(cdihblockref, GSN_DIGETPRIMREQ, signal, 4, JBB);
9380
    return;
unknown's avatar
unknown committed
9381
  }
unknown's avatar
unknown committed
9382
 /* 
unknown's avatar
unknown committed
9383 9384 9385 9386 9387
  Uint32 totalLen = 0;
  for(Uint32 i = 0; i<noCompletedOps; i++){
    Uint32 tmp = conf->opReturnDataLen[i];
    totalLen += tmp;
  }
unknown's avatar
unknown committed
9388
 */ 
unknown's avatar
unknown committed
9389 9390 9391 9392 9393 9394 9395 9396
  {
    ScanFragList run(c_scan_frag_pool, scanptr.p->m_running_scan_frags);
    ScanFragList queued(c_scan_frag_pool, scanptr.p->m_queued_scan_frags);
    
    run.remove(scanFragptr);
    queued.add(scanFragptr);
    scanptr.p->m_queued_count++;
  }
9397

unknown's avatar
unknown committed
9398
  scanFragptr.p->m_scan_frag_conf_status = status;
unknown's avatar
unknown committed
9399
  scanFragptr.p->m_ops = noCompletedOps;
unknown's avatar
unknown committed
9400
  scanFragptr.p->m_totalLen = total_len;
unknown's avatar
unknown committed
9401
  scanFragptr.p->scanFragState = ScanFragRec::QUEUED_FOR_DELIVERY;
9402
  scanFragptr.p->stopFragTimer();
unknown's avatar
unknown committed
9403
  
unknown's avatar
unknown committed
9404 9405
  if(scanptr.p->m_queued_count > /** Min */ 0){
    jam();
unknown's avatar
unknown committed
9406
    sendScanTabConf(signal, scanptr);
9407
  }
unknown's avatar
unknown committed
9408
}//Dbtc::execSCAN_FRAGCONF()
9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427

/****************************************************************************
 * execSCAN_NEXTREQ
 *
 * THE APPLICATION HAVE PROCESSED THE TUPLES TRANSFERRED AND IS NOW READY FOR
 * MORE. THIS SIGNAL IS ALSO USED TO CLOSE THE SCAN. 
 ****************************************************************************/
void Dbtc::execSCAN_NEXTREQ(Signal* signal) 
{
  const ScanNextReq * const req = (ScanNextReq *)&signal->theData[0];
  const UintR transid1 = req->transId1;
  const UintR transid2 = req->transId2;
  const UintR stopScan = req->stopScan;

  jamEntry();

  apiConnectptr.i = req->apiConnectPtr;
  if (apiConnectptr.i >= capiConnectFilesize) {
    jam();
9428
    warningHandlerLab(signal, __LINE__);
9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443
    return;
  }//if
  ptrAss(apiConnectptr, apiConnectRecord);

  /**
   * Check transid
   */
  const UintR ctransid1 = apiConnectptr.p->transid[0] ^ transid1;
  const UintR ctransid2 = apiConnectptr.p->transid[1] ^ transid2;
  if ((ctransid1 | ctransid2) != 0){
    ScanTabRef * ref = (ScanTabRef*)&signal->theData[0];
    ref->apiConnectPtr = apiConnectptr.p->ndbapiConnect;
    ref->transId1 = transid1;
    ref->transId2 = transid2;
    ref->errorCode  = ZSTATE_ERROR;
unknown's avatar
unknown committed
9444
    ref->closeNeeded = 0;
9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462
    sendSignal(signal->senderBlockRef(), GSN_SCAN_TABREF, 
	       signal, ScanTabRef::SignalLength, JBB);
    DEBUG("Wrong transid");
    return;
  }

  /**
   * Check state of API connection
   */
  if (apiConnectptr.p->apiConnectstate != CS_START_SCAN) {
    jam();
    if (apiConnectptr.p->apiConnectstate == CS_CONNECTED) {
      jam();
      /*********************************************************************
       * The application sends a SCAN_NEXTREQ after experiencing a time-out.
       *  We will send a SCAN_TABREF to indicate a time-out occurred.
       *********************************************************************/
      DEBUG("scanTabRefLab: ZSCANTIME_OUT_ERROR2");
9463
      ndbout_c("apiConnectptr(%d) -> abort", apiConnectptr.i);
9464 9465 9466 9467 9468 9469
      ndbrequire(false); //B2 indication of strange things going on
      scanTabRefLab(signal, ZSCANTIME_OUT_ERROR2);
      return;
    }
    DEBUG("scanTabRefLab: ZSTATE_ERROR");
    DEBUG("  apiConnectstate="<<apiConnectptr.p->apiConnectstate);
unknown's avatar
unknown committed
9470
    ndbrequire(false); //B2 indication of strange things going on
9471 9472 9473
    scanTabRefLab(signal, ZSTATE_ERROR);
    return;
  }//if
unknown's avatar
unknown committed
9474
  
9475 9476 9477 9478 9479
  /*******************************************************
   * START THE ACTUAL LOGIC OF SCAN_NEXTREQ. 
   ********************************************************/
  // Stop the timer that is used to check for timeout in the API 
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
unknown's avatar
unknown committed
9480
  ScanRecordPtr scanptr;
9481 9482
  scanptr.i = apiConnectptr.p->apiScanRec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
unknown's avatar
unknown committed
9483
  ScanRecord* scanP = scanptr.p;
9484

unknown's avatar
unknown committed
9485
  const Uint32 len = signal->getLength() - 4;
9486 9487 9488 9489 9490 9491

  if (stopScan == ZTRUE) {
    jam();
    /*********************************************************************
     * APPLICATION IS CLOSING THE SCAN.
     **********************************************************************/
unknown's avatar
unknown committed
9492
    close_scan_req(signal, scanptr, true);
9493 9494 9495
    return;
  }//if

unknown's avatar
unknown committed
9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506
  if (scanptr.p->scanState == ScanRecord::CLOSING_SCAN){
    jam();
    /**
     * The scan is closing (typically due to error)
     *   but the API hasn't understood it yet
     *
     * Wait for API close request
     */
    return;
  }

unknown's avatar
unknown committed
9507 9508
  // Copy op ptrs so I dont overwrite them when sending...
  memcpy(signal->getDataPtrSend()+25, signal->getDataPtr()+4, 4 * len);
9509

unknown's avatar
unknown committed
9510 9511 9512 9513 9514 9515
  ScanFragNextReq tmp;
  tmp.closeFlag = ZFALSE;
  tmp.transId1 = apiConnectptr.p->transid[0];
  tmp.transId2 = apiConnectptr.p->transid[1];
  tmp.batch_size_rows = scanP->batch_size_rows;
  tmp.batch_size_bytes = scanP->batch_byte_size;
unknown's avatar
unknown committed
9516

unknown's avatar
unknown committed
9517 9518 9519
  ScanFragList running(c_scan_frag_pool, scanP->m_running_scan_frags);
  ScanFragList delivered(c_scan_frag_pool, scanP->m_delivered_scan_frags);
  for(Uint32 i = 0 ; i<len; i++){
9520
    jam();
unknown's avatar
unknown committed
9521 9522 9523
    scanFragptr.i = signal->theData[i+25];
    c_scan_frag_pool.getPtr(scanFragptr);
    ndbrequire(scanFragptr.p->scanFragState == ScanFragRec::DELIVERED);
9524
    
unknown's avatar
unknown committed
9525 9526
    scanFragptr.p->startFragTimer(ctcTimer);
    scanFragptr.p->m_ops = 0;
unknown's avatar
unknown committed
9527

unknown's avatar
unknown committed
9528 9529 9530 9531 9532 9533 9534
    if(scanFragptr.p->m_scan_frag_conf_status)
    {
      /**
       * last scan was complete
       */
      jam();
      ndbrequire(scanptr.p->scanNextFragId < scanptr.p->scanNoFrag);
9535 9536 9537
      jam();
      ndbassert(scanptr.p->m_booked_fragments_count);
      scanptr.p->m_booked_fragments_count--;
unknown's avatar
unknown committed
9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558
      scanFragptr.p->scanFragState = ScanFragRec::WAIT_GET_PRIMCONF; 
      
      tcConnectptr.i = scanptr.p->scanTcrec;
      ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
      scanFragptr.p->scanFragId = scanptr.p->scanNextFragId++;
      signal->theData[0] = tcConnectptr.p->dihConnectptr;
      signal->theData[1] = scanFragptr.i;
      signal->theData[2] = scanptr.p->scanTableref;
      signal->theData[3] = scanFragptr.p->scanFragId;
      sendSignal(cdihblockref, GSN_DIGETPRIMREQ, signal, 4, JBB);
    }
    else
    {
      jam();
      scanFragptr.p->scanFragState = ScanFragRec::LQH_ACTIVE;
      ScanFragNextReq * req = (ScanFragNextReq*)signal->getDataPtrSend();
      * req = tmp;
      req->senderData = scanFragptr.i;
      sendSignal(scanFragptr.p->lqhBlockref, GSN_SCAN_NEXTREQ, signal, 
		 ScanFragNextReq::SignalLength, JBB);
    }
unknown's avatar
unknown committed
9559 9560 9561 9562 9563
    delivered.remove(scanFragptr);
    running.add(scanFragptr);
  }//for
  
}//Dbtc::execSCAN_NEXTREQ()
9564

unknown's avatar
unknown committed
9565
void
unknown's avatar
unknown committed
9566
Dbtc::close_scan_req(Signal* signal, ScanRecordPtr scanPtr, bool req_received){
unknown's avatar
unknown committed
9567

unknown's avatar
unknown committed
9568
  ScanRecord* scanP = scanPtr.p;
unknown's avatar
unknown committed
9569
  ndbrequire(scanPtr.p->scanState != ScanRecord::IDLE);  
unknown's avatar
unknown committed
9570
  scanPtr.p->scanState = ScanRecord::CLOSING_SCAN;
unknown's avatar
unknown committed
9571
  scanPtr.p->m_close_scan_req = req_received;
9572

unknown's avatar
unknown committed
9573
  /**
unknown's avatar
unknown committed
9574 9575 9576 9577 9578 9579 9580 9581
   * Queue         : Action
   * ============= : =================
   * completed     : -
   * running       : close -> LQH
   * delivered w/  : close -> LQH
   * delivered wo/ : move to completed
   * queued w/     : close -> LQH
   * queued wo/    : move to completed
unknown's avatar
unknown committed
9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592
   */
  
  ScanFragNextReq * nextReq = (ScanFragNextReq*)&signal->theData[0];
  nextReq->closeFlag = ZTRUE;
  nextReq->transId1 = apiConnectptr.p->transid[0];
  nextReq->transId2 = apiConnectptr.p->transid[1];
  
  {
    ScanFragRecPtr ptr;
    ScanFragList running(c_scan_frag_pool, scanP->m_running_scan_frags);
    ScanFragList delivered(c_scan_frag_pool, scanP->m_delivered_scan_frags);
unknown's avatar
unknown committed
9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613
    ScanFragList queued(c_scan_frag_pool, scanP->m_queued_scan_frags);
    
    // Close running
    for(running.first(ptr); !ptr.isNull(); ){
      ScanFragRecPtr curr = ptr; // Remove while iterating...
      running.next(ptr);

      if(curr.p->scanFragState == ScanFragRec::WAIT_GET_PRIMCONF){
	jam();
	continue;
      }
      ndbrequire(curr.p->scanFragState == ScanFragRec::LQH_ACTIVE);
      
      curr.p->startFragTimer(ctcTimer);
      curr.p->scanFragState = ScanFragRec::LQH_ACTIVE;
      nextReq->senderData = curr.i;
      sendSignal(curr.p->lqhBlockref, GSN_SCAN_NEXTREQ, signal, 
		 ScanFragNextReq::SignalLength, JBB);
    }

    // Close delivered
unknown's avatar
unknown committed
9614 9615 9616 9617 9618 9619 9620 9621
    for(delivered.first(ptr); !ptr.isNull(); ){
      jam();
      ScanFragRecPtr curr = ptr; // Remove while iterating...
      delivered.next(ptr);

      ndbrequire(curr.p->scanFragState == ScanFragRec::DELIVERED);
      delivered.remove(curr);
      
9622
      if(curr.p->m_ops > 0 && curr.p->m_scan_frag_conf_status == 0){
unknown's avatar
unknown committed
9623 9624 9625 9626 9627 9628 9629 9630 9631 9632
	jam();
	running.add(curr);
	curr.p->scanFragState = ScanFragRec::LQH_ACTIVE;
	curr.p->startFragTimer(ctcTimer);
	nextReq->senderData = curr.i;
	sendSignal(curr.p->lqhBlockref, GSN_SCAN_NEXTREQ, signal, 
		   ScanFragNextReq::SignalLength, JBB);
	
      } else {
	jam();
unknown's avatar
unknown committed
9633
	c_scan_frag_pool.release(curr);
unknown's avatar
unknown committed
9634 9635 9636 9637
	curr.p->scanFragState = ScanFragRec::COMPLETED;
	curr.p->stopFragTimer();
      }
    }//for
9638

unknown's avatar
unknown committed
9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660
    /**
     * All queued with data should be closed
     */
    for(queued.first(ptr); !ptr.isNull(); ){
      jam();
      ndbrequire(ptr.p->scanFragState == ScanFragRec::QUEUED_FOR_DELIVERY);
      ScanFragRecPtr curr = ptr; // Remove while iterating...
      queued.next(ptr);
      
      queued.remove(curr); 
      scanP->m_queued_count--;
      
      if(curr.p->m_ops > 0){
	jam();
	running.add(curr);
	curr.p->scanFragState = ScanFragRec::LQH_ACTIVE;
	curr.p->startFragTimer(ctcTimer);
	nextReq->senderData = curr.i;
	sendSignal(curr.p->lqhBlockref, GSN_SCAN_NEXTREQ, signal, 
		   ScanFragNextReq::SignalLength, JBB);
      } else {
	jam();
unknown's avatar
unknown committed
9661
	c_scan_frag_pool.release(curr);
unknown's avatar
unknown committed
9662 9663 9664 9665 9666
	curr.p->scanFragState = ScanFragRec::COMPLETED;
	curr.p->stopFragTimer();
      }
    }
  }
unknown's avatar
unknown committed
9667
  close_scan_req_send_conf(signal, scanPtr);
9668 9669
}

unknown's avatar
unknown committed
9670 9671 9672 9673
void
Dbtc::close_scan_req_send_conf(Signal* signal, ScanRecordPtr scanPtr){

  jam();
unknown's avatar
unknown committed
9674

unknown's avatar
unknown committed
9675 9676
  ndbrequire(scanPtr.p->m_queued_scan_frags.isEmpty());
  ndbrequire(scanPtr.p->m_delivered_scan_frags.isEmpty());
unknown's avatar
unknown committed
9677 9678
  //ndbrequire(scanPtr.p->m_running_scan_frags.isEmpty());

unknown's avatar
unknown committed
9679
#if 0
unknown's avatar
unknown committed
9680 9681 9682 9683 9684 9685 9686 9687 9688 9689
  {
    ScanFragList comp(c_scan_frag_pool, scanPtr.p->m_completed_scan_frags);
    ScanFragRecPtr ptr;
    for(comp.first(ptr); !ptr.isNull(); comp.next(ptr)){
      ndbrequire(ptr.p->scanFragTimer == 0);
      ndbrequire(ptr.p->scanFragState == ScanFragRec::COMPLETED);
    } 
  }
#endif
  
unknown's avatar
unknown committed
9690 9691 9692 9693
  if(!scanPtr.p->m_running_scan_frags.isEmpty()){
    jam();
    return;
  }
unknown's avatar
unknown committed
9694

unknown's avatar
unknown committed
9695 9696
  const bool apiFail = (apiConnectptr.p->apiFailState == ZTRUE);
  
unknown's avatar
unknown committed
9697 9698 9699 9700 9701 9702 9703 9704
  if(!scanPtr.p->m_close_scan_req){
    jam();
    /**
     * The API hasn't order closing yet
     */
    return;
  }

9705 9706
  Uint32 ref = apiConnectptr.p->ndbapiBlockref;
  if(!apiFail && ref){
unknown's avatar
unknown committed
9707 9708 9709 9710 9711 9712 9713 9714
    jam();
    ScanTabConf * conf = (ScanTabConf*)&signal->theData[0];
    conf->apiConnectPtr = apiConnectptr.p->ndbapiConnect;
    conf->requestInfo = ScanTabConf::EndOfData;
    conf->transId1 = apiConnectptr.p->transid[0];
    conf->transId2 = apiConnectptr.p->transid[1];
    sendSignal(ref, GSN_SCAN_TABCONF, signal, ScanTabConf::SignalLength, JBB);
  }
9715
  
unknown's avatar
unknown committed
9716 9717 9718 9719 9720 9721 9722 9723 9724 9725
  releaseScanResources(scanPtr);
  
  if(apiFail){
    jam();
    /**
     * API has failed
     */
    handleApiFailState(signal, apiConnectptr.i);
  }
}
9726

unknown's avatar
unknown committed
9727 9728 9729
Dbtc::ScanRecordPtr
Dbtc::seizeScanrec(Signal* signal) {
  ScanRecordPtr scanptr;
9730 9731 9732 9733 9734
  scanptr.i = cfirstfreeScanrec;
  ptrCheckGuard(scanptr, cscanrecFileSize, scanRecord);
  cfirstfreeScanrec = scanptr.p->nextScan;
  scanptr.p->nextScan = RNIL;
  ndbrequire(scanptr.p->scanState == ScanRecord::IDLE);
unknown's avatar
unknown committed
9735
  return scanptr;
9736 9737
}//Dbtc::seizeScanrec()

unknown's avatar
unknown committed
9738 9739
void Dbtc::sendScanFragReq(Signal* signal, 
			   ScanRecord* scanP, 
unknown's avatar
unknown committed
9740 9741 9742
			   ScanFragRec* scanFragP)
{
  ScanFragReq * const req = (ScanFragReq *)&signal->theData[0];
unknown's avatar
unknown committed
9743
  Uint32 requestInfo = scanP->scanRequestInfo;
9744
  ScanFragReq::setScanPrio(requestInfo, 1);
unknown's avatar
unknown committed
9745
  apiConnectptr.i = scanP->scanApiRec;
unknown's avatar
unknown committed
9746 9747
  req->tableId = scanP->scanTableref;
  req->schemaVersion = scanP->scanSchemaVersion;
9748 9749 9750
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  req->senderData = scanFragptr.i;
  req->requestInfo = requestInfo;
9751
  req->fragmentNoKeyLen = scanFragP->scanFragId | (scanP->scanKeyLen << 16);
unknown's avatar
unknown committed
9752 9753
  req->resultRef = apiConnectptr.p->ndbapiBlockref;
  req->savePointId = apiConnectptr.p->currSavePointId;
9754 9755
  req->transId1 = apiConnectptr.p->transid[0];
  req->transId2 = apiConnectptr.p->transid[1];
unknown's avatar
unknown committed
9756
  req->clientOpPtr = scanFragP->m_apiPtr;
unknown's avatar
unknown committed
9757
  req->batch_size_rows= scanP->batch_size_rows;
unknown's avatar
unknown committed
9758
  req->batch_size_bytes= scanP->batch_byte_size;
unknown's avatar
unknown committed
9759 9760
  sendSignal(scanFragP->lqhBlockref, GSN_SCAN_FRAGREQ, signal,
             ScanFragReq::SignalLength, JBB);
9761 9762 9763 9764 9765
  if(scanP->scanKeyLen > 0)
  {
    tcConnectptr.i = scanFragptr.i;
    packKeyData000Lab(signal, scanFragP->lqhBlockref, scanP->scanKeyLen);
  }
9766
  updateBuddyTimer(apiConnectptr);
unknown's avatar
unknown committed
9767
  scanFragP->startFragTimer(ctcTimer);
9768 9769 9770
}//Dbtc::sendScanFragReq()


unknown's avatar
unknown committed
9771
void Dbtc::sendScanTabConf(Signal* signal, ScanRecordPtr scanPtr) {
9772
  jam();
unknown's avatar
unknown committed
9773
  Uint32* ops = signal->getDataPtrSend()+4;
unknown's avatar
unknown committed
9774
  Uint32 op_count = scanPtr.p->m_queued_count;
unknown's avatar
unknown committed
9775
  if(4 + 3 * op_count > 25){
9776
    jam();
unknown's avatar
unknown committed
9777
    ops += 21;
9778
  }
9779
  
9780
  int left = scanPtr.p->scanNoFrag - scanPtr.p->scanNextFragId;
9781
  Uint32 booked = scanPtr.p->m_booked_fragments_count;
unknown's avatar
unknown committed
9782
  
9783 9784
  ScanTabConf * conf = (ScanTabConf*)&signal->theData[0];
  conf->apiConnectPtr = apiConnectptr.p->ndbapiConnect;
unknown's avatar
unknown committed
9785
  conf->requestInfo = op_count;
9786 9787
  conf->transId1 = apiConnectptr.p->transid[0];
  conf->transId2 = apiConnectptr.p->transid[1];
unknown's avatar
unknown committed
9788
  ScanFragRecPtr ptr;
unknown's avatar
unknown committed
9789 9790 9791 9792 9793 9794 9795 9796
  {
    ScanFragList queued(c_scan_frag_pool, scanPtr.p->m_queued_scan_frags);
    ScanFragList delivered(c_scan_frag_pool,scanPtr.p->m_delivered_scan_frags);
    for(queued.first(ptr); !ptr.isNull(); ){
      ndbrequire(ptr.p->scanFragState == ScanFragRec::QUEUED_FOR_DELIVERY);
      ScanFragRecPtr curr = ptr; // Remove while iterating...
      queued.next(ptr);
      
9797
      bool done = curr.p->m_scan_frag_conf_status && (left <= (int)booked);
9798
      if(curr.p->m_scan_frag_conf_status)
9799 9800
	booked++;
      
unknown's avatar
unknown committed
9801
      * ops++ = curr.p->m_apiPtr;
unknown's avatar
unknown committed
9802
      * ops++ = done ? RNIL : curr.i;
unknown's avatar
unknown committed
9803 9804 9805
      * ops++ = (curr.p->m_totalLen << 10) + curr.p->m_ops;
      
      queued.remove(curr); 
unknown's avatar
unknown committed
9806
      if(!done){
unknown's avatar
unknown committed
9807 9808 9809 9810 9811 9812 9813 9814
	delivered.add(curr);
	curr.p->scanFragState = ScanFragRec::DELIVERED;
	curr.p->stopFragTimer();
      } else {
	c_scan_frag_pool.release(curr);
	curr.p->scanFragState = ScanFragRec::COMPLETED;
	curr.p->stopFragTimer();
      }
unknown's avatar
unknown committed
9815 9816
    }
  }
unknown's avatar
unknown committed
9817
  
9818
  scanPtr.p->m_booked_fragments_count = booked;
unknown's avatar
unknown committed
9819
  if(scanPtr.p->m_delivered_scan_frags.isEmpty() && 
9820 9821
     scanPtr.p->m_running_scan_frags.isEmpty())
  {
unknown's avatar
unknown committed
9822 9823 9824
    conf->requestInfo = op_count | ScanTabConf::EndOfData;    
    releaseScanResources(scanPtr);
  }
unknown's avatar
unknown committed
9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837
  
  if(4 + 3 * op_count > 25){
    jam();
    LinearSectionPtr ptr[3];
    ptr[0].p = signal->getDataPtrSend()+25;
    ptr[0].sz = 3 * op_count;
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_SCAN_TABCONF, signal, 
	       ScanTabConf::SignalLength, JBB, ptr, 1);
  } else {
    jam();
    sendSignal(apiConnectptr.p->ndbapiBlockref, GSN_SCAN_TABCONF, signal, 
	       ScanTabConf::SignalLength + 3 * op_count, JBB);
  }
unknown's avatar
unknown committed
9838
  scanPtr.p->m_queued_count = 0;
9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856
}//Dbtc::sendScanTabConf()


void Dbtc::gcpTcfinished(Signal* signal) 
{
  signal->theData[1] = tcheckGcpId;
  sendSignal(cdihblockref, GSN_GCP_TCFINISHED, signal, 2, JBB);
}//Dbtc::gcpTcfinished()

void Dbtc::initApiConnect(Signal* signal) 
{
  Uint32 tiacTmp;
  Uint32 guard4;

  tiacTmp = capiConnectFilesize / 3;
  ndbrequire(tiacTmp > 0);
  guard4 = tiacTmp + 1;
  for (cachePtr.i = 0; cachePtr.i < guard4; cachePtr.i++) {
unknown's avatar
unknown committed
9857
    refresh_watch_dog();
9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871
    ptrAss(cachePtr, cacheRecord);
    cachePtr.p->firstAttrbuf = RNIL;
    cachePtr.p->lastAttrbuf = RNIL;
    cachePtr.p->firstKeybuf = RNIL;
    cachePtr.p->lastKeybuf = RNIL;
    cachePtr.p->nextCacheRec = cachePtr.i + 1;
  }//for
  cachePtr.i = tiacTmp;
  ptrCheckGuard(cachePtr, ccacheFilesize, cacheRecord);
  cachePtr.p->nextCacheRec = RNIL;
  cfirstfreeCacheRec = 0;

  guard4 = tiacTmp - 1;
  for (apiConnectptr.i = 0; apiConnectptr.i <= guard4; apiConnectptr.i++) {
unknown's avatar
unknown committed
9872
    refresh_watch_dog();
9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883
    jam();
    ptrAss(apiConnectptr, apiConnectRecord);
    apiConnectptr.p->apiConnectstate = CS_DISCONNECTED;
    apiConnectptr.p->apiFailState = ZFALSE;
    setApiConTimer(apiConnectptr.i, 0, __LINE__);
    apiConnectptr.p->takeOverRec = (Uint8)Z8NIL;
    apiConnectptr.p->cachePtr = RNIL;
    apiConnectptr.p->nextApiConnect = apiConnectptr.i + 1;
    apiConnectptr.p->ndbapiBlockref = 0xFFFFFFFF; // Invalid ref
    apiConnectptr.p->commitAckMarker = RNIL;
    apiConnectptr.p->firstTcConnect = RNIL;
unknown's avatar
unknown committed
9884
    apiConnectptr.p->lastTcConnect = RNIL;
9885 9886 9887 9888 9889 9890
    apiConnectptr.p->triggerPending = false;
    apiConnectptr.p->isIndexOp = false;
    apiConnectptr.p->accumulatingIndexOp = RNIL;
    apiConnectptr.p->executingIndexOp = RNIL;
    apiConnectptr.p->buddyPtr = RNIL;
    apiConnectptr.p->currSavePointId = 0;
unknown's avatar
unknown committed
9891
    apiConnectptr.p->m_transaction_nodes.clear();
9892 9893 9894 9895 9896 9897 9898 9899
  }//for
  apiConnectptr.i = tiacTmp - 1;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  apiConnectptr.p->nextApiConnect = RNIL;
  cfirstfreeApiConnect = 0;
  guard4 = (2 * tiacTmp) - 1;
  for (apiConnectptr.i = tiacTmp; apiConnectptr.i <= guard4; apiConnectptr.i++)
    {
unknown's avatar
unknown committed
9900
      refresh_watch_dog();
9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911
      jam();
      ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
      apiConnectptr.p->apiConnectstate = CS_RESTART;
      apiConnectptr.p->apiFailState = ZFALSE;
      setApiConTimer(apiConnectptr.i, 0, __LINE__);
      apiConnectptr.p->takeOverRec = (Uint8)Z8NIL;
      apiConnectptr.p->cachePtr = RNIL;
      apiConnectptr.p->nextApiConnect = apiConnectptr.i + 1;
      apiConnectptr.p->ndbapiBlockref = 0xFFFFFFFF; // Invalid ref
      apiConnectptr.p->commitAckMarker = RNIL;
      apiConnectptr.p->firstTcConnect = RNIL;
unknown's avatar
unknown committed
9912
      apiConnectptr.p->lastTcConnect = RNIL;
9913 9914 9915 9916 9917 9918
      apiConnectptr.p->triggerPending = false;
      apiConnectptr.p->isIndexOp = false;
      apiConnectptr.p->accumulatingIndexOp = RNIL;
      apiConnectptr.p->executingIndexOp = RNIL;
      apiConnectptr.p->buddyPtr = RNIL;
      apiConnectptr.p->currSavePointId = 0;
unknown's avatar
unknown committed
9919
      apiConnectptr.p->m_transaction_nodes.clear();
9920 9921 9922 9923 9924 9925 9926 9927
    }//for
  apiConnectptr.i = (2 * tiacTmp) - 1;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  apiConnectptr.p->nextApiConnect = RNIL;
  cfirstfreeApiConnectCopy = tiacTmp;
  guard4 = (3 * tiacTmp) - 1;
  for (apiConnectptr.i = 2 * tiacTmp; apiConnectptr.i <= guard4; 
       apiConnectptr.i++) {
unknown's avatar
unknown committed
9928
    refresh_watch_dog();
9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939
    jam();
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    setApiConTimer(apiConnectptr.i, 0, __LINE__);
    apiConnectptr.p->apiFailState = ZFALSE;
    apiConnectptr.p->apiConnectstate = CS_RESTART;
    apiConnectptr.p->takeOverRec = (Uint8)Z8NIL;
    apiConnectptr.p->cachePtr = RNIL;
    apiConnectptr.p->nextApiConnect = apiConnectptr.i + 1;
    apiConnectptr.p->ndbapiBlockref = 0xFFFFFFFF; // Invalid ref
    apiConnectptr.p->commitAckMarker = RNIL;
    apiConnectptr.p->firstTcConnect = RNIL;
unknown's avatar
unknown committed
9940
    apiConnectptr.p->lastTcConnect = RNIL;
9941 9942 9943 9944 9945 9946
    apiConnectptr.p->triggerPending = false;
    apiConnectptr.p->isIndexOp = false;
    apiConnectptr.p->accumulatingIndexOp = RNIL;
    apiConnectptr.p->executingIndexOp = RNIL;
    apiConnectptr.p->buddyPtr = RNIL;
    apiConnectptr.p->currSavePointId = 0;
unknown's avatar
unknown committed
9947
    apiConnectptr.p->m_transaction_nodes.clear();
9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958
  }//for
  apiConnectptr.i = (3 * tiacTmp) - 1;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  apiConnectptr.p->nextApiConnect = RNIL;
  cfirstfreeApiConnectFail = 2 * tiacTmp;
}//Dbtc::initApiConnect()

void Dbtc::initattrbuf(Signal* signal) 
{
  ndbrequire(cattrbufFilesize > 0);
  for (attrbufptr.i = 0; attrbufptr.i < cattrbufFilesize; attrbufptr.i++) {
unknown's avatar
unknown committed
9959
    refresh_watch_dog();
9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973
    jam();
    ptrAss(attrbufptr, attrbufRecord);
    attrbufptr.p->attrbuf[ZINBUF_NEXT] = attrbufptr.i + 1;  /* NEXT ATTRBUF */
  }//for
  attrbufptr.i = cattrbufFilesize - 1;
  ptrAss(attrbufptr, attrbufRecord);
  attrbufptr.p->attrbuf[ZINBUF_NEXT] = RNIL;    /* NEXT ATTRBUF */
  cfirstfreeAttrbuf = 0;
}//Dbtc::initattrbuf()

void Dbtc::initdatabuf(Signal* signal) 
{
  ndbrequire(cdatabufFilesize > 0);
  for (databufptr.i = 0; databufptr.i < cdatabufFilesize; databufptr.i++) {
unknown's avatar
unknown committed
9974
    refresh_watch_dog();
9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013
    ptrAss(databufptr, databufRecord);
    databufptr.p->nextDatabuf = databufptr.i + 1;
  }//for
  databufptr.i = cdatabufFilesize - 1;
  ptrCheckGuard(databufptr, cdatabufFilesize, databufRecord);
  databufptr.p->nextDatabuf = RNIL;
  cfirstfreeDatabuf = 0;
}//Dbtc::initdatabuf()

void Dbtc::initgcp(Signal* signal) 
{
  ndbrequire(cgcpFilesize > 0);
  for (gcpPtr.i = 0; gcpPtr.i < cgcpFilesize; gcpPtr.i++) {
    ptrAss(gcpPtr, gcpRecord);
    gcpPtr.p->nextGcp = gcpPtr.i + 1;
  }//for
  gcpPtr.i = cgcpFilesize - 1;
  ptrCheckGuard(gcpPtr, cgcpFilesize, gcpRecord);
  gcpPtr.p->nextGcp = RNIL;
  cfirstfreeGcp = 0;
  cfirstgcp = RNIL;
  clastgcp = RNIL;
}//Dbtc::initgcp()

void Dbtc::inithost(Signal* signal) 
{
  cpackedListIndex = 0;
  ndbrequire(chostFilesize > 0);
  for (hostptr.i = 0; hostptr.i < chostFilesize; hostptr.i++) {
    jam();
    ptrAss(hostptr, hostRecord);
    hostptr.p->hostStatus = HS_DEAD;
    hostptr.p->inPackedList = false;
    hostptr.p->lqhTransStatus = LTS_IDLE;
    hostptr.p->noOfWordsTCKEYCONF = 0;
    hostptr.p->noOfWordsTCINDXCONF = 0;
    hostptr.p->noOfPackedWordsLqh = 0;
    hostptr.p->hostLqhBlockRef = calcLqhBlockRef(hostptr.i);
  }//for
unknown's avatar
unknown committed
10014
  c_alive_nodes.clear();
10015 10016
}//Dbtc::inithost()

10017 10018
void Dbtc::initialiseRecordsLab(Signal* signal, UintR Tdata0, 
				Uint32 retRef, Uint32 retData) 
10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067
{
  switch (Tdata0) {
  case 0:
    jam();
    initApiConnect(signal);
    break;
  case 1:
    jam();
    initattrbuf(signal);
    break;
  case 2:
    jam();
    initdatabuf(signal);
    break;
  case 3:
    jam();
    initgcp(signal);
    break;
  case 4:
    jam();
    inithost(signal);
    break;
  case 5:
    jam();
    // UNUSED Free to initialise something
    break;
  case 6:
    jam();
    initTable(signal);
    break;
  case 7:
    jam();
    initialiseScanrec(signal);
    break;
  case 8:
    jam();
    initialiseScanOprec(signal);
    break;
  case 9:
    jam();
    initialiseScanFragrec(signal);
    break;
  case 10:
    jam();
    initialiseTcConnect(signal);
    break;
  case 11:
    jam();
    initTcFail(signal);
10068 10069 10070 10071 10072 10073 10074 10075

    {
      ReadConfigConf * conf = (ReadConfigConf*)signal->getDataPtrSend();
      conf->senderRef = reference();
      conf->senderData = retData;
      sendSignal(retRef, GSN_READ_CONFIG_CONF, signal, 
		 ReadConfigConf::SignalLength, JBB);
    }
10076 10077 10078 10079
    return;
    break;
  default:
    jam();
10080
    systemErrorLab(signal, __LINE__);
10081 10082 10083 10084
    return;
    break;
  }//switch

10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096
  signal->theData[0] = TcContinueB::ZINITIALISE_RECORDS;
  signal->theData[1] = Tdata0 + 1;
  signal->theData[2] = 0;
  signal->theData[3] = retRef;
  signal->theData[4] = retData;
  sendSignal(DBTC_REF, GSN_CONTINUEB, signal, 5, JBB);
}

/* ========================================================================= */
/* =======                       INITIALISE_SCANREC                  ======= */
/*                                                                           */
/* ========================================================================= */
10097 10098
void Dbtc::initialiseScanrec(Signal* signal) 
{
unknown's avatar
unknown committed
10099
  ScanRecordPtr scanptr;
10100 10101
  ndbrequire(cscanrecFileSize > 0);
  for (scanptr.i = 0; scanptr.i < cscanrecFileSize; scanptr.i++) {
unknown's avatar
unknown committed
10102
    refresh_watch_dog();
10103 10104
    jam();
    ptrAss(scanptr, scanRecord);
unknown's avatar
unknown committed
10105
    new (scanptr.p) ScanRecord();
10106
    scanptr.p->scanState = ScanRecord::IDLE;
unknown's avatar
unknown committed
10107
    scanptr.p->scanApiRec = RNIL;
10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128
    scanptr.p->nextScan = scanptr.i + 1;
  }//for
  scanptr.i = cscanrecFileSize - 1;
  ptrAss(scanptr, scanRecord);
  scanptr.p->nextScan = RNIL;
  cfirstfreeScanrec = 0;
}//Dbtc::initialiseScanrec()

void Dbtc::initialiseScanFragrec(Signal* signal) 
{
}//Dbtc::initialiseScanFragrec()

void Dbtc::initialiseScanOprec(Signal* signal) 
{
}//Dbtc::initialiseScanOprec()

void Dbtc::initTable(Signal* signal) 
{

  ndbrequire(ctabrecFilesize > 0);
  for (tabptr.i = 0; tabptr.i < ctabrecFilesize; tabptr.i++) {
unknown's avatar
unknown committed
10129
    refresh_watch_dog();
10130 10131 10132 10133 10134 10135
    ptrAss(tabptr, tableRecord);
    tabptr.p->currentSchemaVersion = 0;
    tabptr.p->storedTable = true;
    tabptr.p->tableType = 0;
    tabptr.p->enabled = false;
    tabptr.p->dropping = false;
unknown's avatar
unknown committed
10136 10137
    tabptr.p->noOfKeyAttr = 0;
    tabptr.p->hasCharAttr = 0;
10138
    tabptr.p->noOfDistrKeys = 0;
10139 10140 10141 10142 10143 10144 10145 10146 10147 10148
  }//for
}//Dbtc::initTable()

void Dbtc::initialiseTcConnect(Signal* signal) 
{
  ndbrequire(ctcConnectFilesize >= 2);

  // Place half of tcConnectptr's in cfirstfreeTcConnectFail list
  Uint32 titcTmp = ctcConnectFilesize / 2;
  for (tcConnectptr.i = 0; tcConnectptr.i < titcTmp; tcConnectptr.i++) {
unknown's avatar
unknown committed
10149
    refresh_watch_dog();
10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164
    jam();
    ptrAss(tcConnectptr, tcConnectRecord);
    tcConnectptr.p->tcConnectstate = OS_RESTART;
    tcConnectptr.p->apiConnect = RNIL;
    tcConnectptr.p->noOfNodes = 0;
    tcConnectptr.p->nextTcConnect = tcConnectptr.i + 1;
  }//for
  tcConnectptr.i = titcTmp - 1;
  ptrAss(tcConnectptr, tcConnectRecord);
  tcConnectptr.p->nextTcConnect = RNIL;
  cfirstfreeTcConnectFail = 0;

  // Place other half in cfirstfreeTcConnect list
  for (tcConnectptr.i = titcTmp; tcConnectptr.i < ctcConnectFilesize; 
       tcConnectptr.i++) {
unknown's avatar
unknown committed
10165
    refresh_watch_dog();
10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176
    jam();
    ptrAss(tcConnectptr, tcConnectRecord);
    tcConnectptr.p->tcConnectstate = OS_RESTART;
    tcConnectptr.p->apiConnect = RNIL;
    tcConnectptr.p->noOfNodes = 0;
    tcConnectptr.p->nextTcConnect = tcConnectptr.i + 1;
  }//for
  tcConnectptr.i = ctcConnectFilesize - 1;
  ptrAss(tcConnectptr, tcConnectRecord);
  tcConnectptr.p->nextTcConnect = RNIL;
  cfirstfreeTcConnect = titcTmp;
10177
  c_counters.cconcurrentOp = 0;
10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247
}//Dbtc::initialiseTcConnect()

/* ------------------------------------------------------------------------- */
/* ----     LINK A GLOBAL CHECKPOINT RECORD INTO THE LIST WITH TRANSACTIONS  */
/*          WAITING FOR COMPLETION.                                          */
/* ------------------------------------------------------------------------- */
void Dbtc::linkGciInGcilist(Signal* signal) 
{
  GcpRecordPtr tmpGcpPointer;
  if (cfirstgcp == RNIL) {
    jam();
    cfirstgcp = gcpPtr.i;
  } else {
    jam();
    tmpGcpPointer.i = clastgcp;
    ptrCheckGuard(tmpGcpPointer, cgcpFilesize, gcpRecord);
    tmpGcpPointer.p->nextGcp = gcpPtr.i;
  }//if
  clastgcp = gcpPtr.i;
}//Dbtc::linkGciInGcilist()

/* ------------------------------------------------------------------------- */
/* -------        LINK SECONDARY KEY BUFFER IN OPERATION RECORD      ------- */
/* ------------------------------------------------------------------------- */
void Dbtc::linkKeybuf(Signal* signal) 
{
  seizeDatabuf(signal);
  tmpDatabufptr.i = cachePtr.p->lastKeybuf;
  cachePtr.p->lastKeybuf = databufptr.i;
  if (tmpDatabufptr.i == RNIL) {
    jam();
    cachePtr.p->firstKeybuf = databufptr.i;
  } else {
    jam();
    ptrCheckGuard(tmpDatabufptr, cdatabufFilesize, databufRecord);
    tmpDatabufptr.p->nextDatabuf = databufptr.i;
  }//if
}//Dbtc::linkKeybuf()

/* ------------------------------------------------------------------------- */
/* ------- LINK A TC CONNECT RECORD INTO THE API LIST OF TC CONNECTIONS  --- */
/* ------------------------------------------------------------------------- */
void Dbtc::linkTcInConnectionlist(Signal* signal) 
{
  /* POINTER FOR THE CONNECT_RECORD */
  TcConnectRecordPtr ltcTcConnectptr;

  tcConnectptr.p->nextTcConnect = RNIL;
  ltcTcConnectptr.i = apiConnectptr.p->lastTcConnect;
  ptrCheck(ltcTcConnectptr, ctcConnectFilesize, tcConnectRecord);
  apiConnectptr.p->lastTcConnect = tcConnectptr.i;
  if (ltcTcConnectptr.i == RNIL) {
    jam();
    apiConnectptr.p->firstTcConnect = tcConnectptr.i;
  } else {
    jam();
    ptrGuard(ltcTcConnectptr);
    ltcTcConnectptr.p->nextTcConnect = tcConnectptr.i;
  }//if
}//Dbtc::linkTcInConnectionlist()

/*---------------------------------------------------------------------------*/
/*                    RELEASE_ABORT_RESOURCES                                */
/* THIS CODE RELEASES ALL RESOURCES AFTER AN ABORT OF A TRANSACTION AND ALSO */
/* SENDS THE ABORT DECISION TO THE APPLICATION.                              */
/*---------------------------------------------------------------------------*/
void Dbtc::releaseAbortResources(Signal* signal) 
{
  TcConnectRecordPtr rarTcConnectptr;

10248
  c_counters.cabortCount++;
10249 10250 10251
  if (apiConnectptr.p->cachePtr != RNIL) {
    cachePtr.i = apiConnectptr.p->cachePtr;
    ptrCheckGuard(cachePtr, ccacheFilesize, cacheRecord);
unknown's avatar
unknown committed
10252 10253
    releaseAttrinfo();
    releaseKeys();
10254 10255 10256 10257 10258 10259 10260 10261
  }//if
  tcConnectptr.i = apiConnectptr.p->firstTcConnect;
  while (tcConnectptr.i != RNIL) {
    jam();
    ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
    // Clear any markers that were set in CS_RECEIVING state
    clearCommitAckMarker(apiConnectptr.p, tcConnectptr.p);
    rarTcConnectptr.i = tcConnectptr.p->nextTcConnect;
unknown's avatar
unknown committed
10262
    releaseTcCon();
10263 10264 10265 10266
    tcConnectptr.i = rarTcConnectptr.i;
  }//while
  apiConnectptr.p->firstTcConnect = RNIL;
  apiConnectptr.p->lastTcConnect = RNIL;
unknown's avatar
unknown committed
10267
  apiConnectptr.p->m_transaction_nodes.clear();
10268 10269 10270 10271 10272 10273 10274 10275

  // MASV let state be CS_ABORTING until all 
  // signals in the "air" have been received. Reset to CS_CONNECTED
  // will be done when a TCKEYREQ with start flag is recieved
  // or releaseApiCon is called
  // apiConnectptr.p->apiConnectstate = CS_CONNECTED;
  apiConnectptr.p->apiConnectstate = CS_ABORTING;
  apiConnectptr.p->abortState = AS_IDLE;
10276
  releaseAllSeizedIndexOperations(apiConnectptr.p);
10277
  if(apiConnectptr.p->m_exec_flag || apiConnectptr.p->apiFailState == ZTRUE){
10278
    jam();
10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317
    bool ok = false;
    Uint32 blockRef = apiConnectptr.p->ndbapiBlockref;
    ReturnSignal ret = apiConnectptr.p->returnsignal;
    apiConnectptr.p->returnsignal = RS_NO_RETURN;
    apiConnectptr.p->m_exec_flag = 0;
    switch(ret){
    case RS_TCROLLBACKCONF:
      jam();
      ok = true;
      signal->theData[0] = apiConnectptr.p->ndbapiConnect;
      signal->theData[1] = apiConnectptr.p->transid[0];
      signal->theData[2] = apiConnectptr.p->transid[1];
      sendSignal(blockRef, GSN_TCROLLBACKCONF, signal, 3, JBB);
      break;
    case RS_TCROLLBACKREP:{
      jam();
      ok = true;
      TcRollbackRep * const tcRollbackRep = 
	(TcRollbackRep *) signal->getDataPtr();
      
      tcRollbackRep->connectPtr = apiConnectptr.p->ndbapiConnect;
      tcRollbackRep->transId[0] = apiConnectptr.p->transid[0];
      tcRollbackRep->transId[1] = apiConnectptr.p->transid[1];
      tcRollbackRep->returnCode = apiConnectptr.p->returncode;
      sendSignal(blockRef, GSN_TCROLLBACKREP, signal, 
		 TcRollbackRep::SignalLength, JBB);
    }
      break;
    case RS_NO_RETURN:
      jam();
      ok = true;
      break;
    case RS_TCKEYCONF:
    case RS_TC_COMMITCONF:
      break;
    }    
    if(!ok){
      jam();
      ndbout_c("returnsignal = %d", apiConnectptr.p->returnsignal);
10318
      sendSystemError(signal, __LINE__);
10319
    }//if
10320

10321
  }
unknown's avatar
unknown committed
10322 10323
  setApiConTimer(apiConnectptr.i, 0, 
		 100000+c_apiConTimer_line[apiConnectptr.i]);
10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340
  if (apiConnectptr.p->apiFailState == ZTRUE) {
    jam();
    handleApiFailState(signal, apiConnectptr.i);
    return;
  }//if
}//Dbtc::releaseAbortResources()

void Dbtc::releaseApiCon(Signal* signal, UintR TapiConnectPtr) 
{
  ApiConnectRecordPtr TlocalApiConnectptr;

  TlocalApiConnectptr.i = TapiConnectPtr;
  ptrCheckGuard(TlocalApiConnectptr, capiConnectFilesize, apiConnectRecord);
  TlocalApiConnectptr.p->nextApiConnect = cfirstfreeApiConnect;
  cfirstfreeApiConnect = TlocalApiConnectptr.i;
  setApiConTimer(TlocalApiConnectptr.i, 0, __LINE__);
  TlocalApiConnectptr.p->apiConnectstate = CS_DISCONNECTED;
unknown's avatar
unknown committed
10341
  ndbassert(TlocalApiConnectptr.p->m_transaction_nodes.isclear());
unknown's avatar
unknown committed
10342 10343
  ndbassert(TlocalApiConnectptr.p->apiScanRec == RNIL);
  TlocalApiConnectptr.p->ndbapiBlockref = 0;
10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361
}//Dbtc::releaseApiCon()

void Dbtc::releaseApiConnectFail(Signal* signal) 
{
  apiConnectptr.p->apiConnectstate = CS_RESTART;
  apiConnectptr.p->takeOverRec = (Uint8)Z8NIL;
  setApiConTimer(apiConnectptr.i, 0, __LINE__);
  apiConnectptr.p->nextApiConnect = cfirstfreeApiConnectFail;
  cfirstfreeApiConnectFail = apiConnectptr.i;
}//Dbtc::releaseApiConnectFail()

void Dbtc::releaseGcp(Signal* signal) 
{
  ptrGuard(gcpPtr);
  gcpPtr.p->nextGcp = cfirstfreeGcp;
  cfirstfreeGcp = gcpPtr.i;
}//Dbtc::releaseGcp()

unknown's avatar
unknown committed
10362
void Dbtc::releaseKeys() 
10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395
{
  UintR Tmp;
  databufptr.i = cachePtr.p->firstKeybuf;
  while (databufptr.i != RNIL) {
    jam();
    ptrCheckGuard(databufptr, cdatabufFilesize, databufRecord);
    Tmp = databufptr.p->nextDatabuf;
    databufptr.p->nextDatabuf = cfirstfreeDatabuf;
    cfirstfreeDatabuf = databufptr.i;
    databufptr.i = Tmp;
  }//while
  cachePtr.p->firstKeybuf = RNIL;
  cachePtr.p->lastKeybuf = RNIL;
}//Dbtc::releaseKeys()

void Dbtc::releaseTcConnectFail(Signal* signal) 
{
  ptrGuard(tcConnectptr);
  tcConnectptr.p->nextTcConnect = cfirstfreeTcConnectFail;
  cfirstfreeTcConnectFail = tcConnectptr.i;
}//Dbtc::releaseTcConnectFail()

void Dbtc::seizeApiConnect(Signal* signal) 
{
  if (cfirstfreeApiConnect != RNIL) {
    jam();
    terrorCode = ZOK;
    apiConnectptr.i = cfirstfreeApiConnect;     /* ASSIGN A FREE RECORD FROM */
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    cfirstfreeApiConnect = apiConnectptr.p->nextApiConnect;
    apiConnectptr.p->nextApiConnect = RNIL;
    setApiConTimer(apiConnectptr.i, 0, __LINE__);
    apiConnectptr.p->apiConnectstate = CS_CONNECTED; /* STATE OF CONNECTION */
unknown's avatar
unknown committed
10396 10397
    apiConnectptr.p->triggerPending = false;
    apiConnectptr.p->isIndexOp = false;
10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423
  } else {
    jam();
    terrorCode = ZNO_FREE_API_CONNECTION;
  }//if
}//Dbtc::seizeApiConnect()

void Dbtc::seizeApiConnectFail(Signal* signal) 
{
  apiConnectptr.i = cfirstfreeApiConnectFail;
  ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
  cfirstfreeApiConnectFail = apiConnectptr.p->nextApiConnect;
}//Dbtc::seizeApiConnectFail()

void Dbtc::seizeDatabuf(Signal* signal) 
{
  databufptr.i = cfirstfreeDatabuf;
  ptrCheckGuard(databufptr, cdatabufFilesize, databufRecord);
  cfirstfreeDatabuf = databufptr.p->nextDatabuf;
  databufptr.p->nextDatabuf = RNIL;
}//Dbtc::seizeDatabuf()

void Dbtc::seizeTcConnect(Signal* signal) 
{
  tcConnectptr.i = cfirstfreeTcConnect;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  cfirstfreeTcConnect = tcConnectptr.p->nextTcConnect;
10424
  c_counters.cconcurrentOp++;
10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545
  tcConnectptr.p->isIndexOp = false;
}//Dbtc::seizeTcConnect()

void Dbtc::seizeTcConnectFail(Signal* signal) 
{
  tcConnectptr.i = cfirstfreeTcConnectFail;
  ptrCheckGuard(tcConnectptr, ctcConnectFilesize, tcConnectRecord);
  cfirstfreeTcConnectFail = tcConnectptr.p->nextTcConnect;
}//Dbtc::seizeTcConnectFail()

void Dbtc::sendAttrinfo(Signal* signal,
                        UintR TattrinfoPtr,
                        AttrbufRecord * const regAttrPtr,
                        UintR TBref) 
{
  UintR TdataPos;
  UintR sig0, sig1, sig2, sig3, sig4, sig5, sig6, sig7;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  TdataPos = regAttrPtr->attrbuf[ZINBUF_DATA_LEN];
  sig0 = TattrinfoPtr;
  sig1 = regApiPtr->transid[0];
  sig2 = regApiPtr->transid[1];

  signal->theData[0] = sig0;
  signal->theData[1] = sig1;
  signal->theData[2] = sig2;

  sig0 = regAttrPtr->attrbuf[0];
  sig1 = regAttrPtr->attrbuf[1];
  sig2 = regAttrPtr->attrbuf[2];
  sig3 = regAttrPtr->attrbuf[3];
  sig4 = regAttrPtr->attrbuf[4];
  sig5 = regAttrPtr->attrbuf[5];
  sig6 = regAttrPtr->attrbuf[6];
  sig7 = regAttrPtr->attrbuf[7];

  signal->theData[3] = sig0;
  signal->theData[4] = sig1;
  signal->theData[5] = sig2;
  signal->theData[6] = sig3;
  signal->theData[7] = sig4;
  signal->theData[8] = sig5;
  signal->theData[9] = sig6;
  signal->theData[10] = sig7;

  if (TdataPos > 8) {
    sig0 = regAttrPtr->attrbuf[8];
    sig1 = regAttrPtr->attrbuf[9];
    sig2 = regAttrPtr->attrbuf[10];
    sig3 = regAttrPtr->attrbuf[11];
    sig4 = regAttrPtr->attrbuf[12];
    sig5 = regAttrPtr->attrbuf[13];
    sig6 = regAttrPtr->attrbuf[14];

    jam();
    signal->theData[11] = sig0;
    signal->theData[12] = sig1;
    signal->theData[13] = sig2;
    signal->theData[14] = sig3;
    signal->theData[15] = sig4;
    signal->theData[16] = sig5;
    signal->theData[17] = sig6;

    if (TdataPos > 15) {

      sig0 = regAttrPtr->attrbuf[15];
      sig1 = regAttrPtr->attrbuf[16];
      sig2 = regAttrPtr->attrbuf[17];
      sig3 = regAttrPtr->attrbuf[18];
      sig4 = regAttrPtr->attrbuf[19];
      sig5 = regAttrPtr->attrbuf[20];
      sig6 = regAttrPtr->attrbuf[21];

      jam();
      signal->theData[18] = sig0;
      signal->theData[19] = sig1;
      signal->theData[20] = sig2;
      signal->theData[21] = sig3;
      signal->theData[22] = sig4;
      signal->theData[23] = sig5;
      signal->theData[24] = sig6;
    }//if
  }//if
  sendSignal(TBref, GSN_ATTRINFO, signal, TdataPos + 3, JBB);
}//Dbtc::sendAttrinfo()

void Dbtc::sendContinueTimeOutControl(Signal* signal, Uint32 TapiConPtr) 
{
  signal->theData[0] = TcContinueB::ZCONTINUE_TIME_OUT_CONTROL;
  signal->theData[1] = TapiConPtr;
  sendSignal(cownref, GSN_CONTINUEB, signal, 2, JBB);
}//Dbtc::sendContinueTimeOutControl()

void Dbtc::sendKeyinfo(Signal* signal, BlockReference TBRef, Uint32 len) 
{
  signal->theData[0] = tcConnectptr.i;
  signal->theData[1] = apiConnectptr.p->transid[0];
  signal->theData[2] = apiConnectptr.p->transid[1];
  signal->theData[3] = cdata[0];
  signal->theData[4] = cdata[1];
  signal->theData[5] = cdata[2];
  signal->theData[6] = cdata[3];
  signal->theData[7] = cdata[4];
  signal->theData[8] = cdata[5];
  signal->theData[9] = cdata[6];
  signal->theData[10] = cdata[7];
  signal->theData[11] = cdata[8];
  signal->theData[12] = cdata[9];
  signal->theData[13] = cdata[10];
  signal->theData[14] = cdata[11];
  signal->theData[15] = cdata[12];
  signal->theData[16] = cdata[13];
  signal->theData[17] = cdata[14];
  signal->theData[18] = cdata[15];
  signal->theData[19] = cdata[16];
  signal->theData[20] = cdata[17];
  signal->theData[21] = cdata[18];
  signal->theData[22] = cdata[19];
  sendSignal(TBRef, GSN_KEYINFO, signal, 3 + len, JBB);
}//Dbtc::sendKeyinfo()

10546
void Dbtc::sendSystemError(Signal* signal, int line) 
10547
{
10548
  progError(line, NDBD_EXIT_NDBREQUIRE);
10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568
}//Dbtc::sendSystemError()

/* ========================================================================= */
/* -------             LINK ACTUAL GCP OUT OF LIST                   ------- */
/* ------------------------------------------------------------------------- */
void Dbtc::unlinkGcp(Signal* signal) 
{
  if (cfirstgcp == gcpPtr.i) {
    jam();
    cfirstgcp = gcpPtr.p->nextGcp;
    if (gcpPtr.i == clastgcp) {
      jam();
      clastgcp = RNIL;
    }//if
  } else {
    jam();
    /* -------------------------------------------------------------------- 
     * WE ARE TRYING TO REMOVE A GLOBAL CHECKPOINT WHICH WAS NOT THE OLDEST. 
     * THIS IS A SYSTEM ERROR.                                              
     * ------------------------------------------------------------------- */
10569
    sendSystemError(signal, __LINE__);
10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640
  }//if
  gcpPtr.p->nextGcp = cfirstfreeGcp;
  cfirstfreeGcp = gcpPtr.i;
}//Dbtc::unlinkGcp()

void
Dbtc::execDUMP_STATE_ORD(Signal* signal)
{
  DumpStateOrd * const dumpState = (DumpStateOrd *)&signal->theData[0];
  if(signal->theData[0] == DumpStateOrd::CommitAckMarkersSize){
    infoEvent("TC: m_commitAckMarkerPool: %d free size: %d",
              m_commitAckMarkerPool.getNoOfFree(),
              m_commitAckMarkerPool.getSize());
  }
  if(signal->theData[0] == DumpStateOrd::CommitAckMarkersDump){
    infoEvent("TC: m_commitAckMarkerPool: %d free size: %d",
              m_commitAckMarkerPool.getNoOfFree(),
              m_commitAckMarkerPool.getSize());
    
    CommitAckMarkerIterator iter;
    for(m_commitAckMarkerHash.first(iter); iter.curr.i != RNIL;
        m_commitAckMarkerHash.next(iter)){
      infoEvent("CommitAckMarker: i = %d (0x%x, 0x%x)"
                " Api: %d Lghs(%d): %d %d %d %d bucket = %d",
                iter.curr.i,
                iter.curr.p->transid1,
                iter.curr.p->transid2,
                iter.curr.p->apiNodeId,
                iter.curr.p->noOfLqhs,
                iter.curr.p->lqhNodeId[0],
                iter.curr.p->lqhNodeId[1],
                iter.curr.p->lqhNodeId[2],
                iter.curr.p->lqhNodeId[3],
                iter.bucket);
    }
  }
  // Dump all ScanFragRecs
  if (dumpState->args[0] == DumpStateOrd::TcDumpAllScanFragRec){
    Uint32 recordNo = 0;
    if (signal->getLength() == 1)
      infoEvent("TC: Dump all ScanFragRec - size: %d",
		cscanFragrecFileSize);
    else if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;
    
    dumpState->args[0] = DumpStateOrd::TcDumpOneScanFragRec;
    dumpState->args[1] = recordNo;
    execDUMP_STATE_ORD(signal);

    if (recordNo < cscanFragrecFileSize-1){
      dumpState->args[0] = DumpStateOrd::TcDumpAllScanFragRec;
      dumpState->args[1] = recordNo+1;
      sendSignal(reference(), GSN_DUMP_STATE_ORD, signal, 2, JBB);
    }
  }

  // Dump one ScanFragRec
  if (dumpState->args[0] == DumpStateOrd::TcDumpOneScanFragRec){
    Uint32 recordNo = RNIL;
    if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;

    if (recordNo >= cscanFragrecFileSize)
      return;

    ScanFragRecPtr sfp;
    sfp.i = recordNo;
unknown's avatar
unknown committed
10641 10642
    c_scan_frag_pool.getPtr(sfp);
    infoEvent("Dbtc::ScanFragRec[%d]: state=%d fragid=%d",
10643 10644
	      sfp.i,
	      sfp.p->scanFragState,
unknown's avatar
unknown committed
10645
	      sfp.p->scanFragId);
unknown's avatar
unknown committed
10646
    infoEvent(" nodeid=%d, timer=%d",
unknown's avatar
unknown committed
10647 10648
	      refToNode(sfp.p->lqhBlockref),
	      sfp.p->scanFragTimer);
10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714
  }

  // Dump all ScanRecords
  if (dumpState->args[0] == DumpStateOrd::TcDumpAllScanRec){
    Uint32 recordNo = 0;
    if (signal->getLength() == 1)
      infoEvent("TC: Dump all ScanRecord - size: %d",
		cscanrecFileSize);
    else if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;  

    dumpState->args[0] = DumpStateOrd::TcDumpOneScanRec;
    dumpState->args[1] = recordNo;
    execDUMP_STATE_ORD(signal);
    
    if (recordNo < cscanrecFileSize-1){
      dumpState->args[0] = DumpStateOrd::TcDumpAllScanRec;
      dumpState->args[1] = recordNo+1;
      sendSignal(reference(), GSN_DUMP_STATE_ORD, signal, 2, JBB);
    }
  }

  // Dump all active ScanRecords
  if (dumpState->args[0] == DumpStateOrd::TcDumpAllActiveScanRec){
    Uint32 recordNo = 0;     
    if (signal->getLength() == 1)
      infoEvent("TC: Dump active ScanRecord - size: %d",
		cscanrecFileSize);
    else if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;

    ScanRecordPtr sp;
    sp.i = recordNo;
    ptrAss(sp, scanRecord);
    if (sp.p->scanState != ScanRecord::IDLE){
      dumpState->args[0] = DumpStateOrd::TcDumpOneScanRec;
      dumpState->args[1] = recordNo;
      execDUMP_STATE_ORD(signal);
    }

    if (recordNo < cscanrecFileSize-1){
      dumpState->args[0] = DumpStateOrd::TcDumpAllActiveScanRec;
      dumpState->args[1] = recordNo+1;
      sendSignal(reference(), GSN_DUMP_STATE_ORD, signal, 2, JBB);
    }
  }

  // Dump one ScanRecord
  // and associated ScanFragRec and ApiConnectRecord
  if (dumpState->args[0] == DumpStateOrd::TcDumpOneScanRec){
    Uint32 recordNo = RNIL;
    if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;

    if (recordNo >= cscanrecFileSize)
      return;

    ScanRecordPtr sp;
    sp.i = recordNo;
    ptrAss(sp, scanRecord);
unknown's avatar
unknown committed
10715
    infoEvent("Dbtc::ScanRecord[%d]: state=%d"
10716 10717 10718 10719 10720 10721 10722 10723 10724
	      "nextfrag=%d, nofrag=%d",
	      sp.i,
	      sp.p->scanState,
	      sp.p->scanNextFragId,
	      sp.p->scanNoFrag);
    infoEvent(" ailen=%d, para=%d, receivedop=%d, noOprePperFrag=%d",
	      sp.p->scanAiLength,
	      sp.p->scanParallel,
	      sp.p->scanReceivedOperations,
unknown's avatar
unknown committed
10725
	      sp.p->batch_size_rows);
unknown's avatar
unknown committed
10726
    infoEvent(" schv=%d, tab=%d, sproc=%d",
10727 10728
	      sp.p->scanSchemaVersion,
	      sp.p->scanTableref,
unknown's avatar
unknown committed
10729
	      sp.p->scanStoredProcId);
10730 10731 10732 10733 10734
    infoEvent(" apiRec=%d, next=%d",
	      sp.p->scanApiRec, sp.p->nextScan);

    if (sp.p->scanState != ScanRecord::IDLE){
      // Request dump of ScanFragRec
unknown's avatar
unknown committed
10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747
      ScanFragRecPtr sfptr;
#define DUMP_SFR(x){\
      ScanFragList list(c_scan_frag_pool, x);\
      for(list.first(sfptr); !sfptr.isNull(); list.next(sfptr)){\
	dumpState->args[0] = DumpStateOrd::TcDumpOneScanFragRec; \
	dumpState->args[1] = sfptr.i;\
	execDUMP_STATE_ORD(signal);\
      }}

      DUMP_SFR(sp.p->m_running_scan_frags);
      DUMP_SFR(sp.p->m_queued_scan_frags);
      DUMP_SFR(sp.p->m_delivered_scan_frags);

10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823
      // Request dump of ApiConnectRecord
      dumpState->args[0] = DumpStateOrd::TcDumpOneApiConnectRec;
      dumpState->args[1] = sp.p->scanApiRec;
      execDUMP_STATE_ORD(signal);      
    }

  }

  // Dump all ApiConnectRecord(s)
  if (dumpState->args[0] == DumpStateOrd::TcDumpAllApiConnectRec){
    Uint32 recordNo = 0;
    if (signal->getLength() == 1)
      infoEvent("TC: Dump all ApiConnectRecord - size: %d",
		capiConnectFilesize);
    else if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;

    dumpState->args[0] = DumpStateOrd::TcDumpOneApiConnectRec;
    dumpState->args[1] = recordNo;
    execDUMP_STATE_ORD(signal);
    
    if (recordNo < capiConnectFilesize-1){
      dumpState->args[0] = DumpStateOrd::TcDumpAllApiConnectRec;
      dumpState->args[1] = recordNo+1;
      sendSignal(reference(), GSN_DUMP_STATE_ORD, signal, 2, JBB);
    }
  }

  // Dump one ApiConnectRecord
  if (dumpState->args[0] == DumpStateOrd::TcDumpOneApiConnectRec){
    Uint32 recordNo = RNIL;
    if (signal->getLength() == 2)
      recordNo = dumpState->args[1];
    else
      return;

    if (recordNo >= capiConnectFilesize)
      return;

    ApiConnectRecordPtr ap;
    ap.i = recordNo;
    ptrAss(ap, apiConnectRecord);
    infoEvent("Dbtc::ApiConnectRecord[%d]: state=%d, abortState=%d, "
	      "apiFailState=%d",
	      ap.i,
	      ap.p->apiConnectstate,
	      ap.p->abortState,
	      ap.p->apiFailState);
    infoEvent(" transid(0x%x, 0x%x), apiBref=0x%x, scanRec=%d",
	      ap.p->transid[0],
	      ap.p->transid[1],
	      ap.p->ndbapiBlockref,
	      ap.p->apiScanRec);
    infoEvent(" ctcTimer=%d, apiTimer=%d, counter=%d, retcode=%d, "
	      "retsig=%d",
	      ctcTimer, getApiConTimer(ap.i),
	      ap.p->counter,
	      ap.p->returncode,
	      ap.p->returnsignal);
    infoEvent(" lqhkeyconfrec=%d, lqhkeyreqrec=%d, "
	      "tckeyrec=%d",
	      ap.p->lqhkeyconfrec,
	      ap.p->lqhkeyreqrec,
	      ap.p->tckeyrec);
    infoEvent(" next=%d ",
	      ap.p->nextApiConnect);
  }

  if (dumpState->args[0] == DumpStateOrd::TcSetTransactionTimeout){
    jam();
    if(signal->getLength() > 1){
      set_timeout_value(signal->theData[1]);
    }
  }
unknown's avatar
unknown committed
10824 10825 10826 10827 10828 10829 10830

  if (dumpState->args[0] == DumpStateOrd::TcSetApplTransactionTimeout){
    jam();
    if(signal->getLength() > 1){
      set_appl_timeout_value(signal->theData[1]);
    }
  }
10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849

  if (dumpState->args[0] == DumpStateOrd::StartTcTimer){
    c_counters.c_trans_status = TransCounters::Started;
    c_counters.reset();
  }

  if (dumpState->args[0] == DumpStateOrd::StopTcTimer){
    c_counters.c_trans_status = TransCounters::Off;
    Uint32 len = c_counters.report(signal);
    sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, len, JBB);
    c_counters.reset();
  }
  
  if (dumpState->args[0] == DumpStateOrd::StartPeriodicTcTimer){
    c_counters.c_trans_status = TransCounters::Timer;
    c_counters.reset();
    signal->theData[0] = TcContinueB::ZTRANS_EVENT_REP;
    sendSignalWithDelay(cownref, GSN_CONTINUEB, signal, 5000, 1);
  }
10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876

  if (dumpState->args[0] == DumpStateOrd::TcStartDumpIndexOpCount)
  {
    static int frequency = 1;
    if (signal->getLength() > 1)
      frequency = signal->theData[1];
    else
      if (refToBlock(signal->getSendersBlockRef()) != DBTC)
	frequency = 1;
    
    if (frequency)
    {
      dumpState->args[0] = DumpStateOrd::TcDumpIndexOpCount;
      execDUMP_STATE_ORD(signal);
      dumpState->args[0] = DumpStateOrd::TcStartDumpIndexOpCount;
      
      Uint32 delay = 1000 * (frequency > 25 ? 25 : frequency);
      sendSignalWithDelay(cownref, GSN_DUMP_STATE_ORD, signal, delay, 1);
    }
  }
  
  if (dumpState->args[0] == DumpStateOrd::TcDumpIndexOpCount)
  {
    infoEvent("IndexOpCount: pool: %d free: %d", 
	      c_theIndexOperationPool.getSize(),
	      c_theIndexOperationPool.getNoOfFree());
  }
unknown's avatar
unknown committed
10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904

  if (dumpState->args[0] == 2514)
  {
    if (signal->getLength() == 2)
    {
      dumpState->args[0] = DumpStateOrd::TcDumpOneApiConnectRec;
      execDUMP_STATE_ORD(signal);
    }

    NodeReceiverGroup rg(CMVMI, c_alive_nodes);
    dumpState->args[0] = 15;
    sendSignal(rg, GSN_DUMP_STATE_ORD, signal, 1, JBB);

    signal->theData[0] = 2515;
    sendSignalWithDelay(cownref, GSN_DUMP_STATE_ORD, signal, 1000, 1);    
    return;
  }

  if (dumpState->args[0] == 2515)
  {
    NdbNodeBitmask mask = c_alive_nodes;
    mask.clear(getOwnNodeId());
    NodeReceiverGroup rg(NDBCNTR, mask);
    
    sendSignal(rg, GSN_SYSTEM_ERROR, signal, 1, JBB);
    sendSignalWithDelay(cownref, GSN_SYSTEM_ERROR, signal, 300, 1);    
    return;
  }
10905 10906 10907 10908
}//Dbtc::execDUMP_STATE_ORD()

void Dbtc::execSET_VAR_REQ(Signal* signal)
{
10909
#if 0
10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933
  SetVarReq* const setVarReq = (SetVarReq*)&signal->theData[0];
  ConfigParamId var = setVarReq->variable();
  int val = setVarReq->value();


  switch (var) {

  case TransactionInactiveTime:
    jam();
    set_appl_timeout_value(val);
    break;
  case TransactionDeadlockDetectionTimeout:
    set_timeout_value(val);
    sendSignal(CMVMI_REF, GSN_SET_VAR_CONF, signal, 1, JBB);
    break;

  case NoOfConcurrentProcessesHandleTakeover:
    set_no_parallel_takeover(val);
    sendSignal(CMVMI_REF, GSN_SET_VAR_CONF, signal, 1, JBB);
    break;

  default:
    sendSignal(CMVMI_REF, GSN_SET_VAR_REF, signal, 1, JBB);
  } // switch
10934
#endif
10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030
}

void Dbtc::execABORT_ALL_REQ(Signal* signal)
{
  jamEntry();
  AbortAllReq * req = (AbortAllReq*)&signal->theData[0];
  AbortAllRef * ref = (AbortAllRef*)&signal->theData[0];
  
  const Uint32 senderData = req->senderData;
  const BlockReference senderRef = req->senderRef;
  
  if(getAllowStartTransaction() == true && !getNodeState().getSingleUserMode()){
    jam();

    ref->senderData = senderData;
    ref->errorCode = AbortAllRef::InvalidState;
    sendSignal(senderRef, GSN_ABORT_ALL_REF, signal, 
	       AbortAllRef::SignalLength, JBB);
    return;
  }
  
  if(c_abortRec.clientRef != 0){
    jam();
    
    ref->senderData = senderData;
    ref->errorCode = AbortAllRef::AbortAlreadyInProgress;
    sendSignal(senderRef, GSN_ABORT_ALL_REF, signal, 
	       AbortAllRef::SignalLength, JBB);
    return;
  }

  if(refToNode(senderRef) != getOwnNodeId()){
    jam();
    
    ref->senderData = senderData;
    ref->errorCode = AbortAllRef::FunctionNotImplemented;
    sendSignal(senderRef, GSN_ABORT_ALL_REF, signal, 
	       AbortAllRef::SignalLength, JBB);
    return;
  }

  c_abortRec.clientRef = senderRef;
  c_abortRec.clientData = senderData;
  c_abortRec.oldTimeOutValue = ctimeOutValue;
  
  ctimeOutValue = 0;
  const Uint32 sleepTime = (2 * 10 * ctimeOutCheckDelay + 199) / 200;
  
  checkAbortAllTimeout(signal, (sleepTime == 0 ? 1 : sleepTime));
}

void Dbtc::checkAbortAllTimeout(Signal* signal, Uint32 sleepTime)
{
  
  ndbrequire(c_abortRec.clientRef != 0);
  
  if(sleepTime > 0){
    jam();
    
    sleepTime -= 1;
    signal->theData[0] = TcContinueB::ZWAIT_ABORT_ALL;
    signal->theData[1] = sleepTime;
    sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 200, 2);
    return;
  }

  AbortAllConf * conf = (AbortAllConf*)&signal->theData[0];
  conf->senderData = c_abortRec.clientData;
  sendSignal(c_abortRec.clientRef, GSN_ABORT_ALL_CONF, signal, 
	     AbortAllConf::SignalLength, JBB);
  
  ctimeOutValue = c_abortRec.oldTimeOutValue;
  c_abortRec.clientRef = 0;
}

/* **************************************************************** */
/* ---------------------------------------------------------------- */
/* ------------------ TRIGGER AND INDEX HANDLING ------------------ */
/* ---------------------------------------------------------------- */
/* **************************************************************** */

void Dbtc::execCREATE_TRIG_REQ(Signal* signal)
{
  jamEntry();
  CreateTrigReq * const createTrigReq = 
    (CreateTrigReq *)&signal->theData[0];
  TcDefinedTriggerData* triggerData;
  DefinedTriggerPtr triggerPtr;
  BlockReference sender = signal->senderBlockRef();

  releaseSections(signal);
  
  triggerPtr.i = createTrigReq->getTriggerId();
  if (ERROR_INSERTED(8033) ||
      !c_theDefinedTriggers.seizeId(triggerPtr, 
				    createTrigReq->getTriggerId())) {
11031
    jam();
11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065
    CLEAR_ERROR_INSERT_VALUE;
    // Failed to allocate trigger record
    CreateTrigRef * const createTrigRef =  
      (CreateTrigRef *)&signal->theData[0];
    
    createTrigRef->setConnectionPtr(createTrigReq->getConnectionPtr());
    createTrigRef->setErrorCode(CreateTrigRef::TooManyTriggers);
    sendSignal(sender, GSN_CREATE_TRIG_REF, 
               signal, CreateTrigRef::SignalLength, JBB);
    return;
  }

  triggerData = triggerPtr.p;
  triggerData->triggerId = createTrigReq->getTriggerId();
  triggerData->triggerType = createTrigReq->getTriggerType();
  triggerData->triggerEvent = createTrigReq->getTriggerEvent();
  triggerData->attributeMask = createTrigReq->getAttributeMask();
  if (triggerData->triggerType == TriggerType::SECONDARY_INDEX)
    triggerData->indexId = createTrigReq->getIndexId();
  CreateTrigConf * const createTrigConf =  
    (CreateTrigConf *)&signal->theData[0];
  
  createTrigConf->setConnectionPtr(createTrigReq->getConnectionPtr());
  sendSignal(sender, GSN_CREATE_TRIG_CONF, 
             signal, CreateTrigConf::SignalLength, JBB);
}


void Dbtc::execDROP_TRIG_REQ(Signal* signal)
{
  jamEntry();
  DropTrigReq * const dropTrigReq =  (DropTrigReq *)&signal->theData[0];
  BlockReference sender = signal->senderBlockRef();

11066 11067
  if (ERROR_INSERTED(8035) ||
      (c_theDefinedTriggers.getPtr(dropTrigReq->getTriggerId())) == NULL) {
11068
    jam();
11069
    CLEAR_ERROR_INSERT_VALUE;
11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100
    // Failed to find find trigger record
    DropTrigRef * const dropTrigRef =  (DropTrigRef *)&signal->theData[0];

    dropTrigRef->setConnectionPtr(dropTrigReq->getConnectionPtr());
    dropTrigRef->setErrorCode(DropTrigRef::TriggerNotFound);
    sendSignal(sender, GSN_DROP_TRIG_REF, 
               signal, DropTrigRef::SignalLength, JBB);
    return;
  }

  // Release trigger record
  c_theDefinedTriggers.release(dropTrigReq->getTriggerId());

  DropTrigConf * const dropTrigConf =  (DropTrigConf *)&signal->theData[0];
  
  dropTrigConf->setConnectionPtr(dropTrigReq->getConnectionPtr());
  sendSignal(sender, GSN_DROP_TRIG_CONF, 
             signal, DropTrigConf::SignalLength, JBB);
}

void Dbtc::execCREATE_INDX_REQ(Signal* signal)
{
  jamEntry();
  CreateIndxReq * const createIndxReq =  
    (CreateIndxReq *)signal->getDataPtr();
  TcIndexData* indexData;
  TcIndexDataPtr indexPtr;
  BlockReference sender = signal->senderBlockRef();
  
  if (ERROR_INSERTED(8034) ||
      !c_theIndexes.seizeId(indexPtr, createIndxReq->getIndexId())) {
11101
    jam();
11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197
    CLEAR_ERROR_INSERT_VALUE;
    // Failed to allocate index record
     CreateIndxRef * const createIndxRef =  
       (CreateIndxRef *)&signal->theData[0];

     createIndxRef->setConnectionPtr(createIndxReq->getConnectionPtr());
     createIndxRef->setErrorCode(CreateIndxRef::TooManyIndexes);
     releaseSections(signal);
     sendSignal(sender, GSN_CREATE_INDX_REF, 
                signal, CreateIndxRef::SignalLength, JBB);
     return;
  }
  indexData = indexPtr.p;
  // Indexes always start in state IS_BUILDING
  // Will become IS_ONLINE in execALTER_INDX_REQ
  indexData->indexState = IS_BUILDING;
  indexData->indexId = indexPtr.i;
  indexData->primaryTableId = createIndxReq->getTableId();

  // So far need only attribute count
  SegmentedSectionPtr ssPtr;
  signal->getSection(ssPtr, CreateIndxReq::ATTRIBUTE_LIST_SECTION);
  SimplePropertiesSectionReader r0(ssPtr, getSectionSegmentPool());
  r0.reset(); // undo implicit first()
  if (!r0.getWord(&indexData->attributeList.sz) ||
      !r0.getWords(indexData->attributeList.id, indexData->attributeList.sz)) {
    ndbrequire(false);
  }
  indexData->primaryKeyPos = indexData->attributeList.sz;

  releaseSections(signal);
  
  CreateIndxConf * const createIndxConf =  
    (CreateIndxConf *)&signal->theData[0];

  createIndxConf->setConnectionPtr(createIndxReq->getConnectionPtr());
  createIndxConf->setTableId(createIndxReq->getTableId());
  createIndxConf->setIndexId(createIndxReq->getIndexId());
  sendSignal(sender, GSN_CREATE_INDX_CONF, 
             signal, CreateIndxConf::SignalLength, JBB);
}

void Dbtc::execALTER_INDX_REQ(Signal* signal)
{
  jamEntry();
  AlterIndxReq * const alterIndxReq =  (AlterIndxReq *)signal->getDataPtr();
  TcIndexData* indexData;
  //BlockReference sender = signal->senderBlockRef();
  BlockReference sender = (BlockReference) alterIndxReq->getUserRef();
  Uint32 connectionPtr = alterIndxReq->getConnectionPtr();
  AlterIndxReq::RequestType requestType = alterIndxReq->getRequestType();
  Uint32 tableId = alterIndxReq->getTableId();
  Uint32 indexId = alterIndxReq->getIndexId();
  bool online = (alterIndxReq->getOnline() == 1) ? true : false;

  if ((indexData = c_theIndexes.getPtr(indexId)) == NULL) {
    jam();
    // Failed to find index record
    AlterIndxRef * const alterIndxRef =  
      (AlterIndxRef *)signal->getDataPtrSend();
    
    alterIndxRef->setUserRef(reference());
    alterIndxRef->setConnectionPtr(connectionPtr);
    alterIndxRef->setRequestType(requestType);
    alterIndxRef->setTableId(tableId);
    alterIndxRef->setIndexId(indexId);
    alterIndxRef->setErrorCode(AlterIndxRef::IndexNotFound);
    alterIndxRef->setErrorLine(__LINE__);
    alterIndxRef->setErrorNode(getOwnNodeId());
    sendSignal(sender, GSN_ALTER_INDX_REF, 
	       signal, AlterIndxRef::SignalLength, JBB);
    return;
  }
  // Found index record, alter it's state  
  if (online) {
    jam();
    indexData->indexState = IS_ONLINE;
  } else {
    jam();
    indexData->indexState = IS_BUILDING;
  }//if 
  AlterIndxConf * const alterIndxConf =  
    (AlterIndxConf *)signal->getDataPtrSend();
  
  alterIndxConf->setUserRef(reference());
  alterIndxConf->setConnectionPtr(connectionPtr);
  alterIndxConf->setRequestType(requestType);
  alterIndxConf->setTableId(tableId);
  alterIndxConf->setIndexId(indexId);
  sendSignal(sender, GSN_ALTER_INDX_CONF, 
	     signal, AlterIndxConf::SignalLength, JBB);
}

void Dbtc::execFIRE_TRIG_ORD(Signal* signal)
{
  jamEntry();
11198
  FireTrigOrd * const fireOrd =  (FireTrigOrd *)signal->getDataPtr();
11199 11200 11201 11202
  ApiConnectRecord *localApiConnectRecord = apiConnectRecord;
  ApiConnectRecordPtr transPtr;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  TcConnectRecordPtr opPtr;
11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235

  /**
   * TODO
   * Check transid,
   * Fix overload i.e invalid word count
   */
  TcFiredTriggerData key;
  key.fireingOperation = fireOrd->getConnectionPtr();
  key.nodeId = refToNode(signal->getSendersBlockRef());
  FiredTriggerPtr trigPtr;
  if(c_firedTriggerHash.find(trigPtr, key)){
    
    c_firedTriggerHash.remove(trigPtr);

    bool ok = trigPtr.p->keyValues.getSize() == fireOrd->m_noPrimKeyWords;
    ok &= trigPtr.p->afterValues.getSize() == fireOrd->m_noAfterValueWords;
    ok &= trigPtr.p->beforeValues.getSize() == fireOrd->m_noBeforeValueWords;
    if(ok){
      opPtr.i = key.fireingOperation;
      ptrCheckGuard(opPtr, ctcConnectFilesize, localTcConnectRecord);
      transPtr.i = opPtr.p->apiConnect;
      transPtr.p = &localApiConnectRecord[transPtr.i];
      
      opPtr.p->noReceivedTriggers++;
      opPtr.p->triggerExecutionCount++;
    
      // Insert fired trigger in execution queue
      transPtr.p->theFiredTriggers.add(trigPtr);
      if (opPtr.p->noReceivedTriggers == opPtr.p->noFiredTriggers) {
	executeTriggers(signal, &transPtr);
      }
      return;
    }
11236
    jam();
11237 11238 11239 11240 11241 11242 11243
    c_theFiredTriggerPool.release(trigPtr);
  }
  jam();
  /**
   * Failed to find record or invalid word counts
   */
  ndbrequire(false);
11244 11245 11246 11247 11248 11249 11250 11251 11252 11253
}

void Dbtc::execTRIG_ATTRINFO(Signal* signal)
{
  jamEntry();
  TrigAttrInfo * const trigAttrInfo =  (TrigAttrInfo *)signal->getDataPtr();
  Uint32 attrInfoLength = signal->getLength() - TrigAttrInfo::StaticLength;
  const Uint32 *src = trigAttrInfo->getData();
  FiredTriggerPtr firedTrigPtr;
  
11254 11255 11256 11257
  TcFiredTriggerData key;
  key.fireingOperation = trigAttrInfo->getConnectionPtr();
  key.nodeId = refToNode(signal->getSendersBlockRef());
  if(!c_firedTriggerHash.find(firedTrigPtr, key)){
11258
    jam();
11259
    if(!c_firedTriggerHash.seize(firedTrigPtr)){
11260
      jam();
11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273
      /**
       * Will be handled when FIRE_TRIG_ORD arrives
       */
      ndbout_c("op: %d node: %d failed to seize",
	       key.fireingOperation, key.nodeId);
      return;
    }
    ndbrequire(firedTrigPtr.p->keyValues.getSize() == 0 &&
	       firedTrigPtr.p->beforeValues.getSize() == 0 &&
	       firedTrigPtr.p->afterValues.getSize() == 0);
    
    firedTrigPtr.p->nodeId = refToNode(signal->getSendersBlockRef());
    firedTrigPtr.p->fireingOperation = key.fireingOperation;
11274
    firedTrigPtr.p->triggerId = trigAttrInfo->getTriggerId();
11275 11276 11277 11278
    c_firedTriggerHash.add(firedTrigPtr);
  }
  
  AttributeBuffer::DataBufferPool & pool = c_theAttributeBufferPool;
11279 11280 11281
  switch (trigAttrInfo->getAttrInfoType()) {
  case(TrigAttrInfo::PRIMARY_KEY):
    jam();
11282 11283 11284
    {
      LocalDataBuffer<11> buf(pool, firedTrigPtr.p->keyValues);
      buf.append(src, attrInfoLength);
11285 11286 11287 11288
    }
    break;
  case(TrigAttrInfo::BEFORE_VALUES):
    jam();
11289 11290 11291
    {
      LocalDataBuffer<11> buf(pool, firedTrigPtr.p->beforeValues);
      buf.append(src, attrInfoLength);
11292 11293 11294 11295
    }
    break;
  case(TrigAttrInfo::AFTER_VALUES):
    jam();
11296 11297 11298
    {
      LocalDataBuffer<11> buf(pool, firedTrigPtr.p->afterValues);
      buf.append(src, attrInfoLength);
11299 11300
    }
    break;
11301 11302
  default:
    ndbrequire(false);
11303 11304 11305 11306 11307 11308 11309 11310 11311 11312
  }
}

void Dbtc::execDROP_INDX_REQ(Signal* signal)
{
  jamEntry();
  DropIndxReq * const dropIndxReq =  (DropIndxReq *)signal->getDataPtr();
  TcIndexData* indexData;
  BlockReference sender = signal->senderBlockRef();
  
11313 11314
  if (ERROR_INSERTED(8036) ||
      (indexData = c_theIndexes.getPtr(dropIndxReq->getIndexId())) == NULL) {
11315
    jam();
11316
    CLEAR_ERROR_INSERT_VALUE;
11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341
    // Failed to find index record
    DropIndxRef * const dropIndxRef =  
      (DropIndxRef *)signal->getDataPtrSend();

    dropIndxRef->setConnectionPtr(dropIndxReq->getConnectionPtr());
    dropIndxRef->setErrorCode(DropIndxRef::IndexNotFound);
    sendSignal(sender, GSN_DROP_INDX_REF, 
               signal, DropIndxRef::SignalLength, JBB);
    return;
  }
  // Release index record
  c_theIndexes.release(dropIndxReq->getIndexId());

  DropIndxConf * const dropIndxConf =  
    (DropIndxConf *)signal->getDataPtrSend();

  dropIndxConf->setConnectionPtr(dropIndxReq->getConnectionPtr());
  sendSignal(sender, GSN_DROP_INDX_CONF, 
             signal, DropIndxConf::SignalLength, JBB);
}

void Dbtc::execTCINDXREQ(Signal* signal)
{
  jamEntry();

unknown's avatar
unknown committed
11342
  TcKeyReq * const tcIndxReq =  (TcKeyReq *)signal->getDataPtr();
11343 11344 11345 11346 11347 11348 11349 11350 11351 11352
  const UintR TapiIndex = tcIndxReq->apiConnectPtr;
  Uint32 tcIndxRequestInfo = tcIndxReq->requestInfo;
  Uint32 startFlag = tcIndxReq->getStartFlag(tcIndxRequestInfo);
  Uint32 * dataPtr = &tcIndxReq->scanInfo;
  Uint32 indexBufSize = 8; // Maximum for index in TCINDXREQ
  Uint32 attrBufSize = 5;  // Maximum for attrInfo in TCINDXREQ
  ApiConnectRecordPtr transPtr;
  transPtr.i = TapiIndex;
  if (transPtr.i >= capiConnectFilesize) {
    jam();
11353
    warningHandlerLab(signal, __LINE__);
11354 11355 11356 11357 11358 11359 11360
    return;
  }//if
  ptrAss(transPtr, apiConnectRecord);
  ApiConnectRecord * const regApiPtr = transPtr.p;  
  // Seize index operation
  TcIndexOperationPtr indexOpPtr;
  if ((startFlag == 1) &&
unknown's avatar
unknown committed
11361 11362 11363 11364 11365
      (regApiPtr->apiConnectstate == CS_CONNECTED ||
       (regApiPtr->apiConnectstate == CS_STARTED && 
	regApiPtr->firstTcConnect == RNIL)) ||
      (regApiPtr->apiConnectstate == CS_ABORTING && 
       regApiPtr->abortState == AS_IDLE)) {
11366 11367 11368
    jam();
    // This is a newly started transaction, clean-up
    releaseAllSeizedIndexOperations(regApiPtr);
11369 11370 11371

    regApiPtr->transid[0] = tcIndxReq->transId1;
    regApiPtr->transid[1] = tcIndxReq->transId2;
11372
  }//if
11373 11374

  if (ERROR_INSERTED(8036) || !seizeIndexOperation(regApiPtr, indexOpPtr)) {
11375 11376
    jam();
    // Failed to allocate index operation
unknown's avatar
unknown committed
11377 11378 11379 11380
    terrorCode = 288;
    regApiPtr->m_exec_flag |= TcKeyReq::getExecuteFlag(tcIndxRequestInfo);
    apiConnectptr = transPtr;
    abortErrorLab(signal);
11381 11382 11383 11384 11385 11386
    return;
  }
  TcIndexOperation* indexOp = indexOpPtr.p;
  indexOp->indexOpId = indexOpPtr.i;

  // Save original signal
unknown's avatar
unknown committed
11387
  indexOp->tcIndxReq = *tcIndxReq;
11388 11389 11390 11391 11392
  indexOp->connectionIndex = TapiIndex;
  regApiPtr->accumulatingIndexOp = indexOp->indexOpId;

  // If operation is readTupleExclusive or updateTuple then read index 
  // table with exclusive lock
unknown's avatar
unknown committed
11393
  Uint32 indexLength = TcKeyReq::getKeyLength(tcIndxRequestInfo);
11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434
  Uint32 attrLength = tcIndxReq->attrLen;
  indexOp->expectedKeyInfo = indexLength;
  Uint32 includedIndexLength = MIN(indexLength, indexBufSize);
  indexOp->expectedAttrInfo = attrLength;
  Uint32 includedAttrLength = MIN(attrLength, attrBufSize);
  if (saveINDXKEYINFO(signal, 
                      indexOp, 
                      dataPtr, 
                      includedIndexLength)) {
    jam();
    // We have received all we need
    readIndexTable(signal, regApiPtr, indexOp);
    return;
  }
  dataPtr += includedIndexLength;
  if (saveINDXATTRINFO(signal, 
                       indexOp, 
                       dataPtr, 
                       includedAttrLength)) {
    jam();
    // We have received all we need
    readIndexTable(signal, regApiPtr, indexOp);
    return;
  }
}


void Dbtc::sendTcIndxConf(Signal* signal, UintR TcommitFlag) 
{
  HostRecordPtr localHostptr;
  ApiConnectRecord * const regApiPtr = apiConnectptr.p;
  const UintR TopWords = (UintR)regApiPtr->tcindxrec;
  localHostptr.i = refToNode(regApiPtr->ndbapiBlockref);
  const Uint32 type = getNodeInfo(localHostptr.i).m_type;
  const bool is_api = (type >= NodeInfo::API && type <= NodeInfo::REP);
  const BlockNumber TblockNum = refToBlock(regApiPtr->ndbapiBlockref);
  const Uint32 Tmarker = (regApiPtr->commitAckMarker == RNIL ? 0 : 1);
  ptrAss(localHostptr, hostRecord);
  UintR TcurrLen = localHostptr.p->noOfWordsTCINDXCONF;
  UintR confInfo = 0;
  TcIndxConf::setNoOfOperations(confInfo, (TopWords >> 1));
unknown's avatar
unknown committed
11435
  TcIndxConf::setCommitFlag(confInfo, TcommitFlag == 1);
11436 11437 11438
  TcIndxConf::setMarkerFlag(confInfo, Tmarker);
  const UintR TpacketLen = 6 + TopWords;
  regApiPtr->tcindxrec = 0;
unknown's avatar
unknown committed
11439 11440 11441 11442 11443 11444

  if(TcommitFlag || (regApiPtr->lqhkeyreqrec == regApiPtr->lqhkeyconfrec)){
    jam();
    regApiPtr->m_exec_flag = 0;
  }

11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506
  if ((TpacketLen > 25) || !is_api){
    TcIndxConf * const tcIndxConf = (TcIndxConf *)signal->getDataPtrSend();
    
    jam();
    tcIndxConf->apiConnectPtr = regApiPtr->ndbapiConnect;
    tcIndxConf->gci = regApiPtr->globalcheckpointid;;
    tcIndxConf->confInfo = confInfo;
    tcIndxConf->transId1 = regApiPtr->transid[0];
    tcIndxConf->transId2 = regApiPtr->transid[1];
    copyFromToLen(&regApiPtr->tcIndxSendArray[0],
                  (UintR*)&tcIndxConf->operations,
                  (UintR)ZTCOPCONF_SIZE);
    sendSignal(regApiPtr->ndbapiBlockref,
               GSN_TCINDXCONF, signal, (TpacketLen - 1), JBB);
    return;
  } else if (((TcurrLen + TpacketLen) > 25) && (TcurrLen > 0)) {
    jam();
    sendPackedTCINDXCONF(signal, localHostptr.p, localHostptr.i);
    TcurrLen = 0;
  } else {
    jam();
    updatePackedList(signal, localHostptr.p, localHostptr.i);
  }//if
// -------------------------------------------------------------------------
// The header contains the block reference of receiver plus the real signal
// length - 3, since we have the real signal length plus one additional word
// for the header we have to do - 4.
// -------------------------------------------------------------------------
  UintR Tpack0 = (TblockNum << 16) + (TpacketLen - 4);
  UintR Tpack1 = regApiPtr->ndbapiConnect;
  UintR Tpack2 = regApiPtr->globalcheckpointid;
  UintR Tpack3 = confInfo;
  UintR Tpack4 = regApiPtr->transid[0];
  UintR Tpack5 = regApiPtr->transid[1];

  localHostptr.p->noOfWordsTCINDXCONF = TcurrLen + TpacketLen;

  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 0] = Tpack0;
  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 1] = Tpack1;
  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 2] = Tpack2;
  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 3] = Tpack3;
  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 4] = Tpack4;
  localHostptr.p->packedWordsTCINDXCONF[TcurrLen + 5] = Tpack5;

  UintR Ti;
  for (Ti = 6; Ti < TpacketLen; Ti++) {
    localHostptr.p->packedWordsTCINDXCONF[TcurrLen + Ti] = 
          regApiPtr->tcIndxSendArray[Ti - 6];
  }//for
}//Dbtc::sendTcIndxConf()

void Dbtc::execINDXKEYINFO(Signal* signal)
{
  jamEntry();
  Uint32 keyInfoLength = signal->getLength() - IndxKeyInfo::HeaderLength;
  IndxKeyInfo * const indxKeyInfo =  (IndxKeyInfo *)signal->getDataPtr();
  const Uint32 *src = indxKeyInfo->getData();
  const UintR TconnectIndex = indxKeyInfo->connectPtr;
  ApiConnectRecordPtr transPtr;
  transPtr.i = TconnectIndex;
  if (transPtr.i >= capiConnectFilesize) {
    jam();
11507
    warningHandlerLab(signal, __LINE__);
11508 11509 11510 11511 11512 11513 11514
    return;
  }//if
  ptrAss(transPtr, apiConnectRecord);
  ApiConnectRecord * const regApiPtr = transPtr.p;
  TcIndexOperationPtr indexOpPtr;
  TcIndexOperation* indexOp;

11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525
  if((indexOpPtr.i = regApiPtr->accumulatingIndexOp) != RNIL)
  {
    indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
    if (saveINDXKEYINFO(signal,
			indexOp, 
			src, 
			keyInfoLength)) {
      jam();
      // We have received all we need
      readIndexTable(signal, regApiPtr, indexOp);
    }
11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539
  }
}

void Dbtc::execINDXATTRINFO(Signal* signal)
{
  jamEntry();
  Uint32 attrInfoLength = signal->getLength() - IndxAttrInfo::HeaderLength;
  IndxAttrInfo * const indxAttrInfo =  (IndxAttrInfo *)signal->getDataPtr();
  const Uint32 *src = indxAttrInfo->getData();
  const UintR TconnectIndex = indxAttrInfo->connectPtr;
  ApiConnectRecordPtr transPtr;
  transPtr.i = TconnectIndex;
  if (transPtr.i >= capiConnectFilesize) {
    jam();
11540
    warningHandlerLab(signal, __LINE__);
11541 11542 11543 11544 11545 11546 11547
    return;
  }//if
  ptrAss(transPtr, apiConnectRecord);
  ApiConnectRecord * const regApiPtr = transPtr.p;  
  TcIndexOperationPtr indexOpPtr;
  TcIndexOperation* indexOp;

11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558
  if((indexOpPtr.i = regApiPtr->accumulatingIndexOp) != RNIL)
  {
    indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
    if (saveINDXATTRINFO(signal,
			 indexOp, 
			 src, 
			 attrInfoLength)) {
      jam();
      // We have received all we need
      readIndexTable(signal, regApiPtr, indexOp);
    }
11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582
  }
}

/**
 * Save signal INDXKEYINFO
 * Return true if we have received all needed data
 */
bool Dbtc::saveINDXKEYINFO(Signal* signal,
                           TcIndexOperation* indexOp,
                           const Uint32 *src, 
                           Uint32 len)
{
  if (!indexOp->keyInfo.append(src, len)) {
    jam();
    // Failed to seize keyInfo, abort transaction
#ifdef VM_TRACE
    ndbout_c("Dbtc::saveINDXKEYINFO: Failed to seize keyinfo\n");
#endif
    // Abort transaction
    apiConnectptr.i = indexOp->connectionIndex;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    releaseIndexOperation(apiConnectptr.p, indexOp);
    terrorCode = 4000;
    abortErrorLab(signal);
11583
    return false;
11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615
  }
  if (receivedAllINDXKEYINFO(indexOp) && receivedAllINDXATTRINFO(indexOp)) {
    jam();
    return true;
  }
  return false;
}

bool Dbtc::receivedAllINDXKEYINFO(TcIndexOperation* indexOp)
{
  return (indexOp->keyInfo.getSize() == indexOp->expectedKeyInfo);
}

/**
 * Save signal INDXATTRINFO
 * Return true if we have received all needed data
 */
bool Dbtc::saveINDXATTRINFO(Signal* signal,
                            TcIndexOperation* indexOp,
                            const Uint32 *src, 
                            Uint32 len)
{
  if (!indexOp->attrInfo.append(src, len)) {
    jam();
#ifdef VM_TRACE
    ndbout_c("Dbtc::saveINDXATTRINFO: Failed to seize attrInfo\n");
#endif
    apiConnectptr.i = indexOp->connectionIndex;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    releaseIndexOperation(apiConnectptr.p, indexOp);
    terrorCode = 4000;
    abortErrorLab(signal);
11616
    return false;
11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675
  }
  if (receivedAllINDXKEYINFO(indexOp) && receivedAllINDXATTRINFO(indexOp)) {
    jam();
    return true;
  }
  return false;
}

bool Dbtc::receivedAllINDXATTRINFO(TcIndexOperation* indexOp)
{
  return (indexOp->attrInfo.getSize() == indexOp->expectedAttrInfo);
}

bool  Dbtc::saveTRANSID_AI(Signal* signal,
			   TcIndexOperation* indexOp, 
                           const Uint32 *src,
                           Uint32 len)
{
  Uint32 currentTransIdAILength = indexOp->transIdAI.getSize();
    
  if (currentTransIdAILength == 0) {
    jam();
    // Read first AttributeHeader to get expected size
    // of the single key attribute expected
    AttributeHeader* head = (AttributeHeader *) src;
    indexOp->expectedTransIdAI = head->getHeaderSize() + head->getDataSize();
  }
  if (!indexOp->transIdAI.append(src, len)) {
    jam();
#ifdef VM_TRACE
    ndbout_c("Dbtc::saveTRANSID_AI: Failed to seize transIdAI\n");
#endif
    apiConnectptr.i = indexOp->connectionIndex;
    ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
    releaseIndexOperation(apiConnectptr.p, indexOp);
    terrorCode = 4000;
    abortErrorLab(signal);
    return false;
  }
  return true;
}

bool Dbtc::receivedAllTRANSID_AI(TcIndexOperation* indexOp)
{
  return (indexOp->transIdAI.getSize() == indexOp->expectedTransIdAI);
}

/**
 * Receive signal TCINDXCONF
 * This can be either the return of reading an index table
 * or performing an index operation
 */
void Dbtc::execTCKEYCONF(Signal* signal)
{
  TcKeyConf * const tcKeyConf =  (TcKeyConf *)signal->getDataPtr();
  TcIndexOperationPtr indexOpPtr;

  jamEntry();
  indexOpPtr.i = tcKeyConf->apiConnectPtr;
11676
  TcIndexOperation* indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691
  Uint32 confInfo = tcKeyConf->confInfo;

  /**
   * Check on TCKEYCONF wheater the the transaction was committed
   */
  Uint32 Tcommit = TcKeyConf::getCommitFlag(confInfo);

  indexOpPtr.p = indexOp;
  if (!indexOp) {
    jam();
    // Missing index operation
    return;
  }
  const UintR TconnectIndex = indexOp->connectionIndex;
  ApiConnectRecord * const regApiPtr = &apiConnectRecord[TconnectIndex];
unknown's avatar
unknown committed
11692 11693
  apiConnectptr.p = regApiPtr;
  apiConnectptr.i = TconnectIndex;
11694 11695 11696 11697
  switch(indexOp->indexOpState) {
  case(IOS_NOOP): {
    jam();
    // Should never happen, abort
11698
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11699

unknown's avatar
unknown committed
11700
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
11701 11702 11703 11704
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;    
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11705
	       TcKeyRef::SignalLength, JBB);
11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716
    return;
  }
  case(IOS_INDEX_ACCESS): {
    jam();
    // Wait for TRANSID_AI
    indexOp->indexOpState = IOS_INDEX_ACCESS_WAIT_FOR_TRANSID_AI;
    break;
  }
  case(IOS_INDEX_ACCESS_WAIT_FOR_TRANSID_AI): {
    jam();
    // Double TCKEYCONF, should never happen, abort
11717
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11718

unknown's avatar
unknown committed
11719
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
11720 11721 11722 11723
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;    
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11724
	       TcKeyRef::SignalLength, JBB);
11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738
    return;
  }
  case(IOS_INDEX_ACCESS_WAIT_FOR_TCKEYCONF): {  
    jam();
    // Continue with index operation
    executeIndexOperation(signal, regApiPtr, indexOp);
    break;
  }
  case(IOS_INDEX_OPERATION): {
    // We are done, send TCINDXCONF
    jam();    
    Uint32 Ttcindxrec = regApiPtr->tcindxrec;
    // Copy reply from TcKeyConf

unknown's avatar
unknown committed
11739
    ndbassert(regApiPtr->noIndexOp);
11740
    regApiPtr->noIndexOp--; // Decrease count
unknown's avatar
unknown committed
11741
    regApiPtr->tcIndxSendArray[Ttcindxrec] = indexOp->tcIndxReq.senderData;
11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764
    regApiPtr->tcIndxSendArray[Ttcindxrec + 1] = 
      tcKeyConf->operations[0].attrInfoLen;
    regApiPtr->tcindxrec = Ttcindxrec + 2;
    if (regApiPtr->noIndexOp == 0) {
      jam();
      sendTcIndxConf(signal, Tcommit);
    } else if (regApiPtr->tcindxrec == ZTCOPCONF_SIZE) {
      jam();
      sendTcIndxConf(signal, 0);
    }
    releaseIndexOperation(regApiPtr, indexOp);
    break;
  }
  }
}

void Dbtc::execTCKEYREF(Signal* signal)
{
  TcKeyRef * const tcKeyRef = (TcKeyRef *)signal->getDataPtr();
  TcIndexOperationPtr indexOpPtr;

  jamEntry();
  indexOpPtr.i = tcKeyRef->connectPtr;
11765
  TcIndexOperation* indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
11766 11767 11768 11769 11770 11771 11772 11773
  indexOpPtr.p = indexOp;
  if (!indexOp) {
    jam();    
    // Missing index operation
    return;
  }
  const UintR TconnectIndex = indexOp->connectionIndex;
  ApiConnectRecord * const regApiPtr = &apiConnectRecord[TconnectIndex];
unknown's avatar
unknown committed
11774
  Uint32 tcKeyRequestInfo  = indexOp->tcIndxReq.requestInfo;
11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797
  Uint32 commitFlg = TcKeyReq::getCommitFlag(tcKeyRequestInfo);

  switch(indexOp->indexOpState) {
  case(IOS_NOOP): {
    jam();    
    // Should never happen, abort
    break;
  }
  case(IOS_INDEX_ACCESS):
  case(IOS_INDEX_ACCESS_WAIT_FOR_TRANSID_AI):
  case(IOS_INDEX_ACCESS_WAIT_FOR_TCKEYCONF): {
    jam();    
    // If we fail index access for a non-read operation during commit 
    // we abort transaction
    if (commitFlg == 1) {
      jam();
      releaseIndexOperation(regApiPtr, indexOp);
      apiConnectptr.i = indexOp->connectionIndex;
      ptrCheckGuard(apiConnectptr, capiConnectFilesize, apiConnectRecord);
      terrorCode = tcKeyRef->errorCode;
      abortErrorLab(signal);
      break;
    }
unknown's avatar
unknown committed
11798 11799 11800 11801 11802 11803
    /**
     * Increase count as it will be decreased below...
     *   (and the code is written to handle failing lookup on "real" table
     *    not lookup on index table)
     */
    regApiPtr->noIndexOp++;
11804 11805 11806 11807 11808 11809
    // else continue
  }
  case(IOS_INDEX_OPERATION): {
    // Send TCINDXREF 
    
    jam();
unknown's avatar
unknown committed
11810
    TcKeyReq * const tcIndxReq = &indexOp->tcIndxReq;
11811
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11812
    
unknown's avatar
unknown committed
11813
    ndbassert(regApiPtr->noIndexOp);
11814 11815 11816 11817 11818 11819
    regApiPtr->noIndexOp--; // Decrease count
    tcIndxRef->connectPtr = tcIndxReq->senderData;
    tcIndxRef->transId[0] = tcKeyRef->transId[0];
    tcIndxRef->transId[1] = tcKeyRef->transId[1];
    tcIndxRef->errorCode = tcKeyRef->errorCode;
    sendSignal(regApiPtr->ndbapiBlockref, 
11820
               GSN_TCINDXREF, signal, TcKeyRef::SignalLength, JBB);
11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865
    return;
  }
  }
}

void Dbtc::execTRANSID_AI_R(Signal* signal){
  TransIdAI * const transIdAI =  (TransIdAI *)signal->getDataPtr();
  Uint32 sigLen = signal->length();
  Uint32 dataLen = sigLen - TransIdAI::HeaderLength - 1;
  Uint32 recBlockref = transIdAI->attrData[dataLen];

  jamEntry();

  /**
   * Forward signal to final destination
   * Truncate last word since that was used to hold the final dest.
   */
  sendSignal(recBlockref, GSN_TRANSID_AI,
	     signal, sigLen - 1, JBB);
}

void Dbtc::execKEYINFO20_R(Signal* signal){
  KeyInfo20 * const keyInfo =  (KeyInfo20 *)signal->getDataPtr();
  Uint32 sigLen = signal->length();
  Uint32 dataLen = sigLen - KeyInfo20::HeaderLength - 1;
  Uint32 recBlockref = keyInfo->keyData[dataLen];

  jamEntry();
  
  /**
   * Forward signal to final destination
   * Truncate last word since that was used to hold the final dest.
   */
  sendSignal(recBlockref, GSN_KEYINFO20,
	     signal, sigLen - 1, JBB);
}


void Dbtc::execTRANSID_AI(Signal* signal)
{
  TransIdAI * const transIdAI =  (TransIdAI *)signal->getDataPtr();

  jamEntry();
  TcIndexOperationPtr indexOpPtr;
  indexOpPtr.i = transIdAI->connectPtr;
11866
  TcIndexOperation* indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886
  indexOpPtr.p = indexOp;
  if (!indexOp) {
    jam();
    // Missing index operation
  }
  const UintR TconnectIndex = indexOp->connectionIndex;
  //  ApiConnectRecord * const regApiPtr = &apiConnectRecord[TconnectIndex];
  ApiConnectRecordPtr transPtr;
  
  transPtr.i = TconnectIndex;
  ptrCheckGuard(transPtr, capiConnectFilesize, apiConnectRecord);
  ApiConnectRecord * const regApiPtr = transPtr.p;

  // Acccumulate attribute data
  if (!saveTRANSID_AI(signal,
		      indexOp, 
                      transIdAI->getData(), 
                      signal->getLength() - TransIdAI::HeaderLength)) {
    jam();
    // Failed to allocate space for TransIdAI
11887
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11888
    
unknown's avatar
unknown committed
11889
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
11890 11891 11892 11893
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4000;
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11894
	       TcKeyRef::SignalLength, JBB);
11895 11896 11897 11898 11899 11900 11901
    return;
  }

  switch(indexOp->indexOpState) {
  case(IOS_NOOP): {
    jam();
    // Should never happen, abort
11902
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11903
    
unknown's avatar
unknown committed
11904
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
11905 11906 11907 11908
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11909
	       TcKeyRef::SignalLength, JBB);
11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929
    return;
    break;
  }
  case(IOS_INDEX_ACCESS): {
    jam();
    // Check if all TRANSID_AI have been received
    if (receivedAllTRANSID_AI(indexOp)) {
      jam();
      // Wait for TRANSID_AI
      indexOp->indexOpState = IOS_INDEX_ACCESS_WAIT_FOR_TCKEYCONF;
    }
    break;
    }
  case(IOS_INDEX_ACCESS_WAIT_FOR_TCKEYCONF): {
    jam();
#ifdef VM_TRACE
    ndbout_c("Dbtc::execTRANSID_AI: Too many TRANSID_AI, ignore for now\n");
#endif
    /*
    // Too many TRANSID_AI
11930
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11931
    
unknown's avatar
unknown committed
11932
    tcIndexRef->connectPtr = indexOp->tcIndxReq.senderData;
11933 11934 11935 11936
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11937
               TcKeyRef::SignalLength, JBB);
11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954
    */
    break;
  }
  case(IOS_INDEX_ACCESS_WAIT_FOR_TRANSID_AI): { 
    jam();
    // Check if all TRANSID_AI have been received
    if (receivedAllTRANSID_AI(indexOp)) {
      jam();
      // Continue with index operation
      executeIndexOperation(signal, regApiPtr, indexOp);
    }
    // else continue waiting for more TRANSID_AI
    break;
  }
  case(IOS_INDEX_OPERATION): {
    // Should never receive TRANSID_AI in this state!!
    jam();    
11955
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
11956

unknown's avatar
unknown committed
11957
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
11958 11959 11960 11961
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
11962
	       TcKeyRef::SignalLength, JBB);
11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973
    return;
  }
  }
}

void Dbtc::execTCROLLBACKREP(Signal* signal)
{
  TcRollbackRep* tcRollbackRep =  (TcRollbackRep *)signal->getDataPtr();
  jamEntry();
  TcIndexOperationPtr indexOpPtr;
  indexOpPtr.i = tcRollbackRep->connectPtr;
11974
  TcIndexOperation* indexOp = c_theIndexOperationPool.getPtr(indexOpPtr.i);
11975 11976
  indexOpPtr.p = indexOp;
  tcRollbackRep =  (TcRollbackRep *)signal->getDataPtrSend();
unknown's avatar
unknown committed
11977
  tcRollbackRep->connectPtr = indexOp->tcIndxReq.senderData;
11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993
  sendSignal(apiConnectptr.p->ndbapiBlockref, 
	     GSN_TCROLLBACKREP, signal, TcRollbackRep::SignalLength, JBB);
}

/**
 * Read index table with the index attributes as PK
 */
void Dbtc::readIndexTable(Signal* signal, 
			  ApiConnectRecord* regApiPtr,
			  TcIndexOperation* indexOp) 
{
  Uint32 keyBufSize = 8; // Maximum for key in TCKEYREQ
  Uint32 dataPos = 0;
  TcKeyReq * const tcKeyReq = (TcKeyReq *)signal->getDataPtrSend();
  Uint32 * dataPtr = &tcKeyReq->scanInfo;
  Uint32 tcKeyLength = TcKeyReq::StaticLength;
unknown's avatar
unknown committed
11994
  Uint32 tcKeyRequestInfo = indexOp->tcIndxReq.requestInfo; 
11995 11996 11997
  AttributeBuffer::DataBufferIterator keyIter;
  Uint32 keyLength = TcKeyReq::getKeyLength(tcKeyRequestInfo);
  TcIndexData* indexData;
unknown's avatar
unknown committed
11998 11999
  Uint32 transId1 = indexOp->tcIndxReq.transId1;
  Uint32 transId2 = indexOp->tcIndxReq.transId2;
12000

unknown's avatar
unknown committed
12001 12002
  const Operation_t opType = 
    (Operation_t)TcKeyReq::getOperationType(tcKeyRequestInfo);
12003 12004

  // Find index table
unknown's avatar
unknown committed
12005
  if ((indexData = c_theIndexes.getPtr(indexOp->tcIndxReq.tableId)) == NULL) {
12006 12007
    jam();
    // Failed to find index record
12008
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
12009

unknown's avatar
unknown committed
12010
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
12011 12012 12013 12014
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4000;    
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
12015
	       TcKeyRef::SignalLength, JBB);
12016 12017 12018 12019 12020 12021
    return;
  }
  tcKeyReq->transId1 = transId1;
  tcKeyReq->transId2 = transId2;
  tcKeyReq->tableId = indexData->indexId;
  tcKeyLength += MIN(keyLength, keyBufSize);
unknown's avatar
unknown committed
12022
  tcKeyReq->tableSchemaVersion = indexOp->tcIndxReq.tableSchemaVersion;
12023
  TcKeyReq::setOperationType(tcKeyRequestInfo, 
unknown's avatar
unknown committed
12024
			     opType == ZREAD ? ZREAD : ZREAD_EX);
12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048
  TcKeyReq::setAIInTcKeyReq(tcKeyRequestInfo, 1); // Allways send one AttrInfo
  TcKeyReq::setExecutingTrigger(tcKeyRequestInfo, 0);
  BlockReference originalReceiver = regApiPtr->ndbapiBlockref;
  regApiPtr->ndbapiBlockref = reference(); // Send result to me
  tcKeyReq->senderData = indexOp->indexOpId;
  indexOp->indexOpState = IOS_INDEX_ACCESS;
  regApiPtr->executingIndexOp = regApiPtr->accumulatingIndexOp;
  regApiPtr->accumulatingIndexOp = RNIL;
  regApiPtr->isIndexOp = true;

  Uint32 remainingKey = indexOp->keyInfo.getSize();
  bool moreKeyData = indexOp->keyInfo.first(keyIter);
  // *********** KEYINFO in TCKEYREQ ***********
  while((dataPos < keyBufSize) &&
        (remainingKey-- != 0)) {
    *dataPtr++ = *keyIter.data;
    dataPos++;
    moreKeyData = indexOp->keyInfo.next(keyIter);
  }
  // *********** ATTRINFO in TCKEYREQ ***********  
  tcKeyReq->attrLen = 1; // Primary key is stored as one attribute
  AttributeHeader::init(dataPtr, indexData->primaryKeyPos, 0);
  tcKeyLength++;
  tcKeyReq->requestInfo = tcKeyRequestInfo;
unknown's avatar
unknown committed
12049 12050 12051

  ndbassert(TcKeyReq::getDirtyFlag(tcKeyRequestInfo) == 0);
  ndbassert(TcKeyReq::getSimpleFlag(tcKeyRequestInfo) == 0);
12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073
  EXECUTE_DIRECT(DBTC, GSN_TCKEYREQ, signal, tcKeyLength);
 
  /**
   * "Fool" TC not to start commiting transaction since it always will
   *   have one outstanding lqhkeyreq
   * This is later decreased when the index read is complete
   */ 
  regApiPtr->lqhkeyreqrec++;

  /**
   * Remember ptr to index read operation
   *   (used to set correct save point id on index operation later)
   */
  indexOp->indexReadTcConnect = regApiPtr->lastTcConnect;

  jamEntry();
  // *********** KEYINFO ***********
  if (moreKeyData) {
    jam();
    // Send KEYINFO sequence
    KeyInfo * const keyInfo =  (KeyInfo *)signal->getDataPtrSend();
    
unknown's avatar
unknown committed
12074
    keyInfo->connectPtr = indexOp->tcIndxReq.apiConnectPtr;
12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113
    keyInfo->transId[0] = transId1;
    keyInfo->transId[1] = transId2;
    dataPtr = (Uint32 *) &keyInfo->keyData;
    dataPos = 0;
    while(remainingKey-- != 0) {// If we have not read complete key
      *dataPtr++ = *keyIter.data;
      dataPos++;
      if (dataPos == KeyInfo::DataLength) {
	// Flush KEYINFO
	EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal, 
		       KeyInfo::HeaderLength + KeyInfo::DataLength);
        jamEntry();
	dataPos = 0;
	dataPtr = (Uint32 *) &keyInfo->keyData;
      }       
      moreKeyData = indexOp->keyInfo.next(keyIter);
    }
    if (dataPos != 0) {
      // Flush last KEYINFO
      EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		     KeyInfo::HeaderLength + dataPos);
      jamEntry();
    }
  }
  
  regApiPtr->ndbapiBlockref = originalReceiver; // reset original receiver
}

/**
 * Execute the index operation with the result from
 * the index table read as PK
 */
void Dbtc::executeIndexOperation(Signal* signal, 
				 ApiConnectRecord* regApiPtr,
				 TcIndexOperation* indexOp) {
  
  Uint32 keyBufSize = 8; // Maximum for key in TCKEYREQ
  Uint32 attrBufSize = 5;
  Uint32 dataPos = 0;
unknown's avatar
unknown committed
12114
  TcKeyReq * const tcIndxReq = &indexOp->tcIndxReq;
12115 12116 12117 12118 12119 12120 12121 12122 12123 12124
  TcKeyReq * const tcKeyReq = (TcKeyReq *)signal->getDataPtrSend();
  Uint32 * dataPtr = &tcKeyReq->scanInfo;
  Uint32 tcKeyLength = TcKeyReq::StaticLength;
  Uint32 tcKeyRequestInfo = tcIndxReq->requestInfo;
  TcIndexData* indexData;
  AttributeBuffer::DataBufferIterator attrIter;
  AttributeBuffer::DataBufferIterator aiIter;
  bool moreKeyData = indexOp->transIdAI.first(aiIter);
      
  // Find index table
unknown's avatar
unknown committed
12125
  if ((indexData = c_theIndexes.getPtr(tcIndxReq->tableId)) == NULL) {
12126 12127
    jam();
    // Failed to find index record 
12128
    TcKeyRef * const tcIndxRef = (TcKeyRef *)signal->getDataPtrSend();
12129

unknown's avatar
unknown committed
12130
    tcIndxRef->connectPtr = indexOp->tcIndxReq.senderData;
12131 12132 12133 12134
    tcIndxRef->transId[0] = regApiPtr->transid[0];
    tcIndxRef->transId[1] = regApiPtr->transid[1];
    tcIndxRef->errorCode = 4349;    
    sendSignal(regApiPtr->ndbapiBlockref, GSN_TCINDXREF, signal, 
12135
	       TcKeyRef::SignalLength, JBB);
12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202
    return;
  }    
  // Find schema version of primary table
  TableRecordPtr tabPtr;
  tabPtr.i = indexData->primaryTableId;
  ptrCheckGuard(tabPtr, ctabrecFilesize, tableRecord);

  tcKeyReq->apiConnectPtr = tcIndxReq->apiConnectPtr;
  tcKeyReq->attrLen = tcIndxReq->attrLen;
  tcKeyReq->tableId = indexData->primaryTableId;
  tcKeyReq->tableSchemaVersion = tabPtr.p->currentSchemaVersion;
  tcKeyReq->transId1 = regApiPtr->transid[0];
  tcKeyReq->transId2 = regApiPtr->transid[1];
  tcKeyReq->senderData = tcIndxReq->senderData; // Needed for TRANSID_AI to API
  indexOp->indexOpState = IOS_INDEX_OPERATION;
  regApiPtr->isIndexOp = true;
  regApiPtr->executingIndexOp = indexOp->indexOpId;;
  regApiPtr->noIndexOp++; // Increase count

  // Filter out AttributeHeader:s since this should not be in key
  AttributeHeader* attrHeader = (AttributeHeader *) aiIter.data;
    
  Uint32 headerSize = attrHeader->getHeaderSize();
  Uint32 keySize = attrHeader->getDataSize();
  TcKeyReq::setKeyLength(tcKeyRequestInfo, keySize);
  // Skip header
  if (headerSize == 1) {
    jam();
    moreKeyData = indexOp->transIdAI.next(aiIter);
  } else {
    jam();
    moreKeyData = indexOp->transIdAI.next(aiIter, headerSize - 1);
  }//if
  while(// If we have not read complete key
	(keySize != 0) &&
	(dataPos < keyBufSize)) {
    *dataPtr++ = *aiIter.data;
    dataPos++;
    keySize--;
    moreKeyData = indexOp->transIdAI.next(aiIter);
  }
  tcKeyLength += dataPos;

  Uint32 attributesLength = indexOp->attrInfo.getSize();
  if (attributesLength <= attrBufSize) {
    jam();
    // ATTRINFO fits in TCKEYREQ
    // Pack ATTRINFO IN TCKEYREQ
    TcKeyReq::setAIInTcKeyReq(tcKeyRequestInfo, indexOp->attrInfo.getSize());
    // Insert IndxAttrInfo
    for(bool moreAttrData = indexOp->attrInfo.first(attrIter);
        moreAttrData;
        moreAttrData = indexOp->attrInfo.next(attrIter)) {      
      *dataPtr++ = *attrIter.data;
    }
    tcKeyLength += attributesLength;
  } else {
    jam();
    // No ATTRINFO in TCKEYREQ
    TcKeyReq::setAIInTcKeyReq(tcKeyRequestInfo, 0);
  }

  TcKeyReq::setCommitFlag(tcKeyRequestInfo, 0);
  TcKeyReq::setExecuteFlag(tcKeyRequestInfo, 0);
  TcKeyReq::setExecutingTrigger(tcKeyRequestInfo, 0);
  tcKeyReq->requestInfo = tcKeyRequestInfo;

unknown's avatar
unknown committed
12203 12204 12205
  ndbassert(TcKeyReq::getDirtyFlag(tcKeyRequestInfo) == 0);
  ndbassert(TcKeyReq::getSimpleFlag(tcKeyRequestInfo) == 0);

12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232
  /**
   * Decrease lqhkeyreqrec to compensate for addition
   *   during read of index table
   * I.e. let TC start committing when other operations has completed
   */
  regApiPtr->lqhkeyreqrec--;

  /**
   * Fix savepoint id -
   *   fix so that index operation has the same savepoint id
   *   as the read of the index table (TCINDXREQ)
   */
  TcConnectRecordPtr tmp;
  tmp.i = indexOp->indexReadTcConnect;
  ptrCheckGuard(tmp, ctcConnectFilesize, tcConnectRecord);
  const Uint32 currSavePointId = regApiPtr->currSavePointId;
  regApiPtr->currSavePointId = tmp.p->savePointId;
  EXECUTE_DIRECT(DBTC, GSN_TCKEYREQ, signal, tcKeyLength);
  regApiPtr->currSavePointId = currSavePointId;
  
  jamEntry();
  // *********** KEYINFO ***********
  if (moreKeyData) {
    jam();
    // Send KEYINFO sequence
    KeyInfo * const keyInfo =  (KeyInfo *)signal->getDataPtrSend();
    
unknown's avatar
unknown committed
12233
    keyInfo->connectPtr = indexOp->tcIndxReq.apiConnectPtr;
12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268
    keyInfo->transId[0] = regApiPtr->transid[0];
    keyInfo->transId[1] = regApiPtr->transid[1];
    dataPtr = (Uint32 *) &keyInfo->keyData;
    dataPos = 0;
    // Pack any part of a key attribute that did no fit TCKEYREQ
    while(keySize-- != 0) {// If we have not read complete key
      *dataPtr++ = *aiIter.data;
      dataPos++;
      if (dataPos == KeyInfo::DataLength) {
	// Flush KEYINFO
	EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal, 
		       KeyInfo::HeaderLength + KeyInfo::DataLength);
        jamEntry();
	dataPos = 0;
	dataPtr = (Uint32 *) &keyInfo->keyData;
      }
      moreKeyData = indexOp->transIdAI.next(aiIter);
    }
    if (dataPos != 0) {
      // Flush last KEYINFO
      EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		     KeyInfo::HeaderLength + dataPos);
      jamEntry();
    }
  }
  
  // *********** ATTRINFO ***********
  if (attributesLength > attrBufSize) {
    jam();
    // No ATTRINFO in TcKeyReq
    TcKeyReq::setAIInTcKeyReq(tcKeyReq->requestInfo, 0);
    // Send ATTRINFO sequence
    AttrInfo * const attrInfo =  (AttrInfo *)signal->getDataPtrSend();
    Uint32 attrInfoPos = 0;
    
unknown's avatar
unknown committed
12269
    attrInfo->connectPtr = indexOp->tcIndxReq.apiConnectPtr;
12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301
    attrInfo->transId[0] = regApiPtr->transid[0];
    attrInfo->transId[1] = regApiPtr->transid[1];
    dataPtr = (Uint32 *) &attrInfo->attrData;

 
    // Insert attribute values (insert key values of primary table)
    for(bool moreAttrData = indexOp->attrInfo.first(attrIter);
        moreAttrData;
        moreAttrData = indexOp->attrInfo.next(attrIter)) {      
      *dataPtr++ = *attrIter.data;
      attrInfoPos++;
      if (attrInfoPos == AttrInfo::DataLength) {
        // Flush ATTRINFO
        EXECUTE_DIRECT(DBTC, GSN_ATTRINFO, signal,
		       AttrInfo::HeaderLength + AttrInfo::DataLength);
        jamEntry();
        attrInfoPos = 0;
	dataPtr = (Uint32 *) &attrInfo->attrData;
      }
    }
    if (attrInfoPos != 0) {
      // Send last ATTRINFO
      EXECUTE_DIRECT(DBTC, GSN_ATTRINFO, signal, 
		     AttrInfo::HeaderLength + attrInfoPos);
      jamEntry();
    }
  }
}

bool Dbtc::seizeIndexOperation(ApiConnectRecord* regApiPtr,
			       TcIndexOperationPtr& indexOpPtr)
{
12302
  return regApiPtr->theSeizedIndexOperations.seize(indexOpPtr);
12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319
}

void Dbtc::releaseIndexOperation(ApiConnectRecord* regApiPtr,
				 TcIndexOperation* indexOp)
{
  indexOp->indexOpState = IOS_NOOP;
  indexOp->expectedKeyInfo = 0;
  indexOp->keyInfo.release();
  indexOp->expectedAttrInfo = 0;
  indexOp->attrInfo.release();
  indexOp->expectedTransIdAI = 0;
  indexOp->transIdAI.release();
  regApiPtr->theSeizedIndexOperations.release(indexOp->indexOpId);
}

void Dbtc::releaseAllSeizedIndexOperations(ApiConnectRecord* regApiPtr)
{
12320
  TcIndexOperationPtr seizedIndexOpPtr;
12321 12322 12323 12324

  regApiPtr->theSeizedIndexOperations.first(seizedIndexOpPtr);
  while(seizedIndexOpPtr.i != RNIL) {
    jam();
12325
    TcIndexOperation* indexOp = seizedIndexOpPtr.p;
12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346

    indexOp->indexOpState = IOS_NOOP;
    indexOp->expectedKeyInfo = 0;
    indexOp->keyInfo.release();
    indexOp->expectedAttrInfo = 0;
    indexOp->attrInfo.release();
    indexOp->expectedTransIdAI = 0;
    indexOp->transIdAI.release();
    regApiPtr->theSeizedIndexOperations.next(seizedIndexOpPtr);    
  }
  regApiPtr->theSeizedIndexOperations.release();
}

void Dbtc::saveTriggeringOpState(Signal* signal, TcConnectRecord* trigOp)
{
  LqhKeyConf * lqhKeyConf = (LqhKeyConf *)signal->getDataPtr();
  copyFromToLen((UintR*)lqhKeyConf,
		&trigOp->savedState[0],
                LqhKeyConf::SignalLength);  
}

12347
void Dbtc::continueTriggeringOp(Signal* signal, TcConnectRecord* trigOp)
12348 12349 12350 12351 12352 12353
{
  LqhKeyConf * lqhKeyConf = (LqhKeyConf *)signal->getDataPtr();
  copyFromToLen(&trigOp->savedState[0],
                (UintR*)lqhKeyConf,
		LqhKeyConf::SignalLength);

12354
  lqhKeyConf->noFiredTriggers = 0;
12355
  trigOp->noReceivedTriggers = 0;
12356 12357 12358

  // All triggers executed successfully, continue operation
  execLQHKEYCONF(signal);
12359 12360 12361 12362 12363 12364 12365
}

void Dbtc::scheduleFiredTrigger(ApiConnectRecordPtr* transPtr,
                                TcConnectRecordPtr* opPtr)
{
  // Set initial values for trigger fireing operation
  opPtr->p->triggerExecutionCount++;
12366

12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390
  // Insert fired trigger in execution queue
  transPtr->p->theFiredTriggers.add(opPtr->p->accumulatingTriggerData);
  opPtr->p->accumulatingTriggerData.i = RNIL;
  opPtr->p->accumulatingTriggerData.p = NULL;
}

void Dbtc::executeTriggers(Signal* signal, ApiConnectRecordPtr* transPtr)
{
  ApiConnectRecord* regApiPtr = transPtr->p;
  TcConnectRecord *localTcConnectRecord = tcConnectRecord;
  TcConnectRecordPtr opPtr;
  FiredTriggerPtr trigPtr;

  if (!regApiPtr->theFiredTriggers.isEmpty()) {
    jam();
    if ((regApiPtr->apiConnectstate == CS_STARTED) ||
        (regApiPtr->apiConnectstate == CS_START_COMMITTING)) {
      jam();
      regApiPtr->theFiredTriggers.first(trigPtr);
      while (trigPtr.i != RNIL) {
        jam();
        // Execute all ready triggers in parallel
        opPtr.i = trigPtr.p->fireingOperation;
        ptrCheckGuard(opPtr, ctcConnectFilesize, localTcConnectRecord);
12391
	FiredTriggerPtr nextTrigPtr = trigPtr;
12392 12393 12394 12395 12396 12397 12398 12399
	regApiPtr->theFiredTriggers.next(nextTrigPtr);
        if (opPtr.p->noReceivedTriggers == opPtr.p->noFiredTriggers) {
          jam();
          // Fireing operation is ready to have a trigger executing
          executeTrigger(signal, trigPtr.p, transPtr, &opPtr);
          // Should allow for interleaving here by sending a CONTINUEB and 
	  // return
          // Release trigger records
12400 12401 12402 12403 12404 12405 12406
	  AttributeBuffer::DataBufferPool & pool = c_theAttributeBufferPool;
	  LocalDataBuffer<11> tmp1(pool, trigPtr.p->keyValues);
	  tmp1.release();
	  LocalDataBuffer<11> tmp2(pool, trigPtr.p->beforeValues);
	  tmp2.release();
	  LocalDataBuffer<11> tmp3(pool, trigPtr.p->afterValues);
	  tmp3.release();
12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493
          regApiPtr->theFiredTriggers.release(trigPtr.i);
        }
	trigPtr = nextTrigPtr;
      }
      return;
    // No more triggers, continue transaction after last executed trigger has
    // reurned (in execLQHKEYCONF or execLQHKEYREF)
    } else {
      // Wait until transaction is ready to execute a trigger
      jam();
      if (!regApiPtr->triggerPending) {
        jam();
        regApiPtr->triggerPending = true;
        signal->theData[0] = TcContinueB::TRIGGER_PENDING;
        signal->theData[1] = transPtr->i;
        sendSignal(reference(), GSN_CONTINUEB, signal, 3, JBB);
      }
      // else  
      // We are already waiting for a pending trigger (CONTINUEB)
    }
  }
}

void Dbtc::executeTrigger(Signal* signal,
                          TcFiredTriggerData* firedTriggerData,
                          ApiConnectRecordPtr* transPtr,
                          TcConnectRecordPtr* opPtr)
{
  TcDefinedTriggerData* definedTriggerData;

  if ((definedTriggerData = 
       c_theDefinedTriggers.getPtr(firedTriggerData->triggerId)) 
      != NULL) {
    switch(definedTriggerData->triggerType) {
    case(TriggerType::SECONDARY_INDEX):
      jam();
      executeIndexTrigger(signal, definedTriggerData, firedTriggerData, 
                          transPtr, opPtr);
      break;
    default:
      ndbrequire(false);
    }
  }
}

void Dbtc::executeIndexTrigger(Signal* signal,
                               TcDefinedTriggerData* definedTriggerData,
                               TcFiredTriggerData* firedTriggerData,
                               ApiConnectRecordPtr* transPtr,
                               TcConnectRecordPtr* opPtr)
{
  TcIndexData* indexData;

  indexData = c_theIndexes.getPtr(definedTriggerData->indexId);
  ndbassert(indexData != NULL);

  switch (definedTriggerData->triggerEvent) {
  case(TriggerEvent::TE_INSERT): {
    jam();
    insertIntoIndexTable(signal, firedTriggerData, transPtr, opPtr, indexData);
    break;
  }
  case(TriggerEvent::TE_DELETE): {
    jam();
    deleteFromIndexTable(signal, firedTriggerData, transPtr, opPtr, indexData);
    break;
  }
  case(TriggerEvent::TE_UPDATE): {
    jam();
    deleteFromIndexTable(signal, firedTriggerData, transPtr, opPtr, 
			 indexData, true); // Hold the triggering operation
    insertIntoIndexTable(signal, firedTriggerData, transPtr, opPtr, indexData);
    break;
  }
  default:
    ndbrequire(false);
  }
}

void Dbtc::releaseFiredTriggerData(DLFifoList<TcFiredTriggerData>* triggers)
{
  FiredTriggerPtr trigPtr;

  triggers->first(trigPtr);
  while (trigPtr.i != RNIL) {
    jam();
    // Release trigger records
12494 12495 12496 12497 12498 12499 12500 12501 12502

    AttributeBuffer::DataBufferPool & pool = c_theAttributeBufferPool;
    LocalDataBuffer<11> tmp1(pool, trigPtr.p->keyValues);
    tmp1.release();
    LocalDataBuffer<11> tmp2(pool, trigPtr.p->beforeValues);
    tmp2.release();
    LocalDataBuffer<11> tmp3(pool, trigPtr.p->afterValues);
    tmp3.release();
    
12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535
    triggers->next(trigPtr);
  }
  triggers->release();
}

void Dbtc::insertIntoIndexTable(Signal* signal, 
                                TcFiredTriggerData* firedTriggerData, 
                                ApiConnectRecordPtr* transPtr,
                                TcConnectRecordPtr* opPtr,
                                TcIndexData* indexData,
                                bool holdOperation)
{
  ApiConnectRecord* regApiPtr = transPtr->p;
  TcConnectRecord* opRecord = opPtr->p;
  TcKeyReq * const tcKeyReq =  (TcKeyReq *)signal->getDataPtrSend();
  Uint32 tcKeyRequestInfo = 0;
  Uint32 tcKeyLength = TcKeyReq::StaticLength;
  TableRecordPtr indexTabPtr;
  AttributeBuffer::DataBufferIterator iter;
  Uint32 attrId = 0;
  Uint32 keyLength = 0;
  Uint32 totalPrimaryKeyLength = 0;
  Uint32 hops;

  indexTabPtr.i = indexData->indexId;
  ptrCheckGuard(indexTabPtr, ctabrecFilesize, tableRecord);
  tcKeyReq->apiConnectPtr = transPtr->i;
  tcKeyReq->senderData = opPtr->i;
  if (holdOperation) {
    jam();
    opRecord->triggerExecutionCount++;
  }//if
  // Calculate key length and renumber attribute id:s
12536 12537
  AttributeBuffer::DataBufferPool & pool = c_theAttributeBufferPool;
  LocalDataBuffer<11> afterValues(pool, firedTriggerData->afterValues);
12538
  bool skipNull = false;
12539
  for(bool moreKeyAttrs = afterValues.first(iter); moreKeyAttrs; attrId++) {
12540 12541 12542
    jam();
    AttributeHeader* attrHeader = (AttributeHeader *) iter.data;

12543 12544 12545 12546 12547
    // Filter out NULL valued attributes
    if (attrHeader->isNULL()) {
      skipNull = true;
      break;
    }
12548 12549 12550
    attrHeader->setAttributeId(attrId);      
    keyLength += attrHeader->getDataSize();
    hops = attrHeader->getHeaderSize() + attrHeader->getDataSize();
12551
    moreKeyAttrs = afterValues.next(iter, hops);
12552
  }
12553
  if (skipNull) {
12554
    jam();
12555 12556 12557 12558 12559 12560 12561 12562
    opRecord->triggerExecutionCount--;
    if (opRecord->triggerExecutionCount == 0) {
      /*
	We have completed current trigger execution
	Continue triggering operation
      */
      jam();
      continueTriggeringOp(signal, opRecord);	
12563
    }//if
12564
    return;
12565 12566 12567
  }//if

  // Calculate total length of primary key to be stored in index table
12568 12569
  LocalDataBuffer<11> keyValues(pool, firedTriggerData->keyValues);
  for(bool moreAttrData = keyValues.first(iter); moreAttrData; ) {
12570 12571 12572 12573 12574
    jam();
    AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
    
    totalPrimaryKeyLength += attrHeader->getDataSize();
    hops = attrHeader->getHeaderSize() + attrHeader->getDataSize();
12575
    moreAttrData = keyValues.next(iter, hops);
12576 12577
  }
  AttributeHeader pkAttrHeader(attrId, totalPrimaryKeyLength);
12578
  
12579
  TcKeyReq::setKeyLength(tcKeyRequestInfo, keyLength);
12580
  tcKeyReq->attrLen = afterValues.getSize() + 
12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593
    pkAttrHeader.getHeaderSize() + pkAttrHeader.getDataSize();
  tcKeyReq->tableId = indexData->indexId;
  TcKeyReq::setOperationType(tcKeyRequestInfo, ZINSERT);
  TcKeyReq::setExecutingTrigger(tcKeyRequestInfo, true);
  tcKeyReq->tableSchemaVersion = indexTabPtr.p->currentSchemaVersion;
  tcKeyReq->transId1 = regApiPtr->transid[0];
  tcKeyReq->transId2 = regApiPtr->transid[1];
  Uint32 * dataPtr = &tcKeyReq->scanInfo;
  // Write first part of key in TCKEYREQ
  Uint32 keyBufSize = 8; // Maximum for key in TCKEYREQ
  Uint32 attrBufSize = 5; // Maximum for key in TCKEYREQ
  Uint32 dataPos = 0;
  // Filter out AttributeHeader:s since this should no be in key
12594
  bool moreKeyData = afterValues.first(iter);
12595 12596
  Uint32 headerSize = 0, keyAttrSize = 0, dataSize = 0, headAndData = 0;

12597
  while (moreKeyData && (dataPos < keyBufSize)) {
12598
    /*
12599 12600 12601
     * If we have not read complete key
     * and it fits in the signal
     */
12602 12603 12604 12605 12606 12607 12608 12609 12610
    jam();
    AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
    
    headerSize = attrHeader->getHeaderSize();
    keyAttrSize = attrHeader->getDataSize();
    headAndData = headerSize + attrHeader->getDataSize();
    // Skip header
    if (headerSize == 1) {
      jam();
12611
      moreKeyData = afterValues.next(iter);
12612 12613
    } else {
      jam();
12614
      moreKeyData = afterValues.next(iter, headerSize - 1);
12615
    }//if
12616
    while((keyAttrSize != 0) && (dataPos < keyBufSize)) {
12617 12618 12619 12620 12621
      // If we have not read complete key
      jam();
      *dataPtr++ = *iter.data;
      dataPos++;
      keyAttrSize--;
12622
      moreKeyData = afterValues.next(iter);
12623 12624 12625 12626 12627 12628 12629 12630
    }
    if (keyAttrSize != 0) {
      jam();
      break;
    }//if
  }

  tcKeyLength += dataPos;
12631
  Uint32 attributesLength = afterValues.getSize() + 
12632 12633 12634 12635 12636 12637 12638 12639
    pkAttrHeader.getHeaderSize() + pkAttrHeader.getDataSize();
  if (attributesLength <= attrBufSize) {
    jam();
    // ATTRINFO fits in TCKEYREQ
    // Pack ATTRINFO IN TCKEYREQ as one attribute
    TcKeyReq::setAIInTcKeyReq(tcKeyRequestInfo, attributesLength);
    bool moreAttrData;
    // Insert primary key attributes (insert after values of primary table)
12640
    for(moreAttrData = afterValues.first(iter);
12641
	moreAttrData;
12642
	moreAttrData = afterValues.next(iter)) {      
12643 12644 12645 12646 12647 12648
      *dataPtr++ = *iter.data;
    }
    // Insert attribute values (insert key values of primary table)
    // as one attribute
    pkAttrHeader.insertHeader(dataPtr);
    dataPtr += pkAttrHeader.getHeaderSize();
12649
    moreAttrData = keyValues.first(iter);
12650 12651 12652 12653 12654 12655 12656 12657 12658
    while(moreAttrData) {
      jam();
      AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
      
      headerSize = attrHeader->getHeaderSize();
      dataSize = attrHeader->getDataSize();
      // Skip header
      if (headerSize == 1) {
        jam();
12659
        moreAttrData = keyValues.next(iter);
12660 12661
      } else {
        jam();
12662
        moreAttrData = keyValues.next(iter, headerSize - 1);
12663 12664 12665 12666
      }//if
      // Copy attribute data
      while(dataSize-- != 0) {
	*dataPtr++ = *iter.data;
12667
	moreAttrData = keyValues.next(iter);
12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700
      }
    }
    tcKeyLength += attributesLength;
  } else {
    jam();
    // No ATTRINFO in TCKEYREQ
    TcKeyReq::setAIInTcKeyReq(tcKeyRequestInfo, 0);
  }
  tcKeyReq->requestInfo = tcKeyRequestInfo;

  /**
   * Fix savepoint id -
   *   fix so that insert has same savepoint id as triggering operation
   */
  const Uint32 currSavePointId = regApiPtr->currSavePointId;
  regApiPtr->currSavePointId = opRecord->savePointId;
  EXECUTE_DIRECT(DBTC, GSN_TCKEYREQ, signal, tcKeyLength);
  regApiPtr->currSavePointId = currSavePointId;
  tcConnectptr.p->currentIndexId = indexData->indexId;
  jamEntry();

  // *********** KEYINFO ***********
  if (moreKeyData) {
    jam();
    // Send KEYINFO sequence
    KeyInfo * const keyInfo =  (KeyInfo *)signal->getDataPtrSend();
    
    keyInfo->connectPtr = transPtr->i;
    keyInfo->transId[0] = regApiPtr->transid[0];
    keyInfo->transId[1] = regApiPtr->transid[1];
    dataPtr = (Uint32 *) &keyInfo->keyData;
    dataPos = 0;
    // Pack any part of a key attribute that did no fit TCKEYREQ
12701
    while((keyAttrSize != 0) && (dataPos < KeyInfo::DataLength)) {
12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719
      // If we have not read complete key
      *dataPtr++ = *iter.data;
      dataPos++;
      keyAttrSize--;
      if (dataPos == KeyInfo::DataLength) {
        jam();
	// Flush KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	sendSignal(reference(), GSN_KEYINFO, signal, 
		   KeyInfo::HeaderLength + KeyInfo::DataLength, JBA);
#else
	EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		       KeyInfo::HeaderLength + KeyInfo::DataLength);
        jamEntry();
#endif
	dataPtr = (Uint32 *) &keyInfo->keyData;
	dataPos = 0;
      }
12720
      moreKeyData = afterValues.next(iter);
12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732
    }
    
    while(moreKeyData) {
      jam();
      AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
      
      headerSize = attrHeader->getHeaderSize();
      keyAttrSize = attrHeader->getDataSize();
      headAndData = headerSize + attrHeader->getDataSize();
      // Skip header
      if (headerSize == 1) {
        jam();
12733
        moreKeyData = afterValues.next(iter);
12734 12735
      } else {
        jam();
12736
        moreKeyData = afterValues.next(iter, headerSize - 1);
12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754
      }//if
      while (keyAttrSize-- != 0) {
        *dataPtr++ = *iter.data;
        dataPos++;
        if (dataPos == KeyInfo::DataLength) {
          jam();
          // Flush KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	  sendSignal(reference(), GSN_KEYINFO, signal, 
		     KeyInfo::HeaderLength + KeyInfo::DataLength, JBA);
#else
          EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
			 KeyInfo::HeaderLength + KeyInfo::DataLength);
          jamEntry();
#endif
	  dataPtr = (Uint32 *) &keyInfo->keyData;	  
          dataPos = 0;
        }       
12755
        moreKeyData = afterValues.next(iter);
12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787
      }
    }
    if (dataPos != 0) {
      jam();
      // Flush last KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
      sendSignal(reference(), GSN_KEYINFO, signal, 
		 KeyInfo::HeaderLength + dataPos, JBA);
#else
      EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		     KeyInfo::HeaderLength + dataPos);
      jamEntry();
#endif
    }
  }
  
  // *********** ATTRINFO ***********
  if (attributesLength > attrBufSize) {
    jam();
    // No ATTRINFO in TcKeyReq
    TcKeyReq::setAIInTcKeyReq(tcKeyReq->requestInfo, 0);
    // Send ATTRINFO sequence
    AttrInfo * const attrInfo =  (AttrInfo *)signal->getDataPtrSend();
    Uint32 attrInfoPos = 0;
    
    attrInfo->connectPtr = transPtr->i;
    attrInfo->transId[0] = regApiPtr->transid[0];
    attrInfo->transId[1] = regApiPtr->transid[1];
    dataPtr = (Uint32 *) &attrInfo->attrData;
    
    bool moreAttrData;
    // Insert primary key attributes (insert after values of primary table)
12788
    for(moreAttrData = afterValues.first(iter);
12789
	moreAttrData;
12790
	moreAttrData = afterValues.next(iter)) {      
12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812
      *dataPtr++ = *iter.data;
      attrInfoPos++;
      if (attrInfoPos == AttrInfo::DataLength) {
        jam();
	// Flush ATTRINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	sendSignal(reference(), GSN_ATTRINFO, signal, 
		   AttrInfo::HeaderLength + AttrInfo::DataLength, JBA);
#else
	EXECUTE_DIRECT(DBTC, GSN_ATTRINFO, signal,
		       AttrInfo::HeaderLength + AttrInfo::DataLength);
        jamEntry();
#endif
	dataPtr = (Uint32 *) &attrInfo->attrData;
	attrInfoPos = 0;
      }
    }
    // Insert attribute values (insert key values of primary table)
    // as one attribute
    pkAttrHeader.insertHeader(dataPtr);
    dataPtr += pkAttrHeader.getHeaderSize();
    attrInfoPos += pkAttrHeader.getHeaderSize();
12813
    moreAttrData = keyValues.first(iter);
12814 12815 12816 12817 12818 12819 12820 12821 12822
    while(moreAttrData) {
      jam();
      AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
      
      headerSize = attrHeader->getHeaderSize();
      dataSize = attrHeader->getDataSize();
      // Skip header
      if (headerSize == 1) {
        jam();
12823
        moreAttrData = keyValues.next(iter);
12824 12825
      } else {
        jam();
12826
        moreAttrData = keyValues.next(iter, headerSize - 1);
12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844
      }//if
      while(dataSize-- != 0) { // If we have not read complete key
        if (attrInfoPos == AttrInfo::DataLength) {
          jam();
          // Flush ATTRINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	  sendSignal(reference(), GSN_ATTRINFO, signal, 
		     AttrInfo::HeaderLength + AttrInfo::DataLength, JBA);
#else
	  EXECUTE_DIRECT(DBTC, GSN_ATTRINFO, signal,
			 AttrInfo::HeaderLength + AttrInfo::DataLength);
          jamEntry();
#endif
	  dataPtr = (Uint32 *) &attrInfo->attrData;	  
          attrInfoPos = 0;
        }       
        *dataPtr++ = *iter.data;
        attrInfoPos++;
12845
        moreAttrData = keyValues.next(iter);
12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889
      }
    }
    if (attrInfoPos != 0) {
      jam();
      // Flush last ATTRINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
      sendSignal(reference(), GSN_ATTRINFO, signal, 
		 AttrInfo::HeaderLength + attrInfoPos, JBA);
#else
      EXECUTE_DIRECT(DBTC, GSN_ATTRINFO, signal,
		     AttrInfo::HeaderLength + attrInfoPos);
      jamEntry();
#endif
    }
  }
}

void Dbtc::deleteFromIndexTable(Signal* signal, 
                                TcFiredTriggerData* firedTriggerData, 
                                ApiConnectRecordPtr* transPtr,
                                TcConnectRecordPtr* opPtr,
                                TcIndexData* indexData,
                                bool holdOperation)
{
  ApiConnectRecord* regApiPtr = transPtr->p;
  TcConnectRecord* opRecord = opPtr->p;
  TcKeyReq * const tcKeyReq =  (TcKeyReq *)signal->getDataPtrSend();
  Uint32 tcKeyRequestInfo = 0;
  Uint32 tcKeyLength = 12; // Static length
  TableRecordPtr indexTabPtr;
  AttributeBuffer::DataBufferIterator iter;
  Uint32 attrId = 0;
  Uint32 keyLength = 0;
  Uint32 hops;

  indexTabPtr.i = indexData->indexId;
  ptrCheckGuard(indexTabPtr, ctabrecFilesize, tableRecord);
  tcKeyReq->apiConnectPtr = transPtr->i;
  tcKeyReq->senderData = opPtr->i;
  if (holdOperation) {
    jam();
    opRecord->triggerExecutionCount++;
  }//if
  // Calculate key length and renumber attribute id:s
12890 12891
  AttributeBuffer::DataBufferPool & pool = c_theAttributeBufferPool;
  LocalDataBuffer<11> beforeValues(pool, firedTriggerData->beforeValues);
12892
  bool skipNull = false;
12893
  for(bool moreKeyAttrs = beforeValues.first(iter);
12894 12895 12896 12897 12898
      (moreKeyAttrs);
      attrId++) {
    jam();
    AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
    
12899 12900 12901 12902 12903
    // Filter out NULL valued attributes
    if (attrHeader->isNULL()) {
      skipNull = true;
      break;
    }
12904 12905 12906
    attrHeader->setAttributeId(attrId);      
    keyLength += attrHeader->getDataSize();
    hops = attrHeader->getHeaderSize() + attrHeader->getDataSize();
12907
    moreKeyAttrs = beforeValues.next(iter, hops);
12908 12909
  }

12910
  if (skipNull) {
12911
    jam();
12912 12913 12914
    opRecord->triggerExecutionCount--;
    if (opRecord->triggerExecutionCount == 0) {
      /*
12915 12916
        We have completed current trigger execution
	Continue triggering operation
12917 12918 12919
      */
      jam();
      continueTriggeringOp(signal, opRecord);	
12920
    }//if
12921
    return;
12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936
  }//if

  TcKeyReq::setKeyLength(tcKeyRequestInfo, keyLength);
  tcKeyReq->attrLen = 0;
  tcKeyReq->tableId = indexData->indexId;
  TcKeyReq::setOperationType(tcKeyRequestInfo, ZDELETE);
  TcKeyReq::setExecutingTrigger(tcKeyRequestInfo, true);
  tcKeyReq->tableSchemaVersion = indexTabPtr.p->currentSchemaVersion;
  tcKeyReq->transId1 = regApiPtr->transid[0];
  tcKeyReq->transId2 = regApiPtr->transid[1];
  Uint32 * dataPtr = &tcKeyReq->scanInfo;
  // Write first part of key in TCKEYREQ
  Uint32 keyBufSize = 8; // Maximum for key in TCKEYREQ
  Uint32 dataPos = 0;
  // Filter out AttributeHeader:s since this should no be in key
12937
  bool moreKeyData = beforeValues.first(iter);
12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954
  Uint32 headerSize = 0, keyAttrSize = 0, headAndData = 0;

  while (moreKeyData && 
         (dataPos < keyBufSize)) {
    /*
    If we have not read complete key
    and it fits in the signal
    */
    jam();
    AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
    
    headerSize = attrHeader->getHeaderSize();
    keyAttrSize = attrHeader->getDataSize();
    headAndData = headerSize + attrHeader->getDataSize();
    // Skip header
    if (headerSize == 1) {
      jam();
12955
      moreKeyData = beforeValues.next(iter);
12956 12957
    } else {
      jam();
12958
      moreKeyData = beforeValues.next(iter, headerSize - 1);
12959 12960 12961 12962 12963 12964 12965 12966
    }//if
    while((keyAttrSize != 0) && 
          (dataPos < keyBufSize)) {
      // If we have not read complete key
      jam();
      *dataPtr++ = *iter.data;
      dataPos++;
      keyAttrSize--;
12967
      moreKeyData = beforeValues.next(iter);
12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020
    }
    if (keyAttrSize != 0) {
      jam();
      break;
    }//if
  }

  tcKeyLength += dataPos;
  tcKeyReq->requestInfo = tcKeyRequestInfo;

  /**
   * Fix savepoint id -
   *   fix so that delete has same savepoint id as triggering operation
   */
  const Uint32 currSavePointId = regApiPtr->currSavePointId;
  regApiPtr->currSavePointId = opRecord->savePointId;
  EXECUTE_DIRECT(DBTC, GSN_TCKEYREQ, signal, tcKeyLength);
  regApiPtr->currSavePointId = currSavePointId;
  tcConnectptr.p->currentIndexId = indexData->indexId;
  jamEntry();

  // *********** KEYINFO ***********
  if (moreKeyData) {
    jam();
    // Send KEYINFO sequence
    KeyInfo * const keyInfo =  (KeyInfo *)signal->getDataPtrSend();
    
    keyInfo->connectPtr = transPtr->i;
    keyInfo->transId[0] = regApiPtr->transid[0];
    keyInfo->transId[1] = regApiPtr->transid[1];
    dataPtr = (Uint32 *) &keyInfo->keyData;
    dataPos = 0;
    // Pack any part of a key attribute that did no fit TCKEYREQ
    while((keyAttrSize != 0) &&
	  (dataPos < KeyInfo::DataLength)) {
      // If we have not read complete key
      *dataPtr++ = *iter.data;
      dataPos++;
      keyAttrSize--;
      if (dataPos == KeyInfo::DataLength) {
        jam();
	// Flush KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	sendSignal(reference(), GSN_KEYINFO, signal, 
		   KeyInfo::HeaderLength + KeyInfo::DataLength, JBA);
#else
	EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		       KeyInfo::HeaderLength + KeyInfo::DataLength);
        jamEntry();
#endif
	dataPtr = (Uint32 *) &keyInfo->keyData;
	dataPos = 0;
      }
13021
      moreKeyData = beforeValues.next(iter);
13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033
    }
    
    while(moreKeyData) {
      jam();
      AttributeHeader* attrHeader = (AttributeHeader *) iter.data;
      
      headerSize = attrHeader->getHeaderSize();
      keyAttrSize = attrHeader->getDataSize();
      headAndData = headerSize + attrHeader->getDataSize();
      // Skip header
      if (headerSize == 1) {
        jam();
13034
        moreKeyData = beforeValues.next(iter);
13035 13036
      } else {
        jam();
13037
        moreKeyData = beforeValues.next(iter, 
13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056
							  headerSize - 1);
      }//if
      while (keyAttrSize-- != 0) {
        *dataPtr++ = *iter.data;
        dataPos++;
        if (dataPos == KeyInfo::DataLength) {
          jam();
          // Flush KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
	  sendSignal(reference(), GSN_KEYINFO, signal, 
		     KeyInfo::HeaderLength + KeyInfo::DataLength, JBA);
#else
          EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
			 KeyInfo::HeaderLength + KeyInfo::DataLength);
          jamEntry();
#endif
	  dataPtr = (Uint32 *) &keyInfo->keyData;	  
          dataPos = 0;
        }       
13057
        moreKeyData = beforeValues.next(iter);
13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080
      }
    }
    if (dataPos != 0) {
      jam();
      // Flush last KEYINFO
#if INTERNAL_TRIGGER_TCKEYREQ_JBA
      sendSignal(reference(), GSN_KEYINFO, signal, 
		 KeyInfo::HeaderLength + dataPos, JBA);
#else
      EXECUTE_DIRECT(DBTC, GSN_KEYINFO, signal,
		     KeyInfo::HeaderLength + dataPos);
      jamEntry();
#endif
    }
  }
}

Uint32 
Dbtc::TableRecord::getErrorCode(Uint32 schemaVersion) const {
  if(!enabled)
    return ZNO_SUCH_TABLE;
  if(dropping)
    return ZDROP_TABLE_IN_PROGRESS;
13081
  if(table_version_major(schemaVersion) != table_version_major(currentSchemaVersion))
13082 13083 13084 13085 13086 13087
    return ZWRONG_SCHEMA_VERSION_ERROR;
  ErrorReporter::handleAssert("Dbtc::TableRecord::getErrorCode",
			      __FILE__, __LINE__);
  return 0;
}