@@ -133,6 +133,7 @@ protected PrimaryResponse<BulkShardResponse, BulkShardRequest> shardOperationOnP
133133 Set <Tuple <String , String >> mappingsToUpdate = null ;
134134
135135 BulkItemResponse [] responses = new BulkItemResponse [request .items ().length ];
136+ long [] versions = new long [request .items ().length ];
136137 for (int i = 0 ; i < request .items ().length ; i ++) {
137138 BulkItemRequest item = request .items ()[i ];
138139 if (item .request () instanceof IndexRequest ) {
@@ -163,6 +164,7 @@ protected PrimaryResponse<BulkShardResponse, BulkShardRequest> shardOperationOnP
163164 version = create .version ();
164165 op = create ;
165166 }
167+ versions [i ] = indexRequest .version ();
166168 // update the version on request so it will happen on the replicas
167169 indexRequest .version (version );
168170
@@ -188,6 +190,10 @@ protected PrimaryResponse<BulkShardResponse, BulkShardRequest> shardOperationOnP
188190 } catch (Exception e ) {
189191 // rethrow the failure if we are going to retry on primary and let parent failure to handle it
190192 if (retryPrimaryException (e )) {
193+ // restore updated versions...
194+ for (int j = 0 ; j < i ; j ++) {
195+ applyVersion (request .items ()[j ], versions [j ]);
196+ }
191197 throw (ElasticSearchException ) e ;
192198 }
193199 if (e instanceof ElasticSearchException && ((ElasticSearchException ) e ).status () == RestStatus .CONFLICT ) {
@@ -214,6 +220,10 @@ protected PrimaryResponse<BulkShardResponse, BulkShardRequest> shardOperationOnP
214220 } catch (Exception e ) {
215221 // rethrow the failure if we are going to retry on primary and let parent failure to handle it
216222 if (retryPrimaryException (e )) {
223+ // restore updated versions...
224+ for (int j = 0 ; j < i ; j ++) {
225+ applyVersion (request .items ()[j ], versions [j ]);
226+ }
217227 throw (ElasticSearchException ) e ;
218228 }
219229 if (e instanceof ElasticSearchException && ((ElasticSearchException ) e ).status () == RestStatus .CONFLICT ) {
@@ -352,4 +362,14 @@ public void onFailure(Throwable e) {
352362 logger .warn ("failed to update master on updated mapping for index [{}], type [{}]" , e , index , type );
353363 }
354364 }
365+
366+ private void applyVersion (BulkItemRequest item , long version ) {
367+ if (item .request () instanceof IndexRequest ) {
368+ ((IndexRequest ) item .request ()).version (version );
369+ } else if (item .request () instanceof DeleteRequest ) {
370+ ((DeleteRequest ) item .request ()).version (version );
371+ } else {
372+ // log?
373+ }
374+ }
355375}
0 commit comments