10 public static function SetOnLine($node_id, $master_id)
15 if (!is_array($arNode))
20 if ($arNode[
'ROLE_ID'] ==
'SLAVE')
29 $masterDB = CDatabase::GetDBNodeConnection($master_id,
true);
33 $rs = $masterDB->Query(
'show master status',
false,
'', [
'fixed_connection' =>
true]);
34 if ($arMasterStatus =
$rs->Fetch())
37 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true);
39 if (is_object($nodeDB))
41 $rs = $nodeDB->Query(
"
43 MASTER_HOST = '" .
$DB->ForSql($arNode[
'MASTER_HOST']) .
"'
44 ,MASTER_USER = '" .
$DB->ForSql($masterDB->DBLogin) .
"'
45 ,MASTER_PASSWORD = '" .
$DB->ForSql($masterDB->DBPassword) .
"'
46 ,MASTER_PORT = " .
$DB->ForSql($arNode[
'MASTER_PORT']) .
"
47 ,MASTER_LOG_FILE = '" . $arMasterStatus[
'File'] .
"'
48 ,MASTER_LOG_POS = " . $arMasterStatus[
'Position'] .
'
49 ',
false,
'', [
'fixed_connection' =>
true]);
53 $rs = $nodeDB->Query(
'START SLAVE');
59 $obNode->
Update($node_id, [
'MASTER_ID' => $master_id]);
67 elseif ($arNode[
'ROLE_ID'] ==
'MASTER' && preg_match(
'/^(.+):(\\d+)$/', $arNode[
'DB_HOST'], $match))
69 $rs =
$DB->Query(
'show master status',
false,
'', [
'fixed_connection' =>
true]);
70 if ($arMasterStatus =
$rs->Fetch())
73 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true);
75 if (is_object($nodeDB))
77 $nodeDB->Query(
'STOP SLAVE',
false,
'', [
'fixed_connection' =>
true]);
81 MASTER_HOST = '" .
$DB->ForSql($arNode[
'MASTER_HOST']) .
"'
82 ,MASTER_USER = '" .
$DB->ForSql(
$DB->DBLogin) .
"'
83 ,MASTER_PASSWORD = '" .
$DB->ForSql(
$DB->DBPassword) .
"'
84 ,MASTER_PORT = " .
$DB->ForSql($arNode[
'MASTER_PORT']) .
"
85 ,MASTER_LOG_FILE = '" . $arMasterStatus[
'File'] .
"'
86 ,MASTER_LOG_POS = " . $arMasterStatus[
'Position'] .
'
87 ',
false,
'', [
'fixed_connection' =>
true]);
89 $nodeDB->Query(
'START SLAVE',
false,
'', [
'fixed_connection' =>
true]);
91 $rs = $nodeDB->Query(
'show master status',
false,
'', [
'fixed_connection' =>
true]);
92 if ($arMasterStatus =
$rs->Fetch())
94 $rs =
$DB->Query(
'STOP SLAVE',
true,
'', [
'fixed_connection' =>
true]);
100 MASTER_HOST = '" .
$DB->ForSql($match[1]) .
"'
101 ,MASTER_USER = '" .
$DB->ForSql($arNode[
'DB_LOGIN']) .
"'
102 ,MASTER_PASSWORD = '" .
$DB->ForSql($arNode[
'DB_PASSWORD']) .
"'
103 ,MASTER_PORT = " .
$DB->ForSql($match[2]) .
"
104 ,MASTER_LOG_FILE = '" . $arMasterStatus[
'File'] .
"'
105 ,MASTER_LOG_POS = " . $arMasterStatus[
'Position'] .
'
106 ',
false,
'', [
'fixed_connection' =>
true]);
111 $rs =
$DB->Query(
'START SLAVE');
117 $obNode->
Update($node_id, [
'MASTER_ID' => $master_id]);
118 $obNode->Update($master_id, [
'MASTER_ID' => $node_id]);
129 public static function Pause($node_id)
134 if (!is_array($arNode))
146 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true);
150 if (!is_object($nodeDB))
155 $rs = $nodeDB->Query(
'STOP SLAVE SQL_THREAD',
false,
'', [
'fixed_connection' =>
true]);
159 $ob->
Update($arNode[
'ID'], [
'STATUS' =>
'PAUSED']);
168 if (!is_array($arNode))
180 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true,
false);
184 if (!is_object($nodeDB))
189 $rs = $nodeDB->Query(
'START SLAVE',
false,
'', [
'fixed_connection' =>
true]);
193 $ob->
Update($arNode[
'ID'], [
'STATUS' =>
'ONLINE']);
197 public static function Stop($node_id)
202 if (!is_array($arNode))
214 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true,
false);
218 if (!is_object($nodeDB))
223 $rs = $nodeDB->Query(
'STOP SLAVE',
false,
'', [
'fixed_connection' =>
true]);
229 $res = $ob->Update($arNode[
'ID'], [
'MASTER_ID' =>
false,
'STATUS' =>
'ONLINE']);
233 $res = $ob->Update($arNode[
'ID'], [
'STATUS' =>
'READY']);
249 if (is_array($arNode))
258 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true,
false);
262 if (is_object($nodeDB))
265 $rs = $nodeDB->Query(
'STOP SLAVE',
false,
'', [
'fixed_connection' =>
true]);
269 $rs = $nodeDB->Query(
'SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1',
true,
'', [
'fixed_connection' =>
true]);
272 return $nodeDB->GetErrorMessage();
278 $nodeDB->Query(
'START SLAVE',
false,
'', [
'fixed_connection' =>
true]);
286 public static function GetStatus($node_id, $bSlaveStatus =
true, $bGlobalStatus =
true, $bVariables =
true)
291 if (!is_array($arNode))
305 $nodeDB = CDatabase::GetDBNodeConnection($node_id,
true,
false);
307 catch (\
Bitrix\Main\DB\ConnectionException $_)
314 if (!is_object($nodeDB))
319 $arStatus = [
'server_id' =>
null];
323 $rs = $nodeDB->Query(
"show variables like 'server_id'",
false,
'', [
'fixed_connection' =>
true]);
326 $arStatus[
'server_id'] =
$ar[
'Value'];
330 $cacheID =
'slave_for_master';
335 CACHED_b_cluster_dbnode !==
false
336 && $managedCache->read(CACHED_b_cluster_dbnode, $cacheID,
'b_cluster_dbnode')
339 $existSlave = $managedCache->get($cacheID);
347 $managedCache->set($cacheID, $existSlave);
352 $arStatus = array_merge($arStatus, [
359 $rs = $nodeDB->Query(
'SHOW MASTER STATUS',
true,
'', [
'fixed_connection' =>
true]);
362 return GetMessage(
'CLU_NO_PRIVILEGES', [
'#sql#' =>
"GRANT REPLICATION CLIENT on *.* to '" . $nodeDB->DBLogin .
"'@'%';"]);
368 foreach (
$ar as
$key => $value)
370 if (
$key ==
'Last_Error')
372 $key =
'Last_SQL_Error';
375 if (array_key_exists(
$key, $arStatus))
377 $arStatus[
$key] = $value;
384 if ($arNode[
'MASTER_ID'] <>
'')
386 $arStatus = array_merge($arStatus, [
387 'Slave_IO_State' =>
null,
388 'Slave_IO_Running' =>
null,
389 'Read_Master_Log_Pos' =>
null,
390 'Slave_SQL_Running' =>
null,
391 'Exec_Master_Log_Pos' =>
null,
392 'Seconds_Behind_Master' =>
null,
393 'Last_IO_Error' =>
null,
394 'Last_SQL_Error' =>
null,
395 'Com_select' =>
null,
400 $rs = $nodeDB->Query(
'SHOW SLAVE STATUS',
true,
'', [
'fixed_connection' =>
true]);
403 return GetMessage(
'CLU_NO_PRIVILEGES', [
'#sql#' =>
"GRANT REPLICATION CLIENT on *.* to '" . $nodeDB->DBLogin .
"'@'%';"]);
409 foreach (
$ar as
$key => $value)
411 if (
$key ==
'Last_Error')
413 $key =
'Last_SQL_Error';
416 if (array_key_exists(
$key, $arStatus))
418 $arStatus[
$key] = $value;
427 $rs = $nodeDB->Query(
"show global status where Variable_name in ('Com_select', 'Com_do')",
true,
'', [
'fixed_connection' =>
true]);
430 while (
$ar =
$rs->Fetch())
432 if (!isset($arStatus[
'Com_select']))
434 $arStatus[
'Com_select'] = 0;
437 if (
$ar[
'Variable_name'] ==
'Com_do')
439 $arStatus[
'Com_select'] -=
$ar[
'Value'] * 2;
443 $arStatus[
'Com_select'] +=
$ar[
'Value'];
449 $rs = $nodeDB->Query(
"show status like 'Com_select'",
false,
'', [
'fixed_connection' =>
true]);
453 $arStatus[
'Com_select'] +=
$ar[
'Value'];
456 $rs = $nodeDB->Query(
"show status like 'Com_do'",
false,
'', [
'fixed_connection' =>
true]);
460 $arStatus[
'Com_select'] -=
$ar[
'Value'] * 2;
468 public static function GetList():
array
471 static $slaves =
false;
472 if ($slaves ===
false)
474 $cacheID =
'db_slaves_v2';
477 $cache = Application::getInstance()->getManagedCache();
479 CACHED_b_cluster_dbnode !==
false
480 && $cache->read(CACHED_b_cluster_dbnode, $cacheID,
'b_cluster_dbnode')
483 $slaves = $cache->get($cacheID);
490 SELECT ID, WEIGHT, ROLE_ID, GROUP_ID
491 FROM b_cluster_dbnode
492 WHERE STATUS = 'ONLINE' AND (SELECTABLE is null or SELECTABLE = 'Y')
494 ",
false,
'', [
'fixed_connection' =>
true]);
495 while (
$ar =
$rs->Fetch())
497 $slaves[intval(
$ar[
'ID'])] =
$ar;
500 if (CACHED_b_cluster_dbnode !==
false)
502 $cache->set($cacheID, $slaves);
515 $rs = $nodeDB->Query(
"show variables like 'server_id'",
false,
'', [
'fixed_connection' =>
true]);
518 if (
$ar[
'Value'] != $arNode[
'SERVER_ID'])
521 $ob->
Update($arNode[
'ID'], [
'SERVER_ID' =>
$ar[
'Value']]);
528 static $max_slave_delay =
null;
529 if (!isset($max_slave_delay))
531 $max_slave_delay = (int) Option::get(
'cluster',
'max_slave_delay');
533 Application::getInstance()->isInitialized()
534 && isset(Application::getInstance()->getKernelSession()[
'BX_REDIRECT_TIME'])
537 $redirect_delay = time() - Application::getInstance()->getKernelSession()[
'BX_REDIRECT_TIME'] + 1;
540 && $redirect_delay < $max_slave_delay
543 $max_slave_delay = $redirect_delay;
547 return $max_slave_delay;
552 $cache = \Bitrix\Main\Data\Cache::createInstance();
553 if ($cache->initCache(
554 (
int) Option::get(
'cluster',
'slave_status_cache_time'),
555 'cluster_slave_status_' . (
int) $slave_id,
559 $slaveStatus = $cache->getVars();
563 $slaveStatus = static::GetStatus($slave_id,
true,
false,
false);
568 || $slaveStatus[
'Seconds_Behind_Master'] > static::GetMaxSlaveDelay()
569 || $slaveStatus[
'Last_SQL_Error'] !=
''
570 || $slaveStatus[
'Last_IO_Error'] !=
''
571 || $slaveStatus[
'Slave_SQL_Running'] ===
'No'
574 if ($cache->startDataCache())
576 $cache->endDataCache($slaveStatus);
585 $slaves = static::GetList();
592 foreach ($slaves as
$i => $slave)
594 $isOtherGroup = defined(
'BX_CLUSTER_GROUP') && ($slave[
'GROUP_ID'] != constant(
'BX_CLUSTER_GROUP'));
596 defined(
'BX_CLUSTER_SLAVE_USE_ANY_GROUP')
597 && constant(
'BX_CLUSTER_SLAVE_USE_ANY_GROUP') ===
true
598 && $slave[
'ROLE_ID'] ==
'SLAVE'
601 $isOtherGroup =
false;
619 foreach ($slaves as
$i => $slave)
621 $total_weight += $slave[
'WEIGHT'];
622 $slaves[
$i][
'PIE_WEIGHT'] = $total_weight;
625 $rand = ($total_weight > 0 ? mt_rand(1, $total_weight) : 0);
626 foreach ($slaves as
$i => $slave)
628 if ($rand <= $slave[
'PIE_WEIGHT'])
630 if ($slave[
'ROLE_ID'] ==
'SLAVE')
632 if (!static::IsSlaveOk($slave[
'ID']))
645 if (!$found || $found[
'ROLE_ID'] !=
'SLAVE')
static SetOnline($node_id)
static GetList($arOrder=false, $arFilter=false, $arSelect=false)
static GetByID($node_id, $arVirtNode=false)
static SkipSQLError($node_id)
static GetStatus($node_id, $bSlaveStatus=true, $bGlobalStatus=true, $bVariables=true)
static AdjustServerID($arNode, $nodeDB)
static GetMaxSlaveDelay()
static IsSlaveOk($slave_id)
static SetOnLine($node_id, $master_id)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
GetMessage($name, $aReplace=null)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key