start only if sql server if turned on in csql.conf file
[csql.git] / src / tools / csqlserver.cxx
blobc4635c931e2c31d8f82dccd46d33ae7befc0c984
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<CSql.h>
17 #include<SessionImpl.h>
18 #include<Debug.h>
19 #include<Process.h>
20 #include<Database.h>
21 #include<Transaction.h>
22 #include<Lock.h>
23 #include<CacheTableLoader.h>
24 char* version = "csql-linux-i686-2.0GA";
25 int srvStop =0;
26 pid_t replpid=0;
27 pid_t cachepid=0;
28 void dumpData();
29 static void sigTermHandler(int sig)
31 printf("Received signal %d\nStopping the server\n", sig);
32 srvStop = 1;
35 bool checkDead(pid_t pid)
37 int ret = os::kill(pid, 0);
38 if (ret == -1) return true; else return false;
41 DbRetVal releaseAllResources(Database *sysdb, ThreadInfo *info )
43 printf("Releasing all the resources for process %d %lu\n", info->pid_, info->thrid_);
44 //recover for all the mutexes in has_
45 for (int i =0; i < MAX_MUTEX_PER_THREAD; i++)
47 if (info->has_[i] != NULL)
49 printf("Dead Procs: %d %lu holding mutex %x %s \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
50 logFine(logger, "Dead Procs: %d %lu holding mutex %x %s \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
51 //TODO::recovery of mutexes
52 sysdb->recoverMutex(info->has_[i]);
53 //srvStop = 1;
54 //return ErrSysFatal;
57 TransactionManager *tm = new TransactionManager();
58 LockManager *lm = new LockManager(sysdb);
59 for (int i = 0 ;i < MAX_THREADS_PER_PROCESS; i++)
61 if (info->thrTrans_[i].trans_ != NULL && info->thrTrans_[i].trans_->status_ == TransRunning)
63 printf("Rollback Transaction %x\n", info->thrTrans_[i].trans_);
64 tm->rollback(lm, info->thrTrans_[i].trans_);
67 info->init();
68 delete tm;
69 delete lm;
70 return OK;
73 DbRetVal cleanupDeadProcs(Database *sysdb)
75 DbRetVal rv = sysdb->getProcessTableMutex(false);
76 if (OK != rv)
78 printError(rv,"Unable to get process table mutex");
79 return rv;
81 pid_t pid;
82 pid = os::getpid();
83 pthread_t thrid = os::getthrid();
86 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
87 int i=0;
88 ThreadInfo* freeSlot = NULL;
89 for (; i < Conf::config.getMaxProcs(); i++)
91 //check whether it is alive
92 if (pInfo->pid_ !=0 && checkDead(pInfo->pid_)) releaseAllResources(sysdb, pInfo);
93 pInfo++;
95 sysdb->releaseProcessTableMutex(false);
96 return OK;
100 DbRetVal logActiveProcs(Database *sysdb)
102 DbRetVal rv = sysdb->getProcessTableMutex(false);
103 if (OK != rv)
105 printError(rv,"Unable to get process table mutex");
106 return rv;
108 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
109 int i=0;
110 ThreadInfo* freeSlot = NULL;
111 for (; i < Conf::config.getMaxProcs(); i++)
113 if (pInfo->pid_ !=0 ) logFine(logger, "Registered Procs: %d %lu\n", pInfo->pid_, pInfo->thrid_);
114 pInfo++;
116 sysdb->releaseProcessTableMutex(false);
117 return OK;
119 void startCacheServer()
121 printf("Starting Cache Recv Server\n");
122 char execName[1024];
123 sprintf(execName, "%s/bin/csqlcacheserver", os::getenv("CSQL_INSTALL_ROOT"));
124 printf("filename is %s\n", execName);
125 cachepid = os::createProcess(execName, "-s");
126 if (cachepid != -1)
127 printf("Cache Recv Server Started pid=%d\n", cachepid);
128 return;
131 void startServiceClient()
133 printf("Starting Csql Network Daemon\n");
134 char execName[1024];
135 sprintf(execName, "%s/bin/csqlsqlserver", os::getenv("CSQL_INSTALL_ROOT"));
136 printf("filename is %s\n", execName);
137 cachepid = os::createProcess(execName, "-s");
138 if (cachepid != -1)
139 printf("Csql Network Daemon Started pid=%d\n", cachepid);
140 return;
143 void printUsage()
145 printf("Usage: csqlserver [-c] [-v]\n");
146 printf(" v -> print the version.\n");
147 printf(" c -> recover all cached tables from the target database.\n");
148 printf("Description: Start the csql server and initialize the database.\n");
149 return;
151 int main(int argc, char **argv)
153 int c = 0, opt = 0;
154 while ((c = getopt(argc, argv, "cv?")) != EOF)
156 switch (c)
158 case '?' : { opt = 10; break; } //print help
159 case 'c' : { opt = 1; break; } //recover all the tables from cache
160 case 'v' : { opt = 2; break; } //print version
161 default: opt=10;
164 }//while options
166 if (opt == 10) {
167 printUsage();
168 return 0;
169 }else if (opt ==2) {
170 printf("%s\n",version);
171 return 0;
174 SessionImpl session;
175 DbRetVal rv = session.readConfigFile();
176 if (rv != OK)
178 printf("Unable to read the configuration file \n");
179 return 1;
181 os::signal(SIGINT, sigTermHandler);
182 os::signal(SIGTERM, sigTermHandler);
183 rv = logger.startLogger(Conf::config.getLogFile(), true);
184 if (rv != OK)
186 printf("Unable to start the logger\n");
187 return 2;
190 logFine(logger, "Server Started");
191 int ret = session.initSystemDatabase();
192 if (0 != ret)
194 printf(" System Database Initialization failed\n");
195 return 3;
197 printf("System Database initialized\n");
200 bool end = false;
202 struct timeval timeout, tval;
203 timeout.tv_sec = 5;
204 timeout.tv_usec = 0;
205 Database* sysdb = session.getSystemDatabase();
206 if (FILE *file = fopen(Conf::config.getDbFile(), "r"))
208 fclose(file);
209 char cmd[1024];
210 sprintf(cmd, "csql -S -s %s",Conf::config.getDbFile());
211 int ret = system(cmd);
212 if (ret != 0) {
213 printf("Tables cannot be recovered. DB file corrupted\n");
216 if (opt == 1) {
217 if (Conf::config.useCache()) {
218 printf("Database server recovering cached tables...\n");
219 int ret = system("cachetable -U root -P manager -R");
220 if (ret != 0) {
221 printf("Cached Tables recovery failed %d\n", ret);
222 logger.stopLogger();
223 session.destroySystemDatabase();
224 return 2;
226 printf("Cached Tables recovered\n");
227 } else {
228 printf("Cache mode is not set in csql.conf. Cannot recover\n");
229 logger.stopLogger();
230 session.destroySystemDatabase();
231 return 1;
235 if (Conf::config.useReplication())
237 printf("Starting Replication Server\n");
238 char execName[1024];
239 sprintf(execName, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
240 printf("filename is %s\n", execName);
241 replpid = os::createProcess(execName, "-s");
242 if (replpid != -1)
243 printf("Repl Server Started pid=%d\n", replpid);
246 if (Conf::config.useCache() && Conf::config.useTwoWayCache()) startCacheServer();
247 printf("Database server started\n");
249 if(Conf::config.useCsqlSqlServer()) startServiceClient();
251 while(!srvStop)
253 tval.tv_sec = timeout.tv_sec;
254 tval.tv_usec = timeout.tv_usec;
255 os::select(0, 0, 0, 0, &tval);
257 //send signal to all the registered process..check they are alive
258 cleanupDeadProcs(sysdb);
260 //TODO::check repl server is alive, if not restart it
262 //TODO::if it fails to start 5 times, exit
263 if (cachepid !=0 && checkDead(cachepid)) startCacheServer();
266 os::kill(cachepid, SIGTERM);
267 dumpData();
268 logFine(logger, "Server Exiting");
269 logActiveProcs(sysdb);
270 printf("Server Exiting\n");
271 logFine(logger, "Server Ended");
272 logger.stopLogger();
273 session.destroySystemDatabase();
274 return 0;
276 void dumpData()
278 char cmd[1024];
279 sprintf(cmd, "csqldump >%s",Conf::config.getDbFile());
280 int ret = system(cmd);
281 if (ret != 0) {
282 printf("Table cannot be written. Recovery will fail\n");
284 return;