Coverage for src/es_testbed/classes/entitymgrs/indexmgr.py: 83%
77 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-23 15:01 -0600
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-23 15:01 -0600
1"""Index Entity Manager Class"""
2import typing as t
3from elasticsearch8 import Elasticsearch
4from es_testbed.helpers import es_api
5from es_testbed.helpers.utils import getlogger, setting_component
6from .entitymgr import EntityMgr
7from .snapshotmgr import SnapshotMgr
8from ..entities import Alias, DataStream, Index
9from ..testplan import TestPlan
11# pylint: disable=missing-docstring,too-many-arguments,broad-exception-caught,too-many-instance-attributes
13class IndexMgr(EntityMgr):
14 def __init__(
15 self,
16 client: Elasticsearch = None,
17 plan: TestPlan = None,
18 autobuild: t.Optional[bool] = True,
19 snapmgr: SnapshotMgr = None,
20 policy_name: str = None,
21 ):
22 super().__init__(client=client, plan=plan, autobuild=autobuild)
23 self.kind = 'index'
24 self.logger = getlogger('es_testbed.IndexMgr')
25 self.snapmgr = snapmgr
26 self.policy_name = policy_name
27 self.alias: Alias = None # Only used for tracking the rollover alias
28 self.ds: DataStream = None # Only used for child class DataStreamMgr
29 self.index_trackers: t.Sequence[Index] = [] # Only used for child class DataStreamMgr
30 self.doc_incr = 0
31 if self.autobuild: 31 ↛ exitline 31 didn't return from function '__init__', because the condition on line 31 was never false
32 self.setup()
34 @property
35 def aliasname(self) -> str:
36 return f'{self.plan.prefix}-{self.ident()}-{self.plan.uniq}'
38 @property
39 def indexlist(self) -> t.Sequence[str]:
40 return [x.name for x in self.entity_list]
42 def add(self, value) -> None:
43 # In this case, value is a single array element from plan.entities
44 self.logger.debug('Creating index: %s', value)
45 es_api.create_index(self.client, value)
46 self.filler(value)
47 self.track_index(value)
49 def add_rollover(self) -> None:
50 settings = setting_component(
51 ilm_policy=self.policy_name, rollover_alias=self.aliasname)['settings']
52 aliascfg = {self.aliasname: {'is_write_index': True}}
53 for scheme in self.plan.entities:
54 if not self.entity_list:
55 self.logger.debug('No indices created yet. Starting with a rollover alias index...')
56 es_api.create_index(
57 self.client, self.name, aliases=aliascfg, settings=settings)
58 self.logger.debug('Created %s with rollover alias %s', self.name, self.aliasname)
59 self.track_alias()
60 else:
61 self.alias.rollover()
62 if self.policy_name: 62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true
63 self.logger.debug('Going to wait now...')
64 self.last.ilm_tracker.wait4complete()
65 self.logger.debug('The wait is over!')
66 self.filler(scheme)
67 self.track_index(self.name)
68 created = [x.name for x in self.entity_list]
69 self.logger.debug('Created indices: %s', created)
70 if not self.alias.verify(created): 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true
71 self.logger.error('Unable to confirm rollover of alias "%s" was successfully executed')
73 def filler(self, scheme) -> None:
74 """If the scheme from the TestPlan says to write docs, do it"""
75 # scheme is a single array element from plan.entities
76 self.logger.debug('Adding docs to %s', self.name)
77 if scheme['docs'] > 0: 77 ↛ 85line 77 didn't jump to line 85, because the condition on line 77 was never false
78 es_api.fill_index(
79 self.client,
80 name=self.name,
81 count=scheme['docs'],
82 start_num=self.doc_incr,
83 match=scheme['match']
84 )
85 self.doc_incr += scheme['docs']
87 def searchable(self) -> None:
88 """If the indices were marked as searchable snapshots, we do that now"""
89 for idx, scheme in enumerate(self.plan.entities):
90 self.entity_list[idx].mount_ss(scheme)
92 def setup(self) -> None:
93 self.logger.debug('Beginning setup...')
94 if self.plan.rollover_alias: 94 ↛ 98line 94 didn't jump to line 98, because the condition on line 94 was never false
95 self.logger.debug('rollover_alias is True...')
96 self.add_rollover()
97 else:
98 for scheme in self.plan.entities:
99 self.add(scheme)
100 self.searchable()
101 self.logger.info('Successfully created indices: %s', self.indexlist)
102 self.success = True
104 def track_alias(self) -> None:
105 self.logger.debug('Tracking alias: %s', self.aliasname)
106 self.alias = Alias(client=self.client, name=self.aliasname)
108 def track_index(self, name: str) -> None:
109 self.logger.debug('Tracking index: %s', name)
110 entity = Index(
111 client=self.client,
112 name=name,
113 snapmgr=self.snapmgr,
114 policy_name=self.policy_name
115 )
116 self.entity_list.append(entity)