29
29
import org .apache .lucene .store .IndexInput ;
30
30
import org .apache .lucene .store .OutputStreamIndexOutput ;
31
31
import org .apache .lucene .store .SimpleFSDirectory ;
32
- import org .elasticsearch .common .logging .Loggers ;
33
- import org .elasticsearch .core .internal .io .IOUtils ;
34
32
import org .elasticsearch .ExceptionsHelper ;
35
- import org .elasticsearch .common .bytes . BytesArray ;
33
+ import org .elasticsearch .common .logging . Loggers ;
36
34
import org .elasticsearch .common .lucene .store .IndexOutputOutputStream ;
37
35
import org .elasticsearch .common .lucene .store .InputStreamIndexInput ;
38
36
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
39
37
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
40
38
import org .elasticsearch .common .xcontent .XContentBuilder ;
41
39
import org .elasticsearch .common .xcontent .XContentFactory ;
42
- import org .elasticsearch .common .xcontent .XContentHelper ;
43
40
import org .elasticsearch .common .xcontent .XContentParser ;
44
41
import org .elasticsearch .common .xcontent .XContentType ;
42
+ import org .elasticsearch .core .internal .io .IOUtils ;
45
43
46
44
import java .io .FileNotFoundException ;
47
45
import java .io .IOException ;
54
52
import java .util .ArrayList ;
55
53
import java .util .Collection ;
56
54
import java .util .List ;
57
- import java .util .function .Predicate ;
58
55
import java .util .regex .Matcher ;
59
56
import java .util .regex .Pattern ;
60
57
import java .util .stream .Collectors ;
@@ -70,9 +67,8 @@ public abstract class MetaDataStateFormat<T> {
70
67
public static final String STATE_FILE_EXTENSION = ".st" ;
71
68
72
69
private static final String STATE_FILE_CODEC = "state" ;
73
- private static final int MIN_COMPATIBLE_STATE_FILE_VERSION = 0 ;
70
+ private static final int MIN_COMPATIBLE_STATE_FILE_VERSION = 1 ;
74
71
private static final int STATE_FILE_VERSION = 1 ;
75
- private static final int STATE_FILE_VERSION_ES_2X_AND_BELOW = 0 ;
76
72
private static final int BUFFER_SIZE = 4096 ;
77
73
private final String prefix ;
78
74
private final Pattern stateFilePattern ;
@@ -186,16 +182,11 @@ public final T read(NamedXContentRegistry namedXContentRegistry, Path file) thro
186
182
try (IndexInput indexInput = dir .openInput (file .getFileName ().toString (), IOContext .DEFAULT )) {
187
183
// We checksum the entire file before we even go and parse it. If it's corrupted we barf right here.
188
184
CodecUtil .checksumEntireFile (indexInput );
189
- final int fileVersion = CodecUtil .checkHeader (indexInput , STATE_FILE_CODEC , MIN_COMPATIBLE_STATE_FILE_VERSION ,
190
- STATE_FILE_VERSION );
185
+ CodecUtil .checkHeader (indexInput , STATE_FILE_CODEC , MIN_COMPATIBLE_STATE_FILE_VERSION , STATE_FILE_VERSION );
191
186
final XContentType xContentType = XContentType .values ()[indexInput .readInt ()];
192
187
if (xContentType != FORMAT ) {
193
188
throw new IllegalStateException ("expected state in " + file + " to be " + FORMAT + " format but was " + xContentType );
194
189
}
195
- if (fileVersion == STATE_FILE_VERSION_ES_2X_AND_BELOW ) {
196
- // format version 0, wrote a version that always came from the content state file and was never used
197
- indexInput .readLong (); // version currently unused
198
- }
199
190
long filePointer = indexInput .getFilePointer ();
200
191
long contentSize = indexInput .length () - CodecUtil .footerLength () - filePointer ;
201
192
try (IndexInput slice = indexInput .slice ("state_xcontent" , filePointer , contentSize )) {
@@ -263,10 +254,9 @@ long findMaxStateId(final String prefix, Path... locations) throws IOException {
263
254
* @param dataLocations the data-locations to try.
264
255
* @return the latest state or <code>null</code> if no state was found.
265
256
*/
266
- public T loadLatestState (Logger logger , NamedXContentRegistry namedXContentRegistry , Path ... dataLocations ) throws IOException {
257
+ public T loadLatestState (Logger logger , NamedXContentRegistry namedXContentRegistry , Path ... dataLocations ) throws IOException {
267
258
List <PathAndStateId > files = new ArrayList <>();
268
259
long maxStateId = -1 ;
269
- boolean maxStateIdIsLegacy = true ;
270
260
if (dataLocations != null ) { // select all eligible files first
271
261
for (Path dataLocation : dataLocations ) {
272
262
final Path stateDir = dataLocation .resolve (STATE_DIR_NAME );
@@ -280,9 +270,7 @@ public T loadLatestState(Logger logger, NamedXContentRegistry namedXContentRegi
280
270
if (matcher .matches ()) {
281
271
final long stateId = Long .parseLong (matcher .group (1 ));
282
272
maxStateId = Math .max (maxStateId , stateId );
283
- final boolean legacy = MetaDataStateFormat .STATE_FILE_EXTENSION .equals (matcher .group (2 )) == false ;
284
- maxStateIdIsLegacy &= legacy ; // on purpose, see NOTE below
285
- PathAndStateId pav = new PathAndStateId (stateFile , stateId , legacy );
273
+ PathAndStateId pav = new PathAndStateId (stateFile , stateId );
286
274
logger .trace ("found state file: {}" , pav );
287
275
files .add (pav );
288
276
}
@@ -292,39 +280,19 @@ public T loadLatestState(Logger logger, NamedXContentRegistry namedXContentRegi
292
280
}
293
281
}
294
282
}
295
- final List <Throwable > exceptions = new ArrayList <>();
296
- T state = null ;
297
283
// NOTE: we might have multiple version of the latest state if there are multiple data dirs.. for this case
298
- // we iterate only over the ones with the max version. If we have at least one state file that uses the
299
- // new format (ie. legacy == false) then we know that the latest version state ought to use this new format.
300
- // In case the state file with the latest version does not use the new format while older state files do,
301
- // the list below will be empty and loading the state will fail
284
+ // we iterate only over the ones with the max version.
285
+ long finalMaxStateId = maxStateId ;
302
286
Collection <PathAndStateId > pathAndStateIds = files
303
287
.stream ()
304
- .filter (new StateIdAndLegacyPredicate ( maxStateId , maxStateIdIsLegacy ) )
288
+ .filter (pathAndStateId -> pathAndStateId . id == finalMaxStateId )
305
289
.collect (Collectors .toCollection (ArrayList ::new ));
306
290
291
+ final List <Throwable > exceptions = new ArrayList <>();
307
292
for (PathAndStateId pathAndStateId : pathAndStateIds ) {
308
293
try {
309
- final Path stateFile = pathAndStateId .file ;
310
- final long id = pathAndStateId .id ;
311
- if (pathAndStateId .legacy ) { // read the legacy format -- plain XContent
312
- final byte [] data = Files .readAllBytes (stateFile );
313
- if (data .length == 0 ) {
314
- logger .debug ("{}: no data for [{}], ignoring..." , prefix , stateFile .toAbsolutePath ());
315
- continue ;
316
- }
317
- try (XContentParser parser = XContentHelper
318
- .createParser (namedXContentRegistry , LoggingDeprecationHandler .INSTANCE , new BytesArray (data ))) {
319
- state = fromXContent (parser );
320
- }
321
- if (state == null ) {
322
- logger .debug ("{}: no data for [{}], ignoring..." , prefix , stateFile .toAbsolutePath ());
323
- }
324
- } else {
325
- state = read (namedXContentRegistry , stateFile );
326
- logger .trace ("state id [{}] read from [{}]" , id , stateFile .getFileName ());
327
- }
294
+ T state = read (namedXContentRegistry , pathAndStateId .file );
295
+ logger .trace ("state id [{}] read from [{}]" , pathAndStateId .id , pathAndStateId .file .getFileName ());
328
296
return state ;
329
297
} catch (Exception e ) {
330
298
exceptions .add (new IOException ("failed to read " + pathAndStateId .toString (), e ));
@@ -338,46 +306,24 @@ public T loadLatestState(Logger logger, NamedXContentRegistry namedXContentRegi
338
306
// We have some state files but none of them gave us a usable state
339
307
throw new IllegalStateException ("Could not find a state file to recover from among " + files );
340
308
}
341
- return state ;
342
- }
343
-
344
- /**
345
- * Filters out all {@link org.elasticsearch.gateway.MetaDataStateFormat.PathAndStateId} instances with a different id than
346
- * the given one.
347
- */
348
- private static final class StateIdAndLegacyPredicate implements Predicate <PathAndStateId > {
349
- private final long id ;
350
- private final boolean legacy ;
351
-
352
- StateIdAndLegacyPredicate (long id , boolean legacy ) {
353
- this .id = id ;
354
- this .legacy = legacy ;
355
- }
356
-
357
- @ Override
358
- public boolean test (PathAndStateId input ) {
359
- return input .id == id && input .legacy == legacy ;
360
- }
309
+ return null ;
361
310
}
362
311
363
312
/**
364
- * Internal struct-like class that holds the parsed state id, the file
365
- * and a flag if the file is a legacy state ie. pre 1.5
313
+ * Internal struct-like class that holds the parsed state id and the file
366
314
*/
367
315
private static class PathAndStateId {
368
316
final Path file ;
369
317
final long id ;
370
- final boolean legacy ;
371
318
372
- private PathAndStateId (Path file , long id , boolean legacy ) {
319
+ private PathAndStateId (Path file , long id ) {
373
320
this .file = file ;
374
321
this .id = id ;
375
- this .legacy = legacy ;
376
322
}
377
323
378
324
@ Override
379
325
public String toString () {
380
- return "[id:" + id + ", legacy:" + legacy + ", file:" + file .toAbsolutePath () + "]" ;
326
+ return "[id:" + id + ", file:" + file .toAbsolutePath () + "]" ;
381
327
}
382
328
}
383
329
0 commit comments