diff --git a/docs/reference/ccr/apis/auto-follow/get-auto-follow-stats.asciidoc b/docs/reference/ccr/apis/auto-follow/get-auto-follow-stats.asciidoc deleted file mode 100644 index 85c6775af1c..00000000000 --- a/docs/reference/ccr/apis/auto-follow/get-auto-follow-stats.asciidoc +++ /dev/null @@ -1,46 +0,0 @@ -[role="xpack"] -[testenv="platinum"] -[[ccr-get-auto-follow-stats]] -=== Get Auto-Follow Stats API -++++ -Get Auto-Follow Stats -++++ - -Get auto-follow stats. - -==== Description - -This API gets stats about auto-follow patterns. - -==== Request - -[source,js] --------------------------------------------------- -GET /_ccr/auto_follow/stats --------------------------------------------------- -// CONSOLE -// TEST - -==== Example - -This example retrieves stats about auto-follow patterns: - -[source,js] --------------------------------------------------- -GET /_ccr/auto_follow/stats --------------------------------------------------- -// CONSOLE -// TEST - -The API returns the following result: - -[source,js] --------------------------------------------------- -{ - "number_of_successful_follow_indices" : 16, - "number_of_failed_follow_indices" : 0, - "number_of_failed_remote_cluster_state_requests" : 0, - "recent_auto_follow_errors" : [ ] -} --------------------------------------------------- -// TESTRESPONSE[s/"number_of_successful_follow_indices" : 16/"number_of_successful_follow_indices" : $body.number_of_successful_follow_indices/] diff --git a/docs/reference/ccr/apis/follow/get-follow-stats.asciidoc b/docs/reference/ccr/apis/follow/get-follow-stats.asciidoc index efbaeecb712..0efa156b95a 100644 --- a/docs/reference/ccr/apis/follow/get-follow-stats.asciidoc +++ b/docs/reference/ccr/apis/follow/get-follow-stats.asciidoc @@ -38,12 +38,6 @@ POST /follower_index/_ccr/pause_follow ////////////////////////// -[source,js] --------------------------------------------------- -GET /_ccr/stats --------------------------------------------------- -// CONSOLE - [source,js] -------------------------------------------------- GET //_ccr/stats @@ -186,7 +180,7 @@ This example retrieves follower stats: [source,js] -------------------------------------------------- -GET /_ccr/stats +GET /follower_index/_ccr/stats -------------------------------------------------- // CONSOLE diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index ed3a03f0b17..8b1ed236c56 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -102,8 +102,9 @@ public class FollowIndexIT extends ESCCRRestTestCase { } assertBusy(() -> { - Request statsRequest = new Request("GET", "/_ccr/auto_follow/stats"); - Map response = toMap(client().performRequest(statsRequest)); + Request statsRequest = new Request("GET", "/_ccr/stats"); + Map response = toMap(client().performRequest(statsRequest)); + response = (Map) response.get("auto_follow_stats"); assertThat(response.get("number_of_successful_follow_indices"), equalTo(1)); ensureYellow("logs-20190101"); diff --git a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow_stats.yml b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow_stats.yml deleted file mode 100644 index 4d26eb1ff24..00000000000 --- a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow_stats.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -"Test autofollow stats": - - do: - ccr.auto_follow_stats: {} - - - match: { number_of_successful_follow_indices: 0 } - - match: { number_of_failed_follow_indices: 0 } - - match: { number_of_failed_remote_cluster_state_requests: 0 } - - length: { recent_auto_follow_errors: 0 } - diff --git a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/follow_stats.yml b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/follow_stats.yml index 97c538b60bc..aa63c804aba 100644 --- a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/follow_stats.yml +++ b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/follow_stats.yml @@ -45,7 +45,7 @@ # we can not reliably wait for replication to occur so we test the endpoint without indexing any documents - do: - ccr.stats: + ccr.follow_stats: index: bar - match: { indices.0.index: "bar" } - match: { indices.0.shards.0.leader_index: "foo" } diff --git a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/stats.yml b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/stats.yml new file mode 100644 index 00000000000..e9f5c0306df --- /dev/null +++ b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/stats.yml @@ -0,0 +1,11 @@ +--- +"Test stats": + - do: + ccr.stats: {} + + - match: { auto_follow_stats.number_of_successful_follow_indices: 0 } + - match: { auto_follow_stats.number_of_failed_follow_indices: 0 } + - match: { auto_follow_stats.number_of_failed_remote_cluster_state_requests: 0 } + - length: { auto_follow_stats.recent_auto_follow_errors: 0 } + - length: { follow_stats.indices: 0 } + diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java index 68a6310dcaa..4ff363b7d1e 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java @@ -43,10 +43,10 @@ import org.elasticsearch.xpack.ccr.action.AutoFollowCoordinator; import org.elasticsearch.xpack.ccr.action.TransportGetAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.action.TransportUnfollowAction; import org.elasticsearch.xpack.ccr.rest.RestGetAutoFollowPatternAction; -import org.elasticsearch.xpack.ccr.action.TransportAutoFollowStatsAction; +import org.elasticsearch.xpack.ccr.action.TransportStatsAction; import org.elasticsearch.xpack.ccr.rest.RestAutoFollowStatsAction; import org.elasticsearch.xpack.ccr.rest.RestUnfollowAction; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import org.elasticsearch.xpack.core.ccr.action.DeleteAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.GetAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; @@ -164,7 +164,7 @@ public class Ccr extends Plugin implements ActionPlugin, PersistentTaskPlugin, E new ActionHandler<>(ShardChangesAction.INSTANCE, ShardChangesAction.TransportAction.class), // stats action new ActionHandler<>(FollowStatsAction.INSTANCE, TransportFollowStatsAction.class), - new ActionHandler<>(AutoFollowStatsAction.INSTANCE, TransportAutoFollowStatsAction.class), + new ActionHandler<>(StatsAction.INSTANCE, TransportStatsAction.class), // follow actions new ActionHandler<>(PutFollowAction.INSTANCE, TransportPutFollowAction.class), new ActionHandler<>(ResumeFollowAction.INSTANCE, TransportResumeFollowAction.class), diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportAutoFollowStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportStatsAction.java similarity index 64% rename from x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportAutoFollowStatsAction.java rename to x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportStatsAction.java index b08cbe85fe7..97234158042 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportAutoFollowStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportStatsAction.java @@ -9,11 +9,13 @@ package org.elasticsearch.xpack.ccr.action; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.master.TransportMasterNodeAction; +import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.LicenseUtils; @@ -23,18 +25,19 @@ import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.ccr.Ccr; import org.elasticsearch.xpack.ccr.CcrLicenseChecker; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import java.util.Objects; -public class TransportAutoFollowStatsAction - extends TransportMasterNodeAction { +public class TransportStatsAction extends TransportMasterNodeAction { + private final Client client; private final CcrLicenseChecker ccrLicenseChecker; private final AutoFollowCoordinator autoFollowCoordinator; @Inject - public TransportAutoFollowStatsAction( + public TransportStatsAction( Settings settings, TransportService transportService, ClusterService clusterService, @@ -42,18 +45,20 @@ public class TransportAutoFollowStatsAction ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, AutoFollowCoordinator autoFollowCoordinator, - CcrLicenseChecker ccrLicenseChecker + CcrLicenseChecker ccrLicenseChecker, + Client client ) { super( settings, - AutoFollowStatsAction.NAME, + StatsAction.NAME, transportService, clusterService, threadPool, actionFilters, - AutoFollowStatsAction.Request::new, + StatsAction.Request::new, indexNameExpressionResolver ); + this.client = client; this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker); this.autoFollowCoordinator = Objects.requireNonNull(autoFollowCoordinator); } @@ -64,12 +69,12 @@ public class TransportAutoFollowStatsAction } @Override - protected AutoFollowStatsAction.Response newResponse() { - return new AutoFollowStatsAction.Response(); + protected StatsAction.Response newResponse() { + return new StatsAction.Response(); } @Override - protected void doExecute(Task task, AutoFollowStatsAction.Request request, ActionListener listener) { + protected void doExecute(Task task, StatsAction.Request request, ActionListener listener) { if (ccrLicenseChecker.isCcrAllowed() == false) { listener.onFailure(LicenseUtils.newComplianceException("ccr")); return; @@ -79,16 +84,20 @@ public class TransportAutoFollowStatsAction @Override protected void masterOperation( - AutoFollowStatsAction.Request request, + StatsAction.Request request, ClusterState state, - ActionListener listener + ActionListener listener ) throws Exception { - AutoFollowStats stats = autoFollowCoordinator.getStats(); - listener.onResponse(new AutoFollowStatsAction.Response(stats)); + CheckedConsumer handler = statsResponse -> { + AutoFollowStats stats = autoFollowCoordinator.getStats(); + listener.onResponse(new StatsAction.Response(stats, statsResponse)); + }; + FollowStatsAction.StatsRequest statsRequest = new FollowStatsAction.StatsRequest(); + client.execute(FollowStatsAction.INSTANCE, statsRequest, ActionListener.wrap(handler, listener::onFailure)); } @Override - protected ClusterBlockException checkBlock(AutoFollowStatsAction.Request request, ClusterState state) { + protected ClusterBlockException checkBlock(StatsAction.Request request, ClusterState state) { return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestAutoFollowStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestAutoFollowStatsAction.java index 924bc2f831d..434343e7cc0 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestAutoFollowStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestAutoFollowStatsAction.java @@ -12,7 +12,7 @@ import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import java.io.IOException; @@ -20,7 +20,7 @@ public class RestAutoFollowStatsAction extends BaseRestHandler { public RestAutoFollowStatsAction(final Settings settings, final RestController controller) { super(settings); - controller.registerHandler(RestRequest.Method.GET, "/_ccr/auto_follow/stats", this); + controller.registerHandler(RestRequest.Method.GET, "/_ccr/stats", this); } @Override @@ -30,8 +30,8 @@ public class RestAutoFollowStatsAction extends BaseRestHandler { @Override protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException { - final AutoFollowStatsAction.Request request = new AutoFollowStatsAction.Request(); - return channel -> client.execute(AutoFollowStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); + final StatsAction.Request request = new StatsAction.Request(); + return channel -> client.execute(StatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestFollowStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestFollowStatsAction.java index 25572894af3..15b73292976 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestFollowStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestFollowStatsAction.java @@ -21,7 +21,6 @@ public class RestFollowStatsAction extends BaseRestHandler { public RestFollowStatsAction(final Settings settings, final RestController controller) { super(settings); - controller.registerHandler(RestRequest.Method.GET, "/_ccr/stats", this); controller.registerHandler(RestRequest.Method.GET, "/{index}/_ccr/stats", this); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index 5fc39f0f29e..f9d375068ab 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -19,7 +19,7 @@ import org.elasticsearch.persistent.PersistentTasksCustomMetaData; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import org.elasticsearch.xpack.core.ccr.action.DeleteAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; @@ -260,8 +260,8 @@ public class AutoFollowIT extends CcrIntegTestCase { } private AutoFollowStats getAutoFollowStats() { - AutoFollowStatsAction.Request request = new AutoFollowStatsAction.Request(); - return followerClient().execute(AutoFollowStatsAction.INSTANCE, request).actionGet().getStats(); + StatsAction.Request request = new StatsAction.Request(); + return followerClient().execute(StatsAction.INSTANCE, request).actionGet().getAutoFollowStats(); } private void createLeaderIndex(String index, Settings settings) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java index 742eb761005..7e377f7abcd 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java @@ -7,25 +7,28 @@ package org.elasticsearch.xpack.ccr.action; import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import static org.elasticsearch.xpack.ccr.action.AutoFollowStatsTests.randomReadExceptions; +import static org.elasticsearch.xpack.ccr.action.StatsResponsesTests.createStatsResponse; -public class AutoFollowStatsResponseTests extends AbstractStreamableTestCase { +public class AutoFollowStatsResponseTests extends AbstractStreamableTestCase { @Override - protected AutoFollowStatsAction.Response createBlankInstance() { - return new AutoFollowStatsAction.Response(); + protected StatsAction.Response createBlankInstance() { + return new StatsAction.Response(); } @Override - protected AutoFollowStatsAction.Response createTestInstance() { + protected StatsAction.Response createTestInstance() { AutoFollowStats autoFollowStats = new AutoFollowStats( randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomReadExceptions() ); - return new AutoFollowStatsAction.Response(autoFollowStats); + FollowStatsAction.StatsResponses statsResponse = createStatsResponse(); + return new StatsAction.Response(autoFollowStats, statsResponse); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java index e4830413dff..86851d98ffe 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java @@ -23,6 +23,10 @@ public class StatsResponsesTests extends AbstractStreamableTestCase responses = new ArrayList<>(numResponses); for (int i = 0; i < numResponses; i++) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollectorTestCase.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollectorTestCase.java deleted file mode 100644 index f98e541a9d9..00000000000 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollectorTestCase.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.monitoring.collector.ccr; - -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.xpack.monitoring.BaseCollectorTestCase; - -import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public abstract class AbstractCcrCollectorTestCase extends BaseCollectorTestCase { - - public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - final boolean ccrAllowed = randomBoolean(); - final boolean isElectedMaster = randomBoolean(); - whenLocalNodeElectedMaster(isElectedMaster); - - // this controls the blockage - when(licenseState.isMonitoringAllowed()).thenReturn(false); - when(licenseState.isCcrAllowed()).thenReturn(ccrAllowed); - - final AbstractCcrCollector collector = createCollector(settings, clusterService, licenseState, client); - - assertThat(collector.shouldCollect(isElectedMaster), is(false)); - if (isElectedMaster) { - verify(licenseState).isMonitoringAllowed(); - } - } - - public void testShouldCollectReturnsFalseIfNotMaster() { - // regardless of CCR being enabled - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); - // this controls the blockage - final boolean isElectedMaster = false; - - final AbstractCcrCollector collector = createCollector(settings, clusterService, licenseState, client); - - assertThat(collector.shouldCollect(isElectedMaster), is(false)); - } - - public void testShouldCollectReturnsFalseIfCCRIsDisabled() { - // this is controls the blockage - final Settings settings = ccrDisabledSettings(); - - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); - when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); - - final boolean isElectedMaster = randomBoolean(); - whenLocalNodeElectedMaster(isElectedMaster); - - final AbstractCcrCollector collector = createCollector(settings, clusterService, licenseState, client); - - assertThat(collector.shouldCollect(isElectedMaster), is(false)); - - if (isElectedMaster) { - verify(licenseState).isMonitoringAllowed(); - } - } - - public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() { - final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); - - when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); - // this is controls the blockage - when(licenseState.isCcrAllowed()).thenReturn(false); - final boolean isElectedMaster = randomBoolean(); - whenLocalNodeElectedMaster(isElectedMaster); - - final AbstractCcrCollector collector = createCollector(settings, clusterService, licenseState, client); - - assertThat(collector.shouldCollect(isElectedMaster), is(false)); - - if (isElectedMaster) { - verify(licenseState).isMonitoringAllowed(); - } - } - - public void testShouldCollectReturnsTrue() { - final Settings settings = ccrEnabledSettings(); - - when(licenseState.isMonitoringAllowed()).thenReturn(true); - when(licenseState.isCcrAllowed()).thenReturn(true); - final boolean isElectedMaster = true; - - final AbstractCcrCollector collector = createCollector(settings, clusterService, licenseState, client); - - assertThat(collector.shouldCollect(isElectedMaster), is(true)); - - verify(licenseState).isMonitoringAllowed(); - } - - abstract AbstractCcrCollector createCollector(Settings settings, - ClusterService clusterService, - XPackLicenseState licenseState, - Client client); - - private Settings ccrEnabledSettings() { - // since it's the default, we want to ensure we test both with/without it - return randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); - } - - private Settings ccrDisabledSettings() { - return Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), false).build(); - } - -} diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollectorTests.java deleted file mode 100644 index 7a302503d2d..00000000000 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollectorTests.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.monitoring.collector.ccr; - -import org.elasticsearch.action.ActionFuture; -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.ccr.AutoFollowStats; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; -import org.elasticsearch.xpack.core.ccr.client.CcrClient; -import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; -import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; - -import java.util.Collection; - -import static org.elasticsearch.xpack.monitoring.MonitoringTestUtils.randomMonitoringNode; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class CcrAutoFollowStatsCollectorTests extends AbstractCcrCollectorTestCase { - - @Override - AbstractCcrCollector createCollector(Settings settings, ClusterService clusterService, XPackLicenseState licenseState, Client client) { - return new CcrAutoFollowStatsCollector(settings, clusterService, licenseState, client); - } - - public void testDoCollect() throws Exception { - final String clusterUuid = randomAlphaOfLength(5); - whenClusterStateWithUUID(clusterUuid); - - final MonitoringDoc.Node node = randomMonitoringNode(random()); - final CcrClient client = mock(CcrClient.class); - final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - - final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); - withCollectionTimeout(CcrAutoFollowStatsCollector.CCR_AUTO_FOLLOW_STATS_TIMEOUT, timeout); - - final CcrAutoFollowStatsCollector collector = - new CcrAutoFollowStatsCollector(Settings.EMPTY, clusterService, licenseState, client, threadContext); - assertEquals(timeout, collector.getCollectionTimeout()); - - final AutoFollowStats autoFollowStats = mock(AutoFollowStats.class); - - @SuppressWarnings("unchecked") - final ActionFuture future = (ActionFuture)mock(ActionFuture.class); - final AutoFollowStatsAction.Response response = new AutoFollowStatsAction.Response(autoFollowStats); - - when(client.autoFollowStats(any())).thenReturn(future); - when(future.actionGet(timeout)).thenReturn(response); - - final long interval = randomNonNegativeLong(); - - final Collection documents = collector.doCollect(node, interval, clusterState); - verify(clusterState).metaData(); - verify(metaData).clusterUUID(); - - assertThat(documents, hasSize(1)); - final AutoFollowStatsMonitoringDoc document = (AutoFollowStatsMonitoringDoc) documents.iterator().next(); - - assertThat(document.getCluster(), is(clusterUuid)); - assertThat(document.getTimestamp(), greaterThan(0L)); - assertThat(document.getIntervalMillis(), equalTo(interval)); - assertThat(document.getNode(), equalTo(node)); - assertThat(document.getSystem(), is(MonitoredSystem.ES)); - assertThat(document.getType(), is(AutoFollowStatsMonitoringDoc.TYPE)); - assertThat(document.getId(), nullValue()); - assertThat(document.stats(), is(autoFollowStats)); - } - -} diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollectorTests.java deleted file mode 100644 index 904735ffedf..00000000000 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollectorTests.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.monitoring.collector.ccr; - -import org.elasticsearch.action.ActionFuture; -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; -import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; -import org.elasticsearch.xpack.core.ccr.client.CcrClient; -import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; -import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; -import org.mockito.ArgumentMatcher; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import static java.util.Collections.emptyList; -import static org.elasticsearch.xpack.monitoring.MonitoringTestUtils.randomMonitoringNode; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class FollowStatsCollectorTests extends AbstractCcrCollectorTestCase { - - @Override - AbstractCcrCollector createCollector(Settings settings, ClusterService clusterService, XPackLicenseState licenseState, Client client) { - return new FollowStatsCollector(settings, clusterService, licenseState, client); - } - - public void testDoCollect() throws Exception { - final String clusterUuid = randomAlphaOfLength(5); - whenClusterStateWithUUID(clusterUuid); - - final MonitoringDoc.Node node = randomMonitoringNode(random()); - final CcrClient client = mock(CcrClient.class); - final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - - final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); - withCollectionTimeout(FollowStatsCollector.CCR_STATS_TIMEOUT, timeout); - - final FollowStatsCollector collector = - new FollowStatsCollector(Settings.EMPTY, clusterService, licenseState, client, threadContext); - assertEquals(timeout, collector.getCollectionTimeout()); - - final List statuses = mockStatuses(); - - @SuppressWarnings("unchecked") - final ActionFuture future = - (ActionFuture)mock(ActionFuture.class); - final FollowStatsAction.StatsResponses responses = new FollowStatsAction.StatsResponses(emptyList(), emptyList(), statuses); - - final FollowStatsAction.StatsRequest request = new FollowStatsAction.StatsRequest(); - request.setIndices(Strings.EMPTY_ARRAY); - when(client.stats(statsRequestEq(request))).thenReturn(future); - when(future.actionGet(timeout)).thenReturn(responses); - - final long interval = randomNonNegativeLong(); - - final Collection documents = collector.doCollect(node, interval, clusterState); - verify(clusterState).metaData(); - verify(metaData).clusterUUID(); - - assertThat(documents, hasSize(statuses.size())); - - int index = 0; - for (final Iterator it = documents.iterator(); it.hasNext(); index++) { - final FollowStatsMonitoringDoc document = (FollowStatsMonitoringDoc)it.next(); - final FollowStatsAction.StatsResponse status = statuses.get(index); - - assertThat(document.getCluster(), is(clusterUuid)); - assertThat(document.getTimestamp(), greaterThan(0L)); - assertThat(document.getIntervalMillis(), equalTo(interval)); - assertThat(document.getNode(), equalTo(node)); - assertThat(document.getSystem(), is(MonitoredSystem.ES)); - assertThat(document.getType(), is(FollowStatsMonitoringDoc.TYPE)); - assertThat(document.getId(), nullValue()); - assertThat(document.status(), is(status.status())); - } - } - - private List mockStatuses() { - final int count = randomIntBetween(1, 8); - final List statuses = new ArrayList<>(count); - - for (int i = 0; i < count; ++i) { - FollowStatsAction.StatsResponse statsResponse = mock(FollowStatsAction.StatsResponse.class); - ShardFollowNodeTaskStatus status = mock(ShardFollowNodeTaskStatus.class); - when(statsResponse.status()).thenReturn(status); - statuses.add(statsResponse); - } - - return statuses; - } - - private static FollowStatsAction.StatsRequest statsRequestEq(FollowStatsAction.StatsRequest expected) { - return argThat(new FollowStatsRequest(expected)); - } - - private static class FollowStatsRequest extends ArgumentMatcher { - - private final FollowStatsAction.StatsRequest expected; - - private FollowStatsRequest(FollowStatsAction.StatsRequest expected) { - this.expected = expected; - } - - @Override - public boolean matches(Object o) { - FollowStatsAction.StatsRequest actual = (FollowStatsAction.StatsRequest) o; - return Arrays.equals(expected.indices(), actual.indices()); - } - } - -} diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java new file mode 100644 index 00000000000..e23ba7800fb --- /dev/null +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java @@ -0,0 +1,215 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.monitoring.collector.ccr; + +import org.elasticsearch.action.ActionFuture; +import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.ccr.AutoFollowStats; +import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; +import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; +import org.elasticsearch.xpack.core.ccr.client.CcrClient; +import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; +import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; +import org.elasticsearch.xpack.monitoring.BaseCollectorTestCase; + +import java.util.ArrayList; +import java.util.List; + +import static org.elasticsearch.xpack.monitoring.MonitoringTestUtils.randomMonitoringNode; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class StatsCollectorTests extends BaseCollectorTestCase { + + public void testShouldCollectReturnsFalseIfMonitoringNotAllowed() { + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + final boolean ccrAllowed = randomBoolean(); + final boolean isElectedMaster = randomBoolean(); + whenLocalNodeElectedMaster(isElectedMaster); + + // this controls the blockage + when(licenseState.isMonitoringAllowed()).thenReturn(false); + when(licenseState.isCcrAllowed()).thenReturn(ccrAllowed); + + final StatsCollector collector = createCollector(settings, clusterService, licenseState, client); + + assertThat(collector.shouldCollect(isElectedMaster), is(false)); + if (isElectedMaster) { + verify(licenseState).isMonitoringAllowed(); + } + } + + public void testShouldCollectReturnsFalseIfNotMaster() { + // regardless of CCR being enabled + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); + // this controls the blockage + final boolean isElectedMaster = false; + + final StatsCollector collector = createCollector(settings, clusterService, licenseState, client); + + assertThat(collector.shouldCollect(isElectedMaster), is(false)); + } + + public void testShouldCollectReturnsFalseIfCCRIsDisabled() { + // this is controls the blockage + final Settings settings = ccrDisabledSettings(); + + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + when(licenseState.isCcrAllowed()).thenReturn(randomBoolean()); + + final boolean isElectedMaster = randomBoolean(); + whenLocalNodeElectedMaster(isElectedMaster); + + final StatsCollector collector = createCollector(settings, clusterService, licenseState, client); + + assertThat(collector.shouldCollect(isElectedMaster), is(false)); + + if (isElectedMaster) { + verify(licenseState).isMonitoringAllowed(); + } + } + + public void testShouldCollectReturnsFalseIfCCRIsNotAllowed() { + final Settings settings = randomFrom(ccrEnabledSettings(), ccrDisabledSettings()); + + when(licenseState.isMonitoringAllowed()).thenReturn(randomBoolean()); + // this is controls the blockage + when(licenseState.isCcrAllowed()).thenReturn(false); + final boolean isElectedMaster = randomBoolean(); + whenLocalNodeElectedMaster(isElectedMaster); + + final StatsCollector collector = createCollector(settings, clusterService, licenseState, client); + + assertThat(collector.shouldCollect(isElectedMaster), is(false)); + + if (isElectedMaster) { + verify(licenseState).isMonitoringAllowed(); + } + } + + public void testShouldCollectReturnsTrue() { + final Settings settings = ccrEnabledSettings(); + + when(licenseState.isMonitoringAllowed()).thenReturn(true); + when(licenseState.isCcrAllowed()).thenReturn(true); + final boolean isElectedMaster = true; + + final StatsCollector collector = createCollector(settings, clusterService, licenseState, client); + + assertThat(collector.shouldCollect(isElectedMaster), is(true)); + + verify(licenseState).isMonitoringAllowed(); + } + + public void testDoCollect() throws Exception { + final String clusterUuid = randomAlphaOfLength(5); + whenClusterStateWithUUID(clusterUuid); + + final MonitoringDoc.Node node = randomMonitoringNode(random()); + final CcrClient client = mock(CcrClient.class); + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); + final List statuses = mockStatuses(); + + final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(1, 120)); + withCollectionTimeout(StatsCollector.CCR_STATS_TIMEOUT, timeout); + + final AutoFollowStats autoFollowStats = mock(AutoFollowStats.class); + final FollowStatsAction.StatsResponses statsResponse = mock(FollowStatsAction.StatsResponses.class); + when(statsResponse.getStatsResponses()).thenReturn(statuses); + + @SuppressWarnings("unchecked") + final ActionFuture future = (ActionFuture) mock(ActionFuture.class); + final StatsAction.Response response = new StatsAction.Response(autoFollowStats, statsResponse); + + when(client.stats(any())).thenReturn(future); + when(future.actionGet(timeout)).thenReturn(response); + + final StatsCollector collector = new StatsCollector(settings, clusterService, licenseState, client, threadContext); + assertEquals(timeout, collector.getCollectionTimeout()); + + final long interval = randomNonNegativeLong(); + final List documents = new ArrayList<>(collector.doCollect(node, interval, clusterState)); + verify(clusterState).metaData(); + verify(metaData).clusterUUID(); + + assertThat(documents, hasSize(statuses.size() + 1)); + + for (int i = 0; i < documents.size() - 1; i++) { + final FollowStatsMonitoringDoc document = (FollowStatsMonitoringDoc) documents.get(i); + final FollowStatsAction.StatsResponse status = statuses.get(i); + + assertThat(document.getCluster(), is(clusterUuid)); + assertThat(document.getTimestamp(), greaterThan(0L)); + assertThat(document.getIntervalMillis(), equalTo(interval)); + assertThat(document.getNode(), equalTo(node)); + assertThat(document.getSystem(), is(MonitoredSystem.ES)); + assertThat(document.getType(), is(FollowStatsMonitoringDoc.TYPE)); + assertThat(document.getId(), nullValue()); + assertThat(document.status(), is(status.status())); + } + + final AutoFollowStatsMonitoringDoc document = (AutoFollowStatsMonitoringDoc) documents.get(documents.size() - 1); + assertThat(document, notNullValue()); + assertThat(document.getCluster(), is(clusterUuid)); + assertThat(document.getTimestamp(), greaterThan(0L)); + assertThat(document.getIntervalMillis(), equalTo(interval)); + assertThat(document.getNode(), equalTo(node)); + assertThat(document.getSystem(), is(MonitoredSystem.ES)); + assertThat(document.getType(), is(AutoFollowStatsMonitoringDoc.TYPE)); + assertThat(document.getId(), nullValue()); + assertThat(document.stats(), is(autoFollowStats)); + } + + private List mockStatuses() { + final int count = randomIntBetween(1, 8); + final List statuses = new ArrayList<>(count); + + for (int i = 0; i < count; ++i) { + FollowStatsAction.StatsResponse statsResponse = mock(FollowStatsAction.StatsResponse.class); + ShardFollowNodeTaskStatus status = mock(ShardFollowNodeTaskStatus.class); + when(status.followerIndex()).thenReturn("follow_index"); + when(statsResponse.status()).thenReturn(status); + statuses.add(statsResponse); + } + + return statuses; + } + + private StatsCollector createCollector(Settings settings, + ClusterService clusterService, + XPackLicenseState licenseState, + Client client) { + return new StatsCollector(settings, clusterService, licenseState, client); + } + + private Settings ccrEnabledSettings() { + // since it's the default, we want to ensure we test both with/without it + return randomBoolean() ? Settings.EMPTY : Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true).build(); + } + + private Settings ccrDisabledSettings() { + return Settings.builder().put(XPackSettings.CCR_ENABLED_SETTING.getKey(), false).build(); + } + +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java index dba5f3b6f1f..ff47f6e105b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java @@ -32,7 +32,7 @@ import java.util.TreeMap; public class FollowStatsAction extends Action { - public static final String NAME = "cluster:monitor/ccr/stats"; + public static final String NAME = "cluster:monitor/ccr/follow_stats"; public static final FollowStatsAction INSTANCE = new FollowStatsAction(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/AutoFollowStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/StatsAction.java similarity index 60% rename from x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/AutoFollowStatsAction.java rename to x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/StatsAction.java index 12cb1b4bf59..e8cfe7f3682 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/AutoFollowStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/StatsAction.java @@ -19,12 +19,12 @@ import org.elasticsearch.xpack.core.ccr.AutoFollowStats; import java.io.IOException; import java.util.Objects; -public class AutoFollowStatsAction extends Action { +public class StatsAction extends Action { - public static final String NAME = "cluster:monitor/ccr/auto_follow_stats"; - public static final AutoFollowStatsAction INSTANCE = new AutoFollowStatsAction(); + public static final String NAME = "cluster:monitor/ccr/stats"; + public static final StatsAction INSTANCE = new StatsAction(); - private AutoFollowStatsAction() { + private StatsAction() { super(NAME); } @@ -55,34 +55,48 @@ public class AutoFollowStatsAction extends Action listener) { client.execute(FollowStatsAction.INSTANCE, request, listener); } - public ActionFuture stats(final FollowStatsAction.StatsRequest request) { + public ActionFuture followStats(final FollowStatsAction.StatsRequest request) { final PlainActionFuture listener = PlainActionFuture.newFuture(); client.execute(FollowStatsAction.INSTANCE, request, listener); return listener; } - public void autoFollowStats(final AutoFollowStatsAction.Request request, - final ActionListener listener) { - client.execute(AutoFollowStatsAction.INSTANCE, request, listener); + public void stats(final StatsAction.Request request, + final ActionListener listener) { + client.execute(StatsAction.INSTANCE, request, listener); } - public ActionFuture autoFollowStats(final AutoFollowStatsAction.Request request) { - final PlainActionFuture listener = PlainActionFuture.newFuture(); - autoFollowStats(request, listener); + public ActionFuture stats(final StatsAction.Request request) { + final PlainActionFuture listener = PlainActionFuture.newFuture(); + stats(request, listener); return listener; } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java index 31f403918fa..641e25eabf9 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java @@ -39,8 +39,7 @@ import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.monitoring.action.TransportMonitoringBulkAction; import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; import org.elasticsearch.xpack.monitoring.collector.Collector; -import org.elasticsearch.xpack.monitoring.collector.ccr.CcrAutoFollowStatsCollector; -import org.elasticsearch.xpack.monitoring.collector.ccr.FollowStatsCollector; +import org.elasticsearch.xpack.monitoring.collector.ccr.StatsCollector; import org.elasticsearch.xpack.monitoring.collector.cluster.ClusterStatsCollector; import org.elasticsearch.xpack.monitoring.collector.indices.IndexRecoveryCollector; import org.elasticsearch.xpack.monitoring.collector.indices.IndexStatsCollector; @@ -144,8 +143,7 @@ public class Monitoring extends Plugin implements ActionPlugin { collectors.add(new NodeStatsCollector(settings, clusterService, getLicenseState(), client)); collectors.add(new IndexRecoveryCollector(settings, clusterService, getLicenseState(), client)); collectors.add(new JobStatsCollector(settings, clusterService, getLicenseState(), client)); - collectors.add(new FollowStatsCollector(settings, clusterService, getLicenseState(), client)); - collectors.add(new CcrAutoFollowStatsCollector(settings, clusterService, getLicenseState(), client)); + collectors.add(new StatsCollector(settings, clusterService, getLicenseState(), client)); final MonitoringService monitoringService = new MonitoringService(settings, clusterService, threadPool, collectors, exporters); @@ -184,8 +182,7 @@ public class Monitoring extends Plugin implements ActionPlugin { settings.add(IndexRecoveryCollector.INDEX_RECOVERY_ACTIVE_ONLY); settings.add(IndexStatsCollector.INDEX_STATS_TIMEOUT); settings.add(JobStatsCollector.JOB_STATS_TIMEOUT); - settings.add(FollowStatsCollector.CCR_STATS_TIMEOUT); - settings.add(CcrAutoFollowStatsCollector.CCR_AUTO_FOLLOW_STATS_TIMEOUT); + settings.add(StatsCollector.CCR_STATS_TIMEOUT); settings.add(NodeStatsCollector.NODE_STATS_TIMEOUT); settings.addAll(Exporters.getSettings()); return Collections.unmodifiableList(settings); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollector.java deleted file mode 100644 index e179c204416..00000000000 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrAutoFollowStatsCollector.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.monitoring.collector.ccr; - -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.XPackClient; -import org.elasticsearch.xpack.core.ccr.action.AutoFollowStatsAction; -import org.elasticsearch.xpack.core.ccr.client.CcrClient; -import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; - -import java.util.Collection; -import java.util.Collections; - -public final class CcrAutoFollowStatsCollector extends AbstractCcrCollector { - - public static final Setting CCR_AUTO_FOLLOW_STATS_TIMEOUT = collectionTimeoutSetting("ccr.auto_follow.stats.timeout"); - - public CcrAutoFollowStatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { - super(settings, clusterService, CCR_AUTO_FOLLOW_STATS_TIMEOUT, licenseState, new XPackClient(client).ccr(), - client.threadPool().getThreadContext()); - } - - CcrAutoFollowStatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final CcrClient ccrClient, - final ThreadContext threadContext) { - super(settings, clusterService, CCR_AUTO_FOLLOW_STATS_TIMEOUT, licenseState, ccrClient, threadContext); - } - - @Override - Collection innerDoCollect( - long timestamp, - String clusterUuid, - long interval, - MonitoringDoc.Node node) throws Exception { - - final AutoFollowStatsAction.Request request = new AutoFollowStatsAction.Request(); - final AutoFollowStatsAction.Response response = ccrClient.autoFollowStats(request).actionGet(getCollectionTimeout()); - - final AutoFollowStatsMonitoringDoc doc = - new AutoFollowStatsMonitoringDoc(clusterUuid, timestamp, interval, node, response.getStats()); - return Collections.singletonList(doc); - } - -} diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollector.java deleted file mode 100644 index 3255032e785..00000000000 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsCollector.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.monitoring.collector.ccr; - -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.XPackClient; -import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; -import org.elasticsearch.xpack.core.ccr.client.CcrClient; -import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; - -import java.util.Collection; -import java.util.stream.Collectors; - -public final class FollowStatsCollector extends AbstractCcrCollector { - - public static final Setting CCR_STATS_TIMEOUT = collectionTimeoutSetting("ccr.stats.timeout"); - - public FollowStatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { - super(settings, clusterService, CCR_STATS_TIMEOUT, licenseState, new XPackClient(client).ccr(), - client.threadPool().getThreadContext()); - } - - FollowStatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final CcrClient ccrClient, - final ThreadContext threadContext) { - super(settings, clusterService, CCR_STATS_TIMEOUT, licenseState, ccrClient, threadContext); - } - - @Override - Collection innerDoCollect( - long timestamp, - String clusterUuid, - long interval, - MonitoringDoc.Node node) throws Exception { - - - final FollowStatsAction.StatsRequest request = new FollowStatsAction.StatsRequest(); - request.setIndices(getCollectionIndices()); - final FollowStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout()); - - return responses - .getStatsResponses() - .stream() - .map(stats -> new FollowStatsMonitoringDoc(clusterUuid, timestamp, interval, node, stats.status())) - .collect(Collectors.toList()); - } - -} diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java similarity index 54% rename from x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollector.java rename to x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java index e3db3d33667..e7c15cc4b27 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AbstractCcrCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; +import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; @@ -13,30 +14,46 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xpack.core.XPackClient; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.ccr.action.StatsAction; import org.elasticsearch.xpack.core.ccr.client.CcrClient; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.Collector; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import static org.elasticsearch.xpack.core.ClientHelper.MONITORING_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.stashWithOrigin; import static org.elasticsearch.xpack.monitoring.collector.ccr.FollowStatsMonitoringDoc.TYPE; -public abstract class AbstractCcrCollector extends Collector { +public final class StatsCollector extends Collector { + + public static final Setting CCR_STATS_TIMEOUT = collectionTimeoutSetting("ccr.stats.timeout"); private final ThreadContext threadContext; - final CcrClient ccrClient; + private final CcrClient ccrClient; - AbstractCcrCollector( + public StatsCollector( final Settings settings, final ClusterService clusterService, - final Setting timeoutSetting, final XPackLicenseState licenseState, - final CcrClient ccrClient, - final ThreadContext threadContext) { - super(settings, TYPE, clusterService, timeoutSetting, licenseState); + final Client client) { + this(settings, clusterService, licenseState, new XPackClient(client).ccr(), client.threadPool().getThreadContext()); + } + + StatsCollector( + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final CcrClient ccrClient, + final ThreadContext threadContext) { + super(settings, TYPE, clusterService, CCR_STATS_TIMEOUT, licenseState); this.ccrClient = ccrClient; this.threadContext = threadContext; } @@ -59,13 +76,23 @@ public abstract class AbstractCcrCollector extends Collector { try (ThreadContext.StoredContext ignore = stashWithOrigin(threadContext, MONITORING_ORIGIN)) { final long timestamp = timestamp(); final String clusterUuid = clusterUuid(clusterState); - return innerDoCollect(timestamp, clusterUuid, interval, node); + + final StatsAction.Request request = new StatsAction.Request(); + final StatsAction.Response response = ccrClient.stats(request).actionGet(getCollectionTimeout()); + + final AutoFollowStatsMonitoringDoc autoFollowStatsDoc = + new AutoFollowStatsMonitoringDoc(clusterUuid, timestamp, interval, node, response.getAutoFollowStats()); + + Set collectionIndices = new HashSet<>(Arrays.asList(getCollectionIndices())); + List docs = response + .getFollowStats() + .getStatsResponses() + .stream() + .filter(statsResponse -> collectionIndices.isEmpty() || collectionIndices.contains(statsResponse.status().followerIndex())) + .map(stats -> new FollowStatsMonitoringDoc(clusterUuid, timestamp, interval, node, stats.status())) + .collect(Collectors.toList()); + docs.add(autoFollowStatsDoc); + return docs; } } - - abstract Collection innerDoCollect( - long timestamp, - String clusterUuid, - long interval, - MonitoringDoc.Node node) throws Exception; } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.auto_follow_stats.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.auto_follow_stats.json deleted file mode 100644 index 4d5ca5fe39c..00000000000 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.auto_follow_stats.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "ccr.auto_follow_stats": { - "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/current", - "methods": [ "GET" ], - "url": { - "path": "/_ccr/auto_follow/stats", - "paths": [ "/_ccr/auto_follow/stats" ], - "parts": {}, - "body": null - } - } -} diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.follow_stats.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.follow_stats.json new file mode 100644 index 00000000000..852a0dc05df --- /dev/null +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.follow_stats.json @@ -0,0 +1,16 @@ +{ + "ccr.follow_stats": { + "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/current", + "methods": [ "GET" ], + "url": { + "path": "/{index}/_ccr/stats", + "paths": [ "/{index}/_ccr/stats" ], + "parts": { + "index": { + "type": "list", + "description": "A comma-separated list of index names; use `_all` or empty string to perform the operation on all indices" + } + } + } + } +} diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.stats.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.stats.json index aa9e9a7fec3..91867d05e03 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.stats.json +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.stats.json @@ -4,13 +4,9 @@ "methods": [ "GET" ], "url": { "path": "/_ccr/stats", - "paths": [ "/_ccr/stats", "/{index}/_ccr/stats" ], - "parts": { - "index": { - "type": "list", - "description": "A comma-separated list of index names; use `_all` or empty string to perform the operation on all indices" - } - } + "paths": [ "/_ccr/stats" ], + "parts": {}, + "body": null } } }