- Notifications
You must be signed in to change notification settings - Fork35
Port numbers management is improved (#164)#165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
dmitry-lipetsk merged 6 commits intopostgrespro:masterfromdmitry-lipetsk:master-fix164--v01Dec 15, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
e4c2e07
reserve_port and release_port are "pointers" to functions for a port …
dmitry-lipetsk85d2aa3
TestgresTests::test_the_same_port is corrected
dmitry-lipetsk88371d1
OsOperations::read_binary(self, filename, start_pos) is added
dmitry-lipetsk4fe1894
OsOperations::get_file_size(self, filename) is added
dmitry-lipetsk28ac425
Port numbers management is improved (#164)
dmitry-lipetsk663612c
PostgresNode._C_MAX_START_ATEMPTS=5 is added (+ 1 new test)
dmitry-lipetskFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
99 changes: 82 additions & 17 deletionstestgres/node.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -83,13 +83,13 @@ | ||
from .standby import First | ||
from . import utils | ||
from .utils import \ | ||
PgVer, \ | ||
eprint, \ | ||
get_bin_path, \ | ||
get_pg_version, \ | ||
execute_utility, \ | ||
options_string, \ | ||
clean_on_error | ||
@@ -128,6 +128,9 @@ def __repr__(self): | ||
class PostgresNode(object): | ||
# a max number of node start attempts | ||
_C_MAX_START_ATEMPTS = 5 | ||
def __init__(self, name=None, base_dir=None, port=None, conn_params: ConnectionParams = ConnectionParams(), bin_dir=None, prefix=None): | ||
""" | ||
PostgresNode constructor. | ||
@@ -158,7 +161,7 @@ def __init__(self, name=None, base_dir=None, port=None, conn_params: ConnectionP | ||
self.os_ops = LocalOperations(conn_params) | ||
self.host = self.os_ops.host | ||
self.port = port orutils.reserve_port() | ||
self.ssh_key = self.os_ops.ssh_key | ||
@@ -471,6 +474,28 @@ def _collect_special_files(self): | ||
return result | ||
def _collect_log_files(self): | ||
# dictionary of log files + size in bytes | ||
files = [ | ||
self.pg_log_file | ||
] # yapf: disable | ||
result = {} | ||
for f in files: | ||
# skip missing files | ||
if not self.os_ops.path_exists(f): | ||
continue | ||
file_size = self.os_ops.get_file_size(f) | ||
assert type(file_size) == int # noqa: E721 | ||
assert file_size >= 0 | ||
result[f] = file_size | ||
return result | ||
def init(self, initdb_params=None, cached=True, **kwargs): | ||
""" | ||
Perform initdb for this node. | ||
@@ -722,6 +747,22 @@ def slow_start(self, replica=False, dbname='template1', username=None, max_attem | ||
OperationalError}, | ||
max_attempts=max_attempts) | ||
def _detect_port_conflict(self, log_files0, log_files1): | ||
assert type(log_files0) == dict # noqa: E721 | ||
assert type(log_files1) == dict # noqa: E721 | ||
for file in log_files1.keys(): | ||
read_pos = 0 | ||
if file in log_files0.keys(): | ||
read_pos = log_files0[file] # the previous size | ||
file_content = self.os_ops.read_binary(file, read_pos) | ||
file_content_s = file_content.decode() | ||
if 'Is another postmaster already running on port' in file_content_s: | ||
return True | ||
return False | ||
def start(self, params=[], wait=True): | ||
""" | ||
Starts the PostgreSQL node using pg_ctl if node has not been started. | ||
@@ -736,6 +777,9 @@ def start(self, params=[], wait=True): | ||
Returns: | ||
This instance of :class:`.PostgresNode`. | ||
""" | ||
assert __class__._C_MAX_START_ATEMPTS > 1 | ||
if self.is_started: | ||
return self | ||
@@ -745,27 +789,46 @@ def start(self, params=[], wait=True): | ||
"-w" if wait else '-W', # --wait or --no-wait | ||
"start"] + params # yapf: disable | ||
log_files0 = self._collect_log_files() | ||
assert type(log_files0) == dict # noqa: E721 | ||
nAttempt = 0 | ||
dmitry-lipetsk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
timeout = 1 | ||
while True: | ||
assert nAttempt >= 0 | ||
assert nAttempt < __class__._C_MAX_START_ATEMPTS | ||
nAttempt += 1 | ||
try: | ||
exit_status, out, error = execute_utility(_params, self.utils_log_file, verbose=True) | ||
if error and 'does not exist' in error: | ||
raise Exception | ||
except Exception as e: | ||
assert nAttempt > 0 | ||
assert nAttempt <= __class__._C_MAX_START_ATEMPTS | ||
if self._should_free_port and nAttempt < __class__._C_MAX_START_ATEMPTS: | ||
log_files1 = self._collect_log_files() | ||
if self._detect_port_conflict(log_files0, log_files1): | ||
log_files0 = log_files1 | ||
logging.warning( | ||
"Detected an issue with connecting to port {0}. " | ||
"Trying another port after a {1}-second sleep...".format(self.port, timeout) | ||
) | ||
time.sleep(timeout) | ||
timeout = min(2 * timeout, 5) | ||
cur_port = self.port | ||
new_port = utils.reserve_port() # can raise | ||
try: | ||
options = {'port': str(new_port)} | ||
self.set_auto_conf(options) | ||
except: # noqa: E722 | ||
utils.release_port(new_port) | ||
raise | ||
self.port = new_port | ||
utils.release_port(cur_port) | ||
continue | ||
msg = 'Cannot start node' | ||
files = self._collect_special_files() | ||
raise_from(StartNodeException(msg, files), e) | ||
break | ||
self._maybe_start_logger() | ||
@@ -930,8 +993,10 @@ def free_port(self): | ||
""" | ||
if self._should_free_port: | ||
port = self.port | ||
self._should_free_port = False | ||
self.port = None | ||
utils.release_port(port) | ||
def cleanup(self, max_attempts=3, full=False): | ||
""" | ||
16 changes: 16 additions & 0 deletionstestgres/operations/local_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletionstestgres/operations/os_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletionstestgres/operations/remote_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletionstestgres/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.