Coverage for /Users/buh/.pyenv/versions/3.12.9/envs/es-testbed/lib/python3.12/site-packages/es_testbed/mgrs/index.py: 98%

88 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-31 12:31 -0600

1"""Index Entity Manager Class""" 

2 

3import typing as t 

4import logging 

5from importlib import import_module 

6import tiered_debug as debug 

7from es_testbed.entities import Alias, Index 

8from es_testbed.helpers.es_api import create_index, fill_index 

9from es_testbed.helpers.utils import prettystr 

10from es_testbed.mgrs.entity import EntityMgr 

11from es_testbed.mgrs.snapshot import SnapshotMgr 

12 

13if t.TYPE_CHECKING: 

14 from elasticsearch8 import Elasticsearch 

15 from dotmap import DotMap 

16 

17logger = logging.getLogger(__name__) 

18 

19 

20class IndexMgr(EntityMgr): 

21 """Index Entity Manager Class""" 

22 

23 kind = 'index' 

24 listname = 'indices' 

25 

26 def __init__( 

27 self, 

28 client: t.Optional['Elasticsearch'] = None, 

29 plan: t.Optional['DotMap'] = None, 

30 snapmgr: t.Optional[SnapshotMgr] = None, 

31 ): 

32 self.snapmgr = snapmgr 

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

34 debug.lv2('Initializing IndexMgr object...') 

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

36 debug.lv3('IndexMgr object initialized') 

37 

38 @property 

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

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

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

42 

43 @property 

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

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

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

47 return self.plan.ilm_policies[-1] 

48 return None 

49 

50 def _rollover_path(self) -> None: 

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

52 debug.lv2('Starting method...') 

53 if not self.entity_list: 

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

55 debug.lv5(f'Creating index with config: {acfg}') 

56 create_index(self.client, self.name, aliases=acfg) 

57 self.track_alias() 

58 else: 

59 self.alias.rollover() 

60 debug.lv5('Rolled over index with alias') 

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

62 kw = {'phase': 'hot', 'action': 'complete', 'name': 'complete'} 

63 debug.lv5(f'Advancing ILM policy with config: {kw}') 

64 self.last.ilm_tracker.advance(**kw) 

65 debug.lv3('Exiting method') 

66 

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

68 """Create a single index""" 

69 debug.lv2('Starting method...') 

70 debug.lv1(f'Creating index: "{value}"') 

71 create_index(self.client, value) 

72 debug.lv3('Exiting method') 

73 

74 def add_indices(self) -> None: 

75 """Add indices according to plan""" 

76 debug.lv2('Starting method...') 

77 mod = import_module(f'{self.plan.modpath}.functions') 

78 func = getattr(mod, 'doc_generator') 

79 for scheme in self.plan.index_buildlist: 

80 if self.plan.rollover_alias: 

81 self._rollover_path() 

82 else: 

83 self.add(self.name) 

84 # self.filler(scheme) 

85 fill_index( 

86 self.client, 

87 name=self.name, 

88 doc_generator=func, 

89 options=scheme['options'], 

90 ) 

91 self.track_index(self.name) 

92 debug.lv2(f'Created indices: {prettystr(self.indexlist)}') 

93 if self.plan.rollover_alias: 

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

95 logger.error( 

96 f'Unable to confirm rollover of alias ' 

97 f'"{self.plan.rollover_alias}" was successful' 

98 ) 

99 debug.lv3('Exiting method') 

100 

101 def searchable(self) -> None: 

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

103 debug.lv2('Starting method...') 

104 for idx, scheme in enumerate(self.plan.index_buildlist): 

105 if scheme['target_tier'] in ['cold', 'frozen']: 

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

107 debug.lv3('Exiting method') 

108 

109 def setup(self) -> None: 

110 """Setup the entity manager""" 

111 debug.lv2('Starting method...') 

112 debug.lv5(f'PLAN: {prettystr(self.plan.toDict())}') 

113 if self.plan.rollover_alias: 

114 debug.lv3('rollover_alias is True...') 

115 self.add_indices() 

116 self.searchable() 

117 logger.info(f'Successfully created indices: {prettystr(self.indexlist)}') 

118 debug.lv3('Exiting method') 

119 

120 def track_alias(self) -> None: 

121 """Track a rollover alias""" 

122 debug.lv2('Starting method...') 

123 debug.lv3(f'Tracking alias: {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 debug.lv2('Starting method...') 

129 debug.lv3(f'Tracking index: {name}') 

130 entity = Index( 

131 client=self.client, 

132 name=name, 

133 snapmgr=self.snapmgr, 

134 policy_name=self.policy_name, 

135 ) 

136 entity.track_ilm(self.name) 

137 self.entity_list.append(entity) 

138 debug.lv3('Exiting method')