-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathcluster.py
501 lines (414 loc) · 18 KB
/
cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
__all__ = ["Cluster"]
from typing import List, Optional
from arango.api import ApiGroup
from arango.exceptions import (
ClusterEndpointsError,
ClusterHealthError,
ClusterMaintenanceModeError,
ClusterRebalanceError,
ClusterServerCountError,
ClusterServerEngineError,
ClusterServerIDError,
ClusterServerModeError,
ClusterServerRoleError,
ClusterServerStatisticsError,
ClusterServerVersionError,
ClusterVpackSortMigrationError,
)
from arango.formatter import format_body
from arango.request import Request
from arango.response import Response
from arango.result import Result
from arango.typings import Json
class Cluster(ApiGroup): # pragma: no cover
def server_id(self) -> Result[str]:
"""Return the server ID.
:return: Server ID.
:rtype: str
:raise arango.exceptions.ClusterServerIDError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_admin/server/id")
def response_handler(resp: Response) -> str:
if resp.is_success:
return str(resp.body["id"])
raise ClusterServerIDError(resp, request)
return self._execute(request, response_handler)
def server_role(self) -> Result[str]:
"""Return the server role.
:return: Server role. Possible values are "SINGLE" (server which is
not in a cluster), "COORDINATOR" (cluster coordinator), "PRIMARY",
"SECONDARY", "AGENT" (Agency server in a cluster) or "UNDEFINED".
:rtype: str
:raise arango.exceptions.ClusterServerRoleError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_admin/server/role")
def response_handler(resp: Response) -> str:
if resp.is_success:
return str(resp.body["role"])
raise ClusterServerRoleError(resp, request)
return self._execute(request, response_handler)
def server_mode(self) -> Result[str]:
"""Return the server mode.
In a read-only server, all write operations will fail
with an error code of 1004 (ERROR_READ_ONLY). Creating or dropping
databases and collections will also fail with error code 11 (ERROR_FORBIDDEN).
:return: Server mode. Possible values are "default" or "readonly".
:rtype: str
:raise arango.exceptions.ClusterServerModeError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_admin/server/mode")
def response_handler(resp: Response) -> str:
if resp.is_success:
return str(resp.body["mode"])
raise ClusterServerModeError(resp, request)
return self._execute(request, response_handler)
def server_version(self, server_id: str) -> Result[Json]:
"""Return the version of the given server.
:param server_id: Server ID.
:type server_id: str
:return: Version of the given server.
:rtype: dict
:raise arango.exceptions.ClusterServerVersionError: If retrieval fails.
"""
request = Request(
method="get",
endpoint="/_admin/cluster/nodeVersion",
params={"ServerID": server_id},
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterServerVersionError(resp, request)
return self._execute(request, response_handler)
def server_engine(self, server_id: str) -> Result[Json]:
"""Return the engine details for the given server.
:param server_id: Server ID.
:type server_id: str
:return: Engine details of the given server.
:rtype: dict
:raise arango.exceptions.ClusterServerEngineError: If retrieval fails.
"""
request = Request(
method="get",
endpoint="/_admin/cluster/nodeEngine",
params={"ServerID": server_id},
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterServerEngineError(resp, request)
return self._execute(request, response_handler)
def server_count(self) -> Result[int]:
"""Return the number of servers in the cluster.
:return: Number of servers in the cluster.
:rtype: int
:raise arango.exceptions.ClusterServerCountError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_admin/cluster/numberOfServers")
def response_handler(resp: Response) -> int:
if resp.is_success:
result: int = resp.body
return result
raise ClusterServerCountError(resp, request)
return self._execute(request, response_handler)
def server_statistics(self, server_id: str) -> Result[Json]:
"""Return the statistics for the given server.
:param server_id: Server ID.
:type server_id: str
:return: Statistics for the given server.
:rtype: dict
:raise arango.exceptions.ClusterServerStatisticsError: If retrieval fails.
"""
request = Request(
method="get",
endpoint="/_admin/cluster/nodeStatistics",
params={"ServerID": server_id},
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterServerStatisticsError(resp, request)
return self._execute(request, response_handler)
def server_maintenance_mode(self, server_id: str) -> Result[Json]:
"""Return the maintenance status for the given server.
:param server_id: Server ID.
:type server_id: str
:return: Maintenance status for the given server.
:rtype: dict
:raise arango.exceptions.ClusterMaintenanceModeError: If retrieval fails.
"""
request = Request(
method="get",
endpoint=f"/_admin/cluster/maintenance/{server_id}",
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
result: Json = resp.body.get("result", {})
return result
raise ClusterMaintenanceModeError(resp, request)
return self._execute(request, response_handler)
def toggle_server_maintenance_mode(
self, server_id: str, mode: str, timeout: Optional[int] = None
) -> Result[Json]:
"""Enable or disable the maintenance mode for the given server.
:param server_id: Server ID.
:type server_id: str
:param mode: Maintenance mode. Allowed values are "normal" and "maintenance".
:type mode: str
:param timeout: Timeout in seconds.
:type timeout: Optional[int]
:return: Result of the operation.
:rtype: dict
:raise arango.exceptions.ClusterMaintenanceModeError: If toggle fails.
"""
request = Request(
method="put",
endpoint=f"/_admin/cluster/maintenance/{server_id}",
data={"mode": mode, "timeout": timeout},
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterMaintenanceModeError(resp, request)
return self._execute(request, response_handler)
def health(self) -> Result[Json]:
"""Return the cluster health.
:return: Cluster health.
:rtype: dict
:raise arango.exceptions.ClusterHealthError: If retrieval fails.
"""
request = Request(
method="get",
endpoint="/_admin/cluster/health",
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterHealthError(resp, request)
return self._execute(request, response_handler)
def toggle_maintenance_mode(self, mode: str) -> Result[Json]:
"""Enable or disable the cluster supervision (agency) maintenance mode.
:param mode: Maintenance mode. Allowed values are "on" and "off".
:type mode: str
:return: Result of the operation.
:rtype: dict
:raise arango.exceptions.ClusterMaintenanceModeError: If toggle fails.
"""
request = Request(
method="put",
endpoint="/_admin/cluster/maintenance",
data=f'"{mode}"',
)
def response_handler(resp: Response) -> Json:
if resp.is_success:
return format_body(resp.body)
raise ClusterMaintenanceModeError(resp, request)
return self._execute(request, response_handler)
def endpoints(self) -> Result[List[str]]:
"""Return coordinate endpoints. This method is for clusters only.
:return: List of endpoints.
:rtype: [str]
:raise arango.exceptions.ServerEndpointsError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_api/cluster/endpoints")
def response_handler(resp: Response) -> List[str]:
if not resp.is_success:
raise ClusterEndpointsError(resp, request)
return [item["endpoint"] for item in resp.body["endpoints"]]
return self._execute(request, response_handler)
def calculate_imbalance(self) -> Result[Json]:
"""Compute the current cluster imbalance, including
the amount of ongoing and pending move shard operations.
:return: Cluster imbalance information.
:rtype: dict
:raise: arango.exceptions.ClusterRebalanceError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_admin/cluster/rebalance")
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterRebalanceError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)
def rebalance(
self,
version: int = 1,
max_moves: Optional[int] = None,
leader_changes: Optional[bool] = None,
move_leaders: Optional[bool] = None,
move_followers: Optional[bool] = None,
pi_factor: Optional[float] = None,
exclude_system_collections: Optional[bool] = None,
databases_excluded: Optional[List[str]] = None,
) -> Result[Json]:
"""Compute and execute a cluster rebalance plan.
:param version: Must be set to 1.
:type version: int
:param max_moves: Maximum number of moves to be computed.
:type max_moves: int | None
:param leader_changes: Allow leader changes without moving data.
:type leader_changes: bool | None
:param move_leaders: Allow moving shard leaders.
:type move_leaders: bool | None
:param move_followers: Allow moving shard followers.
:type move_followers: bool | None
:param pi_factor: A weighting factor that should remain untouched.
:type pi_factor: float | None
:param exclude_system_collections: Ignore system collections in the
rebalance plan.
:type exclude_system_collections: bool | None
:param databases_excluded: List of database names to be excluded
from the analysis.
:type databases_excluded: [str] | None
:return: Cluster rebalance plan that has been executed.
:rtype: dict
:raise: arango.exceptions.ClusterRebalanceError: If retrieval fails.
"""
data: Json = dict(version=version)
if max_moves is not None:
data["maximumNumberOfMoves"] = max_moves
if leader_changes is not None:
data["leaderChanges"] = leader_changes
if move_leaders is not None:
data["moveLeaders"] = move_leaders
if move_followers is not None:
data["moveFollowers"] = move_followers
if pi_factor is not None:
data["piFactor"] = pi_factor
if exclude_system_collections is not None:
data["excludeSystemCollections"] = exclude_system_collections
if databases_excluded is not None:
data["databasesExcluded"] = databases_excluded
request = Request(method="put", endpoint="/_admin/cluster/rebalance", data=data)
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterRebalanceError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)
def calculate_rebalance_plan(
self,
version: int = 1,
max_moves: Optional[int] = None,
leader_changes: Optional[bool] = None,
move_leaders: Optional[bool] = None,
move_followers: Optional[bool] = None,
pi_factor: Optional[float] = None,
exclude_system_collections: Optional[bool] = None,
databases_excluded: Optional[List[str]] = None,
) -> Result[Json]:
"""Compute the cluster rebalance plan.
:param version: Must be set to 1.
:type version: int
:param max_moves: Maximum number of moves to be computed.
:type max_moves: int | None
:param leader_changes: Allow leader changes without moving data.
:type leader_changes: bool | None
:param move_leaders: Allow moving shard leaders.
:type move_leaders: bool | None
:param move_followers: Allow moving shard followers.
:type move_followers: bool | None
:param pi_factor: A weighting factor that should remain untouched.
:type pi_factor: float | None
:param exclude_system_collections: Ignore system collections in the
rebalance plan.
:type exclude_system_collections: bool | None
:param databases_excluded: List of database names to be excluded
from the analysis.
:type databases_excluded: [str] | None
:return: Cluster rebalance plan.
:rtype: dict
:raise: arango.exceptions.ClusterRebalanceError: If retrieval fails.
"""
data: Json = dict(version=version)
if max_moves is not None:
data["maximumNumberOfMoves"] = max_moves
if leader_changes is not None:
data["leaderChanges"] = leader_changes
if move_leaders is not None:
data["moveLeaders"] = move_leaders
if move_followers is not None:
data["moveFollowers"] = move_followers
if pi_factor is not None:
data["piFactor"] = pi_factor
if exclude_system_collections is not None:
data["excludeSystemCollections"] = exclude_system_collections
if databases_excluded is not None:
data["databasesExcluded"] = databases_excluded
request = Request(
method="post", endpoint="/_admin/cluster/rebalance", data=data
)
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterRebalanceError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)
def execute_rebalance_plan(
self, moves: List[Json], version: int = 1
) -> Result[bool]:
"""Execute the given set of move shard operations.
You can use :meth:`Cluster.calculate_rebalance_plan` to calculate
these operations to improve the balance of shards, leader shards,
and follower shards.
:param moves: List of move shard operations.
:type moves: [dict]
:param version: Must be set to 1.
:type version: int
:return: True if the methods have been accepted and scheduled
for execution.
:rtype: bool
:raise: arango.exceptions.ClusterRebalanceError: If request fails.
"""
data: Json = dict(version=version, moves=moves)
request = Request(
method="post", endpoint="/_admin/cluster/rebalance/execute", data=data
)
def response_handler(resp: Response) -> bool:
if not resp.is_success:
raise ClusterRebalanceError(resp, request)
result: bool = resp.body["code"] == 202
return result
return self._execute(request, response_handler)
def vpack_sort_migration_status(self) -> Result[Json]:
"""Query the status of the vpack sorting migration.
:return: Status of the VPack sort migration.
:rtype: dict
"""
request = Request(
method="get", endpoint="/_admin/cluster/vpackSortMigration/status"
)
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterVpackSortMigrationError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)
def vpack_sort_migration_index_check(self) -> Result[Json]:
"""Check for indexes impacted by the sorting behavior before 3.12.2.
:return: Status of indexes.
:rtype: dict
"""
request = Request(
method="get", endpoint="/_admin/cluster/vpackSortMigration/check"
)
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterVpackSortMigrationError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)
def migrate_vpack_sorting(self) -> Result[Json]:
"""Migrate instances to the new VPack sorting behavior.
:return: Status of the VPack sort migration.
:rtype: dict
"""
request = Request(
method="put", endpoint="/_admin/cluster/vpackSortMigration/migrate"
)
def response_handler(resp: Response) -> Json:
if not resp.is_success:
raise ClusterVpackSortMigrationError(resp, request)
result: Json = resp.body["result"]
return result
return self._execute(request, response_handler)