Skip to content

Commit

Permalink
fix: gather tabledump (#328)
Browse files Browse the repository at this point in the history
* table dump print pretty result

* Fix regression testing bugs

* Fix regression testing bugs

* Optimize logs

* Optimize logs

* Optimize logs

* fix: gather tabledump

* fix: gather tabledump
  • Loading branch information
Teingi authored Jul 15, 2024
1 parent 5950030 commit b3fe9d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion diag_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def __init__(self):
self.parser.add_option('--table', type='string', help="Specifies the name of the table in the database to operate on.")
self.parser.add_option('--user', type='string', help="The username to use for the database connection.")
self.parser.add_option('--password', type='string', help="The password for the database user. If not specified, an attempt will be made to connect without a password.", default='')
self.parser.add_option('--store_dir', type='string', help='the dir to store gather result, current dir by default.', default='./gather_report')
self.parser.add_option('--store_dir', type='string', help='the dir to store gather result, current dir by default.', default='./obdiag_gather_report')
self.parser.add_option('-c', type='string', help='obdiag custom config', default=os.path.expanduser('~/.obdiag/config.yml'))

def init(self, cmd, args):
Expand Down
27 changes: 14 additions & 13 deletions handler/gather/gather_tabledump.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ def __init__(self, context, store_dir="./obdiag_gather_report", is_inner=False):
self.result_list = []
self.store_dir = store_dir
self.is_innner = is_inner
try:
if not os.path.exists(store_dir):
os.makedirs(store_dir)
except Exception as e:
self.stdio.error("init gather_report {0}".format(e))
raise Exception("int gather_report {0}".format(e))
if self.context.get_variable("gather_timestamp", None):
self.gather_timestamp = self.context.get_variable("gather_timestamp")
else:
self.gather_timestamp = TimeUtils.get_current_us_timestamp()

def init_config(self):
def init(self):
try:
self.ob_cluster = self.context.cluster_config
self.obproxy_nodes = self.context.obproxy_config['servers']
Expand All @@ -68,7 +62,15 @@ def init_config(self):
self.table = Util.get_option(options, 'table')
user = Util.get_option(options, 'user')
password = Util.get_option(options, 'password')
self.store_dir = Util.get_option(options, 'store_dir')
if not (self.database and self.database and user and password):
self.stdio.error("option --database/--table/--user/--password not found, please provide")
return False
store_dir_option = Util.get_option(options, 'store_dir')
if store_dir_option is not None and store_dir_option != './':
if not os.path.exists(os.path.abspath(store_dir_option)):
self.stdio.warn('args --store_dir [{0}]: No such directory, Now create it'.format(os.path.abspath(store_dir_option)))
os.makedirs(os.path.abspath(store_dir_option))
self.store_dir = os.path.abspath(store_dir_option)
if self.context.get_variable("gather_database", None):
self.database = self.context.get_variable("gather_database")
if self.context.get_variable("gather_table", None):
Expand All @@ -87,16 +89,16 @@ def init_config(self):
ip=self.ob_cluster.get("db_host"), port=self.ob_cluster.get("db_port"), username=self.ob_cluster.get("tenant_sys").get("user"), password=self.ob_cluster.get("tenant_sys").get("password"), stdio=self.stdio, timeout=100
)
self.tenant_connector = OBConnector(ip=self.ob_cluster.get("db_host"), port=self.ob_cluster.get("db_port"), username=user, password=password, stdio=self.stdio, timeout=100)
self.file_name = "{0}/obdiag_tabledump_result_{1}.txt".format(self.store_dir, self.gather_timestamp)
self.file_name = "{0}/obdiag_tabledump_result_{1}.txt".format(self.store_dir, TimeUtils.timestamp_to_filename_time(self.gather_timestamp))
return True
except Exception as e:
self.stdio.error(e)
return False

def handle(self):
self.start_time = time.time()
if not self.init_config():
self.stdio.error('init config failed')
if not self.init():
self.stdio.error('init failed')
return False
self.execute()
if not self.is_innner:
Expand All @@ -111,8 +113,7 @@ def execute(self):
else:
self.__get_table_info_v3()
except Exception as e:
self.stdio.error("report sql result to file: {0} failed, error: ".format(self.file_name))
self.stdio.error("GatherTableDumpHandler execute Exception: {0}".format(e).strip())
self.stdio.error("report sql result to file: {0} failed, error: {1}".format(self.file_name, e))

def __get_table_schema(self):
sql = "show create table " + self.database + "." + self.table
Expand Down

0 comments on commit b3fe9d9

Please sign in to comment.