Skip to content

Commit

Permalink
Introduce a Few Settings Singleton Instances (#78897)
Browse files Browse the repository at this point in the history
This is mostly motivated by the ILM steps changes, that showed up as hot in
profiling since instantiating a `Settings` instance turns out to be somewhat expensive.
Also cleans up a couple of other spots and some duplication as well.
  • Loading branch information
original-brownbear authored Oct 11, 2021
1 parent 2e69f6d commit 68ee6d4
Show file tree
Hide file tree
Showing 32 changed files with 100 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* A request to create an index.
*/
Expand All @@ -47,7 +45,7 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, ToX
static final ParseField ALIASES = new ParseField("aliases");

private final String index;
private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private BytesReference mappings;
private XContentType mappingsXContentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static class Builder {

private List<String> indexPatterns;

private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private MappingMetadata mappings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* A request to create an index template.
*/
Expand All @@ -53,7 +51,7 @@ public class PutIndexTemplateRequest extends TimedRequest implements ToXContentF

private boolean create;

private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private BytesReference mappings = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Settings indexSettings() {
}

protected Settings recoverySettings() {
return Settings.Builder.EMPTY_SETTINGS;
return Settings.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Map;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;

Expand All @@ -39,7 +38,7 @@ public class PutRepositoryRequest extends AcknowledgedRequest<PutRepositoryReque

private boolean verify = true;

private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

public PutRepositoryRequest(StreamInput in) throws IOException {
super(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* Request for an update cluster settings action
Expand All @@ -44,8 +43,8 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest<ClusterUpd
PARSER.declareObject((r, t) -> r.transientSettings = t, (p, c) -> Settings.fromXContent(p), TRANSIENT);
}

private Settings transientSettings = EMPTY_SETTINGS;
private Settings persistentSettings = EMPTY_SETTINGS;
private Settings transientSettings = Settings.EMPTY;
private Settings persistentSettings = Settings.EMPTY;

public ClusterUpdateSettingsRequest(StreamInput in) throws IOException {
super(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Objects;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;
Expand All @@ -49,7 +48,7 @@ public class RestoreSnapshotRequest extends MasterNodeRequest<RestoreSnapshotReq
private boolean includeGlobalState = false;
private boolean partial = false;
private boolean includeAliases = true;
private Settings indexSettings = EMPTY_SETTINGS;
private Settings indexSettings = Settings.EMPTY;
private String[] ignoreIndexSettings = Strings.EMPTY_ARRAY;

// This field does not get serialised (except toString for debugging purpose) because it is always set locally by authz
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ
private boolean copySettings;
private SystemDataStreamDescriptor systemDataStreamDescriptor;

private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private String mappings = "{}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.Set;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;

Expand All @@ -65,7 +64,7 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>

private String index;

private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private String mappings = "{}";

Expand Down Expand Up @@ -116,7 +115,7 @@ public CreateIndexRequest() {
* @param index the name of the index
*/
public CreateIndexRequest(String index) {
this(index, EMPTY_SETTINGS);
this(index, Settings.EMPTY);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class MetadataRolloverService {
private static final Pattern INDEX_NAME_PATTERN = Pattern.compile("^.*-\\d+$");
private static final List<IndexAbstraction.Type> VALID_ROLLOVER_TARGETS = List.of(ALIAS, DATA_STREAM);

public static final Settings HIDDEN_INDEX_SETTINGS = Settings.builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, true).build();

private final ThreadPool threadPool;
private final MetadataCreateIndexService createIndexService;
private final MetadataIndexAliasesService indexAliasesService;
Expand Down Expand Up @@ -272,7 +274,7 @@ static CreateIndexClusterStateUpdateRequest prepareDataStreamCreateIndexRequest(
final String targetIndexName,
CreateIndexRequest createIndexRequest,
final SystemDataStreamDescriptor descriptor) {
Settings settings = descriptor != null ? Settings.EMPTY : Settings.builder().put("index.hidden", true).build();
Settings settings = descriptor != null ? Settings.EMPTY : HIDDEN_INDEX_SETTINGS;
return prepareCreateIndexRequest(targetIndexName, targetIndexName, "rollover_data_stream", createIndexRequest, settings)
.dataStreamName(dataStreamName)
.systemDataStreamDescriptor(descriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;

/**
* Request for an update index settings action
Expand All @@ -43,7 +42,7 @@ public class UpdateSettingsRequest extends AcknowledgedRequest<UpdateSettingsReq

private String[] indices;
private IndicesOptions indicesOptions = DEFAULT_INDICES_OPTIONS;
private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;
private boolean preserveExisting = false;
private String origin = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.stream.Collectors;

import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;

Expand All @@ -62,7 +61,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR

private boolean create;

private Settings settings = EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

@Nullable
private String mappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ public static class Builder {
private long settingsVersion = 1;
private long aliasesVersion = 1;
private long[] primaryTerms = null;
private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;
private final ImmutableOpenMap.Builder<String, MappingMetadata> mappings;
private final ImmutableOpenMap.Builder<String, AliasMetadata> aliases;
private final ImmutableOpenMap.Builder<String, DiffableStringMap> customMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static class Builder {

private List<String> indexPatterns;

private Settings settings = Settings.Builder.EMPTY_SETTINGS;
private Settings settings = Settings.EMPTY;

private final ImmutableOpenMap.Builder<String, CompressedXContent> mappings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ public static class Builder {
private long version;

private CoordinationMetadata coordinationMetadata = CoordinationMetadata.EMPTY_METADATA;
private Settings transientSettings = Settings.Builder.EMPTY_SETTINGS;
private Settings persistentSettings = Settings.Builder.EMPTY_SETTINGS;
private Settings transientSettings = Settings.EMPTY;
private Settings persistentSettings = Settings.EMPTY;
private DiffableStringMap hashesOfConsistentSettings = DiffableStringMap.EMPTY;

private final ImmutableOpenMap.Builder<String, IndexMetadata> indices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
import org.elasticsearch.action.admin.indices.rollover.MetadataRolloverService;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.ActiveShardsObserver;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
Expand All @@ -22,7 +23,6 @@
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.DataStreamTimestampFieldMapper;
Expand Down Expand Up @@ -195,7 +195,7 @@ static ClusterState createDataStream(MetadataCreateIndexService metadataCreateIn
.systemDataStreamDescriptor(systemDataStreamDescriptor);

if (isSystem == false) {
createIndexRequest.settings(Settings.builder().put("index.hidden", true).build());
createIndexRequest.settings(MetadataRolloverService.HIDDEN_INDEX_SETTINGS);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ public static class PutRequest {
int order;
Integer version;
List<String> indexPatterns;
Settings settings = Settings.Builder.EMPTY_SETTINGS;
Settings settings = Settings.EMPTY;
String mappings = null;
List<Alias> aliases = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.elasticsearch.snapshots.SearchableSnapshotsSettings;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* The {@code DataTier} class encapsulates the formalization of the "content",
Expand All @@ -46,6 +46,12 @@ public class DataTier {

public static final String TIER_PREFERENCE = "index.routing.allocation.include._tier_preference";

private static final Settings DATA_CONTENT_TIER_PREFERENCE_SETTINGS = Settings.builder().put(TIER_PREFERENCE, DATA_CONTENT).build();

private static final Settings DATA_HOT_TIER_PREFERENCE_SETTINGS = Settings.builder().put(TIER_PREFERENCE, DATA_HOT).build();

private static final Settings NULL_TIER_PREFERENCE_SETTINGS = Settings.builder().putNull(TIER_PREFERENCE).build();

public static final Setting<String> TIER_PREFERENCE_SETTING = new Setting<>(
new Setting.SimpleKey(TIER_PREFERENCE),
DataTierSettingValidator::getDefaultTierPreference,
Expand All @@ -66,6 +72,24 @@ public class DataTier {
// Represents an ordered list of data tiers from frozen to hot (or slow to fast)
private static final List<String> ORDERED_FROZEN_TO_HOT_TIERS = List.of(DATA_FROZEN, DATA_COLD, DATA_WARM, DATA_HOT);

private static final Map<String, String> PREFERENCE_TIER_CONFIGURATIONS;

private static final Map<String, Settings> PREFERENCE_TIER_CONFIGURATION_SETTINGS;

static {
final Map<String, String> tmp = new HashMap<>();
final Map<String, Settings> tmpSettings = new HashMap<>();
for (int i = 0, ordered_frozen_to_hot_tiersSize = ORDERED_FROZEN_TO_HOT_TIERS.size(); i < ordered_frozen_to_hot_tiersSize; i++) {
String tier = ORDERED_FROZEN_TO_HOT_TIERS.get(i);
final String prefTierString =
String.join(",", ORDERED_FROZEN_TO_HOT_TIERS.subList(i, ORDERED_FROZEN_TO_HOT_TIERS.size())).intern();
tmp.put(tier, prefTierString);
tmpSettings.put(tier, Settings.builder().put(DataTier.TIER_PREFERENCE, prefTierString).build());
}
PREFERENCE_TIER_CONFIGURATIONS = Map.copyOf(tmp);
PREFERENCE_TIER_CONFIGURATION_SETTINGS = Map.copyOf(tmpSettings);
}

/**
* Returns true if the given tier name is a valid tier
*/
Expand All @@ -79,11 +103,19 @@ public static boolean validTierName(String tierName) {
* This is usually used in conjunction with {@link #TIER_PREFERENCE_SETTING}.
*/
public static String getPreferredTiersConfiguration(String targetTier) {
int indexOfTargetTier = ORDERED_FROZEN_TO_HOT_TIERS.indexOf(targetTier);
if (indexOfTargetTier == -1) {
final String res = PREFERENCE_TIER_CONFIGURATIONS.get(targetTier);
if (res == null) {
throw new IllegalArgumentException("invalid data tier [" + targetTier + "]");
}
return res;
}

public static Settings getPreferredTiersConfigurationSettings(String targetTier) {
final Settings res = PREFERENCE_TIER_CONFIGURATION_SETTINGS.get(targetTier);
if (res == null) {
throw new IllegalArgumentException("invalid data tier [" + targetTier + "]");
}
return ORDERED_FROZEN_TO_HOT_TIERS.stream().skip(indexOfTargetTier).collect(Collectors.joining(","));
return res;
}

/**
Expand Down Expand Up @@ -155,7 +187,7 @@ public Settings getAdditionalIndexSettings(String indexName, boolean isDataStrea
Set<String> settings = indexSettings.keySet();
if (settings.contains(TIER_PREFERENCE)) {
// just a marker -- this null value will be removed or overridden by the template/request settings
return Settings.builder().putNull(TIER_PREFERENCE).build();
return NULL_TIER_PREFERENCE_SETTINGS;
} else if (settings.stream().anyMatch(s -> s.startsWith(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + ".")) ||
settings.stream().anyMatch(s -> s.startsWith(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_PREFIX + ".")) ||
settings.stream().anyMatch(s -> s.startsWith(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_PREFIX + "."))) {
Expand All @@ -167,9 +199,9 @@ public Settings getAdditionalIndexSettings(String indexName, boolean isDataStrea
// tier if the index is part of a data stream, the "content"
// tier if it is not.
if (isDataStreamIndex) {
return Settings.builder().put(TIER_PREFERENCE, DATA_HOT).build();
return DATA_HOT_TIER_PREFERENCE_SETTINGS;
} else {
return Settings.builder().put(TIER_PREFERENCE, DATA_CONTENT).build();
return DATA_CONTENT_TIER_PREFERENCE_SETTINGS;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public class DiskThresholdMonitor {

private static final Logger logger = LogManager.getLogger(DiskThresholdMonitor.class);

private static final Settings READ_ONLY_ALLOW_DELETE_SETTINGS = Settings.builder()
.put(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE, Boolean.TRUE.toString()).build();

private static final Settings NOT_READ_ONLY_ALLOW_DELETE_SETTINGS =
Settings.builder().putNull(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE).build();

private final DiskThresholdSettings diskThresholdSettings;
private final Client client;
private final Supplier<ClusterState> clusterStateSupplier;
Expand Down Expand Up @@ -351,9 +357,7 @@ protected void updateIndicesReadOnly(Set<String> indicesToUpdate, ActionListener
setLastRunTimeMillis();
listener.onFailure(e);
});
Settings readOnlySettings = readOnly ? Settings.builder()
.put(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE, Boolean.TRUE.toString()).build() :
Settings.builder().putNull(IndexMetadata.SETTING_READ_ONLY_ALLOW_DELETE).build();
Settings readOnlySettings = readOnly ? READ_ONLY_ALLOW_DELETE_SETTINGS : NOT_READ_ONLY_ALLOW_DELETE_SETTINGS;
client.admin().indices().prepareUpdateSettings(indicesToUpdate.toArray(Strings.EMPTY_ARRAY))
.setSettings(readOnlySettings)
.execute(wrappedListener.map(r -> null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,6 @@ public Set<String> keySet() {
*/
public static class Builder {

public static final Settings EMPTY_SETTINGS = new Builder().build();

// we use a sorted map for consistent serialization when using getAsMap()
private final Map<String, Object> map = new TreeMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public IndexAnalyzers build(IndexSettings indexSettings,
if (analyzers.containsKey(DEFAULT_ANALYZER_NAME) == false) {
analyzers.put(DEFAULT_ANALYZER_NAME,
produceAnalyzer(DEFAULT_ANALYZER_NAME,
new StandardAnalyzerProvider(indexSettings, null, DEFAULT_ANALYZER_NAME, Settings.Builder.EMPTY_SETTINGS),
new StandardAnalyzerProvider(indexSettings, null, DEFAULT_ANALYZER_NAME, Settings.EMPTY),
tokenFilterFactoryFactories, charFilterFactoryFactories, tokenizerFactoryFactories));
}
NamedAnalyzer defaultAnalyzer = analyzers.get(DEFAULT_ANALYZER_NAME);
Expand Down
Loading

0 comments on commit 68ee6d4

Please sign in to comment.