|
83 | 83 | BackupException, \
|
84 | 84 | InvalidOperationException
|
85 | 85 |
|
| 86 | +from .port_managerimportPostgresNodePortManager |
| 87 | +from .port_managerimportPostgresNodePortManager__ThisHost |
| 88 | +from .port_managerimportPostgresNodePortManager__Generic |
| 89 | + |
86 | 90 | from .loggerimportTestgresLogger
|
87 | 91 |
|
88 | 92 | from .pubsubimportPublication,Subscription
|
89 | 93 |
|
90 | 94 | from .standbyimportFirst
|
91 | 95 |
|
92 |
| -from .importutils |
93 |
| - |
94 | 96 | from .utilsimport \
|
95 | 97 | PgVer, \
|
96 | 98 | eprint, \
|
|
100 | 102 | options_string, \
|
101 | 103 | clean_on_error
|
102 | 104 |
|
103 |
| -from .helpers.port_managerimportPortForException |
104 |
| - |
105 | 105 | from .backupimportNodeBackup
|
106 | 106 |
|
107 | 107 | from .operations.os_opsimportConnectionParams
|
@@ -131,93 +131,10 @@ def __getattr__(self, name):
|
131 | 131 | returngetattr(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 |
| -classPostgresNodePortManager: |
140 |
| -def__init__(self): |
141 |
| -super().__init__() |
142 |
| - |
143 |
| -defreserve_port(self)->int: |
144 |
| -raiseNotImplementedError("PostgresNodePortManager::reserve_port is not implemented.") |
145 |
| - |
146 |
| -defrelease_port(self,number:int)->None: |
147 |
| -asserttype(number)==int# noqa: E721 |
148 |
| -raiseNotImplementedError("PostgresNodePortManager::release_port is not implemented.") |
149 |
| - |
150 |
| - |
151 |
| -classPostgresNodePortManager__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_guardisnotNone |
161 |
| - |
162 |
| -if__class__.sm_single_instanceisNone: |
163 |
| -with__class__.sm_single_instance_guard: |
164 |
| -__class__.sm_single_instance=super().__new__(cls) |
165 |
| -assert__class__.sm_single_instance |
166 |
| -asserttype(__class__.sm_single_instance)==__class__# noqa: E721 |
167 |
| -return__class__.sm_single_instance |
168 |
| - |
169 |
| -defreserve_port(self)->int: |
170 |
| -returnutils.reserve_port() |
171 |
| - |
172 |
| -defrelease_port(self,number:int)->None: |
173 |
| -asserttype(number)==int# noqa: E721 |
174 |
| -returnutils.release_port(number) |
175 |
| - |
176 |
| - |
177 |
| -classPostgresNodePortManager__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 |
| -assertos_opsisnotNone |
184 |
| -assertisinstance(os_ops,OsOperations) |
185 |
| -self._os_ops=os_ops |
186 |
| -self._allocated_ports_guard=threading.Lock() |
187 |
| -self._allocated_ports=set[int]() |
188 |
| - |
189 |
| -defreserve_port(self)->int: |
190 |
| -ports=set(range(1024,65535)) |
191 |
| -asserttype(ports)==set# noqa: E721 |
192 |
| - |
193 |
| -assertself._allocated_ports_guardisnotNone |
194 |
| -asserttype(self._allocated_ports)==set# noqa: E721 |
195 |
| - |
196 |
| -withself._allocated_ports_guard: |
197 |
| -ports.difference_update(self._allocated_ports) |
198 |
| - |
199 |
| -sampled_ports=random.sample(tuple(ports),min(len(ports),100)) |
200 |
| - |
201 |
| -forportinsampled_ports: |
202 |
| -assertnot (portinself._allocated_ports) |
203 |
| - |
204 |
| -ifnotself._os_ops.is_port_free(port): |
205 |
| -continue |
206 |
| - |
207 |
| -self._allocated_ports.add(port) |
208 |
| -returnport |
209 |
| - |
210 |
| -raisePortForException("Can't select a port") |
211 |
| - |
212 |
| -defrelease_port(self,number:int)->None: |
213 |
| -asserttype(number)==int# noqa: E721 |
214 |
| - |
215 |
| -assertself._allocated_ports_guardisnotNone |
216 |
| -asserttype(self._allocated_ports)==set# noqa: E721 |
217 |
| - |
218 |
| -withself._allocated_ports_guard: |
219 |
| -assertnumberinself._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 | classPostgresNode(object):
|
|