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 ***************************************************************************/
17 #include<SessionImpl.h>
21 #include<Transaction.h>
23 #include<CacheTableLoader.h>
24 char* version
= "csql-linux-i686-1.2Beta";
28 static void sigTermHandler(int sig
)
30 printf("Received signal %d\nStopping the server\n", sig
);
34 bool checkDead(pid_t pid
)
36 int ret
= os::kill(pid
, 0);
37 if (ret
== -1) return true; else return false;
40 DbRetVal
releaseAllResources(Database
*sysdb
, ThreadInfo
*info
)
42 printf("Releasing all the resources for process %d %lu\n", info
->pid_
, info
->thrid_
);
43 //recover for all the mutexes in has_
44 for (int i
=0; i
< MAX_MUTEX_PER_THREAD
; i
++)
46 if (info
->has_
[i
] != NULL
)
48 printf("Dead Procs: %d %lu holding mutex %x %s \n", info
->pid_
, info
->thrid_
, info
->has_
[i
], info
->has_
[i
]->name
);
49 logFine(logger
, "Dead Procs: %d %lu holding mutex %x %s \n", info
->pid_
, info
->thrid_
, info
->has_
[i
], info
->has_
[i
]->name
);
50 //TODO::recovery of mutexes
51 //info->has_[i]->recoverMutex();
56 TransactionManager
*tm
= new TransactionManager();
57 LockManager
*lm
= new LockManager(sysdb
);
58 for (int i
= 0 ;i
< MAX_THREADS_PER_PROCESS
; i
++)
60 if (info
->thrTrans_
[i
].trans_
!= NULL
&& info
->thrTrans_
[i
].trans_
->status_
== TransRunning
)
62 printf("Rollback Transaction %x\n", info
->thrTrans_
[i
].trans_
);
63 tm
->rollback(lm
, info
->thrTrans_
[i
].trans_
);
72 DbRetVal
cleanupDeadProcs(Database
*sysdb
)
74 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
77 printError(rv
,"Unable to get process table mutex");
82 pthread_t thrid
= os::getthrid();
85 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(0);
87 ThreadInfo
* freeSlot
= NULL
;
88 for (; i
< Conf::config
.getMaxProcs(); i
++)
90 //check whether it is alive
91 if (pInfo
->pid_
!=0 && checkDead(pInfo
->pid_
)) releaseAllResources(sysdb
, pInfo
);
94 sysdb
->releaseProcessTableMutex(false);
99 DbRetVal
logActiveProcs(Database
*sysdb
)
101 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
104 printError(rv
,"Unable to get process table mutex");
107 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(0);
109 ThreadInfo
* freeSlot
= NULL
;
110 for (; i
< Conf::config
.getMaxProcs(); i
++)
112 if (pInfo
->pid_
!=0 ) logFine(logger
, "Registered Procs: %d %lu\n", pInfo
->pid_
, pInfo
->thrid_
);
115 sysdb
->releaseProcessTableMutex(false);
118 void startCacheServer()
120 printf("Starting Cache Recv Server\n");
122 sprintf(execName
, "%s/bin/csqlcacheserver", os::getenv("CSQL_INSTALL_ROOT"));
123 printf("filename is %s\n", execName
);
124 cachepid
= os::createProcess(execName
, "-s");
126 printf("Cache Recv Server Started pid=%d\n", replpid
);
132 printf("Usage: csqlserver [-c] [-v]\n");
133 printf(" v -> print the version.\n");
134 printf(" c -> recover all cached tables from the target database.\n");
135 printf("Description: Start the csql server and initialize the database.\n");
138 int main(int argc
, char **argv
)
141 while ((c
= getopt(argc
, argv
, "cv?")) != EOF
)
145 case '?' : { opt
= 10; break; } //print help
146 case 'c' : { opt
= 1; break; } //recover all the tables from cache
147 case 'v' : { opt
= 2; break; } //print version
157 printf("%s\n",version
);
162 DbRetVal rv
= session
.readConfigFile();
165 printf("Unable to read the configuration file \n");
168 os::signal(SIGINT
, sigTermHandler
);
169 os::signal(SIGTERM
, sigTermHandler
);
170 rv
= logger
.startLogger(Conf::config
.getLogFile(), true);
173 printf("Unable to start the logger\n");
177 logFine(logger
, "Server Started");
178 int ret
= session
.initSystemDatabase();
181 printf(" System Database Initialization failed\n");
184 printf("System Database initialized\n");
189 struct timeval timeout
, tval
;
192 Database
* sysdb
= session
.getSystemDatabase();
194 if (Conf::config
.useCache()) {
195 printf("Database server recovering cached tables...\n");
196 int ret
= system("cachetable -U root -P manager -R");
198 printf("Cached Tables recovery failed %d\n", ret
);
200 session
.destroySystemDatabase();
203 printf("Cached Tables recovered\n");
205 printf("Cache mode is not set in csql.conf. Cannot recover\n");
207 session
.destroySystemDatabase();
211 if (Conf::config
.useReplication())
213 printf("Starting Replication Server\n");
215 sprintf(execName
, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
216 printf("filename is %s\n", execName
);
217 replpid
= os::createProcess(execName
, "-s");
219 printf("Repl Server Started pid=%d\n", replpid
);
222 if (Conf::config
.useCache() && Conf::config
.useTwoWayCache()) startCacheServer();
224 printf("Database server started\n");
228 tval
.tv_sec
= timeout
.tv_sec
;
229 tval
.tv_usec
= timeout
.tv_usec
;
230 os::select(0, 0, 0, 0, &tval
);
232 //send signal to all the registered process..check they are alive
233 cleanupDeadProcs(sysdb
);
235 //TODO::check repl server is alive, if not restart it
237 //TODO::if it fails to start 5 times, exit
238 if (cachepid
!=0 && checkDead(cachepid
)) startCacheServer();
241 os::kill(cachepid
, SIGTERM
);
242 logFine(logger
, "Server Exiting");
243 logActiveProcs(sysdb
);
244 printf("Server Exiting\n");
245 logFine(logger
, "Server Ended");
247 session
.destroySystemDatabase();