Skip to content

Commit 00fd925

Browse files
Node.start is refactored [Victoria Shepard' ideas are used, #149]
- Save an orignal text of 'does not exist' error - When we reach maximum retry attempt of restarts - We log an error message - We raise exception "Cannot start node after multiple attempts" - A new port number is not tranlating into string - Reorganization TestgresTests.test_port_conflict is updated.
1 parent 93d1122 commit 00fd925

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

testgres/node.py

+62-38
Original file line numberDiff line numberDiff line change
@@ -790,48 +790,72 @@ def start(self, params=[], wait=True):
790790
"-w" if wait else '-W', # --wait or --no-wait
791791
"start"] + params # yapf: disable
792792

793-
log_files0 = self._collect_log_files()
794-
assert type(log_files0) == dict # noqa: E721
793+
def LOCAL__start_node():
794+
_, _, error = execute_utility(_params, self.utils_log_file, verbose=True)
795+
assert type(error) == str # noqa: E721
796+
if error and 'does not exist' in error:
797+
raise Exception(error)
795798

796-
nAttempt = 0
797-
timeout = 1
798-
while True:
799-
assert nAttempt >= 0
800-
assert nAttempt < __class__._C_MAX_START_ATEMPTS
801-
nAttempt += 1
799+
def LOCAL__raise_cannot_start_node(from_exception, msg):
800+
assert isinstance(from_exception, Exception)
801+
assert type(msg) == str # noqa: E721
802+
files = self._collect_special_files()
803+
raise_from(StartNodeException(msg, files), from_exception)
804+
805+
def LOCAL__raise_cannot_start_node__std(from_exception):
806+
assert isinstance(from_exception, Exception)
807+
LOCAL__raise_cannot_start_node(from_exception, 'Cannot start node')
808+
809+
if not self._should_free_port:
802810
try:
803-
exit_status, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
804-
if error and 'does not exist' in error:
805-
raise Exception
811+
LOCAL__start_node()
806812
except Exception as e:
807-
assert nAttempt > 0
808-
assert nAttempt <= __class__._C_MAX_START_ATEMPTS
809-
if self._should_free_port and nAttempt < __class__._C_MAX_START_ATEMPTS:
813+
LOCAL__raise_cannot_start_node__std(e)
814+
else:
815+
assert self._should_free_port
816+
assert __class__._C_MAX_START_ATEMPTS > 1
817+
818+
log_files0 = self._collect_log_files()
819+
assert type(log_files0) == dict # noqa: E721
820+
821+
nAttempt = 0
822+
timeout = 1
823+
while True:
824+
assert nAttempt >= 0
825+
assert nAttempt < __class__._C_MAX_START_ATEMPTS
826+
nAttempt += 1
827+
try:
828+
LOCAL__start_node()
829+
except Exception as e:
830+
assert nAttempt > 0
831+
assert nAttempt <= __class__._C_MAX_START_ATEMPTS
832+
if nAttempt == __class__._C_MAX_START_ATEMPTS:
833+
logging.error("Reached maximum retry attempts. Unable to start node.")
834+
LOCAL__raise_cannot_start_node(e, "Cannot start node after multiple attempts")
835+
810836
log_files1 = self._collect_log_files()
811-
if self._detect_port_conflict(log_files0, log_files1):
812-
log_files0 = log_files1
813-
logging.warning(
814-
"Detected an issue with connecting to port {0}. "
815-
"Trying another port after a {1}-second sleep...".format(self.port, timeout)
816-
)
817-
time.sleep(timeout)
818-
timeout = min(2 * timeout, 5)
819-
cur_port = self.port
820-
new_port = utils.reserve_port() # can raise
821-
try:
822-
options = {'port': str(new_port)}
823-
self.set_auto_conf(options)
824-
except: # noqa: E722
825-
utils.release_port(new_port)
826-
raise
827-
self.port = new_port
828-
utils.release_port(cur_port)
829-
continue
830-
831-
msg = 'Cannot start node'
832-
files = self._collect_special_files()
833-
raise_from(StartNodeException(msg, files), e)
834-
break
837+
if not self._detect_port_conflict(log_files0, log_files1):
838+
LOCAL__raise_cannot_start_node__std(e)
839+
840+
log_files0 = log_files1
841+
logging.warning(
842+
"Detected a conflict with using the port {0}. "
843+
"Trying another port after a {1}-second sleep...".format(self.port, timeout)
844+
)
845+
time.sleep(timeout)
846+
timeout = min(2 * timeout, 5)
847+
cur_port = self.port
848+
new_port = utils.reserve_port() # can raise
849+
try:
850+
options = {'port': str(new_port)}
851+
self.set_auto_conf(options)
852+
except: # noqa: E722
853+
utils.release_port(new_port)
854+
raise
855+
self.port = new_port
856+
utils.release_port(cur_port)
857+
continue
858+
break
835859
self._maybe_start_logger()
836860
self.is_started = True
837861
return self

tests/test_simple.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ def test_port_conflict(self):
12191219
with self.assertRaises(StartNodeException) as ctx:
12201220
node2.init().start()
12211221

1222-
self.assertIn("Cannot start node", str(ctx.exception))
1222+
self.assertIn("Cannot start node after multiple attempts", str(ctx.exception))
12231223

12241224
self.assertEqual(node2.port, node1.port)
12251225
self.assertTrue(node2._should_free_port)

0 commit comments

Comments
 (0)