Skip to content

Commit

Permalink
Cache index.hidden setting (#78612)
Browse files Browse the repository at this point in the history
We look up the value for `index.hidden` for every index every time we
build a new `Metadata`, which involves map lookups and string parsing
and so on and takes nontrivial time. This commit moves the value to a
field so the lookups are only needed if the index metadata changes.

Closes #77974
  • Loading branch information
DaveCTurner authored Oct 4, 2021
1 parent abdffda commit 9aef331
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public static APIBlock readFrom(StreamInput input) throws IOException {
private final ActiveShardCount waitForActiveShards;
private final ImmutableOpenMap<String, RolloverInfo> rolloverInfos;
private final boolean isSystem;
private final boolean isHidden;

private final IndexLongFieldRange timestampRange;

Expand Down Expand Up @@ -421,6 +422,7 @@ private IndexMetadata(
final ActiveShardCount waitForActiveShards,
final ImmutableOpenMap<String, RolloverInfo> rolloverInfos,
final boolean isSystem,
final boolean isHidden,
final IndexLongFieldRange timestampRange,
final int priority,
final long creationDate) {
Expand Down Expand Up @@ -455,6 +457,8 @@ private IndexMetadata(
this.waitForActiveShards = waitForActiveShards;
this.rolloverInfos = rolloverInfos;
this.isSystem = isSystem;
assert isHidden == INDEX_HIDDEN_SETTING.get(settings);
this.isHidden = isHidden;
this.timestampRange = timestampRange;
this.priority = priority;
this.creationDate = creationDate;
Expand Down Expand Up @@ -938,6 +942,10 @@ public boolean isSystem() {
return isSystem;
}

public boolean isHidden() {
return isHidden;
}

public int priority() {
return priority;
}
Expand Down Expand Up @@ -1318,6 +1326,7 @@ public IndexMetadata build() {
waitForActiveShards,
rolloverInfos.build(),
isSystem,
INDEX_HIDDEN_SETTING.get(settings),
timestampRange,
IndexMetadata.INDEX_PRIORITY_SETTING.get(settings),
settings.getAsLong(SETTING_CREATION_DATE, -1L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ public Metadata build() {
final String name = indexMetadata.getIndex().getName();
boolean added = allIndices.add(name);
assert added : "double index named [" + name + "]";
final boolean visible = IndexMetadata.INDEX_HIDDEN_SETTING.get(indexMetadata.getSettings()) == false;
final boolean visible = indexMetadata.isHidden() == false;
if (visible) {
visibleIndices.add(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ private static IndexMetadata.Builder restoreOverClosedIndex(IndexMetadata snapsh
}

private void ensureValidIndexName(ClusterState currentState, IndexMetadata snapshotIndexMetadata, String renamedIndexName) {
final boolean isHidden = IndexMetadata.INDEX_HIDDEN_SETTING.get(snapshotIndexMetadata.getSettings());
final boolean isHidden = snapshotIndexMetadata.isHidden();
createIndexService.validateIndexName(renamedIndexName, currentState);
createIndexService.validateDotIndex(renamedIndexName, isHidden);
createIndexService.validateIndexSettings(renamedIndexName, snapshotIndexMetadata.getSettings(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxPrimaryShardSizeCondition;
Expand Down Expand Up @@ -40,6 +41,7 @@
import java.util.Map;
import java.util.Set;

import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_HIDDEN_SETTING;
import static org.elasticsearch.cluster.metadata.IndexMetadata.parseIndexNameCounter;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -366,4 +368,24 @@ public void testParseIndexNameCannotFormatNumber() {
}
}

public void testIsHidden() {
Settings.Builder settings = Settings.builder()
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 8))
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT);
IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings).build();
assertFalse(indexMetadata.isHidden());

settings.put(INDEX_HIDDEN_SETTING.getKey(), "false");
indexMetadata = IndexMetadata.builder(indexMetadata).settings(settings).build();
assertFalse(indexMetadata.isHidden());

settings.put(INDEX_HIDDEN_SETTING.getKey(), "true");
indexMetadata = IndexMetadata.builder(indexMetadata).settings(settings).build();
assertTrue(indexMetadata.isHidden());

indexMetadata = IndexMetadata.builder(indexMetadata).build();
assertTrue(indexMetadata.isHidden()); // preserved if settings unchanged
}

}

0 comments on commit 9aef331

Please sign in to comment.