Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize IndexLifecycleMetadata#getPolicies #116988

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,22 @@ public class IndexLifecycleMetadata implements Metadata.Custom {

private final Map<String, LifecyclePolicyMetadata> policyMetadatas;
private final OperationMode operationMode;
// a slightly different view of the policyMetadatas -- it's hot in a couple of places so we pre-calculate it
private final Map<String, LifecyclePolicy> policies;

private static Map<String, LifecyclePolicy> policiesMap(final Map<String, LifecyclePolicyMetadata> policyMetadatas) {
final Map<String, LifecyclePolicy> policies = new HashMap<>(policyMetadatas.size());
for (LifecyclePolicyMetadata policyMetadata : policyMetadatas.values()) {
LifecyclePolicy policy = policyMetadata.getPolicy();
policies.put(policy.getName(), policy);
}
return Collections.unmodifiableMap(policies);
}

public IndexLifecycleMetadata(Map<String, LifecyclePolicyMetadata> policies, OperationMode operationMode) {
this.policyMetadatas = Collections.unmodifiableMap(policies);
this.operationMode = operationMode;
this.policies = policiesMap(policyMetadatas);
}

public IndexLifecycleMetadata(StreamInput in) throws IOException {
Expand All @@ -72,6 +84,7 @@ public IndexLifecycleMetadata(StreamInput in) throws IOException {
}
this.policyMetadatas = policies;
this.operationMode = in.readEnum(OperationMode.class);
this.policies = policiesMap(policyMetadatas);
}

@Override
Expand All @@ -93,13 +106,7 @@ public OperationMode getOperationMode() {
}

public Map<String, LifecyclePolicy> getPolicies() {
// note: this loop is unrolled rather than streaming-style because it's hot enough to show up in a flamegraph
Map<String, LifecyclePolicy> policies = new HashMap<>(policyMetadatas.size());
for (LifecyclePolicyMetadata policyMetadata : policyMetadatas.values()) {
LifecyclePolicy policy = policyMetadata.getPolicy();
policies.put(policy.getName(), policy);
}
return Collections.unmodifiableMap(policies);
return policies;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,16 @@ private void addIndexLifecyclePoliciesIfMissing(ClusterState state) {
logger.trace("running in data stream lifecycle only mode. skipping the installation of ILM policies.");
return;
}
IndexLifecycleMetadata metadata = state.metadata().custom(IndexLifecycleMetadata.TYPE);
final IndexLifecycleMetadata metadata = state.metadata().custom(IndexLifecycleMetadata.TYPE);
final Map<String, LifecyclePolicy> policies = metadata != null ? metadata.getPolicies() : Map.of();

for (LifecyclePolicy policy : getLifecyclePolicies()) {
final AtomicBoolean creationCheck = policyCreationsInProgress.computeIfAbsent(
policy.getName(),
key -> new AtomicBoolean(false)
);
if (creationCheck.compareAndSet(false, true)) {
final LifecyclePolicy currentPolicy = metadata != null ? metadata.getPolicies().get(policy.getName()) : null;
final LifecyclePolicy currentPolicy = policies.get(policy.getName());
if (Objects.isNull(currentPolicy)) {
logger.debug("adding lifecycle policy [{}] for [{}], because it doesn't exist", policy.getName(), getOrigin());
putPolicy(policy, creationCheck);
Expand Down