예제 #1
0
 function SetOnLine($node_id, $master_id)
 {
     global $DB;
     $arNode = CClusterDBNode::GetByID($node_id);
     if (!is_array($arNode)) {
         return;
     }
     if ($arNode["ROLE_ID"] == "SLAVE") {
         if ($master_id == 1) {
             $masterDB = $DB;
         } else {
             ob_start();
             $masterDB = CDatabase::GetDBNodeConnection($master_id, true);
             $error = ob_get_contents();
             ob_end_clean();
         }
         $rs = $masterDB->Query("show master status", false, '', array('fixed_connection' => true));
         if ($arMasterStatus = $rs->Fetch()) {
             ob_start();
             $nodeDB = CDatabase::GetDBNodeConnection($arNode["ID"], true);
             $error = ob_get_contents();
             ob_end_clean();
             if (is_object($nodeDB)) {
                 $rs = $nodeDB->Query("\n\t\t\t\t\t\tCHANGE MASTER TO\n\t\t\t\t\t\t\tMASTER_HOST = '" . $DB->ForSQL($arNode["MASTER_HOST"]) . "'\n\t\t\t\t\t\t\t,MASTER_USER = '******'\n\t\t\t\t\t\t\t,MASTER_PASSWORD = '******'\n\t\t\t\t\t\t\t,MASTER_PORT = " . $DB->ForSQL($arNode["MASTER_PORT"]) . "\n\t\t\t\t\t\t\t,MASTER_LOG_FILE = '" . $arMasterStatus["File"] . "'\n\t\t\t\t\t\t\t,MASTER_LOG_POS = " . $arMasterStatus["Position"] . "\n\t\t\t\t\t", false, '', array('fixed_connection' => true));
                 if ($rs) {
                     $rs = $nodeDB->Query("START SLAVE");
                 }
                 if ($rs) {
                     $obNode = new CClusterDBNode();
                     $obNode->Update($node_id, array("MASTER_ID" => $master_id));
                     CClusterDBNode::SetOnline($node_id);
                     CClusterSlave::AdjustServerID($arNode, $nodeDB);
                 }
             }
         }
     } elseif ($arNode["ROLE_ID"] == "MASTER" && preg_match("/^(.+):(\\d+)\$/", $arNode["DB_HOST"], $match)) {
         $rs = $DB->Query("show master status", false, '', array('fixed_connection' => true));
         if ($arMasterStatus = $rs->Fetch()) {
             ob_start();
             $nodeDB = CDatabase::GetDBNodeConnection($arNode["ID"], true);
             $error = ob_get_contents();
             ob_end_clean();
             if (is_object($nodeDB)) {
                 $rs = $nodeDB->Query("STOP SLAVE", true, '', array('fixed_connection' => true));
                 if ($rs) {
                     $rs = $nodeDB->Query("\n\t\t\t\t\t\t\tCHANGE MASTER TO\n\t\t\t\t\t\t\t\tMASTER_HOST = '" . $DB->ForSQL($arNode["MASTER_HOST"]) . "'\n\t\t\t\t\t\t\t\t,MASTER_USER = '******'\n\t\t\t\t\t\t\t\t,MASTER_PASSWORD = '******'\n\t\t\t\t\t\t\t\t,MASTER_PORT = " . $DB->ForSQL($arNode["MASTER_PORT"]) . "\n\t\t\t\t\t\t\t\t,MASTER_LOG_FILE = '" . $arMasterStatus["File"] . "'\n\t\t\t\t\t\t\t\t,MASTER_LOG_POS = " . $arMasterStatus["Position"] . "\n\t\t\t\t\t\t", false, '', array('fixed_connection' => true));
                 }
                 if ($rs) {
                     $rs = $nodeDB->Query("START SLAVE");
                 }
                 if ($rs) {
                     $rs = $nodeDB->Query("show master status", false, '', array('fixed_connection' => true));
                     if ($arMasterStatus = $rs->Fetch()) {
                         $rs = $DB->Query("STOP SLAVE", true, '', array('fixed_connection' => true));
                         if ($rs) {
                             $rs = $DB->Query("\n\t\t\t\t\t\t\t\t\tCHANGE MASTER TO\n\t\t\t\t\t\t\t\t\t\tMASTER_HOST = '" . $DB->ForSQL($match[1]) . "'\n\t\t\t\t\t\t\t\t\t\t,MASTER_USER = '******'\n\t\t\t\t\t\t\t\t\t\t,MASTER_PASSWORD = '******'\n\t\t\t\t\t\t\t\t\t\t,MASTER_PORT = " . $DB->ForSQL($match[2]) . "\n\t\t\t\t\t\t\t\t\t\t,MASTER_LOG_FILE = '" . $arMasterStatus["File"] . "'\n\t\t\t\t\t\t\t\t\t\t,MASTER_LOG_POS = " . $arMasterStatus["Position"] . "\n\t\t\t\t\t\t\t\t", false, '', array('fixed_connection' => true));
                         }
                         if ($rs) {
                             $rs = $DB->Query("START SLAVE");
                         }
                         if ($rs) {
                             $obNode = new CClusterDBNode();
                             $obNode->Update($node_id, array("MASTER_ID" => $master_id));
                             $obNode->Update($master_id, array("MASTER_ID" => $node_id));
                             CClusterDBNode::SetOnline($node_id);
                             CClusterSlave::AdjustServerID($arNode, $nodeDB);
                         }
                     }
                 }
             }
         }
     }
 }