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

1"""Index replica count action class"""

2 import logging

3 from curator.exceptions import MissingArgument

4 from curator.helpers.testers import verify_index_list

5 from curator.helpers.utils import chunk_index_list, report_failure, show_dry_run, to_csv

6 from curator.helpers.waiters import wait_for_it

7

8 class Replicas:

9 """Replica Action Class"""

10 def __init__(self, ilo, count=None, wait_for_completion=False, wait_interval=9, max_wait=-1):

11 """

12 :param ilo: An IndexList Object

13 :param count: The count of replicas per shard

14 :param wait_for_completion: Wait for completion before returning.

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

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

17

18 :type ilo: :py:class:`~.curator.indexlist.IndexList`

19 :type count: int

20 :type wait_for_completion: bool

21 :type wait_interval: int

22 :type max_wait: int

23 """

24 verify_index_list(ilo)

25 # It's okay for count to be zero

26 if count == 0:

27 pass

28 elif not count:

29 raise MissingArgument('Missing value for "count"')

30

31 #: The :py:class:`~.curator.indexlist.IndexList` object passed from param ``ilo``

32 self.index_list = ilo

33 #: The :py:class:`~.elasticsearch.Elasticsearch` client object derived from

34 #: :py:attr:`index_list`

35 self.client = ilo.client

36 #: Object attribute that gets the value of param ``count``.

37 self.count = count

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

39 self.wfc = wait_for_completion

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

41 self.wait_interval = wait_interval

42 #: Object attribute that gets the value of param ``max_wait``.

43 self.max_wait = max_wait

44 self.loggit = logging.getLogger('curator.actions.replicas')

45

46 def do_dry_run(self):

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

48 show_dry_run(self.index_list, 'replicas', count=self.count)

49

50 def do_action(self):

51 """

52 Update ``number_of_replicas`` with :py:attr:`count` and

53 :py:meth:`~.elasticsearch.client.IndicesClient.put_settings` to indices in

54 :py:attr:`index_list`

55 """

56 self.loggit.debug(

57 'Cannot get update replica count of closed indices. Omitting any closed indices.')

58 self.index_list.filter_closed()

59 self.index_list.empty_list_check()

60 msg = (

61 f'Setting the replica count to {self.count} for {len(self.index_list.indices)} '

62 f'indices: {self.index_list.indices}'

63 )

64 self.loggit.info(msg)

65 try:

66 index_lists = chunk_index_list(self.index_list.indices)

67 for lst in index_lists:

68 self.client.indices.put_settings(

69 index=to_csv(lst), settings={'number_of_replicas': self.count}

70 )

71 if self.wfc and self.count > 0:

72 msg = (

73 f'Waiting for shards to complete replication for indices: {to_csv(lst)}')

74 self.loggit.debug(msg)

75 wait_for_it(

76 self.client, 'replicas',

77 wait_interval=self.wait_interval, max_wait=self.max_wait

78 )

79 # pylint: disable=broad-except

80 except Exception as err:

81 report_failure(err)