@@ -54,13 +54,13 @@ def create_datetime_index_sync(
54
54
) -> str :
55
55
pass
56
56
57
- async def create_index_alias (self , client : Any , collection_id : str , end_date : str ):
57
+ async def update_index_alias (self , client : Any , collection_id : str , end_date : str ):
58
58
index = index_alias_by_collection_id (collection_id )
59
59
await client .indices .put_alias (
60
60
index = index , name = self .alias_by_index_and_end_date (index , end_date )
61
61
)
62
62
63
- def create_index_alias_sync (
63
+ def update_index_alias_sync (
64
64
self , sync_client : Any , collection_id : str , end_date : str
65
65
):
66
66
index = index_alias_by_collection_id (collection_id )
@@ -91,11 +91,12 @@ async def create_datetime_index(
91
91
self , client : Any , collection_id : str , start_date : str
92
92
) -> str :
93
93
index_name = self .index_by_collection_id_and_date (collection_id , start_date )
94
+ alias_name = index_name .removeprefix (ITEMS_INDEX_PREFIX )
94
95
await client .options (ignore_status = 400 ).indices .create (
95
96
index = index_name ,
96
- body = {"aliases" : {index_alias_by_collection_id (collection_id ): {}}},
97
+ body = {"aliases" : {index_alias_by_collection_id (collection_id ): {}, alias_name : {} }},
97
98
)
98
- return index_name
99
+ return alias_name
99
100
100
101
def create_simple_index_sync (self , sync_client : Any , collection_id : str ) -> str :
101
102
index_name = f"{ index_by_collection_id (collection_id )} -000001"
@@ -109,11 +110,12 @@ def create_datetime_index_sync(
109
110
self , sync_client : Any , collection_id : str , start_date : str
110
111
) -> str :
111
112
index_name = self .index_by_collection_id_and_date (collection_id , start_date )
113
+ alias_name = index_name .removeprefix (ITEMS_INDEX_PREFIX )
112
114
sync_client .options (ignore_status = 400 ).indices .create (
113
115
index = index_name ,
114
- body = {"aliases" : {index_alias_by_collection_id (collection_id ): {}}},
116
+ body = {"aliases" : {index_alias_by_collection_id (collection_id ): {}, alias_name : {} }},
115
117
)
116
- return index_name
118
+ return alias_name
117
119
118
120
119
121
class OpenSearchAdapter (SearchEngineAdapter ):
@@ -135,15 +137,16 @@ async def create_datetime_index(
135
137
self , client : Any , collection_id : str , start_date : str
136
138
) -> str :
137
139
index_name = self .index_by_collection_id_and_date (collection_id , start_date )
140
+ alias_name = index_name .removeprefix (ITEMS_INDEX_PREFIX )
138
141
await client .indices .create (
139
142
index = index_name ,
140
143
body = {
141
- "aliases" : {index_alias_by_collection_id (collection_id ): {}},
144
+ "aliases" : {index_alias_by_collection_id (collection_id ): {}, alias_name : {} },
142
145
"mappings" : ES_ITEMS_MAPPINGS ,
143
146
"settings" : ES_ITEMS_SETTINGS ,
144
147
},
145
148
)
146
- return index_name
149
+ return alias_name
147
150
148
151
def create_simple_index_sync (self , sync_client : Any , collection_id : str ) -> str :
149
152
index_name = f"{ index_by_collection_id (collection_id )} -000001"
@@ -163,15 +166,16 @@ def create_datetime_index_sync(
163
166
self , sync_client : Any , collection_id : str , start_date : str
164
167
) -> str :
165
168
index_name = self .index_by_collection_id_and_date (collection_id , start_date )
169
+ alias_name = index_name .removeprefix (ITEMS_INDEX_PREFIX )
166
170
sync_client .indices .create (
167
171
index = index_name ,
168
172
body = {
169
- "aliases" : {index_alias_by_collection_id (collection_id ): {}},
173
+ "aliases" : {index_alias_by_collection_id (collection_id ): {}, alias_name : {} },
170
174
"mappings" : ES_ITEMS_MAPPINGS ,
171
175
"settings" : ES_ITEMS_SETTINGS ,
172
176
},
173
177
)
174
- return index_name
178
+ return alias_name
175
179
176
180
177
181
class SearchEngineAdapterFactory :
@@ -217,6 +221,10 @@ def should_create_collection_index(self) -> bool:
217
221
async def create_simple_index (self , client : Any , collection_id : str ):
218
222
return await self .search_adapter .create_simple_index (client , collection_id )
219
223
224
+ async def get_index_size_in_gb (self , index_name : str ) -> float :
225
+ data = await self .client .indices .stats (index = index_name )
226
+ return data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
227
+
220
228
async def _get_target_index_base (
221
229
self ,
222
230
index_selector ,
@@ -243,16 +251,14 @@ async def _get_target_index_base(
243
251
return target_index
244
252
245
253
if check_size :
246
- data = await self .client .indices .stats (index = target_index )
247
- current_size_in_gb = (
248
- data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
249
- )
254
+ breakpoint ()
255
+ index_size_gb = await self .get_index_size_in_gb (target_index )
250
256
max_size_gb = float (os .getenv ("DATETIME_INDEX_MAX_SIZE_GB" , 20 ))
251
257
252
- if current_size_in_gb > max_size_gb :
258
+ if index_size_gb > max_size_gb :
253
259
end_date = extract_date (product_datetime )
254
260
if end_date != extract_date_from_index (all_indexes [- 1 ]):
255
- await self .search_adapter .create_index_alias (
261
+ await self .search_adapter .update_index_alias (
256
262
self .client , collection_id , str (end_date )
257
263
)
258
264
target_index = await self .search_adapter .create_datetime_index (
@@ -298,18 +304,15 @@ async def prepare_bulk_actions(
298
304
new_index = None
299
305
300
306
if first_item_index == latest_index :
301
- data = await self .client .indices .stats (index = first_item_index )
302
- current_size_in_gb = (
303
- data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
304
- )
307
+ index_size_gb = await self .get_index_size_in_gb (first_item_index )
305
308
max_size_gb = float (os .getenv ("DATETIME_INDEX_MAX_SIZE_GB" , 20 ))
306
309
307
- if current_size_in_gb > max_size_gb :
310
+ if index_size_gb > max_size_gb :
308
311
current_index_end_date = extract_date_from_index (first_item_index )
309
312
first_item_date = extract_date (first_item ["properties" ]["datetime" ])
310
313
311
314
if first_item_date != current_index_end_date :
312
- await self .search_adapter .create_index_alias (
315
+ await self .search_adapter .update_index_alias (
313
316
self .client , collection_id , str (current_index_end_date )
314
317
)
315
318
next_day_start = current_index_end_date + timedelta (days = 1 )
@@ -348,6 +351,10 @@ def __init__(self, sync_client, search_adapter: SearchEngineAdapter):
348
351
def should_create_collection_index (self ) -> bool :
349
352
return False
350
353
354
+ def get_index_size_in_gb (self , index_name : str ) -> float :
355
+ data = self .sync_client .indices .stats (index = index_name )
356
+ return data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
357
+
351
358
def _get_target_index_base (
352
359
self ,
353
360
index_selector ,
@@ -372,16 +379,13 @@ def _get_target_index_base(
372
379
return target_index
373
380
374
381
if check_size :
375
- data = self .sync_client .indices .stats (index = target_index )
376
- current_size_in_gb = (
377
- data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
378
- )
382
+ index_size_gb = self .get_index_size_in_gb (target_index )
379
383
max_size_gb = float (os .getenv ("DATETIME_INDEX_MAX_SIZE_GB" , 20 ))
380
384
381
- if current_size_in_gb > max_size_gb :
385
+ if index_size_gb > max_size_gb :
382
386
end_date = extract_date (product_datetime )
383
387
if end_date != extract_date_from_index (all_indexes [- 1 ]):
384
- self .search_adapter .create_index_alias_sync (
388
+ self .search_adapter .update_index_alias_sync (
385
389
self .sync_client , collection_id , str (end_date )
386
390
)
387
391
target_index = self .search_adapter .create_datetime_index_sync (
@@ -425,18 +429,14 @@ def prepare_bulk_actions(
425
429
new_index = None
426
430
427
431
if first_item_index == latest_index :
428
- data = self .sync_client .indices .stats (index = first_item_index )
429
- current_size_in_gb = (
430
- data ["_all" ]["primaries" ]["store" ]["size_in_bytes" ] / 1e9
431
- )
432
+ index_size_gb = self .get_index_size_in_gb (first_item_index )
432
433
max_size_gb = float (os .getenv ("DATETIME_INDEX_MAX_SIZE_GB" , 20 ))
433
-
434
- if current_size_in_gb > max_size_gb :
434
+ if index_size_gb > max_size_gb :
435
435
current_index_end_date = extract_date_from_index (first_item_index )
436
436
first_item_date = extract_date (first_item ["properties" ]["datetime" ])
437
437
438
438
if first_item_date != current_index_end_date :
439
- self .search_adapter .create_index_alias_sync (
439
+ self .search_adapter .update_index_alias_sync (
440
440
self .sync_client , collection_id , str (current_index_end_date )
441
441
)
442
442
next_day_start = current_index_end_date + timedelta (days = 1 )
0 commit comments