summaryrefslogtreecommitdiff
path: root/ndb/src/kernel/Main.cpp
blob: 7bd4e75ca18a64e3ca4aad2d618aa111cefd74be (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
/* 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 <ndb_global.h>

#include <ndb_version.h>
#include "Configuration.hpp"
#include <TransporterRegistry.hpp>

#include "SimBlockList.hpp"
#include "ThreadConfig.hpp"
#include <SignalLoggerManager.hpp>
#include <NdbOut.hpp>
#include <NdbMain.h>
#include <NdbDaemon.h>
#include <NdbConfig.h>
#include <WatchDog.hpp>

#include <LogLevel.hpp>
#include <EventLogger.hpp>
#include <NodeState.hpp>

#if defined NDB_SOLARIS // ok
#include <sys/processor.h> // For system informatio
#endif

#if !defined NDB_SOFTOSE && !defined NDB_OSE
#include <signal.h>        // For process signals
#endif

extern EventLogger g_eventLogger;

void catchsigs(bool ignore); // for process signal handling
extern "C" void handler(int signo);  // for process signal handling

// Shows system information
void systemInfo(const Configuration & conf,
		const LogLevel & ll); 

const char programName[] = "NDB Kernel";

NDB_MAIN(ndb_kernel){

  // Print to stdout/console
  g_eventLogger.createConsoleHandler();
  g_eventLogger.setCategory("NDB");
  g_eventLogger.enable(Logger::LL_INFO, Logger::LL_ALERT); // Log INFO to ALERT

  globalEmulatorData.create();

  // Parse command line options
  Configuration* theConfig = globalEmulatorData.theConfiguration;
  if(!theConfig->init(argc, argv)){
    return 0;
  }
  
  { // Do configuration
    theConfig->setupConfiguration();
  }

  // Get NDB_HOME path
  char homePath[255];
  NdbConfig_HomePath(homePath, 255);

  if (theConfig->getDaemonMode()) {
    // Become a daemon
    char lockfile[255], logfile[255];
    snprintf(lockfile, 255, "%snode%d.pid", homePath, globalData.ownId);
    snprintf(logfile, 255, "%snode%d.out", homePath, globalData.ownId);
    if (NdbDaemon_Make(lockfile, logfile, 0) == -1) {
      ndbout << "Cannot become daemon: " << NdbDaemon_ErrorText << endl;
      return 1;
    }
  }
  
  for(pid_t child = fork(); child != 0; child = fork()){
    /**
     * Parent
     */
    catchsigs(true);

    int status = 0;
    while(waitpid(child, &status, 0) != child);
    if(WIFEXITED(status)){
      switch(WEXITSTATUS(status)){
      case NRT_Default:
	g_eventLogger.info("Angel shutting down");
	exit(0);
	break;
      case NRT_NoStart_Restart:
	theConfig->setInitialStart(false);
	globalData.theRestartFlag = initial_state;
	break;
      case NRT_NoStart_InitialStart:
	theConfig->setInitialStart(true);
	globalData.theRestartFlag = initial_state;
	break;
      case NRT_DoStart_InitialStart:
	theConfig->setInitialStart(true);
	globalData.theRestartFlag = perform_start;
	break;
      default:
	if(theConfig->stopOnError()){
	  /**
	   * Error shutdown && stopOnError()
	   */
	  exit(0);
	}
	// Fall-through
      case NRT_DoStart_Restart:
	theConfig->setInitialStart(false);
	globalData.theRestartFlag = perform_start;
	break;
      }
    } else if(theConfig->stopOnError()){
      /**
       * Error shutdown && stopOnError()
       */
      exit(0);
    }
    g_eventLogger.info("Ndb has terminated (pid %d) restarting", child);
  }

  g_eventLogger.info("Angel pid: %d ndb pid: %d", getppid(), getpid());
  systemInfo(* theConfig, * theConfig->m_logLevel); 

    // Load blocks
  globalEmulatorData.theSimBlockList->load(* theConfig);
    
  // Set thread concurrency for Solaris' light weight processes
  int status;
  status = NdbThread_SetConcurrencyLevel(30);
  NDB_ASSERT(status == 0, "Can't set appropriate concurrency level.");
  
#ifdef VM_TRACE
  // Create a signal logger
  char buf[255];
  strcpy(buf, homePath);
  FILE * signalLog = fopen(strncat(buf,"Signal.log", 255), "a");
  globalSignalLoggers.setOwnNodeId(globalData.ownId);
  globalSignalLoggers.setOutputStream(signalLog);
#endif
  
  catchsigs(false);
   
  /**
   * Do startup
   */
  switch(globalData.theRestartFlag){
  case initial_state:
    globalEmulatorData.theThreadConfig->doStart(NodeState::SL_CMVMI);
    break;
  case perform_start:
    globalEmulatorData.theThreadConfig->doStart(NodeState::SL_CMVMI);
    globalEmulatorData.theThreadConfig->doStart(NodeState::SL_STARTING);
    break;
  default:
    NDB_ASSERT(0, "Illegal state globalData.theRestartFlag");
  }

  globalTransporterRegistry.startSending();
  globalTransporterRegistry.startReceiving();
  globalEmulatorData.theWatchDog->doStart();
  
  globalEmulatorData.theThreadConfig->ipControlLoop();
  
  NdbShutdown(NST_Normal);
  return NRT_Default;
}


void 
systemInfo(const Configuration & config, const LogLevel & logLevel){
#ifdef NDB_WIN32
  int processors = 0;
  int speed;
  SYSTEM_INFO sinfo;
  GetSystemInfo(&sinfo);
  processors = sinfo.dwNumberOfProcessors;
  HKEY hKey;
  if(ERROR_SUCCESS==RegOpenKeyEx
     (HKEY_LOCAL_MACHINE, 
      TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 
      0, KEY_READ, &hKey)) {
    DWORD dwMHz;
    DWORD cbData = sizeof(dwMHz);
    if(ERROR_SUCCESS==RegQueryValueEx(hKey, 
				      "~MHz", 0, 0, (LPBYTE)&dwMHz, &cbData)) {
      speed = int(dwMHz);
    }
    RegCloseKey(hKey);
  }
#elif defined NDB_SOLARIS // ok
  // Search for at max 16 processors among the first 256 processor ids
  processor_info_t pinfo; memset(&pinfo, 0, sizeof(pinfo));
  int pid = 0;
  while(processors < 16 && pid < 256){
    if(!processor_info(pid++, &pinfo))
      processors++;
  }
  speed = pinfo.pi_clock;
#endif
  
  if(logLevel.getLogLevel(LogLevel::llStartUp) > 0){
    g_eventLogger.info("NDB Cluster -- DB node %d", globalData.ownId);
    g_eventLogger.info("%s --", NDB_VERSION_STRING);
#ifdef NDB_SOLARIS // ok
    g_eventLogger.info("NDB is running on a machine with %d processor(s) at %d MHz",
		       processor, speed);
#endif
  }
  if(logLevel.getLogLevel(LogLevel::llStartUp) > 3){
    Uint32 t = config.timeBetweenWatchDogCheck();
    g_eventLogger.info("WatchDog timer is set to %d ms", t);
  }

}

void 
catchsigs(bool ignore){
#if ! defined NDB_SOFTOSE && !defined NDB_OSE 

#if defined SIGRTMIN
  #define MAX_SIG_CATCH SIGRTMIN
#elif defined NSIG
  #define MAX_SIG_CATCH NSIG
#else
  #error "neither SIGRTMIN or NSIG is defined on this platform, please report bug at bugs.mysql.com"
#endif

  // Makes the main process catch process signals, eg installs a 
  // handler named "handler". "handler" will then be called is instead 
  // of the defualt process signal handler)
  if(ignore){
    for(int i = 1; i < MAX_SIG_CATCH; i++){
      if(i != SIGCHLD)
	signal(i, SIG_IGN);
    }
  } else {
    for(int i = 1; i < MAX_SIG_CATCH; i++){
      signal(i, handler);
    }
  }
#endif
}

extern "C"
void 
handler(int sig){
  switch(sig){
  case SIGHUP:   /*  1 - Hang up    */
  case SIGINT:   /*  2 - Interrupt  */
  case SIGQUIT:  /*  3 - Quit       */
  case SIGTERM:  /* 15 - Terminate  */
#ifdef SIGPWR
  case SIGPWR:   /* 19 - Power fail */
#endif
#ifdef SIGPOLL
  case SIGPOLL:  /* 22              */
#endif
  case SIGSTOP:  /* 23              */
  case SIGTSTP:  /* 24              */
  case SIGTTIN:  /* 26              */
  case SIGTTOU:  /* 27              */
    globalData.theRestartFlag = perform_stop;
    break;
#ifdef SIGWINCH
  case SIGWINCH:
#endif
  case SIGPIPE:
    /**
     * Can happen in TCP Transporter
     *  
     *  Just ignore
     */
    break;
  default:
    // restart the system
    char errorData[40];
    snprintf(errorData, 40, "Signal %d received", sig);
    ERROR_SET(fatal, 0, errorData, __FILE__);
    break;
  }
}