Skip to content

Commit 4adf9b1

Browse files
authored
[8.x] Support recovery for closed shard in N-2 version (#120595)
Changes in 8.x to allow shard recovery for shards in version N-2 that have been verified before being closed, but not verified as read-only, in 7.x or 8.x. Reopening such closed indices automatically adds an index.blocks.write.
1 parent 7fd2af0 commit 4adf9b1

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadataVerifier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939
import java.util.Set;
4040

41+
import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.isIndexVerifiedBeforeClosed;
4142
import static org.elasticsearch.core.Strings.format;
4243

4344
/**
@@ -163,7 +164,7 @@ public static boolean isReadOnlySupportedVersion(
163164
) {
164165
if (isReadOnlyCompatible(indexMetadata, minimumCompatible, minimumReadOnlyCompatible)) {
165166
assert isFullySupportedVersion(indexMetadata, minimumCompatible) == false : indexMetadata;
166-
final boolean isReadOnly = hasIndexWritesBlock(indexMetadata);
167+
final boolean isReadOnly = hasReadOnlyBlocks(indexMetadata) || isIndexVerifiedBeforeClosed(indexMetadata);
167168
if (isReadOnly == false) {
168169
throw new IllegalStateException(
169170
"The index "
@@ -206,7 +207,7 @@ private static boolean isReadOnlyCompatible(
206207
return false;
207208
}
208209

209-
private static boolean hasIndexWritesBlock(IndexMetadata indexMetadata) {
210+
static boolean hasReadOnlyBlocks(IndexMetadata indexMetadata) {
210211
var indexSettings = indexMetadata.getSettings();
211212
if (IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(indexSettings) || IndexMetadata.INDEX_READ_ONLY_SETTING.get(indexSettings)) {
212213
return indexMetadata.isSearchableSnapshot()

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexStateService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import java.util.stream.Collectors;
9191

9292
import static java.util.stream.Collectors.joining;
93+
import static org.elasticsearch.cluster.metadata.IndexMetadataVerifier.hasReadOnlyBlocks;
9394
import static org.elasticsearch.core.Strings.format;
9495

9596
/**
@@ -1185,6 +1186,18 @@ private ClusterState openIndices(final Index[] indices, final ClusterState curre
11851186
final Settings.Builder updatedSettings = Settings.builder().put(indexMetadata.getSettings());
11861187
updatedSettings.remove(VERIFIED_BEFORE_CLOSE_SETTING.getKey());
11871188

1189+
// Reopening a read-only compatible index that has not been marked as read-only is possible if the index was
1190+
// verified-before-close in the first place.
1191+
var compatibilityVersion = indexMetadata.getCompatibilityVersion();
1192+
if (compatibilityVersion.before(minIndexCompatibilityVersion) && hasReadOnlyBlocks(indexMetadata) == false) {
1193+
if (isIndexVerifiedBeforeClosed(indexMetadata)) {
1194+
updatedSettings.put(VERIFIED_READ_ONLY_SETTING.getKey(), true);
1195+
// at least set a write block if the index was verified-before-close at the time the cluster was upgraded
1196+
blocks.addIndexBlock(index.getName(), APIBlock.WRITE.block);
1197+
updatedSettings.put(APIBlock.WRITE.settingName(), true);
1198+
} // or else, the following indexMetadataVerifier.verifyIndexMetadata() should throw.
1199+
}
1200+
11881201
IndexMetadata newIndexMetadata = IndexMetadata.builder(indexMetadata)
11891202
.state(IndexMetadata.State.OPEN)
11901203
.settingsVersion(indexMetadata.getSettingsVersion() + 1)

0 commit comments

Comments
 (0)