Coverage for curator/actions/cluster_routing.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-20 21:00 -0600

1"""Cluster Routing action class""" 

2import logging 

3# pylint: disable=import-error 

4from curator.helpers.testers import verify_client_object 

5from curator.helpers.utils import report_failure 

6from curator.helpers.waiters import wait_for_it 

7 

8class ClusterRouting: 

9 """ClusterRouting Action Class""" 

10 def __init__( 

11 self, client, routing_type=None, setting=None, value=None, wait_for_completion=False, 

12 wait_interval=9, max_wait=-1 

13 ): 

14 """ 

15 For now, the cluster routing settings are hardcoded to be ``transient`` 

16 

17 :param client: A client connection object 

18 :param routing_type: Type of routing to apply. Either ``allocation`` or ``rebalance`` 

19 :param setting: Currently, the only acceptable value for ``setting`` is ``enable``. This is 

20 here in case that changes. 

21 :param value: Used only if ``setting`` is ``enable``. Semi-dependent on ``routing_type``. 

22 Acceptable values for ``allocation`` and ``rebalance`` are ``all``, ``primaries``, and 

23 ``none`` (string, not :py:class:`None`). If ``routing_type`` is ``allocation``, this 

24 can also be ``new_primaries``, and if ``rebalance``, it can be ``replicas``. 

25 :param wait_for_completion: Wait for completion before returning. 

26 :param wait_interval: Seconds to wait between completion checks. 

27 :param max_wait: Maximum number of seconds to ``wait_for_completion`` 

28 

29 :type client: :py:class:`~.elasticsearch.Elasticsearch` 

30 :type routing_type: str 

31 :type setting: str 

32 :type value: str 

33 :type wait_for_completion: bool 

34 :type wait_interval: int 

35 :type max_wait: int 

36 """ 

37 verify_client_object(client) 

38 #: An :py:class:`~.elasticsearch.Elasticsearch` client object 

39 self.client = client 

40 self.loggit = logging.getLogger('curator.actions.cluster_routing') 

41 #: Object attribute that gets the value of param ``wait_for_completion`` 

42 self.wfc = wait_for_completion 

43 #: Object attribute that gets the value of param ``wait_interval`` 

44 self.wait_interval = wait_interval 

45 #: Object attribute that gets the value of param ``max_wait``. How long in seconds to 

46 #: :py:attr:`wfc` before returning with an exception. A value of ``-1`` means wait forever. 

47 self.max_wait = max_wait 

48 

49 if setting != 'enable': 

50 raise ValueError(f'Invalid value for "setting": {setting}.') 

51 if routing_type == 'allocation': 

52 if value not in ['all', 'primaries', 'new_primaries', 'none']: 

53 raise ValueError( 

54 f'Invalid "value": {value} with "routing_type":{routing_type}.') 

55 elif routing_type == 'rebalance': 

56 if value not in ['all', 'primaries', 'replicas', 'none']: 

57 raise ValueError( 

58 f'Invalid "value": {value} with "routing_type": {routing_type}.') 

59 else: 

60 raise ValueError(f'Invalid value for "routing_type":{routing_type}.') 

61 bkey = f'cluster.routing.{routing_type}.{setting}' 

62 #: Populated at instance creation time. Value is built from the passed values from params 

63 #: ``routing_type`` and ``setting``, e.g. ``cluster.routing.routing_type.setting`` 

64 self.settings = {bkey : value} 

65 

66 def do_dry_run(self): 

67 """Log what the output would be, but take no action.""" 

68 self.loggit.info('DRY-RUN MODE. No changes will be made.') 

69 msg = f'DRY-RUN: Update cluster routing transient settings: {self.settings}' 

70 self.loggit.info(msg) 

71 

72 def do_action(self): 

73 """ 

74 :py:meth:`~.elasticsearch.client.ClusterClient.put_settings` to the cluster with 

75 :py:attr:`settings`. 

76 """ 

77 self.loggit.info('Updating cluster settings: %s', self.settings) 

78 try: 

79 self.client.cluster.put_settings(transient=self.settings) 

80 if self.wfc: 

81 self.loggit.debug( 

82 'Waiting for shards to complete routing and/or rebalancing' 

83 ) 

84 wait_for_it( 

85 self.client, 'cluster_routing', 

86 wait_interval=self.wait_interval, max_wait=self.max_wait 

87 ) 

88 # pylint: disable=broad-except 

89 except Exception as err: 

90 report_failure(err)