Explorar el Código

Mark monitoring code as `NotMultiProjectCapable` (#131414)

The monitoring plugin is not available in serverless, so we're simply
marking the code as not being multi-project capable.
Niels Bauman hace 3 meses
padre
commit
057d78c0a5

+ 9 - 5
x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java

@@ -11,10 +11,12 @@ import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
 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;
@@ -79,13 +81,15 @@ public class IndexStatsCollector extends Collector {
 
         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
@@ -98,7 +102,7 @@ public class IndexStatsCollector extends Collector {
                         interval,
                         node,
                         indexStats,
-                        metadata.getProject().index(indexName),
+                        metadata.index(indexName),
                         routingTable.index(indexName)
                     )
                 );

+ 4 - 1
x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java

@@ -8,12 +8,14 @@ package org.elasticsearch.xpack.monitoring.collector.shards;
 
 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;
@@ -48,7 +50,8 @@ public class ShardsCollector extends Collector {
         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();

+ 12 - 4
x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java

@@ -19,6 +19,7 @@ import org.elasticsearch.cluster.ClusterState;
 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;
@@ -28,6 +29,7 @@ import org.elasticsearch.common.settings.Setting.Property;
 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;
@@ -395,7 +397,8 @@ public final class LocalExporter extends Exporter implements ClusterStateListene
         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
@@ -434,7 +437,8 @@ public final class LocalExporter extends Exporter implements ClusterStateListene
     ) {
         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
@@ -472,7 +476,9 @@ public final class LocalExporter extends Exporter implements ClusterStateListene
     }
 
     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);
     }
@@ -633,7 +639,9 @@ public final class LocalExporter extends Exporter implements ClusterStateListene
             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)) {

+ 2 - 1
x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java

@@ -30,6 +30,7 @@ import org.elasticsearch.xpack.monitoring.collector.Collector;
 
 import java.util.function.Function;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -54,7 +55,7 @@ public abstract class BaseCollectorTestCase extends ESTestCase {
         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);

+ 2 - 1
x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java

@@ -8,6 +8,7 @@ package org.elasticsearch.xpack.monitoring;
 
 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;
@@ -109,7 +110,7 @@ public class MultiNodesStatsTests extends MonitoringIntegTestCase {
                 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;
                 }

+ 2 - 1
x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java

@@ -35,6 +35,7 @@ import static org.hamcrest.Matchers.equalTo;
 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;
@@ -70,7 +71,7 @@ public class IndexStatsCollectorTests extends BaseCollectorTestCase {
         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());

+ 2 - 1
x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java

@@ -38,6 +38,7 @@ import static org.hamcrest.Matchers.is;
 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;
@@ -92,7 +93,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase {
         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);