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-2.0GA";
29 static void sigTermHandler(int sig
)
31 printf("Received signal %d\nStopping the server\n", sig
);
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
]);
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_
);
73 DbRetVal
cleanupDeadProcs(Database
*sysdb
)
75 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
78 printError(rv
,"Unable to get process table mutex");
83 pthread_t thrid
= os::getthrid();
86 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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
);
95 sysdb
->releaseProcessTableMutex(false);
100 DbRetVal
logActiveProcs(Database
*sysdb
)
102 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
105 printError(rv
,"Unable to get process table mutex");
108 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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_
);
116 sysdb
->releaseProcessTableMutex(false);
119 void startCacheServer()
121 printf("Starting Cache Recv Server\n");
123 sprintf(execName
, "%s/bin/csqlcacheserver", os::getenv("CSQL_INSTALL_ROOT"));
124 printf("filename is %s\n", execName
);
125 cachepid
= os::createProcess(execName
, "-s");
127 printf("Cache Recv Server Started pid=%d\n", cachepid
);
131 void startServiceClient()
133 printf("Starting Csql Network Daemon\n");
135 sprintf(execName
, "%s/bin/csqlsqlserver", os::getenv("CSQL_INSTALL_ROOT"));
136 printf("filename is %s\n", execName
);
137 cachepid
= os::createProcess(execName
, "-s");
139 printf("Csql Network Daemon Started pid=%d\n", cachepid
);
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");
151 int main(int argc
, char **argv
)
154 while ((c
= getopt(argc
, argv
, "cv?")) != EOF
)
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
170 printf("%s\n",version
);
175 DbRetVal rv
= session
.readConfigFile();
178 printf("Unable to read the configuration file \n");
181 os::signal(SIGINT
, sigTermHandler
);
182 os::signal(SIGTERM
, sigTermHandler
);
183 rv
= logger
.startLogger(Conf::config
.getLogFile(), true);
186 printf("Unable to start the logger\n");
190 logFine(logger
, "Server Started");
191 int ret
= session
.initSystemDatabase();
194 printf(" System Database Initialization failed\n");
197 printf("System Database initialized\n");
202 struct timeval timeout
, tval
;
205 Database
* sysdb
= session
.getSystemDatabase();
206 if (FILE *file
= fopen(Conf::config
.getDbFile(), "r"))
210 sprintf(cmd
, "csql -S -s %s",Conf::config
.getDbFile());
211 int ret
= system(cmd
);
213 printf("Tables cannot be recovered. DB file corrupted\n");
217 if (Conf::config
.useCache()) {
218 printf("Database server recovering cached tables...\n");
219 int ret
= system("cachetable -U root -P manager -R");
221 printf("Cached Tables recovery failed %d\n", ret
);
223 session
.destroySystemDatabase();
226 printf("Cached Tables recovered\n");
228 printf("Cache mode is not set in csql.conf. Cannot recover\n");
230 session
.destroySystemDatabase();
235 if (Conf::config
.useReplication())
237 printf("Starting Replication Server\n");
239 sprintf(execName
, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
240 printf("filename is %s\n", execName
);
241 replpid
= os::createProcess(execName
, "-s");
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 startServiceClient();
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
);
268 logFine(logger
, "Server Exiting");
269 logActiveProcs(sysdb
);
270 printf("Server Exiting\n");
271 logFine(logger
, "Server Ended");
273 session
.destroySystemDatabase();
279 sprintf(cmd
, "csqldump >%s",Conf::config
.getDbFile());
280 int ret
= system(cmd
);
282 printf("Table cannot be written. Recovery will fail\n");