def start_kafka_cluster_heartbeat(self, instance_id): # For heartbeat we will send a message to a specific "voltha-heartbeat" # topic. The message is a protocol buf # message message = dict(type='heartbeat', adapter=self.args.name, instance=instance_id, ip=get_my_primary_local_ipv4()) topic = defs['heartbeat_topic'] def send_msg(start_time): try: kafka_cluster_proxy = get_kafka_proxy() if kafka_cluster_proxy and not kafka_cluster_proxy.is_faulty(): # self.log.debug('kafka-proxy-available') message['ts'] = arrow.utcnow().timestamp message['uptime'] = time.time() - start_time # self.log.debug('start-kafka-heartbeat') kafka_cluster_proxy.send_message(topic, dumps(message)) else: self.log.error('kafka-proxy-unavailable') except Exception as e: self.log.exception('failed-sending-message-heartbeat', e=e) try: t0 = time.time() lc = LoopingCall(send_msg, t0) lc.start(10) except Exception, e: self.log.exception('failed-kafka-heartbeat', e=e)
def start_kafka_cluster_heartbeat(self, instance_id): # For heartbeat we will send a message to a specific "voltha-heartbeat" # topic. The message is a protocol buf # message message = dict( type='heartbeat', adapter=self.args.name, instance=instance_id, ip=get_my_primary_local_ipv4() ) topic = defs['heartbeat_topic'] def send_heartbeat_msg(): try: kafka_cluster_proxy = get_kafka_proxy() if kafka_cluster_proxy: message['ts'] = arrow.utcnow().timestamp self.log.debug('sending-kafka-heartbeat-message') # Creating a handler to receive the message callbacks df = Deferred() df.addCallback(self.process_kafka_alive_state_update) kafka_cluster_proxy.register_alive_state_update(df) kafka_cluster_proxy.send_heartbeat_message(topic, dumps(message)) else: Probe.kafka_cluster_proxy_running = False self.log.error('kafka-proxy-unavailable') except Exception as e: self.log.exception('failed-sending-message-heartbeat', e=e) def check_heartbeat_delivery(): try: kafka_cluster_proxy = get_kafka_proxy() if kafka_cluster_proxy: kafka_cluster_proxy.check_heartbeat_delivery() except Exception as e: self.log.exception('failed-checking-heartbeat-delivery', e=e) def schedule_periodic_heartbeat(): try: # Sending the heartbeat message in a loop lc_heartbeat = LoopingCall(send_heartbeat_msg) lc_heartbeat.start(10) # Polling the delivery status more frequently to get early notification lc_poll = LoopingCall(check_heartbeat_delivery) lc_poll.start(2) except Exception as e: self.log.exception('failed-kafka-heartbeat-startup', e=e) from twisted.internet import reactor # Delaying heartbeat initially to let kafka connection be established reactor.callLater(5, schedule_periodic_heartbeat)
import os import yaml from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks from pyvoltha.common.structlog_setup import setup_logging from pyvoltha.common.utils.dockerhelpers import get_my_containers_name from pyvoltha.common.utils.nethelpers import get_my_primary_local_ipv4 from connection_mgr import ConnectionManager defs = dict(config=os.environ.get('CONFIG', './ofagent.yml'), consul=os.environ.get('CONSUL', 'localhost:8500'), controller=os.environ.get('CONTROLLER', 'localhost:6653'), external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()), grpc_endpoint=os.environ.get('GRPC_ENDPOINT', 'localhost:50055'), grpc_timeout=os.environ.get('GRPC_TIMEOUT', '10'), core_binding_key=os.environ.get('CORE_BINDING_KEY', 'voltha_backend_name'), core_transaction_key=os.environ.get('CORE_TRANSACTION_KEY', 'voltha_serial_number'), instance_id=os.environ.get('INSTANCE_ID', os.environ.get('HOSTNAME', '1')), internal_host_address=os.environ.get('INTERNAL_HOST_ADDRESS', get_my_primary_local_ipv4()), work_dir=os.environ.get('WORK_DIR', '/tmp/ofagent'), key_file=os.environ.get('KEY_FILE', '/ofagent/pki/voltha.key'), cert_file=os.environ.get('CERT_FILE', '/ofagent/pki/voltha.crt'))