33
33
import org .elasticsearch .index .shard .ShardId ;
34
34
import org .elasticsearch .repositories .IndexId ;
35
35
import org .elasticsearch .test .AbstractDiffableWireSerializationTestCase ;
36
+ import org .elasticsearch .test .ESTestCase ;
36
37
import org .elasticsearch .test .VersionUtils ;
37
38
38
39
import java .util .ArrayList ;
39
40
import java .util .Arrays ;
41
+ import java .util .HashMap ;
40
42
import java .util .List ;
43
+ import java .util .Map ;
41
44
import java .util .stream .Collectors ;
45
+ import java .util .stream .Stream ;
42
46
43
47
public class SnapshotsInProgressSerializationTests extends AbstractDiffableWireSerializationTestCase <Custom > {
44
48
@@ -110,7 +114,7 @@ protected Custom makeTestChanges(Custom testInstance) {
110
114
for (int i = 0 ; i < entries .size (); i ++) {
111
115
if (randomBoolean ()) {
112
116
final Entry entry = entries .get (i );
113
- entries .set (i , entry . fail (entry . shards (), randomState ( entry . shards ()), entry . failure () ));
117
+ entries .set (i , mutateEntry (entry ));
114
118
}
115
119
}
116
120
}
@@ -130,15 +134,93 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() {
130
134
@ Override
131
135
protected Custom mutateInstance (Custom instance ) {
132
136
List <Entry > entries = new ArrayList <>(((SnapshotsInProgress ) instance ).entries ());
133
- boolean addEntry = entries .isEmpty () ? true : randomBoolean ();
134
- if (addEntry ) {
135
- entries .add (randomSnapshot ());
137
+ if (false || entries .isEmpty ()) {
138
+ // add or remove an entry
139
+ boolean addEntry = entries .isEmpty () ? true : randomBoolean ();
140
+ if (addEntry ) {
141
+ entries .add (randomSnapshot ());
142
+ } else {
143
+ entries .remove (randomIntBetween (0 , entries .size () - 1 ));
144
+ }
136
145
} else {
137
- entries .remove (randomIntBetween (0 , entries .size () - 1 ));
146
+ // mutate an entry
147
+ int index = randomIntBetween (0 , entries .size () - 1 );
148
+ Entry entry = entries .get (index );
149
+ entries .set (index , mutateEntry (entry ));
138
150
}
139
151
return SnapshotsInProgress .of (entries );
140
152
}
141
153
154
+ private Entry mutateEntry (Entry entry ) {
155
+ switch (randomInt (7 )) {
156
+ case 0 :
157
+ boolean includeGlobalState = !entry .includeGlobalState ();
158
+ return new Entry (entry .snapshot (), includeGlobalState , entry .partial (), entry .state (), entry .indices (), entry .dataStreams (),
159
+ entry .startTime (), entry .repositoryStateId (), entry .shards (), entry .failure (), entry .userMetadata (), entry .version ());
160
+ case 1 :
161
+ boolean partial = !entry .partial ();
162
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), partial , entry .state (), entry .indices (), entry .dataStreams (),
163
+ entry .startTime (), entry .repositoryStateId (), entry .shards (), entry .failure (), entry .userMetadata (), entry .version ());
164
+ case 2 :
165
+ List <String > dataStreams = Stream .concat (
166
+ entry .dataStreams ().stream (),
167
+ Stream .of (randomAlphaOfLength (10 )))
168
+ .collect (Collectors .toList ());
169
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), entry .state (), entry .indices (),
170
+ dataStreams , entry .startTime (), entry .repositoryStateId (), entry .shards (), entry .failure (), entry .userMetadata (),
171
+ entry .version ());
172
+ case 3 :
173
+ long startTime = randomValueOtherThan (entry .startTime (), ESTestCase ::randomLong );
174
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), entry .state (), entry .indices (),
175
+ entry .dataStreams (), startTime , entry .repositoryStateId (), entry .shards (), entry .failure (), entry .userMetadata (),
176
+ entry .version ());
177
+ case 4 :
178
+ long repositoryStateId = randomValueOtherThan (entry .startTime (), ESTestCase ::randomLong );
179
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), entry .state (), entry .indices (),
180
+ entry .dataStreams (), entry .startTime (), repositoryStateId , entry .shards (), entry .failure (), entry .userMetadata (),
181
+ entry .version ());
182
+ case 5 :
183
+ String failure = randomValueOtherThan (entry .failure (), () -> randomAlphaOfLengthBetween (2 , 10 ));
184
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), entry .state (), entry .indices (),
185
+ entry .dataStreams (), entry .startTime (), entry .repositoryStateId (), entry .shards (), failure , entry .userMetadata (),
186
+ entry .version ());
187
+ case 6 :
188
+ List <IndexId > indices = entry .indices ();
189
+ ImmutableOpenMap <ShardId , SnapshotsInProgress .ShardSnapshotStatus > shards = entry .shards ();
190
+ IndexId indexId = new IndexId (randomAlphaOfLength (10 ), randomAlphaOfLength (10 ));
191
+ indices .add (indexId );
192
+ ImmutableOpenMap .Builder <ShardId , SnapshotsInProgress .ShardSnapshotStatus > builder = ImmutableOpenMap .builder (shards );
193
+ Index index = new Index (indexId .getName (), randomAlphaOfLength (10 ));
194
+ int shardsCount = randomIntBetween (1 , 10 );
195
+ for (int j = 0 ; j < shardsCount ; j ++) {
196
+ ShardId shardId = new ShardId (index , j );
197
+ String nodeId = randomAlphaOfLength (10 );
198
+ ShardState shardState = randomFrom (ShardState .values ());
199
+ builder .put (shardId ,
200
+ shardState == ShardState .QUEUED ? SnapshotsInProgress .ShardSnapshotStatus .UNASSIGNED_QUEUED :
201
+ new SnapshotsInProgress .ShardSnapshotStatus (nodeId , shardState ,
202
+ shardState .failed () ? randomAlphaOfLength (10 ) : null , "1" ));
203
+ }
204
+ shards = builder .build ();
205
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), randomState (shards ), indices ,
206
+ entry .dataStreams (), entry .startTime (), entry .repositoryStateId (), shards , entry .failure (), entry .userMetadata (),
207
+ entry .version ());
208
+ case 7 :
209
+ Map <String , Object > userMetadata = entry .userMetadata () != null ? new HashMap <>(entry .userMetadata ()) : new HashMap <>();
210
+ String key = randomAlphaOfLengthBetween (2 , 10 );
211
+ if (userMetadata .containsKey (key )) {
212
+ userMetadata .remove (key );
213
+ } else {
214
+ userMetadata .put (key , randomAlphaOfLengthBetween (2 , 10 ));
215
+ }
216
+ return new Entry (entry .snapshot (), entry .includeGlobalState (), entry .partial (), entry .state (), entry .indices (),
217
+ entry .dataStreams (), entry .startTime (), entry .repositoryStateId (), entry .shards (), entry .failure (), userMetadata ,
218
+ entry .version ());
219
+ default :
220
+ throw new IllegalArgumentException ("invalid randomization case" );
221
+ }
222
+ }
223
+
142
224
public static State randomState (ImmutableOpenMap <ShardId , SnapshotsInProgress .ShardSnapshotStatus > shards ) {
143
225
return SnapshotsInProgress .completed (shards .values ())
144
226
? randomFrom (State .SUCCESS , State .FAILED ) : randomFrom (State .STARTED , State .INIT , State .ABORTED );
0 commit comments