Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/classes/entitymgrs/indexmgr.py: 92%

77 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-27 20:59 -0600

1"""Index Entity Manager Class""" 

2 

3import typing as t 

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, Index 

9 

10if t.TYPE_CHECKING: 10 ↛ 11line 10 didn't jump to line 11, because the condition on line 10 was never true

11 from elasticsearch8 import Elasticsearch 

12 from dotmap import DotMap 

13 

14# pylint: disable=missing-docstring 

15 

16 

17class IndexMgr(EntityMgr): 

18 kind = 'index' 

19 listname = 'indices' 

20 

21 def __init__( 

22 self, 

23 client: t.Union['Elasticsearch', None] = None, 

24 plan: t.Union['DotMap', None] = None, 

25 autobuild: t.Optional[bool] = True, 

26 snapmgr: t.Union[SnapshotMgr, None] = None, 

27 ): 

28 self.doc_incr = 0 

29 self.snapmgr = snapmgr 

30 self.alias = None # Only used for tracking the rollover alias 

31 super().__init__(client=client, plan=plan, autobuild=autobuild) 

32 self.logger = getlogger('es_testbed.IndexMgr') 

33 

34 @property 

35 def indexlist(self) -> t.Sequence[str]: 

36 """Return a list of index names currently being managed""" 

37 return [x.name for x in self.entity_list] 

38 

39 @property 

40 def policy_name(self) -> t.Union[str, None]: 

41 """Return the name of the ILM policy, if it exists""" 

42 if len(self.plan.ilm_policies) > 0: 42 ↛ 44line 42 didn't jump to line 44, because the condition on line 42 was never false

43 return self.plan.ilm_policies[-1] 

44 return None 

45 

46 def _rollover_path(self) -> None: 

47 """This is the execution path for rollover indices""" 

48 if not self.entity_list: 

49 kw = { 

50 'ilm_policy': self.policy_name, 

51 'rollover_alias': self.plan.rollover_alias, 

52 } 

53 cfg = setting_component(**kw)['settings'] 

54 acfg = {self.plan.rollover_alias: {'is_write_index': True}} 

55 self.logger.debug( 

56 'No indices created yet. Starting with a rollover alias index...' 

57 ) 

58 es_api.create_index(self.client, self.name, aliases=acfg, settings=cfg) 

59 self.logger.debug( 

60 'Created %s with rollover alias %s', self.name, self.plan.rollover_alias 

61 ) 

62 self.track_alias() 

63 else: 

64 self.alias.rollover() 

65 if self.policy_name: # We have an ILM policy 

66 self.logger.debug('Going to wait now...') 

67 self.last.ilm_tracker.wait4complete() 

68 self.logger.debug('The wait is over!') 

69 

70 def add(self, value) -> None: 

71 """Create a single index""" 

72 # In this case, value is a single array element from plan.entities 

73 self.logger.debug('Creating index: %s', value) 

74 es_api.create_index(self.client, value) 

75 

76 def add_indices(self) -> None: 

77 """Add indices according to plan""" 

78 for scheme in self.plan.entities: 

79 if self.plan.rollover_alias: 

80 self._rollover_path() 

81 else: 

82 self.add(self.name) 

83 self.filler(scheme) 

84 self.track_index(self.name) 

85 self.logger.debug('Created indices: %s', self.indexlist) 

86 if self.plan.rollover_alias: 

87 if not self.alias.verify(self.indexlist): 87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never true

88 self.logger.error( 

89 'Unable to confirm rollover of alias "%s" was successfully executed' 

90 ) 

91 

92 def filler(self, scheme) -> None: 

93 """If the scheme from the TestPlan says to write docs, do it""" 

94 # scheme is a single array element from plan.entities 

95 self.logger.debug('Adding docs to %s', self.name) 

96 if scheme['docs'] > 0: 96 ↛ 104line 96 didn't jump to line 104, because the condition on line 96 was never false

97 es_api.fill_index( 

98 self.client, 

99 name=self.name, 

100 count=scheme['docs'], 

101 start_num=self.doc_incr, 

102 match=scheme['match'], 

103 ) 

104 self.doc_incr += scheme['docs'] 

105 

106 def searchable(self) -> None: 

107 """If the indices were marked as searchable snapshots, we do that now""" 

108 for idx, scheme in enumerate(self.plan.entities): 

109 self.entity_list[idx].mount_ss(scheme) 

110 

111 def setup(self) -> None: 

112 """Setup the entity manager""" 

113 self.logger.debug('Beginning setup...') 

114 if self.plan.rollover_alias: 

115 self.logger.debug('rollover_alias is True...') 

116 self.add_indices() 

117 self.searchable() 

118 self.logger.info('Successfully created indices: %s', self.indexlist) 

119 self.success = True 

120 

121 def track_alias(self) -> None: 

122 """Track a rollover alias""" 

123 self.logger.debug('Tracking alias: %s', self.plan.rollover_alias) 

124 self.alias = Alias(client=self.client, name=self.plan.rollover_alias) 

125 

126 def track_index(self, name: str) -> None: 

127 """Track an index and append that tracking entity to entity_list""" 

128 self.logger.debug('Tracking index: %s', name) 

129 entity = Index( 

130 client=self.client, 

131 name=name, 

132 snapmgr=self.snapmgr, 

133 policy_name=self.policy_name, 

134 ) 

135 self.entity_list.append(entity)