two way caching changes related to config, deamon
[csql.git] / src / tools / csqlserver.cxx
blob4894d003565fcf1c022ffb5a1867613cfb0a518f
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=0;
27 pid_t cachepid=0;
28 static void sigTermHandler(int sig)
30 printf("Received signal %d\nStopping the server\n", sig);
31 srvStop = 1;
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();
52 srvStop = 1;
53 return ErrSysFatal;
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_);
66 info->init();
67 delete tm;
68 delete lm;
69 return OK;
72 DbRetVal cleanupDeadProcs(Database *sysdb)
74 DbRetVal rv = sysdb->getProcessTableMutex(false);
75 if (OK != rv)
77 printError(rv,"Unable to get process table mutex");
78 return rv;
80 pid_t pid;
81 pid = os::getpid();
82 pthread_t thrid = os::getthrid();
85 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
86 int i=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);
92 pInfo++;
94 sysdb->releaseProcessTableMutex(false);
95 return OK;
99 DbRetVal logActiveProcs(Database *sysdb)
101 DbRetVal rv = sysdb->getProcessTableMutex(false);
102 if (OK != rv)
104 printError(rv,"Unable to get process table mutex");
105 return rv;
107 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
108 int i=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_);
113 pInfo++;
115 sysdb->releaseProcessTableMutex(false);
116 return OK;
118 void startCacheServer()
120 printf("Starting Cache Recv Server\n");
121 char execName[1024];
122 sprintf(execName, "%s/bin/csqlcacheserver", os::getenv("CSQL_INSTALL_ROOT"));
123 printf("filename is %s\n", execName);
124 cachepid = os::createProcess(execName, "-s");
125 if (cachepid != -1)
126 printf("Cache Recv Server Started pid=%d\n", replpid);
127 return;
130 void printUsage()
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");
136 return;
138 int main(int argc, char **argv)
140 int c = 0, opt = 0;
141 while ((c = getopt(argc, argv, "cv?")) != EOF)
143 switch (c)
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
148 default: opt=10;
151 }//while options
153 if (opt == 10) {
154 printUsage();
155 return 0;
156 }else if (opt ==2) {
157 printf("%s\n",version);
158 return 0;
161 SessionImpl session;
162 DbRetVal rv = session.readConfigFile();
163 if (rv != OK)
165 printf("Unable to read the configuration file \n");
166 return 1;
168 os::signal(SIGINT, sigTermHandler);
169 os::signal(SIGTERM, sigTermHandler);
170 rv = logger.startLogger(Conf::config.getLogFile(), true);
171 if (rv != OK)
173 printf("Unable to start the logger\n");
174 return 2;
177 logFine(logger, "Server Started");
178 int ret = session.initSystemDatabase();
179 if (0 != ret)
181 printf(" System Database Initialization failed\n");
182 return 3;
184 printf("System Database initialized\n");
187 bool end = false;
189 struct timeval timeout, tval;
190 timeout.tv_sec = 5;
191 timeout.tv_usec = 0;
192 Database* sysdb = session.getSystemDatabase();
193 if (opt == 1) {
194 if (Conf::config.useCache()) {
195 printf("Database server recovering cached tables...\n");
196 int ret = system("cachetable -U root -P manager -R");
197 if (ret != 0) {
198 printf("Cached Tables recovery failed %d\n", ret);
199 logger.stopLogger();
200 session.destroySystemDatabase();
201 return 2;
203 printf("Cached Tables recovered\n");
204 } else {
205 printf("Cache mode is not set in csql.conf. Cannot recover\n");
206 logger.stopLogger();
207 session.destroySystemDatabase();
208 return 1;
211 if (Conf::config.useReplication())
213 printf("Starting Replication Server\n");
214 char execName[1024];
215 sprintf(execName, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
216 printf("filename is %s\n", execName);
217 replpid = os::createProcess(execName, "-s");
218 if (replpid != -1)
219 printf("Repl Server Started pid=%d\n", replpid);
222 if (Conf::config.useCache() && Conf::config.useTwoWayCache()) startCacheServer();
224 printf("Database server started\n");
226 while(!srvStop)
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");
246 logger.stopLogger();
247 session.destroySystemDatabase();
248 return 0;