From 341b5bb2347c30344662675936b90b325efe5520 Mon Sep 17 00:00:00 2001 From: Vijay Venkatesh Kumar Date: Thu, 17 Nov 2022 14:51:38 -0500 Subject: Heartbeat code refactoring code optimization & test improvement Issue-ID: DCAEGEN2-2953 Signed-off-by: Vijay Venkatesh Kumar Change-Id: I99229d966c13ad666ac994ab5a582aeeaa306639 Signed-off-by: Vijay Venkatesh Kumar --- miss_htbt_service/misshtbtd.py | 83 ++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 51 deletions(-) (limited to 'miss_htbt_service/misshtbtd.py') diff --git a/miss_htbt_service/misshtbtd.py b/miss_htbt_service/misshtbtd.py index fd1a09c..23d4835 100644 --- a/miss_htbt_service/misshtbtd.py +++ b/miss_htbt_service/misshtbtd.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # ============LICENSE_START======================================================= -# Copyright (c) 2017-2022 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2017-2023 AT&T Intellectual Property. All rights reserved. # Copyright (c) 2019 Pantheon.tech. All rights reserved. # Copyright (c) 2020 Deutsche Telekom. All rights reserved. # Copyright (c) 2021 Samsung Electronics. All rights reserved. @@ -40,14 +40,15 @@ import yaml import socket import os.path as path import tempfile +import psycopg2 from pathlib import Path import check_health import htbtworker as heartbeat import get_logger import cbs_polling -from mod import trapd_settings as tds -from mod.trapd_get_cbs_config import get_cbs_config +from mod import htbt_settings as tds +from mod.htbt_get_cbs_config import get_cbs_config hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.yaml")) _logger = logging.getLogger(__name__) @@ -83,14 +84,7 @@ def create_database(update_db, jsfile, ip_address, port_num, user_name, password def read_hb_common(user_name, password, ip_address, port_num, db_name): - env_pytest = os.getenv("pytest", "") - if env_pytest == "test": - hbc_pid = 10 - hbc_src_name = "srvc_name" - hbc_time = 1584595881 - hbc_state = "RUNNING" - return hbc_pid, hbc_state, hbc_src_name, hbc_time - connection_db = heartbeat.postgres_db_open(user_name, password, ip_address, port_num, db_name) + connection_db = heartbeat.postgres_db_open() cur = connection_db.cursor() cur.execute("SELECT process_id, source_name, last_accessed_time, current_state FROM hb_common") rows = cur.fetchall() @@ -98,7 +92,6 @@ def read_hb_common(user_name, password, ip_address, port_num, db_name): hbc_src_name = rows[0][1] hbc_time = rows[0][2] hbc_state = rows[0][3] - heartbeat.commit_and_close_db(connection_db) cur.close() return hbc_pid, hbc_state, hbc_src_name, hbc_time @@ -109,9 +102,9 @@ def create_update_hb_common(update_flg, process_id, state, user_name, password, source_name = source_name + "-" + os.getenv("SERVICE_NAME", "") env_pytest = os.getenv("pytest", "") if env_pytest != "test": - connection_db = heartbeat.postgres_db_open(user_name, password, ip_address, port_num, db_name) + connection_db = heartbeat.postgres_db_open() cur = connection_db.cursor() - if heartbeat.db_table_creation_check(connection_db, "hb_common") is False: + if db_table_creation_check(connection_db, "hb_common") is False: cur.execute( """ CREATE TABLE hb_common ( @@ -133,18 +126,33 @@ def create_update_hb_common(update_flg, process_id, state, user_name, password, heartbeat.commit_and_close_db(connection_db) cur.close() - +def db_table_creation_check(connection_db, table_name): + cur = connection_db.cursor() + try: + cur.execute("SELECT * FROM information_schema.tables WHERE table_name = %s", (table_name,)) + database_names = cur.fetchone() + if database_names is not None: + if table_name in database_names: + return True + else: + return False + except psycopg2.DatabaseError as e: + msg = "COMMON:Error %s" % e + _logger.error(msg) + finally: + cur.close() + def create_update_vnf_table_1(jsfile, update_db, connection_db): with open(jsfile, "r") as outfile: cfg = json.load(outfile) hbcfg = cfg["heartbeat_config"] jhbcfg = json.loads(hbcfg) + cur = connection_db.cursor() env_pytest = os.getenv("pytest", "") if env_pytest == "test": vnf_list = ["Heartbeat_vDNS", "Heartbeat_vFW", "Heartbeat_xx"] else: - cur = connection_db.cursor() - if heartbeat.db_table_creation_check(connection_db, "vnf_table_1") is False: + if db_table_creation_check(connection_db, "vnf_table_1") is False: cur.execute( """ CREATE TABLE vnf_table_1 ( @@ -232,14 +240,14 @@ def create_update_vnf_table_1(jsfile, update_db, connection_db): def hb_worker_process(config_file_path): subprocess.call([ABSOLUTE_PATH1, config_file_path]) sys.stdout.flush() - _logger.info("MSHBT:Creaated Heartbeat worker process") + _logger.info("MSHBT:Created Heartbeat worker process") return def db_monitoring_process(current_pid, jsfile): subprocess.call([ABSOLUTE_PATH2, str(current_pid), jsfile]) sys.stdout.flush() - _logger.info("MSHBT:Creaated DB Monitoring process") + _logger.info("MSHBT:Created DB Monitoring process") return @@ -335,10 +343,10 @@ def create_update_db(update_db, jsfile, ip_address, port_num, user_name, passwor create_database(update_db, jsfile, ip_address, port_num, user_name, password, db_name) msg = "MSHBT: DB parameters -", ip_address, port_num, user_name, password, db_name _logger.info(msg) - connection_db = heartbeat.postgres_db_open(user_name, password, ip_address, port_num, db_name) + connection_db = heartbeat.postgres_db_open() cur = connection_db.cursor() if update_db == 0: - if heartbeat.db_table_creation_check(connection_db, "vnf_table_1") is False: + if db_table_creation_check(connection_db, "vnf_table_1") is False: create_update_vnf_table_1(jsfile, update_db, connection_db) else: create_update_vnf_table_1(jsfile, update_db, connection_db) @@ -367,6 +375,7 @@ def create_process(job_list, jsfile, pid_current): return job_list + def main(): get_logger.configure_logger("misshtbtd") pid_current = os.getpid() @@ -412,7 +421,7 @@ def main(): _logger.info("MSHBD:Current process id is %d", pid_current) _logger.info("MSHBD:Now be in a continuous loop") i = 0 - while True: + while True: # pragma: no cover hbc_pid, hbc_state, hbc_src_name, hbc_time = read_hb_common( user_name, password, ip_address, port_num, db_name ) @@ -432,16 +441,6 @@ def main(): _logger.info(msg) source_name = socket.gethostname() source_name = source_name + "-" + str(os.getenv("SERVICE_NAME", "")) - env_pytest = os.getenv("pytest", "") - if env_pytest == "test": - if i == 2: - hbc_pid = pid_current - source_name = hbc_src_name - hbc_state = "RECONFIGURATION" - elif i > 3: - hbc_pid = pid_current - source_name = hbc_src_name - hbc_state = "RUNNING" if time_difference < 60: if (int(hbc_pid) == int(pid_current)) and (source_name == hbc_src_name): msg = "MSHBD:config status is", hbc_state @@ -491,7 +490,7 @@ def main(): _logger.info("MSHBD:HB and DBM thread are waiting to become ACTIVE") else: jsfile = fetch_json_file() - msg = "MSHBD: Creating HB and DBM threads. The param pssed %d and %s", jsfile, pid_current + msg = "MSHBD: Creating HB and DBM threads. The param passed %d and %s", jsfile, pid_current _logger.info(msg) job_list = create_process(job_list, jsfile, pid_current) hbc_pid, hbc_state, hbc_src_name, hbc_time = read_hb_common( @@ -504,24 +503,6 @@ def main(): else: _logger.error("MSHBD:ERROR - Active instance is not updating hb_common in 60 sec - ERROR") time.sleep(25) - if os.getenv("pytest", "") == "test": - i = i + 1 - if i > 5: - _logger.info("Terminating main process for pytest") - cbs_polling_proc.terminate() - time.sleep(1) - cbs_polling_proc.join() - if len(job_list) > 0: - job_list[0].terminate() - time.sleep(1) - job_list[0].join() - job_list.remove(job_list[0]) - if len(job_list) > 0: - job_list[0].terminate() - time.sleep(1) - job_list[0].join() - job_list.remove(job_list[0]) - break except Exception as e: msg = "MSHBD:Exception as %s" % (str(traceback.format_exc())) -- cgit 1.2.3-korg