Skip to content

Commit 6261e7d

Browse files
authored
Harmonize 7.16/master PersistedClusterStateService (#79857)
The implementations of PersistedClusterStateService in `master` and `7.16` have diverged, making backporting more painful than it needs to be. This commit adjusts the `7.16` version to match the one in `master` as closely as possible.
1 parent f7281b8 commit 6261e7d

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ public static void deleteAll(Path[] dataPaths) throws IOException {
224224
// exposed for tests
225225
Directory createDirectory(Path path) throws IOException {
226226
// 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.
229228
return new NIOFSDirectory(path);
230229
}
231230

@@ -323,7 +322,7 @@ public OnDiskState loadBestOnDiskState() throws IOException {
323322
}
324323

325324
/**
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.
327326
* @param checkClean whether to check the index for corruption before loading, only for tests
328327
*/
329328
OnDiskState loadBestOnDiskState(boolean checkClean) throws IOException {
@@ -339,32 +338,31 @@ OnDiskState loadBestOnDiskState(boolean checkClean) throws IOException {
339338
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
340339
if (Files.exists(indexPath)) {
341340
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+
}
351350

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+
}
356356
}
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+
);
357363
}
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-
);
364364
}
365365
}
366-
}
367-
368366

369367
try (DirectoryReader directoryReader = DirectoryReader.open(directory)) {
370368
final OnDiskState onDiskState = loadOnDiskState(dataPath, directoryReader);
@@ -394,9 +392,9 @@ OnDiskState loadBestOnDiskState(boolean checkClean) throws IOException {
394392
if (bestOnDiskState.empty()
395393
|| acceptedTerm > maxAcceptedTerm
396394
|| (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))) {
400398
bestOnDiskState = onDiskState;
401399
}
402400
}

0 commit comments

Comments
 (0)