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-1.2Beta";
27 static void sigTermHandler(int sig
)
29 printf("Received signal %d\nStopping the server\n", sig
);
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();
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_
);
71 DbRetVal
cleanupDeadProcs(Database
*sysdb
)
73 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
76 printError(rv
,"Unable to get process table mutex");
81 pthread_t thrid
= os::getthrid();
84 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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
);
93 sysdb
->releaseProcessTableMutex(false);
98 DbRetVal
logActiveProcs(Database
*sysdb
)
100 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
103 printError(rv
,"Unable to get process table mutex");
106 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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_
);
114 sysdb
->releaseProcessTableMutex(false);
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");
127 int main(int argc
, char **argv
)
130 while ((c
= getopt(argc
, argv
, "cv?")) != EOF
)
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
146 printf("%s\n",version
);
151 DbRetVal rv
= session
.readConfigFile();
154 printf("Unable to read the configuration file \n");
157 os::signal(SIGINT
, sigTermHandler
);
158 os::signal(SIGTERM
, sigTermHandler
);
159 rv
= logger
.startLogger(Conf::config
.getLogFile(), true);
162 printf("Unable to start the logger\n");
166 logFine(logger
, "Server Started");
167 int ret
= session
.initSystemDatabase();
170 printf(" System Database Initialization failed\n");
173 printf("System Database initialized\n");
178 struct timeval timeout
, tval
;
181 Database
* sysdb
= session
.getSystemDatabase();
183 if (Conf::config
.useCache()) {
184 printf("Database server recovering cached tables...\n");
185 int ret
= system("cachetable -U root -P manager -R");
187 printf("Cached Tables recovery failed %d\n", ret
);
189 session
.destroySystemDatabase();
192 printf("Cached Tables recovered\n");
194 printf("Cache mode is not set in csql.conf. Cannot recover\n");
196 session
.destroySystemDatabase();
200 if (Conf::config
.useReplication())
202 printf("Starting Replication Server\n");
204 sprintf(execName
, "%s/bin/csqlreplserver", os::getenv("CSQL_INSTALL_ROOT"));
205 printf("filename is %s\n", execName
);
206 replpid
= os::createProcess(execName
, "-s");
208 printf("Repl Server Started pid=%d\n", replpid
);
212 printf("Database server started\n");
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");
233 session
.destroySystemDatabase();