Skip to content

Commit

Permalink
Cache index.hidden setting
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 elastic#77974
  • Loading branch information
DaveCTurner committed Oct 4, 2021
1 parent 84b5e35 commit fe1b5e0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 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 @@ -417,6 +418,7 @@ private IndexMetadata(
final ActiveShardCount waitForActiveShards,
final ImmutableOpenMap<String, RolloverInfo> rolloverInfos,
final boolean isSystem,
final boolean isHidden,
final IndexLongFieldRange timestampRange) {

this.index = index;
Expand Down Expand Up @@ -449,6 +451,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;
assert numberOfShards * routingFactor == routingNumShards : routingNumShards + " must be a multiple of " + numberOfShards;
}
Expand Down Expand Up @@ -930,6 +934,10 @@ public boolean isSystem() {
return isSystem;
}

public boolean isHidden() {
return isHidden;
}

public static Builder builder(String index) {
return new Builder(index);
}
Expand Down Expand Up @@ -1309,7 +1317,8 @@ public IndexMetadata build() {
waitForActiveShards,
rolloverInfos.build(),
isSystem,
timestampRange);
INDEX_HIDDEN_SETTING.get(settings),
timestampRange);
}

@SuppressWarnings("unchecked")
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 fe1b5e0

Please sign in to comment.