code reorg
[csql.git] / src / server / Server.cxx
blob39e52fcd22509dc3faeeb085b2170061b376b031
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>
25 int srvStop =0;
26 static void sigTermHandler(int sig)
28 printf("Received signal %d\nStopping the server\n", sig);
29 srvStop = 1;
32 bool checkDead(pid_t pid)
34 int ret = os::kill(pid, 0);
35 if (ret == -1) return true; else return false;
38 DbRetVal releaseAllResources(Database *sysdb, ThreadInfo *info )
40 printf("Releasing all the resources for process %d %lu\n", info->pid_, info->thrid_);
41 //recover for all the mutexes in has_
42 for (int i =0; i < MAX_MUTEX_PER_THREAD; i++)
44 if (info->has_[i] != NULL)
46 printf("Dead Procs: %d %lu holding mutex %x %s) \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
47 logFine(logger, "Dead Procs: %d %lu holding mutex %x %s) \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
48 //TODO::recovery of mutexes
49 //info->has_[i]->recoverMutex();
50 srvStop = 1;
53 TransactionManager *tm = new TransactionManager();
54 LockManager *lm = new LockManager(sysdb);
55 for (int i = 0 ;i < MAX_THREADS_PER_PROCESS; i++)
57 if (info->thrTrans_[i].trans_ != NULL && info->thrTrans_[i].trans_->status_ == TransRunning)
59 printf("Rollback Transaction %x\n", info->thrTrans_[i].trans_);
60 tm->rollback(lm, info->thrTrans_[i].trans_);
63 info->init();
64 delete tm;
65 delete lm;
66 return OK;
69 DbRetVal cleanupDeadProcs(Database *sysdb)
71 DbRetVal rv = sysdb->getProcessTableMutex(false);
72 if (OK != rv)
74 printError(rv,"Unable to get process table mutex");
75 return rv;
77 pid_t pid;
78 pid = os::getpid();
79 pthread_t thrid = os::getthrid();
82 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
83 int i=0;
84 ThreadInfo* freeSlot = NULL;
85 for (; i < Conf::config.getMaxProcs(); i++)
87 //check whether it is alive
88 if (pInfo->pid_ !=0 && checkDead(pInfo->pid_)) releaseAllResources(sysdb, pInfo);
89 pInfo++;
91 sysdb->releaseProcessTableMutex(false);
92 return OK;
96 DbRetVal logActiveProcs(Database *sysdb)
98 DbRetVal rv = sysdb->getProcessTableMutex(false);
99 if (OK != rv)
101 printError(rv,"Unable to get process table mutex");
102 return rv;
104 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
105 int i=0;
106 ThreadInfo* freeSlot = NULL;
107 for (; i < Conf::config.getMaxProcs(); i++)
109 if (pInfo->pid_ !=0 ) logFine(logger, "Registered Procs: %d %lu\n", pInfo->pid_, pInfo->thrid_);
110 pInfo++;
112 sysdb->releaseProcessTableMutex(false);
113 return OK;
118 int main()
120 SessionImpl session;
121 DbRetVal rv = session.readConfigFile();
122 if (rv != OK)
124 printf("Unable to read the configuration file \n");
125 return 1;
127 os::signal(SIGINT, sigTermHandler);
128 os::signal(SIGTERM, sigTermHandler);
129 rv = logger.startLogger(Conf::config.getLogFile(), true);
130 if (rv != OK)
132 printf("Unable to start the logger\n");
133 return 2;
136 logFine(logger, "Server Started");
137 int ret = session.initSystemDatabase();
138 if (0 != ret)
140 printf(" System Database Initialization failed\n");
141 return 3;
143 printf("System Database initialized\n");
146 bool end = false;
148 struct timeval timeout, tval;
149 timeout.tv_sec = 5;
150 timeout.tv_usec = 0;
151 Database* sysdb = session.getSystemDatabase();
152 printf("Database server recovering cached tables...\n");
154 system("cachetable -u root -p manager -r");
156 printf("Cached Tables recovered\n");
158 printf("Database server started\n");
160 while(!srvStop)
162 tval.tv_sec = timeout.tv_sec;
163 tval.tv_usec = timeout.tv_usec;
164 os::select(0, 0, 0, 0, &tval);
166 //send signal to all the registered process..check they are alive
167 cleanupDeadProcs(sysdb);
171 logFine(logger, "Server Exiting");
172 logActiveProcs(sysdb);
173 printf("Server Exiting\n");
174 logFine(logger, "Server Ended");
175 logger.stopLogger();
176 session.destroySystemDatabase();
177 return 0;