1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
18 #include<SessionImpl.h>
22 #include<Transaction.h>
24 #include<CacheTableLoader.h>
25 char* version
= "csql-linux-i686-3.0GA";
31 bool recoverFlag
=false;
32 bool monitorServer
= false;
33 SessionImpl
*session
= NULL
;
34 static void sigTermHandler(int sig
)
36 printf("Received signal %d\nStopping the server\n", sig
);
40 static void sigChildHandler(int sig
)
42 os::signal(SIGCHLD
, sigChildHandler
);
44 waitpid(-1, &stat
, WNOHANG
);
45 //TODO::move waitpid to os wrapper
48 bool checkDead(pid_t pid
)
50 int ret
= os::kill(pid
, 0);
53 printError(ErrWarning
, "No permission to check process %d is alive.");
60 DbRetVal
releaseAllResources(Database
*sysdb
, ThreadInfo
*info
)
62 printf("Releasing all the resources for process %d %lu\n", info
->pid_
, info
->thrid_
);
63 //recover for all the mutexes in has_
64 for (int i
=0; i
< MAX_MUTEX_PER_THREAD
; i
++)
66 if (info
->has_
[i
] != NULL
)
68 printf("Dead Procs: %d %lu holding mutex %x %s \n", info
->pid_
, info
->thrid_
, info
->has_
[i
], info
->has_
[i
]->name
);
69 logFine(Conf::logger
, "Dead Procs: %d %lu holding mutex %x %s \n", info
->pid_
, info
->thrid_
, info
->has_
[i
], info
->has_
[i
]->name
);
70 //TODO::recovery of mutexes
71 sysdb
->recoverMutex(info
->has_
[i
]);
76 TransactionManager
*tm
= new TransactionManager();
77 LockManager
*lm
= new LockManager(sysdb
);
78 if (info
->thrTrans_
.trans_
!= NULL
&& info
->thrTrans_
.trans_
->status_
== TransRunning
)
80 printf("Rollback Transaction %x\n", info
->thrTrans_
.trans_
);
81 tm
->rollback(lm
, info
->thrTrans_
.trans_
);
82 info
->thrTrans_
.trans_
->status_
= TransNotUsed
;
90 DbRetVal
cleanupDeadProcs(Database
*sysdb
)
92 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
95 printError(rv
,"Unable to get process table mutex");
100 pthread_t thrid
= os::getthrid();
103 ThreadInfo
* tInfo
= sysdb
->getThreadInfo(0);
105 ThreadInfo
* freeSlot
= NULL
;
106 for (; i
< Conf::config
.getMaxProcs(); i
++)
108 //check whether it is alive
109 if (tInfo
->pid_
!=0 && checkDead(tInfo
->pid_
)) releaseAllResources(sysdb
, tInfo
);
112 sysdb
->releaseProcessTableMutex(false);
117 DbRetVal
logActiveProcs(Database
*sysdb
)
119 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
122 printError(rv
,"Unable to get process table mutex");
125 ThreadInfo
* tInfo
= sysdb
->getThreadInfo(0);
127 ThreadInfo
* freeSlot
= NULL
;
128 for (; i
< Conf::config
.getMaxProcs(); i
++)
130 if (tInfo
->pid_
!=0 ) {
131 logFine(Conf::logger
, "Registered Procs: %d %lu\n", tInfo
->pid_
, tInfo
->thrid_
);
132 printf("Client process with pid %d is still registered\n", tInfo
->pid_
);
133 if( tInfo
->pid_
!= asyncpid
&& tInfo
->pid_
!= cachepid
&&
134 tInfo
->pid_
!= sqlserverpid
)
139 sysdb
->releaseProcessTableMutex(false);
140 if (count
) return ErrSysInternal
; else return OK
;
142 void startCacheServer()
145 sprintf(execName
, "%s/bin/csqlcacheserver", os::getenv("CSQL_INSTALL_ROOT"));
147 //printf("filename is %s\n", execName);
148 cachepid
= os::createProcess(execName
, "csqlcacheserver");
150 printf("Cache Receiver Started\t [PID=%d]\n",cachepid
);
154 void startServiceClient()
157 sprintf(execName
, "%s/bin/csqlsqlserver", os::getenv("CSQL_INSTALL_ROOT"));
158 //printf("filename is %s\n", execName);
160 sqlserverpid
= os::createProcess(execName
, "csqlsqlserver");
161 if (sqlserverpid
!= -1)
162 printf("Network Server Started\t [PID=%d] [PORT=%d]\n", sqlserverpid
,Conf::config
.getPort());
167 void startAsyncServer()
170 sprintf(execName
, "%s/bin/csqlasyncserver", os::getenv("CSQL_INSTALL_ROOT"));
171 //printf("filename is %s\n", execName);
173 asyncpid
= os::createProcess(execName
, "csqlasyncserver");
175 printf("Async Cache Server Started [PID=%d]\n", asyncpid
);
178 void startCheckpointServer()
181 sprintf(execName
, "%s/bin/csqlcheckpointserver", os::getenv("CSQL_INSTALL_ROOT"));
183 chkptpid
= os::createProcess(execName
, "csqlcheckpointserver");
184 if (chkptpid
!= -1) {
185 printf("Checkpoint Server Started [PID=%d]\n", chkptpid
);
186 logFine(Conf::logger
, "Checkpoint Server Started pid:%d", chkptpid
);
195 printf("Usage: csqlserver [-c] [-v]\n");
196 printf(" v -> print the version.\n");
197 printf(" c -> recover all cached tables from the target database.\n");
198 printf("Description: Start the csql server and initialize the database.\n");
201 int main(int argc
, char **argv
)
205 while ((c
= getopt(argc
, argv
, "cv?")) != EOF
)
209 case '?' : { opt
= 10; break; } //print help
210 case 'c' : { opt
= 1; break; } //recover all the tables from cache
211 case 'v' : { opt
= 2; break; } //print version
221 printf("%s\n",version
);
224 session
= new SessionImpl();
225 DbRetVal rv
= session
->readConfigFile();
228 printf("Unable to read the configuration file \n");
231 os::signal(SIGINT
, sigTermHandler
);
232 os::signal(SIGTERM
, sigTermHandler
);
233 os::signal(SIGCHLD
, sigChildHandler
);
234 rv
= Conf::logger
.startLogger(Conf::config
.getLogFile(), true);
237 printf("Unable to start the Conf::logger\n");
241 logFine(Conf::logger
, "Server Started");
242 int ret
= session
->initSystemDatabase();
245 //printf(" System Database Initialization failed\n");
246 printf("Attaching to exising database\n");
249 session
= new SessionImpl();
250 ret
= session
->open(DBAUSER
, DBAPASS
);
252 printf("Unable to attach to existing database\n");
257 struct timeval timeout
, tval
;
260 Database
* sysdb
= session
->getSystemDatabase();
264 if (isInit
) UID
.create();
266 if(isInit
&& Conf::config
.useDurability())
268 char dbRedoFileName
[MAX_FILE_LEN
];
269 char dbChkptSchema
[MAX_FILE_LEN
];
270 char dbChkptMap
[MAX_FILE_LEN
];
271 char dbChkptData
[MAX_FILE_LEN
];
272 char dbBackupFile
[MAX_FILE_LEN
];
274 //check for check point file if present recover
275 sprintf(dbChkptSchema
, "%s/db.chkpt.schema1", Conf::config
.getDbFile());
276 if (FILE *file
= fopen(dbChkptSchema
, "r")) {
278 sprintf(cmd
, "cp -f %s %s/db.chkpt.schema", dbChkptSchema
, Conf::config
.getDbFile());
279 int ret
= system(cmd
);
281 Conf::logger
.stopLogger();
282 session
->destroySystemDatabase();
287 sprintf(dbChkptMap
, "%s/db.chkpt.map1", Conf::config
.getDbFile());
288 if (FILE *file
= fopen(dbChkptMap
, "r")) {
290 sprintf(cmd
, "cp -f %s %s/db.chkpt.map", dbChkptMap
, Conf::config
.getDbFile());
291 int ret
= system(cmd
);
293 Conf::logger
.stopLogger();
294 session
->destroySystemDatabase();
299 int chkptID
= Database::getCheckpointID();
300 sprintf(dbChkptData
, "%s/db.chkpt.data%d", Conf::config
.getDbFile(),
302 sprintf(dbBackupFile
, "%s/db.chkpt.data1", Conf::config
.getDbFile());
304 if (!Conf::config
.useMmap() && (fl
= fopen(dbBackupFile
, "r"))) {
306 sprintf(cmd
, "cp %s/db.chkpt.data1 %s", Conf::config
.getDbFile(), dbChkptData
);
307 int ret
= system(cmd
);
309 printError(ErrOS
, "Unable to take backup for chkpt data file");
313 if (FILE *file
= fopen(dbChkptData
, "r")) {
315 int ret
= system("recover");
317 printf("Recovery failed\n");
318 Conf::logger
.stopLogger();
319 session
->destroySystemDatabase();
325 //check for redo log file if present apply redo logs
326 sprintf(dbRedoFileName
, "%s/csql.db.cur", Conf::config
.getDbFile());
327 if (FILE *file
= fopen(dbRedoFileName
, "r"))
330 int ret
= system("redo -a");
332 printf("Recovery failed. Redo log file corrupted\n");
333 Conf::logger
.stopLogger();
334 session
->destroySystemDatabase();
339 // take check point at this moment
340 DatabaseManager
*dbMgr
= session
->getDatabaseManager();
341 rv
= dbMgr
->checkPoint();
344 printError(ErrSysInternal
, "checkpoint failed after redo log apply");
345 Conf::logger
.stopLogger();
346 session
->destroySystemDatabase();
352 bool isCacheReq
= false, isSQLReq
= false, isAsyncReq
=false, isChkptReq
=false;
354 if (opt
== 1 && isInit
&& ! Conf::config
.useDurability()) {
355 if (Conf::config
.useCache()) {
356 printf("Database server recovering cached tables...\n");
357 int ret
= system("cachetable -R");
359 printf("Cached Tables recovery failed %d\n", ret
);
360 Conf::logger
.stopLogger();
361 session
->destroySystemDatabase();
365 printf("Cached Tables recovered\n");
367 printf("Cache mode is not set in csql.conf. Cannot recover\n");
368 Conf::logger
.stopLogger();
369 session
->destroySystemDatabase();
374 //TODO:: kill all the child servers and restart if !isInit
376 if(Conf::config
.useCsqlSqlServer()) {
378 startServiceClient();
380 if ( (Conf::config
.useCache() &&
381 Conf::config
.getCacheMode()==ASYNC_MODE
)) {
382 int msgid
= os::msgget(Conf::config
.getMsgKey(), 0666);
383 if (msgid
!= -1) os::msgctl(msgid
, IPC_RMID
, NULL
);
387 if (Conf::config
.useCache() && Conf::config
.useTwoWayCache()) {
391 if(Conf::config
.useDurability())
394 startCheckpointServer();
397 printf("Database Server Started...\n");
398 logFine(Conf::logger
, "Database Server Started");
399 monitorServer
= Conf::config
.useMonitorServers();
404 tval
.tv_sec
= timeout
.tv_sec
;
405 tval
.tv_usec
= timeout
.tv_usec
;
406 os::select(0, 0, 0, 0, &tval
);
408 //send signal to all the registered process..check they are alive
409 cleanupDeadProcs(sysdb
);
412 if (isCacheReq
&& cachepid
!=0 && checkDead(cachepid
)) {
413 logFine(Conf::logger
, "Cache Receiver Died pid:%d", cachepid
);
416 if (isAsyncReq
&& asyncpid
!=0 && checkDead(asyncpid
)) {
417 logFine(Conf::logger
, "Async Server Died pid:%d", asyncpid
);
418 int msgid
= os::msgget(Conf::config
.getMsgKey(), 0666);
419 if (msgid
!= -1) os::msgctl(msgid
, IPC_RMID
, NULL
);
422 if (isSQLReq
&& sqlserverpid
!=0 && checkDead(sqlserverpid
)) {
423 logFine(Conf::logger
, "Network Server Died pid:%d", sqlserverpid
);
424 startServiceClient();
426 if (isChkptReq
&& chkptpid
!=0 && checkDead(chkptpid
)) {
427 logFine(Conf::logger
, "Checkpoint Server Died pid:%d", chkptpid
);
428 startCheckpointServer();
432 if (logActiveProcs(sysdb
) != OK
) {srvStop
= 0;
433 monitorServer
= Conf::config
.useMonitorServers();
436 if (cachepid
) os::kill(cachepid
, SIGTERM
);
437 if(asyncpid
) os::kill(asyncpid
, SIGTERM
);
438 if (sqlserverpid
) os::kill(sqlserverpid
, SIGTERM
);
439 if (chkptpid
) os::kill(chkptpid
, SIGTERM
);
440 if (Conf::config
.useDurability() && Conf::config
.useMmap()) {
442 char *startAddr
= (char *) sysdb
->getMetaDataPtr();
443 msync(startAddr
+ Conf::config
.getMaxSysDbSize(),Conf::config
.getMaxDbSize(), MS_SYNC
);
444 munmap(startAddr
+ Conf::config
.getMaxSysDbSize(), Conf::config
.getMaxDbSize());
446 logFine(Conf::logger
, "Server Exiting");
447 printf("Server Exiting\n");
448 logFine(Conf::logger
, "Server Ended");
450 session
->destroySystemDatabase();
451 Conf::logger
.stopLogger();