4.4. 监控跨站点复制的状态
监控备份位置的站点状态,以检测站点之间的通信中断。当远程站点状态变为 离线时,Data Grid 会停止将数据复制到备份位置。您的数据不同步,您必须在使集群重新上线前修复不一致的问题。
对于早期问题检测,需要监控跨站点事件。使用以下监控策略之一:
使用 REST API 监控跨站点复制
使用 REST 端点监控所有缓存的跨站点复制状态。您可以实施自定义脚本来轮询 REST 端点,或者使用以下示例。
先决条件
- 启用跨站点复制。
流程
实施脚本以轮询 REST 端点。
以下示例演示了如何使用 Python 脚本每 5 秒轮询站点状态。
#!/usr/bin/python3
import time
import requests
from requests.auth import HTTPDigestAuth
class InfinispanConnection:
def __init__(self, server: str = 'http://localhost:11222', cache_manager: str = 'default',
auth: tuple = ('admin', 'change_me')) -> None:
super().__init__()
self.__url = f'{server}/rest/v2/container/x-site/backups/'
self.__auth = auth
self.__headers = {
'accept': 'application/json'
}
def get_sites_status(self):
try:
rsp = requests.get(self.__url, headers=self.__headers, auth=HTTPDigestAuth(self.__auth[0], self.__auth[1]))
if rsp.status_code != 200:
return None
return rsp.json()
except:
return None
# Specify credentials for Data Grid user with permission to access the REST endpoint
USERNAME = 'admin'
PASSWORD = 'change_me'
# Set an interval between cross-site status checks
POLL_INTERVAL_SEC = 5
# Provide a list of servers
SERVERS = [
InfinispanConnection('http://127.0.0.1:11222', auth=(USERNAME, PASSWORD)),
InfinispanConnection('http://127.0.0.1:12222', auth=(USERNAME, PASSWORD))
]
#Specify the names of remote sites
REMOTE_SITES = [
'nyc'
]
#Provide a list of caches to monitor
CACHES = [
'work',
'sessions'
]
def on_event(site: str, cache: str, old_status: str, new_status: str):
# TODO implement your handling code here
print(f'site={site} cache={cache} Status changed {old_status} -> {new_status}')
def __handle_mixed_state(state: dict, site: str, site_status: dict):
if site not in state:
state[site] = {c: 'online' if c in site_status['online'] else 'offline' for c in CACHES}
return
for cache in CACHES:
__update_cache_state(state, site, cache, 'online' if cache in site_status['online'] else 'offline')
def __handle_online_or_offline_state(state: dict, site: str, new_status: str):
if site not in state:
state[site] = {c: new_status for c in CACHES}
return
for cache in CACHES:
__update_cache_state(state, site, cache, new_status)
def __update_cache_state(state: dict, site: str, cache: str, new_status: str):
old_status = state[site].get(cache)
if old_status != new_status:
on_event(site, cache, old_status, new_status)
state[site][cache] = new_status
def update_state(state: dict):
rsp = None
for conn in SERVERS:
rsp = conn.get_sites_status()
if rsp:
break
if rsp is None:
print('Unable to fetch site status from any server')
return
for site in REMOTE_SITES:
site_status = rsp.get(site, {})
new_status = site_status.get('status')
if new_status == 'mixed':
__handle_mixed_state(state, site, site_status)
else:
__handle_online_or_offline_state(state, site, new_status)
if __name__ == '__main__':
_state = {}
while True:
update_state(_state)
time.sleep(POLL_INTERVAL_SEC)
当站点状态从 Online 变为 offline 或 vice-versa 时,调用 on_event 的功能。
如果要使用这个脚本,您必须指定以下变量:
-
USERNAME和PASSWORD:Data Grid 用户的用户名和密码,有权访问 REST 端点。 -
POLL_INTERVAL_SEC:轮询之间的秒数。 -
SERVERS:此站点的数据网格服务器列表。该脚本只需要一个有效的响应,但提供了列表以允许故障切换。 -
REMOTE_SITES: 要监控这些服务器上的远程站点列表。 -
CACHES:要监控的缓存名称列表。
使用 Prometheus 指标监控跨站点复制
Prometheus 和其他监控系统可让您配置警报来检测站点状态何时更改为 离线。
监控跨站点延迟指标可帮助您发现潜在的问题。
先决条件
- 启用跨站点复制。
流程
- 配置数据网格指标。
使用 Prometheus 指标格式配置警报规则。
-
对于站点状态,
在线使用1,0代表离线。 对于
exprfiled,请使用以下格式:vendor_cache_manager_default_cache_<cache name>_x_site_admin_<site name>_status。在以下示例中,当 NYC 站点为名为
work或 session 的缓存离线时,Prometheus会发出警告。groups: - name: Cross Site Rules rules: - alert: Cache Work and Site NYC expr: vendor_cache_manager_default_cache_work_x_site_admin_nyc_status == 0 - alert: Cache Sessions and Site NYC expr: vendor_cache_manager_default_cache_sessions_x_site_admin_nyc_status == 0下图显示了 NYC 站点
离线进行缓存工作的警报。图 4.1. Prometheus Alert
-
对于站点状态,