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>
26 static void sigTermHandler(int sig
)
28 printf("Received signal %d\nStopping the server\n", sig
);
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();
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_
);
69 DbRetVal
cleanupDeadProcs(Database
*sysdb
)
71 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
74 printError(rv
,"Unable to get process table mutex");
79 pthread_t thrid
= os::getthrid();
82 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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
);
91 sysdb
->releaseProcessTableMutex(false);
96 DbRetVal
logActiveProcs(Database
*sysdb
)
98 DbRetVal rv
= sysdb
->getProcessTableMutex(false);
101 printError(rv
,"Unable to get process table mutex");
104 ThreadInfo
* pInfo
= sysdb
->getThreadInfo(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_
);
112 sysdb
->releaseProcessTableMutex(false);
121 DbRetVal rv
= session
.readConfigFile();
124 printf("Unable to read the configuration file \n");
127 os::signal(SIGINT
, sigTermHandler
);
128 os::signal(SIGTERM
, sigTermHandler
);
129 rv
= logger
.startLogger(Conf::config
.getLogFile(), true);
132 printf("Unable to start the logger\n");
136 logFine(logger
, "Server Started");
137 int ret
= session
.initSystemDatabase();
140 printf(" System Database Initialization failed\n");
143 printf("System Database initialized\n");
148 struct timeval timeout
, tval
;
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");
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");
176 session
.destroySystemDatabase();