Skip to content

Commit

Permalink
Feature input parameters support kv (#386)
Browse files Browse the repository at this point in the history
* add unittest for ssh_client

* unittest for local_client

* add unittest for remote_client

* add unittest for docker_client and kubernetes_cilent

* update unittest for remote_client

* add unittest workflow

* update unittest for test_remote_client

* update unittest for workflow

* input_marameters supports input in kv format

* Delete temporary files

* Using opts to pass input_parameters options

* fix multiple = in options

* delete print()

* Change the warn prompt information to verbose
  • Loading branch information
xiaodong-ji authored Aug 12, 2024
1 parent 91c0b6b commit 9d8e7e2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
57 changes: 56 additions & 1 deletion diag_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import sys
import textwrap
import re
import json
from uuid import uuid1 as uuid, UUID
from optparse import OptionParser, BadOptionError, Option, IndentedHelpFormatter
from core import ObdiagHome
Expand Down Expand Up @@ -867,15 +868,69 @@ def __init__(self):
super(ObdiagRCARunCommand, self).__init__('run', 'root cause analysis')
self.parser.add_option('--scene', type='string', help="rca scene name. The argument is required.")
self.parser.add_option('--store_dir', type='string', help='the dir to store rca result, current dir by default.', default='./rca/')
self.parser.add_option('--input_parameters', type='string', help='input parameters of scene')
self.parser.add_option('--input_parameters', action='callback', type='string', callback=self._input_parameters_scene, help='input parameters of scene')
self.parser.add_option('-c', type='string', help='obdiag custom config', default=os.path.expanduser('~/.obdiag/config.yml'))
self.scene_input_param_map = {}

def _input_parameters_scene(self, option, opt_str, value, parser):
"""
input parameters of scene
"""
try:
# input_parameters option is json format
try:
self.scene_input_param_map = json.loads(value)
return
except Exception as e:
# raise Exception("Failed to parse input_parameters. Please check the option is json:{0}".format(value))
ROOT_IO.verbose("input_parameters option {0} is not json.".format(value))

# input_parameters option is key=val format
key, val = value.split('=', 1)
if key is None or key == "":
return
m = self._input_parameters_scene_set(key, val)

def _scene_input_param(param_map, scene_param_map):
for scene_param_map_key, scene_param_map_value in scene_param_map.items():
if scene_param_map_key in param_map:
if isinstance(scene_param_map_value, dict):
_scene_input_param(param_map[scene_param_map_key], scene_param_map_value)
else:
param_map[scene_param_map_key] = scene_param_map_value
else:
param_map[scene_param_map_key] = scene_param_map_value
return param_map

self.scene_input_param_map = _scene_input_param(self.scene_input_param_map, m)
except Exception as e:
raise Exception("Key or val ({1}) is illegal: {0}".format(e, value))

def _input_parameters_scene_set(self, key, val):
def recursion(param_map, key, val):
if key is None or key == "":
raise Exception("key is None")
if val is None or val == "":
raise Exception("val is None")
if key.startswith(".") or key.endswith("."):
raise Exception("Key starts or ends '.'")
if "." in key:
map_key = key.split(".")[0]
param_map[map_key] = recursion({}, key[len(map_key) + 1 :], val)
return param_map
else:
param_map[key] = val
return param_map

return recursion({}, key, val)

def init(self, cmd, args):
super(ObdiagRCARunCommand, self).init(cmd, args)
self.parser.set_usage('%s [options]' % self.prev_cmd)
return self

def _do_command(self, obdiag):
Util.set_option(self.opts, 'input_parameters', self.scene_input_param_map)
return obdiag.rca_run(self.opts)


Expand Down
11 changes: 1 addition & 10 deletions handler/rca/rca_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def __init__(self, context):
all_scenes_info, all_scenes_item = rca_list.get_all_scenes()
self.context.set_variable("rca_deep_limit", len(all_scenes_info))
self.all_scenes = all_scenes_item
self.rca_scene_parameters = None
self.rca_scene = None
self.cluster = self.context.get_variable("ob_cluster")
self.nodes = self.context.get_variable("observer_nodes")
Expand All @@ -122,15 +121,7 @@ def __init__(self, context):
# init input parameters
self.report = None
self.tasks = None
rca_scene_parameters = Util.get_option(self.options, "input_parameters", "")
if rca_scene_parameters != "":
try:
rca_scene_parameters = json.loads(rca_scene_parameters)
except Exception as e:
raise Exception("Failed to parse input_parameters. Please check the option is json:{0}".format(rca_scene_parameters))
else:
rca_scene_parameters = {}
self.context.set_variable("input_parameters", rca_scene_parameters)
self.context.set_variable("input_parameters", Util.get_option(self.options, "input_parameters"))
self.store_dir = Util.get_option(self.options, "store_dir", "./rca/")
self.context.set_variable("store_dir", self.store_dir)
self.stdio.verbose(
Expand Down
6 changes: 5 additions & 1 deletion handler/rca/scene/ddl_disk_full_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def execute(self):
## if the action is add_index
sql = "select table_id from oceanbase.__all_virtual_table_history where tenant_id = '{0}' and data_table_id = '{1}' and table_name like '%{2}%';".format(self.tenant_id, self.table_id, self.index_name)
self.verbose("execute_sql is {0}".format(sql))
self.index_table_id = self.ob_connector.execute_sql_return_cursor_dictionary(sql).fetchall()[0]["table_id"]
sql_tables_data = self.ob_connector.execute_sql_return_cursor_dictionary(sql).fetchall()
if len(sql_tables_data) == 0:
self.stdio.error("can not find index table id by index name: {0}. Please check the index name.".format(self.index_name))
return
self.index_table_id = sql_tables_data[0]["table_id"]
self.verbose("index_table_id is {0}".format(self.index_table_id))
self.record.add_record("index_table_id is {0}".format(self.index_table_id))

Expand Down

0 comments on commit 9d8e7e2

Please sign in to comment.