checkpoint server changes
[csql.git] / src / tools / csqlcheckpointserver.cxx
blobf7b4a32b8ace3dbefbb47c7ba93ddbc513728e91
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;
32 void printUsage()
34 printf("Usage: csqlcheckpointserver \n");
35 printf("Description: Start the csql checkpoint server.\n");
36 return;
38 DbRetVal takeCheckpoint()
40 char fileName[MAX_FILE_LEN];
41 sprintf(fileName, "%s/csql.db.cur", Conf::config.getDbFile());
42 if (::access(fileName, F_OK) != 0 ) return OK;
43 int fileSize = os::getFileSize(fileName);
44 logFine(Conf::logger, "Redo log file size %d", fileSize);
45 if (fileSize < Conf::config.getChkptRedoLimit()) return OK;
47 Connection conn;
48 DbRetVal rv = conn.open(I_USER, I_PASS);
49 if (rv != OK) return rv;
50 DatabaseManager *dbMgr = conn.getDatabaseManager();
51 NanoTimer timer;
52 timer.reset();
53 timer.start();
54 int tries = 0, totalTries=10;
55 while(tries < totalTries) {
56 rv = dbMgr->checkPoint();
57 if (rv == ErrLockTimeOut) {
58 os::sleep(1); //sleep for 1 second
59 }else if (rv != OK) {
60 printError(rv, "checkpoint: failed");
61 conn.close();
62 timer.stop();
63 return rv;
64 }else if (rv == OK) break;
65 tries++;
67 timer.stop();
68 if (rv != ErrLockTimeOut) {
69 logFine(Conf::logger, "Checkpoint taken %lld microsecs", timer.avg()/1000);
70 //printf("Time Taken for checkpoint %lld\n", timer.avg()/1000/1000);
71 } else {
72 logFine(Conf::logger, "Checkpoint:Could not get TXN consistent point");
74 conn.close();
75 return OK;
77 int main(int argc, char **argv)
79 int c = 0, opt = 0;
80 while ((c = getopt(argc, argv, "?")) != EOF)
82 switch (c)
84 case '?' : { opt = 10; break; } //print help
85 default: opt=10;
88 }//while options
90 if (opt == 10) {
91 printUsage();
92 return 0;
94 SessionImpl session;
95 DbRetVal rv = session.readConfigFile();
96 if (rv != OK)
98 printError(ErrSysInternal,"Unable to read the configuration file \n");
99 return 1;
101 os::signal(SIGINT, sigTermHandler);
102 os::signal(SIGTERM, sigTermHandler);
103 os::signal(SIGCHLD, SIG_IGN);
105 if (!Conf::config.useDurability())
107 printError(ErrSysInternal, "Durablity is set to OFF in csql.conf file\n");
108 return 1;
110 logFine(Conf::logger, "CSQL Checkpoint server started");
111 int ret = 0;
112 struct timeval timeout, tval;
113 timeout.tv_sec = Conf::config.getChkptSecs();
114 tval.tv_usec = 0;
116 while(!srvStop)
118 tval.tv_sec = timeout.tv_sec;
119 ret = os::select(0, 0, 0, 0, &tval);
120 if (srvStop) break;
121 takeCheckpoint();
123 printf("Csql Checkpoint Daemon Exiting\n");
124 logFine(Conf::logger, "CSQL Checkpoint server exiting");
125 return 0;