88
99import org .apache .lucene .index .IndexCommit ;
1010import org .elasticsearch .Version ;
11+ import org .elasticsearch .action .admin .cluster .state .ClusterStateRequest ;
1112import org .elasticsearch .action .admin .cluster .state .ClusterStateResponse ;
13+ import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
1214import org .elasticsearch .action .support .PlainActionFuture ;
1315import org .elasticsearch .client .Client ;
1416import org .elasticsearch .cluster .metadata .IndexMetaData ;
17+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
1518import org .elasticsearch .cluster .metadata .MetaData ;
1619import org .elasticsearch .cluster .metadata .RepositoryMetaData ;
1720import org .elasticsearch .cluster .node .DiscoveryNode ;
2124import org .elasticsearch .common .component .AbstractLifecycleComponent ;
2225import org .elasticsearch .common .settings .Settings ;
2326import org .elasticsearch .index .Index ;
27+ import org .elasticsearch .index .IndexSettings ;
2428import org .elasticsearch .index .engine .EngineException ;
2529import org .elasticsearch .index .shard .IndexShard ;
2630import org .elasticsearch .index .shard .IndexShardRecoveryException ;
3741import org .elasticsearch .snapshots .SnapshotState ;
3842import org .elasticsearch .xpack .ccr .Ccr ;
3943import org .elasticsearch .xpack .ccr .CcrLicenseChecker ;
44+ import org .elasticsearch .xpack .ccr .action .CcrRequests ;
4045import org .elasticsearch .xpack .ccr .action .repositories .ClearCcrRestoreSessionAction ;
4146import org .elasticsearch .xpack .ccr .action .repositories .ClearCcrRestoreSessionRequest ;
4247import org .elasticsearch .xpack .ccr .action .repositories .PutCcrRestoreSessionAction ;
@@ -111,15 +116,10 @@ public SnapshotInfo getSnapshotInfo(SnapshotId snapshotId) {
111116 public MetaData getSnapshotGlobalMetaData (SnapshotId snapshotId ) {
112117 assert SNAPSHOT_ID .equals (snapshotId ) : "RemoteClusterRepository only supports " + SNAPSHOT_ID + " as the SnapshotId" ;
113118 Client remoteClient = client .getRemoteClusterClient (remoteClusterAlias );
114- ClusterStateResponse response = remoteClient
115- .admin ()
116- .cluster ()
117- .prepareState ()
118- .clear ()
119- .setMetaData (true )
120- .setIndices ("dummy_index_name" ) // We set a single dummy index name to avoid fetching all the index data
121- .get ();
122- return response .getState ().metaData ();
119+ // We set a single dummy index name to avoid fetching all the index data
120+ ClusterStateRequest clusterStateRequest = CcrRequests .metaDataRequest ("dummy_index_name" );
121+ ClusterStateResponse clusterState = remoteClient .admin ().cluster ().state (clusterStateRequest ).actionGet ();
122+ return clusterState .getState ().metaData ();
123123 }
124124
125125 @ Override
@@ -128,18 +128,12 @@ public IndexMetaData getSnapshotIndexMetaData(SnapshotId snapshotId, IndexId ind
128128 String leaderIndex = index .getName ();
129129 Client remoteClient = client .getRemoteClusterClient (remoteClusterAlias );
130130
131- ClusterStateResponse response = remoteClient
132- .admin ()
133- .cluster ()
134- .prepareState ()
135- .clear ()
136- .setMetaData (true )
137- .setIndices (leaderIndex )
138- .get ();
131+ ClusterStateRequest clusterStateRequest = CcrRequests .metaDataRequest (leaderIndex );
132+ ClusterStateResponse clusterState = remoteClient .admin ().cluster ().state (clusterStateRequest ).actionGet ();
139133
140134 // Validates whether the leader cluster has been configured properly:
141135 PlainActionFuture <String []> future = PlainActionFuture .newFuture ();
142- IndexMetaData leaderIndexMetaData = response .getState ().metaData ().index (leaderIndex );
136+ IndexMetaData leaderIndexMetaData = clusterState .getState ().metaData ().index (leaderIndex );
143137 ccrLicenseChecker .fetchLeaderHistoryUUIDs (remoteClient , leaderIndexMetaData , future ::onFailure , future ::onResponse );
144138 String [] leaderHistoryUUIDs = future .actionGet ();
145139
@@ -252,7 +246,8 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
252246
253247 Map <String , String > ccrMetaData = indexShard .indexSettings ().getIndexMetaData ().getCustomData (Ccr .CCR_CUSTOM_METADATA_KEY );
254248 String leaderUUID = ccrMetaData .get (Ccr .CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY );
255- ShardId leaderShardId = new ShardId (shardId .getIndexName (), leaderUUID , shardId .getId ());
249+ Index leaderIndex = new Index (shardId .getIndexName (), leaderUUID );
250+ ShardId leaderShardId = new ShardId (leaderIndex , shardId .getId ());
256251
257252 Client remoteClient = client .getRemoteClusterClient (remoteClusterAlias );
258253 String sessionUUID = UUIDs .randomBase64UUID ();
@@ -261,13 +256,28 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
261256 String nodeId = response .getNodeId ();
262257 // TODO: Implement file restore
263258 closeSession (remoteClient , nodeId , sessionUUID );
259+ maybeUpdateMappings (client , remoteClient , leaderIndex , indexShard .indexSettings ());
264260 }
265261
266262 @ Override
267263 public IndexShardSnapshotStatus getShardSnapshotStatus (SnapshotId snapshotId , Version version , IndexId indexId , ShardId leaderShardId ) {
268264 throw new UnsupportedOperationException ("Unsupported for repository of type: " + TYPE );
269265 }
270266
267+ private void maybeUpdateMappings (Client localClient , Client remoteClient , Index leaderIndex , IndexSettings followerIndexSettings ) {
268+ ClusterStateRequest clusterStateRequest = CcrRequests .metaDataRequest (leaderIndex .getName ());
269+ ClusterStateResponse clusterState = remoteClient .admin ().cluster ().state (clusterStateRequest ).actionGet ();
270+ IndexMetaData leaderIndexMetadata = clusterState .getState ().metaData ().getIndexSafe (leaderIndex );
271+ long leaderMappingVersion = leaderIndexMetadata .getMappingVersion ();
272+
273+ if (leaderMappingVersion > followerIndexSettings .getIndexMetaData ().getMappingVersion ()) {
274+ Index followerIndex = followerIndexSettings .getIndex ();
275+ MappingMetaData mappingMetaData = leaderIndexMetadata .mapping ();
276+ PutMappingRequest putMappingRequest = CcrRequests .putMappingRequest (followerIndex .getName (), mappingMetaData );
277+ localClient .admin ().indices ().putMapping (putMappingRequest ).actionGet ();
278+ }
279+ }
280+
271281 private void closeSession (Client remoteClient , String nodeId , String sessionUUID ) {
272282 ClearCcrRestoreSessionRequest clearRequest = new ClearCcrRestoreSessionRequest (nodeId ,
273283 new ClearCcrRestoreSessionRequest .Request (nodeId , sessionUUID ));
0 commit comments