Skip to content

Commit

Permalink
Optimize IndexLifecycleMetadata#getPolicies (elastic#116988)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo authored and alexey-ivanov-es committed Nov 28, 2024
1 parent e511bd2 commit 5bfc0c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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

0 comments on commit 5bfc0c7

Please sign in to comment.