Coverage for src/es_testbed/classes/entities/index.py: 53%
99 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-23 13:32 -0600
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-23 13:32 -0600
1"""Index Entity Class"""
2import typing as t
3from os import getenv
4from elasticsearch8 import Elasticsearch
5from es_wait import Exists
6from es_testbed.defaults import PAUSE_DEFAULT, PAUSE_ENVVAR
7from es_testbed.exceptions import NameChanged, ResultNotExpected
8from es_testbed.helpers import es_api
9from es_testbed.helpers.utils import getlogger, mounted_name
10from .entity import Entity
11# from ..entitymgrs import SnapshotMgr
12from ..ilm import IlmTracker
13PAUSE_VALUE = float(getenv(PAUSE_ENVVAR, default=PAUSE_DEFAULT))
15# pylint: disable=missing-docstring,too-many-arguments
17class Index(Entity):
18 def __init__(
19 self,
20 client: Elasticsearch = None,
21 name: str = None,
22 autobuild: t.Optional[bool] = True,
23 snapmgr = None,
24 policy_name: str = None,
25 ):
26 super().__init__(client=client, name=name, autobuild=autobuild)
27 self.logger = getlogger('es_testbed.Index')
28 self.policy_name = policy_name
29 self.ilm_tracker = None
30 self.snapmgr = snapmgr
31 self.track_ilm(self.name)
33 @property
34 def _get_target(self) -> str:
35 target = None
36 phases = self.ilm_tracker.policy_phases
37 curr = self.ilm_tracker.explain.phase
38 if not bool(('cold' in phases) or ('frozen' in phases)): 38 ↛ 41line 38 didn't jump to line 41, because the condition on line 38 was never false
39 self.logger.info('ILM Policy for "%s" has no cold/frozen phases', self.name)
40 target = curr # Keep the same
41 if bool(('cold' in phases) and ('frozen' in phases)): 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true
42 if self.ilm_tracker.pname(curr) < self.ilm_tracker.pname('cold'):
43 target = 'cold'
44 elif curr == 'cold':
45 target = 'frozen'
46 elif self.ilm_tracker.pname(curr) >= self.ilm_tracker.pname('frozen'):
47 target = curr
48 elif bool(('cold' in phases) and ('frozen' not in phases)): 48 ↛ 49line 48 didn't jump to line 49, because the condition on line 48 was never true
49 target = 'cold'
50 elif bool(('cold' not in phases) and ('frozen' in phases)): 50 ↛ 51line 50 didn't jump to line 51, because the condition on line 50 was never true
51 target = 'frozen'
52 return target
54 @property
55 def phase_tuple(self) -> t.Tuple[str, str]:
56 """Return the current phase and the target phase as a Tuple"""
57 return self.ilm_tracker.explain.phase, self._get_target
59 def _loop_until_target(self):
60 current, target = self.phase_tuple
61 while current != target:
62 self.logger.debug('Attempting to move %s to ILM phase %s', self.name, target)
63 self.ilm_tracker.advance(phase=target)
64 # At this point, it's "in" a searchable tier, but the index name hasn't changed yet
65 newidx = mounted_name(self.name, target)
66 self.logger.debug('Waiting for ILM phase change to complete. New index: %s', newidx)
67 test = Exists(self.client, name=newidx, kind='index', pause=PAUSE_VALUE)
68 test.wait_for_it()
69 self.logger.info('ILM advance to phase %s completed', target)
70 self.aka.append(self.name) # Append the old name to the AKA list
71 self.name = newidx
72 self.track_ilm(self.name) # Refresh the ilm_tracker with the new index name
73 current, target = self.phase_tuple
75 def manual_ss(self, scheme) -> None:
76 """If we are NOT using ILM but have specified searchable snapshots in the plan entities"""
77 if 'searchable' in scheme and scheme['searchable'] is not None: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 self.snapmgr.add(self.name, scheme['searchable'])
79 # Replace self.name with the renamed name
80 self.name = mounted_name(self.name, scheme['searchable'])
82 def mount_ss(self, scheme: dict) -> None:
83 """If the index is planned to become a searchable snapshot, we do that now"""
84 self.logger.debug('Checking if %s should be a searchable snapshot', self.name)
85 if self.am_i_write_idx:
86 self.logger.info(
87 '%s is the write_index. Cannot mount as searchable snapshot', self.name)
88 return
89 if not self.policy_name: # If we have this, chances are we have a policy
90 self.logger.debug('No ILM policy found. Switching to manual mode')
91 self.manual_ss(scheme)
92 return
93 current = self.ilm_tracker.explain.phase
94 target = self._get_target
95 if current != target: 95 ↛ 96line 95 didn't jump to line 96, because the condition on line 95 was never true
96 self.logger.debug('Attempting to move %s to ILM phase %s', self.name, target)
97 self.ilm_tracker.advance(phase=target)
98 # At this point, it's "in" a searchable tier, but the index name hasn't changed yet
99 newidx = mounted_name(self.name, target)
100 self.logger.debug('Waiting for ILM phase change to complete. New index: %s', newidx)
101 test = Exists(self.client, name=newidx, kind='index', pause=PAUSE_VALUE)
102 test.wait_for_it()
103 try:
104 self.ilm_tracker.wait4complete()
105 except NameChanged:
106 try:
107 self.track_ilm(newidx)
108 self.ilm_tracker.wait4complete()
109 except NameChanged as err:
110 self.logger.critical('Index name mismatch. Cannot continue')
111 raise ResultNotExpected from err
112 self.logger.info('ILM advance to phase %s completed', target)
113 self.logger.debug('Getting snapshot name for tracking...')
114 snapname = es_api.snapshot_name(self.client, newidx)
115 self.logger.debug('Snapshot %s backs %s', snapname, newidx)
116 self.snapmgr.add_existing(snapname)
117 self.aka.append(self.name) # Append the old name to the AKA list
118 self.name = newidx
119 self.track_ilm(self.name) # Refresh the ilm_tracker with the new index name
121 def track_ilm(self, name: str) -> None:
122 """
123 Get ILM phase information and put it in self.ilm_tracker
124 Name as an arg makes it configurable
125 """
126 if self.policy_name:
127 self.ilm_tracker = IlmTracker(self.client, name)
128 self.ilm_tracker.update()