rucio-clients 35.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rucio-clients might be problematic. Click here for more details.

Files changed (88) hide show
  1. rucio/__init__.py +17 -0
  2. rucio/alembicrevision.py +15 -0
  3. rucio/client/__init__.py +15 -0
  4. rucio/client/accountclient.py +433 -0
  5. rucio/client/accountlimitclient.py +183 -0
  6. rucio/client/baseclient.py +974 -0
  7. rucio/client/client.py +76 -0
  8. rucio/client/configclient.py +126 -0
  9. rucio/client/credentialclient.py +59 -0
  10. rucio/client/didclient.py +866 -0
  11. rucio/client/diracclient.py +56 -0
  12. rucio/client/downloadclient.py +1785 -0
  13. rucio/client/exportclient.py +44 -0
  14. rucio/client/fileclient.py +50 -0
  15. rucio/client/importclient.py +42 -0
  16. rucio/client/lifetimeclient.py +90 -0
  17. rucio/client/lockclient.py +109 -0
  18. rucio/client/metaconventionsclient.py +140 -0
  19. rucio/client/pingclient.py +44 -0
  20. rucio/client/replicaclient.py +454 -0
  21. rucio/client/requestclient.py +125 -0
  22. rucio/client/rseclient.py +746 -0
  23. rucio/client/ruleclient.py +294 -0
  24. rucio/client/scopeclient.py +90 -0
  25. rucio/client/subscriptionclient.py +173 -0
  26. rucio/client/touchclient.py +82 -0
  27. rucio/client/uploadclient.py +955 -0
  28. rucio/common/__init__.py +13 -0
  29. rucio/common/cache.py +74 -0
  30. rucio/common/config.py +801 -0
  31. rucio/common/constants.py +159 -0
  32. rucio/common/constraints.py +17 -0
  33. rucio/common/didtype.py +189 -0
  34. rucio/common/exception.py +1151 -0
  35. rucio/common/extra.py +36 -0
  36. rucio/common/logging.py +420 -0
  37. rucio/common/pcache.py +1408 -0
  38. rucio/common/plugins.py +153 -0
  39. rucio/common/policy.py +84 -0
  40. rucio/common/schema/__init__.py +150 -0
  41. rucio/common/schema/atlas.py +413 -0
  42. rucio/common/schema/belleii.py +408 -0
  43. rucio/common/schema/domatpc.py +401 -0
  44. rucio/common/schema/escape.py +426 -0
  45. rucio/common/schema/generic.py +433 -0
  46. rucio/common/schema/generic_multi_vo.py +412 -0
  47. rucio/common/schema/icecube.py +406 -0
  48. rucio/common/stomp_utils.py +159 -0
  49. rucio/common/stopwatch.py +55 -0
  50. rucio/common/test_rucio_server.py +148 -0
  51. rucio/common/types.py +403 -0
  52. rucio/common/utils.py +2238 -0
  53. rucio/rse/__init__.py +96 -0
  54. rucio/rse/protocols/__init__.py +13 -0
  55. rucio/rse/protocols/bittorrent.py +184 -0
  56. rucio/rse/protocols/cache.py +122 -0
  57. rucio/rse/protocols/dummy.py +111 -0
  58. rucio/rse/protocols/gfal.py +703 -0
  59. rucio/rse/protocols/globus.py +243 -0
  60. rucio/rse/protocols/gsiftp.py +92 -0
  61. rucio/rse/protocols/http_cache.py +82 -0
  62. rucio/rse/protocols/mock.py +123 -0
  63. rucio/rse/protocols/ngarc.py +209 -0
  64. rucio/rse/protocols/posix.py +250 -0
  65. rucio/rse/protocols/protocol.py +594 -0
  66. rucio/rse/protocols/rclone.py +364 -0
  67. rucio/rse/protocols/rfio.py +136 -0
  68. rucio/rse/protocols/srm.py +338 -0
  69. rucio/rse/protocols/ssh.py +413 -0
  70. rucio/rse/protocols/storm.py +206 -0
  71. rucio/rse/protocols/webdav.py +550 -0
  72. rucio/rse/protocols/xrootd.py +301 -0
  73. rucio/rse/rsemanager.py +764 -0
  74. rucio/vcsversion.py +11 -0
  75. rucio/version.py +38 -0
  76. rucio_clients-35.7.0.data/data/etc/rse-accounts.cfg.template +25 -0
  77. rucio_clients-35.7.0.data/data/etc/rucio.cfg.atlas.client.template +42 -0
  78. rucio_clients-35.7.0.data/data/etc/rucio.cfg.template +257 -0
  79. rucio_clients-35.7.0.data/data/requirements.client.txt +15 -0
  80. rucio_clients-35.7.0.data/data/rucio_client/merge_rucio_configs.py +144 -0
  81. rucio_clients-35.7.0.data/scripts/rucio +2542 -0
  82. rucio_clients-35.7.0.data/scripts/rucio-admin +2447 -0
  83. rucio_clients-35.7.0.dist-info/METADATA +50 -0
  84. rucio_clients-35.7.0.dist-info/RECORD +88 -0
  85. rucio_clients-35.7.0.dist-info/WHEEL +5 -0
  86. rucio_clients-35.7.0.dist-info/licenses/AUTHORS.rst +97 -0
  87. rucio_clients-35.7.0.dist-info/licenses/LICENSE +201 -0
  88. rucio_clients-35.7.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,406 @@
1
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from jsonschema import ValidationError, validate
16
+
17
+ from rucio.common.exception import InvalidObject
18
+
19
+ ACCOUNT_LENGTH = 25
20
+
21
+ ACCOUNT = {"description": "Account name",
22
+ "type": "string",
23
+ "maxLength": ACCOUNT_LENGTH,
24
+ "pattern": "^[a-z0-9-_]+$"}
25
+
26
+ ACCOUNTS = {"description": "Array of accounts",
27
+ "type": "array",
28
+ "items": ACCOUNT,
29
+ "minItems": 0,
30
+ "maxItems": 1000}
31
+
32
+
33
+ ACCOUNT_TYPE = {"description": "Account type",
34
+ "type": "string",
35
+ "enum": ["USER", "GROUP", "SERVICE"]}
36
+
37
+ ACTIVITY = {"description": "Activity name",
38
+ "type": "string",
39
+ "enum": ["Data Brokering", "Data Consolidation", "Data rebalancing",
40
+ "Debug", "Express", "Functional Test", "Group Subscriptions",
41
+ "Production Input", "Production Output",
42
+ "Analysis Input", "Analysis Output", "Staging",
43
+ "T0 Export", "T0 Tape", "Upload/Download (Job)",
44
+ "Upload/Download (User)", "User Subscriptions"]}
45
+
46
+ SCOPE_LENGTH = 25
47
+
48
+ SCOPE = {"description": "Scope name",
49
+ "type": "string",
50
+ "maxLength": SCOPE_LENGTH,
51
+ "pattern": "^[a-zA-Z_\\-.0-9]+$"}
52
+
53
+ R_SCOPE = {"description": "Scope name",
54
+ "type": "string",
55
+ "pattern": "\\w"}
56
+
57
+
58
+ NAME_LENGTH = 500
59
+
60
+ NAME = {"description": "IceCube Data Identifier name",
61
+ "type": "string",
62
+ "maxLength": NAME_LENGTH,
63
+ "pattern": r"^\/[A-Za-z0-9][A-Za-z0-9\\.\\-\\_\/\#]*$"}
64
+
65
+ # read name
66
+ R_NAME = NAME
67
+
68
+ LOCKED = {"description": "Rule locked status",
69
+ "type": ["boolean", "null"]}
70
+
71
+ ASK_APPROVAL = {"description": "Rule approval request",
72
+ "type": ["boolean", "null"]}
73
+
74
+ ASYNCHRONOUS = {"description": "Asynchronous rule creation",
75
+ "type": ["boolean", "null"]}
76
+
77
+ DELAY_INJECTION = {"description": "Time (in seconds) to wait before starting applying the rule. Implies asynchronous rule creation.",
78
+ "type": ["integer", "null"]}
79
+
80
+ PURGE_REPLICAS = {"description": "Rule purge replica status",
81
+ "type": "boolean"}
82
+
83
+ IGNORE_AVAILABILITY = {"description": "Rule ignore availability status",
84
+ "type": "boolean"}
85
+
86
+ RSE = {"description": "RSE name",
87
+ "type": "string",
88
+ "pattern": "^([A-Z0-9]+([_-][A-Z0-9]+)*)$"}
89
+
90
+ RSE_ATTRIBUTE = {"description": "RSE attribute",
91
+ "type": "string",
92
+ "pattern": r'([A-Za-z0-9\._-]+[=<>][A-Za-z0-9_-]+)'}
93
+
94
+ DEFAULT_RSE_ATTRIBUTE = {"description": "Default RSE attribute",
95
+ "type": "string",
96
+ "pattern": r'([A-Z0-9]+([_-][A-Z0-9]+)*)'}
97
+
98
+ REPLICA_STATE = {"description": "Replica state",
99
+ "type": "string",
100
+ "enum": ["AVAILABLE", "UNAVAILABLE", "COPYING", "BEING_DELETED", "BAD", "SOURCE", "A", "U", "C", "B", "D", "S"]}
101
+
102
+ DATE = {"description": "Date",
103
+ "type": "string",
104
+ "pattern": r'((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun))[,]\s\d{2}\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{4}\s(0\d|1\d|2[0-3])(\:)(0\d|1\d|2\d|3\d|4\d|5\d)(\:)(0\d|1\d|2\d|3\d|4\d|5\d)\s(UTC)'}
105
+
106
+ DID_TYPE = {"description": "DID type",
107
+ "type": "string",
108
+ "enum": ["DATASET", "CONTAINER", "FILE", "F"]}
109
+
110
+ GROUPING = {"description": "Rule grouping",
111
+ "type": ["string", "null"],
112
+ "enum": ["DATASET", "NONE", "ALL", None]}
113
+
114
+ NOTIFY = {"description": "Rule notification setting",
115
+ "type": ["string", "null"],
116
+ "enum": ["Y", "C", "N", "P", None]}
117
+
118
+ COMMENT = {"description": "Rule comment",
119
+ "type": ["string", "null"],
120
+ "maxLength": 250}
121
+
122
+ METADATA = {"description": "Rule wfms metadata",
123
+ "type": ["string", "null"],
124
+ "maxLength": 3999}
125
+
126
+ BYTES = {"description": "Size in bytes",
127
+ "type": "integer"}
128
+
129
+ ADLER32 = {"description": "adler32",
130
+ "type": "string",
131
+ "pattern": "^[a-fA-F\\d]{8}$"}
132
+
133
+ WEIGHT = {"description": "Rule weight",
134
+ "type": ["string", "null"]}
135
+
136
+ MD5 = {"description": "md5",
137
+ "type": "string",
138
+ "pattern": "^[a-fA-F\\d]{32}$"}
139
+
140
+ UUID = {"description": "Universally Unique Identifier (UUID)",
141
+ "type": "string",
142
+ "pattern": '^(\\{){0,1}[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}(\\}){0,1}$'}
143
+
144
+ META = {"description": "Data Identifier(DID) metadata",
145
+ "type": "object",
146
+ "properties": {"guid": UUID},
147
+ "additionalProperties": True}
148
+
149
+ PFN = {"description": "Physical File Name", "type": "string"}
150
+
151
+ COPIES = {"description": "Number of replica copies", "type": "integer"}
152
+
153
+ RSE_EXPRESSION = {"description": "RSE expression", "type": "string"}
154
+
155
+ SOURCE_REPLICA_EXPRESSION = {"description": "RSE expression", "type": ["string", "null"]}
156
+
157
+ LIFETIME = {"description": "Lifetime", "type": "number"}
158
+
159
+ RULE_LIFETIME = {"description": "Rule lifetime", "type": ["number", "null"]}
160
+
161
+ SUBSCRIPTION_ID = {"description": "Rule Subscription id", "type": ["string", "null"]}
162
+
163
+ PRIORITY = {"description": "Priority of the transfers",
164
+ "type": "integer"}
165
+
166
+ SPLIT_CONTAINER = {"description": "Rule split container mode",
167
+ "type": ["boolean", "null"]}
168
+
169
+ RULE = {"description": "Replication rule",
170
+ "type": "object",
171
+ "properties": {"dids": {"type": "array"},
172
+ "account": ACCOUNT,
173
+ "copies": COPIES,
174
+ "rse_expression": RSE_EXPRESSION,
175
+ "grouping": GROUPING,
176
+ "weight": WEIGHT,
177
+ "lifetime": RULE_LIFETIME,
178
+ "locked": LOCKED,
179
+ "subscription_id": SUBSCRIPTION_ID,
180
+ "source_replica_expression": SOURCE_REPLICA_EXPRESSION,
181
+ "activity": ACTIVITY,
182
+ "notify": NOTIFY,
183
+ "purge_replicas": PURGE_REPLICAS,
184
+ "ignore_availability": IGNORE_AVAILABILITY,
185
+ "comment": COMMENT,
186
+ "ask_approval": ASK_APPROVAL,
187
+ "asynchronous": ASYNCHRONOUS,
188
+ "delay_injection": DELAY_INJECTION,
189
+ "priority": PRIORITY,
190
+ 'split_container': SPLIT_CONTAINER,
191
+ 'meta': METADATA},
192
+ "required": ["dids", "copies", "rse_expression"],
193
+ "additionalProperties": False}
194
+
195
+ RULES = {"description": "Array of replication rules",
196
+ "type": "array",
197
+ "items": RULE,
198
+ "minItems": 1,
199
+ "maxItems": 1000}
200
+
201
+ COLLECTION_TYPE = {"description": "Dataset or container type",
202
+ "type": "string",
203
+ "enum": ["DATASET", "CONTAINER"]}
204
+
205
+ COLLECTION = {"description": "Dataset or container",
206
+ "type": "object",
207
+ "properties": {"scope": SCOPE,
208
+ "name": NAME,
209
+ "type": COLLECTION_TYPE,
210
+ "meta": META,
211
+ "rules": RULES},
212
+ "required": ["scope", "name", "type"],
213
+ "additionalProperties": False}
214
+
215
+ COLLECTIONS = {"description": "Array of datasets or containers",
216
+ "type": "array",
217
+ "items": COLLECTION,
218
+ "minItems": 1,
219
+ "maxItems": 1000}
220
+
221
+ DID = {"description": "Data Identifier(DID)",
222
+ "type": "object",
223
+ "properties": {"scope": SCOPE,
224
+ "name": NAME,
225
+ "type": DID_TYPE,
226
+ "meta": META,
227
+ "rules": RULES,
228
+ "bytes": BYTES,
229
+ "adler32": ADLER32,
230
+ "md5": MD5,
231
+ "state": REPLICA_STATE,
232
+ "pfn": PFN},
233
+ "required": ["scope", "name"],
234
+ "additionalProperties": False}
235
+
236
+ DID_FILTERS = {"description": "Array to filter DIDs by metadata",
237
+ "type": "array",
238
+ "additionalProperties": True}
239
+
240
+ R_DID = {"description": "Data Identifier(DID)",
241
+ "type": "object",
242
+ "properties": {"scope": R_SCOPE,
243
+ "name": R_NAME,
244
+ "type": DID_TYPE,
245
+ "meta": META,
246
+ "rules": RULES,
247
+ "bytes": BYTES,
248
+ "adler32": ADLER32,
249
+ "md5": MD5,
250
+ "state": REPLICA_STATE,
251
+ "pfn": PFN},
252
+ "required": ["scope", "name"],
253
+ "additionalProperties": False}
254
+
255
+ DIDS = {"description": "Array of Data Identifiers(DIDs)",
256
+ "type": "array",
257
+ "items": DID,
258
+ "minItems": 1,
259
+ "maxItems": 1000}
260
+
261
+ R_DIDS = {"description": "Array of Data Identifiers(DIDs)",
262
+ "type": "array",
263
+ "items": R_DID,
264
+ "minItems": 1,
265
+ "maxItems": 1000}
266
+
267
+ ATTACHMENT = {"description": "Attachement",
268
+ "type": "object",
269
+ "properties": {"scope": SCOPE,
270
+ "name": NAME,
271
+ "rse": {"description": "RSE name",
272
+ "type": ["string", "null"],
273
+ "pattern": "^([A-Z0-9]+([_-][A-Z0-9]+)*)$"},
274
+ "dids": DIDS},
275
+ "required": ["dids"],
276
+ "additionalProperties": False}
277
+
278
+ ATTACHMENTS = {"description": "Array of attachments",
279
+ "type": "array",
280
+ "items": ATTACHMENT,
281
+ "minItems": 1,
282
+ "maxItems": 1000}
283
+
284
+ SUBSCRIPTION_FILTER = {"type": "object",
285
+ "properties": {"datatype": {"type": "array"},
286
+ "prod_step": {"type": "array"},
287
+ "stream_name": {"type": "array"},
288
+ "project": {"type": "array"},
289
+ "scope": {"type": "array"},
290
+ "pattern": {"type": "string"},
291
+ "excluded_pattern": {"type": "string"},
292
+ "group": {"type": "string"},
293
+ "provenance": {"type": "string"},
294
+ "account": ACCOUNTS,
295
+ "grouping": {"type": "string"},
296
+ "split_rule": {"type": "boolean"}}}
297
+
298
+ ADD_REPLICA_FILE = {"description": "add replica file",
299
+ "type": "object",
300
+ "properties": {"scope": SCOPE,
301
+ "name": NAME,
302
+ "bytes": BYTES,
303
+ "adler32": ADLER32},
304
+ "required": ["scope", "name", "bytes", "adler32"]}
305
+
306
+ ADD_REPLICA_FILES = {"description": "add replica files",
307
+ "type": "array",
308
+ "items": ADD_REPLICA_FILE,
309
+ "minItems": 1,
310
+ "maxItems": 1000}
311
+
312
+ CACHE_ADD_REPLICAS = {"description": "rucio cache add replicas",
313
+ "type": "object",
314
+ "properties": {"files": ADD_REPLICA_FILES,
315
+ "rse": RSE,
316
+ "lifetime": LIFETIME,
317
+ "operation": {"enum": ["add_replicas"]}},
318
+ "required": ['files', 'rse', 'lifetime', 'operation']}
319
+
320
+ DELETE_REPLICA_FILE = {"description": "delete replica file",
321
+ "type": "object",
322
+ "properties": {"scope": SCOPE,
323
+ "name": NAME},
324
+ "required": ["scope", "name"]}
325
+
326
+ DELETE_REPLICA_FILES = {"description": "delete replica files",
327
+ "type": "array",
328
+ "items": DELETE_REPLICA_FILE,
329
+ "minItems": 1,
330
+ "maxItems": 1000}
331
+
332
+ CACHE_DELETE_REPLICAS = {"description": "rucio cache delete replicas",
333
+ "type": "object",
334
+ "properties": {"files": DELETE_REPLICA_FILES,
335
+ "rse": RSE,
336
+ "operation": {"enum": ["delete_replicas"]}},
337
+ "required": ['files', 'rse', 'operation']}
338
+
339
+ MESSAGE_OPERATION = {"type": "object",
340
+ "properties": {'operation': {"enum": ["add_replicas", "delete_replicas"]}}}
341
+
342
+ ACCOUNT_ATTRIBUTE = {"description": "Account attribute",
343
+ "type": "string",
344
+ "pattern": r'^[a-zA-Z0-9-_\\/\\.]{1,30}$'}
345
+
346
+ SCOPE_NAME_REGEXP = '/([^/]*)(?=/)(.*)'
347
+
348
+ DISTANCE = {"description": "RSE distance",
349
+ "type": "object",
350
+ "properties": {
351
+ "src_rse_id": {"type": "string"},
352
+ "dest_rse_id": {"type": "string"},
353
+ "ranking": {"type": "integer"}
354
+ },
355
+ "required": ["src_rse_id", "dest_rse_id", "ranking"],
356
+ "additionalProperties": True}
357
+
358
+ IMPORT = {"description": "import data into rucio.",
359
+ "type": "object",
360
+ "properties": {
361
+ "rses": {
362
+ "type": "object"
363
+ },
364
+ "distances": {
365
+ "type": "object"
366
+ }
367
+ }}
368
+
369
+ SCHEMAS = {'account': ACCOUNT,
370
+ 'account_type': ACCOUNT_TYPE,
371
+ 'activity': ACTIVITY,
372
+ 'name': NAME,
373
+ 'r_name': R_NAME,
374
+ 'rse': RSE,
375
+ 'rse_attribute': RSE_ATTRIBUTE,
376
+ 'scope': SCOPE,
377
+ 'r_scope': R_SCOPE,
378
+ 'did': DID,
379
+ 'did_filters': DID_FILTERS,
380
+ 'r_did': R_DID,
381
+ 'dids': DIDS,
382
+ 'rule': RULE,
383
+ 'r_dids': R_DIDS,
384
+ 'collection': COLLECTION,
385
+ 'collections': COLLECTIONS,
386
+ 'attachment': ATTACHMENT,
387
+ 'attachments': ATTACHMENTS,
388
+ 'subscription_filter': SUBSCRIPTION_FILTER,
389
+ 'cache_add_replicas': CACHE_ADD_REPLICAS,
390
+ 'cache_delete_replicas': CACHE_DELETE_REPLICAS,
391
+ 'account_attribute': ACCOUNT_ATTRIBUTE,
392
+ 'import': IMPORT}
393
+
394
+
395
+ def validate_schema(name, obj):
396
+ """
397
+ Validate object against json schema
398
+
399
+ :param name: The json schema name.
400
+ :param obj: The object to validate.
401
+ """
402
+ try:
403
+ if obj:
404
+ validate(obj, SCHEMAS.get(name, {}))
405
+ except ValidationError as error: # NOQA, pylint: disable=W0612
406
+ raise InvalidObject(f'Problem validating {name}: {error}')
@@ -0,0 +1,159 @@
1
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ Common utility functions for stomp connections
17
+ """
18
+
19
+ import logging
20
+ import socket
21
+ from time import monotonic
22
+ from typing import TYPE_CHECKING
23
+
24
+ from stomp import Connection
25
+
26
+ if TYPE_CHECKING:
27
+ from collections.abc import Callable, Sequence
28
+ from typing import Any
29
+
30
+ LoggerFunction = Callable[..., Any]
31
+
32
+
33
+ def resolve_ips(fqdns: "Sequence[str]", logger: "LoggerFunction" = logging.log):
34
+ logger(logging.DEBUG, 'resolving dns aliases: %s' % fqdns)
35
+ resolved = []
36
+ for fqdn in fqdns:
37
+ addrinfos = socket.getaddrinfo(fqdn, 0, socket.AF_INET, 0, socket.IPPROTO_TCP)
38
+ resolved.extend(ai[4][0] for ai in addrinfos)
39
+ logger(logging.DEBUG, 'dns aliases resolved to %s', resolved)
40
+ return resolved
41
+
42
+
43
+ class StompConnectionManager:
44
+
45
+ def __init__(self):
46
+ self._brokers = None
47
+ self._port = None
48
+ self._use_ssl = None
49
+ self._vhost = None
50
+ self._reconnect_attempts = None
51
+ self._ssl_key_file = None
52
+ self._timeout = None
53
+ self._heartbeats = None
54
+
55
+ self._connections = {}
56
+
57
+ def is_stalled(self, connection: Connection, *, logger: "LoggerFunction" = logging.log):
58
+ if not connection.is_connected():
59
+ return True
60
+
61
+ if self._heartbeats and getattr(connection, 'received_heartbeat') and connection.received_heartbeat:
62
+ heartbeat_period_seconds = max(0, self._heartbeats[0], self._heartbeats[1]) / 1000
63
+
64
+ if not heartbeat_period_seconds:
65
+ return False
66
+
67
+ now = monotonic()
68
+ if connection.received_heartbeat + 10 * heartbeat_period_seconds < now:
69
+ logger(logging.WARNING, "Stomp connection missed heartbeats for a long time")
70
+ return True
71
+
72
+ return False
73
+
74
+ def disconnect(self):
75
+ for conn in self._connections.values():
76
+ if not conn.is_connected():
77
+ conn.disconnect()
78
+
79
+ def re_configure(
80
+ self,
81
+ brokers: "Sequence[str]",
82
+ port: int,
83
+ use_ssl: bool,
84
+ vhost,
85
+ reconnect_attempts: int,
86
+ ssl_key_file,
87
+ ssl_cert_file,
88
+ timeout,
89
+ heartbeats=(0, 1000),
90
+ *,
91
+ logger: "LoggerFunction" = logging.log
92
+ ) -> tuple[list, list]:
93
+
94
+ configuration_changed = any([
95
+ self._brokers != brokers,
96
+ self._port != port,
97
+ self._use_ssl != use_ssl,
98
+ self._vhost != vhost,
99
+ self._reconnect_attempts != reconnect_attempts,
100
+ self._ssl_key_file != ssl_key_file,
101
+ self._timeout != timeout,
102
+ self._heartbeats != heartbeats,
103
+ ])
104
+ if configuration_changed:
105
+ self._brokers = brokers
106
+ self._port = port
107
+ self._use_ssl = use_ssl
108
+ self._vhost = vhost
109
+ self._reconnect_attempts = reconnect_attempts
110
+ self._ssl_key_file = ssl_key_file
111
+ self._timeout = timeout
112
+ self._heartbeats = heartbeats
113
+
114
+ current_remotes = set(self._connections)
115
+ desired_remotes = set((ip, port) for ip in resolve_ips(brokers, logger=logger))
116
+
117
+ if configuration_changed:
118
+ # Re-create all connections
119
+ to_delete = current_remotes
120
+ to_create = desired_remotes
121
+ else:
122
+ to_delete = current_remotes.difference(desired_remotes)
123
+ to_create = desired_remotes.difference(current_remotes)
124
+
125
+ for remote in current_remotes.intersection(desired_remotes):
126
+ conn = self._connections[remote]
127
+
128
+ if self.is_stalled(conn, logger=logger):
129
+ # Re-create stalled connections
130
+ to_delete.add(remote)
131
+ to_create.add(remote)
132
+
133
+ deleted_conns = []
134
+ for remote in to_delete:
135
+ conn = self._connections.pop(remote)
136
+ if conn.is_connected():
137
+ conn.disconnect()
138
+ deleted_conns.append(to_delete)
139
+
140
+ created_conns = []
141
+ for remote in to_create:
142
+ conn = Connection(
143
+ host_and_ports=[remote],
144
+ vhost=vhost,
145
+ timeout=timeout,
146
+ heartbeats=heartbeats,
147
+ reconnect_attempts_max=reconnect_attempts
148
+ )
149
+ if use_ssl:
150
+ conn.set_ssl(key_file=ssl_key_file, cert_file=ssl_cert_file)
151
+ self._connections[remote] = conn
152
+ created_conns.append(conn)
153
+
154
+ if not to_delete and not to_create:
155
+ logger(logging.INFO, "Stomp connections didn't change")
156
+ else:
157
+ logger(logging.INFO, f"Stomp connections refreshed. Deleted: {list(to_delete)}. Added: {list(to_create)}")
158
+
159
+ return created_conns, deleted_conns
@@ -0,0 +1,55 @@
1
+ # Copyright European Organization for Nuclear Research (CERN) since 2012
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ import time
17
+ from typing import Optional
18
+
19
+
20
+ class Stopwatch:
21
+ """Stopwatch to measure time durations.
22
+
23
+ Note: The stopwatch is started on initialization.
24
+ """
25
+
26
+ _t_start: float
27
+ _t_end: Optional[float]
28
+
29
+ def __init__(self) -> None:
30
+ self.restart()
31
+
32
+ def _now(self) -> float:
33
+ # TODO: change to time.monotonic_ns() if python 3.6 support is dropped.
34
+ return time.monotonic()
35
+
36
+ def restart(self) -> None:
37
+ """Resets and starts the stopwatch."""
38
+ self._t_start = self._now()
39
+ self._t_end = None
40
+
41
+ def stop(self) -> None:
42
+ """Stops the stopwatch."""
43
+ self._t_end = self._now()
44
+
45
+ @property
46
+ def elapsed(self) -> float:
47
+ """Returns the total number of elapsed seconds."""
48
+ if self._t_end is None:
49
+ return self._now() - self._t_start
50
+ else:
51
+ return self._t_end - self._t_start
52
+
53
+ def __float__(self) -> float:
54
+ """Returns the total number of elapsed seconds."""
55
+ return self.elapsed