When mysql server is down csqlserver -c fails.
[csql.git] / src / tools / csqlserver.cxx
blob771a2b8496ba154bdd53eeac2934ed72d94ce7fc
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-1.2Beta";
25 int srvStop =0;
26 pid_t replpid;
27 static void sigTermHandler(int sig)
29 printf("Received signal %d\nStopping the server\n", sig);
30 srvStop = 1;
33 bool checkDead(pid_t pid)
35 int ret = os::kill(pid, 0);
36 if (ret == -1) return true; else return false;
39 DbRetVal releaseAllResources(Database *sysdb, ThreadInfo *info )
41 printf("Releasing all the resources for process %d %lu\n", info->pid_, info->thrid_);
42 //recover for all the mutexes in has_
43 for (int i =0; i < MAX_MUTEX_PER_THREAD; i++)
45 if (info->has_[i] != NULL)
47 printf("Dead Procs: %d %lu holding mutex %x %s \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
48 logFine(logger, "Dead Procs: %d %lu holding mutex %x %s \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
49 //TODO::recovery of mutexes
50 //info->has_[i]->recoverMutex();
51 srvStop = 1;
52 return ErrSysFatal;
55 TransactionManager *tm = new TransactionManager();
56 LockManager *lm = new LockManager(sysdb);
57 for (int i = 0 ;i < MAX_THREADS_PER_PROCESS; i++)
59 if (info->thrTrans_[i].trans_ != NULL && info->thrTrans_[i].trans_->status_ == TransRunning)
61 printf("Rollback Transaction %x\n", info->thrTrans_[i].trans_);
62 tm->rollback(lm, info->thrTrans_[i].trans_);
65 info->init();
66 delete tm;
67 delete lm;
68 return OK;
71 DbRetVal cleanupDeadProcs(Database *sysdb)
73 DbRetVal rv = sysdb->getProcessTableMutex(false);
74 if (OK != rv)
76 printError(rv,"Unable to get process table mutex");
77 return rv;
79 pid_t pid;
80 pid = os::getpid();
81 pthread_t thrid = os::getthrid();
84 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
85 int i=0;
86 ThreadInfo* freeSlot = NULL;
87 for (; i < Conf::config.getMaxProcs(); i++)
89 //check whether it is alive
90 if (pInfo->pid_ !=0 && checkDead(pInfo->pid_)) releaseAllResources(sysdb, pInfo);
91 pInfo++;
93 sysdb->releaseProcessTableMutex(false);
94 return OK;
98 DbRetVal logActiveProcs(Database *sysdb)
100 DbRetVal rv = sysdb->getProcessTableMutex(false);
101 if (OK != rv)
103 printError(rv,"Unable to get process table mutex");
104 return rv;
106 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
107 int i=0;
108 ThreadInfo* freeSlot = NULL;
109 for (; i < Conf::config.getMaxProcs(); i++)
111 if (pInfo->pid_ !=0 ) logFine(logger, "Registered Procs: %d %lu\n", pInfo->pid_, pInfo->thrid_);
112 pInfo++;
114 sysdb->releaseProcessTableMutex(false);
115 return OK;
119 void printUsage()
121 printf("Usage: csqlserver [-c] [-v]\n");
122 printf(" v -> print the version.\n");
123 printf(" c -> recover all cached tables from the target database.\n");
124 printf("Description: Start the csql server and initialize the database.\n");
125 return;
127 int main(int argc, char **argv)
129 int c = 0, opt = 0;
130 while ((c = getopt(argc, argv, "cv?")) != EOF)
132 switch (c)
134 case '?' : { opt = 10; break; } //print help
135 case 'c' : { opt = 1; break; } //recover all the tables from cache
136 case 'v' : { opt = 2; break; } //print version
137 default: opt=10;
140 }//while options
142 if (opt == 10) {
143 printUsage();
144 return 0;
145 }else if (opt ==2) {
146 printf("%s\n",version);
147 return 0;
150 SessionImpl session;
151 DbRetVal rv = session.readConfigFile();
152 if (rv != OK)
154 printf("Unable to read the configuration file \n");
155 return 1;
157 os::signal(SIGINT, sigTermHandler);
158 os::signal(SIGTERM, sigTermHandler);
159 rv = logger.startLogger(Conf::config.getLogFile(), true);
160 if (rv != OK)
162 printf("Unable to start the logger\n");
163 return 2;
166 logFine(logger, "Server Started");
167 int ret = session.initSystemDatabase();
168 if (0 != ret)
170 printf(" System Database Initialization failed\n");
171 return 3;
173 printf("System Database initialized\n");
176 bool end = false;
178 struct timeval timeout, tval;
179 timeout.tv_sec = 5;
180 timeout.tv_usec = 0;
181 Database* sysdb = session.getSystemDatabase();
182 if (opt == 1) {
183 if (Conf::config.useCache()) {
184 printf("Database server recovering cached tables...\n");
185 int ret = system("cachetable -U root -P manager -R");
186 if (ret != 0) {
187 printf("Cached Tables recovery failed %d\n", ret);
188 logger.stopLogger();
189 session.destroySystemDatabase();
190 return 2;
192 printf("Cached Tables recovered\n");
193 } else {
194 printf("Cache mode is not set in csql.conf. Cannot recover\n");
195 logger.stopLogger();
196 session.destroySystemDatabase();
197 return 1;
200 if (Conf::config.useReplication())
202 printf("Starting Replication Server\n");
203 char execName[1024];
204 sprintf(execName, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
205 printf("filename is %s\n", execName);
206 replpid = os::createProcess(execName, "-s");
207 if (replpid != -1)
208 printf("Repl Server Started pid=%d\n", replpid);
212 printf("Database server started\n");
214 while(!srvStop)
216 tval.tv_sec = timeout.tv_sec;
217 tval.tv_usec = timeout.tv_usec;
218 os::select(0, 0, 0, 0, &tval);
220 //send signal to all the registered process..check they are alive
221 cleanupDeadProcs(sysdb);
223 //TODO::check repl server is alive, if not restart it
226 //TODO::kill replication server process
228 logFine(logger, "Server Exiting");
229 logActiveProcs(sysdb);
230 printf("Server Exiting\n");
231 logFine(logger, "Server Ended");
232 logger.stopLogger();
233 session.destroySystemDatabase();
234 return 0;