From d84b89f5a5f47087dbe3837a9dce01f11bc6273e Mon Sep 17 00:00:00 2001 From: Nozomi Anzai Date: Tue, 11 Oct 2011 07:39:04 +0000 Subject: [PATCH] - add pcp_promote_node command (This will be shown in Master/Slave & SR mode.) --- command.php | 9 +- common.php | 44 ++++++-- nodeStatus.php | 44 +++++--- status.php | 41 ++++++-- templates/nodeStatus.tpl | 15 ++- templates/status.tpl | 218 ++++++++++++++++++++++++++------------- 6 files changed, 265 insertions(+), 106 deletions(-) diff --git a/command.php b/command.php index 0c67c57..f316bad 100644 --- a/command.php +++ b/command.php @@ -32,7 +32,7 @@ require_once('common.php'); * @param srgs $num * @return array */ -function execPcp($command, $num='') +function execPcp($command, $num = '') { $pcpStatus = array ( '0' => 'SUCCESS', @@ -138,6 +138,13 @@ function execPcp($command, $num='') $ret = exec($cmd, $output, $return_var); break; + case 'PCP_PROMOTE_NODE': + // -g option means that standby doesn't become primary + // untill all connection get closed. + $cmd = _PGPOOL2_PCP_DIR . '/pcp_promote_node' .' -g '. $args; + $ret = exec($cmd, $output, $return_var); + break; + case 'PCP_RECOVERY_NODE': $cmd = _PGPOOL2_PCP_DIR . '/pcp_recovery_node' . $args; $ret = exec($cmd, $output, $return_var); diff --git a/common.php b/common.php index 11f3c13..120b394 100644 --- a/common.php +++ b/common.php @@ -186,7 +186,7 @@ function selectLanguage($selectLang, $messageList) $lang = 'en'; } else { - $langList = split(',|;', $acceptLanguages); + $langList = explode(',|;', $acceptLanguages); foreach ($langList as $acceptLanguage) { foreach (array_keys($messageList) as $messageLanguage) { if ($acceptLanguage == $messageLanguage ) { @@ -260,20 +260,16 @@ function NodeActive($num) */ function NodeStandby($num) { - - $params = readConfigParams(array('master_slave_mode','master_slave_sub_mode')); - - if ($params['master_slave_mode'] != 'true' || $params['master_slave_sub_mode'] != 'stream') { + if (isMasterSlaveMode() == FALSE || useStreaming() == FALSE) { return -1; } - $healthCheckDb = 'template1'; - $params = readHealthCheckParam(); $healthCheckUser = $params['health_check_user']; $backendHostName = $params['backend_hostname'][$num]; $backendPort = $params['backend_port'][$num]; + $healthCheckDb = 'template1'; if ($backendHostName != '') { $conStr = "dbname=$healthCheckDb user=$healthCheckUser host=$backendHostName port=$backendPort" ; @@ -374,7 +370,39 @@ function isMasterSlaveMode() { $params = readConfigParams(array('master_slave_mode')); - if ($params['master_slave_mode'] == 'true') { + if ($params['master_slave_mode'] == 'on') { + return TRUE; + } else { + return FALSE; + } +} + +/** + * Whether pgpool is using stream sub mode in master slave mode or not? + * + * @return bool + */ +function useStreaming() +{ + $params = readConfigParams(array('master_slave_sub_mode')); + + if (isMasterSlaveMode() && $params['master_slave_sub_mode'] == 'stream') { + return TRUE; + } else { + return FALSE; + } +} + +/** + * Whether pgpool uses syslog or not? + * + * @return bool + */ +function useSyslog() +{ + $params = readConfigParams(array('log_destination')); + + if ($params['log_destination'] == 'syslog') { return TRUE; } else { return FALSE; diff --git a/nodeStatus.php b/nodeStatus.php index eb042d3..6b279ca 100644 --- a/nodeStatus.php +++ b/nodeStatus.php @@ -29,10 +29,16 @@ $tpl->assign('help', basename( __FILE__, '.php')); $MAX_VALUE = PHP_INT_MAX; +// node status in "pcp_node_info" result +define('NODE_ACTIVE_NO_CONNECT', 1); +define('NODE_ACTIVE_CONNECTED', 2); +define('NODE_DOWN', 3); + if (!isset($_SESSION[SESSION_LOGIN_USER])) { exit(); } +// cout nodes $ret = execPcp('PCP_NODE_COUNT'); if (!array_key_exists('SUCCESS', $ret)) { $errorCode = 'e1002'; @@ -45,15 +51,15 @@ if (!array_key_exists('SUCCESS', $ret)) { $tpl->assign('nodeCount', $nodeCount); -$isParallelMode = isParallelMode(); -$isReplicationMode = isReplicationMode(); -$isMasterSlaveMode = isMasterSlaveMode(); - $nodeInfo = array(); $node_alive = FALSE; -for ($i = 0; $i<$nodeCount; $i++) { +// get nodes' status +for ($i = 0; $i < $nodeCount; $i++) { + // execute "pcp_node_info" command + // ex) host1 5432 1 1073741823.500000 $ret = execPcp('PCP_NODE_INFO', $i); + if (!array_key_exists('SUCCESS', $ret)) { $errorCode = 'e1003'; $tpl->assign('errorCode', $errorCode); @@ -65,14 +71,21 @@ for ($i = 0; $i<$nodeCount; $i++) { } $nodeInfo[$i] = explode(" ", $ret); + + // load balance weight: normalize format $nodeInfo[$i][3] = sprintf('%.3f', $nodeInfo[$i][3]); - /* node is active? */ - if ($nodeInfo[$i][2] != 3) { + // node is active? + if ($nodeInfo[$i][2] != NODE_DOWN) { $node_alive = TRUE; } } +// select buttons to each nodes depending on their status +$isParallelMode = isParallelMode(); +$isReplicationMode = isReplicationMode(); +$isMasterSlaveMode = isMasterSlaveMode(); + for ($i = 0; $i < $nodeCount; $i++) { if ($node_alive == FALSE) { if (($isReplicationMode || $isMasterSlaveMode) && NodeActive($i)) { @@ -81,21 +94,24 @@ for ($i = 0; $i < $nodeCount; $i++) { array_push($nodeInfo[$i], 'none'); } - } elseif( $isParallelMode ) { + } elseif ($isParallelMode ) { array_push($nodeInfo[$i], 'none'); } else { switch($nodeInfo[$i][2]) { - case 1: - case 2: + case NODE_ACTIVE_NO_CONNECT: + case NODE_ACTIVE_CONNECTED: if ($isReplicationMode || $isMasterSlaveMode) { array_push($nodeInfo[$i], 'disconnect'); } else { array_push($nodeInfo[$i], 'none'); } + if (useStreaming()) { + array_push($nodeInfo[$i], 'promote'); + } break; - case 3: + case NODE_DOWN: if ($isReplicationMode || $isMasterSlaveMode) { if (NodeActive($i)) { array_push($nodeInfo[$i], 'return'); @@ -109,10 +125,12 @@ for ($i = 0; $i < $nodeCount; $i++) { } } - $nodeInfo[$i][5] = NodeStandby($i); + // result of "SELECT pg_is_in_recovery()" as integer(0, 1, -1) + // (If pgpool don't act in Master/Slave & SR mode, this value will be ignored.) + $nodeInfo[$i][6] = NodeStandby($i); } -$tpl->assign('refreshTime', _PGPOOL2_STATUS_REFRESH_TIME*1000); +$tpl->assign('refreshTime', _PGPOOL2_STATUS_REFRESH_TIME * 1000); $tpl->assign('nodeInfo', $nodeInfo); $tpl->assign('parallelMode', $isParallelMode); $tpl->assign('msgStopPgpool', $message['msgStopPgpool']); diff --git a/status.php b/status.php index db4cc49..ce87a72 100644 --- a/status.php +++ b/status.php @@ -50,7 +50,6 @@ if (isset($_POST['action'])) { /** * Set pgpool command option */ - $tpl->assign('c', _PGPOOL2_CMD_OPTION_C); $tpl->assign('D', _PGPOOL2_CMD_OPTION_LARGE_D); $tpl->assign('d', _PGPOOL2_CMD_OPTION_D); @@ -64,6 +63,11 @@ if (isPipe(_PGPOOL2_LOG_FILE)) { } switch ($action) { + + /* --------------------------------------------------------------------- */ + /* start */ + /* --------------------------------------------------------------------- */ + case 'start': $args = ' '; @@ -95,7 +99,7 @@ switch ($action) { $tpl->assign('pgpoolMessage', $ret); } else { for ($i = 0; $i < 10; $i++) { - if(DoesPgpoolPidExist()) { + if (DoesPgpoolPidExist()) { break; } else { sleep(1); @@ -112,6 +116,10 @@ switch ($action) { break; + /* --------------------------------------------------------------------- */ + /* stop */ + /* --------------------------------------------------------------------- */ + case 'stop': $m = $_POST['stop_mode']; @@ -140,6 +148,10 @@ switch ($action) { break; + /* --------------------------------------------------------------------- */ + /* restart */ + /* --------------------------------------------------------------------- */ + case 'restart': /** * Stop pgpool @@ -154,7 +166,7 @@ switch ($action) { exit(); } else { - for($i = 0; $i < 10; $i++) { + for ($i = 0; $i < 10; $i++) { if (DoesPgpoolPidExist()) { sleep(1); } else { @@ -216,15 +228,15 @@ switch ($action) { } break; + /* --------------------------------------------------------------------- */ + /* other */ + /* --------------------------------------------------------------------- */ + case 'reload': - /** - * reload pgpool - */ $args = ' '; $ret = execPcp('PCP_RELOAD_PGPOOL', $args); break; - case 'return': $ret = execPcp('PCP_ATTACH_NODE', $nodeNumber); if (!array_key_exists('SUCCESS', $ret)) { @@ -245,7 +257,6 @@ switch ($action) { } break; - case 'detach': $ret = execPcp('PCP_DETACH_NODE', $nodeNumber); if (!array_key_exists('SUCCESS', $ret)) { @@ -256,6 +267,16 @@ switch ($action) { } break; + case 'promote': + $ret = execPcp('PCP_PROMOTE_NODE', $nodeNumber); + if (!array_key_exists('SUCCESS', $ret)) { + $errorCode = 'e1007'; + $tpl->assign('errorCode', $errorCode); + $tpl->display('error.tpl'); + exit(); + } + break; + case 'summary': $viewPHP = 'innerSummary.php'; break; @@ -279,15 +300,15 @@ if (DoesPgpoolPidExist()) { $tpl->assign('pgpoolIsActive', FALSE); } -$tpl->assign('viewPHP', $viewPHP); - if (_PGPOOL2_STATUS_REFRESH_TIME >= 0 ) { $refreshTime = _PGPOOL2_STATUS_REFRESH_TIME * 1000; } +$tpl->assign('viewPHP', $viewPHP); $tpl->assign('pgpoolConf', _PGPOOL2_CONFIG_FILE); $tpl->assign('pcpConf', _PGPOOL2_PASSWORD_FILE); $tpl->assign('refreshTime', $refreshTime); +$tpl->assign('useSyslog', useSyslog()); $tpl->assign('msgStopPgpool', $message['msgStopPgpool']); $tpl->display('status.tpl'); diff --git a/templates/nodeStatus.tpl b/templates/nodeStatus.tpl index 00be20a..6b3cccd 100644 --- a/templates/nodeStatus.tpl +++ b/templates/nodeStatus.tpl @@ -33,16 +33,16 @@ {$nodeInfo[num][1]|escape} - {if $nodeInfo[num][2] == 1} + {if $nodeInfo[num][2] == $smarty.const.NODE_ACTIVE_NO_CONNECT} {$message.strNodeStatus1|escape} - {elseif $nodeInfo[num][2] == 2} + {elseif $nodeInfo[num][2] == $smarty.const.NODE_ACTIVE_CONNECTED} {$message.strNodeStatus2|escape} - {elseif $nodeInfo[num][2] == 3} + {elseif $nodeInfo[num][2] == $smarty.const.NODE_DOWN} {$message.strNodeStatus3|escape} {/if} - {if $nodeInfo[num][5] == 1} + {if $nodeInfo[num][6] == 1} {$message.strStandbyRunning|escape} - {elseif $nodeInfo[num][5] == 0} + {elseif $nodeInfo[num][6] == 0} {$message.strPrimaryRunning|escape} {/if} @@ -65,6 +65,11 @@ onclick="sendCommand('recovery', {$smarty.section.num.index|escape}, '{$message.msgRecoveryConfirm|escape}')" value="{$message.strRecovery|escape}" /> {/if} + {if $nodeInfo[num][5] == 'promote' && $nodeInfo[num][6] == 1} + + {/if} diff --git a/templates/status.tpl b/templates/status.tpl index b1ae2e7..04068a1 100644 --- a/templates/status.tpl +++ b/templates/status.tpl @@ -8,23 +8,25 @@