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

1"""Forcemerge action class"""

2 import logging

3 from time import sleep

4# pylint: disable=import-error

5 from curator.exceptions import MissingArgument

6 from curator.helpers.testers import verify_index_list

7 from curator.helpers.utils import report_failure, show_dry_run

8

9 class ForceMerge:

10 """ForceMerge Action Class"""

11 def __init__(self, ilo, max_num_segments=None, delay=0):

12 """

13 :param ilo: An IndexList Object

14 :param max_num_segments: Number of segments per shard to forceMerge

15 :param delay: Number of seconds to delay between forceMerge operations

16

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

18 :type max_num_segments: int

19 :type delay: int

20 """

21 verify_index_list(ilo)

22 if not max_num_segments:

23 raise MissingArgument('Missing value for "max_num_segments"')

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

25 self.index_list = ilo

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

27 #: :py:attr:`index_list`

28 self.client = ilo.client

29 #: Object attribute that gets the value of param ``max_num_segments``.

30 self.max_num_segments = max_num_segments

31 #: Object attribute that gets the value of param ``delay``.

32 self.delay = delay

33 self.loggit = logging.getLogger('curator.actions.forcemerge')

34

35 def do_dry_run(self):

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

37 show_dry_run(

38 self.index_list, 'forcemerge', max_num_segments=self.max_num_segments, delay=self.delay

39 )

40

41 def do_action(self):

42 """:py:meth:`~.elasticsearch.client.IndicesClient.forcemerge` indices in :py:attr:`index_list`"""

43 self.index_list.filter_closed()

44 self.index_list.filter_forceMerged(max_num_segments=self.max_num_segments)

45 self.index_list.empty_list_check()

46 msg = (

47 f'forceMerging {len(self.index_list.indices)} '

48 f'selected indices: {self.index_list.indices}'

49 )

50 self.loggit.info(msg)

51 try:

52 for index_name in self.index_list.indices:

53 msg = (

54 f'forceMerging index {index_name} to {self.max_num_segments} '

55 f'segments per shard. Please wait...'

56 )

57 self.loggit.info(msg)

58 self.client.indices.forcemerge(

59 index=index_name, max_num_segments=self.max_num_segments)

60 if self.delay > 0:

61 self.loggit.info('Pausing for %s seconds before continuing...', self.delay)

62 sleep(self.delay)

63 # pylint: disable=broad-except

64 except Exception as err:

65 report_failure(err)