10 public static function Pause($node_id)
15 if (!is_array($arNode))
27 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true);
31 if (!is_object($nodeDB))
36 $rs = $nodeDB->Query(
'select pg_wal_replay_pause()',
false,
'', [
'fixed_connection' =>
true]);
40 $ob->
Update($arNode[
'ID'], [
'STATUS' =>
'PAUSED']);
44 public static function Resume($node_id)
49 if (!is_array($arNode))
61 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true,
false);
65 if (!is_object($nodeDB))
70 $rs = $nodeDB->Query(
'select pg_wal_replay_resume()',
false,
'', [
'fixed_connection' =>
true]);
74 $ob->
Update($arNode[
'ID'], [
'STATUS' =>
'ONLINE']);
78 public static function GetStatus($node_id, $bSlaveStatus =
true, $bGlobalStatus =
true, $bVariables =
true)
83 if (!is_array($arNode))
88 if ($node_id == 1 || $arNode[
'MASTER_ID'] == 1)
97 $masterDB = CDatabase::GetDBNodeConnection($arNode[
'MASTER_ID'],
true,
false);
99 catch (\
Bitrix\Main\DB\ConnectionException $_)
106 if (!is_object($masterDB))
111 $arStatus = [
'Rows_returned' =>
null];
113 if ($arNode[
'MASTER_ID'] <>
'')
115 $arStatus = array_merge($arStatus, [
117 'sync_state' =>
null,
119 'replay_lsn' =>
null,
120 'Seconds_Behind_Master' =>
null,
125 $rs = $masterDB->Query(
'select STATE,SYNC_STATE,SENT_LSN,REPLAY_LSN,REPLAY_LAG from bx_cluster_stat_replication() WHERE CLIENT_ADDR = \'' . $masterDB->ForSql($arNode[
'DB_HOST']) .
'\'',
true,
'', [
'fixed_connection' =>
true]);
134 $arStatus[
'state'] =
$ar[
'STATE'];
135 $arStatus[
'sync_state'] =
$ar[
'SYNC_STATE'];
136 $arStatus[
'sent_lsn'] =
$ar[
'SENT_LSN'];
137 $arStatus[
'replay_lsn'] =
$ar[
'REPLAY_LSN'];
141 sscanf(
$ar[
'REPLAY_LAG'] ?:
'00:00:00',
'%d:%d:%d',
$hours, $minutes, $seconds);
142 $arStatus[
'Seconds_Behind_Master'] =
$hours * 3600 + $minutes * 60 + $seconds;
158 $nodeDB = CDatabase::GetDBNodeConnection($arNode[
'ID'],
true,
false);
160 catch (\
Bitrix\Main\DB\ConnectionException $_)
167 if (is_object($nodeDB))
169 $rs = $nodeDB->Query(
"select TUP_RETURNED from pg_stat_database where DATNAME = '" . $nodeDB->ForSql($nodeDB->DBName) .
"'",
true,
'', [
'fixed_connection' =>
true]);
172 while (
$ar =
$rs->Fetch())
174 if (!isset($arStatus[
'Rows_returned']))
176 $arStatus[
'Rows_returned'] = 0;
179 $arStatus[
'Rows_returned'] +=
$ar[
'TUP_RETURNED'];
188 public static function GetList():
array
191 static $slaves =
false;
192 if ($slaves ===
false)
194 $cacheID =
'db_slaves_v2';
197 $cache = Application::getInstance()->getManagedCache();
199 CACHED_b_cluster_dbnode !==
false
200 && $cache->read(CACHED_b_cluster_dbnode, $cacheID,
'b_cluster_dbnode')
203 $slaves = $cache->get($cacheID);
210 SELECT ID, WEIGHT, ROLE_ID, GROUP_ID
211 FROM b_cluster_dbnode
212 WHERE STATUS = 'ONLINE' AND (SELECTABLE is null or SELECTABLE = 'Y')
214 ",
false,
'', [
'fixed_connection' =>
true]);
215 while (
$ar =
$rs->Fetch())
217 $slaves[intval(
$ar[
'ID'])] =
$ar;
220 if (CACHED_b_cluster_dbnode !==
false)
222 $cache->set($cacheID, $slaves);
231 static $max_slave_delay =
null;
232 if (!isset($max_slave_delay))
234 $max_slave_delay = (int) Option::get(
'cluster',
'max_slave_delay');
236 Application::getInstance()->isInitialized()
237 && isset(Application::getInstance()->getKernelSession()[
'BX_REDIRECT_TIME'])
240 $redirect_delay = time() - Application::getInstance()->getKernelSession()[
'BX_REDIRECT_TIME'] + 1;
243 && $redirect_delay < $max_slave_delay
246 $max_slave_delay = $redirect_delay;
250 return $max_slave_delay;
255 $cache = \Bitrix\Main\Data\Cache::createInstance();
256 if ($cache->initCache(
257 (
int) Option::get(
'cluster',
'slave_status_cache_time'),
258 'cluster_slave_status_' . (
int) $slave_id,
262 $slaveStatus = $cache->getVars();
266 $slaveStatus = static::GetStatus($slave_id,
true,
false,
false);
271 || $slaveStatus[
'Seconds_Behind_Master'] > static::GetMaxSlaveDelay()
272 || $slaveStatus[
'state'] !=
'streaming'
275 if ($cache->startDataCache())
277 $cache->endDataCache($slaveStatus);
286 $slaves = static::GetList();
293 foreach ($slaves as
$i => $slave)
295 $isOtherGroup = defined(
'BX_CLUSTER_GROUP') && ($slave[
'GROUP_ID'] != constant(
'BX_CLUSTER_GROUP'));
297 defined(
'BX_CLUSTER_SLAVE_USE_ANY_GROUP')
298 && constant(
'BX_CLUSTER_SLAVE_USE_ANY_GROUP') ===
true
299 && $slave[
'ROLE_ID'] ==
'SLAVE'
302 $isOtherGroup =
false;
320 foreach ($slaves as
$i => $slave)
322 $total_weight += $slave[
'WEIGHT'];
323 $slaves[
$i][
'PIE_WEIGHT'] = $total_weight;
326 $rand = ($total_weight > 0 ? mt_rand(1, $total_weight) : 0);
327 foreach ($slaves as
$i => $slave)
329 if ($rand <= $slave[
'PIE_WEIGHT'])
331 if ($slave[
'ROLE_ID'] ==
'SLAVE')
333 if (!static::IsSlaveOk($slave[
'ID']))
346 if (!$found || $found[
'ROLE_ID'] !=
'SLAVE')
static GetByID($node_id, $arVirtNode=false)
static GetStatus($node_id, $bSlaveStatus=true, $bGlobalStatus=true, $bVariables=true)
static GetMaxSlaveDelay()
static IsSlaveOk($slave_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)