summaryrefslogtreecommitdiff
path: root/ndb/src/kernel/vm/Emulator.cpp
blob: 43b5619d2022bf0ee48303406d25425cd9c043b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "Emulator.hpp"
#include <FastScheduler.hpp>
#include <SignalLoggerManager.hpp>
#include <TransporterRegistry.hpp>
#include <TimeQueue.hpp>

#include "Configuration.hpp"
#include "WatchDog.hpp"
#include "ThreadConfig.hpp"
#include "SimBlockList.hpp"

#include <NodeState.hpp>

#include <NdbMem.h>
#include <NdbOut.hpp>
#include <NdbMutex.h>
#include <NdbSleep.h>
#include <stdlib.h>
#include <new>

#ifdef NDB_WIN32
#include <new.h>
#include <process.h>
#define execvp _execvp
#define set_new_handler _set_new_handler
#endif

/**
 * Declare the global variables 
 */

#ifdef USE_EMULATED_JAM
   Uint8 theEmulatedJam[EMULATED_JAM_SIZE * 4];
   Uint32 theEmulatedJamIndex = 0;
   Uint32 theEmulatedJamBlockNumber = 0;
#endif // USE_EMULATED_JAM

   GlobalData globalData;

   TimeQueue globalTimeQueue;
   FastScheduler globalScheduler;
   TransporterRegistry globalTransporterRegistry;

#ifdef VM_TRACE
   SignalLoggerManager globalSignalLoggers;
#endif

EmulatorData globalEmulatorData;
NdbMutex * theShutdownMutex = 0;

EmulatorData::EmulatorData(){
  theConfiguration = 0;
  theWatchDog      = 0;
  theThreadConfig  = 0;
  theSimBlockList  = 0;
  theShutdownMutex = 0;
}

void
ndb_new_handler(){
  ERROR_SET(fatal, ERR_MEMALLOC, "New handler", "");
}

void
EmulatorData::create(){
  NdbMem_Create();

  theConfiguration = new Configuration();
  theWatchDog      = new WatchDog();
  theThreadConfig  = new ThreadConfig();
  theSimBlockList  = new SimBlockList();

  theShutdownMutex = NdbMutex_Create();

#ifdef NDB_WIN32
  set_new_handler((_PNH)ndb_new_handler);
#else
  std::set_new_handler(ndb_new_handler);
#endif
}

void
EmulatorData::destroy(){
  if(theConfiguration)
    delete theConfiguration; theConfiguration = 0;
  if(theWatchDog)
    delete theWatchDog; theWatchDog = 0;
  if(theThreadConfig)
    delete theThreadConfig; theThreadConfig = 0;
  if(theSimBlockList)
    delete theSimBlockList; theSimBlockList = 0;
  
  NdbMem_Destroy();
}

void
NdbRestart(char * programName,
	   NdbRestartType type, char * connString){
#if ! ( defined NDB_OSE || defined NDB_SOFTOSE) 
  int argc = 2;
  switch(type){
  case NRT_NoStart_Restart:
  case NRT_DoStart_InitialStart:
    argc = 3;
    break;
  case NRT_NoStart_InitialStart:
    argc = 4;
    break;
  case NRT_DoStart_Restart:
  case NRT_Default:
  default:
    argc = 2;
    break;
  }

  if(connString != 0){
    argc += 2;
  }
  
  char ** argv = new char * [argc];
  argv[0] = programName;
  argv[argc - 1] = 0;

  switch(type){
  case NRT_NoStart_Restart:
    argv[1] = "-n";
    break;
  case NRT_DoStart_InitialStart:
    argv[1] = "-i";
    break;
  case NRT_NoStart_InitialStart:
    argv[1] = "-n";
    argv[2] = "-i";
    break;
  case NRT_DoStart_Restart:
  case NRT_Default:
  default:
    break;
  }

  if(connString != 0){
    argv[argc-3] = "-c";
    argv[argc-2] = connString;
  }
  
  execvp(programName, argv);
#endif
}

void
NdbShutdown(NdbShutdownType type,
	    NdbRestartType restartType){
  
  if(type == NST_ErrorInsert){
    type = NST_Restart;
    restartType = (NdbRestartType)
      globalEmulatorData.theConfiguration->getRestartOnErrorInsert();
    if(restartType == NRT_Default){
      type = NST_ErrorHandler;
      globalEmulatorData.theConfiguration->stopOnError(true);
    }
  }
  
  if(NdbMutex_Trylock(theShutdownMutex) == 0){
    globalData.theRestartFlag = perform_stop;

    bool restart = false;
    char * progName = 0;
    char * connString = 0;
#if ! ( defined NDB_OSE || defined NDB_SOFTOSE) 
    if((type != NST_Normal && 
	globalEmulatorData.theConfiguration->stopOnError() == false) ||
       type == NST_Restart) {
      
      restart  = true;
      progName = strdup(globalEmulatorData.theConfiguration->programName());
      connString = globalEmulatorData.theConfiguration->getConnectStringCopy();
      if(type != NST_Restart){
	/**
	 * If we crash before we started
	 *
	 * Do restart -n
	 */
	if(globalData.theStartLevel == NodeState::SL_STARTED)
	  restartType = NRT_Default;
	else
	  restartType = NRT_NoStart_Restart;
      }
    }
#endif
    
    const char * shutting = "shutting down";
    if(restart){
      shutting = "restarting";
    }
    
    switch(type){
    case NST_Normal:
      ndbout << "Shutdown initiated" << endl;
      break;
    case NST_Watchdog:
      ndbout << "Watchdog " << shutting << " system" << endl;
      break;
    case NST_ErrorHandler:
      ndbout << "Error handler " << shutting << " system" << endl;
      break;
    case NST_Restart:
      ndbout << "Restarting system" << endl;
      break;
    default:
      ndbout << "Error handler " << shutting << " system"
	     << " (unknown type: " << type << ")" << endl;
      type = NST_ErrorHandler;
      break;
    }
    
    const char * exitAbort = 0;
#if defined VM_TRACE && ( ! ( defined NDB_OSE || defined NDB_SOFTOSE) )
    exitAbort = "aborting";
#else
    exitAbort = "exiting";
#endif
    
    if(type == NST_Watchdog){
      if(restart){
	NdbRestart(progName, restartType, connString);
      }
      
      /**
       * Very serious
       */
      ndbout << "Watchdog shutdown completed - " << exitAbort << endl;
#if defined VM_TRACE && ( ! ( defined NDB_OSE || defined NDB_SOFTOSE) )
      abort();
#else
      exit(1);
#endif
    }

    globalEmulatorData.theWatchDog->doStop();
    
#ifdef VM_TRACE
    FILE * outputStream = globalSignalLoggers.setOutputStream(0);
    if(outputStream != 0)
      fclose(outputStream);
#endif
    
    globalTransporterRegistry.stopSending();
    globalTransporterRegistry.stopReceiving();
    
    globalTransporterRegistry.removeAll();

#ifdef VM_TRACE
#define UNLOAD (type != NST_ErrorHandler && type != NST_Watchdog)
#else
#define UNLOAD true
#endif
    if(UNLOAD){
      globalEmulatorData.theSimBlockList->unload();    
      globalEmulatorData.destroy();

    }

    if(type != NST_Normal &&
       type != NST_Restart){
      if(restart){
	NdbRestart(progName, restartType, connString);
      }
      
      ndbout << "Error handler shutdown completed - " << exitAbort << endl;
#if defined VM_TRACE && ( ! ( defined NDB_OSE || defined NDB_SOFTOSE) )
      abort();
#else
      exit(1);
#endif
    }
    
    /**
     * This is a normal restart
     */
    if(type == NST_Restart){
      if(restart){
	NdbRestart(progName, restartType, connString);
      }
      /**
       * What to do if in restart mode, but being unable to do it...
       */
#if defined VM_TRACE && ( ! ( defined NDB_OSE || defined NDB_SOFTOSE) )
      abort();
#else
      exit(1);
#endif
    }

    /**
     * This is normal shutdown
     */
    ndbout << "Shutdown completed - exiting" << endl;
  } else {
    /**
     * Shutdown is already in progress
     */

    /** 
     * If this is the watchdog, kill system the hard way
     */
    if (type== NST_Watchdog){
      ndbout << "Watchdog is killing system the hard way" << endl;
#if defined VM_TRACE && ( ! ( defined NDB_OSE || defined NDB_SOFTOSE) )
      abort();
#else
      exit(1);
#endif
    }
      
    while(true)
      NdbSleep_MilliSleep(10);
  }
}