1 /* Copyright (c) 2003-2007 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 #define NDBCNTR_C
18 #include "Ndbcntr.hpp"
19 
20 #include <ndb_limits.h>
21 #include <ndb_version.h>
22 #include <SimpleProperties.hpp>
23 #include <signaldata/DictTabInfo.hpp>
24 #include <signaldata/CreateTable.hpp>
25 #include <signaldata/ReadNodesConf.hpp>
26 #include <signaldata/NodeFailRep.hpp>
27 #include <signaldata/TcKeyReq.hpp>
28 #include <signaldata/TcKeyConf.hpp>
29 #include <signaldata/EventReport.hpp>
30 #include <signaldata/NodeStateSignalData.hpp>
31 #include <signaldata/StopPerm.hpp>
32 #include <signaldata/StopMe.hpp>
33 #include <signaldata/WaitGCP.hpp>
34 #include <signaldata/CheckNodeGroups.hpp>
35 #include <signaldata/StartOrd.hpp>
36 #include <signaldata/AbortAll.hpp>
37 #include <signaldata/SystemError.hpp>
38 #include <signaldata/NdbSttor.hpp>
39 #include <signaldata/CntrStart.hpp>
40 #include <signaldata/DumpStateOrd.hpp>
41 
42 #include <signaldata/FsRemoveReq.hpp>
43 #include <signaldata/ReadConfig.hpp>
44 
45 #include <signaldata/FailRep.hpp>
46 
47 #include <AttributeHeader.hpp>
48 #include <Configuration.hpp>
49 #include <DebuggerNames.hpp>
50 
51 #include <NdbOut.hpp>
52 #include <NdbTick.h>
53 
54 // used during shutdown for reporting current startphase
55 // accessed from Emulator.cpp, NdbShutdown()
56 Uint32 g_currentStartPhase;
57 
58 /**
59  * ALL_BLOCKS Used during start phases and while changing node state
60  *
61  * NDBFS_REF Has to be before NDBCNTR_REF (due to "ndb -i" stuff)
62  */
63 struct BlockInfo {
64   BlockReference Ref; // BlockReference
65   Uint32 NextSP;            // Next start phase
66   Uint32 ErrorInsertStart;
67   Uint32 ErrorInsertStop;
68 };
69 
70 static BlockInfo ALL_BLOCKS[] = {
71   { NDBFS_REF,   0 ,  2000,  2999 },
72   { DBTC_REF,    1 ,  8000,  8035 },
73   { DBDIH_REF,   1 ,  7000,  7173 },
74   { DBLQH_REF,   1 ,  5000,  5030 },
75   { DBACC_REF,   1 ,  3000,  3999 },
76   { DBTUP_REF,   1 ,  4000,  4007 },
77   { DBDICT_REF,  1 ,  6000,  6003 },
78   { NDBCNTR_REF, 0 ,  1000,  1999 },
79   { CMVMI_REF,   1 ,  9000,  9999 }, // before QMGR
80   { QMGR_REF,    1 ,     1,   999 },
81   { TRIX_REF,    1 ,     0,     0 },
82   { BACKUP_REF,  1 , 10000, 10999 },
83   { DBUTIL_REF,  1 , 11000, 11999 },
84   { SUMA_REF,    1 , 13000, 13999 },
85   { DBTUX_REF,   1 , 12000, 12999 }
86   ,{ TSMAN_REF,  1 ,     0,     0 }
87   ,{ LGMAN_REF,  1 ,     0,     0 }
88   ,{ PGMAN_REF,  1 ,     0,     0 }
89   ,{ RESTORE_REF,1 ,     0,     0 }
90 };
91 
92 static const Uint32 ALL_BLOCKS_SZ = sizeof(ALL_BLOCKS)/sizeof(BlockInfo);
93 
94 static BlockReference readConfigOrder[ALL_BLOCKS_SZ] = {
95   CMVMI_REF,
96   DBTUP_REF,
97   DBACC_REF,
98   DBTC_REF,
99   DBLQH_REF,
100   DBTUX_REF,
101   DBDICT_REF,
102   DBDIH_REF,
103   NDBFS_REF,
104   NDBCNTR_REF,
105   QMGR_REF,
106   TRIX_REF,
107   BACKUP_REF,
108   DBUTIL_REF,
109   SUMA_REF,
110   TSMAN_REF,
111   LGMAN_REF,
112   PGMAN_REF,
113   RESTORE_REF
114 };
115 
116 /*******************************/
117 /*  CONTINUEB                  */
118 /*******************************/
execCONTINUEB(Signal * signal)119 void Ndbcntr::execCONTINUEB(Signal* signal)
120 {
121   jamEntry();
122   UintR Ttemp1 = signal->theData[0];
123   switch (Ttemp1) {
124   case ZSTARTUP:{
125     if(getNodeState().startLevel == NodeState::SL_STARTED){
126       jam();
127       return;
128     }
129 
130     if(cmasterNodeId == getOwnNodeId() && c_start.m_starting.isclear()){
131       jam();
132       trySystemRestart(signal);
133       // Fall-through
134     }
135 
136     Uint64 now = NdbTick_CurrentMillisecond();
137     if(now > c_start.m_startFailureTimeout)
138     {
139       jam();
140       Uint32 to_3= 0;
141       const ndb_mgm_configuration_iterator * p =
142 	m_ctx.m_config.getOwnConfigIterator();
143       ndb_mgm_get_int_parameter(p, CFG_DB_START_FAILURE_TIMEOUT, &to_3);
144       BaseString tmp;
145       tmp.append("Shutting down node as total restart time exceeds "
146 		 " StartFailureTimeout as set in config file ");
147       if(to_3 == 0)
148 	tmp.append(" 0 (inifinite)");
149       else
150 	tmp.appfmt(" %d", to_3);
151 
152       progError(__LINE__, NDBD_EXIT_RESTART_TIMEOUT, tmp.c_str());
153     }
154 
155     signal->theData[0] = ZSTARTUP;
156     sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 1000, 1);
157     break;
158   }
159   case ZSHUTDOWN:
160     jam();
161     c_stopRec.checkTimeout(signal);
162     break;
163   default:
164     jam();
165     systemErrorLab(signal, __LINE__);
166     return;
167     break;
168   }//switch
169 }//Ndbcntr::execCONTINUEB()
170 
171 void
execAPI_START_REP(Signal * signal)172 Ndbcntr::execAPI_START_REP(Signal* signal)
173 {
174   if(refToBlock(signal->getSendersBlockRef()) == QMGR)
175   {
176     for(Uint32 i = 0; i<ALL_BLOCKS_SZ; i++){
177       sendSignal(ALL_BLOCKS[i].Ref, GSN_API_START_REP, signal, 1, JBB);
178     }
179   }
180 }
181 /*******************************/
182 /*  SYSTEM_ERROR               */
183 /*******************************/
execSYSTEM_ERROR(Signal * signal)184 void Ndbcntr::execSYSTEM_ERROR(Signal* signal)
185 {
186   const SystemError * const sysErr = (SystemError *)signal->getDataPtr();
187   char buf[100];
188   int killingNode = refToNode(sysErr->errorRef);
189   Uint32 data1 = sysErr->data1;
190 
191   jamEntry();
192   switch (sysErr->errorCode){
193   case SystemError::GCPStopDetected:
194     BaseString::snprintf(buf, sizeof(buf),
195 	     "Node %d killed this node because "
196 	     "GCP stop was detected",
197 	     killingNode);
198     break;
199 
200   case SystemError::CopyFragRefError:
201     CRASH_INSERTION(1000);
202     BaseString::snprintf(buf, sizeof(buf),
203 			 "Killed by node %d as "
204 			 "copyfrag failed, error: %u",
205 			 killingNode, data1);
206     break;
207 
208   case SystemError::StartFragRefError:
209     BaseString::snprintf(buf, sizeof(buf),
210 			 "Node %d killed this node because "
211 			 "it replied StartFragRef error code: %u.",
212 			 killingNode, data1);
213     break;
214 
215   case SystemError::CopySubscriptionRef:
216     BaseString::snprintf(buf, sizeof(buf),
217 	     "Node %d killed this node because "
218 	     "it could not copy a subscription during node restart. "
219 	     "Copy subscription error code: %u.",
220 	     killingNode, data1);
221     break;
222   case SystemError::CopySubscriberRef:
223     BaseString::snprintf(buf, sizeof(buf),
224 	     "Node %d killed this node because "
225 	     "it could not start a subscriber during node restart. "
226 	     "Copy subscription error code: %u.",
227 	     killingNode, data1);
228     break;
229   default:
230     BaseString::snprintf(buf, sizeof(buf), "System error %d, "
231 	     " this node was killed by node %d",
232 	     sysErr->errorCode, killingNode);
233     break;
234   }
235 
236   progError(__LINE__, NDBD_EXIT_SYSTEM_ERROR, buf);
237   return;
238 }//Ndbcntr::execSYSTEM_ERROR()
239 
240 void
execREAD_CONFIG_REQ(Signal * signal)241 Ndbcntr::execREAD_CONFIG_REQ(Signal* signal)
242 {
243   jamEntry();
244 
245   const ReadConfigReq * req = (ReadConfigReq*)signal->getDataPtr();
246 
247   Uint32 ref = req->senderRef;
248   Uint32 senderData = req->senderData;
249 
250   const ndb_mgm_configuration_iterator * p =
251     m_ctx.m_config.getOwnConfigIterator();
252   ndbrequire(p != 0);
253 
254   ReadConfigConf * conf = (ReadConfigConf*)signal->getDataPtrSend();
255   conf->senderRef = reference();
256   conf->senderData = senderData;
257   sendSignal(ref, GSN_READ_CONFIG_CONF, signal,
258 	     ReadConfigConf::SignalLength, JBB);
259 }
260 
execSTTOR(Signal * signal)261 void Ndbcntr::execSTTOR(Signal* signal)
262 {
263   jamEntry();
264   cstartPhase = signal->theData[1];
265 
266   cndbBlocksCount = 0;
267   cinternalStartphase = cstartPhase - 1;
268 
269   switch (cstartPhase) {
270   case 0:
271     if(m_ctx.m_config.getInitialStart()){
272       jam();
273       c_fsRemoveCount = 0;
274       clearFilesystem(signal);
275       return;
276     }
277     sendSttorry(signal);
278     break;
279   case ZSTART_PHASE_1:
280     jam();
281     {
282       Uint32 db_watchdog_interval = 0;
283       const ndb_mgm_configuration_iterator * p =
284         m_ctx.m_config.getOwnConfigIterator();
285       ndb_mgm_get_int_parameter(p, CFG_DB_WATCHDOG_INTERVAL, &db_watchdog_interval);
286       ndbrequire(db_watchdog_interval);
287       update_watch_dog_timer(db_watchdog_interval);
288     }
289     startPhase1Lab(signal);
290     break;
291   case ZSTART_PHASE_2:
292     jam();
293     startPhase2Lab(signal);
294     break;
295   case ZSTART_PHASE_3:
296     jam();
297     startPhase3Lab(signal);
298     break;
299   case ZSTART_PHASE_4:
300     jam();
301     startPhase4Lab(signal);
302     break;
303   case ZSTART_PHASE_5:
304     jam();
305     startPhase5Lab(signal);
306     break;
307   case 6:
308     jam();
309     getNodeGroup(signal);
310     // Fall through
311     break;
312   case ZSTART_PHASE_8:
313     jam();
314     startPhase8Lab(signal);
315     break;
316   case ZSTART_PHASE_9:
317     jam();
318     startPhase9Lab(signal);
319     break;
320   default:
321     jam();
322     sendSttorry(signal);
323     break;
324   }//switch
325 }//Ndbcntr::execSTTOR()
326 
327 void
getNodeGroup(Signal * signal)328 Ndbcntr::getNodeGroup(Signal* signal){
329   jam();
330   CheckNodeGroups * sd = (CheckNodeGroups*)signal->getDataPtrSend();
331   sd->requestType = CheckNodeGroups::Direct | CheckNodeGroups::GetNodeGroup;
332   EXECUTE_DIRECT(DBDIH, GSN_CHECKNODEGROUPSREQ, signal,
333 		 CheckNodeGroups::SignalLength);
334   jamEntry();
335   c_nodeGroup = sd->output;
336   sendSttorry(signal);
337 }
338 
339 /*******************************/
340 /*  NDB_STTORRY                */
341 /*******************************/
execNDB_STTORRY(Signal * signal)342 void Ndbcntr::execNDB_STTORRY(Signal* signal)
343 {
344   jamEntry();
345   switch (cstartPhase) {
346   case ZSTART_PHASE_2:
347     jam();
348     ph2GLab(signal);
349     return;
350     break;
351   case ZSTART_PHASE_3:
352     jam();
353     ph3ALab(signal);
354     return;
355     break;
356   case ZSTART_PHASE_4:
357     jam();
358     ph4BLab(signal);
359     return;
360     break;
361   case ZSTART_PHASE_5:
362     jam();
363     ph5ALab(signal);
364     return;
365     break;
366   case ZSTART_PHASE_6:
367     jam();
368     ph6ALab(signal);
369     return;
370     break;
371   case ZSTART_PHASE_7:
372     jam();
373     ph6BLab(signal);
374     return;
375     break;
376   case ZSTART_PHASE_8:
377     jam();
378     ph7ALab(signal);
379     return;
380     break;
381   case ZSTART_PHASE_9:
382     jam();
383     ph8ALab(signal);
384     return;
385     break;
386   default:
387     jam();
388     systemErrorLab(signal, __LINE__);
389     return;
390     break;
391   }//switch
392 }//Ndbcntr::execNDB_STTORRY()
393 
startPhase1Lab(Signal * signal)394 void Ndbcntr::startPhase1Lab(Signal* signal)
395 {
396   jamEntry();
397 
398   initData(signal);
399 
400   cdynamicNodeId = 0;
401 
402   NdbBlocksRecPtr ndbBlocksPtr;
403   ndbBlocksPtr.i = 0;
404   ptrAss(ndbBlocksPtr, ndbBlocksRec);
405   ndbBlocksPtr.p->blockref = DBLQH_REF;
406   ndbBlocksPtr.i = 1;
407   ptrAss(ndbBlocksPtr, ndbBlocksRec);
408   ndbBlocksPtr.p->blockref = DBDICT_REF;
409   ndbBlocksPtr.i = 2;
410   ptrAss(ndbBlocksPtr, ndbBlocksRec);
411   ndbBlocksPtr.p->blockref = DBTUP_REF;
412   ndbBlocksPtr.i = 3;
413   ptrAss(ndbBlocksPtr, ndbBlocksRec);
414   ndbBlocksPtr.p->blockref = DBACC_REF;
415   ndbBlocksPtr.i = 4;
416   ptrAss(ndbBlocksPtr, ndbBlocksRec);
417   ndbBlocksPtr.p->blockref = DBTC_REF;
418   ndbBlocksPtr.i = 5;
419   ptrAss(ndbBlocksPtr, ndbBlocksRec);
420   ndbBlocksPtr.p->blockref = DBDIH_REF;
421   sendSttorry(signal);
422   return;
423 }
424 
execREAD_NODESREF(Signal * signal)425 void Ndbcntr::execREAD_NODESREF(Signal* signal)
426 {
427   jamEntry();
428   systemErrorLab(signal, __LINE__);
429   return;
430 }//Ndbcntr::execREAD_NODESREF()
431 
432 
433 /*******************************/
434 /*  NDB_STARTREF               */
435 /*******************************/
execNDB_STARTREF(Signal * signal)436 void Ndbcntr::execNDB_STARTREF(Signal* signal)
437 {
438   jamEntry();
439   systemErrorLab(signal, __LINE__);
440   return;
441 }//Ndbcntr::execNDB_STARTREF()
442 
443 /*******************************/
444 /*  STTOR                      */
445 /*******************************/
startPhase2Lab(Signal * signal)446 void Ndbcntr::startPhase2Lab(Signal* signal)
447 {
448   c_start.m_lastGci = 0;
449   c_start.m_lastGciNodeId = getOwnNodeId();
450 
451   signal->theData[0] = reference();
452   sendSignal(DBDIH_REF, GSN_DIH_RESTARTREQ, signal, 1, JBB);
453   return;
454 }//Ndbcntr::startPhase2Lab()
455 
456 /*******************************/
457 /*  DIH_RESTARTCONF            */
458 /*******************************/
execDIH_RESTARTCONF(Signal * signal)459 void Ndbcntr::execDIH_RESTARTCONF(Signal* signal)
460 {
461   jamEntry();
462   //cmasterDihId = signal->theData[0];
463   c_start.m_lastGci = signal->theData[1];
464   ctypeOfStart = NodeState::ST_SYSTEM_RESTART;
465   ph2ALab(signal);
466   return;
467 }//Ndbcntr::execDIH_RESTARTCONF()
468 
469 /*******************************/
470 /*  DIH_RESTARTREF             */
471 /*******************************/
execDIH_RESTARTREF(Signal * signal)472 void Ndbcntr::execDIH_RESTARTREF(Signal* signal)
473 {
474   jamEntry();
475   ctypeOfStart = NodeState::ST_INITIAL_START;
476   ph2ALab(signal);
477   return;
478 }//Ndbcntr::execDIH_RESTARTREF()
479 
ph2ALab(Signal * signal)480 void Ndbcntr::ph2ALab(Signal* signal)
481 {
482   /******************************/
483   /* request configured nodes   */
484   /* from QMGR                  */
485   /*  READ_NODESREQ             */
486   /******************************/
487   signal->theData[0] = reference();
488   sendSignal(QMGR_REF, GSN_READ_NODESREQ, signal, 1, JBB);
489   return;
490 }//Ndbcntr::ph2ALab()
491 
492 inline
493 Uint64
setTimeout(Uint64 time,Uint32 timeoutValue)494 setTimeout(Uint64 time, Uint32 timeoutValue){
495   if(timeoutValue == 0)
496     return ~(Uint64)0;
497   return time + timeoutValue;
498 }
499 
500 /*******************************/
501 /*  READ_NODESCONF             */
502 /*******************************/
execREAD_NODESCONF(Signal * signal)503 void Ndbcntr::execREAD_NODESCONF(Signal* signal)
504 {
505   jamEntry();
506   const ReadNodesConf * readNodes = (ReadNodesConf *)&signal->theData[0];
507 
508   cmasterNodeId = readNodes->masterNodeId;
509   cdynamicNodeId = readNodes->ndynamicId;
510 
511   /**
512    * All defined nodes...
513    */
514   c_allDefinedNodes.assign(NdbNodeBitmask::Size, readNodes->allNodes);
515   c_clusterNodes.assign(NdbNodeBitmask::Size, readNodes->clusterNodes);
516 
517   Uint32 to_1 = 30000;
518   Uint32 to_2 = 0;
519   Uint32 to_3 = 0;
520 
521   const ndb_mgm_configuration_iterator * p =
522     m_ctx.m_config.getOwnConfigIterator();
523 
524   ndbrequire(p != 0);
525   ndb_mgm_get_int_parameter(p, CFG_DB_START_PARTIAL_TIMEOUT, &to_1);
526   ndb_mgm_get_int_parameter(p, CFG_DB_START_PARTITION_TIMEOUT, &to_2);
527   ndb_mgm_get_int_parameter(p, CFG_DB_START_FAILURE_TIMEOUT, &to_3);
528 
529   c_start.m_startTime = NdbTick_CurrentMillisecond();
530   c_start.m_startPartialTimeout = setTimeout(c_start.m_startTime, to_1);
531   c_start.m_startPartitionedTimeout = setTimeout(c_start.m_startTime, to_2);
532   c_start.m_startFailureTimeout = setTimeout(c_start.m_startTime, to_3);
533 
534   UpgradeStartup::sendCmAppChg(* this, signal, 0); // ADD
535 
536   sendCntrStartReq(signal);
537 
538   signal->theData[0] = ZSTARTUP;
539   sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 1000, 1);
540 
541   return;
542 }
543 
544 void
execCM_ADD_REP(Signal * signal)545 Ndbcntr::execCM_ADD_REP(Signal* signal){
546   jamEntry();
547   c_clusterNodes.set(signal->theData[0]);
548 }
549 
550 void
sendCntrStartReq(Signal * signal)551 Ndbcntr::sendCntrStartReq(Signal * signal){
552   jamEntry();
553 
554   CntrStartReq * req = (CntrStartReq*)signal->getDataPtrSend();
555   req->startType = ctypeOfStart;
556   req->lastGci = c_start.m_lastGci;
557   req->nodeId = getOwnNodeId();
558   sendSignal(calcNdbCntrBlockRef(cmasterNodeId), GSN_CNTR_START_REQ,
559 	     signal, CntrStartReq::SignalLength, JBB);
560 }
561 
562 void
execCNTR_START_REF(Signal * signal)563 Ndbcntr::execCNTR_START_REF(Signal * signal){
564   jamEntry();
565   const CntrStartRef * ref = (CntrStartRef*)signal->getDataPtr();
566 
567   switch(ref->errorCode){
568   case CntrStartRef::NotMaster:
569     jam();
570     cmasterNodeId = ref->masterNodeId;
571     sendCntrStartReq(signal);
572     return;
573   case CntrStartRef::StopInProgress:
574     jam();
575     progError(__LINE__, NDBD_EXIT_RESTART_DURING_SHUTDOWN);
576   }
577   ndbrequire(false);
578 }
579 
580 void
reset()581 Ndbcntr::StartRecord::reset(){
582   m_starting.clear();
583   m_waiting.clear();
584   m_withLog.clear();
585   m_withoutLog.clear();
586   m_lastGci = m_lastGciNodeId = 0;
587   m_startPartialTimeout = ~0;
588   m_startPartitionedTimeout = ~0;
589   m_startFailureTimeout = ~0;
590 
591   m_logNodesCount = 0;
592 }
593 
594 void
execCNTR_START_CONF(Signal * signal)595 Ndbcntr::execCNTR_START_CONF(Signal * signal){
596   jamEntry();
597   const CntrStartConf * conf = (CntrStartConf*)signal->getDataPtr();
598 
599   cnoStartNodes = conf->noStartNodes;
600   ctypeOfStart = (NodeState::StartType)conf->startType;
601   c_start.m_lastGci = conf->startGci;
602   cmasterNodeId = conf->masterNodeId;
603   NdbNodeBitmask tmp;
604   tmp.assign(NdbNodeBitmask::Size, conf->startedNodes);
605   c_startedNodes.bitOR(tmp);
606   c_start.m_starting.assign(NdbNodeBitmask::Size, conf->startingNodes);
607   ph2GLab(signal);
608 
609   UpgradeStartup::sendCmAppChg(* this, signal, 2); //START
610 }
611 
612 /**
613  * Tried with parallell nr, but it crashed in DIH
614  * so I turned it off, as I don't want to debug DIH now...
615  * Jonas 19/11-03
616  *
617  * After trying for 2 hours, I gave up.
618  * DIH is not designed to support it, and
619  * it requires quite of lot of changes to
620  * make it work
621  * Jonas 5/12-03
622  */
623 #define PARALLELL_NR 0
624 
625 #if PARALLELL_NR
626 const bool parallellNR = true;
627 #else
628 const bool parallellNR = false;
629 #endif
630 
631 void
execCNTR_START_REP(Signal * signal)632 Ndbcntr::execCNTR_START_REP(Signal* signal){
633   jamEntry();
634   Uint32 nodeId = signal->theData[0];
635   c_startedNodes.set(nodeId);
636   c_start.m_starting.clear(nodeId);
637 
638   /**
639    * Inform all interested blocks that node has started
640    */
641   for(Uint32 i = 0; i<ALL_BLOCKS_SZ; i++){
642     sendSignal(ALL_BLOCKS[i].Ref, GSN_NODE_START_REP, signal, 1, JBB);
643   }
644 
645   if(!c_start.m_starting.isclear()){
646     jam();
647     return;
648   }
649 
650   if(cmasterNodeId != getOwnNodeId()){
651     jam();
652     c_start.reset();
653     return;
654   }
655 
656   if(c_start.m_waiting.isclear()){
657     jam();
658     c_start.reset();
659     return;
660   }
661 
662   startWaitingNodes(signal);
663 }
664 
665 void
execCNTR_START_REQ(Signal * signal)666 Ndbcntr::execCNTR_START_REQ(Signal * signal){
667   jamEntry();
668   const CntrStartReq * req = (CntrStartReq*)signal->getDataPtr();
669 
670   const Uint32 nodeId = req->nodeId;
671   const Uint32 lastGci = req->lastGci;
672   const NodeState::StartType st = (NodeState::StartType)req->startType;
673 
674   if(cmasterNodeId == 0){
675     jam();
676     // Has not completed READNODES yet
677     sendSignalWithDelay(reference(), GSN_CNTR_START_REQ, signal, 100,
678 			signal->getLength());
679     return;
680   }
681 
682   if(cmasterNodeId != getOwnNodeId()){
683     jam();
684     sendCntrStartRef(signal, nodeId, CntrStartRef::NotMaster);
685     return;
686   }
687 
688   const NodeState & nodeState = getNodeState();
689   switch(nodeState.startLevel){
690   case NodeState::SL_NOTHING:
691   case NodeState::SL_CMVMI:
692     jam();
693     ndbrequire(false);
694   case NodeState::SL_STARTING:
695   case NodeState::SL_STARTED:
696     jam();
697     break;
698 
699   case NodeState::SL_STOPPING_1:
700   case NodeState::SL_STOPPING_2:
701   case NodeState::SL_STOPPING_3:
702   case NodeState::SL_STOPPING_4:
703     jam();
704     sendCntrStartRef(signal, nodeId, CntrStartRef::StopInProgress);
705     return;
706   }
707 
708   /**
709    * Am I starting (or started)
710    */
711   const bool starting = (nodeState.startLevel != NodeState::SL_STARTED);
712 
713   c_start.m_waiting.set(nodeId);
714   switch(st){
715   case NodeState::ST_INITIAL_START:
716     jam();
717     c_start.m_withoutLog.set(nodeId);
718     break;
719   case NodeState::ST_SYSTEM_RESTART:
720     jam();
721     c_start.m_withLog.set(nodeId);
722     if(starting && lastGci > c_start.m_lastGci){
723       jam();
724       CntrStartRef * ref = (CntrStartRef*)signal->getDataPtrSend();
725       ref->errorCode = CntrStartRef::NotMaster;
726       ref->masterNodeId = nodeId;
727       NodeReceiverGroup rg (NDBCNTR, c_start.m_waiting);
728       sendSignal(rg, GSN_CNTR_START_REF, signal,
729 		 CntrStartRef::SignalLength, JBB);
730       return;
731     }
732     if(starting){
733       jam();
734       Uint32 i = c_start.m_logNodesCount++;
735       c_start.m_logNodes[i].m_nodeId = nodeId;
736       c_start.m_logNodes[i].m_lastGci = req->lastGci;
737     }
738     break;
739   case NodeState::ST_NODE_RESTART:
740   case NodeState::ST_INITIAL_NODE_RESTART:
741   case NodeState::ST_ILLEGAL_TYPE:
742     ndbrequire(false);
743   }
744 
745   const bool startInProgress = !c_start.m_starting.isclear();
746 
747   if((starting && startInProgress) || (startInProgress && !parallellNR)){
748     jam();
749     // We're already starting together with a bunch of nodes
750     // Let this node wait...
751     return;
752   }
753 
754   if(starting){
755     jam();
756     trySystemRestart(signal);
757   } else {
758     jam();
759     startWaitingNodes(signal);
760   }
761   return;
762 }
763 
764 void
startWaitingNodes(Signal * signal)765 Ndbcntr::startWaitingNodes(Signal * signal){
766 
767 #if ! PARALLELL_NR
768   const Uint32 nodeId = c_start.m_waiting.find(0);
769   const Uint32 Tref = calcNdbCntrBlockRef(nodeId);
770   ndbrequire(nodeId != c_start.m_waiting.NotFound);
771 
772   NodeState::StartType nrType = NodeState::ST_NODE_RESTART;
773   if(c_start.m_withoutLog.get(nodeId)){
774     jam();
775     nrType = NodeState::ST_INITIAL_NODE_RESTART;
776   }
777 
778   /**
779    * Let node perform restart
780    */
781   CntrStartConf * conf = (CntrStartConf*)signal->getDataPtrSend();
782   conf->noStartNodes = 1;
783   conf->startType = nrType;
784   conf->startGci = ~0; // Not used
785   conf->masterNodeId = getOwnNodeId();
786   BitmaskImpl::clear(NdbNodeBitmask::Size, conf->startingNodes);
787   BitmaskImpl::set(NdbNodeBitmask::Size, conf->startingNodes, nodeId);
788   c_startedNodes.copyto(NdbNodeBitmask::Size, conf->startedNodes);
789   sendSignal(Tref, GSN_CNTR_START_CONF, signal,
790 	     CntrStartConf::SignalLength, JBB);
791 
792   c_start.m_waiting.clear(nodeId);
793   c_start.m_withLog.clear(nodeId);
794   c_start.m_withoutLog.clear(nodeId);
795   c_start.m_starting.set(nodeId);
796 #else
797   // Parallell nr
798 
799   c_start.m_starting = c_start.m_waiting;
800   c_start.m_waiting.clear();
801 
802   CntrStartConf * conf = (CntrStartConf*)signal->getDataPtrSend();
803   conf->noStartNodes = 1;
804   conf->startGci = ~0; // Not used
805   conf->masterNodeId = getOwnNodeId();
806   c_start.m_starting.copyto(NdbNodeBitmask::Size, conf->startingNodes);
807   c_startedNodes.copyto(NdbNodeBitmask::Size, conf->startedNodes);
808 
809   char buf[100];
810   if(!c_start.m_withLog.isclear()){
811     jam();
812     ndbout_c("Starting nodes w/ log: %s", c_start.m_withLog.getText(buf));
813 
814     NodeReceiverGroup rg(NDBCNTR, c_start.m_withLog);
815     conf->startType = NodeState::ST_NODE_RESTART;
816 
817     sendSignal(rg, GSN_CNTR_START_CONF, signal,
818 	       CntrStartConf::SignalLength, JBB);
819   }
820 
821   if(!c_start.m_withoutLog.isclear()){
822     jam();
823     ndbout_c("Starting nodes wo/ log: %s", c_start.m_withoutLog.getText(buf));
824     NodeReceiverGroup rg(NDBCNTR, c_start.m_withoutLog);
825     conf->startType = NodeState::ST_INITIAL_NODE_RESTART;
826 
827     sendSignal(rg, GSN_CNTR_START_CONF, signal,
828 	       CntrStartConf::SignalLength, JBB);
829   }
830 
831   c_start.m_waiting.clear();
832   c_start.m_withLog.clear();
833   c_start.m_withoutLog.clear();
834 #endif
835 }
836 
837 void
sendCntrStartRef(Signal * signal,Uint32 nodeId,CntrStartRef::ErrorCode code)838 Ndbcntr::sendCntrStartRef(Signal * signal,
839 			  Uint32 nodeId, CntrStartRef::ErrorCode code){
840   CntrStartRef * ref = (CntrStartRef*)signal->getDataPtrSend();
841   ref->errorCode = code;
842   ref->masterNodeId = cmasterNodeId;
843   sendSignal(calcNdbCntrBlockRef(nodeId), GSN_CNTR_START_REF, signal,
844 	     CntrStartRef::SignalLength, JBB);
845 }
846 
847 CheckNodeGroups::Output
checkNodeGroups(Signal * signal,const NdbNodeBitmask & mask)848 Ndbcntr::checkNodeGroups(Signal* signal, const NdbNodeBitmask & mask){
849   CheckNodeGroups* sd = (CheckNodeGroups*)&signal->theData[0];
850   sd->blockRef = reference();
851   sd->requestType = CheckNodeGroups::Direct | CheckNodeGroups::ArbitCheck;
852   sd->mask = mask;
853   EXECUTE_DIRECT(DBDIH, GSN_CHECKNODEGROUPSREQ, signal,
854 		 CheckNodeGroups::SignalLength);
855   jamEntry();
856   return (CheckNodeGroups::Output)sd->output;
857 }
858 
859 bool
trySystemRestart(Signal * signal)860 Ndbcntr::trySystemRestart(Signal* signal){
861   /**
862    * System restart something
863    */
864   const bool allNodes = c_start.m_waiting.equal(c_allDefinedNodes);
865   const bool allClusterNodes = c_start.m_waiting.equal(c_clusterNodes);
866 
867   if(!allClusterNodes){
868     jam();
869     return false;
870   }
871 
872   NodeState::StartType srType = NodeState::ST_SYSTEM_RESTART;
873   if(c_start.m_waiting.equal(c_start.m_withoutLog))
874   {
875     jam();
876     srType = NodeState::ST_INITIAL_START;
877     c_start.m_starting = c_start.m_withoutLog; // Used for starting...
878     c_start.m_withoutLog.clear();
879   } else {
880 
881     CheckNodeGroups::Output wLog = checkNodeGroups(signal, c_start.m_withLog);
882 
883     switch (wLog) {
884     case CheckNodeGroups::Win:
885       jam();
886       break;
887     case CheckNodeGroups::Lose:
888       jam();
889       // If we lose with all nodes, then we're in trouble
890       ndbrequire(!allNodes);
891       return false;
892     case CheckNodeGroups::Partitioning:
893       jam();
894       bool allowPartition = (c_start.m_startPartitionedTimeout != (Uint64)~0);
895 
896       if(allNodes){
897 	if(allowPartition){
898 	  jam();
899 	  break;
900 	}
901 	ndbrequire(false); // All nodes -> partitioning, which is not allowed
902       }
903 
904       break;
905     }
906 
907     // For now only with the "logged"-ones.
908     // Let the others do node restart afterwards...
909     c_start.m_starting = c_start.m_withLog;
910     c_start.m_withLog.clear();
911   }
912 
913   /**
914    * Okidoki, we try to start
915    */
916   CntrStartConf * conf = (CntrStartConf*)signal->getDataPtr();
917   conf->noStartNodes = c_start.m_starting.count();
918   conf->startType = srType;
919   conf->startGci = c_start.m_lastGci;
920   conf->masterNodeId = c_start.m_lastGciNodeId;
921   c_start.m_starting.copyto(NdbNodeBitmask::Size, conf->startingNodes);
922   c_startedNodes.copyto(NdbNodeBitmask::Size, conf->startedNodes);
923 
924   ndbrequire(c_start.m_lastGciNodeId == getOwnNodeId());
925 
926   NodeReceiverGroup rg(NDBCNTR, c_start.m_starting);
927   sendSignal(rg, GSN_CNTR_START_CONF, signal, CntrStartConf::SignalLength,JBB);
928 
929   c_start.m_waiting.bitANDC(c_start.m_starting);
930 
931   return true;
932 }
933 
ph2GLab(Signal * signal)934 void Ndbcntr::ph2GLab(Signal* signal)
935 {
936   if (cndbBlocksCount < ZNO_NDB_BLOCKS) {
937     jam();
938     sendNdbSttor(signal);
939     return;
940   }//if
941   sendSttorry(signal);
942   return;
943 }//Ndbcntr::ph2GLab()
944 
945 /*
946 4.4  START PHASE 3 */
947 /*###########################################################################*/
948 // SEND SIGNAL NDBSTTOR TO ALL BLOCKS, ACC, DICT, DIH, LQH, TC AND TUP
949 // WHEN ALL BLOCKS HAVE RETURNED THEIR NDB_STTORRY ALL BLOCK HAVE FINISHED
950 // THEIR LOCAL CONNECTIONs SUCESSFULLY
951 // AND THEN WE CAN SEND APPL_STARTREG TO INFORM QMGR THAT WE ARE READY TO
952 // SET UP DISTRIBUTED CONNECTIONS.
953 /*--------------------------------------------------------------*/
954 // THIS IS NDB START PHASE 3.
955 /*--------------------------------------------------------------*/
956 /*******************************/
957 /*  STTOR                      */
958 /*******************************/
startPhase3Lab(Signal * signal)959 void Ndbcntr::startPhase3Lab(Signal* signal)
960 {
961   ph3ALab(signal);
962   return;
963 }//Ndbcntr::startPhase3Lab()
964 
965 /*******************************/
966 /*  NDB_STTORRY                */
967 /*******************************/
ph3ALab(Signal * signal)968 void Ndbcntr::ph3ALab(Signal* signal)
969 {
970   if (cndbBlocksCount < ZNO_NDB_BLOCKS) {
971     jam();
972     sendNdbSttor(signal);
973     return;
974   }//if
975 
976   sendSttorry(signal);
977   return;
978 }//Ndbcntr::ph3ALab()
979 
980 /*
981 4.5  START PHASE 4      */
982 /*###########################################################################*/
983 // WAIT FOR ALL NODES IN CLUSTER TO CHANGE STATE INTO ZSTART ,
984 // APPL_CHANGEREP IS ALWAYS SENT WHEN SOMEONE HAVE
985 // CHANGED THEIR STATE. APPL_STARTCONF INDICATES THAT ALL NODES ARE IN START
986 // STATE SEND NDB_STARTREQ TO DIH AND THEN WAIT FOR NDB_STARTCONF
987 /*---------------------------------------------------------------------------*/
988 /*******************************/
989 /*  STTOR                      */
990 /*******************************/
startPhase4Lab(Signal * signal)991 void Ndbcntr::startPhase4Lab(Signal* signal)
992 {
993   ph4ALab(signal);
994 }//Ndbcntr::startPhase4Lab()
995 
996 
ph4ALab(Signal * signal)997 void Ndbcntr::ph4ALab(Signal* signal)
998 {
999   ph4BLab(signal);
1000   return;
1001 }//Ndbcntr::ph4ALab()
1002 
1003 /*******************************/
1004 /*  NDB_STTORRY                */
1005 /*******************************/
ph4BLab(Signal * signal)1006 void Ndbcntr::ph4BLab(Signal* signal)
1007 {
1008 /*--------------------------------------*/
1009 /* CASE: CSTART_PHASE = ZSTART_PHASE_4  */
1010 /*--------------------------------------*/
1011   if (cndbBlocksCount < ZNO_NDB_BLOCKS) {
1012     jam();
1013     sendNdbSttor(signal);
1014     return;
1015   }//if
1016   if ((ctypeOfStart == NodeState::ST_NODE_RESTART) ||
1017       (ctypeOfStart == NodeState::ST_INITIAL_NODE_RESTART)) {
1018     jam();
1019     sendSttorry(signal);
1020     return;
1021   }//if
1022   waitpoint41Lab(signal);
1023   return;
1024 }//Ndbcntr::ph4BLab()
1025 
waitpoint41Lab(Signal * signal)1026 void Ndbcntr::waitpoint41Lab(Signal* signal)
1027 {
1028   if (getOwnNodeId() == cmasterNodeId) {
1029     jam();
1030 /*--------------------------------------*/
1031 /* MASTER WAITS UNTIL ALL SLAVES HAS    */
1032 /* SENT THE REPORTS                     */
1033 /*--------------------------------------*/
1034     cnoWaitrep++;
1035     if (cnoWaitrep == cnoStartNodes) {
1036       jam();
1037       cnoWaitrep = 0;
1038 /*---------------------------------------------------------------------------*/
1039 // NDB_STARTREQ STARTS UP ALL SET UP OF DISTRIBUTION INFORMATION IN DIH AND
1040 // DICT. AFTER SETTING UP THIS
1041 // DATA IT USES THAT DATA TO SET UP WHICH FRAGMENTS THAT ARE TO START AND
1042 // WHERE THEY ARE TO START. THEN
1043 // IT SETS UP THE FRAGMENTS AND RECOVERS THEM BY:
1044 //  1) READING A LOCAL CHECKPOINT FROM DISK.
1045 //  2) EXECUTING THE UNDO LOG ON INDEX AND DATA.
1046 //  3) EXECUTING THE FRAGMENT REDO LOG FROM ONE OR SEVERAL NODES TO
1047 //     RESTORE THE RESTART CONFIGURATION OF DATA IN NDB CLUSTER.
1048 /*---------------------------------------------------------------------------*/
1049       signal->theData[0] = reference();
1050       signal->theData[1] = ctypeOfStart;
1051       sendSignal(DBDIH_REF, GSN_NDB_STARTREQ, signal, 2, JBB);
1052     }//if
1053   } else {
1054     jam();
1055 /*--------------------------------------*/
1056 /* SLAVE NODES WILL PASS HERE ONCE AND  */
1057 /* SEND A WAITPOINT REPORT TO MASTER.   */
1058 /* SLAVES WONT DO ANYTHING UNTIL THEY   */
1059 /* RECEIVE A WAIT REPORT FROM THE MASTER*/
1060 /*--------------------------------------*/
1061     signal->theData[0] = getOwnNodeId();
1062     signal->theData[1] = ZWAITPOINT_4_1;
1063     sendSignal(calcNdbCntrBlockRef(cmasterNodeId),
1064 	       GSN_CNTR_WAITREP, signal, 2, JBB);
1065   }//if
1066   return;
1067 }//Ndbcntr::waitpoint41Lab()
1068 
1069 /*******************************/
1070 /*  NDB_STARTCONF              */
1071 /*******************************/
execNDB_STARTCONF(Signal * signal)1072 void Ndbcntr::execNDB_STARTCONF(Signal* signal)
1073 {
1074   jamEntry();
1075 
1076   NodeReceiverGroup rg(NDBCNTR, c_start.m_starting);
1077   signal->theData[0] = getOwnNodeId();
1078   signal->theData[1] = ZWAITPOINT_4_2;
1079   sendSignal(rg, GSN_CNTR_WAITREP, signal, 2, JBB);
1080   return;
1081 }//Ndbcntr::execNDB_STARTCONF()
1082 
1083 /*
1084 4.6  START PHASE 5      */
1085 /*###########################################################################*/
1086 // SEND APPL_RUN TO THE QMGR IN THIS BLOCK
1087 // SEND NDB_STTOR ALL BLOCKS ACC, DICT, DIH, LQH, TC AND TUP THEN WAIT FOR
1088 // THEIR NDB_STTORRY
1089 /*---------------------------------------------------------------------------*/
1090 /*******************************/
1091 /*  STTOR                      */
1092 /*******************************/
startPhase5Lab(Signal * signal)1093 void Ndbcntr::startPhase5Lab(Signal* signal)
1094 {
1095   ph5ALab(signal);
1096   return;
1097 }//Ndbcntr::startPhase5Lab()
1098 
1099 /*******************************/
1100 /*  NDB_STTORRY                */
1101 /*******************************/
1102 /*---------------------------------------------------------------------------*/
1103 // THIS IS NDB START PHASE 5.
1104 /*---------------------------------------------------------------------------*/
1105 // IN THIS START PHASE TUP INITIALISES DISK FILES FOR DISK STORAGE IF INITIAL
1106 // START. DIH WILL START UP
1107 // THE GLOBAL CHECKPOINT PROTOCOL AND WILL CONCLUDE ANY UNFINISHED TAKE OVERS
1108 // THAT STARTED BEFORE THE SYSTEM CRASH.
1109 /*---------------------------------------------------------------------------*/
ph5ALab(Signal * signal)1110 void Ndbcntr::ph5ALab(Signal* signal)
1111 {
1112   if (cndbBlocksCount < ZNO_NDB_BLOCKS) {
1113     jam();
1114     sendNdbSttor(signal);
1115     return;
1116   }//if
1117 
1118   cstartPhase = cstartPhase + 1;
1119   cinternalStartphase = cstartPhase - 1;
1120   if (getOwnNodeId() == cmasterNodeId) {
1121     switch(ctypeOfStart){
1122     case NodeState::ST_INITIAL_START:
1123       jam();
1124       /*--------------------------------------*/
1125       /* MASTER CNTR IS RESPONSIBLE FOR       */
1126       /* CREATING SYSTEM TABLES               */
1127       /*--------------------------------------*/
1128       createSystableLab(signal, 0);
1129       return;
1130     case NodeState::ST_SYSTEM_RESTART:
1131       jam();
1132       waitpoint52Lab(signal);
1133       return;
1134     case NodeState::ST_NODE_RESTART:
1135     case NodeState::ST_INITIAL_NODE_RESTART:
1136       jam();
1137       break;
1138     case NodeState::ST_ILLEGAL_TYPE:
1139       jam();
1140       break;
1141     }
1142     ndbrequire(false);
1143   }
1144 
1145   /**
1146    * Not master
1147    */
1148   NdbSttor * const req = (NdbSttor*)signal->getDataPtrSend();
1149   switch(ctypeOfStart){
1150   case NodeState::ST_NODE_RESTART:
1151   case NodeState::ST_INITIAL_NODE_RESTART:
1152     jam();
1153     /*----------------------------------------------------------------------*/
1154     // SEND NDB START PHASE 5 IN NODE RESTARTS TO COPY DATA TO THE NEWLY
1155     // STARTED NODE.
1156     /*----------------------------------------------------------------------*/
1157     req->senderRef = reference();
1158     req->nodeId = getOwnNodeId();
1159     req->internalStartPhase = cinternalStartphase;
1160     req->typeOfStart = ctypeOfStart;
1161     req->masterNodeId = cmasterNodeId;
1162 
1163     //#define TRACE_STTOR
1164 #ifdef TRACE_STTOR
1165     ndbout_c("sending NDB_STTOR(%d) to DIH", cinternalStartphase);
1166 #endif
1167     sendSignal(DBDIH_REF, GSN_NDB_STTOR, signal,
1168 	       NdbSttor::SignalLength, JBB);
1169     return;
1170   case NodeState::ST_INITIAL_START:
1171   case NodeState::ST_SYSTEM_RESTART:
1172     jam();
1173     /*--------------------------------------*/
1174     /* DURING SYSTEMRESTART AND INITALSTART:*/
1175     /* SLAVE NODES WILL PASS HERE ONCE AND  */
1176     /* SEND A WAITPOINT REPORT TO MASTER.   */
1177     /* SLAVES WONT DO ANYTHING UNTIL THEY   */
1178     /* RECEIVE A WAIT REPORT FROM THE MASTER*/
1179     /* WHEN THE MASTER HAS FINISHED HIS WORK*/
1180     /*--------------------------------------*/
1181     signal->theData[0] = getOwnNodeId();
1182     signal->theData[1] = ZWAITPOINT_5_2;
1183     sendSignal(calcNdbCntrBlockRef(cmasterNodeId),
1184 	       GSN_CNTR_WAITREP, signal, 2, JBB);
1185     return;
1186   default:
1187     ndbrequire(false);
1188   }
1189 }//Ndbcntr::ph5ALab()
1190 
waitpoint52Lab(Signal * signal)1191 void Ndbcntr::waitpoint52Lab(Signal* signal)
1192 {
1193   cnoWaitrep = cnoWaitrep + 1;
1194 /*---------------------------------------------------------------------------*/
1195 // THIS WAITING POINT IS ONLY USED BY A MASTER NODE. WE WILL EXECUTE NDB START
1196 // PHASE 5 FOR DIH IN THE
1197 // MASTER. THIS WILL START UP LOCAL CHECKPOINTS AND WILL ALSO CONCLUDE ANY
1198 // UNFINISHED LOCAL CHECKPOINTS
1199 // BEFORE THE SYSTEM CRASH. THIS WILL ENSURE THAT WE ALWAYS RESTART FROM A
1200 // WELL KNOWN STATE.
1201 /*---------------------------------------------------------------------------*/
1202 /*--------------------------------------*/
1203 /* MASTER WAITS UNTIL HE RECEIVED WAIT  */
1204 /* REPORTS FROM ALL SLAVE CNTR          */
1205 /*--------------------------------------*/
1206   if (cnoWaitrep == cnoStartNodes) {
1207     jam();
1208     cnoWaitrep = 0;
1209 
1210     NdbSttor * const req = (NdbSttor*)signal->getDataPtrSend();
1211     req->senderRef = reference();
1212     req->nodeId = getOwnNodeId();
1213     req->internalStartPhase = cinternalStartphase;
1214     req->typeOfStart = ctypeOfStart;
1215     req->masterNodeId = cmasterNodeId;
1216 #ifdef TRACE_STTOR
1217     ndbout_c("sending NDB_STTOR(%d) to DIH", cinternalStartphase);
1218 #endif
1219     sendSignal(DBDIH_REF, GSN_NDB_STTOR, signal,
1220 	       NdbSttor::SignalLength, JBB);
1221   }//if
1222   return;
1223 }//Ndbcntr::waitpoint52Lab()
1224 
1225 /*******************************/
1226 /*  NDB_STTORRY                */
1227 /*******************************/
ph6ALab(Signal * signal)1228 void Ndbcntr::ph6ALab(Signal* signal)
1229 {
1230   if ((ctypeOfStart == NodeState::ST_NODE_RESTART) ||
1231       (ctypeOfStart == NodeState::ST_INITIAL_NODE_RESTART)) {
1232     jam();
1233     waitpoint51Lab(signal);
1234     return;
1235   }//if
1236 
1237   NodeReceiverGroup rg(NDBCNTR, c_start.m_starting);
1238   rg.m_nodes.clear(getOwnNodeId());
1239   signal->theData[0] = getOwnNodeId();
1240   signal->theData[1] = ZWAITPOINT_5_1;
1241   sendSignal(rg, GSN_CNTR_WAITREP, signal, 2, JBB);
1242 
1243   waitpoint51Lab(signal);
1244   return;
1245 }//Ndbcntr::ph6ALab()
1246 
waitpoint51Lab(Signal * signal)1247 void Ndbcntr::waitpoint51Lab(Signal* signal)
1248 {
1249   cstartPhase = cstartPhase + 1;
1250 /*---------------------------------------------------------------------------*/
1251 // A FINAL STEP IS NOW TO SEND NDB_STTOR TO TC. THIS MAKES IT POSSIBLE TO
1252 // CONNECT TO TC FOR APPLICATIONS.
1253 // THIS IS NDB START PHASE 6 WHICH IS FOR ALL BLOCKS IN ALL NODES.
1254 /*---------------------------------------------------------------------------*/
1255   cinternalStartphase = cstartPhase - 1;
1256   cndbBlocksCount = 0;
1257   ph6BLab(signal);
1258   return;
1259 }//Ndbcntr::waitpoint51Lab()
1260 
ph6BLab(Signal * signal)1261 void Ndbcntr::ph6BLab(Signal* signal)
1262 {
1263   // c_missra.currentStartPhase - cstartPhase - cinternalStartphase =
1264   // 5 - 7 - 6
1265   if (cndbBlocksCount < ZNO_NDB_BLOCKS) {
1266     jam();
1267     sendNdbSttor(signal);
1268     return;
1269   }//if
1270   if ((ctypeOfStart == NodeState::ST_NODE_RESTART) ||
1271       (ctypeOfStart == NodeState::ST_INITIAL_NODE_RESTART)) {
1272     jam();
1273     sendSttorry(signal);
1274     return;
1275   }
1276   waitpoint61Lab(signal);
1277 }
1278 
waitpoint61Lab(Signal * signal)1279 void Ndbcntr::waitpoint61Lab(Signal* signal)
1280 {
1281   if (getOwnNodeId() == cmasterNodeId) {
1282     jam();
1283     cnoWaitrep6++;
1284     if (cnoWaitrep6 == cnoStartNodes) {
1285       jam();
1286       NodeReceiverGroup rg(NDBCNTR, c_start.m_starting);
1287       rg.m_nodes.clear(getOwnNodeId());
1288       signal->theData[0] = getOwnNodeId();
1289       signal->theData[1] = ZWAITPOINT_6_2;
1290       sendSignal(rg, GSN_CNTR_WAITREP, signal, 2, JBB);
1291       sendSttorry(signal);
1292     }
1293   } else {
1294     jam();
1295     signal->theData[0] = getOwnNodeId();
1296     signal->theData[1] = ZWAITPOINT_6_1;
1297     sendSignal(calcNdbCntrBlockRef(cmasterNodeId), GSN_CNTR_WAITREP, signal, 2, JBB);
1298   }
1299 }
1300 
1301 // Start phase 8 (internal 7)
startPhase8Lab(Signal * signal)1302 void Ndbcntr::startPhase8Lab(Signal* signal)
1303 {
1304   cinternalStartphase = cstartPhase - 1;
1305   cndbBlocksCount = 0;
1306   ph7ALab(signal);
1307 }
1308 
ph7ALab(Signal * signal)1309 void Ndbcntr::ph7ALab(Signal* signal)
1310 {
1311   while (cndbBlocksCount < ZNO_NDB_BLOCKS) {
1312     jam();
1313     sendNdbSttor(signal);
1314     return;
1315   }
1316   if ((ctypeOfStart == NodeState::ST_NODE_RESTART) ||
1317       (ctypeOfStart == NodeState::ST_INITIAL_NODE_RESTART)) {
1318     jam();
1319     sendSttorry(signal);
1320     return;
1321   }
1322   waitpoint71Lab(signal);
1323 }
1324 
waitpoint71Lab(Signal * signal)1325 void Ndbcntr::waitpoint71Lab(Signal* signal)
1326 {
1327   if (getOwnNodeId() == cmasterNodeId) {
1328     jam();
1329     cnoWaitrep7++;
1330     if (cnoWaitrep7 == cnoStartNodes) {
1331       jam();
1332       NodeReceiverGroup rg(NDBCNTR, c_start.m_starting);
1333       rg.m_nodes.clear(getOwnNodeId());
1334       signal->theData[0] = getOwnNodeId();
1335       signal->theData[1] = ZWAITPOINT_7_2;
1336       sendSignal(rg, GSN_CNTR_WAITREP, signal, 2, JBB);
1337       sendSttorry(signal);
1338     }
1339   } else {
1340     jam();
1341     signal->theData[0] = getOwnNodeId();
1342     signal->theData[1] = ZWAITPOINT_7_1;
1343     sendSignal(calcNdbCntrBlockRef(cmasterNodeId), GSN_CNTR_WAITREP, signal, 2, JBB);
1344   }
1345 }
1346 
1347 // Start phase 9 (internal 8)
startPhase9Lab(Signal * signal)1348 void Ndbcntr::startPhase9Lab(Signal* signal)
1349 {
1350   cinternalStartphase = cstartPhase - 1;
1351   cndbBlocksCount = 0;
1352   ph8ALab(signal);
1353 }
1354 
ph8ALab(Signal * signal)1355 void Ndbcntr::ph8ALab(Signal* signal)
1356 {
1357 /*---------------------------------------------------------------------------*/
1358 // NODES WHICH PERFORM A NODE RESTART NEEDS TO GET THE DYNAMIC ID'S
1359 // OF THE OTHER NODES HERE.
1360 /*---------------------------------------------------------------------------*/
1361   sendSttorry(signal);
1362   resetStartVariables(signal);
1363   return;
1364 }//Ndbcntr::ph8BLab()
1365 
1366 /*******************************/
1367 /*  CNTR_WAITREP               */
1368 /*******************************/
execCNTR_WAITREP(Signal * signal)1369 void Ndbcntr::execCNTR_WAITREP(Signal* signal)
1370 {
1371   Uint16 twaitPoint;
1372 
1373   jamEntry();
1374   twaitPoint = signal->theData[1];
1375   switch (twaitPoint) {
1376   case ZWAITPOINT_4_1:
1377     jam();
1378     waitpoint41Lab(signal);
1379     break;
1380   case ZWAITPOINT_4_2:
1381     jam();
1382     sendSttorry(signal);
1383     break;
1384   case ZWAITPOINT_5_1:
1385     jam();
1386     waitpoint51Lab(signal);
1387     break;
1388   case ZWAITPOINT_5_2:
1389     jam();
1390     waitpoint52Lab(signal);
1391     break;
1392   case ZWAITPOINT_6_1:
1393     jam();
1394     waitpoint61Lab(signal);
1395     break;
1396   case ZWAITPOINT_6_2:
1397     jam();
1398     sendSttorry(signal);
1399     break;
1400   case ZWAITPOINT_7_1:
1401     jam();
1402     waitpoint71Lab(signal);
1403     break;
1404   case ZWAITPOINT_7_2:
1405     jam();
1406     sendSttorry(signal);
1407     break;
1408   default:
1409     jam();
1410     systemErrorLab(signal, __LINE__);
1411     break;
1412   }//switch
1413 }//Ndbcntr::execCNTR_WAITREP()
1414 
1415 /*******************************/
1416 /*  NODE_FAILREP               */
1417 /*******************************/
execNODE_FAILREP(Signal * signal)1418 void Ndbcntr::execNODE_FAILREP(Signal* signal)
1419 {
1420   jamEntry();
1421 
1422   if (ERROR_INSERTED(1001))
1423   {
1424     sendSignalWithDelay(reference(), GSN_NODE_FAILREP, signal, 100,
1425                         signal->getLength());
1426     return;
1427   }
1428 
1429   const NodeFailRep * nodeFail = (NodeFailRep *)&signal->theData[0];
1430   NdbNodeBitmask allFailed;
1431   allFailed.assign(NdbNodeBitmask::Size, nodeFail->theNodes);
1432 
1433   NdbNodeBitmask failedStarted = c_startedNodes;
1434   NdbNodeBitmask failedStarting = c_start.m_starting;
1435   NdbNodeBitmask failedWaiting = c_start.m_waiting;
1436 
1437   failedStarted.bitAND(allFailed);
1438   failedStarting.bitAND(allFailed);
1439   failedWaiting.bitAND(allFailed);
1440 
1441   const bool tMasterFailed = allFailed.get(cmasterNodeId);
1442   const bool tStarted = !failedStarted.isclear();
1443   const bool tStarting = !failedStarting.isclear();
1444 
1445   if(tMasterFailed){
1446     jam();
1447     /**
1448      * If master has failed choose qmgr president as master
1449      */
1450     cmasterNodeId = nodeFail->masterNodeId;
1451   }
1452 
1453   /**
1454    * Clear node bitmasks from failed nodes
1455    */
1456   c_start.m_starting.bitANDC(allFailed);
1457   c_start.m_waiting.bitANDC(allFailed);
1458   c_start.m_withLog.bitANDC(allFailed);
1459   c_start.m_withoutLog.bitANDC(allFailed);
1460   c_clusterNodes.bitANDC(allFailed);
1461   c_startedNodes.bitANDC(allFailed);
1462 
1463   const NodeState & st = getNodeState();
1464   if(st.startLevel == st.SL_STARTING){
1465     jam();
1466 
1467     const Uint32 phase = st.starting.startPhase;
1468 
1469     const bool tStartConf = (phase > 2) || (phase == 2 && cndbBlocksCount > 0);
1470 
1471     if(tMasterFailed){
1472       progError(__LINE__, NDBD_EXIT_SR_OTHERNODEFAILED,
1473 		"Unhandled node failure during restart");
1474     }
1475 
1476     if(tStartConf && tStarting){
1477       // One of other starting nodes has crashed...
1478       progError(__LINE__, NDBD_EXIT_SR_OTHERNODEFAILED,
1479 		"Unhandled node failure of starting node during restart");
1480     }
1481 
1482     if(tStartConf && tStarted){
1483       // One of other started nodes has crashed...
1484       progError(__LINE__, NDBD_EXIT_SR_OTHERNODEFAILED,
1485 		"Unhandled node failure of started node during restart");
1486     }
1487 
1488     Uint32 nodeId = 0;
1489     while(!allFailed.isclear()){
1490       nodeId = allFailed.find(nodeId + 1);
1491       allFailed.clear(nodeId);
1492       signal->theData[0] = nodeId;
1493       sendSignal(QMGR_REF, GSN_NDB_FAILCONF, signal, 1, JBB);
1494     }//for
1495 
1496     return;
1497   }
1498 
1499   ndbrequire(!allFailed.get(getOwnNodeId()));
1500 
1501   NodeFailRep * rep = (NodeFailRep *)&signal->theData[0];
1502   rep->masterNodeId = cmasterNodeId;
1503 
1504   sendSignal(DBTC_REF, GSN_NODE_FAILREP, signal,
1505 	     NodeFailRep::SignalLength, JBB);
1506 
1507   sendSignal(DBLQH_REF, GSN_NODE_FAILREP, signal,
1508 	     NodeFailRep::SignalLength, JBB);
1509 
1510   sendSignal(DBDIH_REF, GSN_NODE_FAILREP, signal,
1511 	     NodeFailRep::SignalLength, JBB);
1512 
1513   sendSignal(DBDICT_REF, GSN_NODE_FAILREP, signal,
1514 	     NodeFailRep::SignalLength, JBB);
1515 
1516   sendSignal(BACKUP_REF, GSN_NODE_FAILREP, signal,
1517 	     NodeFailRep::SignalLength, JBB);
1518 
1519   sendSignal(SUMA_REF, GSN_NODE_FAILREP, signal,
1520 	     NodeFailRep::SignalLength, JBB);
1521 
1522   sendSignal(QMGR_REF, GSN_NODE_FAILREP, signal,
1523 	     NodeFailRep::SignalLength, JBB);
1524 
1525   if (c_stopRec.stopReq.senderRef)
1526   {
1527     jam();
1528     switch(c_stopRec.m_state){
1529     case StopRecord::SR_WAIT_NODE_FAILURES:
1530     {
1531       jam();
1532       NdbNodeBitmask tmp;
1533       tmp.assign(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
1534       tmp.bitANDC(allFailed);
1535       tmp.copyto(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
1536 
1537       if (tmp.isclear())
1538       {
1539 	jam();
1540 	if (c_stopRec.stopReq.senderRef != RNIL)
1541 	{
1542 	  jam();
1543 	  StopConf * const stopConf = (StopConf *)&signal->theData[0];
1544 	  stopConf->senderData = c_stopRec.stopReq.senderData;
1545 	  stopConf->nodeState  = (Uint32) NodeState::SL_SINGLEUSER;
1546 	  sendSignal(c_stopRec.stopReq.senderRef, GSN_STOP_CONF, signal,
1547 		     StopConf::SignalLength, JBB);
1548 	}
1549 
1550 	c_stopRec.stopReq.senderRef = 0;
1551 	WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
1552 	req->senderRef = reference();
1553 	req->senderData = StopRecord::SR_UNBLOCK_GCP_START_GCP;
1554 	req->requestType = WaitGCPReq::UnblockStartGcp;
1555 	sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
1556 		   WaitGCPReq::SignalLength, JBA);
1557       }
1558       break;
1559     }
1560     case StopRecord::SR_QMGR_STOP_REQ:
1561     {
1562       NdbNodeBitmask tmp;
1563       tmp.assign(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
1564       tmp.bitANDC(allFailed);
1565 
1566       if (tmp.isclear())
1567       {
1568 	Uint32 nodeId = allFailed.find(0);
1569 	tmp.set(nodeId);
1570 
1571 	StopConf* conf = (StopConf*)signal->getDataPtrSend();
1572 	conf->senderData = c_stopRec.stopReq.senderData;
1573 	conf->nodeId = nodeId;
1574 	sendSignal(reference(),
1575 		   GSN_STOP_CONF, signal, StopConf::SignalLength, JBB);
1576       }
1577 
1578       tmp.copyto(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
1579 
1580       break;
1581     }
1582     case StopRecord::SR_BLOCK_GCP_START_GCP:
1583     case StopRecord::SR_WAIT_COMPLETE_GCP:
1584     case StopRecord::SR_UNBLOCK_GCP_START_GCP:
1585     case StopRecord::SR_CLUSTER_SHUTDOWN:
1586       break;
1587     }
1588   }
1589 
1590   signal->theData[0] = NDB_LE_NODE_FAILREP;
1591   signal->theData[2] = 0;
1592 
1593   Uint32 nodeId = 0;
1594   while(!allFailed.isclear()){
1595     nodeId = allFailed.find(nodeId + 1);
1596     allFailed.clear(nodeId);
1597     signal->theData[1] = nodeId;
1598     sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 3, JBB);
1599   }//for
1600 
1601   return;
1602 }//Ndbcntr::execNODE_FAILREP()
1603 
1604 /*******************************/
1605 /*  READ_NODESREQ              */
1606 /*******************************/
execREAD_NODESREQ(Signal * signal)1607 void Ndbcntr::execREAD_NODESREQ(Signal* signal)
1608 {
1609   jamEntry();
1610 
1611   /*----------------------------------------------------------------------*/
1612   // ANY BLOCK MAY SEND A REQUEST ABOUT NDB NODES AND VERSIONS IN THE
1613   // SYSTEM. THIS REQUEST CAN ONLY BE HANDLED IN
1614   // ABSOLUTE STARTPHASE 3 OR LATER
1615   /*----------------------------------------------------------------------*/
1616   BlockReference TuserBlockref = signal->theData[0];
1617   ReadNodesConf * const readNodes = (ReadNodesConf *)&signal->theData[0];
1618 
1619   /**
1620    * Prepare inactiveNodes bitmask.
1621    * The concept as such is by the way pretty useless.
1622    * It makes parallell starts more or less impossible...
1623    */
1624   NdbNodeBitmask tmp1;
1625   tmp1.bitOR(c_startedNodes);
1626   if(!getNodeState().getNodeRestartInProgress()){
1627     tmp1.bitOR(c_start.m_starting);
1628   } else {
1629     tmp1.set(getOwnNodeId());
1630   }
1631 
1632   NdbNodeBitmask tmp2;
1633   tmp2.bitOR(c_allDefinedNodes);
1634   tmp2.bitANDC(tmp1);
1635   /**
1636    * Fill in return signal
1637    */
1638   tmp2.copyto(NdbNodeBitmask::Size, readNodes->inactiveNodes);
1639   c_allDefinedNodes.copyto(NdbNodeBitmask::Size, readNodes->allNodes);
1640   c_clusterNodes.copyto(NdbNodeBitmask::Size, readNodes->clusterNodes);
1641   c_startedNodes.copyto(NdbNodeBitmask::Size, readNodes->startedNodes);
1642   c_start.m_starting.copyto(NdbNodeBitmask::Size, readNodes->startingNodes);
1643 
1644   readNodes->noOfNodes = c_allDefinedNodes.count();
1645   readNodes->masterNodeId = cmasterNodeId;
1646   readNodes->ndynamicId = cdynamicNodeId;
1647   if (cstartPhase > ZSTART_PHASE_2) {
1648     jam();
1649     sendSignal(TuserBlockref, GSN_READ_NODESCONF, signal,
1650 	       ReadNodesConf::SignalLength, JBB);
1651 
1652   } else {
1653     jam();
1654     signal->theData[0] = ZNOT_AVAILABLE;
1655     sendSignal(TuserBlockref, GSN_READ_NODESREF, signal, 1, JBB);
1656   }//if
1657 }//Ndbcntr::execREAD_NODESREQ()
1658 
1659 /*----------------------------------------------------------------------*/
1660 // SENDS APPL_ERROR TO QMGR AND THEN SET A POINTER OUT OF BOUNDS
1661 /*----------------------------------------------------------------------*/
systemErrorLab(Signal * signal,int line)1662 void Ndbcntr::systemErrorLab(Signal* signal, int line)
1663 {
1664   progError(line, NDBD_EXIT_NDBREQUIRE); /* BUG INSERTION */
1665   return;
1666 }//Ndbcntr::systemErrorLab()
1667 
1668 /*###########################################################################*/
1669 /* CNTR MASTER CREATES AND INITIALIZES A SYSTEMTABLE AT INITIALSTART         */
1670 /*       |-2048| # 1 00000001    |                                           */
1671 /*       |  :  |   :             |                                           */
1672 /*       | -1  | # 1 00000001    |                                           */
1673 /*       |  1  |   0             | tupleid sequence now created on first use */
1674 /*       |  :  |   :             |                   v                       */
1675 /*       | 2048|   0             |                   v                       */
1676 /*---------------------------------------------------------------------------*/
createSystableLab(Signal * signal,unsigned index)1677 void Ndbcntr::createSystableLab(Signal* signal, unsigned index)
1678 {
1679   if (index >= g_sysTableCount) {
1680     ndbassert(index == g_sysTableCount);
1681     startInsertTransactions(signal);
1682     return;
1683   }
1684   const SysTable& table = *g_sysTableList[index];
1685   Uint32 propPage[256];
1686   LinearWriter w(propPage, 256);
1687 
1688   // XXX remove commented-out lines later
1689 
1690   w.first();
1691   w.add(DictTabInfo::TableName, table.name);
1692   w.add(DictTabInfo::TableLoggedFlag, table.tableLoggedFlag);
1693   //w.add(DictTabInfo::TableKValue, 6);
1694   //w.add(DictTabInfo::MinLoadFactor, 70);
1695   //w.add(DictTabInfo::MaxLoadFactor, 80);
1696   w.add(DictTabInfo::FragmentTypeVal, (Uint32)table.fragmentType);
1697   //w.add(DictTabInfo::NoOfKeyAttr, 1);
1698   w.add(DictTabInfo::NoOfAttributes, (Uint32)table.columnCount);
1699   //w.add(DictTabInfo::NoOfNullable, (Uint32)0);
1700   //w.add(DictTabInfo::NoOfVariable, (Uint32)0);
1701   //w.add(DictTabInfo::KeyLength, 1);
1702   w.add(DictTabInfo::TableTypeVal, (Uint32)table.tableType);
1703   w.add(DictTabInfo::SingleUserMode, (Uint32)NDB_SUM_READ_WRITE);
1704 
1705   for (unsigned i = 0; i < table.columnCount; i++) {
1706     const SysColumn& column = table.columnList[i];
1707     ndbassert(column.pos == i);
1708     w.add(DictTabInfo::AttributeName, column.name);
1709     w.add(DictTabInfo::AttributeId, (Uint32)i);
1710     w.add(DictTabInfo::AttributeKeyFlag, (Uint32)column.keyFlag);
1711     w.add(DictTabInfo::AttributeStorageType,
1712 	  (Uint32)NDB_STORAGETYPE_MEMORY);
1713     w.add(DictTabInfo::AttributeArrayType,
1714 	  (Uint32)NDB_ARRAYTYPE_FIXED);
1715     w.add(DictTabInfo::AttributeNullableFlag, (Uint32)column.nullable);
1716     w.add(DictTabInfo::AttributeExtType, (Uint32)column.type);
1717     w.add(DictTabInfo::AttributeExtLength, (Uint32)column.length);
1718     w.add(DictTabInfo::AttributeEnd, (Uint32)true);
1719   }
1720   w.add(DictTabInfo::TableEnd, (Uint32)true);
1721 
1722   Uint32 length = w.getWordsUsed();
1723   LinearSectionPtr ptr[3];
1724   ptr[0].p = &propPage[0];
1725   ptr[0].sz = length;
1726 
1727   CreateTableReq* const req = (CreateTableReq*)signal->getDataPtrSend();
1728   req->senderData = index;
1729   req->senderRef = reference();
1730   sendSignal(DBDICT_REF, GSN_CREATE_TABLE_REQ, signal,
1731 	     CreateTableReq::SignalLength, JBB, ptr, 1);
1732   return;
1733 }//Ndbcntr::createSystableLab()
1734 
execCREATE_TABLE_REF(Signal * signal)1735 void Ndbcntr::execCREATE_TABLE_REF(Signal* signal)
1736 {
1737   jamEntry();
1738   progError(__LINE__,NDBD_EXIT_NDBREQUIRE, "CREATE_TABLE_REF");
1739   return;
1740 }//Ndbcntr::execDICTTABREF()
1741 
execCREATE_TABLE_CONF(Signal * signal)1742 void Ndbcntr::execCREATE_TABLE_CONF(Signal* signal)
1743 {
1744   jamEntry();
1745   CreateTableConf * const conf = (CreateTableConf*)signal->getDataPtrSend();
1746   //csystabId = conf->tableId;
1747   ndbrequire(conf->senderData < g_sysTableCount);
1748   const SysTable& table = *g_sysTableList[conf->senderData];
1749   table.tableId = conf->tableId;
1750   createSystableLab(signal, conf->senderData + 1);
1751   //startInsertTransactions(signal);
1752   return;
1753 }//Ndbcntr::execDICTTABCONF()
1754 
1755 /*******************************/
1756 /*  DICTRELEASECONF            */
1757 /*******************************/
startInsertTransactions(Signal * signal)1758 void Ndbcntr::startInsertTransactions(Signal* signal)
1759 {
1760   jamEntry();
1761 
1762   ckey = 1;
1763   ctransidPhase = ZTRUE;
1764   signal->theData[0] = 0;
1765   signal->theData[1] = reference();
1766   sendSignal(DBTC_REF, GSN_TCSEIZEREQ, signal, 2, JBB);
1767   return;
1768 }//Ndbcntr::startInsertTransactions()
1769 
1770 /*******************************/
1771 /*  TCSEIZECONF                */
1772 /*******************************/
execTCSEIZECONF(Signal * signal)1773 void Ndbcntr::execTCSEIZECONF(Signal* signal)
1774 {
1775   jamEntry();
1776   ctcConnectionP = signal->theData[1];
1777   crSystab7Lab(signal);
1778   return;
1779 }//Ndbcntr::execTCSEIZECONF()
1780 
1781 const unsigned int RowsPerCommit = 16;
crSystab7Lab(Signal * signal)1782 void Ndbcntr::crSystab7Lab(Signal* signal)
1783 {
1784   UintR tkey;
1785   UintR Tmp;
1786 
1787   TcKeyReq * const tcKeyReq = (TcKeyReq *)&signal->theData[0];
1788 
1789   UintR reqInfo_Start = 0;
1790   tcKeyReq->setOperationType(reqInfo_Start, ZINSERT); // Insert
1791   tcKeyReq->setKeyLength    (reqInfo_Start, 1);
1792   tcKeyReq->setAIInTcKeyReq (reqInfo_Start, 5);
1793   tcKeyReq->setAbortOption  (reqInfo_Start, TcKeyReq::AbortOnError);
1794 
1795 /* KEY LENGTH = 1, ATTRINFO LENGTH IN TCKEYREQ = 5 */
1796   cresponses = 0;
1797   const UintR guard0 = ckey + (RowsPerCommit - 1);
1798   for (Tmp = ckey; Tmp <= guard0; Tmp++) {
1799     UintR reqInfo = reqInfo_Start;
1800     if (Tmp == ckey) { // First iteration, Set start flag
1801       jam();
1802       tcKeyReq->setStartFlag(reqInfo, 1);
1803     } //if
1804     if (Tmp == guard0) { // Last iteration, Set commit flag
1805       jam();
1806       tcKeyReq->setCommitFlag(reqInfo, 1);
1807       tcKeyReq->setExecuteFlag(reqInfo, 1);
1808     } //if
1809     if (ctransidPhase == ZTRUE) {
1810       jam();
1811       tkey = 0;
1812       tkey = tkey - Tmp;
1813     } else {
1814       jam();
1815       tkey = Tmp;
1816     }//if
1817 
1818     tcKeyReq->apiConnectPtr      = ctcConnectionP;
1819     tcKeyReq->attrLen            = 5;
1820     tcKeyReq->tableId            = g_sysTable_SYSTAB_0.tableId;
1821     tcKeyReq->requestInfo        = reqInfo;
1822     tcKeyReq->tableSchemaVersion = ZSYSTAB_VERSION;
1823     tcKeyReq->transId1           = 0;
1824     tcKeyReq->transId2           = ckey;
1825 
1826 //-------------------------------------------------------------
1827 // There is no optional part in this TCKEYREQ. There is one
1828 // key word and five ATTRINFO words.
1829 //-------------------------------------------------------------
1830     Uint32* tKeyDataPtr          = &tcKeyReq->scanInfo;
1831     Uint32* tAIDataPtr           = &tKeyDataPtr[1];
1832 
1833     tKeyDataPtr[0]               = tkey;
1834 
1835     AttributeHeader::init(&tAIDataPtr[0], 0, 1 << 2);
1836     tAIDataPtr[1]                = tkey;
1837     AttributeHeader::init(&tAIDataPtr[2], 1, 2 << 2);
1838     tAIDataPtr[3]                = (tkey << 16);
1839     tAIDataPtr[4]                = 1;
1840     sendSignal(DBTC_REF, GSN_TCKEYREQ, signal,
1841 	       TcKeyReq::StaticLength + 6, JBB);
1842   }//for
1843   ckey = ckey + RowsPerCommit;
1844   return;
1845 }//Ndbcntr::crSystab7Lab()
1846 
1847 /*******************************/
1848 /*  TCKEYCONF09                */
1849 /*******************************/
execTCKEYCONF(Signal * signal)1850 void Ndbcntr::execTCKEYCONF(Signal* signal)
1851 {
1852   const TcKeyConf * const keyConf = (TcKeyConf *)&signal->theData[0];
1853 
1854   jamEntry();
1855   cgciSystab = keyConf->gci;
1856   UintR confInfo = keyConf->confInfo;
1857 
1858   if (TcKeyConf::getMarkerFlag(confInfo)){
1859     Uint32 transId1 = keyConf->transId1;
1860     Uint32 transId2 = keyConf->transId2;
1861     signal->theData[0] = transId1;
1862     signal->theData[1] = transId2;
1863     sendSignal(DBTC_REF, GSN_TC_COMMIT_ACK, signal, 2, JBB);
1864   }//if
1865 
1866   cresponses = cresponses + TcKeyConf::getNoOfOperations(confInfo);
1867   if (TcKeyConf::getCommitFlag(confInfo)){
1868     jam();
1869     ndbrequire(cresponses == RowsPerCommit);
1870 
1871     crSystab8Lab(signal);
1872     return;
1873   }
1874   return;
1875 }//Ndbcntr::tckeyConfLab()
1876 
crSystab8Lab(Signal * signal)1877 void Ndbcntr::crSystab8Lab(Signal* signal)
1878 {
1879   if (ckey < ZSIZE_SYSTAB) {
1880     jam();
1881     crSystab7Lab(signal);
1882     return;
1883   } else if (ctransidPhase == ZTRUE) {
1884     jam();
1885     ckey = 1;
1886     ctransidPhase = ZFALSE;
1887     // skip 2nd loop - tupleid sequence now created on first use
1888   }//if
1889   signal->theData[0] = ctcConnectionP;
1890   signal->theData[1] = reference();
1891   signal->theData[2] = 0;
1892   sendSignal(DBTC_REF, GSN_TCRELEASEREQ, signal, 2, JBB);
1893   return;
1894 }//Ndbcntr::crSystab8Lab()
1895 
1896 /*******************************/
1897 /*  TCRELEASECONF              */
1898 /*******************************/
execTCRELEASECONF(Signal * signal)1899 void Ndbcntr::execTCRELEASECONF(Signal* signal)
1900 {
1901   jamEntry();
1902   waitpoint52Lab(signal);
1903   return;
1904 }//Ndbcntr::execTCRELEASECONF()
1905 
crSystab9Lab(Signal * signal)1906 void Ndbcntr::crSystab9Lab(Signal* signal)
1907 {
1908   signal->theData[1] = reference();
1909   sendSignalWithDelay(DBDIH_REF, GSN_GETGCIREQ, signal, 100, 2);
1910   return;
1911 }//Ndbcntr::crSystab9Lab()
1912 
1913 /*******************************/
1914 /*  GETGCICONF                 */
1915 /*******************************/
execGETGCICONF(Signal * signal)1916 void Ndbcntr::execGETGCICONF(Signal* signal)
1917 {
1918   jamEntry();
1919 
1920 #ifndef NO_GCP
1921   if (signal->theData[1] < cgciSystab) {
1922     jam();
1923 /*--------------------------------------*/
1924 /* MAKE SURE THAT THE SYSTABLE IS       */
1925 /* NOW SAFE ON DISK                     */
1926 /*--------------------------------------*/
1927     crSystab9Lab(signal);
1928     return;
1929   }//if
1930 #endif
1931   waitpoint52Lab(signal);
1932   return;
1933 }//Ndbcntr::execGETGCICONF()
1934 
execTCKEYREF(Signal * signal)1935 void Ndbcntr::execTCKEYREF(Signal* signal)
1936 {
1937   jamEntry();
1938   systemErrorLab(signal, __LINE__);
1939   return;
1940 }//Ndbcntr::execTCKEYREF()
1941 
execTCROLLBACKREP(Signal * signal)1942 void Ndbcntr::execTCROLLBACKREP(Signal* signal)
1943 {
1944   jamEntry();
1945   systemErrorLab(signal, __LINE__);
1946   return;
1947 }//Ndbcntr::execTCROLLBACKREP()
1948 
execTCRELEASEREF(Signal * signal)1949 void Ndbcntr::execTCRELEASEREF(Signal* signal)
1950 {
1951   jamEntry();
1952   systemErrorLab(signal, __LINE__);
1953   return;
1954 }//Ndbcntr::execTCRELEASEREF()
1955 
execTCSEIZEREF(Signal * signal)1956 void Ndbcntr::execTCSEIZEREF(Signal* signal)
1957 {
1958   jamEntry();
1959   systemErrorLab(signal, __LINE__);
1960   return;
1961 }//Ndbcntr::execTCSEIZEREF()
1962 
1963 
1964 /*---------------------------------------------------------------------------*/
1965 /*INITIALIZE VARIABLES AND RECORDS                                           */
1966 /*---------------------------------------------------------------------------*/
initData(Signal * signal)1967 void Ndbcntr::initData(Signal* signal)
1968 {
1969   c_start.reset();
1970   cmasterNodeId = 0;
1971   cnoStartNodes = 0;
1972   cnoWaitrep = 0;
1973 }//Ndbcntr::initData()
1974 
1975 
1976 /*---------------------------------------------------------------------------*/
1977 /*RESET VARIABLES USED DURING THE START                                      */
1978 /*---------------------------------------------------------------------------*/
resetStartVariables(Signal * signal)1979 void Ndbcntr::resetStartVariables(Signal* signal)
1980 {
1981   cnoStartNodes = 0;
1982   cnoWaitrep6 = cnoWaitrep7 = 0;
1983 }//Ndbcntr::resetStartVariables()
1984 
1985 
1986 /*---------------------------------------------------------------------------*/
1987 // SEND THE SIGNAL
1988 // INPUT                  CNDB_BLOCKS_COUNT
1989 /*---------------------------------------------------------------------------*/
sendNdbSttor(Signal * signal)1990 void Ndbcntr::sendNdbSttor(Signal* signal)
1991 {
1992   NdbBlocksRecPtr ndbBlocksPtr;
1993 
1994   ndbBlocksPtr.i = cndbBlocksCount;
1995   ptrCheckGuard(ndbBlocksPtr, ZSIZE_NDB_BLOCKS_REC, ndbBlocksRec);
1996 
1997   NdbSttor * const req = (NdbSttor*)signal->getDataPtrSend();
1998   req->senderRef = reference();
1999   req->nodeId = getOwnNodeId();
2000   req->internalStartPhase = cinternalStartphase;
2001   req->typeOfStart = ctypeOfStart;
2002   req->masterNodeId = cmasterNodeId;
2003 
2004   for (int i = 0; i < 16; i++) {
2005     // Garbage
2006     req->config[i] = 0x88776655;
2007     //cfgBlockPtr.p->cfgData[i];
2008   }
2009 
2010   //#define MAX_STARTPHASE 2
2011 #ifdef TRACE_STTOR
2012   ndbout_c("sending NDB_STTOR(%d) to %s",
2013 	   cinternalStartphase,
2014 	   getBlockName( refToBlock(ndbBlocksPtr.p->blockref)));
2015 #endif
2016   sendSignal(ndbBlocksPtr.p->blockref, GSN_NDB_STTOR, signal, 22, JBB);
2017   cndbBlocksCount++;
2018 }//Ndbcntr::sendNdbSttor()
2019 
2020 /*---------------------------------------------------------------------------*/
2021 // JUST SEND THE SIGNAL
2022 /*---------------------------------------------------------------------------*/
sendSttorry(Signal * signal)2023 void Ndbcntr::sendSttorry(Signal* signal)
2024 {
2025   signal->theData[3] = ZSTART_PHASE_1;
2026   signal->theData[4] = ZSTART_PHASE_2;
2027   signal->theData[5] = ZSTART_PHASE_3;
2028   signal->theData[6] = ZSTART_PHASE_4;
2029   signal->theData[7] = ZSTART_PHASE_5;
2030   signal->theData[8] = ZSTART_PHASE_6;
2031   // skip simulated phase 7
2032   signal->theData[9] = ZSTART_PHASE_8;
2033   signal->theData[10] = ZSTART_PHASE_9;
2034   signal->theData[11] = ZSTART_PHASE_END;
2035   sendSignal(NDBCNTR_REF, GSN_STTORRY, signal, 12, JBB);
2036 }//Ndbcntr::sendSttorry()
2037 
2038 void
execDUMP_STATE_ORD(Signal * signal)2039 Ndbcntr::execDUMP_STATE_ORD(Signal* signal)
2040 {
2041   DumpStateOrd * const & dumpState = (DumpStateOrd *)&signal->theData[0];
2042   Uint32 arg = dumpState->args[0];
2043 
2044   if(arg == 13){
2045     infoEvent("Cntr: cstartPhase = %d, cinternalStartphase = %d, block = %d",
2046 	      cstartPhase, cinternalStartphase, cndbBlocksCount);
2047     infoEvent("Cntr: cmasterNodeId = %d", cmasterNodeId);
2048   }
2049 
2050   if (arg == DumpStateOrd::NdbcntrTestStopOnError){
2051     if (m_ctx.m_config.stopOnError() == true)
2052       ((Configuration&)m_ctx.m_config).stopOnError(false);
2053 
2054     const BlockReference tblockref = calcNdbCntrBlockRef(getOwnNodeId());
2055 
2056     SystemError * const sysErr = (SystemError*)&signal->theData[0];
2057     sysErr->errorCode = SystemError::TestStopOnError;
2058     sysErr->errorRef = reference();
2059     sendSignal(tblockref, GSN_SYSTEM_ERROR, signal,
2060 	       SystemError::SignalLength, JBA);
2061   }
2062 
2063   if (arg == DumpStateOrd::NdbcntrStopNodes)
2064   {
2065     NdbNodeBitmask mask;
2066     for(Uint32 i = 1; i<signal->getLength(); i++)
2067       mask.set(signal->theData[i]);
2068 
2069     StopReq* req = (StopReq*)signal->getDataPtrSend();
2070     req->senderRef = RNIL;
2071     req->senderData = 123;
2072     req->requestInfo = 0;
2073     req->singleuser = 0;
2074     req->singleUserApi = 0;
2075     mask.copyto(NdbNodeBitmask::Size, req->nodes);
2076     StopReq::setPerformRestart(req->requestInfo, 1);
2077     StopReq::setNoStart(req->requestInfo, 1);
2078     StopReq::setStopNodes(req->requestInfo, 1);
2079     StopReq::setStopAbort(req->requestInfo, 1);
2080 
2081     sendSignal(reference(), GSN_STOP_REQ, signal,
2082 	       StopReq::SignalLength, JBB);
2083     return;
2084   }
2085 
2086 }//Ndbcntr::execDUMP_STATE_ORD()
2087 
updateNodeState(Signal * signal,const NodeState & newState) const2088 void Ndbcntr::updateNodeState(Signal* signal, const NodeState& newState) const{
2089   NodeStateRep * const stateRep = (NodeStateRep *)&signal->theData[0];
2090 
2091   if (newState.startLevel == NodeState::SL_STARTED)
2092   {
2093     CRASH_INSERTION(1000);
2094   }
2095 
2096   stateRep->nodeState = newState;
2097   stateRep->nodeState.masterNodeId = cmasterNodeId;
2098   stateRep->nodeState.setNodeGroup(c_nodeGroup);
2099 
2100   for(Uint32 i = 0; i<ALL_BLOCKS_SZ; i++){
2101     sendSignal(ALL_BLOCKS[i].Ref, GSN_NODE_STATE_REP, signal,
2102 	       NodeStateRep::SignalLength, JBB);
2103   }
2104 }
2105 
2106 void
execRESUME_REQ(Signal * signal)2107 Ndbcntr::execRESUME_REQ(Signal* signal){
2108   //ResumeReq * const req = (ResumeReq *)&signal->theData[0];
2109   //ResumeRef * const ref = (ResumeRef *)&signal->theData[0];
2110 
2111   jamEntry();
2112 
2113   signal->theData[0] = NDB_LE_SingleUser;
2114   signal->theData[1] = 2;
2115   sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 2, JBB);
2116 
2117   //Uint32 senderData = req->senderData;
2118   //BlockReference senderRef = req->senderRef;
2119   NodeState newState(NodeState::SL_STARTED);
2120   updateNodeState(signal, newState);
2121   c_stopRec.stopReq.senderRef=0;
2122 }
2123 
2124 void
execSTOP_REQ(Signal * signal)2125 Ndbcntr::execSTOP_REQ(Signal* signal){
2126   StopReq * const req = (StopReq *)&signal->theData[0];
2127   StopRef * const ref = (StopRef *)&signal->theData[0];
2128   Uint32 singleuser  = req->singleuser;
2129   jamEntry();
2130   Uint32 senderData = req->senderData;
2131   BlockReference senderRef = req->senderRef;
2132   bool abort = StopReq::getStopAbort(req->requestInfo);
2133   bool stopnodes = StopReq::getStopNodes(req->requestInfo);
2134 
2135   if(!singleuser &&
2136      (getNodeState().startLevel < NodeState::SL_STARTED ||
2137       (abort && !stopnodes)))
2138   {
2139     /**
2140      * Node is not started yet
2141      *
2142      * So stop it quickly
2143      */
2144     jam();
2145     const Uint32 reqInfo = req->requestInfo;
2146     if(StopReq::getPerformRestart(reqInfo)){
2147       jam();
2148       StartOrd * startOrd = (StartOrd *)&signal->theData[0];
2149       startOrd->restartInfo = reqInfo;
2150       sendSignal(CMVMI_REF, GSN_START_ORD, signal, 1, JBA);
2151     } else {
2152       jam();
2153       sendSignal(CMVMI_REF, GSN_STOP_ORD, signal, 1, JBA);
2154     }
2155     return;
2156   }
2157 
2158   if(c_stopRec.stopReq.senderRef != 0 ||
2159      (cmasterNodeId == getOwnNodeId() && !c_start.m_starting.isclear()))
2160   {
2161     /**
2162      * Requested a system shutdown
2163      */
2164     if(!singleuser && StopReq::getSystemStop(req->requestInfo)){
2165       jam();
2166       sendSignalWithDelay(reference(), GSN_STOP_REQ, signal, 100,
2167 			  StopReq::SignalLength);
2168       return;
2169     }
2170 
2171     /**
2172      * Requested a node shutdown
2173      */
2174     if(c_stopRec.stopReq.senderRef &&
2175        StopReq::getSystemStop(c_stopRec.stopReq.requestInfo))
2176       ref->errorCode = StopRef::SystemShutdownInProgress;
2177     else
2178       ref->errorCode = StopRef::NodeShutdownInProgress;
2179     ref->senderData = senderData;
2180     ref->masterNodeId = cmasterNodeId;
2181 
2182     if (senderRef != RNIL)
2183       sendSignal(senderRef, GSN_STOP_REF, signal, StopRef::SignalLength, JBB);
2184     return;
2185   }
2186 
2187   if (stopnodes && !abort)
2188   {
2189     jam();
2190     ref->errorCode = StopRef::UnsupportedNodeShutdown;
2191     ref->senderData = senderData;
2192     ref->masterNodeId = cmasterNodeId;
2193     if (senderRef != RNIL)
2194       sendSignal(senderRef, GSN_STOP_REF, signal, StopRef::SignalLength, JBB);
2195     return;
2196   }
2197 
2198   if (stopnodes && cmasterNodeId != getOwnNodeId())
2199   {
2200     jam();
2201     ref->errorCode = StopRef::MultiNodeShutdownNotMaster;
2202     ref->senderData = senderData;
2203     ref->masterNodeId = cmasterNodeId;
2204     if (senderRef != RNIL)
2205       sendSignal(senderRef, GSN_STOP_REF, signal, StopRef::SignalLength, JBB);
2206     return;
2207   }
2208 
2209   c_stopRec.stopReq = * req;
2210   c_stopRec.stopInitiatedTime = NdbTick_CurrentMillisecond();
2211 
2212   if (stopnodes)
2213   {
2214     jam();
2215 
2216     if(!c_stopRec.checkNodeFail(signal))
2217     {
2218       jam();
2219       return;
2220     }
2221 
2222     char buf[100];
2223     NdbNodeBitmask mask;
2224     mask.assign(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
2225     infoEvent("Initiating shutdown abort of %s", mask.getText(buf));
2226     ndbout_c("Initiating shutdown abort of %s", mask.getText(buf));
2227 
2228     WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
2229     req->senderRef = reference();
2230     req->senderData = StopRecord::SR_BLOCK_GCP_START_GCP;
2231     req->requestType = WaitGCPReq::BlockStartGcp;
2232     sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
2233 	       WaitGCPReq::SignalLength, JBB);
2234     return;
2235   }
2236   else if(!singleuser)
2237   {
2238     if(StopReq::getSystemStop(c_stopRec.stopReq.requestInfo))
2239     {
2240       jam();
2241       if(StopReq::getPerformRestart(c_stopRec.stopReq.requestInfo))
2242       {
2243 	((Configuration&)m_ctx.m_config).stopOnError(false);
2244       }
2245     }
2246     if(!c_stopRec.checkNodeFail(signal))
2247     {
2248       jam();
2249       return;
2250     }
2251     signal->theData[0] = NDB_LE_NDBStopStarted;
2252     signal->theData[1] = StopReq::getSystemStop(c_stopRec.stopReq.requestInfo) ? 1 : 0;
2253     sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 2, JBB);
2254   }
2255   else
2256   {
2257     signal->theData[0] = NDB_LE_SingleUser;
2258     signal->theData[1] = 0;
2259     sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 2, JBB);
2260   }
2261 
2262   NodeState newState(NodeState::SL_STOPPING_1,
2263 		     StopReq::getSystemStop(c_stopRec.stopReq.requestInfo));
2264 
2265    if(singleuser) {
2266      newState.setSingleUser(true);
2267      newState.setSingleUserApi(c_stopRec.stopReq.singleUserApi);
2268    }
2269   updateNodeState(signal, newState);
2270   signal->theData[0] = ZSHUTDOWN;
2271   sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 100, 1);
2272 }
2273 
2274 void
checkTimeout(Signal * signal)2275 Ndbcntr::StopRecord::checkTimeout(Signal* signal){
2276   jamEntry();
2277 
2278   if(!cntr.getNodeState().getSingleUserMode())
2279     if(!checkNodeFail(signal)){
2280       jam();
2281       return;
2282     }
2283 
2284   switch(cntr.getNodeState().startLevel){
2285   case NodeState::SL_STOPPING_1:
2286     checkApiTimeout(signal);
2287     break;
2288   case NodeState::SL_STOPPING_2:
2289     checkTcTimeout(signal);
2290     break;
2291   case NodeState::SL_STOPPING_3:
2292     checkLqhTimeout_1(signal);
2293     break;
2294   case NodeState::SL_STOPPING_4:
2295     checkLqhTimeout_2(signal);
2296     break;
2297   case NodeState::SL_SINGLEUSER:
2298     break;
2299   default:
2300     ndbrequire(false);
2301   }
2302 }
2303 
2304 bool
checkNodeFail(Signal * signal)2305 Ndbcntr::StopRecord::checkNodeFail(Signal* signal){
2306   jam();
2307   if(StopReq::getSystemStop(stopReq.requestInfo)){
2308     jam();
2309     return true;
2310   }
2311 
2312   /**
2313    * Check if I can survive me stopping
2314    */
2315   NodeBitmask ndbMask;
2316   ndbMask.assign(cntr.c_startedNodes);
2317 
2318   if (StopReq::getStopNodes(stopReq.requestInfo))
2319   {
2320     NdbNodeBitmask tmp;
2321     tmp.assign(NdbNodeBitmask::Size, stopReq.nodes);
2322 
2323     NdbNodeBitmask ndbStopNodes;
2324     ndbStopNodes.assign(NdbNodeBitmask::Size, stopReq.nodes);
2325     ndbStopNodes.bitAND(ndbMask);
2326     ndbStopNodes.copyto(NdbNodeBitmask::Size, stopReq.nodes);
2327 
2328     ndbMask.bitANDC(tmp);
2329 
2330     bool allNodesStopped = true;
2331     int i ;
2332     for( i = 0; i < (int) NdbNodeBitmask::Size; i++ ){
2333       if ( stopReq.nodes[i] != 0 ){
2334         allNodesStopped = false;
2335         break;
2336       }
2337     }
2338 
2339     if ( allNodesStopped ) {
2340       StopConf * const stopConf = (StopConf *)&signal->theData[0];
2341       stopConf->senderData = stopReq.senderData;
2342       stopConf->nodeState  = (Uint32) NodeState::SL_NOTHING;
2343       cntr.sendSignal(stopReq.senderRef, GSN_STOP_CONF, signal,
2344                        StopConf::SignalLength, JBB);
2345       stopReq.senderRef = 0;
2346       return false;
2347     }
2348 
2349   }
2350   else
2351   {
2352     ndbMask.clear(cntr.getOwnNodeId());
2353   }
2354 
2355   CheckNodeGroups* sd = (CheckNodeGroups*)&signal->theData[0];
2356   sd->blockRef = cntr.reference();
2357   sd->requestType = CheckNodeGroups::Direct | CheckNodeGroups::ArbitCheck;
2358   sd->mask = ndbMask;
2359   cntr.EXECUTE_DIRECT(DBDIH, GSN_CHECKNODEGROUPSREQ, signal,
2360 		      CheckNodeGroups::SignalLength);
2361   jamEntry();
2362   switch (sd->output) {
2363   case CheckNodeGroups::Win:
2364   case CheckNodeGroups::Partitioning:
2365     return true;
2366     break;
2367   }
2368 
2369   StopRef * const ref = (StopRef *)&signal->theData[0];
2370 
2371   ref->senderData = stopReq.senderData;
2372   ref->errorCode = StopRef::NodeShutdownWouldCauseSystemCrash;
2373   ref->masterNodeId = cntr.cmasterNodeId;
2374 
2375   const BlockReference bref = stopReq.senderRef;
2376   if (bref != RNIL)
2377     cntr.sendSignal(bref, GSN_STOP_REF, signal, StopRef::SignalLength, JBB);
2378 
2379   stopReq.senderRef = 0;
2380 
2381   if (cntr.getNodeState().startLevel != NodeState::SL_SINGLEUSER)
2382   {
2383     NodeState newState(NodeState::SL_STARTED);
2384     cntr.updateNodeState(signal, newState);
2385   }
2386 
2387   signal->theData[0] = NDB_LE_NDBStopAborted;
2388   cntr.sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 1, JBB);
2389 
2390   return false;
2391 }
2392 
2393 void
checkApiTimeout(Signal * signal)2394 Ndbcntr::StopRecord::checkApiTimeout(Signal* signal){
2395   const Int32 timeout = stopReq.apiTimeout;
2396   const NDB_TICKS alarm = stopInitiatedTime + (NDB_TICKS)timeout;
2397   const NDB_TICKS now = NdbTick_CurrentMillisecond();
2398   if((timeout >= 0 && now >= alarm)){
2399     // || checkWithApiInSomeMagicWay)
2400     jam();
2401     NodeState newState(NodeState::SL_STOPPING_2,
2402 		       StopReq::getSystemStop(stopReq.requestInfo));
2403     if(stopReq.singleuser) {
2404       newState.setSingleUser(true);
2405       newState.setSingleUserApi(stopReq.singleUserApi);
2406     }
2407     cntr.updateNodeState(signal, newState);
2408 
2409     stopInitiatedTime = now;
2410   }
2411 
2412   signal->theData[0] = ZSHUTDOWN;
2413   cntr.sendSignalWithDelay(cntr.reference(), GSN_CONTINUEB, signal, 100, 1);
2414 }
2415 
2416 void
checkTcTimeout(Signal * signal)2417 Ndbcntr::StopRecord::checkTcTimeout(Signal* signal){
2418   const Int32 timeout = stopReq.transactionTimeout;
2419   const NDB_TICKS alarm = stopInitiatedTime + (NDB_TICKS)timeout;
2420   const NDB_TICKS now = NdbTick_CurrentMillisecond();
2421   if((timeout >= 0 && now >= alarm)){
2422     // || checkWithTcInSomeMagicWay)
2423     jam();
2424     if(stopReq.getSystemStop(stopReq.requestInfo)  || stopReq.singleuser){
2425       jam();
2426       if(stopReq.singleuser)
2427       {
2428 	jam();
2429 	AbortAllReq * req = (AbortAllReq*)&signal->theData[0];
2430 	req->senderRef = cntr.reference();
2431 	req->senderData = 12;
2432 	cntr.sendSignal(DBTC_REF, GSN_ABORT_ALL_REQ, signal,
2433 			AbortAllReq::SignalLength, JBB);
2434       }
2435       else
2436       {
2437 	WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
2438 	req->senderRef = cntr.reference();
2439 	req->senderData = StopRecord::SR_CLUSTER_SHUTDOWN;
2440 	req->requestType = WaitGCPReq::CompleteForceStart;
2441 	cntr.sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
2442 			WaitGCPReq::SignalLength, JBB);
2443       }
2444     } else {
2445       jam();
2446       StopPermReq * req = (StopPermReq*)&signal->theData[0];
2447       req->senderRef = cntr.reference();
2448       req->senderData = 12;
2449       cntr.sendSignal(DBDIH_REF, GSN_STOP_PERM_REQ, signal,
2450 		      StopPermReq::SignalLength, JBB);
2451     }
2452     return;
2453   }
2454   signal->theData[0] = ZSHUTDOWN;
2455   cntr.sendSignalWithDelay(cntr.reference(), GSN_CONTINUEB, signal, 100, 1);
2456 }
2457 
execSTOP_PERM_REF(Signal * signal)2458 void Ndbcntr::execSTOP_PERM_REF(Signal* signal){
2459   //StopPermRef* const ref = (StopPermRef*)&signal->theData[0];
2460 
2461   jamEntry();
2462 
2463   signal->theData[0] = ZSHUTDOWN;
2464   sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 100, 1);
2465 }
2466 
execSTOP_PERM_CONF(Signal * signal)2467 void Ndbcntr::execSTOP_PERM_CONF(Signal* signal){
2468   jamEntry();
2469 
2470   AbortAllReq * req = (AbortAllReq*)&signal->theData[0];
2471   req->senderRef = reference();
2472   req->senderData = 12;
2473   sendSignal(DBTC_REF, GSN_ABORT_ALL_REQ, signal,
2474 	     AbortAllReq::SignalLength, JBB);
2475 }
2476 
execABORT_ALL_CONF(Signal * signal)2477 void Ndbcntr::execABORT_ALL_CONF(Signal* signal){
2478   jamEntry();
2479   if(c_stopRec.stopReq.singleuser) {
2480     jam();
2481 
2482     NodeState newState(NodeState::SL_SINGLEUSER);
2483     newState.setSingleUser(true);
2484     newState.setSingleUserApi(c_stopRec.stopReq.singleUserApi);
2485     updateNodeState(signal, newState);
2486     c_stopRec.stopInitiatedTime = NdbTick_CurrentMillisecond();
2487 
2488     StopConf * const stopConf = (StopConf *)&signal->theData[0];
2489     stopConf->senderData = c_stopRec.stopReq.senderData;
2490     stopConf->nodeState  = (Uint32) NodeState::SL_SINGLEUSER;
2491     sendSignal(c_stopRec.stopReq.senderRef, GSN_STOP_CONF, signal, StopConf::SignalLength, JBB);
2492 
2493     c_stopRec.stopReq.senderRef = 0; // the command is done
2494 
2495     signal->theData[0] = NDB_LE_SingleUser;
2496     signal->theData[1] = 1;
2497     signal->theData[2] = c_stopRec.stopReq.singleUserApi;
2498     sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 3, JBB);
2499   }
2500   else
2501     {
2502       jam();
2503       NodeState newState(NodeState::SL_STOPPING_3,
2504 			 StopReq::getSystemStop(c_stopRec.stopReq.requestInfo));
2505       updateNodeState(signal, newState);
2506 
2507       c_stopRec.stopInitiatedTime = NdbTick_CurrentMillisecond();
2508 
2509       signal->theData[0] = ZSHUTDOWN;
2510       sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 100, 1);
2511     }
2512 }
2513 
execABORT_ALL_REF(Signal * signal)2514 void Ndbcntr::execABORT_ALL_REF(Signal* signal){
2515   jamEntry();
2516 
2517   StopRef * const stopRef = (StopRef *)&signal->theData[0];
2518   stopRef->senderData = c_stopRec.stopReq.senderData;
2519   stopRef->errorCode = StopRef::TransactionAbortFailed;
2520   stopRef->masterNodeId = cmasterNodeId;
2521   sendSignal(c_stopRec.stopReq.senderRef, GSN_STOP_REF, signal, StopRef::SignalLength, JBB);
2522 }
2523 
2524 void
checkLqhTimeout_1(Signal * signal)2525 Ndbcntr::StopRecord::checkLqhTimeout_1(Signal* signal){
2526   const Int32 timeout = stopReq.readOperationTimeout;
2527   const NDB_TICKS alarm = stopInitiatedTime + (NDB_TICKS)timeout;
2528   const NDB_TICKS now = NdbTick_CurrentMillisecond();
2529 
2530   if((timeout >= 0 && now >= alarm)){
2531     // || checkWithLqhInSomeMagicWay)
2532     jam();
2533 
2534     ChangeNodeStateReq * req = (ChangeNodeStateReq*)&signal->theData[0];
2535 
2536     NodeState newState(NodeState::SL_STOPPING_4,
2537 		       StopReq::getSystemStop(stopReq.requestInfo));
2538     req->nodeState = newState;
2539     req->senderRef = cntr.reference();
2540     req->senderData = 12;
2541     cntr.sendSignal(DBLQH_REF, GSN_CHANGE_NODE_STATE_REQ, signal, 2, JBB);
2542     return;
2543   }
2544   signal->theData[0] = ZSHUTDOWN;
2545   cntr.sendSignalWithDelay(cntr.reference(), GSN_CONTINUEB, signal, 100, 1);
2546 }
2547 
execCHANGE_NODE_STATE_CONF(Signal * signal)2548 void Ndbcntr::execCHANGE_NODE_STATE_CONF(Signal* signal){
2549   jamEntry();
2550   signal->theData[0] = reference();
2551   signal->theData[1] = 12;
2552   sendSignal(DBDIH_REF, GSN_STOP_ME_REQ, signal, 2, JBB);
2553 }
2554 
execSTOP_ME_REF(Signal * signal)2555 void Ndbcntr::execSTOP_ME_REF(Signal* signal){
2556   jamEntry();
2557   ndbrequire(false);
2558 }
2559 
2560 
execSTOP_ME_CONF(Signal * signal)2561 void Ndbcntr::execSTOP_ME_CONF(Signal* signal){
2562   jamEntry();
2563 
2564   NodeState newState(NodeState::SL_STOPPING_4,
2565 		     StopReq::getSystemStop(c_stopRec.stopReq.requestInfo));
2566   updateNodeState(signal, newState);
2567 
2568   c_stopRec.stopInitiatedTime = NdbTick_CurrentMillisecond();
2569   signal->theData[0] = ZSHUTDOWN;
2570   sendSignalWithDelay(reference(), GSN_CONTINUEB, signal, 100, 1);
2571 }
2572 
2573 void
checkLqhTimeout_2(Signal * signal)2574 Ndbcntr::StopRecord::checkLqhTimeout_2(Signal* signal){
2575   const Int32 timeout = stopReq.operationTimeout;
2576   const NDB_TICKS alarm = stopInitiatedTime + (NDB_TICKS)timeout;
2577   const NDB_TICKS now = NdbTick_CurrentMillisecond();
2578 
2579   if((timeout >= 0 && now >= alarm)){
2580     // || checkWithLqhInSomeMagicWay)
2581     jam();
2582     if(StopReq::getPerformRestart(stopReq.requestInfo)){
2583       jam();
2584       StartOrd * startOrd = (StartOrd *)&signal->theData[0];
2585       startOrd->restartInfo = stopReq.requestInfo;
2586       cntr.sendSignal(CMVMI_REF, GSN_START_ORD, signal, 2, JBA);
2587     } else {
2588       jam();
2589       cntr.sendSignal(CMVMI_REF, GSN_STOP_ORD, signal, 1, JBA);
2590     }
2591     return;
2592   }
2593   signal->theData[0] = ZSHUTDOWN;
2594   cntr.sendSignalWithDelay(cntr.reference(), GSN_CONTINUEB, signal, 100, 1);
2595 }
2596 
execWAIT_GCP_REF(Signal * signal)2597 void Ndbcntr::execWAIT_GCP_REF(Signal* signal){
2598   jamEntry();
2599 
2600   //WaitGCPRef* const ref = (WaitGCPRef*)&signal->theData[0];
2601 
2602   WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
2603   req->senderRef = reference();
2604   req->senderData = StopRecord::SR_CLUSTER_SHUTDOWN;
2605   req->requestType = WaitGCPReq::CompleteForceStart;
2606   sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
2607 	     WaitGCPReq::SignalLength, JBB);
2608 }
2609 
execWAIT_GCP_CONF(Signal * signal)2610 void Ndbcntr::execWAIT_GCP_CONF(Signal* signal){
2611   jamEntry();
2612 
2613   WaitGCPConf* conf = (WaitGCPConf*)signal->getDataPtr();
2614 
2615   switch(conf->senderData){
2616   case StopRecord::SR_BLOCK_GCP_START_GCP:
2617   {
2618     jam();
2619     /**
2620      *
2621      */
2622     if(!c_stopRec.checkNodeFail(signal))
2623     {
2624       jam();
2625       goto unblock;
2626     }
2627 
2628     WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
2629     req->senderRef = reference();
2630     req->senderData = StopRecord::SR_WAIT_COMPLETE_GCP;
2631     req->requestType = WaitGCPReq::CompleteIfRunning;
2632 
2633     sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
2634 	       WaitGCPReq::SignalLength, JBB);
2635     return;
2636   }
2637   case StopRecord::SR_UNBLOCK_GCP_START_GCP:
2638   {
2639     jam();
2640     return;
2641   }
2642   case StopRecord::SR_WAIT_COMPLETE_GCP:
2643   {
2644     jam();
2645     if(!c_stopRec.checkNodeFail(signal))
2646     {
2647       jam();
2648       goto unblock;
2649     }
2650 
2651     NdbNodeBitmask tmp;
2652     tmp.assign(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
2653     c_stopRec.m_stop_req_counter = tmp;
2654     NodeReceiverGroup rg(QMGR, tmp);
2655     StopReq * stopReq = (StopReq *)&signal->theData[0];
2656     * stopReq = c_stopRec.stopReq;
2657     stopReq->senderRef = reference();
2658     sendSignal(rg, GSN_STOP_REQ, signal, StopReq::SignalLength, JBA);
2659     c_stopRec.m_state = StopRecord::SR_QMGR_STOP_REQ;
2660     return;
2661   }
2662   case StopRecord::SR_CLUSTER_SHUTDOWN:
2663   {
2664     jam();
2665     break;
2666   }
2667   }
2668 
2669   {
2670     ndbrequire(StopReq::getSystemStop(c_stopRec.stopReq.requestInfo));
2671     NodeState newState(NodeState::SL_STOPPING_3, true);
2672 
2673     /**
2674      * Inform QMGR so that arbitrator won't kill us
2675      */
2676     NodeStateRep * rep = (NodeStateRep *)&signal->theData[0];
2677     rep->nodeState = newState;
2678     rep->nodeState.masterNodeId = cmasterNodeId;
2679     rep->nodeState.setNodeGroup(c_nodeGroup);
2680     EXECUTE_DIRECT(QMGR, GSN_NODE_STATE_REP, signal,
2681 		   NodeStateRep::SignalLength);
2682 
2683     if(StopReq::getPerformRestart(c_stopRec.stopReq.requestInfo)){
2684       jam();
2685       StartOrd * startOrd = (StartOrd *)&signal->theData[0];
2686       startOrd->restartInfo = c_stopRec.stopReq.requestInfo;
2687       sendSignalWithDelay(CMVMI_REF, GSN_START_ORD, signal, 500,
2688 			  StartOrd::SignalLength);
2689     } else {
2690       jam();
2691       sendSignalWithDelay(CMVMI_REF, GSN_STOP_ORD, signal, 500, 1);
2692     }
2693     return;
2694   }
2695 
2696 unblock:
2697   WaitGCPReq * req = (WaitGCPReq*)&signal->theData[0];
2698   req->senderRef = reference();
2699   req->senderData = StopRecord::SR_UNBLOCK_GCP_START_GCP;
2700   req->requestType = WaitGCPReq::UnblockStartGcp;
2701   sendSignal(DBDIH_REF, GSN_WAIT_GCP_REQ, signal,
2702 	     WaitGCPReq::SignalLength, JBB);
2703 }
2704 
2705 void
execSTOP_CONF(Signal * signal)2706 Ndbcntr::execSTOP_CONF(Signal* signal)
2707 {
2708   jamEntry();
2709   StopConf *conf = (StopConf*)signal->getDataPtr();
2710   ndbrequire(c_stopRec.m_state == StopRecord::SR_QMGR_STOP_REQ);
2711   c_stopRec.m_stop_req_counter.clearWaitingFor(conf->nodeId);
2712   if (c_stopRec.m_stop_req_counter.done())
2713   {
2714     char buf[100];
2715     NdbNodeBitmask mask;
2716     mask.assign(NdbNodeBitmask::Size, c_stopRec.stopReq.nodes);
2717     infoEvent("Stopping of %s", mask.getText(buf));
2718     ndbout_c("Stopping of %s", mask.getText(buf));
2719 
2720     /**
2721      * Kill any node...
2722      */
2723     FailRep * const failRep = (FailRep *)&signal->theData[0];
2724     failRep->failCause = FailRep::ZMULTI_NODE_SHUTDOWN;
2725     NodeReceiverGroup rg(QMGR, c_clusterNodes);
2726     Uint32 nodeId = 0;
2727     while ((nodeId = NdbNodeBitmask::find(c_stopRec.stopReq.nodes, nodeId+1))
2728 	   != NdbNodeBitmask::NotFound)
2729     {
2730       failRep->failNodeId = nodeId;
2731       sendSignal(rg, GSN_FAIL_REP, signal, FailRep::SignalLength, JBA);
2732     }
2733     c_stopRec.m_state = StopRecord::SR_WAIT_NODE_FAILURES;
2734     return;
2735   }
2736 }
2737 
execSTTORRY(Signal * signal)2738 void Ndbcntr::execSTTORRY(Signal* signal){
2739   jamEntry();
2740   c_missra.execSTTORRY(signal);
2741 }
2742 
execREAD_CONFIG_CONF(Signal * signal)2743 void Ndbcntr::execREAD_CONFIG_CONF(Signal* signal){
2744   jamEntry();
2745   c_missra.execREAD_CONFIG_CONF(signal);
2746 }
2747 
execSTART_ORD(Signal * signal)2748 void Ndbcntr::execSTART_ORD(Signal* signal){
2749   jamEntry();
2750   c_missra.execSTART_ORD(signal);
2751 }
2752 
2753 #define CLEAR_DX 13
2754 #define CLEAR_LCP 3
2755 
2756 void
clearFilesystem(Signal * signal)2757 Ndbcntr::clearFilesystem(Signal* signal)
2758 {
2759   const Uint32 lcp = c_fsRemoveCount >= CLEAR_DX;
2760 
2761   FsRemoveReq * req  = (FsRemoveReq *)signal->getDataPtrSend();
2762   req->userReference = reference();
2763   req->userPointer   = 0;
2764   req->directory     = 1;
2765   req->ownDirectory  = 1;
2766 
2767   if (lcp == 0)
2768   {
2769     FsOpenReq::setVersion(req->fileNumber, 3);
2770     FsOpenReq::setSuffix(req->fileNumber, FsOpenReq::S_CTL); // Can by any...
2771     FsOpenReq::v1_setDisk(req->fileNumber, c_fsRemoveCount);
2772   }
2773   else
2774   {
2775     FsOpenReq::setVersion(req->fileNumber, 5);
2776     FsOpenReq::setSuffix(req->fileNumber, FsOpenReq::S_DATA);
2777     FsOpenReq::v5_setLcpNo(req->fileNumber, c_fsRemoveCount - CLEAR_DX);
2778     FsOpenReq::v5_setTableId(req->fileNumber, 0);
2779     FsOpenReq::v5_setFragmentId(req->fileNumber, 0);
2780   }
2781   sendSignal(NDBFS_REF, GSN_FSREMOVEREQ, signal,
2782              FsRemoveReq::SignalLength, JBA);
2783   c_fsRemoveCount++;
2784 }
2785 
2786 void
execFSREMOVECONF(Signal * signal)2787 Ndbcntr::execFSREMOVECONF(Signal* signal){
2788   jamEntry();
2789   if(c_fsRemoveCount == CLEAR_DX + CLEAR_LCP){
2790     jam();
2791     sendSttorry(signal);
2792   } else {
2793     jam();
2794     ndbrequire(c_fsRemoveCount < CLEAR_DX + CLEAR_LCP);
2795     clearFilesystem(signal);
2796   }//if
2797 }
2798 
execSTART_ORD(Signal * signal)2799 void Ndbcntr::Missra::execSTART_ORD(Signal* signal){
2800   signal->theData[0] = NDB_LE_NDBStartStarted;
2801   signal->theData[1] = NDB_VERSION;
2802   cntr.sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 2, JBB);
2803 
2804   currentBlockIndex = 0;
2805   sendNextREAD_CONFIG_REQ(signal);
2806 }
2807 
sendNextREAD_CONFIG_REQ(Signal * signal)2808 void Ndbcntr::Missra::sendNextREAD_CONFIG_REQ(Signal* signal){
2809 
2810   if(currentBlockIndex < ALL_BLOCKS_SZ){
2811     jam();
2812 
2813     ReadConfigReq * req = (ReadConfigReq*)signal->getDataPtrSend();
2814     req->senderData = 0;
2815     req->senderRef = cntr.reference();
2816     req->noOfParameters = 0;
2817 
2818     const BlockReference ref = readConfigOrder[currentBlockIndex];
2819 
2820 #if 0
2821     ndbout_c("sending READ_CONFIG_REQ to %s(ref=%x index=%d)",
2822 	     getBlockName( refToBlock(ref)),
2823 	     ref,
2824 	     currentBlockIndex);
2825 #endif
2826 
2827     cntr.sendSignal(ref, GSN_READ_CONFIG_REQ, signal,
2828 		    ReadConfigReq::SignalLength, JBB);
2829     return;
2830   }
2831 
2832   /**
2833    * Finished...
2834    */
2835   currentStartPhase = 0;
2836   for(Uint32 i = 0; i<ALL_BLOCKS_SZ; i++){
2837     if(ALL_BLOCKS[i].NextSP < currentStartPhase)
2838       currentStartPhase = ALL_BLOCKS[i].NextSP;
2839   }
2840 
2841   currentBlockIndex = 0;
2842   sendNextSTTOR(signal);
2843 }
2844 
execREAD_CONFIG_CONF(Signal * signal)2845 void Ndbcntr::Missra::execREAD_CONFIG_CONF(Signal* signal){
2846   const ReadConfigConf * conf = (ReadConfigConf*)signal->getDataPtr();
2847 
2848   const Uint32 ref = conf->senderRef;
2849   ndbrequire(refToBlock(readConfigOrder[currentBlockIndex])
2850 	     == refToBlock(ref));
2851 
2852   currentBlockIndex++;
2853   sendNextREAD_CONFIG_REQ(signal);
2854 }
2855 
execSTTORRY(Signal * signal)2856 void Ndbcntr::Missra::execSTTORRY(Signal* signal){
2857   const BlockReference ref = signal->senderBlockRef();
2858   ndbrequire(refToBlock(ref) == refToBlock(ALL_BLOCKS[currentBlockIndex].Ref));
2859 
2860   /**
2861    * Update next start phase
2862    */
2863   for (Uint32 i = 3; i < 25; i++){
2864     jam();
2865     if (signal->theData[i] > currentStartPhase){
2866       jam();
2867       ALL_BLOCKS[currentBlockIndex].NextSP = signal->theData[i];
2868       break;
2869     }
2870   }
2871 
2872   currentBlockIndex++;
2873   sendNextSTTOR(signal);
2874 }
2875 
sendNextSTTOR(Signal * signal)2876 void Ndbcntr::Missra::sendNextSTTOR(Signal* signal){
2877 
2878   for(; currentStartPhase < 255 ;
2879       currentStartPhase++, g_currentStartPhase = currentStartPhase){
2880     jam();
2881 
2882     const Uint32 start = currentBlockIndex;
2883 
2884     if (currentStartPhase == ZSTART_PHASE_6)
2885     {
2886       // Ndbd has passed the critical startphases.
2887       // Change error handler from "startup" state
2888       // to normal state.
2889       ErrorReporter::setErrorHandlerShutdownType();
2890     }
2891 
2892     for(; currentBlockIndex < ALL_BLOCKS_SZ; currentBlockIndex++){
2893       jam();
2894       if(ALL_BLOCKS[currentBlockIndex].NextSP == currentStartPhase){
2895 	jam();
2896 	signal->theData[0] = 0;
2897 	signal->theData[1] = currentStartPhase;
2898 	signal->theData[2] = 0;
2899 	signal->theData[3] = 0;
2900 	signal->theData[4] = 0;
2901 	signal->theData[5] = 0;
2902 	signal->theData[6] = 0;
2903 	signal->theData[7] = cntr.ctypeOfStart;
2904 
2905 	const BlockReference ref = ALL_BLOCKS[currentBlockIndex].Ref;
2906 
2907 #ifdef MAX_STARTPHASE
2908 	ndbrequire(currentStartPhase <= MAX_STARTPHASE);
2909 #endif
2910 
2911 #ifdef TRACE_STTOR
2912 	ndbout_c("sending STTOR(%d) to %s(ref=%x index=%d)",
2913 		 currentStartPhase,
2914 		 getBlockName( refToBlock(ref)),
2915 		 ref,
2916 		 currentBlockIndex);
2917 #endif
2918 
2919 	cntr.sendSignal(ref, GSN_STTOR, signal, 8, JBB);
2920 
2921 	return;
2922       }
2923     }
2924 
2925     currentBlockIndex = 0;
2926 
2927     NodeState newState(NodeState::SL_STARTING, currentStartPhase,
2928 		       (NodeState::StartType)cntr.ctypeOfStart);
2929     cntr.updateNodeState(signal, newState);
2930 
2931     if(start != 0){
2932       /**
2933        * At least one wanted this start phase,  report it
2934        */
2935       jam();
2936       signal->theData[0] = NDB_LE_StartPhaseCompleted;
2937       signal->theData[1] = currentStartPhase;
2938       signal->theData[2] = cntr.ctypeOfStart;
2939       cntr.sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 3, JBB);
2940     }
2941   }
2942 
2943   signal->theData[0] = NDB_LE_NDBStartCompleted;
2944   signal->theData[1] = NDB_VERSION;
2945   cntr.sendSignal(CMVMI_REF, GSN_EVENT_REP, signal, 2, JBB);
2946 
2947   NodeState newState(NodeState::SL_STARTED);
2948   cntr.updateNodeState(signal, newState);
2949 
2950   /**
2951    * Backward
2952    */
2953   UpgradeStartup::sendCmAppChg(cntr, signal, 3); //RUN
2954 
2955   NdbNodeBitmask nodes = cntr.c_clusterNodes;
2956   Uint32 node = 0;
2957   while((node = nodes.find(node+1)) != NdbNodeBitmask::NotFound){
2958     if(cntr.getNodeInfo(node).m_version < MAKE_VERSION(3,5,0)){
2959       nodes.clear(node);
2960     }
2961   }
2962 
2963   NodeReceiverGroup rg(NDBCNTR, nodes);
2964   signal->theData[0] = cntr.getOwnNodeId();
2965   cntr.sendSignal(rg, GSN_CNTR_START_REP, signal, 1, JBB);
2966 }
2967 
2968 /**
2969  * Backward compatible code
2970  */
2971 void
sendCmAppChg(Ndbcntr & cntr,Signal * signal,Uint32 startLevel)2972 UpgradeStartup::sendCmAppChg(Ndbcntr& cntr, Signal* signal, Uint32 startLevel){
2973 
2974   if(cntr.getNodeInfo(cntr.cmasterNodeId).m_version >= MAKE_VERSION(3,5,0)){
2975     jam();
2976     return;
2977   }
2978 
2979   /**
2980    * Old NDB running
2981    */
2982 
2983   signal->theData[0] = startLevel;
2984   signal->theData[1] = cntr.getOwnNodeId();
2985   signal->theData[2] = 3 | ('N' << 8);
2986   signal->theData[3] = 'D' | ('B' << 8);
2987   signal->theData[4] = 0;
2988   signal->theData[5] = 0;
2989   signal->theData[6] = 0;
2990   signal->theData[7] = 0;
2991   signal->theData[8] = 0;
2992   signal->theData[9] = 0;
2993   signal->theData[10] = 0;
2994   signal->theData[11] = 0;
2995 
2996   NdbNodeBitmask nodes = cntr.c_clusterNodes;
2997   nodes.clear(cntr.getOwnNodeId());
2998   Uint32 node = 0;
2999   while((node = nodes.find(node+1)) != NdbNodeBitmask::NotFound){
3000     if(cntr.getNodeInfo(node).m_version < MAKE_VERSION(3,5,0)){
3001       cntr.sendSignal(cntr.calcQmgrBlockRef(node),
3002 		      GSN_CM_APPCHG, signal, 12, JBB);
3003     } else {
3004       cntr.c_startedNodes.set(node); // Fake started
3005     }
3006   }
3007 }
3008 
3009 void
execCM_APPCHG(SimulatedBlock & block,Signal * signal)3010 UpgradeStartup::execCM_APPCHG(SimulatedBlock & block, Signal* signal){
3011   Uint32 state = signal->theData[0];
3012   Uint32 nodeId = signal->theData[1];
3013   if(block.number() == QMGR){
3014     Ndbcntr& cntr = * (Ndbcntr*)globalData.getBlock(CNTR);
3015     switch(state){
3016     case 0: // ZADD
3017       break;
3018     case 2: // ZSTART
3019       break;
3020     case 3: // ZRUN{
3021       cntr.c_startedNodes.set(nodeId);
3022 
3023       Uint32 recv = cntr.c_startedNodes.count();
3024       Uint32 cnt = cntr.c_clusterNodes.count();
3025       if(recv + 1 == cnt){ //+1 == own node
3026 	/**
3027 	 * Check master
3028 	 */
3029 	sendCntrMasterReq(cntr, signal, 0);
3030       }
3031       return;
3032     }
3033   }
3034   block.progError(__LINE__,NDBD_EXIT_NDBREQUIRE,
3035 		  "UpgradeStartup::execCM_APPCHG");
3036 }
3037 
3038 void
sendCntrMasterReq(Ndbcntr & cntr,Signal * signal,Uint32 n)3039 UpgradeStartup::sendCntrMasterReq(Ndbcntr& cntr, Signal* signal, Uint32 n){
3040   Uint32 node = cntr.c_startedNodes.find(n);
3041   if(node != NdbNodeBitmask::NotFound &&
3042      (node == cntr.getOwnNodeId() ||
3043       cntr.getNodeInfo(node).m_version >= MAKE_VERSION(3,5,0))){
3044     node = cntr.c_startedNodes.find(node+1);
3045   }
3046 
3047   if(node == NdbNodeBitmask::NotFound){
3048     cntr.progError(__LINE__,NDBD_EXIT_NDBREQUIRE,
3049 		   "UpgradeStartup::sendCntrMasterReq "
3050 		   "NdbNodeBitmask::NotFound");
3051   }
3052 
3053   CntrMasterReq * const cntrMasterReq = (CntrMasterReq*)&signal->theData[0];
3054   cntr.c_clusterNodes.copyto(NdbNodeBitmask::Size, cntrMasterReq->theNodes);
3055   NdbNodeBitmask::clear(cntrMasterReq->theNodes, cntr.getOwnNodeId());
3056   cntrMasterReq->userBlockRef = 0;
3057   cntrMasterReq->userNodeId = cntr.getOwnNodeId();
3058   cntrMasterReq->typeOfStart = NodeState::ST_INITIAL_NODE_RESTART;
3059   cntrMasterReq->noRestartNodes = cntr.c_clusterNodes.count() - 1;
3060   cntr.sendSignal(cntr.calcNdbCntrBlockRef(node), GSN_CNTR_MASTERREQ,
3061 		  signal, CntrMasterReq::SignalLength, JBB);
3062 }
3063 
3064 void
execCNTR_MASTER_REPLY(SimulatedBlock & block,Signal * signal)3065 UpgradeStartup::execCNTR_MASTER_REPLY(SimulatedBlock & block, Signal* signal){
3066   Uint32 gsn = signal->header.theVerId_signalNumber;
3067   Uint32 node = refToNode(signal->getSendersBlockRef());
3068   if(block.number() == CNTR){
3069     Ndbcntr& cntr = (Ndbcntr&)block;
3070     switch(gsn){
3071     case GSN_CNTR_MASTERREF:
3072       sendCntrMasterReq(cntr, signal, node + 1);
3073       return;
3074       break;
3075     case GSN_CNTR_MASTERCONF:{
3076       CntrStartConf* conf = (CntrStartConf*)signal->getDataPtrSend();
3077       conf->startGci = 0;
3078       conf->masterNodeId = node;
3079       conf->noStartNodes = 1;
3080       conf->startType = NodeState::ST_INITIAL_NODE_RESTART;
3081       NodeBitmask mask;
3082       mask.clear();
3083       mask.copyto(NdbNodeBitmask::Size, conf->startedNodes);
3084       mask.clear();
3085       mask.set(cntr.getOwnNodeId());
3086       mask.copyto(NdbNodeBitmask::Size, conf->startingNodes);
3087       cntr.execCNTR_START_CONF(signal);
3088       return;
3089     }
3090     }
3091   }
3092   block.progError(__LINE__,NDBD_EXIT_NDBREQUIRE,
3093 		  "UpgradeStartup::execCNTR_MASTER_REPLY");
3094 }
3095