8
8
9
9
import org .apache .lucene .index .IndexCommit ;
10
10
import org .elasticsearch .Version ;
11
+ import org .elasticsearch .action .admin .cluster .state .ClusterStateRequest ;
11
12
import org .elasticsearch .action .admin .cluster .state .ClusterStateResponse ;
13
+ import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequest ;
12
14
import org .elasticsearch .action .support .PlainActionFuture ;
13
15
import org .elasticsearch .client .Client ;
14
16
import org .elasticsearch .cluster .metadata .IndexMetaData ;
17
+ import org .elasticsearch .cluster .metadata .MappingMetaData ;
15
18
import org .elasticsearch .cluster .metadata .MetaData ;
16
19
import org .elasticsearch .cluster .metadata .RepositoryMetaData ;
17
20
import org .elasticsearch .cluster .node .DiscoveryNode ;
21
24
import org .elasticsearch .common .component .AbstractLifecycleComponent ;
22
25
import org .elasticsearch .common .settings .Settings ;
23
26
import org .elasticsearch .index .Index ;
27
+ import org .elasticsearch .index .IndexSettings ;
24
28
import org .elasticsearch .index .engine .EngineException ;
25
29
import org .elasticsearch .index .shard .IndexShard ;
26
30
import org .elasticsearch .index .shard .IndexShardRecoveryException ;
37
41
import org .elasticsearch .snapshots .SnapshotState ;
38
42
import org .elasticsearch .xpack .ccr .Ccr ;
39
43
import org .elasticsearch .xpack .ccr .CcrLicenseChecker ;
44
+ import org .elasticsearch .xpack .ccr .action .CcrRequests ;
40
45
import org .elasticsearch .xpack .ccr .action .repositories .ClearCcrRestoreSessionAction ;
41
46
import org .elasticsearch .xpack .ccr .action .repositories .ClearCcrRestoreSessionRequest ;
42
47
import org .elasticsearch .xpack .ccr .action .repositories .PutCcrRestoreSessionAction ;
@@ -111,15 +116,10 @@ public SnapshotInfo getSnapshotInfo(SnapshotId snapshotId) {
111
116
public MetaData getSnapshotGlobalMetaData (SnapshotId snapshotId ) {
112
117
assert SNAPSHOT_ID .equals (snapshotId ) : "RemoteClusterRepository only supports " + SNAPSHOT_ID + " as the SnapshotId" ;
113
118
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 ();
123
123
}
124
124
125
125
@ Override
@@ -128,18 +128,12 @@ public IndexMetaData getSnapshotIndexMetaData(SnapshotId snapshotId, IndexId ind
128
128
String leaderIndex = index .getName ();
129
129
Client remoteClient = client .getRemoteClusterClient (remoteClusterAlias );
130
130
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 ();
139
133
140
134
// Validates whether the leader cluster has been configured properly:
141
135
PlainActionFuture <String []> future = PlainActionFuture .newFuture ();
142
- IndexMetaData leaderIndexMetaData = response .getState ().metaData ().index (leaderIndex );
136
+ IndexMetaData leaderIndexMetaData = clusterState .getState ().metaData ().index (leaderIndex );
143
137
ccrLicenseChecker .fetchLeaderHistoryUUIDs (remoteClient , leaderIndexMetaData , future ::onFailure , future ::onResponse );
144
138
String [] leaderHistoryUUIDs = future .actionGet ();
145
139
@@ -252,7 +246,8 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
252
246
253
247
Map <String , String > ccrMetaData = indexShard .indexSettings ().getIndexMetaData ().getCustomData (Ccr .CCR_CUSTOM_METADATA_KEY );
254
248
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 ());
256
251
257
252
Client remoteClient = client .getRemoteClusterClient (remoteClusterAlias );
258
253
String sessionUUID = UUIDs .randomBase64UUID ();
@@ -261,13 +256,30 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
261
256
String nodeId = response .getNodeId ();
262
257
// TODO: Implement file restore
263
258
closeSession (remoteClient , nodeId , sessionUUID );
259
+ maybeUpdateMappings (client , remoteClient , leaderIndex , indexShard .indexSettings ());
264
260
}
265
261
266
262
@ Override
267
263
public IndexShardSnapshotStatus getShardSnapshotStatus (SnapshotId snapshotId , Version version , IndexId indexId , ShardId leaderShardId ) {
268
264
throw new UnsupportedOperationException ("Unsupported for repository of type: " + TYPE );
269
265
}
270
266
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
+ assert leaderIndexMetadata .getMappings ().size () == 1 : "expected exactly one mapping, but got [" +
276
+ leaderIndexMetadata .getMappings ().size () + "]" ;
277
+ MappingMetaData mappingMetaData = leaderIndexMetadata .getMappings ().iterator ().next ().value ;
278
+ PutMappingRequest putMappingRequest = CcrRequests .putMappingRequest (followerIndex .getName (), mappingMetaData );
279
+ localClient .admin ().indices ().putMapping (putMappingRequest ).actionGet ();
280
+ }
281
+ }
282
+
271
283
private void closeSession (Client remoteClient , String nodeId , String sessionUUID ) {
272
284
ClearCcrRestoreSessionRequest clearRequest = new ClearCcrRestoreSessionRequest (nodeId ,
273
285
new ClearCcrRestoreSessionRequest .Request (nodeId , sessionUUID ));
0 commit comments