@@ -224,8 +224,7 @@ public static void deleteAll(Path[] dataPaths) throws IOException {
224
224
// exposed for tests
225
225
Directory createDirectory (Path path ) throws IOException {
226
226
// it is possible to disable the use of MMapDirectory for indices, and it may be surprising to users that have done so if we still
227
- // use a MMapDirectory here, which might happen with FSDirectory.open(path). Concurrency is of no concern here so a
228
- // NIOFSDirectory is fine:
227
+ // use a MMapDirectory here, which might happen with FSDirectory.open(path), so we force an NIOFSDirectory to be on the safe side.
229
228
return new NIOFSDirectory (path );
230
229
}
231
230
@@ -323,7 +322,7 @@ public OnDiskState loadBestOnDiskState() throws IOException {
323
322
}
324
323
325
324
/**
326
- * Loads the best available on-disk cluster state. Returns {@link OnDiskState#NO_ON_DISK_STATE} if no such state was found.
325
+ * Loads the available on-disk cluster state. Returns {@link OnDiskState#NO_ON_DISK_STATE} if no such state was found.
327
326
* @param checkClean whether to check the index for corruption before loading, only for tests
328
327
*/
329
328
OnDiskState loadBestOnDiskState (boolean checkClean ) throws IOException {
@@ -339,32 +338,31 @@ OnDiskState loadBestOnDiskState(boolean checkClean) throws IOException {
339
338
final Path indexPath = dataPath .resolve (METADATA_DIRECTORY_NAME );
340
339
if (Files .exists (indexPath )) {
341
340
try (Directory directory = createDirectory (indexPath )) {
342
- if (checkClean ) {
343
- try (BytesStreamOutput outputStream = new BytesStreamOutput ()) {
344
- final boolean isClean ;
345
- try (PrintStream printStream = new PrintStream (outputStream , true , StandardCharsets .UTF_8 .name ());
346
- CheckIndex checkIndex = new CheckIndex (directory )) {
347
- checkIndex .setInfoStream (printStream );
348
- checkIndex .setChecksumsOnly (true );
349
- isClean = checkIndex .checkIndex ().clean ;
350
- }
341
+ if (checkClean ) {
342
+ try (BytesStreamOutput outputStream = new BytesStreamOutput ()) {
343
+ final boolean isClean ;
344
+ try (PrintStream printStream = new PrintStream (outputStream , true , StandardCharsets .UTF_8 .name ());
345
+ CheckIndex checkIndex = new CheckIndex (directory )) {
346
+ checkIndex .setInfoStream (printStream );
347
+ checkIndex .setChecksumsOnly (true );
348
+ isClean = checkIndex .checkIndex ().clean ;
349
+ }
351
350
352
- if (isClean == false ) {
353
- if (logger .isErrorEnabled ()) {
354
- for (final String line : outputStream .bytes ().utf8ToString ().split ("\\ r?\\ n" )) {
355
- logger .error ("checkIndex: {}" , line );
351
+ if (isClean == false ) {
352
+ if (logger .isErrorEnabled ()) {
353
+ for (final String line : outputStream .bytes ().utf8ToString ().split ("\\ r?\\ n" )) {
354
+ logger .error ("checkIndex: {}" , line );
355
+ }
356
356
}
357
+ throw new CorruptStateException (
358
+ "the index containing the cluster metadata under the data path ["
359
+ + dataPath
360
+ + "] has been changed by an external force after it was last written by Elasticsearch and is "
361
+ + "now unreadable"
362
+ );
357
363
}
358
- throw new CorruptStateException (
359
- "the index containing the cluster metadata under the data path ["
360
- + dataPath
361
- + "] has been changed by an external force after it was last written by Elasticsearch and is "
362
- + "now unreadable"
363
- );
364
364
}
365
365
}
366
- }
367
-
368
366
369
367
try (DirectoryReader directoryReader = DirectoryReader .open (directory )) {
370
368
final OnDiskState onDiskState = loadOnDiskState (dataPath , directoryReader );
@@ -394,9 +392,9 @@ OnDiskState loadBestOnDiskState(boolean checkClean) throws IOException {
394
392
if (bestOnDiskState .empty ()
395
393
|| acceptedTerm > maxAcceptedTerm
396
394
|| (acceptedTerm == maxAcceptedTerm
397
- && (onDiskState .lastAcceptedVersion > bestOnDiskState .lastAcceptedVersion
398
- || (onDiskState .lastAcceptedVersion == bestOnDiskState .lastAcceptedVersion )
399
- && onDiskState .currentTerm > bestOnDiskState .currentTerm ))) {
395
+ && (onDiskState .lastAcceptedVersion > bestOnDiskState .lastAcceptedVersion
396
+ || (onDiskState .lastAcceptedVersion == bestOnDiskState .lastAcceptedVersion )
397
+ && onDiskState .currentTerm > bestOnDiskState .currentTerm ))) {
400
398
bestOnDiskState = onDiskState ;
401
399
}
402
400
}
0 commit comments