|
83 | 83 | BackupException, \
|
84 | 84 | InvalidOperationException
|
85 | 85 |
|
| 86 | +from .port_manager import PostgresNodePortManager |
| 87 | +from .port_manager import PostgresNodePortManager__ThisHost |
| 88 | +from .port_manager import PostgresNodePortManager__Generic |
| 89 | + |
86 | 90 | from .logger import TestgresLogger
|
87 | 91 |
|
88 | 92 | from .pubsub import Publication, Subscription
|
89 | 93 |
|
90 | 94 | from .standby import First
|
91 | 95 |
|
92 |
| -from . import utils |
93 |
| - |
94 | 96 | from .utils import \
|
95 | 97 | PgVer, \
|
96 | 98 | eprint, \
|
|
100 | 102 | options_string, \
|
101 | 103 | clean_on_error
|
102 | 104 |
|
103 |
| -from .helpers.port_manager import PortForException |
104 |
| - |
105 | 105 | from .backup import NodeBackup
|
106 | 106 |
|
107 | 107 | from .operations.os_ops import ConnectionParams
|
@@ -131,93 +131,10 @@ def __getattr__(self, name):
|
131 | 131 | return getattr(self.process, name)
|
132 | 132 |
|
133 | 133 | def __repr__(self):
|
134 |
| - return '{}(ptype={}, process={})'.format(self.__class__.__name__, |
135 |
| - str(self.ptype), |
136 |
| - repr(self.process)) |
137 |
| - |
138 |
| - |
139 |
| -class PostgresNodePortManager: |
140 |
| - def __init__(self): |
141 |
| - super().__init__() |
142 |
| - |
143 |
| - def reserve_port(self) -> int: |
144 |
| - raise NotImplementedError("PostgresNodePortManager::reserve_port is not implemented.") |
145 |
| - |
146 |
| - def release_port(self, number: int) -> None: |
147 |
| - assert type(number) == int # noqa: E721 |
148 |
| - raise NotImplementedError("PostgresNodePortManager::release_port is not implemented.") |
149 |
| - |
150 |
| - |
151 |
| -class PostgresNodePortManager__ThisHost(PostgresNodePortManager): |
152 |
| - sm_single_instance: PostgresNodePortManager = None |
153 |
| - sm_single_instance_guard = threading.Lock() |
154 |
| - |
155 |
| - def __init__(self): |
156 |
| - pass |
157 |
| - |
158 |
| - def __new__(cls) -> PostgresNodePortManager: |
159 |
| - assert __class__ == PostgresNodePortManager__ThisHost |
160 |
| - assert __class__.sm_single_instance_guard is not None |
161 |
| - |
162 |
| - if __class__.sm_single_instance is None: |
163 |
| - with __class__.sm_single_instance_guard: |
164 |
| - __class__.sm_single_instance = super().__new__(cls) |
165 |
| - assert __class__.sm_single_instance |
166 |
| - assert type(__class__.sm_single_instance) == __class__ # noqa: E721 |
167 |
| - return __class__.sm_single_instance |
168 |
| - |
169 |
| - def reserve_port(self) -> int: |
170 |
| - return utils.reserve_port() |
171 |
| - |
172 |
| - def release_port(self, number: int) -> None: |
173 |
| - assert type(number) == int # noqa: E721 |
174 |
| - return utils.release_port(number) |
175 |
| - |
176 |
| - |
177 |
| -class PostgresNodePortManager__Generic(PostgresNodePortManager): |
178 |
| - _os_ops: OsOperations |
179 |
| - _allocated_ports_guard: object |
180 |
| - _allocated_ports: set[int] |
181 |
| - |
182 |
| - def __init__(self, os_ops: OsOperations): |
183 |
| - assert os_ops is not None |
184 |
| - assert isinstance(os_ops, OsOperations) |
185 |
| - self._os_ops = os_ops |
186 |
| - self._allocated_ports_guard = threading.Lock() |
187 |
| - self._allocated_ports = set[int]() |
188 |
| - |
189 |
| - def reserve_port(self) -> int: |
190 |
| - ports = set(range(1024, 65535)) |
191 |
| - assert type(ports) == set # noqa: E721 |
192 |
| - |
193 |
| - assert self._allocated_ports_guard is not None |
194 |
| - assert type(self._allocated_ports) == set # noqa: E721 |
195 |
| - |
196 |
| - with self._allocated_ports_guard: |
197 |
| - ports.difference_update(self._allocated_ports) |
198 |
| - |
199 |
| - sampled_ports = random.sample(tuple(ports), min(len(ports), 100)) |
200 |
| - |
201 |
| - for port in sampled_ports: |
202 |
| - assert not (port in self._allocated_ports) |
203 |
| - |
204 |
| - if not self._os_ops.is_port_free(port): |
205 |
| - continue |
206 |
| - |
207 |
| - self._allocated_ports.add(port) |
208 |
| - return port |
209 |
| - |
210 |
| - raise PortForException("Can't select a port") |
211 |
| - |
212 |
| - def release_port(self, number: int) -> None: |
213 |
| - assert type(number) == int # noqa: E721 |
214 |
| - |
215 |
| - assert self._allocated_ports_guard is not None |
216 |
| - assert type(self._allocated_ports) == set # noqa: E721 |
217 |
| - |
218 |
| - with self._allocated_ports_guard: |
219 |
| - assert number in self._allocated_ports |
220 |
| - self._allocated_ports.discard(number) |
| 134 | + return '{}(ptype={}, process={})'.format( |
| 135 | + self.__class__.__name__, |
| 136 | + str(self.ptype), |
| 137 | + repr(self.process)) |
221 | 138 |
|
222 | 139 |
|
223 | 140 | class PostgresNode(object):
|
|
0 commit comments