55
66
77class FailingBulkClient (object ):
8- def __init__ (self , client , fail_at = 1 ):
8+ def __init__ (self , client , fail_at = ( 2 , ), fail_with = TransportError ( 599 , "Error!" , {}) ):
99 self .client = client
10- self ._called = - 1
10+ self ._called = 0
1111 self ._fail_at = fail_at
1212 self .transport = client .transport
13+ self ._fail_with = fail_with
1314
1415 def bulk (self , * args , ** kwargs ):
1516 self ._called += 1
16- if self ._called == self ._fail_at :
17- raise TransportError ( 599 , "Error!" , {})
17+ if self ._called in self ._fail_at :
18+ raise self . _fail_with
1819 return self .client .bulk (* args , ** kwargs )
1920
2021class TestStreamingBulk (ElasticsearchTestCase ):
@@ -87,7 +88,6 @@ def test_transport_error_can_becaught(self):
8788 '_index' : 'i' ,
8889 '_type' : 't' ,
8990 '_id' : 45 ,
90-
9191 'data' : {'f' : 'v' },
9292 'error' : "TransportError(599, 'Error!')" ,
9393 'status' : 599
@@ -96,6 +96,46 @@ def test_transport_error_can_becaught(self):
9696 results [1 ][1 ]
9797 )
9898
99+ def test_rejected_documents_are_retried (self ):
100+ failing_client = FailingBulkClient (self .client , fail_with = TransportError (429 , 'Rejected!' , {}))
101+ docs = [
102+ {'_index' : 'i' , '_type' : 't' , '_id' : 47 , 'f' : 'v' },
103+ {'_index' : 'i' , '_type' : 't' , '_id' : 45 , 'f' : 'v' },
104+ {'_index' : 'i' , '_type' : 't' , '_id' : 42 , 'f' : 'v' },
105+ ]
106+ results = list (helpers .streaming_bulk (failing_client , docs ,
107+ raise_on_exception = False ,
108+ raise_on_error = False ,
109+ chunk_size = 1 , max_retries = 1 ,
110+ initial_backoff = 0 ))
111+ self .assertEquals (3 , len (results ))
112+ self .assertEquals ([True , True , True ], [r [0 ] for r in results ])
113+ self .client .indices .refresh (index = 'i' )
114+ res = self .client .search (index = 'i' )
115+ self .assertEquals (3 , res ['hits' ]['total' ])
116+ self .assertEquals (4 , failing_client ._called )
117+
118+ def test_rejected_documents_are_retried_at_most_max_retries_times (self ):
119+ failing_client = FailingBulkClient (self .client , fail_at = (1 , 2 , ),
120+ fail_with = TransportError (429 , 'Rejected!' , {}))
121+
122+ docs = [
123+ {'_index' : 'i' , '_type' : 't' , '_id' : 47 , 'f' : 'v' },
124+ {'_index' : 'i' , '_type' : 't' , '_id' : 45 , 'f' : 'v' },
125+ {'_index' : 'i' , '_type' : 't' , '_id' : 42 , 'f' : 'v' },
126+ ]
127+ results = list (helpers .streaming_bulk (failing_client , docs ,
128+ raise_on_exception = False ,
129+ raise_on_error = False ,
130+ chunk_size = 1 , max_retries = 1 ,
131+ initial_backoff = 0 ))
132+ self .assertEquals (3 , len (results ))
133+ self .assertEquals ([False , True , True ], [r [0 ] for r in results ])
134+ self .client .indices .refresh (index = 'i' )
135+ res = self .client .search (index = 'i' )
136+ self .assertEquals (2 , res ['hits' ]['total' ])
137+ self .assertEquals (4 , failing_client ._called )
138+
99139
100140class TestBulk (ElasticsearchTestCase ):
101141 def test_bulk_works_with_single_item (self ):
0 commit comments