Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/classes/entitymgrs/indexmgr.py: 95%
81 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-25 19:21 -0600
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-25 19:21 -0600
1"""Index Entity Manager Class"""
2import typing as t
3from dotmap import DotMap
4from elasticsearch8 import Elasticsearch
5from es_testbed.helpers import es_api
6from es_testbed.helpers.utils import getlogger, setting_component
7from .entitymgr import EntityMgr
8from .snapshotmgr import SnapshotMgr
9from ..entities import Alias, Index
11# pylint: disable=missing-docstring,too-many-arguments,broad-exception-caught,too-many-instance-attributes
13class IndexMgr(EntityMgr):
14 kind = 'index'
15 listname = 'indices'
16 def __init__(
17 self,
18 client: Elasticsearch = None,
19 plan: DotMap = None,
20 autobuild: t.Optional[bool] = True,
21 snapmgr: SnapshotMgr = None,
22 ):
23 self.doc_incr = 0
24 self.snapmgr = snapmgr
25 self.alias = None # Only used for tracking the rollover alias
26 super().__init__(client=client, plan=plan, autobuild=autobuild)
27 self.logger = getlogger('es_testbed.IndexMgr')
29 @property
30 def indexlist(self) -> t.Sequence[str]:
31 return [x.name for x in self.entity_list]
32 @property
33 def policy_name(self) -> str:
34 if len(self.plan.ilm_policies) > 0: 34 ↛ 36line 34 didn't jump to line 36, because the condition on line 34 was never false
35 return self.plan.ilm_policies[-1]
36 return None
38 def _rollover_path(self) -> None:
39 if not self.entity_list:
40 kw = {'ilm_policy': self.policy_name,
41 'rollover_alias': self.plan.rollover_alias}
42 cfg = setting_component(**kw)['settings']
43 acfg = {self.plan.rollover_alias: {'is_write_index': True}}
44 self.logger.debug('No indices created yet. Starting with a rollover alias index...')
45 es_api.create_index(self.client, self.name, aliases=acfg, settings=cfg)
46 self.logger.debug(
47 'Created %s with rollover alias %s', self.name, self.plan.rollover_alias)
48 self.track_alias()
49 else:
50 self.alias.rollover()
51 if self.policy_name: # We have an ILM policy
52 self.logger.debug('Going to wait now...')
53 self.last.ilm_tracker.wait4complete()
54 self.logger.debug('The wait is over!')
56 def add(self, value) -> None:
57 # In this case, value is a single array element from plan.entities
58 self.logger.debug('Creating index: %s', value)
59 es_api.create_index(self.client, value)
61 def add_indices(self) -> None:
62 for scheme in self.plan.entities:
63 if self.plan.rollover_alias:
64 self._rollover_path()
65 else:
66 self.add(self.name)
67 self.filler(scheme)
68 self.track_index(self.name)
69 self.logger.debug('Created indices: %s', self.indexlist)
70 if self.plan.rollover_alias:
71 if not self.alias.verify(self.indexlist): 71 ↛ 72line 71 didn't jump to line 72, because the condition on line 71 was never true
72 self.logger.error(
73 'Unable to confirm rollover of alias "%s" was successfully executed')
75 def filler(self, scheme) -> None:
76 """If the scheme from the TestPlan says to write docs, do it"""
77 # scheme is a single array element from plan.entities
78 self.logger.debug('Adding docs to %s', self.name)
79 if scheme['docs'] > 0: 79 ↛ 87line 79 didn't jump to line 87, because the condition on line 79 was never false
80 es_api.fill_index(
81 self.client,
82 name=self.name,
83 count=scheme['docs'],
84 start_num=self.doc_incr,
85 match=scheme['match']
86 )
87 self.doc_incr += scheme['docs']
89 def searchable(self) -> None:
90 """If the indices were marked as searchable snapshots, we do that now"""
91 for idx, scheme in enumerate(self.plan.entities):
92 old = self.entity_list[idx].name
93 self.entity_list[idx].mount_ss(scheme)
94 new = self.entity_list[idx].name
95 # Replace the old index name in self.failsafe with the new one at the same list position
96 pos = [i for i, value in enumerate(self.failsafe) if value == old]
97 self.failsafe[pos[0]] = new
100 def setup(self) -> None:
101 self.logger.debug('Beginning setup...')
102 if self.plan.rollover_alias:
103 self.logger.debug('rollover_alias is True...')
104 self.add_indices()
105 self.searchable()
106 self.logger.info('Successfully created indices: %s', self.indexlist)
107 self.success = True
109 def track_alias(self) -> None:
110 self.logger.debug('Tracking alias: %s', self.plan.rollover_alias)
111 self.alias = Alias(client=self.client, name=self.plan.rollover_alias)
113 def track_index(self, name: str) -> None:
114 self.logger.debug('Tracking index: %s', name)
115 entity = Index(
116 client=self.client,
117 name=name,
118 snapmgr=self.snapmgr,
119 policy_name=self.policy_name
120 )
121 self.failsafe.append(name)
122 self.entity_list.append(entity)