|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.snapshots; |
| 11 | + |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
| 14 | +import org.elasticsearch.cluster.metadata.RepositoriesMetadata; |
| 15 | +import org.elasticsearch.cluster.metadata.RepositoryMetadata; |
| 16 | +import org.elasticsearch.cluster.service.ClusterService; |
| 17 | +import org.elasticsearch.common.component.Lifecycle; |
| 18 | +import org.elasticsearch.common.util.Maps; |
| 19 | +import org.elasticsearch.core.Tuple; |
| 20 | +import org.elasticsearch.repositories.SnapshotMetrics; |
| 21 | +import org.elasticsearch.telemetry.metric.LongWithAttributes; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * Generates the snapshots-by-state and shards-by-state metrics when polled. Only produces |
| 30 | + * metrics on the master node, and only while the {@link ClusterService} is started. Will only |
| 31 | + * re-calculate the metrics if the {@link SnapshotsInProgress} has changed since the last time |
| 32 | + * they were calculated. |
| 33 | + */ |
| 34 | +public class CachingSnapshotAndShardByStateMetricsService { |
| 35 | + |
| 36 | + private final ClusterService clusterService; |
| 37 | + private volatile CachedSnapshotStateMetrics cachedSnapshotStateMetrics; |
| 38 | + |
| 39 | + public CachingSnapshotAndShardByStateMetricsService(ClusterService clusterService) { |
| 40 | + this.clusterService = clusterService; |
| 41 | + } |
| 42 | + |
| 43 | + public Collection<LongWithAttributes> getShardsByState() { |
| 44 | + if (clusterService.lifecycleState() != Lifecycle.State.STARTED) { |
| 45 | + return List.of(); |
| 46 | + } |
| 47 | + final ClusterState state = clusterService.state(); |
| 48 | + if (state.nodes().isLocalNodeElectedMaster() == false) { |
| 49 | + // Only the master should report on shards-by-state |
| 50 | + return List.of(); |
| 51 | + } |
| 52 | + return recalculateIfStale(state).shardStateMetrics(); |
| 53 | + } |
| 54 | + |
| 55 | + public Collection<LongWithAttributes> getSnapshotsByState() { |
| 56 | + if (clusterService.lifecycleState() != Lifecycle.State.STARTED) { |
| 57 | + return List.of(); |
| 58 | + } |
| 59 | + final ClusterState state = clusterService.state(); |
| 60 | + if (state.nodes().isLocalNodeElectedMaster() == false) { |
| 61 | + // Only the master should report on snapshots-by-state |
| 62 | + return List.of(); |
| 63 | + } |
| 64 | + return recalculateIfStale(state).snapshotStateMetrics(); |
| 65 | + } |
| 66 | + |
| 67 | + private CachedSnapshotStateMetrics recalculateIfStale(ClusterState currentState) { |
| 68 | + if (cachedSnapshotStateMetrics == null || cachedSnapshotStateMetrics.isStale(currentState)) { |
| 69 | + cachedSnapshotStateMetrics = recalculateSnapshotStats(currentState); |
| 70 | + } |
| 71 | + return cachedSnapshotStateMetrics; |
| 72 | + } |
| 73 | + |
| 74 | + private CachedSnapshotStateMetrics recalculateSnapshotStats(ClusterState currentState) { |
| 75 | + final SnapshotsInProgress snapshotsInProgress = SnapshotsInProgress.get(currentState); |
| 76 | + final List<LongWithAttributes> snapshotStateMetrics = new ArrayList<>(); |
| 77 | + final List<LongWithAttributes> shardStateMetrics = new ArrayList<>(); |
| 78 | + |
| 79 | + currentState.metadata().projects().forEach((projectId, project) -> { |
| 80 | + final RepositoriesMetadata repositoriesMetadata = RepositoriesMetadata.get(project); |
| 81 | + if (repositoriesMetadata != null) { |
| 82 | + for (RepositoryMetadata repository : repositoriesMetadata.repositories()) { |
| 83 | + final Tuple<Map<SnapshotsInProgress.State, Integer>, Map<SnapshotsInProgress.ShardState, Integer>> stateSummaries = |
| 84 | + snapshotsInProgress.shardStateSummaryForRepository(projectId, repository.name()); |
| 85 | + final Map<String, Object> attributesMap = SnapshotMetrics.createAttributesMap(projectId, repository); |
| 86 | + stateSummaries.v1() |
| 87 | + .forEach( |
| 88 | + (snapshotState, count) -> snapshotStateMetrics.add( |
| 89 | + new LongWithAttributes(count, Maps.copyMapWithAddedEntry(attributesMap, "state", snapshotState.name())) |
| 90 | + ) |
| 91 | + ); |
| 92 | + stateSummaries.v2() |
| 93 | + .forEach( |
| 94 | + (shardState, count) -> shardStateMetrics.add( |
| 95 | + new LongWithAttributes(count, Maps.copyMapWithAddedEntry(attributesMap, "state", shardState.name())) |
| 96 | + ) |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + return new CachedSnapshotStateMetrics(currentState, snapshotStateMetrics, shardStateMetrics); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * A cached copy of the snapshot and shard state metrics |
| 106 | + */ |
| 107 | + private record CachedSnapshotStateMetrics( |
| 108 | + String clusterStateId, |
| 109 | + int snapshotsInProgressIdentityHashcode, |
| 110 | + Collection<LongWithAttributes> snapshotStateMetrics, |
| 111 | + Collection<LongWithAttributes> shardStateMetrics |
| 112 | + ) { |
| 113 | + CachedSnapshotStateMetrics( |
| 114 | + ClusterState sourceState, |
| 115 | + Collection<LongWithAttributes> snapshotStateMetrics, |
| 116 | + Collection<LongWithAttributes> shardStateMetrics |
| 117 | + ) { |
| 118 | + this( |
| 119 | + sourceState.stateUUID(), |
| 120 | + System.identityHashCode(SnapshotsInProgress.get(sourceState)), |
| 121 | + snapshotStateMetrics, |
| 122 | + shardStateMetrics |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Are these metrics stale? |
| 128 | + * |
| 129 | + * @param currentClusterState The current cluster state |
| 130 | + * @return true if these metrics were calculated from a prior {@link SnapshotsInProgress} and need to be recalculated, false |
| 131 | + * otherwise |
| 132 | + */ |
| 133 | + public boolean isStale(ClusterState currentClusterState) { |
| 134 | + return System.identityHashCode(SnapshotsInProgress.get(currentClusterState)) != snapshotsInProgressIdentityHashcode; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments