4141import java .util .Arrays ;
4242import java .util .Collections ;
4343import java .util .HashMap ;
44+ import java .util .HashSet ;
4445import java .util .List ;
4546import java .util .Map ;
4647import java .util .Objects ;
48+ import java .util .Set ;
49+ import java .util .stream .Collectors ;
4750
4851import static org .elasticsearch .snapshots .SnapshotInfo .METADATA_FIELD_INTRODUCED ;
4952
@@ -105,12 +108,25 @@ public Entry(Snapshot snapshot, boolean includeGlobalState, boolean partial, Sta
105108 } else {
106109 this .shards = shards ;
107110 this .waitingIndices = findWaitingIndices (shards );
111+ assert assertShardsConsistent (state , indices , shards );
108112 }
109113 this .repositoryStateId = repositoryStateId ;
110114 this .failure = failure ;
111115 this .userMetadata = userMetadata ;
112116 }
113117
118+ private static boolean assertShardsConsistent (State state , List <IndexId > indices ,
119+ ImmutableOpenMap <ShardId , ShardSnapshotStatus > shards ) {
120+ if ((state == State .INIT || state == State .ABORTED ) && shards .isEmpty ()) {
121+ return true ;
122+ }
123+ final Set <String > indexNames = indices .stream ().map (IndexId ::getName ).collect (Collectors .toSet ());
124+ final Set <String > indexNamesInShards = new HashSet <>();
125+ shards .keysIt ().forEachRemaining (s -> indexNamesInShards .add (s .getIndexName ()));
126+ assert indexNames .equals (indexNamesInShards )
127+ : "Indices in shards " + indexNamesInShards + " differ from expected indices " + indexNames + " for state [" + state + "]" ;
128+ return true ;
129+ }
114130 public Entry (Snapshot snapshot , boolean includeGlobalState , boolean partial , State state , List <IndexId > indices ,
115131 long startTime , long repositoryStateId , ImmutableOpenMap <ShardId , ShardSnapshotStatus > shards ,
116132 Map <String , Object > userMetadata ) {
@@ -189,7 +205,6 @@ public boolean equals(Object o) {
189205 if (!shards .equals (entry .shards )) return false ;
190206 if (!snapshot .equals (entry .snapshot )) return false ;
191207 if (state != entry .state ) return false ;
192- if (!waitingIndices .equals (entry .waitingIndices )) return false ;
193208 if (repositoryStateId != entry .repositoryStateId ) return false ;
194209
195210 return true ;
@@ -203,7 +218,6 @@ public int hashCode() {
203218 result = 31 * result + (partial ? 1 : 0 );
204219 result = 31 * result + shards .hashCode ();
205220 result = 31 * result + indices .hashCode ();
206- result = 31 * result + waitingIndices .hashCode ();
207221 result = 31 * result + Long .hashCode (startTime );
208222 result = 31 * result + Long .hashCode (repositoryStateId );
209223 return result ;
0 commit comments