Beispiel #1
0
 function SlaveConnection()
 {
     if (!class_exists('cmodule') || !class_exists('csqlwhere')) {
         return null;
     }
     if (!CModule::IncludeModule('cluster')) {
         return false;
     }
     $arSlaves = CClusterSlave::GetList();
     if (empty($arSlaves)) {
         return false;
     }
     $max_slave_delay = COption::GetOptionInt("cluster", "max_slave_delay", 10);
     if (isset($_SESSION["BX_REDIRECT_TIME"])) {
         $redirect_delay = time() - $_SESSION["BX_REDIRECT_TIME"] + 1;
         if ($redirect_delay > 0 && $redirect_delay < $max_slave_delay) {
             $max_slave_delay = $redirect_delay;
         }
     }
     $total_weight = 0;
     foreach ($arSlaves as $i => $slave) {
         if (defined("BX_CLUSTER_GROUP") && BX_CLUSTER_GROUP != $slave["GROUP_ID"]) {
             unset($arSlaves[$i]);
         } elseif ($slave["ROLE_ID"] == "SLAVE") {
             $arSlaveStatus = CClusterSlave::GetStatus($slave["ID"], true, false, false);
             if ($arSlaveStatus['Seconds_Behind_Master'] > $max_slave_delay || $arSlaveStatus['Last_SQL_Error'] != '' || $arSlaveStatus['Last_IO_Error'] != '' || $arSlaveStatus['Slave_SQL_Running'] === 'No') {
                 unset($arSlaves[$i]);
             } else {
                 $total_weight += $slave["WEIGHT"];
             }
         } else {
             $total_weight += $slave["WEIGHT"];
         }
     }
     $found = false;
     foreach ($arSlaves as $slave) {
         if (mt_rand(0, $total_weight) < $slave["WEIGHT"]) {
             $found = $slave;
             break;
         }
     }
     if (!$found || $found["ROLE_ID"] != "SLAVE") {
         return false;
         //use main connection
     } else {
         ob_start();
         $conn = CDatabase::GetDBNodeConnection($found["ID"], true);
         ob_end_clean();
         if (is_object($conn)) {
             return $conn;
         } else {
             self::$arNodes[$found["ID"]]["ONHIT_ERROR"] = true;
             CClusterDBNode::SetOffline($found["ID"]);
             return false;
             //use main connection
         }
     }
 }
}
$cData = new CClusterDBNode();
$rsData = $cData->GetList(array("ID" => "ASC"), $arFilter);
if (!isset($_SESSION["SLAVE_LIST"])) {
    $_SESSION["SLAVE_LIST"] = array();
}
$rsData = new CAdminResult($rsData, $sTableID);
$Position = 0;
$bNote1Show = false;
$bHasMaster = false;
while ($arRes = $rsData->Fetch()) {
    $row =& $lAdmin->AddRow($arRes["ID"], $arRes);
    if ($arRes["ROLE_ID"] == "MASTER" || $arRes["ROLE_ID"] == "MAIN") {
        $bHasMaster = true;
    }
    $arSlaveStatus = CClusterSlave::GetStatus($arRes["ID"]);
    if (is_array($arSlaveStatus) && $arRes["STATUS"] == "OFFLINE") {
        CClusterDBNode::SetOnline($arRes["ID"]);
        $arRes["STATUS"] = "ONLINE";
    }
    if ($arRes["STATUS"] != "OFFLINE") {
        $uptime = CClusterDBNode::GetUpTime($arRes["ID"]);
    } else {
        $uptime = false;
    }
    if ($arRes["ID"] > 1) {
        $row->AddViewField("ID", '<a href="cluster_slave_edit.php?lang=' . LANGUAGE_ID . '&group_id=' . $arRes["GROUP_ID"] . '&ID=' . $arRes["ID"] . '">' . $arRes["ID"] . '</a>');
    }
    $Seconds_Behind_Master = 0;
    $Slave_IO_Running = 'Yes';
    $bHasSQLError = false;