Skip to content

Commit 057d78c

Browse files
authored
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.
1 parent 3cd2ecb commit 057d78c

File tree

7 files changed

+33
-14
lines changed

7 files changed

+33
-14
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import org.elasticsearch.action.support.IndicesOptions;
1212
import org.elasticsearch.client.internal.Client;
1313
import org.elasticsearch.cluster.ClusterState;
14-
import org.elasticsearch.cluster.metadata.Metadata;
14+
import org.elasticsearch.cluster.metadata.ProjectId;
15+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1516
import org.elasticsearch.cluster.routing.RoutingTable;
1617
import org.elasticsearch.cluster.service.ClusterService;
1718
import org.elasticsearch.common.settings.Setting;
19+
import org.elasticsearch.core.NotMultiProjectCapable;
1820
import org.elasticsearch.core.TimeValue;
1921
import org.elasticsearch.license.XPackLicenseState;
2022
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
@@ -79,13 +81,15 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin
7981

8082
final long timestamp = timestamp();
8183
final String clusterUuid = clusterUuid(clusterState);
82-
final Metadata metadata = clusterState.metadata();
83-
final RoutingTable routingTable = clusterState.routingTable();
84+
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
85+
final var projectId = ProjectId.DEFAULT;
86+
final ProjectMetadata metadata = clusterState.metadata().getProject(projectId);
87+
final RoutingTable routingTable = clusterState.routingTable(projectId);
8488

8589
// Filters the indices stats to only return the statistics for the indices known by the collector's
8690
// local cluster state. This way indices/index/shards stats all share a common view of indices state.
8791
final List<IndexStats> indicesStats = new ArrayList<>();
88-
for (final String indexName : metadata.getProject().getConcreteAllIndices()) {
92+
for (final String indexName : metadata.getConcreteAllIndices()) {
8993
final IndexStats indexStats = indicesStatsResponse.getIndex(indexName);
9094
if (indexStats != null) {
9195
// The index appears both in the local cluster state and indices stats response
@@ -98,7 +102,7 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin
98102
interval,
99103
node,
100104
indexStats,
101-
metadata.getProject().index(indexName),
105+
metadata.index(indexName),
102106
routingTable.index(indexName)
103107
)
104108
);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
import org.elasticsearch.cluster.ClusterState;
1010
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
11+
import org.elasticsearch.cluster.metadata.ProjectId;
1112
import org.elasticsearch.cluster.routing.IndexRoutingTable;
1213
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
1314
import org.elasticsearch.cluster.routing.RoutingTable;
1415
import org.elasticsearch.cluster.routing.ShardRouting;
1516
import org.elasticsearch.cluster.service.ClusterService;
1617
import org.elasticsearch.common.regex.Regex;
18+
import org.elasticsearch.core.NotMultiProjectCapable;
1719
import org.elasticsearch.license.XPackLicenseState;
1820
import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc;
1921
import org.elasticsearch.xpack.monitoring.collector.Collector;
@@ -48,7 +50,8 @@ protected Collection<MonitoringDoc> doCollect(final MonitoringDoc.Node node, fin
4850
throws Exception {
4951
final List<MonitoringDoc> results = new ArrayList<>(1);
5052
if (clusterState != null) {
51-
RoutingTable routingTable = clusterState.routingTable();
53+
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
54+
RoutingTable routingTable = clusterState.routingTable(ProjectId.DEFAULT);
5255
if (routingTable != null) {
5356
final String clusterUuid = clusterUuid(clusterState);
5457
final String stateUUID = clusterState.stateUUID();

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.cluster.ClusterStateListener;
2020
import org.elasticsearch.cluster.block.ClusterBlockLevel;
2121
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
22+
import org.elasticsearch.cluster.metadata.ProjectId;
2223
import org.elasticsearch.cluster.routing.IndexRoutingTable;
2324
import org.elasticsearch.cluster.service.ClusterService;
2425
import org.elasticsearch.common.bytes.BytesArray;
@@ -28,6 +29,7 @@
2829
import org.elasticsearch.common.time.DateFormatter;
2930
import org.elasticsearch.common.util.concurrent.ThreadContext;
3031
import org.elasticsearch.core.FixForMultiProject;
32+
import org.elasticsearch.core.NotMultiProjectCapable;
3133
import org.elasticsearch.core.Nullable;
3234
import org.elasticsearch.core.TimeValue;
3335
import org.elasticsearch.gateway.GatewayService;
@@ -395,7 +397,8 @@ private void setupClusterAlertsTasks(
395397
boolean shouldSetUpWatcher = state.get() == State.RUNNING && clusterStateChange == false;
396398
if (canUseWatcher()) {
397399
if (shouldSetUpWatcher) {
398-
final IndexRoutingTable watches = clusterState.routingTable().index(Watch.INDEX);
400+
@NotMultiProjectCapable(description = "Monitoring is not available in serverless and will thus not be made project-aware")
401+
final IndexRoutingTable watches = clusterState.routingTable(ProjectId.DEFAULT).index(Watch.INDEX);
399402
final boolean indexExists = watches != null && watches.allPrimaryShardsActive();
400403

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

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

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

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

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

639647
if (Regex.simpleMatch(indexPatterns, indexName)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.util.function.Function;
3232

33+
import static org.mockito.ArgumentMatchers.any;
3334
import static org.mockito.Mockito.mock;
3435
import static org.mockito.Mockito.when;
3536

@@ -54,7 +55,7 @@ public void setUp() throws Exception {
5455
nodes = mock(DiscoveryNodes.class);
5556
metadata = mock(Metadata.class);
5657
projectMetadata = mock(ProjectMetadata.class);
57-
when(metadata.getProject()).thenReturn(projectMetadata);
58+
when(metadata.getProject(any())).thenReturn(projectMetadata);
5859
licenseState = mock(MockLicenseState.class);
5960
client = mock(Client.class);
6061
ThreadPool threadPool = mock(ThreadPool.class);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.elasticsearch.action.support.IndicesOptions;
1010
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
11+
import org.elasticsearch.cluster.metadata.ProjectId;
1112
import org.elasticsearch.common.settings.Settings;
1213
import org.elasticsearch.index.Index;
1314
import org.elasticsearch.index.query.QueryBuilders;
@@ -109,7 +110,7 @@ private void waitForMonitoringIndices() throws Exception {
109110
return false;
110111
}
111112
for (Index index : indices) {
112-
final var indexRoutingTable = cs.routingTable().index(index);
113+
final var indexRoutingTable = cs.routingTable(ProjectId.DEFAULT).index(index);
113114
if (indexRoutingTable.allPrimaryShardsActive() == false) {
114115
return false;
115116
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.hamcrest.Matchers.greaterThan;
3636
import static org.hamcrest.Matchers.is;
3737
import static org.hamcrest.Matchers.nullValue;
38+
import static org.mockito.ArgumentMatchers.any;
3839
import static org.mockito.ArgumentMatchers.anyString;
3940
import static org.mockito.Mockito.doReturn;
4041
import static org.mockito.Mockito.mock;
@@ -70,7 +71,7 @@ public void testDoCollect() throws Exception {
7071
whenClusterStateWithUUID(clusterUUID);
7172

7273
final RoutingTable routingTable = mock(RoutingTable.class);
73-
when(clusterState.routingTable()).thenReturn(routingTable);
74+
when(clusterState.routingTable(any())).thenReturn(routingTable);
7475

7576
final IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
7677
final MonitoringDoc.Node node = randomMonitoringNode(random());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.hamcrest.Matchers.matchesPattern;
3939
import static org.hamcrest.Matchers.notNullValue;
4040
import static org.hamcrest.Matchers.nullValue;
41+
import static org.mockito.ArgumentMatchers.any;
4142
import static org.mockito.ArgumentMatchers.eq;
4243
import static org.mockito.Mockito.mock;
4344
import static org.mockito.Mockito.verify;
@@ -92,7 +93,7 @@ public void testDoCollect() throws Exception {
9293
withCollectionIndices(indices);
9394

9495
final RoutingTable routingTable = mockRoutingTable();
95-
when(clusterState.routingTable()).thenReturn(routingTable);
96+
when(clusterState.routingTable(any())).thenReturn(routingTable);
9697

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

0 commit comments

Comments
 (0)