Skip to content

Commit

Permalink
fix: updating configuration information through the connection string…
Browse files Browse the repository at this point in the history
… of the sys tenant (#586)
  • Loading branch information
Teingi authored Nov 28, 2024
1 parent e37c510 commit 545cac3
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,42 +160,46 @@ def set_offline_context(self, handler_name, namespace):

def update_obcluster_nodes(self, config):
config_data = config.config_data
cluster_config = config.config_data["obcluster"]
cluster_config = config.config_data.get("obcluster", {})

lst = Util.get_option(self.options, 'config')
if lst:
if any(item.startswith('obcluster.servers.nodes') for item in lst):
return
else:
self.stdio.verbose("You have already provided node information, so there is no need to query node information from the sys tenant")
ob_cluster = {"db_host": cluster_config["db_host"], "db_port": cluster_config["db_port"], "tenant_sys": {"user": cluster_config["tenant_sys"]["user"], "password": cluster_config["tenant_sys"]["password"]}}
if config_data['obcluster'] and config_data['obcluster']['servers'] and config_data['obcluster']['servers']['nodes']:
if lst and any(item.startswith('obcluster.servers.nodes') for item in lst):
self.stdio.verbose("You have already provided node information, so there is no need to query node information from the sys tenant")
return
if Util.check_none_values(ob_cluster, self.stdio):
ob_version = get_observer_version_by_sql(ob_cluster, self.stdio)
obConnetcor = OBConnector(ip=ob_cluster["db_host"], port=ob_cluster["db_port"], username=ob_cluster["tenant_sys"]["user"], password=ob_cluster["tenant_sys"]["password"], stdio=self.stdio, timeout=100)

ob_cluster = {"db_host": cluster_config.get("db_host"), "db_port": cluster_config.get("db_port"), "tenant_sys": {"user": cluster_config.get("tenant_sys", {}).get("user"), "password": cluster_config.get("tenant_sys", {}).get("password")}}

if not all(ob_cluster.values()) or not all(ob_cluster['tenant_sys'].values()):
raise ValueError("Missing required configuration values in ob_cluster or tenant_sys")

if (obcluster := config_data.get('obcluster')) and (servers := obcluster.get('servers')) and servers.get('nodes'):
return

ob_version = get_observer_version_by_sql(ob_cluster, self.stdio)
obConnetcor = OBConnector(ip=ob_cluster["db_host"], port=ob_cluster["db_port"], username=ob_cluster["tenant_sys"]["user"], password=ob_cluster["tenant_sys"]["password"], stdio=self.stdio)

sql = "select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.__all_server"
if ob_version.startswith(("1", "2", "3")):
sql = "select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.DBA_OB_SERVERS"
if ob_version.startswith("3") or ob_version.startswith("2") or ob_version.startswith("1"):
sql = "select SVR_IP, SVR_PORT, ZONE, BUILD_VERSION from oceanbase.__all_server"

try:
res = obConnetcor.execute_sql(sql)
if len(res) == 0:
raise Exception("Failed to get the node from sql [{0}], " "please check whether the --config option correct!!!".format(sql))
host_info_list = []
for row in res:
host_info = OrderedDict()
host_info["ip"] = row[0]
self.stdio.verbose("get host info: %s", host_info)
host_info_list.append(host_info)
if not res:
raise Exception(f"Failed to get the node from SQL [{sql}], please check whether the --config option is correct!!!")

host_info_list = [{"ip": row[0]} for row in res]
self.stdio.verbose("get host info: %s", host_info_list)

config_data_new = copy(config_data)
if 'servers' in config_data_new['obcluster']:
if not isinstance(config_data_new['obcluster']['servers'], dict):
config_data_new['obcluster']['servers'] = {}
if 'nodes' not in config_data_new['obcluster']['servers'] or not isinstance(config_data_new['obcluster']['servers']['nodes'], list):
config_data_new['obcluster']['servers']['nodes'] = []
config_data_new.setdefault('obcluster', {}).setdefault('servers', {}).setdefault('nodes', [])

for item in host_info_list:
ip = item['ip']
config_data_new['obcluster']['servers']['nodes'].append({'ip': ip})
self.stdio.verbose("update nodes [{0}]] config: %s", host_info)
config_data_new['obcluster']['servers']['nodes'].append({'ip': item['ip']})

self.stdio.verbose("update nodes config: %s", config_data_new['obcluster']['servers']['nodes'])
config.update_config_data(config_data_new)
except Exception as e:
self.stdio.error(f"An error occurred: {e}")

def get_namespace(self, spacename):
if spacename in self.namespaces:
Expand Down

0 comments on commit 545cac3

Please sign in to comment.