29
29
import org .elasticsearch .common .metrics .CounterMetric ;
30
30
import org .elasticsearch .common .settings .Settings ;
31
31
import org .elasticsearch .common .unit .ByteSizeValue ;
32
+ import org .elasticsearch .common .unit .TimeValue ;
32
33
import org .elasticsearch .common .util .CombinedRateLimiter ;
33
34
import org .elasticsearch .index .Index ;
34
- import org .elasticsearch .index .IndexSettings ;
35
35
import org .elasticsearch .index .engine .EngineException ;
36
36
import org .elasticsearch .index .shard .IndexShard ;
37
37
import org .elasticsearch .index .shard .IndexShardRecoveryException ;
72
72
import java .util .Map ;
73
73
import java .util .Set ;
74
74
import java .util .function .LongConsumer ;
75
+ import java .util .function .Supplier ;
76
+
75
77
76
78
/**
77
79
* This repository relies on a remote cluster for Ccr restores. It is read-only so it can only be used to
@@ -288,30 +290,31 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
288
290
String name = metadata .name ();
289
291
try (RestoreSession restoreSession = openSession (name , remoteClient , leaderShardId , indexShard , recoveryState )) {
290
292
restoreSession .restoreFiles ();
293
+ updateMappings (remoteClient , leaderIndex , restoreSession .mappingVersion , client , indexShard .routingEntry ().index ());
291
294
} catch (Exception e ) {
292
295
throw new IndexShardRestoreFailedException (indexShard .shardId (), "failed to restore snapshot [" + snapshotId + "]" , e );
293
296
}
294
-
295
- maybeUpdateMappings (client , remoteClient , leaderIndex , indexShard .indexSettings ());
296
297
}
297
298
298
299
@ Override
299
300
public IndexShardSnapshotStatus getShardSnapshotStatus (SnapshotId snapshotId , Version version , IndexId indexId , ShardId leaderShardId ) {
300
301
throw new UnsupportedOperationException ("Unsupported for repository of type: " + TYPE );
301
302
}
302
303
303
- private void maybeUpdateMappings (Client localClient , Client remoteClient , Index leaderIndex , IndexSettings followerIndexSettings ) {
304
- ClusterStateRequest clusterStateRequest = CcrRequests .metaDataRequest (leaderIndex .getName ());
305
- ClusterStateResponse clusterState = remoteClient .admin ().cluster ().state (clusterStateRequest )
306
- .actionGet (ccrSettings .getRecoveryActionTimeout ());
307
- IndexMetaData leaderIndexMetadata = clusterState .getState ().metaData ().getIndexSafe (leaderIndex );
308
- long leaderMappingVersion = leaderIndexMetadata .getMappingVersion ();
309
-
310
- if (leaderMappingVersion > followerIndexSettings .getIndexMetaData ().getMappingVersion ()) {
311
- Index followerIndex = followerIndexSettings .getIndex ();
312
- MappingMetaData mappingMetaData = leaderIndexMetadata .mapping ();
313
- PutMappingRequest putMappingRequest = CcrRequests .putMappingRequest (followerIndex .getName (), mappingMetaData );
314
- localClient .admin ().indices ().putMapping (putMappingRequest ).actionGet (ccrSettings .getRecoveryActionTimeout ());
304
+ private void updateMappings (Client leaderClient , Index leaderIndex , long leaderMappingVersion ,
305
+ Client followerClient , Index followerIndex ) {
306
+ final PlainActionFuture <IndexMetaData > indexMetadataFuture = new PlainActionFuture <>();
307
+ final long startTimeInNanos = System .nanoTime ();
308
+ final Supplier <TimeValue > timeout = () -> {
309
+ final long elapsedInNanos = System .nanoTime () - startTimeInNanos ;
310
+ return TimeValue .timeValueNanos (ccrSettings .getRecoveryActionTimeout ().nanos () - elapsedInNanos );
311
+ };
312
+ CcrRequests .getIndexMetadata (leaderClient , leaderIndex , leaderMappingVersion , 0L , timeout , indexMetadataFuture );
313
+ final IndexMetaData leaderIndexMetadata = indexMetadataFuture .actionGet (ccrSettings .getRecoveryActionTimeout ());
314
+ final MappingMetaData mappingMetaData = leaderIndexMetadata .mapping ();
315
+ if (mappingMetaData != null ) {
316
+ final PutMappingRequest putMappingRequest = CcrRequests .putMappingRequest (followerIndex .getName (), mappingMetaData );
317
+ followerClient .admin ().indices ().putMapping (putMappingRequest ).actionGet (ccrSettings .getRecoveryActionTimeout ());
315
318
}
316
319
}
317
320
@@ -321,7 +324,7 @@ private RestoreSession openSession(String repositoryName, Client remoteClient, S
321
324
PutCcrRestoreSessionAction .PutCcrRestoreSessionResponse response = remoteClient .execute (PutCcrRestoreSessionAction .INSTANCE ,
322
325
new PutCcrRestoreSessionRequest (sessionUUID , leaderShardId )).actionGet (ccrSettings .getRecoveryActionTimeout ());
323
326
return new RestoreSession (repositoryName , remoteClient , sessionUUID , response .getNode (), indexShard , recoveryState ,
324
- response .getStoreFileMetaData (), ccrSettings , throttledTime ::inc );
327
+ response .getStoreFileMetaData (), response . getMappingVersion (), ccrSettings , throttledTime ::inc );
325
328
}
326
329
327
330
private static class RestoreSession extends FileRestoreContext implements Closeable {
@@ -332,17 +335,19 @@ private static class RestoreSession extends FileRestoreContext implements Closea
332
335
private final String sessionUUID ;
333
336
private final DiscoveryNode node ;
334
337
private final Store .MetadataSnapshot sourceMetaData ;
338
+ private final long mappingVersion ;
335
339
private final CcrSettings ccrSettings ;
336
340
private final LongConsumer throttleListener ;
337
341
338
342
RestoreSession (String repositoryName , Client remoteClient , String sessionUUID , DiscoveryNode node , IndexShard indexShard ,
339
- RecoveryState recoveryState , Store .MetadataSnapshot sourceMetaData , CcrSettings ccrSettings ,
340
- LongConsumer throttleListener ) {
343
+ RecoveryState recoveryState , Store .MetadataSnapshot sourceMetaData , long mappingVersion ,
344
+ CcrSettings ccrSettings , LongConsumer throttleListener ) {
341
345
super (repositoryName , indexShard , SNAPSHOT_ID , recoveryState , BUFFER_SIZE );
342
346
this .remoteClient = remoteClient ;
343
347
this .sessionUUID = sessionUUID ;
344
348
this .node = node ;
345
349
this .sourceMetaData = sourceMetaData ;
350
+ this .mappingVersion = mappingVersion ;
346
351
this .ccrSettings = ccrSettings ;
347
352
this .throttleListener = throttleListener ;
348
353
}
0 commit comments