Skip to content

[#247] PortManager__Generic uses lock-dirs for reserved ports #255

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
99e645e
[#247] PortManager__Generic uses lock-dirs for reserved ports
dmitry-lipetsk May 6, 2025
c6f4b4d
PortManager__Generic is refactored
dmitry-lipetsk May 7, 2025
f085b70
[#256] A used port range is [1024 ... 65535]
dmitry-lipetsk May 7, 2025
c9b4bbf
PortManager__Generic is refactored [consts, asserts]
dmitry-lipetsk May 7, 2025
862c79f
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk May 7, 2025
b5e6f25
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk May 7, 2025
be3cc11
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk May 12, 2025
cc8333c
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk May 12, 2025
d15ecdb
PortManager__Generic sends debug messages about its operations.
dmitry-lipetsk May 29, 2025
8e0869d
[attention] OsOperations::create_lock_fs_obj is added
dmitry-lipetsk May 31, 2025
82b46b3
Code style is fixed
dmitry-lipetsk Jun 1, 2025
a188a63
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk Jun 22, 2025
597f699
PortManager__Generic is synchronized with master
dmitry-lipetsk Jun 24, 2025
b34837e
[FIX] PortManager__Generic::release_port sends a debug message under …
dmitry-lipetsk Jun 24, 2025
0648926
PortManager__Generic::reserve_port is updated (reordered)
dmitry-lipetsk Jun 24, 2025
8c5e340
PortManager__Generic::helper__send_debug_msg is corrected (assert)
dmitry-lipetsk Jun 24, 2025
ea25215
PortManager__Generic::reserve_port is updated (comment)
dmitry-lipetsk Jun 24, 2025
aa677d2
PortManager__Generic::__init__ is updated
dmitry-lipetsk Jun 24, 2025
1dfac90
Merge remote-tracking branch 'pgpro/master' into master-fix247--v001
dmitry-lipetsk Jun 24, 2025
8aa790a
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk Jun 26, 2025
e63c603
Merge branch 'master' into master-fix247--v001
dmitry-lipetsk Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[#247] PortManager__Generic uses lock-dirs for reserved ports
  • Loading branch information
dmitry-lipetsk committed May 6, 2025
commit 99e645e8bcce52ac85d4dabdc6ae1e4bdceb4a17
4 changes: 4 additions & 0 deletions testgres/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
TMP_CACHE = 'tgsc_'
TMP_BACKUP = 'tgsb_'

TMP_TESTGRES = "testgres"

TMP_TESTGRES_PORTS = TMP_TESTGRES + "/ports"

# path to control file
XLOG_CONTROL_FILE = "global/pg_control"

Expand Down
44 changes: 42 additions & 2 deletions testgres/impl/port_manager__generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from ..port_manager import PortManager
from ..exceptions import PortForException
from .. import consts

import os
import threading
import random
import typing
Expand All @@ -15,6 +17,8 @@ class PortManager__Generic(PortManager):
_available_ports: typing.Set[int]
_reserved_ports: typing.Set[int]

_lock_dir: str

def __init__(self, os_ops: OsOperations):
assert os_ops is not None
assert isinstance(os_ops, OsOperations)
Expand All @@ -23,6 +27,12 @@ def __init__(self, os_ops: OsOperations):
self._available_ports: typing.Set[int] = set(range(1024, 65535))
self._reserved_ports: typing.Set[int] = set()

temp_dir = os_ops.get_tempdir()
assert type(temp_dir) == str # noqa: E721
self._lock_dir = os.path.join(temp_dir, consts.TMP_TESTGRES_PORTS)
assert type(self._lock_dir) == str # noqa: E721
os_ops.makedirs(self._lock_dir)

def reserve_port(self) -> int:
assert self._guard is not None
assert type(self._available_ports) == set # noqa: E721t
Expand All @@ -41,9 +51,23 @@ def reserve_port(self) -> int:
if not self._os_ops.is_port_free(port):
continue

self._reserved_ports.add(port)
self._available_ports.discard(port)
try:
lock_path = self.helper__make_lock_path(port)
self._os_ops.makedir(lock_path)
except: # noqa: 722
continue

assert self._os_ops.path_exists(lock_path)

try:
self._reserved_ports.add(port)
except: # noqa: 722
assert not (port in self._reserved_ports)
self._os_ops.rmdir(lock_path)
raise

assert port in self._reserved_ports
self._available_ports.discard(port)
assert not (port in self._available_ports)
return port

Expand All @@ -55,10 +79,26 @@ def release_port(self, number: int) -> None:
assert self._guard is not None
assert type(self._reserved_ports) == set # noqa: E721

lock_path = self.helper__make_lock_path(number)

with self._guard:
assert number in self._reserved_ports
assert not (number in self._available_ports)
self._available_ports.add(number)
self._reserved_ports.discard(number)
assert not (number in self._reserved_ports)
assert number in self._available_ports

assert isinstance(self._os_ops, OsOperations)
assert self._os_ops.path_exists(lock_path)
self._os_ops.rmdir(lock_path)

return

def helper__make_lock_path(self, port_number: int) -> str:
assert type(port_number) == int # noqa: E721
assert type(self._lock_dir) == str # noqa: E721

result = os.path.join(self._lock_dir, str(port_number) + ".lock")
assert type(result) == str # noqa: E721
return result