Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -10,7 +10,7 @@
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.injection.guice.Inject;
Expand All @@ -32,6 +32,7 @@ public class CCRUsageTransportAction extends XPackUsageFeatureTransportAction {

private final Settings settings;
private final XPackLicenseState licenseState;
private final ProjectResolver projectResolver;

@Inject
public CCRUsageTransportAction(
Expand All @@ -40,11 +41,13 @@ public CCRUsageTransportAction(
ThreadPool threadPool,
ActionFilters actionFilters,
Settings settings,
XPackLicenseState licenseState
XPackLicenseState licenseState,
ProjectResolver projectResolver
) {
super(XPackUsageFeatureAction.CCR.name(), transportService, clusterService, threadPool, actionFilters);
this.settings = settings;
this.licenseState = licenseState;
this.projectResolver = projectResolver;
}

@Override
Expand All @@ -54,19 +57,19 @@ protected void localClusterStateOperation(
ClusterState state,
ActionListener<XPackUsageFeatureResponse> listener
) {
Metadata metadata = state.metadata();
final var project = projectResolver.getProjectMetadata(state);

int numberOfFollowerIndices = 0;
long lastFollowerIndexCreationDate = 0L;
for (IndexMetadata imd : metadata.getProject()) {
for (IndexMetadata imd : project) {
if (imd.getCustomData("ccr") != null) {
numberOfFollowerIndices++;
if (lastFollowerIndexCreationDate < imd.getCreationDate()) {
lastFollowerIndexCreationDate = imd.getCreationDate();
}
}
}
AutoFollowMetadata autoFollowMetadata = metadata.getProject().custom(AutoFollowMetadata.TYPE);
AutoFollowMetadata autoFollowMetadata = project.custom(AutoFollowMetadata.TYPE);
int numberOfAutoFollowPatterns = autoFollowMetadata != null ? autoFollowMetadata.getPatterns().size() : 0;

Long lastFollowTimeInMillis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.Maps;
Expand Down Expand Up @@ -92,7 +93,8 @@ public void testName() {
}

public void testUsageStats() throws Exception {
Metadata.Builder metadata = Metadata.builder();
final var projectId = randomProjectIdOrDefault();
ProjectMetadata.Builder project = ProjectMetadata.builder(projectId);

int numFollowerIndices = randomIntBetween(0, 32);
for (int i = 0; i < numFollowerIndices; i++) {
Expand All @@ -102,7 +104,7 @@ public void testUsageStats() throws Exception {
.numberOfReplicas(0)
.creationDate(i)
.putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>());
metadata.put(followerIndex);
project.put(followerIndex);
}

// Add a regular index, to check that we do not take that one into account:
Expand All @@ -111,7 +113,7 @@ public void testUsageStats() throws Exception {
.numberOfShards(1)
.numberOfReplicas(0)
.creationDate(numFollowerIndices);
metadata.put(regularIndex);
project.put(regularIndex);

int numAutoFollowPatterns = randomIntBetween(0, 32);
Map<String, AutoFollowMetadata.AutoFollowPattern> patterns = Maps.newMapWithExpectedSize(numAutoFollowPatterns);
Expand All @@ -136,9 +138,9 @@ public void testUsageStats() throws Exception {
);
patterns.put("pattern" + i, pattern);
}
metadata.putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()));
project.putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()));

ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).metadata(metadata).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).putProjectMetadata(project).build();
Mockito.when(clusterService.state()).thenReturn(clusterState);

ThreadPool threadPool = mock(ThreadPool.class);
Expand All @@ -149,7 +151,8 @@ public void testUsageStats() throws Exception {
threadPool,
mock(ActionFilters.class),
Settings.EMPTY,
licenseState
licenseState,
TestProjectResolvers.singleProject(projectId)
);
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
usageAction.localClusterStateOperation(null, null, clusterState, future);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetention;
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetentionSettings;
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.Tuple;
Expand All @@ -32,17 +33,20 @@
public class DataStreamLifecycleUsageTransportAction extends XPackUsageFeatureTransportAction {

private final DataStreamGlobalRetentionSettings globalRetentionSettings;
private final ProjectResolver projectResolver;

@Inject
public DataStreamLifecycleUsageTransportAction(
TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
DataStreamGlobalRetentionSettings globalRetentionSettings
DataStreamGlobalRetentionSettings globalRetentionSettings,
ProjectResolver projectResolver
) {
super(XPackUsageFeatureAction.DATA_STREAM_LIFECYCLE.name(), transportService, clusterService, threadPool, actionFilters);
this.globalRetentionSettings = globalRetentionSettings;
this.projectResolver = projectResolver;
}

@Override
Expand All @@ -52,7 +56,7 @@ protected void localClusterStateOperation(
ClusterState state,
ActionListener<XPackUsageFeatureResponse> listener
) {
final Collection<DataStream> dataStreams = state.metadata().getProject().dataStreams().values();
final Collection<DataStream> dataStreams = projectResolver.getProjectMetadata(state).dataStreams().values();
DataStreamLifecycleFeatureSetUsage.LifecycleStats lifecycleStats = calculateStats(
dataStreams,
clusterService.getClusterSettings().get(DataStreamLifecycle.CLUSTER_LIFECYCLE_DEFAULT_ROLLOVER_SETTING),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetention;
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetentionSettings;
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.Tuple;
Expand All @@ -33,6 +34,7 @@ public class DataStreamUsageTransportAction extends XPackUsageFeatureTransportAc

private final DataStreamFailureStoreSettings dataStreamFailureStoreSettings;
private final DataStreamGlobalRetentionSettings globalRetentionSettings;
private final ProjectResolver projectResolver;

@Inject
public DataStreamUsageTransportAction(
Expand All @@ -41,11 +43,13 @@ public DataStreamUsageTransportAction(
ThreadPool threadPool,
ActionFilters actionFilters,
DataStreamFailureStoreSettings dataStreamFailureStoreSettings,
DataStreamGlobalRetentionSettings globalRetentionSettings
DataStreamGlobalRetentionSettings globalRetentionSettings,
ProjectResolver projectResolver
) {
super(XPackUsageFeatureAction.DATA_STREAMS.name(), transportService, clusterService, threadPool, actionFilters);
this.dataStreamFailureStoreSettings = dataStreamFailureStoreSettings;
this.globalRetentionSettings = globalRetentionSettings;
this.projectResolver = projectResolver;
}

@Override
Expand All @@ -55,7 +59,7 @@ protected void localClusterStateOperation(
ClusterState state,
ActionListener<XPackUsageFeatureResponse> listener
) {
final Map<String, DataStream> dataStreams = state.metadata().getProject().dataStreams();
final Map<String, DataStream> dataStreams = projectResolver.getProjectMetadata(state).dataStreams();
long backingIndicesCounter = 0;
long failureStoreExplicitlyEnabledCounter = 0;
long failureStoreEffectivelyEnabledCounter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.ParentTaskAssigningClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.DataTier;
Expand Down Expand Up @@ -43,17 +45,20 @@
public class DataTiersUsageTransportAction extends XPackUsageFeatureTransportAction {

private final Client client;
private final ProjectResolver projectResolver;

@Inject
public DataTiersUsageTransportAction(
TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
Client client
Client client,
ProjectResolver projectResolver
) {
super(XPackUsageFeatureAction.DATA_TIERS.name(), transportService, clusterService, threadPool, actionFilters);
this.client = client;
this.projectResolver = projectResolver;
}

@Override
Expand All @@ -69,11 +74,12 @@ protected void localClusterStateOperation(
NodesDataTiersUsageTransportAction.TYPE,
new NodesDataTiersUsageTransportAction.NodesRequest(),
listener.delegateFailureAndWrap((delegate, response) -> {
final var projectState = projectResolver.getProjectState(state);
// Generate tier specific stats for the nodes and indices
delegate.onResponse(
new XPackUsageFeatureResponse(
new DataTiersFeatureSetUsage(
aggregateStats(response.getNodes(), getIndicesGroupedByTier(state, response.getNodes()))
aggregateStats(response.getNodes(), getIndicesGroupedByTier(projectState, response.getNodes()))
)
)
);
Expand All @@ -82,16 +88,16 @@ protected void localClusterStateOperation(
}

// Visible for testing
static Map<String, Set<String>> getIndicesGroupedByTier(ClusterState state, List<NodeDataTiersUsage> nodes) {
static Map<String, Set<String>> getIndicesGroupedByTier(ProjectState state, List<NodeDataTiersUsage> nodes) {
Set<String> indices = nodes.stream()
.map(nodeResponse -> state.getRoutingNodes().node(nodeResponse.getNode().getId()))
.map(nodeResponse -> state.cluster().getRoutingNodes().node(nodeResponse.getNode().getId()))
.filter(Objects::nonNull)
.flatMap(node -> StreamSupport.stream(node.spliterator(), false))
.map(ShardRouting::getIndexName)
.collect(Collectors.toSet());
Map<String, Set<String>> indicesByTierPreference = new HashMap<>();
for (String indexName : indices) {
IndexMetadata indexMetadata = state.metadata().getProject().index(indexName);
IndexMetadata indexMetadata = state.metadata().index(indexName);
// If the index was deleted in the meantime, skip
if (indexMetadata == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -57,15 +57,14 @@ public void testTierIndices() {
IndexMetadata nonTiered = indexMetadata("non-tier", 1, 0); // No tier
IndexMetadata hotIndex3 = indexMetadata("hot-3", 1, 0, DataTier.DATA_HOT);

Metadata.Builder metadataBuilder = Metadata.builder()
ProjectMetadata.Builder projectBuilder = ProjectMetadata.builder(randomProjectIdOrDefault())
.put(hotIndex1, false)
.put(hotIndex2, false)
.put(warmIndex1, false)
.put(coldIndex1, false)
.put(coldIndex2, false)
.put(nonTiered, false)
.put(hotIndex3, false)
.generateClusterUuidIfNeeded();
.put(hotIndex3, false);
RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
routingTableBuilder.add(getIndexRoutingTable(hotIndex1, dataNode));
routingTableBuilder.add(getIndexRoutingTable(hotIndex2, dataNode));
Expand All @@ -76,11 +75,11 @@ public void testTierIndices() {
routingTableBuilder.add(getIndexRoutingTable(nonTiered, dataNode));
ClusterState clusterState = ClusterState.builder(new ClusterName("test"))
.nodes(discoBuilder)
.metadata(metadataBuilder)
.routingTable(routingTableBuilder.build())
.putProjectMetadata(projectBuilder)
.putRoutingTable(projectBuilder.getId(), routingTableBuilder.build())
.build();
Map<String, Set<String>> result = DataTiersUsageTransportAction.getIndicesGroupedByTier(
clusterState,
clusterState.projectState(projectBuilder.getId()),
List.of(new NodeDataTiersUsage(dataNode, Map.of(DataTier.DATA_WARM, createStats(5, 5, 0, 10))))
);
assertThat(result.keySet(), equalTo(Set.of(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.elasticsearch.action.admin.indices.rollover.RolloverConditions;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.injection.guice.Inject;
Expand Down Expand Up @@ -42,14 +42,18 @@

public class IndexLifecycleUsageTransportAction extends XPackUsageFeatureTransportAction {

private final ProjectResolver projectResolver;

@Inject
public IndexLifecycleUsageTransportAction(
TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters
ActionFilters actionFilters,
ProjectResolver projectResolver
) {
super(XPackUsageFeatureAction.INDEX_LIFECYCLE.name(), transportService, clusterService, threadPool, actionFilters);
this.projectResolver = projectResolver;
}

@Override
Expand All @@ -59,12 +63,12 @@ protected void localClusterStateOperation(
ClusterState state,
ActionListener<XPackUsageFeatureResponse> listener
) {
Metadata metadata = state.metadata();
IndexLifecycleMetadata lifecycleMetadata = metadata.getProject().custom(IndexLifecycleMetadata.TYPE);
final var project = projectResolver.getProjectMetadata(state);
IndexLifecycleMetadata lifecycleMetadata = project.custom(IndexLifecycleMetadata.TYPE);
final IndexLifecycleFeatureSetUsage usage;
if (lifecycleMetadata != null) {
Map<String, Integer> policyUsage = new HashMap<>();
metadata.getProject().indices().values().forEach(value -> {
project.indices().values().forEach(value -> {
String policyName = value.getLifecyclePolicyName();
Integer indicesManaged = policyUsage.get(policyName);
if (indicesManaged == null) {
Expand Down
Loading