pgpool2.git / summary / log / commit / refs

commit    c2b32069c31a04b5eb3fb95a8b0c9e3ddeb3a70d
Author:   Muhammad Usama <m.usama@gmail.com>
Date:     Mon Nov 02 20:40:07 2015 +0000

    Fixing coverity issues.
    
    The commit fixes the following coverity scan issues
    1332232, 1332233, 1332234, 1332235, 1332213,
    1332222, 1332214, 1332220, 1332221, 1332223,
    1332224, 1332225, 1332226, 1332227, 1332228,
    1332217, 1332218, 1332215, 1332229, 1332231,
    1332230, 1332212


src/include/watchdog/wd_json_data.h | 4 +- src/libs/pcp/pcp.c | 6 +- src/main/pgpool_main.c | 8 +- src/watchdog/watchdog.c | 218 ++++++++++++++---------------------- src/watchdog/wd_commands.c | 54 +++------ src/watchdog/wd_if.c | 1 - src/watchdog/wd_json_data.c | 133 +++++++++++++++++++--- src/watchdog/wd_lifecheck.c | 20 +++- 8 files changed, 246 insertions(+), 198 deletions(-) diff --git a/src/include/watchdog/wd_json_data.h b/src/include/watchdog/wd_json_data.h index e763dbeff..53ff9d67d 100644 --- a/src/include/watchdog/wd_json_data.h +++ b/src/include/watchdog/wd_json_data.h @@ -6,7 +6,7 @@ * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * - * Copyright (c) 2003-2012 PgPool Global Development Group + * Copyright (c) 2003-2015 PgPool Global Development Group * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby @@ -48,4 +48,6 @@ extern bool parse_node_status_json(char* json_data, int data_len, int* nodeID, i extern WDNodeInfo* get_WDNodeInfo_from_wd_node_json(json_value* source); +extern char* get_wd_node_function_json(char* func_name, int *node_id_set, int count); +extern bool parse_wd_node_function_json(char* json_data, int data_len, char** func_name, int **node_id_set, int *count); #endif diff --git a/src/libs/pcp/pcp.c b/src/libs/pcp/pcp.c index b3bc31def..7834c55cc 100644 --- a/src/libs/pcp/pcp.c +++ b/src/libs/pcp/pcp.c @@ -1284,17 +1284,19 @@ process_watchdog_info_response(PCPConnInfo* pcpConn, char* buf, int len) { char *index = NULL; + watchdog_info = palloc(sizeof(PCPWDNodeInfo)); + index = (char *) memchr(buf, '\0', len); if(index == NULL) goto INVALID_RESPONSE; index +=1; - strlcpy(watchdog_info->hostName, index, sizeof(watchdog_info->hostName)); + strlcpy(watchdog_info->hostName, index, sizeof(watchdog_info->hostName)-1); index = (char *) memchr(buf, '\0', len); if(index == NULL) goto INVALID_RESPONSE; index +=1; - strlcpy(watchdog_info->nodeName, index, sizeof(watchdog_info->nodeName)); + strlcpy(watchdog_info->nodeName, index, sizeof(watchdog_info->nodeName) -1 ); index = (char *) memchr(index, '\0', len); if(index == NULL) diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c index 977dc3a96..b30af0064 100644 --- a/src/main/pgpool_main.c +++ b/src/main/pgpool_main.c @@ -1098,7 +1098,7 @@ bool degenerate_backend_set_ex(int *node_id_set, int count, bool error, bool tes int x; for (x=0; x < MAX_SEC_WAIT_FOR_CLUSTER_TRANSATION; x++) { - WdCommandResult res = wd_degenerate_backend_set(node_id_set, count); + res = wd_degenerate_backend_set(node_id_set, count); if (res != CLUSTER_IN_TRANSATIONING) break; sleep(1); @@ -1171,7 +1171,7 @@ void promote_backend(int node_id) int x; for (x=0; x < MAX_SEC_WAIT_FOR_CLUSTER_TRANSATION; x++) { - WdCommandResult res = wd_promote_backend(node_id); + res = wd_promote_backend(node_id); if (res != CLUSTER_IN_TRANSATIONING) break; sleep(1); @@ -1229,7 +1229,7 @@ void send_failback_request(int node_id,bool throw_error) int x; for (x=0; x < MAX_SEC_WAIT_FOR_CLUSTER_TRANSATION; x++) { - WdCommandResult res = wd_send_failback_request(node_id); + res = wd_send_failback_request(node_id); if (res != CLUSTER_IN_TRANSATIONING) break; sleep(1); @@ -1245,7 +1245,7 @@ void send_failback_request(int node_id,bool throw_error) (errmsg("failback request for node_id: %d from pid [%d], But cluster is not in stable state" , node_id, getpid()))); } - + if (res != COMMAND_FAILED) { register_node_operation_request(NODE_UP_REQUEST, &node_id, 1); diff --git a/src/watchdog/watchdog.c b/src/watchdog/watchdog.c index eb4d593e7..04f192dfb 100644 --- a/src/watchdog/watchdog.c +++ b/src/watchdog/watchdog.c @@ -116,34 +116,36 @@ packet_types all_packet_types[] = { }; char *wd_event_name[] = -{ "WD_EVENT_WD_STATE_CHANGED", - "WD_EVENT_TIMEOUT", - "WD_EVENT_PACKET_RCV", - "WD_EVENT_COMMAND_FINISHED", - "WD_EVENT_NEW_OUTBOUND_CONNECTION", - "WD_EVENT_NW_IP_IS_REMOVED", - "WD_EVENT_NW_IP_IS_ASSIGNED", - "WD_EVENT_LOCAL_NODE_LOST", - "WD_EVENT_REMOTE_NODE_LOST", - "WD_EVENT_REMOTE_NODE_FOUND", - "WD_EVENT_LOCAL_NODE_FOUND" +{ "STATE CHANGED", + "TIMEOUT", + "PACKET RECEIVED", + "COMMAND FINISHED", + "NEW OUTBOUND_CONNECTION", + "NETWORK IP IS REMOVED", + "NETWORK IP IS ASSIGNED", + "THIS NODE LOST", + "REMOTE NODE LOST", + "REMOTE NODE FOUND", + "THIS NODE FOUND", + "NODE CONNECTION LOST", + "NODE CONNECTION FOUND" }; char *debug_states[] = { - "WD_DEAD", - "WD_LOADING", - "WD_JOINING", - "WD_INITIALIZING", - "WD_WAITING_CONNECT", - "WD_COORDINATOR", - "WD_PARTICIPATE_IN_ELECTION", - "WD_STAND_FOR_COORDINATOR", - "WD_STANDBY", - "WD_WAITING_FOR_QUORUM", - "WD_LOST", - "WD_IN_NW_TROUBLE", - "WD_SHUTDOWN", - "WD_ADD_MESSAGE_SENT"}; + "DEAD", + "LOADING", + "JOINING", + "INITIALIZING", + "WAITING_CONNECT", + "COORDINATOR", + "PARTICIPATE_IN_ELECTION", + "STAND_FOR_COORDINATOR", + "STANDBY", + "WAITING_FOR_QUORUM", + "LOST", + "IN_NETWORK_TROUBLE", + "SHUTDOWN", + "ADD_MESSAGE_SENT"}; typedef struct WDPacketData { @@ -454,8 +456,8 @@ static void wd_cluster_initialize(void) g_cluster.localNode->wd_priority = pool_config->wd_priority; g_cluster.localNode->pgpool_port = pool_config->port; g_cluster.localNode->private_id = 0; - strcpy(g_cluster.localNode->hostname, pool_config->wd_hostname); - strcpy(g_cluster.localNode->delegate_ip, pool_config->delegate_IP); + strncpy(g_cluster.localNode->hostname, pool_config->wd_hostname, sizeof(g_cluster.localNode->hostname) -1); + strncpy(g_cluster.localNode->delegate_ip, pool_config->delegate_IP, sizeof(g_cluster.localNode->delegate_ip) -1); /* Assign the node name */ { struct utsname unameData; @@ -909,6 +911,8 @@ watchdog_main(void) if (service_lost_connections() == true) service_internal_command(); + + update_connected_node_count(); } return 0; } @@ -1389,25 +1393,25 @@ static bool read_ipc_command_and_process(int sock, bool *remove_socket) gettimeofday(&IPCCommand->issue_time, NULL); if (data_len > 0) + { IPCCommand->data_buf = palloc(data_len); + if (socket_read(sock, IPCCommand->data_buf , data_len, 0) <= 0) + { + ereport(LOG, + (errmsg("error reading IPC from socket"), + errdetail("read from socket failed with error \"%s\"",strerror(errno)))); + MemoryContextDelete(mCxt); + return false; + } + } else IPCCommand->data_buf = NULL; - + IPCCommand->nodeResults = NULL; - IPCCommand->memoryContext = mCxt; - + IPCCommand->data_len = data_len; MemoryContextSwitchTo(oldCxt); - if (socket_read(sock, IPCCommand->data_buf , data_len,0) <= 0) - { - ereport(NOTICE, - (errmsg("error reading IPC from socket"), - errdetail("read from socket failed with error \"%s\"",strerror(errno)))); - MemoryContextDelete(mCxt); - return false; - } - IPCCommand->data_len = data_len; res = process_IPC_command(IPCCommand); if (res == IPC_CMD_PROCESSING) { @@ -2339,7 +2343,7 @@ static int accept_incomming_connections(fd_set* rmask, int pending_fds_count) conn->sock = fd; conn->sock_state = WD_SOCK_CONNECTED; gettimeofday(&conn->tv, NULL); - strncpy(conn->addr,inet_ntoa(addr.sin_addr),sizeof(conn->addr)); + strncpy(conn->addr,inet_ntoa(addr.sin_addr),sizeof(conn->addr)-1); ereport(LOG, (errmsg("new watchdog node connection is received from \"%s:%d\"",inet_ntoa(addr.sin_addr),addr.sin_port))); g_cluster.unidentified_socks = lappend(g_cluster.unidentified_socks, conn); @@ -2400,24 +2404,34 @@ static int update_successful_outgoing_cons(fd_set* wmask, int pending_fds_count) gettimeofday(&wdNode->client_socket.tv, NULL); - getsockopt(wdNode->client_socket.sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon); - if (valopt) + if (getsockopt(wdNode->client_socket.sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) == 0) { - ereport(LOG, - (errmsg("error in outbond connection to %s:%d",wdNode->hostname,wdNode->wd_port), - errdetail("%s",strerror(valopt)))); - close_socket_connection(&wdNode->client_socket); - wdNode->client_socket.sock_state = WD_SOCK_ERROR; + if (valopt) + { + ereport(LOG, + (errmsg("error in outbond connection to %s:%d",wdNode->hostname,wdNode->wd_port), + errdetail("%s",strerror(valopt)))); + close_socket_connection(&wdNode->client_socket); + wdNode->client_socket.sock_state = WD_SOCK_ERROR; + } + else + { + wdNode->client_socket.sock_state = WD_SOCK_CONNECTED; + ereport(LOG, + (errmsg("new outbond connection to %s:%d ",wdNode->hostname,wdNode->wd_port))); + /* set socket to blocking again */ + pool_unset_nonblock(wdNode->client_socket.sock); + watchdog_state_machine(WD_EVENT_NEW_OUTBOUND_CONNECTION, wdNode, NULL); + } } else { - wdNode->client_socket.sock_state = WD_SOCK_CONNECTED; ereport(LOG, - (errmsg("new outbond connection to %s:%d ",wdNode->hostname,wdNode->wd_port))); - - /* set socket to blocking again */ - pool_unset_nonblock(wdNode->client_socket.sock); - watchdog_state_machine(WD_EVENT_NEW_OUTBOUND_CONNECTION, wdNode, NULL); + (errmsg("error in outbond connection to %s:%d ",wdNode->hostname,wdNode->wd_port), + errdetail("getsockopt faile with error \"%s\"",strerror(errno)))); + close_socket_connection(&wdNode->client_socket); + wdNode->client_socket.sock_state = WD_SOCK_ERROR; + } count++; if (count >= pending_fds_count) @@ -2619,7 +2633,7 @@ static WDPacketData* get_addnode_message(void) static WDPacketData* get_mynode_info_message(WDPacketData* replyFor) { - char authhash[MD5_PASSWD_LEN +1]; + char authhash[MD5_PASSWD_LEN + MD5_PASSWD_LEN + 10]; //TODO WDPacketData *message = get_empty_packet(); bool include_hash = get_authhash_for_node(g_cluster.localNode, authhash); char *json_data = get_watchdog_node_info_json(g_cluster.localNode, include_hash?authhash:NULL); @@ -2815,7 +2829,10 @@ static int standard_packet_processor(WatchdogNode* wdNode, WDPacketData* pkt) } if (replyPkt) { - send_message_to_node(wdNode,replyPkt); + if (send_message_to_node(wdNode,replyPkt) == false) + ereport(LOG, + (errmsg("sending packet to node \"%s\" failed", wdNode->nodeName))); + pfree(replyPkt); } return 1; @@ -2875,17 +2892,15 @@ static int send_message(WatchdogNode* wdNode, WDPacketData *pkt) static void service_internal_command(void) { int i; - WDCommandNodeResult* nodeResult = NULL; - if (g_cluster.currentCommand.commandStatus != COMMAND_IN_PROGRESS) return; /* get the result node for */ for (i = 0; i< g_cluster.remoteNodeCount; i++) { - WDCommandNodeResult* nodeRes = &g_cluster.currentCommand.nodeResults[i]; - if (nodeRes->cmdState == COMMAND_STATE_SEND_ERROR) + WDCommandNodeResult* nodeResult = &g_cluster.currentCommand.nodeResults[i]; + if (nodeResult->cmdState == COMMAND_STATE_SEND_ERROR) { - if (is_node_reachable(nodeRes->wdNode)) + if (is_node_reachable(nodeResult->wdNode)) { if (send_message_to_node(nodeResult->wdNode, &g_cluster.currentCommand.packet) == true) { @@ -4451,13 +4466,11 @@ static IPC_CMD_PREOCESS_RES execute_replicate_command(WDIPCCommandData* ipcComma static bool process_pgpool_replicate_command(WatchdogNode* wdNode, WDPacketData* pkt) { - /* we need to get the function name */ - json_value *root, *value; char* func_name; - bool is_error = false; int node_count = 0; int *node_id_list = NULL; - + bool ret = false; + if (pkt->data == NULL || pkt->len == 0) { ereport(LOG, @@ -4469,73 +4482,14 @@ static bool process_pgpool_replicate_command(WatchdogNode* wdNode, WDPacketData* (errmsg("processing pgpool replicate variable command"), errdetail("%s",pkt->data))); - root = json_parse(pkt->data,pkt->len); - - /* The root node must be object */ - if (root == NULL || root->type != json_object) - { - json_value_free(root); - ereport(LOG, - (errmsg("watchdog is unable to process pgpool replicate command"), - errdetail("invalid json data \"%s\"",pkt->data))); - return false; - } - func_name = json_get_string_value_for_key(root, "Function"); - if (func_name == NULL) - { - json_value_free(root); - ereport(LOG, - (errmsg("watchdog is unable to process pgpool replicate command"), - errdetail("function node not found in json data \"%s\"",pkt->data))); - return false; - } - func_name = pstrdup(func_name); - /* If it is a node function ?*/ - if (json_get_int_value_for_key(root, "NodeCount", &node_count)) - { - json_value_free(root); - } - - /* find the WatchdogNodes array */ - value = json_get_value_for_key(root,"NodeIdList"); - if (value == NULL) - { - ereport(WARNING, - (errmsg("invalid json data"), - errdetail("unable to find NodeIdList node from data"))); - } - if (value->type != json_array) - { - is_error = true; - ereport(WARNING, - (errmsg("invalid json data"), - errdetail("NodeIdList node does not contains Array"))); - } - if (node_count != value->u.array.length) - { - is_error = true; - ereport(WARNING, - (errmsg("invalid json data"), - errdetail("NodeIdList array contains %d nodes while expecting %d",value->u.array.length, node_count))); - } - - if (is_error == false) - { - int i; - node_id_list = palloc(sizeof(int) * node_count); - for (i = 0; i < node_count; i++) - { - node_id_list[i] = value->u.array.values[i]->u.integer; - } - if (is_error) - { - pfree(node_id_list); - node_id_list = NULL; - node_count = 0; - } - } - json_value_free(root); - return process_wd_command_function(wdNode, pkt, func_name, node_count, node_id_list); + if (parse_wd_node_function_json(pkt->data, pkt->len, &func_name, &node_id_list, &node_count)) + ret = process_wd_command_function(wdNode, pkt, func_name, node_count, node_id_list); + if (func_name) + pfree(func_name); + if (node_id_list) + pfree(node_id_list); + + return ret; } static bool process_wd_command_function(WatchdogNode* wdNode, WDPacketData* pkt, char* func_name, int node_count, int* node_id_list) @@ -4872,7 +4826,7 @@ static bool verify_pool_configurations(POOL_CONFIG* config) pool_config->backend_desc->backend_info[i].backend_hostname))); return false; } - if (pool_config->backend_desc->backend_info[i].backend_port != pool_config->backend_desc->backend_info[i].backend_port) + if (config->backend_desc->backend_info[i].backend_port != pool_config->backend_desc->backend_info[i].backend_port) { ereport(FATAL, (return_code(POOL_EXIT_FATAL), diff --git a/src/watchdog/wd_commands.c b/src/watchdog/wd_commands.c index cbe70141d..0097afa01 100644 --- a/src/watchdog/wd_commands.c +++ b/src/watchdog/wd_commands.c @@ -46,6 +46,7 @@ #include "utils/json.h" #include "utils/pool_stream.h" #include "pool_config.h" +#include "watchdog/wd_json_data.h" #include "watchdog/wd_ipc_commands.h" #include "watchdog/wd_ipc_defines.h" @@ -61,8 +62,6 @@ static void sleep_in_waiting(void); static WDFailoverCMDResults wd_issue_failover_lock_command(WDFailoverCMDTypes cmdType, char* syncReqType); -static char* get_wd_node_function_json(char* func_name, int *node_id_set, int count); -static char* get_wd_simple_function_json(char* func); static char* get_wd_failover_cmd_type_json(WDFailoverCMDTypes cmdType, char* reqType); WDFailoverCMDResults wd_send_failover_sync_command(WDFailoverCMDTypes cmdType, char* syncReqType); @@ -209,46 +208,24 @@ issue_command_to_watchdog(char type, WD_COMMAND_ACTIONS command_action,int timeo } } } + else + { + /* For non blocking mode if we are sucessful in sending the command + * that means the command is success + */ + result = palloc0(sizeof(WDIPCCmdResult)); + result->type = WD_IPC_CMD_RESULT_OK; + } close(sock); return result; } -static char* get_wd_simple_function_json(char* func) -{ - char* json_str; - JsonNode* jNode = jw_create_with_object(true); - jw_put_string(jNode, "Function", func); - jw_finish_document(jNode); - json_str = pstrdup(jw_get_json_string(jNode)); - jw_destroy(jNode); - return json_str; -} - -static char* get_wd_node_function_json(char* func_name, int *node_id_set, int count) -{ - char* json_str; - int i; - JsonNode* jNode = jw_create_with_object(true); - - jw_put_string(jNode, "Function", func_name); - jw_put_int(jNode, "NodeCount", count); - jw_start_array(jNode, "NodeIdList"); - for (i=0; i < count; i++) { - jw_put_int_value(jNode, node_id_set[i]); - } - jw_end_element(jNode); - jw_finish_document(jNode); - json_str = pstrdup(jw_get_json_string(jNode)); - jw_destroy(jNode); - return json_str; -} - WdCommandResult wd_start_recovery(void) { char type; - char* func = get_wd_simple_function_json(WD_FUNCTION_START_RECOVERY); + char* func = get_wd_node_function_json(WD_FUNCTION_START_RECOVERY, NULL,0); WDIPCCmdResult *result = issue_command_to_watchdog(WD_FUNCTION_COMMAND, WD_COMMAND_ACTION_DEFAULT,pool_config->recovery_timeout, func, strlen(func), true); pfree(func); @@ -281,7 +258,7 @@ WdCommandResult wd_end_recovery(void) { char type; - char* func = get_wd_simple_function_json(WD_FUNCTION_END_RECOVERY); + char* func = get_wd_node_function_json(WD_FUNCTION_END_RECOVERY, NULL, 0); WDIPCCmdResult *result = issue_command_to_watchdog(WD_FUNCTION_COMMAND, WD_COMMAND_ACTION_DEFAULT,2, func, strlen(func), true); pfree(func); @@ -375,7 +352,9 @@ wd_send_failover_sync_command(WDFailoverCMDTypes cmdType, char* syncReqType) char* json_data = get_wd_failover_cmd_type_json(cmdType, syncReqType); WDIPCCmdResult *result = issue_command_to_watchdog(WD_FAILOVER_CMD_SYNC_REQUEST, WD_COMMAND_ACTION_DEFAULT,pool_config->recovery_timeout, json_data, strlen(json_data), true); - + + pfree(json_data); + if (result == NULL || result->length <= 0) { ereport(LOG, @@ -497,15 +476,16 @@ wd_promote_backend(int node_id) int n = node_id; char type; char* func; + WDIPCCmdResult *result; /* if promote packet is received already, do nothing */ if (wd_chk_node_mask_for_promote_req(&n,1)) return COMMAND_OK; func = get_wd_node_function_json(WD_FUNCTION_PROMOTE_REQUEST,&n, 1); - WDIPCCmdResult *result = issue_command_to_watchdog(WD_FUNCTION_COMMAND, WD_COMMAND_ACTION_DEFAULT,2, func, strlen(func), true); + result = issue_command_to_watchdog(WD_FUNCTION_COMMAND, WD_COMMAND_ACTION_DEFAULT,2, func, strlen(func), true); pfree(func); - + if (result == NULL) { ereport(LOG, diff --git a/src/watchdog/wd_if.c b/src/watchdog/wd_if.c index 2aa5a18c8..4fa0a349d 100644 --- a/src/watchdog/wd_if.c +++ b/src/watchdog/wd_if.c @@ -365,7 +365,6 @@ bool read_interface_change_event(int sock, char** ip_address, char** interface, for (; NLMSG_OK(nlhdr, len) ;nlhdr = NLMSG_NEXT(nlhdr, len)) { - bool deladdr = false; char addr[48]; char* label = NULL; addr[0] = '\0'; diff --git a/src/watchdog/wd_json_data.c b/src/watchdog/wd_json_data.c index 5401b91cc..d9c0c6318 100644 --- a/src/watchdog/wd_json_data.c +++ b/src/watchdog/wd_json_data.c @@ -123,7 +123,7 @@ POOL_CONFIG* get_pool_config_from_json(char* json_data, int data_len) ptr = json_get_string_value_for_key(arr_value, "backend_hostname"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(config->backend_desc->backend_info[i].backend_hostname, ptr,sizeof(config->backend_desc->backend_info[i].backend_hostname)); + strncpy(config->backend_desc->backend_info[i].backend_hostname, ptr,sizeof(config->backend_desc->backend_info[i].backend_hostname) -1); } /* wd_remote_nodes array */ @@ -143,7 +143,7 @@ POOL_CONFIG* get_pool_config_from_json(char* json_data, int data_len) ptr = json_get_string_value_for_key(arr_value, "hostname"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(config->wd_remote_nodes.wd_remote_node_info[i].hostname, ptr,sizeof(config->wd_remote_nodes.wd_remote_node_info[i].hostname)); + strncpy(config->wd_remote_nodes.wd_remote_node_info[i].hostname, ptr,sizeof(config->wd_remote_nodes.wd_remote_node_info[i].hostname) -1); } json_value_free(root); @@ -152,14 +152,11 @@ POOL_CONFIG* get_pool_config_from_json(char* json_data, int data_len) ERROR_EXIT: if (root) json_value_free(root); - if (config) - { - if (config->backend_desc) - pfree(config->backend_desc); - if (config->master_slave_sub_mode) - pfree(config->master_slave_sub_mode); - pfree(config); - } + if (config->backend_desc) + pfree(config->backend_desc); + if (config->master_slave_sub_mode) + pfree(config->master_slave_sub_mode); + pfree(config); return NULL; } @@ -283,17 +280,17 @@ WatchdogNode* get_watchdog_node_from_json(char* json_data, int data_len, char** ptr = json_get_string_value_for_key(root, "NodeName"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(wdNode->nodeName, ptr, sizeof(wdNode->nodeName)); + strncpy(wdNode->nodeName, ptr, sizeof(wdNode->nodeName) -1); ptr = json_get_string_value_for_key(root, "HostName"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(wdNode->hostname, ptr, sizeof(wdNode->hostname)); + strncpy(wdNode->hostname, ptr, sizeof(wdNode->hostname) -1); ptr = json_get_string_value_for_key(root, "VIP"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(wdNode->delegate_ip, ptr, sizeof(wdNode->delegate_ip)); + strncpy(wdNode->delegate_ip, ptr, sizeof(wdNode->delegate_ip) -1); if (authkey) { @@ -383,7 +380,7 @@ WDNodeInfo* get_WDNodeInfo_from_wd_node_json(json_value* source) (errmsg("invalid json data"), errdetail("unable to find Watchdog Node Name"))); } - strncpy(wdNodeInfo->nodeName, ptr, sizeof(wdNodeInfo->nodeName)); + strncpy(wdNodeInfo->nodeName, ptr, sizeof(wdNodeInfo->nodeName) -1); ptr = json_get_string_value_for_key(source, "HostName"); if (ptr == NULL) @@ -392,7 +389,7 @@ WDNodeInfo* get_WDNodeInfo_from_wd_node_json(json_value* source) (errmsg("invalid json data"), errdetail("unable to find Watchdog Host Name"))); } - strncpy(wdNodeInfo->hostName, ptr, sizeof(wdNodeInfo->hostName)); + strncpy(wdNodeInfo->hostName, ptr, sizeof(wdNodeInfo->hostName) -1); ptr = json_get_string_value_for_key(source, "DelegateIP"); if (ptr == NULL) @@ -401,7 +398,7 @@ WDNodeInfo* get_WDNodeInfo_from_wd_node_json(json_value* source) (errmsg("invalid json data"), errdetail("unable to find Watchdog delegate IP"))); } - strncpy(wdNodeInfo->delegate_ip, ptr, sizeof(wdNodeInfo->delegate_ip)); + strncpy(wdNodeInfo->delegate_ip, ptr, sizeof(wdNodeInfo->delegate_ip) -1); if (json_get_int_value_for_key(source, "PgpoolPort", &wdNodeInfo->wd_port)) { @@ -428,3 +425,107 @@ WDNodeInfo* get_WDNodeInfo_from_wd_node_json(json_value* source) } +char* get_wd_node_function_json(char* func_name, int *node_id_set, int count) +{ + char* json_str; + int i; + JsonNode* jNode = jw_create_with_object(true); + + jw_put_string(jNode, "Function", func_name); + jw_put_int(jNode, "NodeCount", count); + if (count > 0) + { + jw_start_array(jNode, "NodeIdList"); + for (i=0; i < count; i++) { + jw_put_int_value(jNode, node_id_set[i]); + } + jw_end_element(jNode); + } + jw_finish_document(jNode); + json_str = pstrdup(jw_get_json_string(jNode)); + jw_destroy(jNode); + return json_str; +} + +bool parse_wd_node_function_json(char* json_data, int data_len, char** func_name, int **node_id_set, int *count) +{ + json_value *root, *value; + char* ptr; + int node_count = 0; + int i; + + *node_id_set = NULL; + *func_name = NULL; + *count = 0; + + root = json_parse(json_data,data_len); + + /* The root node must be object */ + if (root == NULL || root->type != json_object) + { + json_value_free(root); + ereport(LOG, + (errmsg("watchdog is unable to parse node function json"), + errdetail("invalid json data \"%s\"",json_data))); + return false; + } + ptr = json_get_string_value_for_key(root, "Function"); + if (ptr == NULL) + { + json_value_free(root); + ereport(LOG, + (errmsg("watchdog is unable to parse node function json"), + errdetail("function node not found in json data \"%s\"",json_data))); + return false; + } + *func_name = pstrdup(ptr); + /* If it is a node function ?*/ + if (json_get_int_value_for_key(root, "NodeCount", &node_count)) + { + /*node count not found, But we don't care much about this*/ + json_value_free(root); + return true; + } + if (node_count <= 0) + { + json_value_free(root); + return true; + } + *count = node_count; + + value = json_get_value_for_key(root,"NodeIdList"); + if (value == NULL) + { + json_value_free(root); + ereport(LOG, + (errmsg("invalid json data"), + errdetail("unable to find NodeIdList node from data"))); + return false; + } + if (value->type != json_array) + { + json_value_free(root); + ereport(WARNING, + (errmsg("invalid json data"), + errdetail("NodeIdList node does not contains Array"))); + return false; + } + if (node_count != value->u.array.length) + { + json_value_free(root); + ereport(WARNING, + (errmsg("invalid json data"), + errdetail("NodeIdList array contains %d nodes while expecting %d",value->u.array.length, node_count))); + return false; + } + + *node_id_set = palloc(sizeof(int) * node_count); + for (i = 0; i < node_count; i++) + { + *node_id_set[i] = value->u.array.values[i]->u.integer; + } + json_value_free(root); + return true; +} + + diff --git a/src/watchdog/wd_lifecheck.c b/src/watchdog/wd_lifecheck.c index 88bb748a8..144987102 100644 --- a/src/watchdog/wd_lifecheck.c +++ b/src/watchdog/wd_lifecheck.c @@ -438,9 +438,9 @@ static void print_lifecheck_cluster(void) static bool inform_node_status(LifeCheckNode* node, char *message) { - int node_status; + int node_status,x; char* json_data; - WDIPCCmdResult* res; + WDIPCCmdResult* res = NULL; char* new_status; if (node->nodeState == NODE_DEAD) @@ -464,10 +464,20 @@ static bool inform_node_status(LifeCheckNode* node, char *message) if (json_data == NULL) return false; - res = issue_command_to_watchdog(WD_NODE_STATUS_CHANGE_COMMAND, WD_COMMAND_ACTION_DEFAULT,0, json_data, strlen(json_data),false); - + for (x=0; x < MAX_SEC_WAIT_FOR_CLUSTER_TRANSATION; x++) + { + res = issue_command_to_watchdog(WD_NODE_STATUS_CHANGE_COMMAND, WD_COMMAND_ACTION_DEFAULT,0, json_data, strlen(json_data),false); + if (res) + break; + sleep(1); + } pfree(json_data); - return true; + if (res) + { + pfree(res); + return true; + } + return false; } static bool fetch_watchdog_nodes_data(void) [parent: 5e2a9440f8b0]