Skip to content

Commit

Permalink
ccmlib/node.py: extract logic of running nodetool into separate method
Browse files Browse the repository at this point in the history
Separate parameterizing nodetool and actually invoking it. The
parameterizing is about to diverege for node.py and scylla_node.py, so
this separation allows us to reuse just the invoke logic, while letting
the two go on its separate ways when it comes to parameterizing.
  • Loading branch information
denesb authored and fruch committed Apr 2, 2024
1 parent e02cfe1 commit 25d34c9
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions ccmlib/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,26 @@ def wait_for_compactions(self,
time.sleep(1)
raise TimeoutError(f"Waiting for compactions timed out after {idle_timeout} seconds with pending tasks remaining: {output}.")

def _do_run_nodetool(self, nodetool, capture_output=True, wait=True, timeout=None, verbose=True):
if capture_output and not wait:
raise common.ArgumentError("Cannot set capture_output while wait is False.")
env = self.get_env()
if verbose:
self.debug(f"nodetool cmd={nodetool} wait={wait} timeout={timeout}")
if capture_output:
p = subprocess.Popen(nodetool, universal_newlines=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate(timeout=timeout)
else:
p = subprocess.Popen(nodetool, env=env, universal_newlines=True)
stdout, stderr = None, None

if wait:
exit_status = p.wait(timeout=timeout)
if exit_status != 0:
raise NodetoolError(" ".join(nodetool), exit_status, stdout, stderr)

return stdout, stderr

def nodetool(self, cmd, capture_output=True, wait=True, timeout=None, verbose=True):
"""
Setting wait=False makes it impossible to detect errors,
Expand All @@ -816,9 +836,6 @@ def nodetool(self, cmd, capture_output=True, wait=True, timeout=None, verbose=Tr
When wait=True, timeout may be set to a number, in seconds,
to limit how long the function will wait for nodetool to complete.
"""
if capture_output and not wait:
raise common.ArgumentError("Cannot set capture_output while wait is False.")
env = self.get_env()
if self.is_scylla() and not self.is_docker():
host = self.address()
else:
Expand All @@ -830,21 +847,8 @@ def nodetool(self, cmd, capture_output=True, wait=True, timeout=None, verbose=Tr
# see https://www.oracle.com/java/technologies/javase/8u331-relnotes.html#JDK-8278972
nodetool.extend(['-h', host, '-p', str(self.jmx_port), '-Dcom.sun.jndi.rmiURLParsing=legacy'])
nodetool.extend(cmd.split())
if verbose:
self.debug(f"nodetool cmd={cmd} wait={wait} timeout={timeout}")
if capture_output:
p = subprocess.Popen(nodetool, universal_newlines=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate(timeout=timeout)
else:
p = subprocess.Popen(nodetool, env=env, universal_newlines=True)
stdout, stderr = None, None

if wait:
exit_status = p.wait(timeout=timeout)
if exit_status != 0:
raise NodetoolError(" ".join(nodetool), exit_status, stdout, stderr)

return stdout, stderr
return self._do_run_nodetool(nodetool, capture_output, wait, timeout, verbose)

def dsetool(self, cmd):
raise common.ArgumentError('Cassandra nodes do not support dsetool')
Expand Down

0 comments on commit 25d34c9

Please sign in to comment.