跟其余的agent线程一样
都在InitializerModule中初始化
在InitializerModule.py
中,初始化了众多属性后,进行线程的初始化
这些线程在agent端非常重要
def init_threads(self):
"""
Initialize thread objects
"""
self.component_status_executor = ComponentStatusExecutor(self)
self.action_queue = ActionQueue(self)
self.alert_scheduler_handler = AlertSchedulerHandler(self)
self.command_status_reporter = CommandStatusReporter(self)
self.host_status_reporter = HostStatusReporter(self)
self.alert_status_reporter = AlertStatusReporter(self)
self.heartbeat_thread = HeartbeatThread.HeartbeatThread(self)
其中HostStatusReporter(self)
就是ambari-agent
的主机状态汇报器了
本文也主要讲此线程
def run(self):
while not self.stop_event.is_set():
try:
logger.info('=====gaofeng=====HostStatusReporter===initializer_module.is_registered=%s', self.initializer_module.is_registered)
if self.initializer_module.is_registered:
#获取报告
report = self.get_report()
logger.info('=====gaofeng=====HostStatusReporter===get_report=%s', report)
logger.info('=====gaofeng=====HostStatusReporter===last_report=%s', self.last_report)
# 如果report跟last_report不等则执行if
if self.initializer_module.is_registered and not Utils.are_dicts_equal(report, self.last_report, keys_to_skip=["agentTimeStampAtReporting"]):
# HOST_STATUS_REPORTS_ENDPOINT=/reports/host_status
logger.info('=====gaofeng====report跟last_report不等且处于注册状态=====')
logger.info('=====gaofeng=====HostStatusReporter===get_report=%s', report)
logger.info('=====gaofeng=====HostStatusReporter===correlation_id=%s', correlation_id)
correlation_id = self.initializer_module.connection.send(message=report, destination=Constants.HOST_STATUS_REPORTS_ENDPOINT)
self.server_responses_listener.listener_functions_on_success[correlation_id] = lambda headers, message: self.save_last_report(report)
except ConnectionIsAlreadyClosed: # server and agent disconnected during sending data. Not an issue
pass
except:
logger.exception("Exception in HostStatusReporter. Re-running it")
self.stop_event.wait(self.report_interval)
logger.info("HostStatusReporter has successfully finished")
打印日志如下
其中给server上报的report内容结构如下
get_report
{
'agentEnv': {
'transparentHugePage': '',
'hostHealth': {
'agentTimeStampAtReporting': 1594716052379,
'liveServices': [{
'status': 'Healthy',
'name': 'chronyd',
'desc': ''
}]
},
'reverseLookup': True,
'alternatives': [],
'hasUnlimitedJcePolicy': True,
'umask': '18',
'firewallName': 'iptables',
'stackFoldersAndFiles': [],
'existingUsers': [],
'firewallRunning': False
},
'mounts': [{
'available': '2492292',
'used': '34312768',
'percent': '94%',
'device': '/dev/mapper/rhel-root',
'mountpoint': '/',
'type': 'xfs',
'size': '36805060'
},
{
'available': '483375908',
'used': '6323860',
'percent': '2%',
'device': '/dev/vdb',
'mountpoint': '/data',
'type': 'ext4',
'size': '515930552'
}]
}
如果与last_report
不一致,则汇报给server
correlation_id = self.initializer_module.connection.send(message=report, destination=Constants.HOST_STATUS_REPORTS_ENDPOINT)
self.server_responses_listener.listener_functions_on_success[correlation_id] = lambda headers, message: self.save_last_report(report)
HOST_STATUS_REPORTS_ENDPOINT=/reports/host_status
调用的是ambari-server端的/reports/host_status(依据注解去寻找)
correlation_id如下
INFO 2020-07-14 16:41:52,764 HostStatusReporter.py:58 - =====gaofeng=====HostStatusReporter===correlation_id=31
就是一个id,每次send()的时候会调用security.py.AmbariStompConnection()
def send(self, destination, message, content_type=None, headers=None, log_message_function=lambda x:x, presend_hook=None, **keyword_headers):
with self.lock:
self.correlation_id += 1
correlation_id = self.correlation_id
if presend_hook:
presend_hook(self.correlation_id)
logged_message = log_message_function(copy.deepcopy(message))
logger.info("Event to server at {0} (correlation_id={1}): {2}".format(destination, correlation_id, logged_message))
body = json.dumps(message)
WsConnection.send(self, destination, body, content_type=content_type, headers=headers, correlationId=correlation_id, **keyword_headers)
return correlation_id
每次一个send都会使correlation_id
+1,重启则重置