46
46
import org .elasticsearch .transport .*;
47
47
48
48
import java .io .IOException ;
49
- import java .util .ArrayList ;
50
- import java .util .HashMap ;
51
- import java .util .List ;
52
- import java .util .Map ;
49
+ import java .util .*;
53
50
import java .util .concurrent .CopyOnWriteArrayList ;
54
51
55
52
import static com .google .common .collect .Lists .newArrayList ;
56
53
import static com .google .common .collect .Maps .newHashMap ;
54
+ import static com .google .common .collect .Sets .newHashSet ;
57
55
import static org .elasticsearch .cluster .metadata .MetaDataIndexStateService .INDEX_CLOSED_BLOCK ;
58
56
59
57
/**
@@ -146,6 +144,7 @@ public ClusterState execute(ClusterState currentState) {
146
144
ClusterBlocks .Builder blocks = ClusterBlocks .builder ().blocks (currentState .blocks ());
147
145
RoutingTable .Builder rtBuilder = RoutingTable .builder (currentState .routingTable ());
148
146
final ImmutableMap <ShardId , RestoreMetaData .ShardRestoreStatus > shards ;
147
+ Set <String > aliases = newHashSet ();
149
148
if (!renamedIndices .isEmpty ()) {
150
149
// We have some indices to restore
151
150
ImmutableMap .Builder <ShardId , RestoreMetaData .ShardRestoreStatus > shardsBuilder = ImmutableMap .builder ();
@@ -166,6 +165,10 @@ public ClusterState execute(ClusterState currentState) {
166
165
if (!request .includeAliases () && !snapshotIndexMetaData .aliases ().isEmpty ()) {
167
166
// Remove all aliases - they shouldn't be restored
168
167
indexMdBuilder .removeAllAliases ();
168
+ } else {
169
+ for (ObjectCursor <String > alias : snapshotIndexMetaData .aliases ().keys ()) {
170
+ aliases .add (alias .value );
171
+ }
169
172
}
170
173
IndexMetaData updatedIndexMetaData = indexMdBuilder .build ();
171
174
if (partial ) {
@@ -187,6 +190,10 @@ public ClusterState execute(ClusterState currentState) {
187
190
for (ObjectCursor <AliasMetaData > alias : currentIndexMetaData .aliases ().values ()) {
188
191
indexMdBuilder .putAlias (alias .value );
189
192
}
193
+ } else {
194
+ for (ObjectCursor <String > alias : snapshotIndexMetaData .aliases ().keys ()) {
195
+ aliases .add (alias .value );
196
+ }
190
197
}
191
198
IndexMetaData updatedIndexMetaData = indexMdBuilder .index (renamedIndex ).build ();
192
199
rtBuilder .addAsRestore (updatedIndexMetaData , restoreSource );
@@ -209,12 +216,14 @@ public ClusterState execute(ClusterState currentState) {
209
216
shards = ImmutableMap .of ();
210
217
}
211
218
219
+ checkAliasNameConflicts (renamedIndices , aliases );
220
+
212
221
// Restore global state if needed
213
222
restoreGlobalStateIfRequested (mdBuilder );
214
223
215
224
if (completed (shards )) {
216
225
// We don't have any indices to restore - we are done
217
- restoreInfo = new RestoreInfo (request .name (), ImmutableList .< String > copyOf (renamedIndices .keySet ()),
226
+ restoreInfo = new RestoreInfo (request .name (), ImmutableList .copyOf (renamedIndices .keySet ()),
218
227
shards .size (), shards .size () - failedShards (shards ));
219
228
}
220
229
@@ -223,6 +232,14 @@ public ClusterState execute(ClusterState currentState) {
223
232
return ClusterState .builder (updatedState ).routingResult (routingResult ).build ();
224
233
}
225
234
235
+ private void checkAliasNameConflicts (Map <String , String > renamedIndices , Set <String > aliases ) {
236
+ for (Map .Entry <String , String > renamedIndex : renamedIndices .entrySet ()) {
237
+ if (aliases .contains (renamedIndex .getKey ())) {
238
+ throw new SnapshotRestoreException (snapshotId , "cannot rename index [" + renamedIndex .getValue () + "] into [" + renamedIndex .getKey () + "] because of conflict with an alias with the same name" );
239
+ }
240
+ }
241
+ }
242
+
226
243
private void populateIgnoredShards (String index , IntSet ignoreShards ) {
227
244
for (SnapshotShardFailure failure : snapshot .shardFailures ()) {
228
245
if (index .equals (failure .index ())) {
0 commit comments