reverting back the changes. causes core dump
[csql.git] / src / tools / csqlcheckpointserver.cxx
blobabd210054625edbf7cb4d17ffb9fdc94f297139f
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 == ErrLoadingOn) {
58 conn.close();
59 timer.stop();
60 return rv;
62 if (rv == ErrLockTimeOut) {
63 os::sleep(1); //sleep for 1 second
64 } else if (rv != OK) {
65 printError(rv, "checkpoint: failed");
66 conn.close();
67 timer.stop();
68 return rv;
69 }else if (rv == OK) break;
70 tries++;
72 timer.stop();
73 if (rv != ErrLockTimeOut) {
74 logFine(Conf::logger, "Checkpoint taken %lld microsecs", timer.avg()/1000);
75 //printf("Time Taken for checkpoint %lld\n", timer.avg()/1000/1000);
76 } else {
77 logFine(Conf::logger, "Checkpoint:Could not get TXN consistent point");
79 conn.close();
80 return OK;
82 int main(int argc, char **argv)
84 int c = 0, opt = 0;
85 while ((c = getopt(argc, argv, "?")) != EOF)
87 switch (c)
89 case '?' : { opt = 10; break; } //print help
90 default: opt=10;
93 }//while options
95 if (opt == 10) {
96 printUsage();
97 return 0;
99 SessionImpl session;
100 DbRetVal rv = session.readConfigFile();
101 if (rv != OK)
103 printError(ErrSysInternal,"Unable to read the configuration file \n");
104 return 1;
106 os::signal(SIGINT, sigTermHandler);
107 os::signal(SIGTERM, sigTermHandler);
108 os::signal(SIGCHLD, SIG_IGN);
110 if (!Conf::config.useDurability())
112 printError(ErrSysInternal, "Durablity is set to OFF in csql.conf file\n");
113 return 1;
115 logFine(Conf::logger, "CSQL Checkpoint server started");
116 int ret = 0;
117 struct timeval timeout, tval;
118 timeout.tv_sec = Conf::config.getChkptSecs();
119 tval.tv_usec = 0;
121 while(!srvStop)
123 tval.tv_sec = timeout.tv_sec;
124 ret = os::select(0, 0, 0, 0, &tval);
125 if (srvStop) break;
126 takeCheckpoint();
128 printf("Csql Checkpoint Daemon Exiting\n");
129 logFine(Conf::logger, "CSQL Checkpoint server exiting");
130 return 0;