Skip to content

Mark monitoring code as NotMultiProjectCapable #131414

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -11,10 +11,12 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.core.NotMultiProjectCapable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
Expand Down Expand Up @@ -79,13 +81,15 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin

final long timestamp = timestamp();
final String clusterUuid = clusterUuid(clusterState);
final Metadata metadata = clusterState.metadata();
final RoutingTable routingTable = clusterState.routingTable();
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
final var projectId = ProjectId.DEFAULT;
final ProjectMetadata metadata = clusterState.metadata().getProject(projectId);
final RoutingTable routingTable = clusterState.routingTable(projectId);

// Filters the indices stats to only return the statistics for the indices known by the collector's
// local cluster state. This way indices/index/shards stats all share a common view of indices state.
final List<IndexStats> indicesStats = new ArrayList<>();
for (final String indexName : metadata.getProject().getConcreteAllIndices()) {
for (final String indexName : metadata.getConcreteAllIndices()) {
final IndexStats indexStats = indicesStatsResponse.getIndex(indexName);
if (indexStats != null) {
// The index appears both in the local cluster state and indices stats response
Expand All @@ -98,7 +102,7 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin
interval,
node,
indexStats,
metadata.getProject().index(indexName),
metadata.index(indexName),
routingTable.index(indexName)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.core.NotMultiProjectCapable;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
import org.elasticsearch.xpack.monitoring.collector.Collector;
Expand Down Expand Up @@ -48,7 +50,8 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin
throws Exception {
final List<MonitoringDoc> results = new ArrayList<>(1);
if (clusterState != null) {
RoutingTable routingTable = clusterState.routingTable();
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
RoutingTable routingTable = clusterState.routingTable(ProjectId.DEFAULT);
if (routingTable != null) {
final String clusterUuid = clusterUuid(clusterState);
final String stateUUID = clusterState.stateUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.bytes.BytesArray;
Expand All @@ -28,6 +29,7 @@
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.core.FixForMultiProject;
import org.elasticsearch.core.NotMultiProjectCapable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.gateway.GatewayService;
Expand Down Expand Up @@ -395,7 +397,8 @@ private void setupClusterAlertsTasks(
boolean shouldSetUpWatcher = state.get() == State.RUNNING && clusterStateChange == false;
if (canUseWatcher()) {
if (shouldSetUpWatcher) {
final IndexRoutingTable watches = clusterState.routingTable().index(Watch.INDEX);
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
final IndexRoutingTable watches = clusterState.routingTable(ProjectId.DEFAULT).index(Watch.INDEX);
final boolean indexExists = watches != null && watches.allPrimaryShardsActive();

// we cannot do anything with watches until the index is allocated, so we wait until it's ready
Expand Down Expand Up @@ -434,7 +437,8 @@ private void removeClusterAlertsTasks(
) {
if (canUseWatcher()) {
if (state.get() != State.TERMINATED) {
final IndexRoutingTable watches = clusterState.routingTable().index(Watch.INDEX);
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
final IndexRoutingTable watches = clusterState.routingTable(ProjectId.DEFAULT).index(Watch.INDEX);
final boolean indexExists = watches != null && watches.allPrimaryShardsActive();

// we cannot do anything with watches until the index is allocated, so we wait until it's ready
Expand Down Expand Up @@ -472,7 +476,9 @@ private void responseReceived(
}

private static boolean hasTemplate(final ClusterState clusterState, final String templateName) {
final IndexTemplateMetadata template = clusterState.getMetadata().getProject().templates().get(templateName);
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
final var project = clusterState.metadata().getProject(ProjectId.DEFAULT);
final IndexTemplateMetadata template = project.templates().get(templateName);

return template != null && hasValidVersion(template.getVersion(), MonitoringTemplateRegistry.REGISTRY_VERSION);
}
Expand Down Expand Up @@ -633,7 +639,9 @@ public void onCleanUpIndices(TimeValue retention) {
currents.add(MonitoringTemplateRegistry.ALERTS_INDEX_TEMPLATE_NAME);

Set<String> indices = new HashSet<>();
for (var index : clusterState.getMetadata().getProject().indices().entrySet()) {
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
final var project = clusterState.metadata().getProject(ProjectId.DEFAULT);
for (var index : project.indices().entrySet()) {
String indexName = index.getKey();

if (Regex.simpleMatch(indexPatterns, indexName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.function.Function;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -54,7 +55,7 @@ public void setUp() throws Exception {
nodes = mock(DiscoveryNodes.class);
metadata = mock(Metadata.class);
projectMetadata = mock(ProjectMetadata.class);
when(metadata.getProject()).thenReturn(projectMetadata);
when(metadata.getProject(any())).thenReturn(projectMetadata);
licenseState = mock(MockLicenseState.class);
client = mock(Client.class);
ThreadPool threadPool = mock(ThreadPool.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.query.QueryBuilders;
Expand Down Expand Up @@ -109,7 +110,7 @@ private void waitForMonitoringIndices() throws Exception {
return false;
}
for (Index index : indices) {
final var indexRoutingTable = cs.routingTable().index(index);
final var indexRoutingTable = cs.routingTable(ProjectId.DEFAULT).index(index);
if (indexRoutingTable.allPrimaryShardsActive() == false) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void testDoCollect() throws Exception {
whenClusterStateWithUUID(clusterUUID);

final RoutingTable routingTable = mock(RoutingTable.class);
when(clusterState.routingTable()).thenReturn(routingTable);
when(clusterState.routingTable(any())).thenReturn(routingTable);

final IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
final MonitoringDoc.Node node = randomMonitoringNode(random());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void testDoCollect() throws Exception {
withCollectionIndices(indices);

final RoutingTable routingTable = mockRoutingTable();
when(clusterState.routingTable()).thenReturn(routingTable);
when(clusterState.routingTable(any())).thenReturn(routingTable);

final DiscoveryNode localNode = localNode("_current");
final MonitoringDoc.Node node = Collector.convertNode(randomNonNegativeLong(), localNode);
Expand Down