4 * $Header: /cvsroot/pgpool/pgpool-II/pool_config.l,v 1.24 2009/01/25 10:13:15 t-ishii Exp $
6 * pgpool: a language independent connection pool server for PostgreSQL
7 * written by Tatsuo Ishii
9 * Copyright (c) 2003-2009 PgPool Global Development Group
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby
13 * granted, provided that the above copyright notice appear in all
14 * copies and that both that copyright notice and this permission
15 * notice appear in supporting documentation, and that the name of the
16 * author not be used in advertising or publicity pertaining to
17 * distribution of the software without specific, written prior
18 * permission. The author makes no representations about the
19 * suitability of this software for any purpose. It is provided "as
20 * is" without express or implied warranty.
22 * pool_config.l: read configuration file
34 #define CHECK_CONTEXT(mask, context) ((mask) & (context))
36 /* to shut off compiler warnings */
39 POOL_CONFIG *pool_config; /* configuration values */
40 POOL_SYSTEMDB_CONNECTION_POOL *system_db_info;
41 static unsigned Lineno;
42 static char *default_reset_query_list[] = {"ABORT", "RESET ALL", "SET SESSION AUTHORIZATION DEFAULT"};
55 static char *extract_string(char *value, POOL_TOKEN token);
56 static char **extract_string_tokens(char *str, char *delim, int *n);
57 static int eval_logical(char *str);
58 static void clear_host_entry(int slot);
63 %option never-interactive
71 INTEGER {SIGN}?({DIGIT}+|0x{HEXDIGIT}+)
73 EXPONENT [Ee]{SIGN}?{DIGIT}+
74 REAL {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
76 LETTER [A-Za-z_\200-\377]
77 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
79 KEY {LETTER}{LETTER_OR_DIGIT}*
81 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
82 STRING \'([^'\n]|\\.)*\'
86 \n Lineno++; return POOL_EOL;
87 [ \t\r]+ /* eat whitespace */
88 #.*$ /* eat comment */
90 {KEY} return POOL_KEY;
91 {STRING} return POOL_STRING;
92 {UNQUOTED_STRING} return POOL_UNQUOTED_STRING;
93 {INTEGER} return POOL_INTEGER;
94 {REAL} return POOL_REAL;
97 . return POOL_PARSE_ERROR;
101 int pool_init_config(void)
104 static char localhostname[256];
107 pool_config = malloc(sizeof(POOL_CONFIG));
108 if (pool_config == NULL)
110 pool_error("failed to allocate pool_config");
114 memset(pool_config, 0, sizeof(POOL_CONFIG));
116 pool_config->backend_desc = pool_shared_memory_create(sizeof(BackendDesc));
117 if (pool_config->backend_desc == NULL)
119 pool_error("failed to allocate pool_config->backend_desc");
123 /* set hardcoded default values */
124 pool_config->listen_addresses = "localhost";
125 pool_config->port = 9999;
126 pool_config->pcp_port = 9898;
127 pool_config->socket_dir = DEFAULT_SOCKET_DIR;
128 pool_config->pcp_socket_dir = DEFAULT_SOCKET_DIR;
129 pool_config->backend_socket_dir = DEFAULT_SOCKET_DIR;
130 pool_config->pcp_timeout = 10;
131 pool_config->num_init_children = 32;
132 pool_config->max_pool = 4;
133 pool_config->child_life_time = 300;
134 pool_config->client_idle_limit = 0;
135 pool_config->connection_life_time = 0;
136 pool_config->child_max_connections = 0;
137 pool_config->authentication_timeout = 60;
138 pool_config->logdir = DEFAULT_LOGDIR;
139 pool_config->pid_file_name = DEFAULT_PID_FILE_NAME;
140 pool_config->log_statement = 0;
141 pool_config->log_connections = 0;
142 pool_config->log_hostname = 0;
143 pool_config->enable_pool_hba = 0;
145 pool_config->replication_mode = 0;
146 pool_config->load_balance_mode = 0;
147 pool_config->replication_stop_on_mismatch = 0;
148 pool_config->replicate_select = 0;
149 pool_config->reset_query_list = default_reset_query_list;
150 pool_config->num_reset_queries = sizeof(default_reset_query_list)/sizeof(char *);
151 pool_config->reset_query_list = default_reset_query_list;
152 pool_config->print_timestamp = 1;
153 pool_config->master_slave_mode = 0;
154 pool_config->connection_cache = 1;
155 pool_config->health_check_timeout = 20;
156 pool_config->health_check_period = 0;
157 pool_config->health_check_user = "nobody";
158 pool_config->failover_command = "";
159 pool_config->failback_command = "";
160 pool_config->insert_lock = 1;
161 pool_config->ignore_leading_white_space = 1;
162 pool_config->parallel_mode = 0;
163 pool_config->enable_query_cache = 0;
164 pool_config->system_db_hostname = "localhost";
165 pool_config->system_db_port = 5432;
166 pool_config->system_db_dbname = "pgpool";
167 pool_config->system_db_schema = "pgpool_catalog";
168 pool_config->system_db_user = "pgpool";
169 pool_config->system_db_password = "";
170 pool_config->backend_desc->num_backends = 0;
171 pool_config->recovery_user = "";
172 pool_config->recovery_password = "";
173 pool_config->recovery_1st_stage_command = "";
174 pool_config->recovery_2nd_stage_command = "";
175 pool_config->recovery_timeout = 90;
176 pool_config->client_idle_limit_in_recovery = 0;
178 res = gethostname(localhostname,sizeof(localhostname));
181 pool_debug("faild to get this hostname");
183 pool_config->pgpool2_hostname = localhostname;
185 for (i=0;i<MAX_CONNECTION_SLOTS;i++)
192 int pool_get_config(char *confpath, POOL_CONFIG_CONTEXT context)
200 #define PARSE_ERROR() pool_error("pool_config: parse error at line %d '%s'", Lineno, yytext)
202 /* open config file */
203 fd = fopen(confpath, "r");
206 fprintf(stderr, "pool_config: could not open configuration file (%s)\n",
207 POOL_CONF_FILE_NAME);
208 fprintf(stderr, "pool_config: using default values...\n");
215 while ((token = yylex()))
217 if (token == POOL_PARSE_ERROR)
223 if (token == POOL_EOL)
226 if (token != POOL_KEY)
233 strncpy(key, yytext, sizeof(key));
235 pool_debug("key: %s", key);
239 if (token == POOL_EQUALS)
242 pool_debug("value: %s kind: %d", yytext, token);
244 if (!strcmp(key, "allow_inet_domain_socket") && CHECK_CONTEXT(INIT_CONFIG, context))
246 /* for backward compatibility */
247 int v = eval_logical(yytext);
251 pool_error("pool_config: invalid value %s for %s", yytext, key);
256 pool_config->listen_addresses = strdup("*");
258 pool_config->listen_addresses = strdup("");
260 else if (!strcmp(key, "listen_addresses") && CHECK_CONTEXT(INIT_CONFIG, context))
264 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
270 str = extract_string(yytext, token);
276 pool_config->listen_addresses = str;
279 else if (!strcmp(key, "port") && CHECK_CONTEXT(INIT_CONFIG, context))
281 int v = atoi(yytext);
283 if (token != POOL_INTEGER || v < 1024)
285 pool_error("pool_config: %s must be 1024 or higher numeric value", key);
289 pool_config->port = v;
291 else if (!strcmp(key, "pcp_port") && CHECK_CONTEXT(INIT_CONFIG, context))
293 int v = atoi(yytext);
295 if (token != POOL_INTEGER || v < 1024)
297 pool_error("pool_config: %s must be 1024 or higher numeric value", key);
301 pool_config->pcp_port = v;
303 else if (!strcmp(key, "socket_dir") && CHECK_CONTEXT(INIT_CONFIG, context))
307 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
313 str = extract_string(yytext, token);
319 pool_config->socket_dir = str;
321 else if (!strcmp(key, "pcp_socket_dir") && CHECK_CONTEXT(INIT_CONFIG, context))
325 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
331 str = extract_string(yytext, token);
337 pool_config->pcp_socket_dir = str;
339 else if (!strcmp(key, "pcp_timeout") &&
340 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
342 int v = atoi(yytext);
344 if (token != POOL_INTEGER || v < 0)
346 pool_error("pool_config: %s must be equal or greater or equal to 0 numeric value", key);
350 pool_config->pcp_timeout = v;
352 else if (!strcmp(key, "num_init_children") && CHECK_CONTEXT(INIT_CONFIG, context))
354 int v = atoi(yytext);
356 if (token != POOL_INTEGER || v < 1)
358 pool_error("pool_config: %s must be higher than 1 numeric value", key);
362 pool_config->num_init_children = v;
364 else if (!strcmp(key, "child_life_time") &&
365 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
367 int v = atoi(yytext);
369 if (token != POOL_INTEGER || v < 0)
371 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
375 pool_config->child_life_time = v;
377 else if (!strcmp(key, "client_idle_limit") &&
378 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
380 int v = atoi(yytext);
382 if (token != POOL_INTEGER || v < 0)
384 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
388 pool_config->client_idle_limit = v;
390 else if (!strcmp(key, "connection_life_time") &&
391 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
393 int v = atoi(yytext);
395 if (token != POOL_INTEGER || v < 0)
397 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
401 pool_config->connection_life_time = v;
403 else if (!strcmp(key, "child_max_connections") &&
404 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
406 int v = atoi(yytext);
408 if (token != POOL_INTEGER || v < 0)
410 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
414 pool_config->child_max_connections = v;
416 else if (!strcmp(key, "authentication_timeout") &&
417 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
419 int v = atoi(yytext);
421 if (token != POOL_INTEGER || v < 0)
423 pool_error("pool_config: %s must be higher than 0 numeric value", key);
427 pool_config->authentication_timeout = v;
429 else if (!strcmp(key, "max_pool") && CHECK_CONTEXT(INIT_CONFIG, context))
431 int v = atoi(yytext);
433 if (token != POOL_INTEGER || v < 0)
435 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
439 pool_config->max_pool = v;
441 else if (!strcmp(key, "logdir") && CHECK_CONTEXT(INIT_CONFIG, context))
445 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
451 str = extract_string(yytext, token);
457 pool_config->logdir = str;
459 else if (!strcmp(key, "pid_file_name") && CHECK_CONTEXT(INIT_CONFIG, context))
463 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
469 str = extract_string(yytext, token);
475 pool_config->pid_file_name = str;
477 else if (!strcmp(key, "log_connections") &&
478 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
480 int v = eval_logical(yytext);
484 pool_error("pool_config: invalid value %s for %s", yytext, key);
488 pool_config->log_connections = v;
490 else if (!strcmp(key, "log_hostname") &&
491 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
493 int v = eval_logical(yytext);
497 pool_error("pool_config: invalid value %s for %s", yytext, key);
501 pool_config->log_hostname = v;
503 else if (!strcmp(key, "enable_pool_hba") &&
504 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
506 int v = eval_logical(yytext);
510 pool_error("pool_config: invalid value %s for %s", yytext, key);
514 pool_config->enable_pool_hba = v;
516 else if (!strcmp(key, "backend_socket_dir") && CHECK_CONTEXT(INIT_CONFIG, context))
520 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
526 str = extract_string(yytext, token);
532 pool_config->backend_socket_dir = str;
534 else if (!strcmp(key, "replication_mode") && CHECK_CONTEXT(INIT_CONFIG, context))
536 int v = eval_logical(yytext);
540 pool_error("pool_config: invalid value %s for %s", yytext, key);
544 pool_config->replication_mode = pool_config->replication_enabled = v;
546 if (pool_config->master_slave_enabled && pool_config->replication_enabled)
548 pool_error("pool_config: replication_mode and master_slave_mode cannot be enabled at the same time");
554 else if (!strcmp(key, "load_balance_mode") && CHECK_CONTEXT(INIT_CONFIG, context))
556 int v = eval_logical(yytext);
560 pool_error("pool_config: invalid value %s for %s", yytext, key);
564 pool_config->load_balance_mode = v;
566 else if (!strcmp(key, "replication_stop_on_mismatch") &&
567 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
569 int v = eval_logical(yytext);
573 pool_error("pool_config: invalid value %s for %s", yytext, key);
577 pool_debug("replication_stop_on_mismatch: %d", v);
578 pool_config->replication_stop_on_mismatch = v;
580 else if (!strcmp(key, "replicate_select") &&
581 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
583 int v = eval_logical(yytext);
587 pool_error("pool_config: invalid value %s for %s", yytext, key);
591 pool_debug("replicate_select: %d", v);
592 pool_config->replicate_select = v;
594 else if (!strcmp(key, "reset_query_list") &&
595 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
599 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
605 str = extract_string(yytext, token);
611 pool_config->reset_query_list = extract_string_tokens(str, ";", &pool_config->num_reset_queries);
612 if (pool_config->reset_query_list == NULL)
619 else if (!strcmp(key, "print_timestamp") && CHECK_CONTEXT(INIT_CONFIG, context))
621 int v = eval_logical(yytext);
625 pool_error("pool_config: invalid value %s for %s", yytext, key);
629 pool_config->print_timestamp = v;
632 else if (!strcmp(key, "master_slave_mode") && CHECK_CONTEXT(INIT_CONFIG, context))
634 int v = eval_logical(yytext);
638 pool_error("pool_config: invalid value %s for %s", yytext, key);
642 pool_config->master_slave_mode = pool_config->master_slave_enabled = v;
644 if (pool_config->master_slave_enabled && pool_config->replication_enabled)
646 pool_error("pool_config: replication_mode and master_slave_mode cannot be enabled at the same time");
652 else if (!strcmp(key, "connection_cache") && CHECK_CONTEXT(INIT_CONFIG, context))
654 int v = eval_logical(yytext);
658 pool_error("pool_config: invalid value %s for %s", yytext, key);
662 pool_config->connection_cache = v;
665 else if (!strcmp(key, "health_check_timeout") &&
666 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
668 int v = atoi(yytext);
670 if (token != POOL_INTEGER || v < 0)
672 pool_error("pool_config: %s must be equal or higher than 0 numeric value", key);
676 pool_config->health_check_timeout = v;
679 else if (!strcmp(key, "health_check_period") &&
680 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
682 int v = atoi(yytext);
684 if (token != POOL_INTEGER || v < 0)
686 pool_error("pool_config: %s must be equal or higher than 0 numeric value", key);
690 pool_config->health_check_period = v;
693 else if (!strcmp(key, "health_check_user") &&
694 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
698 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
704 str = extract_string(yytext, token);
710 pool_config->health_check_user = str;
713 else if (!strcmp(key, "failover_command") &&
714 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
718 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
724 str = extract_string(yytext, token);
730 pool_config->failover_command = str;
733 else if (!strcmp(key, "failback_command") &&
734 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
738 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
744 str = extract_string(yytext, token);
750 pool_config->failback_command = str;
753 else if (!strcmp(key, "recovery_user") &&
754 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
758 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
764 str = extract_string(yytext, token);
770 pool_config->recovery_user = str;
773 else if (!strcmp(key, "recovery_password") &&
774 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
778 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
784 str = extract_string(yytext, token);
790 pool_config->recovery_password = str;
793 else if (!strcmp(key, "recovery_1st_stage_command") &&
794 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
798 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
804 str = extract_string(yytext, token);
810 pool_config->recovery_1st_stage_command = str;
813 else if (!strcmp(key, "recovery_2nd_stage_command") &&
814 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
818 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
824 str = extract_string(yytext, token);
830 pool_config->recovery_2nd_stage_command = str;
833 else if (!strcmp(key, "recovery_timeout") &&
834 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
836 int v = atoi(yytext);
838 if (token != POOL_INTEGER || v < 0)
840 pool_error("pool_config: %s must be equal or higher than 0 numeric value", key);
844 pool_config->recovery_timeout = v;
847 else if (!strcmp(key, "client_idle_limit_in_recovery") &&
848 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
850 int v = atoi(yytext);
852 if (token != POOL_INTEGER || v < 0)
854 pool_error("pool_config: %s must be greater or equal to 0 numeric value", key);
858 pool_config->client_idle_limit_in_recovery = v;
861 else if (!strcmp(key, "insert_lock") &&
862 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
864 int v = eval_logical(yytext);
868 pool_error("pool_config: invalid value %s for %s", yytext, key);
872 pool_config->insert_lock = v;
875 else if (!strcmp(key, "ignore_leading_white_space") &&
876 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
878 int v = eval_logical(yytext);
882 pool_error("pool_config: invalid value %s for %s", yytext, key);
886 pool_config->ignore_leading_white_space = v;
889 else if (!strcmp(key, "parallel_mode") && CHECK_CONTEXT(INIT_CONFIG, context))
891 int v = eval_logical(yytext);
895 pool_error("pool_config: invalid value %s for %s", yytext, key);
899 pool_config->parallel_mode = v;
902 else if (!strcmp(key, "enable_query_cache") && CHECK_CONTEXT(INIT_CONFIG, context))
904 int v = eval_logical(yytext);
908 pool_error("pool_config: invalid value %s for %s", yytext, key);
912 pool_config->enable_query_cache = v;
915 else if (!strcmp(key, "pgpool2_hostname") && CHECK_CONTEXT(INIT_CONFIG, context))
919 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
925 str = extract_string(yytext, token);
932 pool_config->pgpool2_hostname = str;
935 else if (!strcmp(key, "system_db_hostname") && CHECK_CONTEXT(INIT_CONFIG, context))
939 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
945 str = extract_string(yytext, token);
951 pool_config->system_db_hostname = str;
954 else if (!strcmp(key, "system_db_port") && CHECK_CONTEXT(INIT_CONFIG, context))
956 int v = atoi(yytext);
958 if (token != POOL_INTEGER || v < 0)
960 pool_error("pool_config: %s must be equal or higher than 0 numeric value", key);
964 pool_config->system_db_port = v;
967 else if (!strcmp(key, "system_db_dbname") && CHECK_CONTEXT(INIT_CONFIG, context))
971 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
977 str = extract_string(yytext, token);
983 pool_config->system_db_dbname = str;
986 else if (!strcmp(key, "system_db_schema") && CHECK_CONTEXT(INIT_CONFIG, context))
990 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
996 str = extract_string(yytext, token);
1002 pool_config->system_db_schema = str;
1005 else if (!strcmp(key, "system_db_user") && CHECK_CONTEXT(INIT_CONFIG, context))
1009 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
1015 str = extract_string(yytext, token);
1021 pool_config->system_db_user = str;
1024 else if (!strcmp(key, "system_db_password") && CHECK_CONTEXT(INIT_CONFIG, context))
1028 if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
1034 str = extract_string(yytext, token);
1040 pool_config->system_db_password = str;
1043 else if (!strncmp(key, "backend_hostname", 16) &&
1044 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context) &&
1045 mypid == getpid()) /* this parameter must be modified by parent pid */
1050 slot = atoi(key + 16);
1051 if (slot < 0 || slot >= MAX_CONNECTION_SLOTS)
1053 pool_error("pool_config: backend number %s for backend_hostname out of range", key);
1058 str = extract_string(yytext, token);
1064 if (context == INIT_CONFIG ||
1065 (context == RELOAD_CONFIG && BACKEND_INFO(slot).backend_status == CON_UNUSED))
1066 strncpy(BACKEND_INFO(slot).backend_hostname, str, MAX_DB_HOST_NAMELEN);
1069 else if (!strncmp(key, "backend_port", 12) &&
1070 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context) &&
1071 mypid == getpid()) /* this parameter must be modified by parent pid */
1075 slot = atoi(key + 12);
1076 if (slot < 0 || slot >= MAX_CONNECTION_SLOTS)
1078 pool_error("pool_config: host number %s for port number out of range", key);
1082 pool_debug("pool_config: port slot number %d ", slot);
1083 if (context == INIT_CONFIG)
1085 BACKEND_INFO(slot).backend_port = atoi(yytext);
1086 BACKEND_INFO(slot).backend_status = CON_CONNECT_WAIT;
1088 else if (context == RELOAD_CONFIG && BACKEND_INFO(slot).backend_status == CON_UNUSED)
1090 BACKEND_INFO(slot).backend_port = atoi(yytext);
1091 BACKEND_INFO(slot).backend_status = CON_DOWN;
1095 else if (!strncmp(key, "backend_weight", 14) &&
1096 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context) &&
1097 mypid == getpid()) /* this parameter must be modified by parent pid */
1101 BACKEND_STATUS status;
1103 slot = atoi(key + 14);
1104 if (slot < 0 || slot >= MAX_CONNECTION_SLOTS)
1106 pool_error("pool_config: weight number %s for port number out of range", key);
1115 pool_error("pool_config: invalid value %s for %s", yytext, key);
1120 pool_debug("pool_config: weight slot number %d weight: %f", slot, v);
1121 status = BACKEND_INFO(slot).backend_status;
1122 if (context == INIT_CONFIG ||
1123 (context == RELOAD_CONFIG && (context == RELOAD_CONFIG && (status == CON_UNUSED || status == CON_DOWN))))
1125 pool_config->backend_desc->backend_info[slot].unnormalized_weight = v;
1128 else if (!strncmp(key, "backend_data_directory", 22) &&
1129 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context) &&
1130 mypid == getpid()) /* this parameter must be modified by parent pid */
1134 BACKEND_STATUS status;
1136 slot = atoi(key + 22);
1137 if (slot < 0 || slot >= MAX_CONNECTION_SLOTS)
1139 pool_error("pool_config: backend number %s for backend_data_directory out of range", key);
1144 str = extract_string(yytext, token);
1150 status = BACKEND_INFO(slot).backend_status;
1151 if (context == INIT_CONFIG ||
1152 (context == RELOAD_CONFIG && (status == CON_UNUSED || status == CON_DOWN)))
1153 strncpy(BACKEND_INFO(slot).backend_data_directory, str, MAX_PATH_LENGTH);
1155 else if (!strcmp(key, "log_statement") && CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
1157 int v = eval_logical(yytext);
1161 pool_error("pool_config: invalid value %s for %s", yytext, key);
1164 pool_config->log_statement = v;
1166 else if (!strcmp(key, "log_statement") && CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
1168 int v = eval_logical(yytext);
1172 pool_error("pool_config: invalid value %s for %s", yytext, key);
1175 pool_config->log_statement = v;
1181 pool_config->backend_desc->num_backends = 0;
1184 for (i=0;i<MAX_CONNECTION_SLOTS;i++)
1186 /* port number == 0 indicates that this server is out of use */
1187 if (BACKEND_INFO(i).backend_port == 0)
1189 clear_host_entry(i);
1194 total_weight += BACKEND_INFO(i).unnormalized_weight;
1195 pool_config->backend_desc->num_backends = i+1;
1199 pool_debug("num_backends: %d num_backends: %d total_weight: %f",
1200 pool_config->backend_desc->num_backends, pool_config->backend_desc->num_backends, total_weight);
1202 * Normalize load balacing weights. What we are doing here is,
1203 * assign 0 to RAND_MAX to each backend's weight according to the
1204 * value weightN. For example, if two backends are assigned 1.0,
1205 * then each backend will get RAND_MAX/2 normalized weight.
1207 for (i=0;i<MAX_CONNECTION_SLOTS;i++)
1210 print_host_entry(i);
1213 if (pool_config->backend_desc->backend_info[i].backend_port != 0)
1215 pool_config->backend_desc->backend_info[i].backend_weight =
1216 (RAND_MAX) * pool_config->backend_desc->backend_info[i].unnormalized_weight / total_weight;
1217 pool_debug("backend %d weight: %f", i, pool_config->backend_desc->backend_info[i].backend_weight);
1221 if (pool_config->parallel_mode || pool_config->enable_query_cache)
1226 system_db_info = malloc(sizeof(POOL_SYSTEMDB_CONNECTION_POOL));
1227 if (system_db_info == NULL)
1229 pool_error("failed to allocate sytem_db_info");
1232 memset(system_db_info, 0, sizeof(*system_db_info));
1234 system_db_info->system_db_status = pool_shared_memory_create(sizeof(BACKEND_STATUS));
1235 if (system_db_info->system_db_status == NULL)
1237 pool_error("failed to allocate system_db_info->system_db_status");
1240 *system_db_info->system_db_status = CON_CONNECT_WAIT; /* which is the same as SYSDB_STATUS = CON_CONNECT_WAIT */
1242 info = malloc(sizeof(SystemDBInfo));
1245 pool_error("failed to allocate info");
1249 system_db_info->info = info;
1250 info->hostname = pool_config->system_db_hostname;
1251 info->port = pool_config->system_db_port;
1252 info->user = pool_config->system_db_user;
1253 info->password = pool_config->system_db_password;
1254 info->database_name = pool_config->system_db_dbname;
1255 info->schema_name = pool_config->system_db_schema;
1256 info->dist_def_num = 0;
1257 info->dist_def_slot = NULL;
1259 if (pool_config->parallel_mode)
1261 dist_num = pool_memset_system_db_info(info);
1264 pool_error("failed to get systemdb info");
1268 if (pool_config->enable_query_cache)
1270 info->query_cache_table_info.register_prepared_statement = NULL;
1271 if (! pool_query_cache_table_exists())
1273 pool_error("failed to locate query_cache table. perhaps it's not defined?");
1277 SYSDB_STATUS = CON_UP;
1283 static char *extract_string(char *value, POOL_TOKEN token)
1287 ret = strdup(value);
1290 pool_error("extract_string: out of memory");
1294 if (token == POOL_STRING)
1296 ret[strlen(ret)-1] = '\0';
1302 static int eval_logical(char *str)
1306 if (!strcasecmp(str, "true"))
1308 else if (!strcasecmp(str, "false"))
1310 else if (!strcmp(str, "1"))
1312 else if (!strcmp(str, "0"))
1321 * extract tokens separated by delimi from str. return value is an
1322 * array of pointers to malloced strings. number of tokens is set to
1323 * n; note that str will be destroyed by strtok(). Also return value
1324 * points to static data, that means subsequent call will change the
1327 #define MAXTOKENS 1024
1328 static char **extract_string_tokens(char *str, char *delimi, int *n)
1331 static char *tokens[MAXTOKENS];
1335 for (token = strtok(str, delimi); token != NULL && *n < MAXTOKENS; token = strtok(NULL, delimi))
1337 tokens[*n] = strdup(token);
1338 if (tokens[*n] == NULL)
1340 pool_error("extract_string_tokens: out of memory");
1343 pool_debug("extract_string_tokens: token: %s", tokens[*n]);
1349 static void clear_host_entry(int slot)
1351 *pool_config->backend_desc->backend_info[slot].backend_hostname = '\0';
1352 pool_config->backend_desc->backend_info[slot].backend_port = 0;
1353 pool_config->backend_desc->backend_info[slot].backend_status = CON_UNUSED;
1354 pool_config->backend_desc->backend_info[slot].backend_weight = 0.0;
1358 static void print_host_entry(int slot)
1360 pool_debug("slot: %d host: %s port: %d status: %d weight: %f",
1362 pool_config->server_hostnames[slot],
1363 pool_config->server_ports[slot],
1364 pool_config->server_status[slot],
1365 pool_config->server_weights[slot]);