00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include<CSql.h>
00017 #include<SessionImpl.h>
00018 #include<Debug.h>
00019 #include<Process.h>
00020 #include<Database.h>
00021 #include<Transaction.h>
00022 #include<Lock.h>
00023 #include<CacheTableLoader.h>
00024
00025 int srvStop =0;
00026 static void sigTermHandler(int sig)
00027 {
00028 printf("Received signal %d\nStopping the server\n", sig);
00029 srvStop = 1;
00030 }
00031
00032 bool checkDead(pid_t pid)
00033 {
00034 int ret = os::kill(pid, 0);
00035 if (ret == -1) return true; else return false;
00036 }
00037
00038 DbRetVal releaseAllResources(Database *sysdb, ThreadInfo *info )
00039 {
00040 printf("Releasing all the resources for process %d %lu\n", info->pid_, info->thrid_);
00041
00042 for (int i =0; i < MAX_MUTEX_PER_THREAD; i++)
00043 {
00044 if (info->has_[i] != NULL)
00045 {
00046 printf("Dead Procs: %d %lu holding mutex %x %s) \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
00047 logFine(logger, "Dead Procs: %d %lu holding mutex %x %s) \n", info->pid_, info->thrid_, info->has_[i], info->has_[i]->name);
00048
00049
00050 srvStop = 1;
00051 }
00052 }
00053 TransactionManager *tm = new TransactionManager();
00054 LockManager *lm = new LockManager(sysdb);
00055 for (int i = 0 ;i < MAX_THREADS_PER_PROCESS; i++)
00056 {
00057 if (info->thrTrans_[i].trans_ != NULL && info->thrTrans_[i].trans_->status_ == TransRunning)
00058 {
00059 printf("Rollback Transaction %x\n", info->thrTrans_[i].trans_);
00060 tm->rollback(lm, info->thrTrans_[i].trans_);
00061 }
00062 }
00063 info->init();
00064 delete tm;
00065 delete lm;
00066 return OK;
00067 }
00068
00069 DbRetVal cleanupDeadProcs(Database *sysdb)
00070 {
00071 DbRetVal rv = sysdb->getProcessTableMutex(false);
00072 if (OK != rv)
00073 {
00074 printError(rv,"Unable to get process table mutex");
00075 return rv;
00076 }
00077 pid_t pid;
00078 pid = os::getpid();
00079 pthread_t thrid = os::getthrid();
00080
00081
00082 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
00083 int i=0;
00084 ThreadInfo* freeSlot = NULL;
00085 for (; i < Conf::config.getMaxProcs(); i++)
00086 {
00087
00088 if (pInfo->pid_ !=0 && checkDead(pInfo->pid_)) releaseAllResources(sysdb, pInfo);
00089 pInfo++;
00090 }
00091 sysdb->releaseProcessTableMutex(false);
00092 return OK;
00093 }
00094
00095
00096 DbRetVal logActiveProcs(Database *sysdb)
00097 {
00098 DbRetVal rv = sysdb->getProcessTableMutex(false);
00099 if (OK != rv)
00100 {
00101 printError(rv,"Unable to get process table mutex");
00102 return rv;
00103 }
00104 ThreadInfo* pInfo = sysdb->getThreadInfo(0);
00105 int i=0;
00106 ThreadInfo* freeSlot = NULL;
00107 for (; i < Conf::config.getMaxProcs(); i++)
00108 {
00109 if (pInfo->pid_ !=0 ) logFine(logger, "Registered Procs: %d %lu\n", pInfo->pid_, pInfo->thrid_);
00110 pInfo++;
00111 }
00112 sysdb->releaseProcessTableMutex(false);
00113 return OK;
00114 }
00115
00116
00117
00118 int main()
00119 {
00120 SessionImpl session;
00121 DbRetVal rv = session.readConfigFile();
00122 if (rv != OK)
00123 {
00124 printf("Unable to read the configuration file \n");
00125 return 1;
00126 }
00127 os::signal(SIGINT, sigTermHandler);
00128 os::signal(SIGTERM, sigTermHandler);
00129 rv = logger.startLogger(Conf::config.getLogFile(), true);
00130 if (rv != OK)
00131 {
00132 printf("Unable to start the logger\n");
00133 return 2;
00134 }
00135
00136 logFine(logger, "Server Started");
00137 int ret = session.initSystemDatabase();
00138 if (0 != ret)
00139 {
00140 printf(" System Database Initialization failed\n");
00141 return 3;
00142 }
00143 printf("System Database initialized\n");
00144
00145
00146 bool end = false;
00147
00148 struct timeval timeout, tval;
00149 timeout.tv_sec = 5;
00150 timeout.tv_usec = 0;
00151 Database* sysdb = session.getSystemDatabase();
00152 printf("Database server recovering cached tables...\n");
00153
00154 system("cachetable -u root -p manager -r");
00155
00156 printf("Cached Tables recovered\n");
00157
00158 printf("Database server started\n");
00159
00160 while(!srvStop)
00161 {
00162 tval.tv_sec = timeout.tv_sec;
00163 tval.tv_usec = timeout.tv_usec;
00164 os::select(0, 0, 0, 0, &tval);
00165
00166
00167 cleanupDeadProcs(sysdb);
00168
00169 }
00170
00171 logFine(logger, "Server Exiting");
00172 logActiveProcs(sysdb);
00173 printf("Server Exiting\n");
00174 logFine(logger, "Server Ended");
00175 logger.stopLogger();
00176 session.destroySystemDatabase();
00177 return 0;
00178 }