two way caching changes related to config, deamon
[csql.git] / src / server / Config.cxx
blobff7c97b7c3216b05c14898bb4d97b245d984d19a
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<Config.h>
17 #include<ErrorType.h>
18 #include<Debug.h>
20 Config Conf::config;
22 int Config::readLine(FILE *fp, char * buffer)
24 char c =0;
25 int count =0;
26 while (true)
28 c = fgetc(fp);
29 if (c == '\n') break;
30 if (c == EOF) return EOF;
31 buffer[count++] = c;
33 return count;
35 int Config::storeKeyVal(char *key, char *value)
37 if (strcasestr(key, "PAGE_SIZE") != NULL )
38 { cVal.pageSize = atoi(value); }
39 else if (strcasestr(key, "MAX_PROCS") != NULL)
40 { cVal.maxProcs = atoi(value); }
41 else if (strcasestr(key, "MAX_SYS_DB_SIZE") != NULL)
42 { cVal.maxSysSize = atol(value); }
43 else if (strcasestr(key, "MAX_DB_SIZE") != NULL)
44 { cVal.maxDbSize = atol(value); }
45 else if (strcasestr(key, "SYS_DB_KEY") != NULL)
46 { cVal.sysDbKey = atoi(value); }
47 else if (strcasestr(key, "USER_DB_KEY") != NULL)
48 { cVal.userDbKey = atoi(value); }
49 else if (strcasestr(key, "LOG_FILE") != NULL)
50 { strcpy(cVal.logFile , value); }
51 else if (strcasestr(key, "MAP_ADDRESS") != NULL)
52 { cVal.mapAddr = atol(value); }
53 else if (strcasestr(key, "MUTEX_TIMEOUT_SECS") != NULL)
54 { cVal.mutexSecs = atoi(value); }
55 else if (strcasestr(key, "MUTEX_TIMEOUT_USECS") != NULL)
56 { cVal.mutexUSecs = atoi(value); }
57 else if (strcasestr(key, "MUTEX_TIMEOUT_RETRIES") != NULL)
58 { cVal.mutexRetries = atoi(value); }
59 else if (strcasestr(key, "LOCK_TIMEOUT_SECS") != NULL)
60 { cVal.lockSecs = atoi(value); }
61 else if (strcasestr(key, "LOCK_TIMEOUT_USECS") != NULL)
62 { cVal.lockUSecs = atoi(value); }
63 else if (strcasestr(key, "LOCK_TIMEOUT_RETRIES") != NULL)
64 { cVal.lockRetries = atoi(value); }
65 else if (strcasestr(key, "DSN") != NULL)
66 { strcpy(cVal.dsn , value); }
67 else if (strcasestr(key, "TABLE_CONFIG_FILE") != NULL)
68 { strcpy(cVal.tableConfigFile , value); }
69 else if (strcasestr(key, "CACHE_TABLE") != NULL)
70 { cVal.isCache = os::atobool(value); }
71 else if (strcasestr(key, "REPLICATION") != NULL)
72 { cVal.isReplication = os::atobool(value); }
73 else if (strcasestr(key, "NETWORK_CONFIG_FILE") != NULL)
74 { strcpy(cVal.replConfigFile , value); }
75 else if (strcasestr(key, "MAX_LOG_STORE_SIZE") != NULL)
76 { cVal.logStoreSize = atol(value); }
77 else if (strcasestr(key, "MY_NETWORK_ID") != NULL)
78 { cVal.networkID = atoi(value); }
79 else if (strcasestr(key, "CACHE_NETWORK_ID") != NULL)
80 { cVal.cacheNetworkID = atoi(value); }
81 else if (strcasestr(key, "NETWORK_RESPONSE_TIMEOUT") != NULL)
82 { cVal.nwResponseTimeout = atoi(value); }
83 else if (strcasestr(key, "NETWORK_CONNECT_TIMEOUT") != NULL)
84 { cVal.nwConnectTimeout = atoi(value); }
85 else if (strcasestr(key, "ENABLE_BIDIRECTIONAL_CACHE") != NULL)
86 { cVal.isTwoWay = os::atobool(value); }
87 else if (strcasestr(key, "CACHE_RECEIVER_WAIT_SECS") != NULL)
88 { cVal.cacheWaitSecs = atoi(value); }
89 else return 1;
90 return 0;
92 int Config::validateValues()
94 if (cVal.pageSize < 8192 || cVal.pageSize > 1024 * 1024 * 10 )
96 printError(ErrBadArg, "PAGE_SIZE should be >= 8192 and <= 10 MB");
97 return 1;
99 if (cVal.pageSize % 1024 !=0 )
101 printError(ErrBadArg, "PAGE_SIZE should be multiples of 1024");
102 return 1;
104 if (cVal.maxProcs < 10 || cVal.maxProcs > 8192)
106 printError(ErrBadArg, "MAX_PROCS should be >= 10 and <= 8192");
107 return 1;
109 if (cVal.maxSysSize < 1024 * 1024 || cVal.maxSysSize > 1024 *1024 *1024)
111 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be >= 1 MB and <= 1 GB");
112 return 1;
114 if (cVal.maxSysSize % 8192 !=0 )
116 printError(ErrBadArg, "MAX_SYS_DB_SIZE should be multiples of 8192");
117 return 1;
120 if (cVal.maxDbSize < 1024 * 1024 || cVal.maxDbSize > (1024*1024*1024))
122 printError(ErrBadArg, "MAX_DB_SIZE should be >= 1 MB and <= 2 GB");
123 return 1;
125 if (cVal.maxDbSize % 8192 !=0)
127 printError(ErrBadArg, "MAX_DB_SIZE should be multiples of 8192");
128 return 1;
131 if (cVal.sysDbKey < 10 || cVal.sysDbKey > 8192)
133 printError(ErrBadArg, "SYS_DB_KEY should be >= 10 and <= 8192");
134 return 1;
136 if (cVal.userDbKey < 10 || cVal.userDbKey > 8192)
138 printError(ErrBadArg, "USER_DB_KEY should be >= 10 and <= 8192");
139 return 1;
141 if ( cVal.sysDbKey == cVal.userDbKey)
143 printError(ErrBadArg, "USER_DB_KEY and SYS_DB_KEY have same value %d", cVal.userDbKey);
144 return 1;
146 if (0 == strcmp(cVal.logFile,""))
148 //TODO::check whether file exists
149 printError(ErrBadArg, "LOG_FILE is set to NULL");
150 return 1;
152 //TODO::Check for the existence of the log file
153 if (cVal.mapAddr < 400000000 || cVal.mapAddr > 2000000000)
155 printError(ErrBadArg, "MAP_ADDRESS should be >= 400000000 and <= 2000000000");
156 return 1;
158 if (cVal.mutexSecs < 0 || cVal.mutexSecs > 360)
160 printError(ErrBadArg, "MUTEX_TIMEOUT_SECS should be >= 0 and <= 360");
161 return 1;
163 if (cVal.mutexUSecs < 0 || cVal.mutexUSecs > 1000000)
165 printError(ErrBadArg, "MUTEX_TIMEOUT_USECS should be >= 0 and <= 1000000");
166 return 1;
168 if (cVal.mutexRetries < 0 || cVal.mutexRetries > 100)
170 printError(ErrBadArg, "MUTEX_TIMEOUT_RETRY should be >= 0 and <= 100");
171 return 1;
173 if (cVal.lockSecs < 0 || cVal.lockSecs > 360)
175 printError(ErrBadArg, "LOCK_TIMEOUT_SECS should be >= 0 and <= 360");
176 return 1;
178 if (cVal.lockUSecs < 0 || cVal.lockUSecs > 1000000)
180 printError(ErrBadArg, "LOCK_TIMEOUT_USECS should be >= 0 and <= 1000000");
181 return 1;
183 if (cVal.lockRetries < 0 || cVal.lockRetries > 100)
185 printError(ErrBadArg, "LOCK_TIMEOUT_RETRY should be >= 0 and <= 100");
186 return 1;
188 if (cVal.isCache && cVal.isReplication) {
189 printError(ErrBadArg, "Either caching or replication option should be set."
190 " Both options are not supported together");
191 return 1;
193 if (cVal.isCache) {
194 if (0 == strcmp(cVal.dsn,""))
196 printError(ErrBadArg, "DSN is set to NULL");
197 return 1;
200 if (cVal.isReplication || cVal.isCache) {
201 if (0 == strcmp(cVal.replConfigFile,""))
203 //TODO::check whether file exists
204 printError(ErrBadArg, "NETWORK_CONFIG_FILE is set to NULL");
205 return 1;
207 if (0 == strcmp(cVal.tableConfigFile,""))
209 //TODO::check whether file exists
210 printError(ErrBadArg, "TABLE_CONFIG_FILE is set to NULL");
211 return 1;
213 /*FILE *fp = fopen(cVal.replConfigFile,"r");
214 if( fp == NULL ) {
215 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
216 return 1;
218 int count =0;
219 int nwid, port;
220 char hostname[IDENTIFIER_LENGTH];
221 char nwmode;
223 while(!feof(fp)) {
224 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
225 count++;
227 if (count >2) {
228 printError(ErrSysInit, "NETWORK_CONFIG_FILE has more than 2 entries\n");
229 return 1;
233 /*if (cVal.isCache)
236 if (cVal.cacheNetworkID == -1)
238 printError(ErrBadArg, "CACHE_NETWORK_ID should not be -1");
239 return 1;
240 }else {
241 FILE *fp;
242 int nwid;
243 char hostname[IDENTIFIER_LENGTH];
244 char nwmode;
245 int port;
246 fp = fopen(Conf::config.getReplConfigFile(),"r");
247 if( fp == NULL ) {
248 printError(ErrSysInit, "Invalid path/filename for NETWORK_CONFIG_FILE.\n");
249 return 1;
251 bool found = false;
252 while(!feof(fp)) {
253 fscanf(fp, "%d:%d:%s\n", &nwid, &port, hostname);
254 if (cVal.cacheNetworkID == nwid) found = true;
256 if (!found) return 1;
259 if (cVal.logStoreSize < 1024 * 1024 || cVal.logStoreSize > 1024 *1024 *1024)
261 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be >= 1 MB and <= 1 GB");
262 return 1;
264 if (cVal.logStoreSize % 8192 !=0)
266 printError(ErrBadArg, "MAX_LOG_STORE_SIZE should be multiples of 8192");
267 return 1;
269 if (cVal.nwResponseTimeout <0 || cVal.nwResponseTimeout > 60)
271 printError(ErrBadArg, "NETWORK_RESPONSE_TIMEOUT should be 0 to 60");
272 return 1;
274 if (cVal.nwConnectTimeout <0 || cVal.nwConnectTimeout > 60)
276 printError(ErrBadArg, "NETWORK_CONNECT_TIMEOUT should be 0 to 60");
277 return 1;
279 if (cVal.cacheWaitSecs <1)
281 printError(ErrBadArg, "CACHE_RECEIVER_WAIT_SECS should be >1");
282 return 1;
284 return 0;
287 int Config::readAllValues(char *fileName)
289 FILE *fp;
291 fp = fopen(fileName,"r");
292 if( fp == NULL ) {
293 printError(ErrSysInit, "Invalid path/filename in CSQL_CONFIG_FILE.");
294 return 1;
297 int hasData = 1;
298 char buffer[1024];
299 char key[1024];
300 char value[1024];
301 while (hasData)
303 memset(buffer, 0, 1024);
304 //int ret = fscanf(fp,"%s\r",buffer);
305 int ret = readLine(fp, buffer);
306 if (ret == EOF) break;
307 bool isComment= false;
308 int posEqual =0;
309 for (int i = 0; i <1024; i++)
311 if (buffer[i] == '=' ) posEqual=i;
312 else if (buffer[i] == '#' ) { isComment = true; break; }
313 else if (buffer[i] == '\n') { break; }
314 else if (buffer[i] == '\0') { break; }
316 if (isComment) continue;
317 if (!posEqual) continue;
318 strncpy(key, buffer, posEqual);
319 key[posEqual] = '\0';
320 posEqual++;
321 strcpy(value, &buffer[posEqual]);
322 storeKeyVal(key, value);
324 fclose(fp);
325 if (validateValues())
327 return 1;
330 return 0;
332 void Config::print()
334 printf("ConfigValues\n");
335 printf(" getPageSize %d\n", getPageSize());
336 printf(" getMaxProcs %d\n", getMaxProcs());
337 printf(" getMaxSysDbSize %ld\n", getMaxSysDbSize());
338 printf(" getMaxDbSize %ld\n", getMaxDbSize());
339 printf(" getSysDbKey %d\n", getSysDbKey());
340 printf(" getUserDbKey %d\n", getUserDbKey());
341 printf(" getLogFile %s\n", getLogFile());
342 printf(" getMapAddress %ld\n", getMapAddress());
343 printf(" getMutexSecs %d\n", getMutexSecs());
344 printf(" getMutexUSecs %d\n", getMutexUSecs());
345 printf(" getMutexRetries %d\n", getMutexRetries());
346 printf(" getLockSecs %d\n", getLockSecs());
347 printf(" getLockUSecs %d\n", getLockUSecs());
348 printf(" getLockRetries %d\n", getLockRetries());
349 printf(" useCache %d\n", useCache());
350 printf(" getDSN %s\n", getDSN());
351 printf(" getTableConfigFile %s\n", getTableConfigFile());
352 printf(" isTwoWayCache %d\n", useTwoWayCache());
353 printf(" getCacheWaitSecs %d\n", getCacheWaitSecs());
354 //printf(" useReplication %d\n", useReplication());
355 //printf(" getReplConfigFile %s\n", getReplConfigFile());
356 //printf(" getMaxLogStoreSize %ld\n", getMaxLogStoreSize());
357 //printf(" getNetworkID %d\n", getNetworkID());
358 //printf(" getCacheNetworkID %d\n", getCacheNetworkID());