-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Prevent NPE when generating snapshot metrics before initial cluster state is set #136350
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
Open
nicktindall
wants to merge
16
commits into
elastic:main
Choose a base branch
from
nicktindall:fix_npe_snapshot_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−94
Open
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b3c35ce
Fix checkstyle
nicktindall 4214472
Generate snapshot metrics from last applied state
nicktindall f46ef6e
Merge remote-tracking branch 'origin/main' into fix_npe_snapshot_metrics
nicktindall 4d7ab3d
Update docs/changelog/136350.yaml
nicktindall f1c7b95
Split out metrics calculation
nicktindall 9e926ae
Merge remote-tracking branch 'origin/main' into fix_npe_snapshot_metrics
nicktindall b312683
Merge branch 'fix_npe_snapshot_metrics' of github.com:nicktindall/ela…
nicktindall 8c72da5
Fix changelog
nicktindall 3c5bb9e
Clean up remnants
nicktindall 3684b57
Remove redundant staleness check
nicktindall a9e46fe
Add test for no-longer master
nicktindall 1a79b92
Use ClusterService lifecycle to decide when to poll
nicktindall 6f8978c
Test whole lifecyle
nicktindall ee4a21c
Merge remote-tracking branch 'origin/main' into fix_npe_snapshot_metrics
nicktindall e3146c6
Make minimum nodes 2
nicktindall 20715a3
Use original cluster state when creating snapshotsInProgress
nicktindall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 136350 | ||
summary: Prevent NPE when generating snapshot metrics before initial cluster state is set | ||
area: Snapshot/Restore | ||
type: bug | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
server/src/main/java/org/elasticsearch/snapshots/SnapshotMetricsService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.snapshots; | ||
|
||
import org.elasticsearch.cluster.ClusterChangedEvent; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.ClusterStateListener; | ||
import org.elasticsearch.cluster.SnapshotsInProgress; | ||
import org.elasticsearch.cluster.metadata.RepositoriesMetadata; | ||
import org.elasticsearch.cluster.metadata.RepositoryMetadata; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.core.Tuple; | ||
import org.elasticsearch.gateway.GatewayService; | ||
import org.elasticsearch.repositories.SnapshotMetrics; | ||
import org.elasticsearch.telemetry.metric.LongWithAttributes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Generates the snapshots-by-state and shards-by-state metrics when polled. Only produces | ||
* metrics on the master node, and only after it's seen a cluster state applied. | ||
*/ | ||
public class SnapshotMetricsService implements ClusterStateListener { | ||
|
||
private final ClusterService clusterService; | ||
private volatile boolean shouldReturnSnapshotMetrics; | ||
private CachedSnapshotStateMetrics cachedSnapshotStateMetrics; | ||
|
||
public SnapshotMetricsService(SnapshotMetrics snapshotMetrics, ClusterService clusterService) { | ||
this.clusterService = clusterService; | ||
snapshotMetrics.createSnapshotShardsByStateMetric(this::getShardsByState); | ||
snapshotMetrics.createSnapshotsByStateMetric(this::getSnapshotsByState); | ||
} | ||
|
||
@Override | ||
public void clusterChanged(ClusterChangedEvent event) { | ||
final ClusterState clusterState = event.state(); | ||
// Only return metrics when the state is recovered and we are the master | ||
shouldReturnSnapshotMetrics = clusterState.nodes().isLocalNodeElectedMaster() | ||
&& clusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) == false; | ||
} | ||
|
||
private Collection<LongWithAttributes> getShardsByState() { | ||
if (shouldReturnSnapshotMetrics == false) { | ||
return List.of(); | ||
} | ||
return recalculateIfStale(clusterService.state()).shardStateMetrics(); | ||
} | ||
|
||
private Collection<LongWithAttributes> getSnapshotsByState() { | ||
if (shouldReturnSnapshotMetrics == false) { | ||
return List.of(); | ||
} | ||
return recalculateIfStale(clusterService.state()).snapshotStateMetrics(); | ||
} | ||
|
||
private CachedSnapshotStateMetrics recalculateIfStale(ClusterState currentState) { | ||
if (cachedSnapshotStateMetrics == null || cachedSnapshotStateMetrics.isStale(currentState)) { | ||
cachedSnapshotStateMetrics = recalculateSnapshotStats(currentState); | ||
} | ||
return cachedSnapshotStateMetrics; | ||
} | ||
|
||
private CachedSnapshotStateMetrics recalculateSnapshotStats(ClusterState currentState) { | ||
final SnapshotsInProgress snapshotsInProgress = SnapshotsInProgress.get(currentState); | ||
final List<LongWithAttributes> snapshotStateMetrics = new ArrayList<>(); | ||
final List<LongWithAttributes> shardStateMetrics = new ArrayList<>(); | ||
|
||
currentState.metadata().projects().forEach((projectId, project) -> { | ||
final RepositoriesMetadata repositoriesMetadata = RepositoriesMetadata.get(project); | ||
if (repositoriesMetadata != null) { | ||
for (RepositoryMetadata repository : repositoriesMetadata.repositories()) { | ||
final Tuple<Map<SnapshotsInProgress.State, Integer>, Map<SnapshotsInProgress.ShardState, Integer>> stateSummaries = | ||
snapshotsInProgress.shardStateSummaryForRepository(projectId, repository.name()); | ||
final Map<String, Object> attributesMap = SnapshotMetrics.createAttributesMap(projectId, repository); | ||
stateSummaries.v1() | ||
.forEach( | ||
(snapshotState, count) -> snapshotStateMetrics.add( | ||
new LongWithAttributes(count, Maps.copyMapWithAddedEntry(attributesMap, "state", snapshotState.name())) | ||
) | ||
); | ||
stateSummaries.v2() | ||
.forEach( | ||
(shardState, count) -> shardStateMetrics.add( | ||
new LongWithAttributes(count, Maps.copyMapWithAddedEntry(attributesMap, "state", shardState.name())) | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
return new CachedSnapshotStateMetrics(currentState, snapshotStateMetrics, shardStateMetrics); | ||
} | ||
|
||
/** | ||
* A cached copy of the snapshot and shard state metrics | ||
*/ | ||
private record CachedSnapshotStateMetrics( | ||
String clusterStateId, | ||
int snapshotsInProgressIdentityHashcode, | ||
Collection<LongWithAttributes> snapshotStateMetrics, | ||
Collection<LongWithAttributes> shardStateMetrics | ||
) { | ||
CachedSnapshotStateMetrics( | ||
ClusterState sourceState, | ||
Collection<LongWithAttributes> snapshotStateMetrics, | ||
Collection<LongWithAttributes> shardStateMetrics | ||
) { | ||
this( | ||
sourceState.stateUUID(), | ||
System.identityHashCode(SnapshotsInProgress.get(sourceState)), | ||
snapshotStateMetrics, | ||
shardStateMetrics | ||
); | ||
} | ||
|
||
/** | ||
* Are these metrics stale? | ||
* | ||
* @param currentClusterState The current cluster state | ||
* @return true if these metrics were calculated from a prior cluster state and need to be recalculated, false otherwise | ||
*/ | ||
public boolean isStale(ClusterState currentClusterState) { | ||
return (Objects.equals(clusterStateId, currentClusterState.stateUUID()) == false | ||
&& System.identityHashCode(SnapshotsInProgress.get(currentClusterState)) != snapshotsInProgressIdentityHashcode); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a slight race if the node is demoted from master, it could happen just after we evaluate
shouldReturnSnapshotMetrics == true
, but it's no big deal and definitely not worth synchronizing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the same issue with other Gauge metrics. I don't think it's important enough to address.