typo
[csql.git] / src / tools / csqlserver.cxx
blob652cccb79942b3c21c6d6141155db4f5c5afe86b
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 //info->has_[i]->recoverMutex();
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 printUsage()
133 printf("Usage: csqlserver [-c] [-v]\n");
134 printf(" v -> print the version.\n");
135 printf(" c -> recover all cached tables from the target database.\n");
136 printf("Description: Start the csql server and initialize the database.\n");
137 return;
139 int main(int argc, char **argv)
141 int c = 0, opt = 0;
142 while ((c = getopt(argc, argv, "cv?")) != EOF)
144 switch (c)
146 case '?' : { opt = 10; break; } //print help
147 case 'c' : { opt = 1; break; } //recover all the tables from cache
148 case 'v' : { opt = 2; break; } //print version
149 default: opt=10;
152 }//while options
154 if (opt == 10) {
155 printUsage();
156 return 0;
157 }else if (opt ==2) {
158 printf("%s\n",version);
159 return 0;
162 SessionImpl session;
163 DbRetVal rv = session.readConfigFile();
164 if (rv != OK)
166 printf("Unable to read the configuration file \n");
167 return 1;
169 os::signal(SIGINT, sigTermHandler);
170 os::signal(SIGTERM, sigTermHandler);
171 rv = logger.startLogger(Conf::config.getLogFile(), true);
172 if (rv != OK)
174 printf("Unable to start the logger\n");
175 return 2;
178 logFine(logger, "Server Started");
179 int ret = session.initSystemDatabase();
180 if (0 != ret)
182 printf(" System Database Initialization failed\n");
183 return 3;
185 printf("System Database initialized\n");
188 bool end = false;
190 struct timeval timeout, tval;
191 timeout.tv_sec = 5;
192 timeout.tv_usec = 0;
193 Database* sysdb = session.getSystemDatabase();
194 if (FILE *file = fopen(Conf::config.getDbFile(), "r"))
196 fclose(file);
197 char cmd[1024];
198 sprintf(cmd, "csql -S -s %s",Conf::config.getDbFile());
199 int ret = system(cmd);
200 if (ret != 0) {
201 printf("Tables cannot be recovered. DB file corrupted\n");
204 if (opt == 1) {
205 if (Conf::config.useCache()) {
206 printf("Database server recovering cached tables...\n");
207 int ret = system("cachetable -U root -P manager -R");
208 if (ret != 0) {
209 printf("Cached Tables recovery failed %d\n", ret);
210 logger.stopLogger();
211 session.destroySystemDatabase();
212 return 2;
214 printf("Cached Tables recovered\n");
215 } else {
216 printf("Cache mode is not set in csql.conf. Cannot recover\n");
217 logger.stopLogger();
218 session.destroySystemDatabase();
219 return 1;
222 if (Conf::config.useReplication())
224 printf("Starting Replication Server\n");
225 char execName[1024];
226 sprintf(execName, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
227 printf("filename is %s\n", execName);
228 replpid = os::createProcess(execName, "-s");
229 if (replpid != -1)
230 printf("Repl Server Started pid=%d\n", replpid);
233 if (Conf::config.useCache() && Conf::config.useTwoWayCache()) startCacheServer();
235 printf("Database server started\n");
237 while(!srvStop)
239 tval.tv_sec = timeout.tv_sec;
240 tval.tv_usec = timeout.tv_usec;
241 os::select(0, 0, 0, 0, &tval);
243 //send signal to all the registered process..check they are alive
244 cleanupDeadProcs(sysdb);
246 //TODO::check repl server is alive, if not restart it
248 //TODO::if it fails to start 5 times, exit
249 if (cachepid !=0 && checkDead(cachepid)) startCacheServer();
252 os::kill(cachepid, SIGTERM);
253 dumpData();
254 logFine(logger, "Server Exiting");
255 logActiveProcs(sysdb);
256 printf("Server Exiting\n");
257 logFine(logger, "Server Ended");
258 logger.stopLogger();
259 session.destroySystemDatabase();
260 return 0;
262 void dumpData()
264 char cmd[1024];
265 sprintf(cmd, "csqldump >%s",Conf::config.getDbFile());
266 int ret = system(cmd);
267 if (ret != 0) {
268 printf("Table cannot be written. Recovery will fail\n");
270 return;