code reorg
[csql.git] / src / tools / csqlcheckpointserver.cxx
blob40510356afd3c4d83f8eb5d807534606fba33628
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 <AbsSqlConnection.h>
17 #include <AbsSqlStatement.h>
18 #include <SqlConnection.h>
19 #include <SqlStatement.h>
20 #include <SqlFactory.h>
21 #include <CSql.h>
22 #include <SessionImpl.h>
23 #include <NanoTimer.h>
25 int srvStop =0;
26 static void sigTermHandler(int sig)
28 printf("Received signal %d\nStopping the server\n", sig);
29 srvStop = 1;
31 static void sigChildHandler(int sig)
33 os::signal(SIGCHLD, sigChildHandler);
34 int stat;
35 waitpid(-1, &stat, WNOHANG);
36 //TODO::move waitpid to os wrapper
40 void printUsage()
42 printf("Usage: csqlcheckpointserver \n");
43 printf("Description: Start the csql checkpoint server.\n");
44 return;
46 DbRetVal takeCheckpoint()
48 char fileName[MAX_FILE_LEN];
49 sprintf(fileName, "%s/csql.db.cur", Conf::config.getDbFile());
50 if (::access(fileName, F_OK) != 0 ) return OK;
51 int fileSize = os::getFileSize(fileName);
52 logFine(Conf::logger, "Redo log file size %d", fileSize);
53 if (fileSize < Conf::config.getChkptRedoLimit()) return OK;
55 Connection conn;
56 DbRetVal rv = conn.open(I_USER, I_PASS);
57 if (rv != OK) return rv;
58 DatabaseManager *dbMgr = conn.getDatabaseManager();
59 NanoTimer timer;
60 timer.reset();
61 timer.start();
62 int tries = 0, totalTries=10;
63 while(tries < totalTries) {
64 rv = dbMgr->checkPoint();
65 if (rv == ErrLoadingOn) {
66 conn.close();
67 timer.stop();
68 return rv;
70 if (rv == ErrLockTimeOut) {
71 os::sleep(1); //sleep for 1 second
72 } else if (rv != OK) {
73 printError(rv, "checkpoint: failed");
74 conn.close();
75 timer.stop();
76 return rv;
77 }else if (rv == OK) break;
78 tries++;
80 timer.stop();
81 if (rv != ErrLockTimeOut) {
82 logFine(Conf::logger, "Checkpoint taken %lld microsecs", timer.avg()/1000);
83 //printf("Time Taken for checkpoint %lld\n", timer.avg()/1000/1000);
84 } else {
85 logFine(Conf::logger, "Checkpoint:Could not get TXN consistent point");
87 conn.close();
88 return OK;
90 int main(int argc, char **argv)
92 int c = 0, opt = 0;
93 while ((c = getopt(argc, argv, "?")) != EOF)
95 switch (c)
97 case '?' : { opt = 10; break; } //print help
98 default: opt=10;
101 }//while options
103 if (opt == 10) {
104 printUsage();
105 return 0;
107 SessionImpl session;
108 DbRetVal rv = session.readConfigFile();
109 if (rv != OK)
111 printError(ErrSysInternal,"Unable to read the configuration file \n");
112 return 1;
114 os::signal(SIGINT, sigTermHandler);
115 os::signal(SIGTERM, sigTermHandler);
116 os::signal(SIGCHLD, sigChildHandler);
118 if (!Conf::config.useDurability())
120 printError(ErrSysInternal, "Durablity is set to OFF in csql.conf file\n");
121 return 1;
123 logFine(Conf::logger, "CSQL Checkpoint server started");
124 int ret = 0;
125 struct timeval timeout, tval;
126 timeout.tv_sec = Conf::config.getChkptSecs();
127 tval.tv_usec = 0;
129 while(!srvStop)
131 tval.tv_sec = timeout.tv_sec;
132 ret = os::select(0, 0, 0, 0, &tval);
133 if (srvStop) break;
134 takeCheckpoint();
136 printf("Csql Checkpoint Daemon Exiting\n");
137 logFine(Conf::logger, "CSQL Checkpoint server exiting");
138 return 0;