2929import org .elasticsearch .common .metrics .CounterMetric ;
3030import org .elasticsearch .common .settings .Settings ;
3131import org .elasticsearch .common .unit .ByteSizeValue ;
32+ import org .elasticsearch .common .unit .TimeValue ;
3233import org .elasticsearch .common .util .CombinedRateLimiter ;
3334import org .elasticsearch .index .Index ;
34- import org .elasticsearch .index .IndexSettings ;
3535import org .elasticsearch .index .engine .EngineException ;
3636import org .elasticsearch .index .shard .IndexShard ;
3737import org .elasticsearch .index .shard .IndexShardRecoveryException ;
7272import java .util .Map ;
7373import java .util .Set ;
7474import java .util .function .LongConsumer ;
75+ import java .util .function .Supplier ;
76+
7577
7678/**
7779 * 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
288290 String name = metadata .name ();
289291 try (RestoreSession restoreSession = openSession (name , remoteClient , leaderShardId , indexShard , recoveryState )) {
290292 restoreSession .restoreFiles ();
293+ updateMappings (remoteClient , leaderIndex , restoreSession .mappingVersion , client , indexShard .routingEntry ().index ());
291294 } catch (Exception e ) {
292295 throw new IndexShardRestoreFailedException (indexShard .shardId (), "failed to restore snapshot [" + snapshotId + "]" , e );
293296 }
294-
295- maybeUpdateMappings (client , remoteClient , leaderIndex , indexShard .indexSettings ());
296297 }
297298
298299 @ Override
299300 public IndexShardSnapshotStatus getShardSnapshotStatus (SnapshotId snapshotId , Version version , IndexId indexId , ShardId leaderShardId ) {
300301 throw new UnsupportedOperationException ("Unsupported for repository of type: " + TYPE );
301302 }
302303
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 ());
315318 }
316319 }
317320
@@ -321,7 +324,7 @@ private RestoreSession openSession(String repositoryName, Client remoteClient, S
321324 PutCcrRestoreSessionAction .PutCcrRestoreSessionResponse response = remoteClient .execute (PutCcrRestoreSessionAction .INSTANCE ,
322325 new PutCcrRestoreSessionRequest (sessionUUID , leaderShardId )).actionGet (ccrSettings .getRecoveryActionTimeout ());
323326 return new RestoreSession (repositoryName , remoteClient , sessionUUID , response .getNode (), indexShard , recoveryState ,
324- response .getStoreFileMetaData (), ccrSettings , throttledTime ::inc );
327+ response .getStoreFileMetaData (), response . getMappingVersion (), ccrSettings , throttledTime ::inc );
325328 }
326329
327330 private static class RestoreSession extends FileRestoreContext implements Closeable {
@@ -332,17 +335,19 @@ private static class RestoreSession extends FileRestoreContext implements Closea
332335 private final String sessionUUID ;
333336 private final DiscoveryNode node ;
334337 private final Store .MetadataSnapshot sourceMetaData ;
338+ private final long mappingVersion ;
335339 private final CcrSettings ccrSettings ;
336340 private final LongConsumer throttleListener ;
337341
338342 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 ) {
341345 super (repositoryName , indexShard , SNAPSHOT_ID , recoveryState , BUFFER_SIZE );
342346 this .remoteClient = remoteClient ;
343347 this .sessionUUID = sessionUUID ;
344348 this .node = node ;
345349 this .sourceMetaData = sourceMetaData ;
350+ this .mappingVersion = mappingVersion ;
346351 this .ccrSettings = ccrSettings ;
347352 this .throttleListener = throttleListener ;
348353 }
0 commit comments