Skip to content

Commit

Permalink
readd flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx committed Jan 24, 2025
1 parent 816f008 commit f5a75da
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ public boolean hasIndexBlock(String index, ClusterBlock block) {
return indices.getOrDefault(index, Set.of()).contains(block);
}

public boolean hasIndexBlockLevel(String index, ClusterBlockLevel level) {
return indices.getOrDefault(index, Set.of()).stream().anyMatch(clusterBlock -> clusterBlock.contains(level));
}

public Builder removeIndexBlock(String index, ClusterBlock block) {
if (indices.containsKey(index) == false) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Assertions;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.Index;
Expand All @@ -52,7 +53,9 @@
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;

import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING;
import static org.elasticsearch.index.IndexSettings.same;

/**
Expand Down Expand Up @@ -337,10 +340,22 @@ ClusterState execute(ClusterState currentState) {
}
}

// provides the value of VERIFIED_READ_ONLY_SETTING before block changes
final Function<String, Boolean> verifiedReadOnly = indexName ->
VERIFIED_READ_ONLY_SETTING.get(currentState.metadata().index(indexName).getSettings());

final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
boolean changedBlocks = false;
for (IndexMetadata.APIBlock block : IndexMetadata.APIBlock.values()) {
changedBlocks |= maybeUpdateClusterBlock(actualIndices, blocks, block.block, block.setting, openSettings, metadataBuilder);
changedBlocks |= maybeUpdateClusterBlock(
actualIndices,
blocks,
block.block,
block.setting,
openSettings,
metadataBuilder,
verifiedReadOnly
);
}
changed |= changedBlocks;

Expand Down Expand Up @@ -449,7 +464,8 @@ private static boolean maybeUpdateClusterBlock(
ClusterBlock block,
Setting<Boolean> setting,
Settings openSettings,
Metadata.Builder metadataBuilder
Metadata.Builder metadataBuilder,
Function<String, Boolean> verifiedReadOnlyBeforeBlockChanges
) {
boolean changed = false;
if (setting.exists(openSettings)) {
Expand All @@ -459,16 +475,34 @@ private static boolean maybeUpdateClusterBlock(
if (blocks.hasIndexBlock(index, block) == false) {
blocks.addIndexBlock(index, block);
changed = true;
if (block.contains(ClusterBlockLevel.WRITE)) {
var isVerifiedReadOnly = verifiedReadOnlyBeforeBlockChanges.apply(index);
if (isVerifiedReadOnly) {
var indexMetadata = metadataBuilder.get(index);
metadataBuilder.put(
IndexMetadata.builder(indexMetadata)
.settings(
Settings.builder()
.put(indexMetadata.getSettings())
.put(VERIFIED_READ_ONLY_SETTING.getKey(), true)
)
);
}
}
}
} else {
if (blocks.hasIndexBlock(index, block)) {
blocks.removeIndexBlock(index, block);
changed = true;
if (block.contains(ClusterBlockLevel.WRITE)) {
IndexMetadata indexMetadata = metadataBuilder.get(index);
Settings.Builder indexSettings = Settings.builder().put(indexMetadata.getSettings());
indexSettings.remove(MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING.getKey());
metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(indexSettings));
if (blocks.hasIndexBlockLevel(index, ClusterBlockLevel.WRITE) == false) {
var indexMetadata = metadataBuilder.get(index);
var indexSettings = Settings.builder().put(indexMetadata.getSettings());
indexSettings.remove(VERIFIED_READ_ONLY_SETTING.getKey());
metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(indexSettings));
} else if (Assertions.ENABLED) {
assert VERIFIED_READ_ONLY_SETTING.get(metadataBuilder.get(index).getSettings());
}
}
}
}
Expand Down

0 comments on commit f5a75da

Please sign in to comment.