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

82 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 13:33 -0600

1"""Index Entity Manager Class""" 

2 

3import typing as t 

4from es_testbed.mgrs.entitymgr import EntityMgr 

5from es_testbed.mgrs.snapshotmgr import SnapshotMgr 

6from es_testbed.entities import Alias, Index 

7from es_testbed.helpers.es_api import create_index, fill_index 

8from es_testbed.helpers.utils import getlogger, prettystr, setting_component 

9 

10if t.TYPE_CHECKING: 

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 snapmgr: t.Union[SnapshotMgr, None] = None, 

26 ): 

27 self.doc_incr = 0 

28 self.snapmgr = snapmgr 

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

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

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

32 

33 @property 

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

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

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

37 

38 @property 

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

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

41 if len(self.plan.ilm_policies) > 0: 

42 return self.plan.ilm_policies[-1] 

43 return None 

44 

45 def _rollover_path(self) -> None: 

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

47 if not self.entity_list: 

48 kw = { 

49 'ilm_policy': self.policy_name, 

50 'rollover_alias': self.plan.rollover_alias, 

51 } 

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

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

54 self.logger.debug( 

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

56 ) 

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

58 self.logger.debug( 

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

60 ) 

61 self.track_alias() 

62 else: 

63 self.alias.rollover() 

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

65 if self.last.ilm_tracker.explain.phase == 'new': 

66 self.last.ilm_tracker.wait4complete() 

67 self.last.ilm_tracker.update() 

68 if not self.last.ilm_tracker.explain.phase == 'hot': 

69 self.last.ilm_tracker.advance( 

70 phase='hot', action='complete', name='complete' 

71 ) 

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

73 self.last.ilm_tracker.wait4complete() 

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

75 

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

77 """Create a single index""" 

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

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

80 create_index(self.client, value) 

81 

82 def add_indices(self) -> None: 

83 """Add indices according to plan""" 

84 for scheme in self.plan.entities: 

85 if self.plan.rollover_alias: 

86 self._rollover_path() 

87 else: 

88 self.add(self.name) 

89 self.filler(scheme) 

90 self.track_index(self.name) 

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

92 if self.plan.rollover_alias: 

93 if not self.alias.verify(self.indexlist): 

94 self.logger.error( 

95 'Unable to confirm rollover of alias "%s" was successful', 

96 self.plan.rollover_alias, 

97 ) 

98 

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

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

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

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

103 if scheme['docs'] > 0: 

104 fill_index( 

105 self.client, 

106 name=self.name, 

107 count=scheme['docs'], 

108 start_num=self.doc_incr, 

109 match=scheme['match'], 

110 ) 

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

112 

113 def searchable(self) -> None: 

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

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

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

117 

118 def setup(self) -> None: 

119 """Setup the entity manager""" 

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

121 self.logger.debug('PLAN: %s', prettystr(self.plan.toDict())) 

122 if self.plan.rollover_alias: 

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

124 self.add_indices() 

125 self.searchable() 

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

127 self.success = True 

128 

129 def track_alias(self) -> None: 

130 """Track a rollover alias""" 

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

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

133 

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

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

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

137 entity = Index( 

138 client=self.client, 

139 name=name, 

140 snapmgr=self.snapmgr, 

141 policy_name=self.policy_name, 

142 ) 

143 entity.track_ilm(self.name) 

144 self.entity_list.append(entity)