- Notifications
You must be signed in to change notification settings - Fork35
[Refactoring] Default port manager functions now use PortManager__Generic and LocalOperations#251
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 2 commits intopostgrespro:masterfromdmitry-lipetsk:D20250504_004--global_port_manager_is_refactoredMay 5, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File 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
2 changes: 1 addition & 1 deletionsetup.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
64 changes: 64 additions & 0 deletionstestgres/impl/port_manager__generic.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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from ..operations.os_ops import OsOperations | ||
from ..port_manager import PortManager | ||
from ..exceptions import PortForException | ||
import threading | ||
import random | ||
import typing | ||
class PortManager__Generic(PortManager): | ||
_os_ops: OsOperations | ||
_guard: object | ||
# TODO: is there better to use bitmap fot _available_ports? | ||
_available_ports: typing.Set[int] | ||
_reserved_ports: typing.Set[int] | ||
def __init__(self, os_ops: OsOperations): | ||
assert os_ops is not None | ||
assert isinstance(os_ops, OsOperations) | ||
self._os_ops = os_ops | ||
self._guard = threading.Lock() | ||
self._available_ports: typing.Set[int] = set(range(1024, 65535)) | ||
self._reserved_ports: typing.Set[int] = set() | ||
def reserve_port(self) -> int: | ||
assert self._guard is not None | ||
assert type(self._available_ports) == set # noqa: E721t | ||
assert type(self._reserved_ports) == set # noqa: E721 | ||
with self._guard: | ||
t = tuple(self._available_ports) | ||
assert len(t) == len(self._available_ports) | ||
sampled_ports = random.sample(t, min(len(t), 100)) | ||
t = None | ||
for port in sampled_ports: | ||
assert not (port in self._reserved_ports) | ||
assert port in self._available_ports | ||
if not self._os_ops.is_port_free(port): | ||
continue | ||
self._reserved_ports.add(port) | ||
self._available_ports.discard(port) | ||
assert port in self._reserved_ports | ||
assert not (port in self._available_ports) | ||
return port | ||
raise PortForException("Can't select a port.") | ||
def release_port(self, number: int) -> None: | ||
assert type(number) == int # noqa: E721 | ||
assert self._guard is not None | ||
assert type(self._reserved_ports) == set # noqa: E721 | ||
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 |
33 changes: 33 additions & 0 deletionstestgres/impl/port_manager__this_host.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from ..port_manager import PortManager | ||
from .. import utils | ||
import threading | ||
class PortManager__ThisHost(PortManager): | ||
sm_single_instance: PortManager = None | ||
sm_single_instance_guard = threading.Lock() | ||
@staticmethod | ||
def get_single_instance() -> PortManager: | ||
assert __class__ == PortManager__ThisHost | ||
assert __class__.sm_single_instance_guard is not None | ||
if __class__.sm_single_instance is not None: | ||
assert type(__class__.sm_single_instance) == __class__ # noqa: E721 | ||
return __class__.sm_single_instance | ||
with __class__.sm_single_instance_guard: | ||
if __class__.sm_single_instance is None: | ||
__class__.sm_single_instance = __class__() | ||
assert __class__.sm_single_instance is not None | ||
assert type(__class__.sm_single_instance) == __class__ # noqa: E721 | ||
return __class__.sm_single_instance | ||
def reserve_port(self) -> int: | ||
return utils.reserve_port() | ||
def release_port(self, number: int) -> None: | ||
assert type(number) == int # noqa: E721 | ||
return utils.release_port(number) |
6 changes: 3 additions & 3 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
93 changes: 0 additions & 93 deletionstestgres/port_manager.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
42 changes: 13 additions & 29 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
2 changes: 1 addition & 1 deletiontests/helpers/global_data.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
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.