pgpool2.git / summary / log / commit / refs
commit def541da54ed574a04761bcbb694e8aa4fe41463
Author: Tatsuo Ishii <ishii at sraoss.co.jp>
Date: Tue Sep 13 12:46:33 2011 +0000
Do not start pgpool-II main process if failed to allocate
shared memory query cache.
main.c | 121 ++++++++++++++++---------------------------------------
pool_memqcache.c | 12 ++++--
pool_memqcache.h | 4 +-
3 files changed, 44 insertions(+), 93 deletions(-)
diff --git a/main.c b/main.c
index 98774b315..d65661721 100644
--- a/main.c
+++ b/main.c
@@ -544,15 +544,20 @@ int main(int argc, char **argv)
size_t size;
size = pool_shared_memory_cache_size();
- if (size < 0)
+ if (size == 0)
+ {
+ pool_error("pool_shared_memory_cache_size error");
+ myexit(1);
+ }
+
+ if (pool_init_memory_cache(size) < 0)
{
pool_error("pool_shared_memory_cache_size error");
myexit(1);
}
- pool_init_memory_cache(size);
size = pool_shared_memory_fsmm_size();
- if (size < 0)
+ if (size == 0)
{
pool_error("pool_shared_memory_fsmm_size error");
myexit(1);
@@ -1886,23 +1891,15 @@ static RETSIGTYPE health_check_timer_handler(int sig)
/*
- * check if we can connect to the backend
+ * Check if we can connect to the backend
* returns 0 for ok. otherwise returns backend id + 1
*/
int health_check(void)
{
- int fd;
- int sts;
+ POOL_CONNECTION_POOL_SLOT *slot;
+ BackendInfo *bkinfo;
static bool is_first = true;
static char *dbname;
-
- /* V2 startup packet */
- typedef struct {
- int len; /* startup packet length */
- StartupPacket_v2 sp;
- } MySp;
- MySp mysp;
- char kind;
int i;
/* Do not execute health check during recovery */
@@ -1916,79 +1913,26 @@ int health_check(void)
if (is_first)
dbname = "postgres";
- memset(&mysp, 0, sizeof(mysp));
- mysp.len = htonl(296);
- mysp.sp.protoVersion = htonl(PROTO_MAJOR_V2 << 16);
- strcpy(mysp.sp.database, dbname);
- strncpy(mysp.sp.user, pool_config->health_check_user, sizeof(mysp.sp.user) - 1);
- *mysp.sp.options = '\0';
- *mysp.sp.unused = '\0';
- *mysp.sp.tty = '\0';
-
for (i=0;i<pool_config->backend_desc->num_backends;i++)
{
- pool_debug("health_check: %d th DB node status: %d", i, BACKEND_INFO(i).backend_status);
-
- if (BACKEND_INFO(i).backend_status == CON_UNUSED ||
- BACKEND_INFO(i).backend_status == CON_DOWN)
- continue;
-
- if (*(BACKEND_INFO(i).backend_hostname) == '/')
- fd = connect_unix_domain_socket(i, FALSE);
- else
- fd = connect_inet_domain_socket(i, FALSE);
+ bkinfo = pool_get_node_info(i);
- if (fd < 0)
- {
- pool_error("health check failed. %d th host %s at port %d is down",
- i,
- BACKEND_INFO(i).backend_hostname,
- BACKEND_INFO(i).backend_port);
+ pool_debug("health_check: %d th DB node status: %d", i, bkinfo->backend_status);
- return i+1;
- }
+ if (bkinfo->backend_status == CON_UNUSED ||
+ bkinfo->backend_status == CON_DOWN)
+ continue;
- if (write(fd, &mysp, sizeof(mysp)) < 0)
- {
- pool_error("health check failed during write. host %s at port %d is down. reason: %s",
- BACKEND_INFO(i).backend_hostname,
- BACKEND_INFO(i).backend_port,
- strerror(errno));
- close(fd);
- return i+1;
- }
-
- /*
- * Don't bother to be blocked by read(2). It will be
- * interrupted by ALRAM anyway.
- */
- sts = read(fd, &kind, 1);
- if (sts == -1)
- {
- pool_error("health check failed during read. host %s at port %d is down. reason: %s",
- BACKEND_INFO(i).backend_hostname,
- BACKEND_INFO(i).backend_port,
- strerror(errno));
- close(fd);
- return i+1;
- }
- else if (sts == 0)
- {
- pool_error("health check failed. EOF encountered. host %s at port %d is down",
- BACKEND_INFO(i).backend_hostname,
- BACKEND_INFO(i).backend_port);
- close(fd);
- return i+1;
- }
+ slot = make_persistent_db_connection(bkinfo->backend_hostname,
+ bkinfo->backend_port,
+ dbname,
+ pool_config->health_check_user,
+ pool_config->health_check_password);
if (is_first)
is_first = false;
- /*
- * If a backend raised a FATAL error(max connections error or
- * starting up error?), do not send a Terminate message.
- */
- if ((kind != 'E') && (write(fd, "X", 1) < 0))
+ if (!slot)
{
if (!strcmp(dbname, "postgres"))
{
@@ -1998,16 +1942,19 @@ int health_check(void)
dbname = "template1";
goto Retry;
}
-
- pool_error("health check failed during write. host %s at port %d is down. reason: %s. Perhaps wrong health check user?",
- BACKEND_INFO(i).backend_hostname,
- BACKEND_INFO(i).backend_port,
- strerror(errno));
- close(fd);
- return i+1;
+ else
+ {
+ pool_error("health check failed. %d th host %s at port %d is down",
+ i,
+ bkinfo->backend_hostname,
+ bkinfo->backend_port);
+ return i+1;
+ }
+ }
+ else
+ {
+ discard_persistent_db_connection(slot);
}
-
- close(fd);
}
return 0;
diff --git a/pool_memqcache.c b/pool_memqcache.c
index f9778487a..8cbe2e6b9 100644
--- a/pool_memqcache.c
+++ b/pool_memqcache.c
@@ -1234,7 +1234,7 @@ size_t pool_shared_memory_cache_size(void)
{
pool_error("pool_shared_memory_cache_size: wrong memqcache_total_size %d or memqcache_cache_block_size %d",
pool_config->memqcache_total_size, pool_config->memqcache_cache_block_size);
- return -1;
+ return 0;
}
/* Remenber # of blocks */
@@ -1245,17 +1245,19 @@ size_t pool_shared_memory_cache_size(void)
/*
* Aquire and initialize shared memory cache. This should be called
- * only once from pgpool main process at the proces staring up time.
+ * only once from pgpool main process at the process staring up time.
*/
static void *shmem;
-void pool_init_memory_cache(size_t size)
+int pool_init_memory_cache(size_t size)
{
pool_debug("pool_init_memory_cache: request size:%zd", size);
shmem = pool_shared_memory_create(size);
if (shmem == NULL)
{
pool_error("pool_init_memory_cache: failed to allocate shared memory cache. request size: %zd", size);
+ return -1;
}
+ return 0;
}
/*
@@ -1307,7 +1309,7 @@ size_t pool_shared_memory_fsmm_size(void)
* main process at the proces staring up time.
*/
static void *fsmm;
-void pool_init_fsmm(size_t size)
+int pool_init_fsmm(size_t size)
{
int maxblock = pool_get_memqcache_blocks();
int encode_value;
@@ -1316,10 +1318,12 @@ void pool_init_fsmm(size_t size)
if (fsmm == NULL)
{
pool_error("pool_init_fsmm: failed to allocate shared memory cache. request size: %zd", size);
+ return -1;
}
encode_value = POOL_MAX_FREE_SPACE/POOL_FSMM_RATIO;
memset(fsmm, encode_value, maxblock);
+ return 0;
}
/*
diff --git a/pool_memqcache.h b/pool_memqcache.h
index df59b21f2..e4269bb50 100644
--- a/pool_memqcache.h
+++ b/pool_memqcache.h
@@ -154,9 +154,9 @@ extern void pool_add_dml_table_oid(int oid);
extern void pool_discard_oid_maps(void);
extern bool pool_is_shmem_cache(void);
extern size_t pool_shared_memory_cache_size(void);
-extern void pool_init_memory_cache(size_t size);
+extern int pool_init_memory_cache(size_t size);
extern size_t pool_shared_memory_fsmm_size(void);
-extern void pool_init_fsmm(size_t size);
+extern int pool_init_fsmm(size_t size);
extern void pool_allocate_fsmm_clock_hand(void);
extern POOL_QUERY_CACHE_ARRAY *pool_create_query_cache_array(void);
[parent: 8163c54bf61e]