3
3
# or more contributor license agreements. Licensed under the Elastic License 2.0;
4
4
# you may not use this file except in compliance with the Elastic License 2.0.
5
5
#
6
+ from functools import partial
7
+
6
8
from elasticsearch import ApiError
7
9
8
10
from connectors .es import ESClient
@@ -50,10 +52,14 @@ async def fetch_by_id(self, doc_id):
50
52
51
53
async def fetch_response_by_id (self , doc_id ):
52
54
if not self .serverless :
53
- await self .client .indices .refresh (index = self .index_name )
55
+ await self ._retrier .execute_with_retry (
56
+ partial (self .client .indices .refresh , index = self .index_name )
57
+ )
54
58
55
59
try :
56
- resp = await self .client .get (index = self .index_name , id = doc_id )
60
+ resp = await self ._retrier .execute_with_retry (
61
+ partial (self .client .get , index = self .index_name , id = doc_id )
62
+ )
57
63
except ApiError as e :
58
64
logger .critical (f"The server returned { e .status_code } " )
59
65
logger .critical (e .body , exc_info = True )
@@ -65,30 +71,41 @@ async def fetch_response_by_id(self, doc_id):
65
71
return resp .body
66
72
67
73
async def index (self , doc ):
68
- return await self .client .index (index = self .index_name , document = doc )
74
+ return await self ._retrier .execute_with_retry (
75
+ partial (self .client .index , index = self .index_name , document = doc )
76
+ )
69
77
70
78
async def clean_index (self ):
71
- return await self .client .delete_by_query (
72
- index = self .index_name ,
73
- body = {"query" : {"match_all" : {}}},
74
- ignore_unavailable = True ,
75
- conflicts = "proceed" ,
79
+ return await self ._retrier .execute_with_retry (
80
+ partial (
81
+ self .client .delete_by_query ,
82
+ index = self .index_name ,
83
+ body = {"query" : {"match_all" : {}}},
84
+ ignore_unavailable = True ,
85
+ conflicts = "proceed" ,
86
+ )
76
87
)
77
88
78
89
async def update (self , doc_id , doc , if_seq_no = None , if_primary_term = None ):
79
- return await self .client .update (
80
- index = self .index_name ,
81
- id = doc_id ,
82
- doc = doc ,
83
- if_seq_no = if_seq_no ,
84
- if_primary_term = if_primary_term ,
90
+ return await self ._retrier .execute_with_retry (
91
+ partial (
92
+ self .client .update ,
93
+ index = self .index_name ,
94
+ id = doc_id ,
95
+ doc = doc ,
96
+ if_seq_no = if_seq_no ,
97
+ if_primary_term = if_primary_term ,
98
+ )
85
99
)
86
100
87
101
async def update_by_script (self , doc_id , script ):
88
- return await self .client .update (
89
- index = self .index_name ,
90
- id = doc_id ,
91
- script = script ,
102
+ return await self ._retrier .execute_with_retry (
103
+ partial (
104
+ self .client .update ,
105
+ index = self .index_name ,
106
+ id = doc_id ,
107
+ script = script ,
108
+ )
92
109
)
93
110
94
111
async def get_all_docs (self , query = None , sort = None , page_size = DEFAULT_PAGE_SIZE ):
@@ -103,7 +120,9 @@ async def get_all_docs(self, query=None, sort=None, page_size=DEFAULT_PAGE_SIZE)
103
120
Iterator
104
121
"""
105
122
if not self .serverless :
106
- await self .client .indices .refresh (index = self .index_name )
123
+ await self ._retrier .execute_with_retry (
124
+ partial (self .client .indices .refresh , index = self .index_name )
125
+ )
107
126
108
127
if query is None :
109
128
query = {"match_all" : {}}
@@ -113,14 +132,17 @@ async def get_all_docs(self, query=None, sort=None, page_size=DEFAULT_PAGE_SIZE)
113
132
114
133
while True :
115
134
try :
116
- resp = await self .client .search (
117
- index = self .index_name ,
118
- query = query ,
119
- sort = sort ,
120
- from_ = offset ,
121
- size = page_size ,
122
- expand_wildcards = "hidden" ,
123
- seq_no_primary_term = True ,
135
+ resp = await self ._retrier .execute_with_retry (
136
+ partial (
137
+ self .client .search ,
138
+ index = self .index_name ,
139
+ query = query ,
140
+ sort = sort ,
141
+ from_ = offset ,
142
+ size = page_size ,
143
+ expand_wildcards = "hidden" ,
144
+ seq_no_primary_term = True ,
145
+ )
124
146
)
125
147
except ApiError as e :
126
148
logger .error (
0 commit comments