From e35648396f9d9544fa009e6a8eda643efecaa808 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 12 Dec 2018 14:15:42 +0100 Subject: [PATCH 1/9] [CCR] Add time since last auto follow fetch to auto follow stats For each remote cluster the auto follow coordinator, starts an auto follower that checks the remote cluster state and determines whether an index needs to be auto followed. The time since last auto follow is reported per remote cluster and gives insight whether the auto follow process is alive. Relates to #33007 Originates from #35895 --- .../reference/ccr/apis/get-ccr-stats.asciidoc | 4 +- .../java/org/elasticsearch/xpack/ccr/Ccr.java | 2 +- .../ccr/action/AutoFollowCoordinator.java | 33 +++++- .../action/AutoFollowCoordinatorTests.java | 102 ++++++++++++++++-- .../action/AutoFollowStatsResponseTests.java | 4 +- .../ccr/action/AutoFollowStatsTests.java | 12 ++- .../AutoFollowStatsMonitoringDocTests.java | 30 +++++- .../xpack/core/ccr/AutoFollowStats.java | 59 ++++++++-- .../src/main/resources/monitoring-es.json | 11 ++ 9 files changed, 228 insertions(+), 29 deletions(-) diff --git a/docs/reference/ccr/apis/get-ccr-stats.asciidoc b/docs/reference/ccr/apis/get-ccr-stats.asciidoc index b8491e8a60176..05c47ad45bfcf 100644 --- a/docs/reference/ccr/apis/get-ccr-stats.asciidoc +++ b/docs/reference/ccr/apis/get-ccr-stats.asciidoc @@ -105,7 +105,8 @@ The API returns the following results: "number_of_failed_follow_indices" : 0, "number_of_failed_remote_cluster_state_requests" : 0, "number_of_successful_follow_indices" : 1, - "recent_auto_follow_errors" : [] + "recent_auto_follow_errors" : [], + "tracking_remote_clusters" : [] }, "follow_stats" : { "indices" : [ @@ -151,6 +152,7 @@ The API returns the following results: // TESTRESPONSE[s/"number_of_failed_remote_cluster_state_requests" : 0/"number_of_failed_remote_cluster_state_requests" : $body.auto_follow_stats.number_of_failed_remote_cluster_state_requests/] // TESTRESPONSE[s/"number_of_successful_follow_indices" : 1/"number_of_successful_follow_indices" : $body.auto_follow_stats.number_of_successful_follow_indices/] // TESTRESPONSE[s/"recent_auto_follow_errors" : \[\]/"recent_auto_follow_errors" : $body.auto_follow_stats.recent_auto_follow_errors/] +// TESTRESPONSE[s/"tracking_remote_clusters" : \[\]/"tracking_remote_clusters" : $body.auto_follow_stats.tracking_remote_clusters/] // TESTRESPONSE[s/"leader_global_checkpoint" : 1024/"leader_global_checkpoint" : $body.follow_stats.indices.0.shards.0.leader_global_checkpoint/] // TESTRESPONSE[s/"leader_max_seq_no" : 1536/"leader_max_seq_no" : $body.follow_stats.indices.0.shards.0.leader_max_seq_no/] // TESTRESPONSE[s/"follower_global_checkpoint" : 768/"follower_global_checkpoint" : $body.follow_stats.indices.0.shards.0.follower_global_checkpoint/] 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 b25bd71c67ffc..e1232b5c34c06 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 @@ -156,7 +156,7 @@ public Collection createComponents( return Arrays.asList( ccrLicenseChecker, - new AutoFollowCoordinator(client, clusterService, ccrLicenseChecker) + new AutoFollowCoordinator(client, clusterService, ccrLicenseChecker, System::nanoTime) ); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index 7900351105c06..f9f18eab67411 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -49,9 +49,11 @@ import java.util.Objects; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.LongSupplier; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -67,6 +69,7 @@ public class AutoFollowCoordinator implements ClusterStateListener { private final Client client; private final ClusterService clusterService; private final CcrLicenseChecker ccrLicenseChecker; + private final LongSupplier relativeTimeProvider; private volatile Map autoFollowers = Collections.emptyMap(); @@ -79,10 +82,13 @@ public class AutoFollowCoordinator implements ClusterStateListener { public AutoFollowCoordinator( Client client, ClusterService clusterService, - CcrLicenseChecker ccrLicenseChecker) { + CcrLicenseChecker ccrLicenseChecker, + LongSupplier relativeTimeProvider) { + this.client = client; this.clusterService = clusterService; this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker, "ccrLicenseChecker"); + this.relativeTimeProvider = relativeTimeProvider; clusterService.addListener(this); this.recentAutoFollowErrors = new LinkedHashMap() { @Override @@ -93,11 +99,25 @@ protected boolean removeEldestEntry(final Map.Entry autoFollowers = this.autoFollowers; + final TreeMap timesSinceLastAutoFollowPerRemoteCluster = new TreeMap<>(); + for (Map.Entry entry : autoFollowers.entrySet()) { + long timeSinceLastAutoFollow = entry.getValue().lastAutoFollowTime; + if (timeSinceLastAutoFollow != -1) { + long timeSinceLastAutoFollowInMillis = + TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - timeSinceLastAutoFollow); + timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), timeSinceLastAutoFollowInMillis); + } else { + timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), -1L); + } + } + return new AutoFollowStats( numberOfFailedIndicesAutoFollowed, numberOfFailedRemoteClusterStateRequests, numberOfSuccessfulIndicesAutoFollowed, - new TreeMap<>(recentAutoFollowErrors) + new TreeMap<>(recentAutoFollowErrors), + timesSinceLastAutoFollowPerRemoteCluster ); } @@ -146,7 +166,7 @@ void updateAutoFollowers(ClusterState followerClusterState) { Map newAutoFollowers = new HashMap<>(newRemoteClusters.size()); for (String remoteCluster : newRemoteClusters) { - AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state) { + AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeTimeProvider) { @Override void getRemoteClusterState(final String remoteCluster, @@ -239,20 +259,25 @@ abstract static class AutoFollower { private final String remoteCluster; private final Consumer> statsUpdater; private final Supplier followerClusterStateSupplier; + private final LongSupplier relativeTimeProvider; + private volatile long lastAutoFollowTime = -1; private volatile long metadataVersion = 0; private volatile CountDown autoFollowPatternsCountDown; private volatile AtomicArray autoFollowResults; AutoFollower(final String remoteCluster, final Consumer> statsUpdater, - final Supplier followerClusterStateSupplier) { + final Supplier followerClusterStateSupplier, + LongSupplier relativeTimeProvider) { this.remoteCluster = remoteCluster; this.statsUpdater = statsUpdater; this.followerClusterStateSupplier = followerClusterStateSupplier; + this.relativeTimeProvider = relativeTimeProvider; } void start() { + lastAutoFollowTime = relativeTimeProvider.getAsLong(); final ClusterState clusterState = followerClusterStateSupplier.get(); final AutoFollowMetadata autoFollowMetadata = clusterState.metaData().custom(AutoFollowMetadata.TYPE); if (autoFollowMetadata == null) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index 534397a0a9a51..c6a71e9d88022 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -89,7 +89,7 @@ public void testAutoFollower() { assertThat(entries.get(0).getKey().getName(), equalTo("logs-20190101")); assertThat(entries.get(0).getValue(), nullValue()); }; - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState), () -> 1L) { @Override void getRemoteClusterState(String remoteCluster, long metadataVersion, @@ -154,7 +154,7 @@ public void testAutoFollowerClusterStateApiFailure() { assertThat(results.get(0).clusterStateFetchException, sameInstance(failure)); assertThat(results.get(0).autoFollowExecutionResults.entrySet().size(), equalTo(0)); }; - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L) { @Override void getRemoteClusterState(String remoteCluster, long metadataVersion, @@ -209,7 +209,7 @@ public void testAutoFollowerUpdateClusterStateFailure() { assertThat(entries.get(0).getKey().getName(), equalTo("logs-20190101")); assertThat(entries.get(0).getValue(), sameInstance(failure)); }; - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L) { @Override void getRemoteClusterState(String remoteCluster, long metadataVersion, @@ -266,7 +266,7 @@ public void testAutoFollowerCreateAndFollowApiCallFailure() { assertThat(entries.get(0).getKey().getName(), equalTo("logs-20190101")); assertThat(entries.get(0).getValue(), sameInstance(failure)); }; - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L) { @Override void getRemoteClusterState(String remoteCluster, long metadataVersion, @@ -532,8 +532,8 @@ public void testStats() { AutoFollowCoordinator autoFollowCoordinator = new AutoFollowCoordinator( null, mock(ClusterService.class), - new CcrLicenseChecker(() -> true, () -> false) - ); + new CcrLicenseChecker(() -> true, () -> false), + () -> 1L); autoFollowCoordinator.updateStats(Collections.singletonList( new AutoFollowCoordinator.AutoFollowResult("_alias1")) @@ -585,6 +585,92 @@ public void testStats() { assertThat(autoFollowStats.getRecentAutoFollowErrors().get("_alias2:index2").getCause().getMessage(), equalTo("error")); } + public void testUpdateAutoFollowers() { + ClusterService clusterService = mock(ClusterService.class); + // Return a cluster state with no patterns so that the auto followers never really execute: + ClusterState followerState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) + .build(); + when(clusterService.state()).thenReturn(followerState); + AutoFollowCoordinator autoFollowCoordinator = new AutoFollowCoordinator( + null, + clusterService, + new CcrLicenseChecker(() -> true, () -> false), + () -> 1L); + // Add 3 patterns: + Map patterns = new HashMap<>(); + patterns.put("pattern1", new AutoFollowPattern("remote1", Collections.singletonList("logs-*"), null, null, null, + null, null, null, null, null, null, null, null)); + patterns.put("pattern2", new AutoFollowPattern("remote2", Collections.singletonList("logs-*"), null, null, null, + null, null, null, null, null, null, null, null)); + patterns.put("pattern3", new AutoFollowPattern("remote2", Collections.singletonList("metrics-*"), null, null, null, + null, null, null, null, null, null, null, null)); + ClusterState clusterState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(2)); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote1"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + // Remove patterns 1 and 3: + patterns.remove("pattern1"); + patterns.remove("pattern3"); + clusterState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(1)); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + // Add pattern 4: + patterns.put("pattern4", new AutoFollowPattern("remote1", Collections.singletonList("metrics-*"), null, null, null, + null, null, null, null, null, null, null, null)); + clusterState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(2)); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote1"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + // Remove patterns 2 and 4: + patterns.remove("pattern2"); + patterns.remove("pattern4"); + clusterState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + } + + public void testUpdateAutoFollowersNoPatterns() { + AutoFollowCoordinator autoFollowCoordinator = new AutoFollowCoordinator( + null, + mock(ClusterService.class), + new CcrLicenseChecker(() -> true, () -> false), + () -> 1L); + ClusterState clusterState = ClusterState.builder(new ClusterName("remote")) + .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) + .build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + } + + public void testUpdateAutoFollowersNoAutoFollowMetadata() { + AutoFollowCoordinator autoFollowCoordinator = new AutoFollowCoordinator( + null, + mock(ClusterService.class), + new CcrLicenseChecker(() -> true, () -> false), + () -> 1L); + ClusterState clusterState = ClusterState.builder(new ClusterName("remote")).build(); + autoFollowCoordinator.updateAutoFollowers(clusterState); + assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + } + public void testWaitForMetadataVersion() { Client client = mock(Client.class); when(client.getRemoteClusterClient(anyString())).thenReturn(client); @@ -611,7 +697,7 @@ public void testWaitForMetadataVersion() { List allResults = new ArrayList<>(); Consumer> handler = allResults::addAll; - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(states)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(states), () -> 1L) { long previousRequestedMetadataVersion = 0; @@ -669,7 +755,7 @@ public void testWaitForTimeOut() { fail("should not be invoked"); }; AtomicInteger counter = new AtomicInteger(); - AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(states)) { + AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(states), () -> 1L) { long previousRequestedMetadataVersion = 0; 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 c651cca5b6a71..41e771ac97ebb 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 @@ -12,6 +12,7 @@ import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; import static org.elasticsearch.xpack.ccr.action.AutoFollowStatsTests.randomReadExceptions; +import static org.elasticsearch.xpack.ccr.action.AutoFollowStatsTests.randomTrackingClusters; import static org.elasticsearch.xpack.ccr.action.StatsResponsesTests.createStatsResponse; public class AutoFollowStatsResponseTests extends AbstractWireSerializingTestCase { @@ -27,7 +28,8 @@ protected CcrStatsAction.Response createTestInstance() { randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomReadExceptions() + randomReadExceptions(), + randomTrackingClusters() ); FollowStatsAction.StatsResponses statsResponse = createStatsResponse(); return new CcrStatsAction.Response(autoFollowStats, statsResponse); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java index c4a61529f49a8..3302a55ae0280 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java @@ -34,7 +34,8 @@ protected AutoFollowStats createTestInstance() { randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomReadExceptions() + randomReadExceptions(), + randomTrackingClusters() ); } @@ -47,6 +48,15 @@ static NavigableMap randomReadExceptions() { return readExceptions; } + static NavigableMap randomTrackingClusters() { + final int count = randomIntBetween(0, 16); + final NavigableMap readExceptions = new TreeMap<>(); + for (int i = 0; i < count; i++) { + readExceptions.put("" + i, randomLong()); + } + return readExceptions; + } + @Override protected Writeable.Reader instanceReader() { return AutoFollowStats::new; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index ce1c0136677f6..323487d83093c 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -41,7 +41,7 @@ public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase @Before public void instantiateAutoFollowStats() { autoFollowStats = new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - Collections.emptyNavigableMap()); + Collections.emptyNavigableMap(), Collections.emptyNavigableMap()); } @Override @@ -74,8 +74,14 @@ public void testToXContent() throws IOException { new TreeMap<>(Collections.singletonMap( randomAlphaOfLength(4), new ElasticsearchException("cannot follow index"))); + + final NavigableMap trackingClusters = + new TreeMap<>(Collections.singletonMap( + randomAlphaOfLength(4), + 1L)); final AutoFollowStats autoFollowStats = - new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), recentAutoFollowExceptions); + new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), recentAutoFollowExceptions, + trackingClusters); final AutoFollowStatsMonitoringDoc document = new AutoFollowStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, autoFollowStats); @@ -99,7 +105,7 @@ public void testToXContent() throws IOException { + "\"ccr_auto_follow_stats\":{" + "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + "," + "\"number_of_failed_remote_cluster_state_requests\":" + - autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + "," + autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + "," + "\"number_of_successful_follow_indices\":" + autoFollowStats.getNumberOfSuccessfulFollowIndices() + "," + "\"recent_auto_follow_errors\":[" + "{" @@ -109,6 +115,12 @@ public void testToXContent() throws IOException { + "\"reason\":\"cannot follow index\"" + "}" + "}" + + "]," + + "\"tracking_remote_clusters\":[" + + "{" + + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," + + "\"time_since_last_auto_follow_started_millis\":" + trackingClusters.values().iterator().next() + + "}" + "]" + "}" + "}")); @@ -117,7 +129,11 @@ public void testToXContent() throws IOException { public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { final NavigableMap fetchExceptions = new TreeMap<>(Collections.singletonMap("leader_index", new ElasticsearchException("cannot follow index"))); - final AutoFollowStats status = new AutoFollowStats(1, 0, 2, fetchExceptions); + final NavigableMap trackingClusters = + new TreeMap<>(Collections.singletonMap( + randomAlphaOfLength(4), + 1L)); + final AutoFollowStats status = new AutoFollowStats(1, 0, 2, fetchExceptions, trackingClusters); XContentBuilder builder = jsonBuilder(); builder.value(status); Map serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false); @@ -154,6 +170,12 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat(exceptionFieldMapping.size(), equalTo(2)); assertThat(XContentMapValues.extractValue("type.type", exceptionFieldMapping), equalTo("keyword")); assertThat(XContentMapValues.extractValue("reason.type", exceptionFieldMapping), equalTo("text")); + } else if (fieldName.equals("tracking_remote_clusters")) { + assertThat(fieldType, equalTo("nested")); + assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(2)); + assertThat(XContentMapValues.extractValue("properties.cluster_name.type", fieldMapping), equalTo("keyword")); + assertThat(XContentMapValues.extractValue("properties.time_since_last_auto_follow_started_millis.type", fieldMapping), + equalTo("long")); } else { fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index 6f28c450f0473..77784cb97a680 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -33,6 +33,10 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField RECENT_AUTO_FOLLOW_ERRORS = new ParseField("recent_auto_follow_errors"); private static final ParseField LEADER_INDEX = new ParseField("leader_index"); private static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); + private static final ParseField TRACKING_REMOTE_CLUSTERS = new ParseField("tracking_remote_clusters"); + private static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); + private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = + new ParseField("time_since_last_auto_follow_started_millis"); @SuppressWarnings("unchecked") private static final ConstructingObjectParser STATS_PARSER = new ConstructingObjectParser<>("auto_follow_stats", @@ -43,26 +47,39 @@ public class AutoFollowStats implements Writeable, ToXContentObject { new TreeMap<>( ((List>) args[3]) .stream() - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) - )); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), + new TreeMap<>( + ((List>) args[3]) + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))); private static final ConstructingObjectParser, Void> AUTO_FOLLOW_EXCEPTIONS_PARSER = new ConstructingObjectParser<>( "auto_follow_stats_errors", args -> new AbstractMap.SimpleEntry<>((String) args[0], (ElasticsearchException) args[1])); + private static final ConstructingObjectParser, Void> TRACKING_REMOTE_CLUSTERS_PARSER = + new ConstructingObjectParser<>( + "tracking_remote_clusters", + args -> new AbstractMap.SimpleEntry<>((String) args[0], (Long) args[1])); + static { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX); AUTO_FOLLOW_EXCEPTIONS_PARSER.declareObject( ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); + TRACKING_REMOTE_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); + TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), + TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOW_EXCEPTIONS_PARSER, RECENT_AUTO_FOLLOW_ERRORS); + STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), TRACKING_REMOTE_CLUSTERS_PARSER, + TRACKING_REMOTE_CLUSTERS); } public static AutoFollowStats fromXContent(final XContentParser parser) { @@ -73,24 +90,28 @@ public static AutoFollowStats fromXContent(final XContentParser parser) { private final long numberOfFailedRemoteClusterStateRequests; private final long numberOfSuccessfulFollowIndices; private final NavigableMap recentAutoFollowErrors; + private final NavigableMap trackingRemoteClusters; public AutoFollowStats( - long numberOfFailedFollowIndices, - long numberOfFailedRemoteClusterStateRequests, - long numberOfSuccessfulFollowIndices, - NavigableMap recentAutoFollowErrors + long numberOfFailedFollowIndices, + long numberOfFailedRemoteClusterStateRequests, + long numberOfSuccessfulFollowIndices, + NavigableMap recentAutoFollowErrors, + NavigableMap trackingRemoteClusters ) { this.numberOfFailedFollowIndices = numberOfFailedFollowIndices; this.numberOfFailedRemoteClusterStateRequests = numberOfFailedRemoteClusterStateRequests; this.numberOfSuccessfulFollowIndices = numberOfSuccessfulFollowIndices; this.recentAutoFollowErrors = recentAutoFollowErrors; + this.trackingRemoteClusters = trackingRemoteClusters; } public AutoFollowStats(StreamInput in) throws IOException { numberOfFailedFollowIndices = in.readVLong(); numberOfFailedRemoteClusterStateRequests = in.readVLong(); numberOfSuccessfulFollowIndices = in.readVLong(); - recentAutoFollowErrors= new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readException)); + recentAutoFollowErrors = new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readException)); + trackingRemoteClusters = new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readZLong)); } @Override @@ -99,6 +120,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVLong(numberOfFailedRemoteClusterStateRequests); out.writeVLong(numberOfSuccessfulFollowIndices); out.writeMap(recentAutoFollowErrors, StreamOutput::writeString, StreamOutput::writeException); + out.writeMap(trackingRemoteClusters, StreamOutput::writeString, StreamOutput::writeZLong); } public long getNumberOfFailedFollowIndices() { @@ -117,6 +139,10 @@ public NavigableMap getRecentAutoFollowErrors() return recentAutoFollowErrors; } + public NavigableMap getTrackingRemoteClusters() { + return trackingRemoteClusters; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); @@ -148,6 +174,18 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P } } builder.endArray(); + builder.startArray(TRACKING_REMOTE_CLUSTERS.getPreferredName()); + { + for (final Map.Entry entry : trackingRemoteClusters.entrySet()) { + builder.startObject(); + { + builder.field(CLUSTER_NAME.getPreferredName(), entry.getKey()); + builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), entry.getValue()); + } + builder.endObject(); + } + } + builder.endArray(); return builder; } @@ -165,7 +203,8 @@ public boolean equals(Object o) { * keys. */ recentAutoFollowErrors.keySet().equals(that.recentAutoFollowErrors.keySet()) && - getFetchExceptionMessages(this).equals(getFetchExceptionMessages(that)); + getFetchExceptionMessages(this).equals(getFetchExceptionMessages(that)) && + Objects.equals(trackingRemoteClusters, that.trackingRemoteClusters); } @Override @@ -179,7 +218,8 @@ public int hashCode() { * messages. Note that we are relying on the fact that the auto follow exceptions are ordered by keys. */ recentAutoFollowErrors.keySet(), - getFetchExceptionMessages(this) + getFetchExceptionMessages(this), + trackingRemoteClusters ); } @@ -194,6 +234,7 @@ public String toString() { ", numberOfFailedRemoteClusterStateRequests=" + numberOfFailedRemoteClusterStateRequests + ", numberOfSuccessfulFollowIndices=" + numberOfSuccessfulFollowIndices + ", recentAutoFollowErrors=" + recentAutoFollowErrors + + ", trackingRemoteClusters=" + trackingRemoteClusters + '}'; } } diff --git a/x-pack/plugin/core/src/main/resources/monitoring-es.json b/x-pack/plugin/core/src/main/resources/monitoring-es.json index 1e6d3ec892af7..e20f52719d462 100644 --- a/x-pack/plugin/core/src/main/resources/monitoring-es.json +++ b/x-pack/plugin/core/src/main/resources/monitoring-es.json @@ -1060,6 +1060,17 @@ } } } + }, + "tracking_remote_clusters": { + "type": "nested", + "properties": { + "cluster_name": { + "type": "keyword" + }, + "time_since_last_auto_follow_started_millis": { + "type": "long" + } + } } } } From 7b2bd3c9658e7930fa523458b76ee5596eb72234 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 10:52:29 +0100 Subject: [PATCH 2/9] iter --- .../java/org/elasticsearch/xpack/ccr/Ccr.java | 2 +- .../ccr/action/AutoFollowCoordinator.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) 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 e1232b5c34c06..70d4905d94375 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 @@ -156,7 +156,7 @@ public Collection createComponents( return Arrays.asList( ccrLicenseChecker, - new AutoFollowCoordinator(client, clusterService, ccrLicenseChecker, System::nanoTime) + new AutoFollowCoordinator(client, clusterService, ccrLicenseChecker, threadPool::relativeTimeInMillis) ); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index f9f18eab67411..eabc3dcfddee6 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -69,7 +69,7 @@ public class AutoFollowCoordinator implements ClusterStateListener { private final Client client; private final ClusterService clusterService; private final CcrLicenseChecker ccrLicenseChecker; - private final LongSupplier relativeTimeProvider; + private final LongSupplier relativeNanoTimeProvider; private volatile Map autoFollowers = Collections.emptyMap(); @@ -83,12 +83,12 @@ public AutoFollowCoordinator( Client client, ClusterService clusterService, CcrLicenseChecker ccrLicenseChecker, - LongSupplier relativeTimeProvider) { + LongSupplier relativeNanoTimeProvider) { this.client = client; this.clusterService = clusterService; this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker, "ccrLicenseChecker"); - this.relativeTimeProvider = relativeTimeProvider; + this.relativeNanoTimeProvider = relativeNanoTimeProvider; clusterService.addListener(this); this.recentAutoFollowErrors = new LinkedHashMap() { @Override @@ -102,10 +102,10 @@ public synchronized AutoFollowStats getStats() { final Map autoFollowers = this.autoFollowers; final TreeMap timesSinceLastAutoFollowPerRemoteCluster = new TreeMap<>(); for (Map.Entry entry : autoFollowers.entrySet()) { - long timeSinceLastAutoFollow = entry.getValue().lastAutoFollowTime; - if (timeSinceLastAutoFollow != -1) { + long lastAutoFollowTimeInNanos = entry.getValue().lastAutoFollowTimeInNanos; + if (lastAutoFollowTimeInNanos != -1) { long timeSinceLastAutoFollowInMillis = - TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - timeSinceLastAutoFollow); + TimeUnit.NANOSECONDS.toMillis(relativeNanoTimeProvider.getAsLong() - lastAutoFollowTimeInNanos); timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), timeSinceLastAutoFollowInMillis); } else { timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), -1L); @@ -166,7 +166,7 @@ void updateAutoFollowers(ClusterState followerClusterState) { Map newAutoFollowers = new HashMap<>(newRemoteClusters.size()); for (String remoteCluster : newRemoteClusters) { - AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeTimeProvider) { + AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeNanoTimeProvider) { @Override void getRemoteClusterState(final String remoteCluster, @@ -261,7 +261,7 @@ abstract static class AutoFollower { private final Supplier followerClusterStateSupplier; private final LongSupplier relativeTimeProvider; - private volatile long lastAutoFollowTime = -1; + private volatile long lastAutoFollowTimeInNanos = -1; private volatile long metadataVersion = 0; private volatile CountDown autoFollowPatternsCountDown; private volatile AtomicArray autoFollowResults; @@ -277,7 +277,7 @@ abstract static class AutoFollower { } void start() { - lastAutoFollowTime = relativeTimeProvider.getAsLong(); + lastAutoFollowTimeInNanos = relativeTimeProvider.getAsLong(); final ClusterState clusterState = followerClusterStateSupplier.get(); final AutoFollowMetadata autoFollowMetadata = clusterState.metaData().custom(AutoFollowMetadata.TYPE); if (autoFollowMetadata == null) { From 6dcd88dcf81c66ae0dd87cd8f5a0bb678d5423ce Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 11:29:45 +0100 Subject: [PATCH 3/9] added `lastSeenMetadataVersion` and added serialization version checks --- .../ccr/action/AutoFollowCoordinator.java | 13 ++- .../ccr/action/AutoFollowStatsTests.java | 7 +- .../AutoFollowStatsMonitoringDocTests.java | 14 +-- .../xpack/core/ccr/AutoFollowStats.java | 86 ++++++++++++++++--- 4 files changed, 96 insertions(+), 24 deletions(-) diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index eabc3dcfddee6..ee4291773d244 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -57,6 +57,8 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import static org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster; + /** * A component that runs only on the elected master node and follows leader indices automatically * if they match with a auto follow pattern that is defined in {@link AutoFollowMetadata}. @@ -100,15 +102,17 @@ protected boolean removeEldestEntry(final Map.Entry autoFollowers = this.autoFollowers; - final TreeMap timesSinceLastAutoFollowPerRemoteCluster = new TreeMap<>(); + final TreeMap timesSinceLastAutoFollowPerRemoteCluster = new TreeMap<>(); for (Map.Entry entry : autoFollowers.entrySet()) { long lastAutoFollowTimeInNanos = entry.getValue().lastAutoFollowTimeInNanos; + long lastSeenMetadataVersion = entry.getValue().metadataVersion; if (lastAutoFollowTimeInNanos != -1) { long timeSinceLastAutoFollowInMillis = TimeUnit.NANOSECONDS.toMillis(relativeNanoTimeProvider.getAsLong() - lastAutoFollowTimeInNanos); - timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), timeSinceLastAutoFollowInMillis); + timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), + new AutoFollowedCluster(timeSinceLastAutoFollowInMillis, lastSeenMetadataVersion)); } else { - timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), -1L); + timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), new AutoFollowedCluster(-1L, lastSeenMetadataVersion)); } } @@ -166,7 +170,8 @@ void updateAutoFollowers(ClusterState followerClusterState) { Map newAutoFollowers = new HashMap<>(newRemoteClusters.size()); for (String remoteCluster : newRemoteClusters) { - AutoFollower autoFollower = new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeNanoTimeProvider) { + AutoFollower autoFollower = + new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeNanoTimeProvider) { @Override void getRemoteClusterState(final String remoteCluster, diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java index 3302a55ae0280..50266bd4a6716 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; +import org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster; import java.io.IOException; import java.util.Map; @@ -48,11 +49,11 @@ static NavigableMap randomReadExceptions() { return readExceptions; } - static NavigableMap randomTrackingClusters() { + static NavigableMap randomTrackingClusters() { final int count = randomIntBetween(0, 16); - final NavigableMap readExceptions = new TreeMap<>(); + final NavigableMap readExceptions = new TreeMap<>(); for (int i = 0; i < count; i++) { - readExceptions.put("" + i, randomLong()); + readExceptions.put("" + i, new AutoFollowedCluster(randomLong(), randomNonNegativeLong())); } return readExceptions; } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index 323487d83093c..1f58603785ca1 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; +import org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; @@ -75,10 +76,10 @@ public void testToXContent() throws IOException { randomAlphaOfLength(4), new ElasticsearchException("cannot follow index"))); - final NavigableMap trackingClusters = + final NavigableMap trackingClusters = new TreeMap<>(Collections.singletonMap( randomAlphaOfLength(4), - 1L)); + new AutoFollowedCluster(1L, 1L))); final AutoFollowStats autoFollowStats = new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), recentAutoFollowExceptions, trackingClusters); @@ -119,7 +120,10 @@ public void testToXContent() throws IOException { + "\"tracking_remote_clusters\":[" + "{" + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," - + "\"time_since_last_auto_follow_started_millis\":" + trackingClusters.values().iterator().next() + + "\"time_since_last_auto_follow_started_millis\":" + + trackingClusters.values().iterator().next().getTimeSinceLastAutoFollowMillis() + "," + + "\"last_seen_metadata_version\":" + + trackingClusters.values().iterator().next().getLastSeenMetadataVersion() + "}" + "]" + "}" @@ -129,10 +133,10 @@ public void testToXContent() throws IOException { public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { final NavigableMap fetchExceptions = new TreeMap<>(Collections.singletonMap("leader_index", new ElasticsearchException("cannot follow index"))); - final NavigableMap trackingClusters = + final NavigableMap trackingClusters = new TreeMap<>(Collections.singletonMap( randomAlphaOfLength(4), - 1L)); + new AutoFollowedCluster(1L, 1L))); final AutoFollowStats status = new AutoFollowStats(1, 0, 2, fetchExceptions, trackingClusters); XContentBuilder builder = jsonBuilder(); builder.value(status); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index 77784cb97a680..c674f6a011346 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.core.ccr; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.Version; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -17,6 +18,7 @@ import java.io.IOException; import java.util.AbstractMap; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.NavigableMap; @@ -37,6 +39,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = new ParseField("time_since_last_auto_follow_started_millis"); + private static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") private static final ConstructingObjectParser STATS_PARSER = new ConstructingObjectParser<>("auto_follow_stats", @@ -49,7 +52,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), new TreeMap<>( - ((List>) args[3]) + ((List>) args[3]) .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))); @@ -58,10 +61,10 @@ public class AutoFollowStats implements Writeable, ToXContentObject { "auto_follow_stats_errors", args -> new AbstractMap.SimpleEntry<>((String) args[0], (ElasticsearchException) args[1])); - private static final ConstructingObjectParser, Void> TRACKING_REMOTE_CLUSTERS_PARSER = + private static final ConstructingObjectParser, Void> TRACKING_REMOTE_CLUSTERS_PARSER = new ConstructingObjectParser<>( "tracking_remote_clusters", - args -> new AbstractMap.SimpleEntry<>((String) args[0], (Long) args[1])); + args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2]))); static { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX); @@ -70,8 +73,8 @@ public class AutoFollowStats implements Writeable, ToXContentObject { (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); TRACKING_REMOTE_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), - TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); @@ -90,14 +93,14 @@ public static AutoFollowStats fromXContent(final XContentParser parser) { private final long numberOfFailedRemoteClusterStateRequests; private final long numberOfSuccessfulFollowIndices; private final NavigableMap recentAutoFollowErrors; - private final NavigableMap trackingRemoteClusters; + private final NavigableMap trackingRemoteClusters; public AutoFollowStats( long numberOfFailedFollowIndices, long numberOfFailedRemoteClusterStateRequests, long numberOfSuccessfulFollowIndices, NavigableMap recentAutoFollowErrors, - NavigableMap trackingRemoteClusters + NavigableMap trackingRemoteClusters ) { this.numberOfFailedFollowIndices = numberOfFailedFollowIndices; this.numberOfFailedRemoteClusterStateRequests = numberOfFailedRemoteClusterStateRequests; @@ -111,7 +114,11 @@ public AutoFollowStats(StreamInput in) throws IOException { numberOfFailedRemoteClusterStateRequests = in.readVLong(); numberOfSuccessfulFollowIndices = in.readVLong(); recentAutoFollowErrors = new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readException)); - trackingRemoteClusters = new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readZLong)); + if (in.getVersion().onOrAfter(Version.V_6_6_0)) { + trackingRemoteClusters = new TreeMap<>(in.readMap(StreamInput::readString, AutoFollowedCluster::new)); + } else { + trackingRemoteClusters = Collections.emptyNavigableMap(); + } } @Override @@ -120,7 +127,9 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVLong(numberOfFailedRemoteClusterStateRequests); out.writeVLong(numberOfSuccessfulFollowIndices); out.writeMap(recentAutoFollowErrors, StreamOutput::writeString, StreamOutput::writeException); - out.writeMap(trackingRemoteClusters, StreamOutput::writeString, StreamOutput::writeZLong); + if (out.getVersion().onOrAfter(Version.V_6_6_0)) { + out.writeMap(trackingRemoteClusters, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); + } } public long getNumberOfFailedFollowIndices() { @@ -139,7 +148,7 @@ public NavigableMap getRecentAutoFollowErrors() return recentAutoFollowErrors; } - public NavigableMap getTrackingRemoteClusters() { + public NavigableMap getTrackingRemoteClusters() { return trackingRemoteClusters; } @@ -176,11 +185,13 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P builder.endArray(); builder.startArray(TRACKING_REMOTE_CLUSTERS.getPreferredName()); { - for (final Map.Entry entry : trackingRemoteClusters.entrySet()) { + for (final Map.Entry entry : trackingRemoteClusters.entrySet()) { builder.startObject(); { builder.field(CLUSTER_NAME.getPreferredName(), entry.getKey()); - builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), entry.getValue()); + builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), + entry.getValue().getTimeSinceLastAutoFollowMillis()); + builder.field(LAST_SEEN_METADATA_VERSION.getPreferredName(), entry.getValue().getLastSeenMetadataVersion()); } builder.endObject(); } @@ -237,4 +248,55 @@ public String toString() { ", trackingRemoteClusters=" + trackingRemoteClusters + '}'; } + + public static class AutoFollowedCluster implements Writeable { + + private final long timeSinceLastAutoFollowMillis; + private final long lastSeenMetadataVersion; + + public AutoFollowedCluster(long timeSinceLastAutoFollowMillis, long lastSeenMetadataVersion) { + this.timeSinceLastAutoFollowMillis = timeSinceLastAutoFollowMillis; + this.lastSeenMetadataVersion = lastSeenMetadataVersion; + } + + public AutoFollowedCluster(StreamInput in) throws IOException { + this(in.readZLong(), in.readVLong()); + } + + public long getTimeSinceLastAutoFollowMillis() { + return timeSinceLastAutoFollowMillis; + } + + public long getLastSeenMetadataVersion() { + return lastSeenMetadataVersion; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeZLong(timeSinceLastAutoFollowMillis); + out.writeVLong(lastSeenMetadataVersion); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + AutoFollowedCluster that = (AutoFollowedCluster) o; + return timeSinceLastAutoFollowMillis == that.timeSinceLastAutoFollowMillis && + lastSeenMetadataVersion == that.lastSeenMetadataVersion; + } + + @Override + public int hashCode() { + return Objects.hash(timeSinceLastAutoFollowMillis, lastSeenMetadataVersion); + } + + @Override + public String toString() { + return "AutoFollowedCluster{" + + "timeSinceLastAutoFollowMillis=" + timeSinceLastAutoFollowMillis + + ", lastSeenMetadataVersion=" + lastSeenMetadataVersion + + '}'; + } + } } From 8f08cb54b1db41d9ed1da4abed0ccf22a4e9f2b6 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 14:22:24 +0100 Subject: [PATCH 4/9] renamed `tracking_remote_clusters` to `auto_followed_clusters` and fixed monitor mapping tests --- .../action/AutoFollowCoordinatorTests.java | 22 +++++----- .../AutoFollowStatsMonitoringDocTests.java | 20 +++++---- .../xpack/core/ccr/AutoFollowStats.java | 42 +++++++++---------- .../src/main/resources/monitoring-es.json | 5 ++- 4 files changed, 49 insertions(+), 40 deletions(-) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index c6a71e9d88022..7228acaacf1a9 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -611,9 +611,9 @@ public void testUpdateAutoFollowers() { new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(2)); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote1"), notNullValue()); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(2)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote1"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote2"), notNullValue()); // Remove patterns 1 and 3: patterns.remove("pattern1"); patterns.remove("pattern3"); @@ -622,8 +622,8 @@ public void testUpdateAutoFollowers() { new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(1)); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(1)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote2"), notNullValue()); // Add pattern 4: patterns.put("pattern4", new AutoFollowPattern("remote1", Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null, null, null, null)); @@ -632,9 +632,9 @@ public void testUpdateAutoFollowers() { new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(2)); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote1"), notNullValue()); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().get("remote2"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(2)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote1"), notNullValue()); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote2"), notNullValue()); // Remove patterns 2 and 4: patterns.remove("pattern2"); patterns.remove("pattern4"); @@ -643,7 +643,7 @@ public void testUpdateAutoFollowers() { new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } public void testUpdateAutoFollowersNoPatterns() { @@ -657,7 +657,7 @@ public void testUpdateAutoFollowersNoPatterns() { new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } public void testUpdateAutoFollowersNoAutoFollowMetadata() { @@ -668,7 +668,7 @@ public void testUpdateAutoFollowersNoAutoFollowMetadata() { () -> 1L); ClusterState clusterState = ClusterState.builder(new ClusterName("remote")).build(); autoFollowCoordinator.updateAutoFollowers(clusterState); - assertThat(autoFollowCoordinator.getStats().getTrackingRemoteClusters().size(), equalTo(0)); + assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); } public void testWaitForMetadataVersion() { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index 1f58603785ca1..98bfccb6f35a0 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; @@ -117,7 +118,7 @@ public void testToXContent() throws IOException { + "}" + "}" + "]," - + "\"tracking_remote_clusters\":[" + + "\"auto_followed_clusters\":[" + "{" + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," + "\"time_since_last_auto_follow_started_millis\":" + @@ -162,24 +163,29 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("keyword"), equalTo("text"))); } else { + Map innerFieldValue = (Map) ((List) fieldValue).get(0); // Manual test specific object fields and if not just fail: if (fieldName.equals("recent_auto_follow_errors")) { assertThat(fieldType, equalTo("nested")); - assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(2)); + assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(innerFieldValue.size())); assertThat(XContentMapValues.extractValue("properties.leader_index.type", fieldMapping), equalTo("keyword")); assertThat(XContentMapValues.extractValue("properties.auto_follow_exception.type", fieldMapping), equalTo("object")); + innerFieldValue = (Map) innerFieldValue.get("auto_follow_exception"); Map exceptionFieldMapping = (Map) XContentMapValues.extractValue("properties.auto_follow_exception.properties", fieldMapping); - assertThat(exceptionFieldMapping.size(), equalTo(2)); + assertThat(exceptionFieldMapping.size(), equalTo(innerFieldValue.size())); assertThat(XContentMapValues.extractValue("type.type", exceptionFieldMapping), equalTo("keyword")); assertThat(XContentMapValues.extractValue("reason.type", exceptionFieldMapping), equalTo("text")); - } else if (fieldName.equals("tracking_remote_clusters")) { + } else if (fieldName.equals("auto_followed_clusters")) { assertThat(fieldType, equalTo("nested")); - assertThat(((Map) fieldMapping.get("properties")).size(), equalTo(2)); - assertThat(XContentMapValues.extractValue("properties.cluster_name.type", fieldMapping), equalTo("keyword")); - assertThat(XContentMapValues.extractValue("properties.time_since_last_auto_follow_started_millis.type", fieldMapping), + Map innerFieldMapping = ((Map) fieldMapping.get("properties")); + assertThat(innerFieldMapping.size(), equalTo(innerFieldValue.size())); + + assertThat(XContentMapValues.extractValue("cluster_name.type", innerFieldMapping), equalTo("keyword")); + assertThat(XContentMapValues.extractValue("time_since_last_auto_follow_started_millis.type", innerFieldMapping), equalTo("long")); + assertThat(XContentMapValues.extractValue("last_seen_metadata_version.type", innerFieldMapping), equalTo("long")); } else { fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index c674f6a011346..a25193ee45920 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -35,7 +35,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField RECENT_AUTO_FOLLOW_ERRORS = new ParseField("recent_auto_follow_errors"); private static final ParseField LEADER_INDEX = new ParseField("leader_index"); private static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); - private static final ParseField TRACKING_REMOTE_CLUSTERS = new ParseField("tracking_remote_clusters"); + private static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); private static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = new ParseField("time_since_last_auto_follow_started_millis"); @@ -61,9 +61,9 @@ public class AutoFollowStats implements Writeable, ToXContentObject { "auto_follow_stats_errors", args -> new AbstractMap.SimpleEntry<>((String) args[0], (ElasticsearchException) args[1])); - private static final ConstructingObjectParser, Void> TRACKING_REMOTE_CLUSTERS_PARSER = + private static final ConstructingObjectParser, Void> AUTO_FOLLOWED_CLUSTERS_PARSER = new ConstructingObjectParser<>( - "tracking_remote_clusters", + "auto_followed_clusters", args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2]))); static { @@ -72,17 +72,17 @@ public class AutoFollowStats implements Writeable, ToXContentObject { ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); - TRACKING_REMOTE_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); - TRACKING_REMOTE_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOW_EXCEPTIONS_PARSER, RECENT_AUTO_FOLLOW_ERRORS); - STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), TRACKING_REMOTE_CLUSTERS_PARSER, - TRACKING_REMOTE_CLUSTERS); + STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, + AUTO_FOLLOWED_CLUSTERS); } public static AutoFollowStats fromXContent(final XContentParser parser) { @@ -93,20 +93,20 @@ public static AutoFollowStats fromXContent(final XContentParser parser) { private final long numberOfFailedRemoteClusterStateRequests; private final long numberOfSuccessfulFollowIndices; private final NavigableMap recentAutoFollowErrors; - private final NavigableMap trackingRemoteClusters; + private final NavigableMap autoFollowedClusters; public AutoFollowStats( long numberOfFailedFollowIndices, long numberOfFailedRemoteClusterStateRequests, long numberOfSuccessfulFollowIndices, NavigableMap recentAutoFollowErrors, - NavigableMap trackingRemoteClusters + NavigableMap autoFollowedClusters ) { this.numberOfFailedFollowIndices = numberOfFailedFollowIndices; this.numberOfFailedRemoteClusterStateRequests = numberOfFailedRemoteClusterStateRequests; this.numberOfSuccessfulFollowIndices = numberOfSuccessfulFollowIndices; this.recentAutoFollowErrors = recentAutoFollowErrors; - this.trackingRemoteClusters = trackingRemoteClusters; + this.autoFollowedClusters = autoFollowedClusters; } public AutoFollowStats(StreamInput in) throws IOException { @@ -115,9 +115,9 @@ public AutoFollowStats(StreamInput in) throws IOException { numberOfSuccessfulFollowIndices = in.readVLong(); recentAutoFollowErrors = new TreeMap<>(in.readMap(StreamInput::readString, StreamInput::readException)); if (in.getVersion().onOrAfter(Version.V_6_6_0)) { - trackingRemoteClusters = new TreeMap<>(in.readMap(StreamInput::readString, AutoFollowedCluster::new)); + autoFollowedClusters = new TreeMap<>(in.readMap(StreamInput::readString, AutoFollowedCluster::new)); } else { - trackingRemoteClusters = Collections.emptyNavigableMap(); + autoFollowedClusters = Collections.emptyNavigableMap(); } } @@ -128,7 +128,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVLong(numberOfSuccessfulFollowIndices); out.writeMap(recentAutoFollowErrors, StreamOutput::writeString, StreamOutput::writeException); if (out.getVersion().onOrAfter(Version.V_6_6_0)) { - out.writeMap(trackingRemoteClusters, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); + out.writeMap(autoFollowedClusters, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); } } @@ -148,8 +148,8 @@ public NavigableMap getRecentAutoFollowErrors() return recentAutoFollowErrors; } - public NavigableMap getTrackingRemoteClusters() { - return trackingRemoteClusters; + public NavigableMap getAutoFollowedClusters() { + return autoFollowedClusters; } @Override @@ -183,9 +183,9 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P } } builder.endArray(); - builder.startArray(TRACKING_REMOTE_CLUSTERS.getPreferredName()); + builder.startArray(AUTO_FOLLOWED_CLUSTERS.getPreferredName()); { - for (final Map.Entry entry : trackingRemoteClusters.entrySet()) { + for (final Map.Entry entry : autoFollowedClusters.entrySet()) { builder.startObject(); { builder.field(CLUSTER_NAME.getPreferredName(), entry.getKey()); @@ -215,7 +215,7 @@ public boolean equals(Object o) { */ recentAutoFollowErrors.keySet().equals(that.recentAutoFollowErrors.keySet()) && getFetchExceptionMessages(this).equals(getFetchExceptionMessages(that)) && - Objects.equals(trackingRemoteClusters, that.trackingRemoteClusters); + Objects.equals(autoFollowedClusters, that.autoFollowedClusters); } @Override @@ -230,7 +230,7 @@ public int hashCode() { */ recentAutoFollowErrors.keySet(), getFetchExceptionMessages(this), - trackingRemoteClusters + autoFollowedClusters ); } @@ -245,7 +245,7 @@ public String toString() { ", numberOfFailedRemoteClusterStateRequests=" + numberOfFailedRemoteClusterStateRequests + ", numberOfSuccessfulFollowIndices=" + numberOfSuccessfulFollowIndices + ", recentAutoFollowErrors=" + recentAutoFollowErrors + - ", trackingRemoteClusters=" + trackingRemoteClusters + + ", autoFollowedClusters=" + autoFollowedClusters + '}'; } diff --git a/x-pack/plugin/core/src/main/resources/monitoring-es.json b/x-pack/plugin/core/src/main/resources/monitoring-es.json index e20f52719d462..68895625cde05 100644 --- a/x-pack/plugin/core/src/main/resources/monitoring-es.json +++ b/x-pack/plugin/core/src/main/resources/monitoring-es.json @@ -1061,7 +1061,7 @@ } } }, - "tracking_remote_clusters": { + "auto_followed_clusters": { "type": "nested", "properties": { "cluster_name": { @@ -1069,6 +1069,9 @@ }, "time_since_last_auto_follow_started_millis": { "type": "long" + }, + "last_seen_metadata_version": { + "type": "long" } } } From ff0724ff6ab3b56a2588a5c4aaf944ee397bfb0f Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 14:35:21 +0100 Subject: [PATCH 5/9] fixed docs --- docs/reference/ccr/apis/get-ccr-stats.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/reference/ccr/apis/get-ccr-stats.asciidoc b/docs/reference/ccr/apis/get-ccr-stats.asciidoc index 05c47ad45bfcf..d849a99c459d4 100644 --- a/docs/reference/ccr/apis/get-ccr-stats.asciidoc +++ b/docs/reference/ccr/apis/get-ccr-stats.asciidoc @@ -106,7 +106,7 @@ The API returns the following results: "number_of_failed_remote_cluster_state_requests" : 0, "number_of_successful_follow_indices" : 1, "recent_auto_follow_errors" : [], - "tracking_remote_clusters" : [] + "auto_followed_clusters" : [] }, "follow_stats" : { "indices" : [ @@ -152,7 +152,7 @@ The API returns the following results: // TESTRESPONSE[s/"number_of_failed_remote_cluster_state_requests" : 0/"number_of_failed_remote_cluster_state_requests" : $body.auto_follow_stats.number_of_failed_remote_cluster_state_requests/] // TESTRESPONSE[s/"number_of_successful_follow_indices" : 1/"number_of_successful_follow_indices" : $body.auto_follow_stats.number_of_successful_follow_indices/] // TESTRESPONSE[s/"recent_auto_follow_errors" : \[\]/"recent_auto_follow_errors" : $body.auto_follow_stats.recent_auto_follow_errors/] -// TESTRESPONSE[s/"tracking_remote_clusters" : \[\]/"tracking_remote_clusters" : $body.auto_follow_stats.tracking_remote_clusters/] +// TESTRESPONSE[s/"auto_followed_clusters" : \[\]/"auto_followed_clusters" : $body.auto_follow_stats.auto_followed_clusters/] // TESTRESPONSE[s/"leader_global_checkpoint" : 1024/"leader_global_checkpoint" : $body.follow_stats.indices.0.shards.0.leader_global_checkpoint/] // TESTRESPONSE[s/"leader_max_seq_no" : 1536/"leader_max_seq_no" : $body.follow_stats.indices.0.shards.0.leader_max_seq_no/] // TESTRESPONSE[s/"follower_global_checkpoint" : 768/"follower_global_checkpoint" : $body.follow_stats.indices.0.shards.0.follower_global_checkpoint/] From 29d46451bc21689b92e0e5fe6652c8687f88b53e Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 15:54:46 +0100 Subject: [PATCH 6/9] fixed hlrc --- .../client/ccr/AutoFollowStats.java | 47 ++++++++++++++++++- .../client/ccr/CcrStatsResponseTests.java | 21 ++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java index 09b57e68ff522..79be6014eacee 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java @@ -39,6 +39,10 @@ public final class AutoFollowStats { static final ParseField RECENT_AUTO_FOLLOW_ERRORS = new ParseField("recent_auto_follow_errors"); static final ParseField LEADER_INDEX = new ParseField("leader_index"); static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); + static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); + static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); + static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = new ParseField("time_since_last_auto_follow_started_millis"); + static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") static final ConstructingObjectParser STATS_PARSER = new ConstructingObjectParser<>("auto_follow_stats", @@ -48,6 +52,10 @@ public final class AutoFollowStats { (Long) args[2], new TreeMap<>( ((List>) args[3]) + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), + new TreeMap<>( + ((List>) args[4]) .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) )); @@ -57,6 +65,11 @@ public final class AutoFollowStats { "auto_follow_stats_errors", args -> new AbstractMap.SimpleEntry<>((String) args[0], (ElasticsearchException) args[1])); + private static final ConstructingObjectParser, Void> AUTO_FOLLOWED_CLUSTERS_PARSER = + new ConstructingObjectParser<>( + "auto_followed_clusters", + args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2]))); + static { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX); AUTO_FOLLOW_EXCEPTIONS_PARSER.declareObject( @@ -64,26 +77,35 @@ public final class AutoFollowStats { (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); + STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOW_EXCEPTIONS_PARSER, RECENT_AUTO_FOLLOW_ERRORS); + STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, + AUTO_FOLLOWED_CLUSTERS); } private final long numberOfFailedFollowIndices; private final long numberOfFailedRemoteClusterStateRequests; private final long numberOfSuccessfulFollowIndices; private final NavigableMap recentAutoFollowErrors; + private final NavigableMap autoFollowedClusters; AutoFollowStats(long numberOfFailedFollowIndices, long numberOfFailedRemoteClusterStateRequests, long numberOfSuccessfulFollowIndices, - NavigableMap recentAutoFollowErrors) { + NavigableMap recentAutoFollowErrors, + NavigableMap autoFollowedClusters) { this.numberOfFailedFollowIndices = numberOfFailedFollowIndices; this.numberOfFailedRemoteClusterStateRequests = numberOfFailedRemoteClusterStateRequests; this.numberOfSuccessfulFollowIndices = numberOfSuccessfulFollowIndices; this.recentAutoFollowErrors = recentAutoFollowErrors; + this.autoFollowedClusters = autoFollowedClusters; } public long getNumberOfFailedFollowIndices() { @@ -102,4 +124,27 @@ public NavigableMap getRecentAutoFollowErrors() return recentAutoFollowErrors; } + public NavigableMap getAutoFollowedClusters() { + return autoFollowedClusters; + } + + public static class AutoFollowedCluster { + + private final long timeSinceLastAutoFollowMillis; + private final long lastSeenMetadataVersion; + + public AutoFollowedCluster(long timeSinceLastAutoFollowMillis, long lastSeenMetadataVersion) { + this.timeSinceLastAutoFollowMillis = timeSinceLastAutoFollowMillis; + this.lastSeenMetadataVersion = lastSeenMetadataVersion; + } + + public long getTimeSinceLastAutoFollowMillis() { + return timeSinceLastAutoFollowMillis; + } + + public long getLastSeenMetadataVersion() { + return lastSeenMetadataVersion; + } + } + } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java index 039e31151c440..2b31b58125200 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.client.ccr; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.client.ccr.AutoFollowStats.AutoFollowedCluster; import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.unit.ByteSizeUnit; @@ -185,6 +186,19 @@ private static void toXContent(CcrStatsResponse response, XContentBuilder builde builder.endObject(); } builder.endArray(); + builder.startArray(AutoFollowStats.AUTO_FOLLOWED_CLUSTERS.getPreferredName()); + for (Map.Entry entry : autoFollowStats.getAutoFollowedClusters().entrySet()) { + builder.startObject(); + { + builder.field(AutoFollowStats.CLUSTER_NAME.getPreferredName(), entry.getKey()); + builder.field(AutoFollowStats.TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), + entry.getValue().getTimeSinceLastAutoFollowMillis()); + builder.field(AutoFollowStats.LAST_SEEN_METADATA_VERSION.getPreferredName(), + entry.getValue().getLastSeenMetadataVersion()); + } + builder.endObject(); + } + builder.endArray(); } builder.endObject(); @@ -315,11 +329,16 @@ private static AutoFollowStats randomAutoFollowStats() { for (int i = 0; i < count; i++) { readExceptions.put("" + i, new ElasticsearchException(new IllegalStateException("index [" + i + "]"))); } + final NavigableMap autoFollowClusters = new TreeMap<>(); + for (int i = 0; i < count; i++) { + autoFollowClusters.put("" + i, new AutoFollowedCluster(randomLong(), randomNonNegativeLong())); + } return new AutoFollowStats( randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - readExceptions + readExceptions, + autoFollowClusters ); } From e99790fc69326af12a032e10d5a586232ba0bda2 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Thu, 13 Dec 2018 16:07:23 +0100 Subject: [PATCH 7/9] fixed serialization bug --- .../xpack/ccr/action/AutoFollowStatsTests.java | 7 +++++++ .../org/elasticsearch/xpack/core/ccr/AutoFollowStats.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java index 50266bd4a6716..61b92b485c14a 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java @@ -67,6 +67,11 @@ protected Writeable.Reader instanceReader() { protected void assertEqualInstances(AutoFollowStats expectedInstance, AutoFollowStats newInstance) { assertNotSame(expectedInstance, newInstance); + assertThat(newInstance.getNumberOfFailedRemoteClusterStateRequests(), + equalTo(expectedInstance.getNumberOfFailedRemoteClusterStateRequests())); + assertThat(newInstance.getNumberOfFailedFollowIndices(), equalTo(expectedInstance.getNumberOfFailedFollowIndices())); + assertThat(newInstance.getNumberOfSuccessfulFollowIndices(), equalTo(expectedInstance.getNumberOfSuccessfulFollowIndices())); + assertThat(newInstance.getRecentAutoFollowErrors().size(), equalTo(expectedInstance.getRecentAutoFollowErrors().size())); assertThat(newInstance.getRecentAutoFollowErrors().keySet(), equalTo(expectedInstance.getRecentAutoFollowErrors().keySet())); for (final Map.Entry entry : newInstance.getRecentAutoFollowErrors().entrySet()) { @@ -79,6 +84,8 @@ protected void assertEqualInstances(AutoFollowStats expectedInstance, AutoFollow anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); assertThat(entry.getValue().getCause().getMessage(), containsString(expected.getCause().getMessage())); } + + assertThat(newInstance.getAutoFollowedClusters(), equalTo(expectedInstance.getAutoFollowedClusters())); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index a25193ee45920..1ec0dd59c903e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -52,7 +52,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), new TreeMap<>( - ((List>) args[3]) + ((List>) args[4]) .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))); From c0c0e8348c62a74330341417734cc9d5b4c3ee6c Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 14 Dec 2018 09:00:30 +0100 Subject: [PATCH 8/9] iter --- .../client/ccr/AutoFollowStats.java | 4 ++-- .../client/ccr/CcrStatsResponseTests.java | 2 +- .../ccr/action/AutoFollowCoordinator.java | 20 +++++++++---------- .../AutoFollowStatsMonitoringDocTests.java | 4 ++-- .../xpack/core/ccr/AutoFollowStats.java | 7 +++---- .../src/main/resources/monitoring-es.json | 2 +- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java index 79be6014eacee..1fbfbb03844df 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java @@ -41,7 +41,7 @@ public final class AutoFollowStats { static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); - static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = new ParseField("time_since_last_auto_follow_started_millis"); + static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS = new ParseField("time_since_last_auto_follow_millis"); static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") @@ -78,7 +78,7 @@ public final class AutoFollowStats { AUTO_FOLLOW_EXCEPTION); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java index 2b31b58125200..1c4de25ad27cf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java @@ -191,7 +191,7 @@ private static void toXContent(CcrStatsResponse response, XContentBuilder builde builder.startObject(); { builder.field(AutoFollowStats.CLUSTER_NAME.getPreferredName(), entry.getKey()); - builder.field(AutoFollowStats.TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), + builder.field(AutoFollowStats.TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS.getPreferredName(), entry.getValue().getTimeSinceLastAutoFollowMillis()); builder.field(AutoFollowStats.LAST_SEEN_METADATA_VERSION.getPreferredName(), entry.getValue().getLastSeenMetadataVersion()); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index ee4291773d244..534719bca4426 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -49,7 +49,6 @@ import java.util.Objects; import java.util.Set; import java.util.TreeMap; -import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -71,7 +70,7 @@ public class AutoFollowCoordinator implements ClusterStateListener { private final Client client; private final ClusterService clusterService; private final CcrLicenseChecker ccrLicenseChecker; - private final LongSupplier relativeNanoTimeProvider; + private final LongSupplier relativeMillisTimeProvider; private volatile Map autoFollowers = Collections.emptyMap(); @@ -85,12 +84,12 @@ public AutoFollowCoordinator( Client client, ClusterService clusterService, CcrLicenseChecker ccrLicenseChecker, - LongSupplier relativeNanoTimeProvider) { + LongSupplier relativeMillisTimeProvider) { this.client = client; this.clusterService = clusterService; this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker, "ccrLicenseChecker"); - this.relativeNanoTimeProvider = relativeNanoTimeProvider; + this.relativeMillisTimeProvider = relativeMillisTimeProvider; clusterService.addListener(this); this.recentAutoFollowErrors = new LinkedHashMap() { @Override @@ -104,11 +103,10 @@ public synchronized AutoFollowStats getStats() { final Map autoFollowers = this.autoFollowers; final TreeMap timesSinceLastAutoFollowPerRemoteCluster = new TreeMap<>(); for (Map.Entry entry : autoFollowers.entrySet()) { - long lastAutoFollowTimeInNanos = entry.getValue().lastAutoFollowTimeInNanos; + long lastAutoFollowTimeInMillis = entry.getValue().lastAutoFollowTimeInMillis; long lastSeenMetadataVersion = entry.getValue().metadataVersion; - if (lastAutoFollowTimeInNanos != -1) { - long timeSinceLastAutoFollowInMillis = - TimeUnit.NANOSECONDS.toMillis(relativeNanoTimeProvider.getAsLong() - lastAutoFollowTimeInNanos); + if (lastAutoFollowTimeInMillis != -1) { + long timeSinceLastAutoFollowInMillis = relativeMillisTimeProvider.getAsLong() - lastAutoFollowTimeInMillis; timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), new AutoFollowedCluster(timeSinceLastAutoFollowInMillis, lastSeenMetadataVersion)); } else { @@ -171,7 +169,7 @@ void updateAutoFollowers(ClusterState followerClusterState) { Map newAutoFollowers = new HashMap<>(newRemoteClusters.size()); for (String remoteCluster : newRemoteClusters) { AutoFollower autoFollower = - new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeNanoTimeProvider) { + new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeMillisTimeProvider) { @Override void getRemoteClusterState(final String remoteCluster, @@ -266,7 +264,7 @@ abstract static class AutoFollower { private final Supplier followerClusterStateSupplier; private final LongSupplier relativeTimeProvider; - private volatile long lastAutoFollowTimeInNanos = -1; + private volatile long lastAutoFollowTimeInMillis = -1; private volatile long metadataVersion = 0; private volatile CountDown autoFollowPatternsCountDown; private volatile AtomicArray autoFollowResults; @@ -282,7 +280,7 @@ abstract static class AutoFollower { } void start() { - lastAutoFollowTimeInNanos = relativeTimeProvider.getAsLong(); + lastAutoFollowTimeInMillis = relativeTimeProvider.getAsLong(); final ClusterState clusterState = followerClusterStateSupplier.get(); final AutoFollowMetadata autoFollowMetadata = clusterState.metaData().custom(AutoFollowMetadata.TYPE); if (autoFollowMetadata == null) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index 98bfccb6f35a0..e51b377229ee1 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -121,7 +121,7 @@ public void testToXContent() throws IOException { + "\"auto_followed_clusters\":[" + "{" + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," - + "\"time_since_last_auto_follow_started_millis\":" + + + "\"time_since_last_auto_follow_millis\":" + trackingClusters.values().iterator().next().getTimeSinceLastAutoFollowMillis() + "," + "\"last_seen_metadata_version\":" + trackingClusters.values().iterator().next().getLastSeenMetadataVersion() @@ -183,7 +183,7 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat(innerFieldMapping.size(), equalTo(innerFieldValue.size())); assertThat(XContentMapValues.extractValue("cluster_name.type", innerFieldMapping), equalTo("keyword")); - assertThat(XContentMapValues.extractValue("time_since_last_auto_follow_started_millis.type", innerFieldMapping), + assertThat(XContentMapValues.extractValue("time_since_last_auto_follow_millis.type", innerFieldMapping), equalTo("long")); assertThat(XContentMapValues.extractValue("last_seen_metadata_version.type", innerFieldMapping), equalTo("long")); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index 1ec0dd59c903e..11767e277b7c6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -37,8 +37,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); private static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); private static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); - private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS = - new ParseField("time_since_last_auto_follow_started_millis"); + private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS = new ParseField("time_since_last_auto_follow_millis"); private static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") @@ -73,7 +72,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); @@ -189,7 +188,7 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P builder.startObject(); { builder.field(CLUSTER_NAME.getPreferredName(), entry.getKey()); - builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_STARTED_MILLIS.getPreferredName(), + builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS.getPreferredName(), entry.getValue().getTimeSinceLastAutoFollowMillis()); builder.field(LAST_SEEN_METADATA_VERSION.getPreferredName(), entry.getValue().getLastSeenMetadataVersion()); } diff --git a/x-pack/plugin/core/src/main/resources/monitoring-es.json b/x-pack/plugin/core/src/main/resources/monitoring-es.json index 68895625cde05..0bbebd24e24ed 100644 --- a/x-pack/plugin/core/src/main/resources/monitoring-es.json +++ b/x-pack/plugin/core/src/main/resources/monitoring-es.json @@ -1067,7 +1067,7 @@ "cluster_name": { "type": "keyword" }, - "time_since_last_auto_follow_started_millis": { + "time_since_last_auto_follow_millis": { "type": "long" }, "last_seen_metadata_version": { From c196e017ac9b2f3800879468ce270f15b1d76df0 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 17 Dec 2018 11:17:33 +0100 Subject: [PATCH 9/9] rename --- .../client/ccr/AutoFollowStats.java | 14 +++++------ .../client/ccr/CcrStatsResponseTests.java | 4 +-- .../ccr/action/AutoFollowCoordinator.java | 4 +-- .../AutoFollowStatsMonitoringDocTests.java | 7 +++--- .../xpack/core/ccr/AutoFollowStats.java | 25 +++++++++---------- .../src/main/resources/monitoring-es.json | 2 +- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java index 1fbfbb03844df..b442336ca4dd2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java @@ -41,7 +41,7 @@ public final class AutoFollowStats { static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); - static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS = new ParseField("time_since_last_auto_follow_millis"); + static final ParseField TIME_SINCE_LAST_CHECK_MILLIS = new ParseField("time_since_last_check_millis"); static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") @@ -78,7 +78,7 @@ public final class AutoFollowStats { AUTO_FOLLOW_EXCEPTION); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_CHECK_MILLIS); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); @@ -130,16 +130,16 @@ public NavigableMap getAutoFollowedClusters() { public static class AutoFollowedCluster { - private final long timeSinceLastAutoFollowMillis; + private final long timeSinceLastCheckMillis; private final long lastSeenMetadataVersion; - public AutoFollowedCluster(long timeSinceLastAutoFollowMillis, long lastSeenMetadataVersion) { - this.timeSinceLastAutoFollowMillis = timeSinceLastAutoFollowMillis; + public AutoFollowedCluster(long timeSinceLastCheckMillis, long lastSeenMetadataVersion) { + this.timeSinceLastCheckMillis = timeSinceLastCheckMillis; this.lastSeenMetadataVersion = lastSeenMetadataVersion; } - public long getTimeSinceLastAutoFollowMillis() { - return timeSinceLastAutoFollowMillis; + public long getTimeSinceLastCheckMillis() { + return timeSinceLastCheckMillis; } public long getLastSeenMetadataVersion() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java index 1c4de25ad27cf..8d53b5cde0827 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java @@ -191,8 +191,8 @@ private static void toXContent(CcrStatsResponse response, XContentBuilder builde builder.startObject(); { builder.field(AutoFollowStats.CLUSTER_NAME.getPreferredName(), entry.getKey()); - builder.field(AutoFollowStats.TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS.getPreferredName(), - entry.getValue().getTimeSinceLastAutoFollowMillis()); + builder.field(AutoFollowStats.TIME_SINCE_LAST_CHECK_MILLIS.getPreferredName(), + entry.getValue().getTimeSinceLastCheckMillis()); builder.field(AutoFollowStats.LAST_SEEN_METADATA_VERSION.getPreferredName(), entry.getValue().getLastSeenMetadataVersion()); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index 534719bca4426..4888b0367fd20 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -106,9 +106,9 @@ public synchronized AutoFollowStats getStats() { long lastAutoFollowTimeInMillis = entry.getValue().lastAutoFollowTimeInMillis; long lastSeenMetadataVersion = entry.getValue().metadataVersion; if (lastAutoFollowTimeInMillis != -1) { - long timeSinceLastAutoFollowInMillis = relativeMillisTimeProvider.getAsLong() - lastAutoFollowTimeInMillis; + long timeSinceLastCheckInMillis = relativeMillisTimeProvider.getAsLong() - lastAutoFollowTimeInMillis; timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), - new AutoFollowedCluster(timeSinceLastAutoFollowInMillis, lastSeenMetadataVersion)); + new AutoFollowedCluster(timeSinceLastCheckInMillis, lastSeenMetadataVersion)); } else { timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), new AutoFollowedCluster(-1L, lastSeenMetadataVersion)); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index e51b377229ee1..cebb7cfd775a6 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -121,8 +121,8 @@ public void testToXContent() throws IOException { + "\"auto_followed_clusters\":[" + "{" + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," - + "\"time_since_last_auto_follow_millis\":" + - trackingClusters.values().iterator().next().getTimeSinceLastAutoFollowMillis() + "," + + "\"time_since_last_check_millis\":" + + trackingClusters.values().iterator().next().getTimeSinceLastCheckMillis() + "," + "\"last_seen_metadata_version\":" + trackingClusters.values().iterator().next().getLastSeenMetadataVersion() + "}" @@ -183,8 +183,7 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat(innerFieldMapping.size(), equalTo(innerFieldValue.size())); assertThat(XContentMapValues.extractValue("cluster_name.type", innerFieldMapping), equalTo("keyword")); - assertThat(XContentMapValues.extractValue("time_since_last_auto_follow_millis.type", innerFieldMapping), - equalTo("long")); + assertThat(XContentMapValues.extractValue("time_since_last_check_millis.type", innerFieldMapping), equalTo("long")); assertThat(XContentMapValues.extractValue("last_seen_metadata_version.type", innerFieldMapping), equalTo("long")); } else { fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index 11767e277b7c6..032cedbdcdf69 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -37,7 +37,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); private static final ParseField AUTO_FOLLOWED_CLUSTERS = new ParseField("auto_followed_clusters"); private static final ParseField CLUSTER_NAME = new ParseField("cluster_name"); - private static final ParseField TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS = new ParseField("time_since_last_auto_follow_millis"); + private static final ParseField TIME_SINCE_LAST_CHECK_MILLIS = new ParseField("time_since_last_check_millis"); private static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") @@ -72,7 +72,7 @@ public class AutoFollowStats implements Writeable, ToXContentObject { (p, c) -> ElasticsearchException.fromXContent(p), AUTO_FOLLOW_EXCEPTION); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); - AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS); + AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_CHECK_MILLIS); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); @@ -188,8 +188,7 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P builder.startObject(); { builder.field(CLUSTER_NAME.getPreferredName(), entry.getKey()); - builder.field(TIME_SINCE_LAST_AUTO_FOLLOW_MILLIS.getPreferredName(), - entry.getValue().getTimeSinceLastAutoFollowMillis()); + builder.field(TIME_SINCE_LAST_CHECK_MILLIS.getPreferredName(), entry.getValue().getTimeSinceLastCheckMillis()); builder.field(LAST_SEEN_METADATA_VERSION.getPreferredName(), entry.getValue().getLastSeenMetadataVersion()); } builder.endObject(); @@ -250,11 +249,11 @@ public String toString() { public static class AutoFollowedCluster implements Writeable { - private final long timeSinceLastAutoFollowMillis; + private final long timeSinceLastCheckMillis; private final long lastSeenMetadataVersion; - public AutoFollowedCluster(long timeSinceLastAutoFollowMillis, long lastSeenMetadataVersion) { - this.timeSinceLastAutoFollowMillis = timeSinceLastAutoFollowMillis; + public AutoFollowedCluster(long timeSinceLastCheckMillis, long lastSeenMetadataVersion) { + this.timeSinceLastCheckMillis = timeSinceLastCheckMillis; this.lastSeenMetadataVersion = lastSeenMetadataVersion; } @@ -262,8 +261,8 @@ public AutoFollowedCluster(StreamInput in) throws IOException { this(in.readZLong(), in.readVLong()); } - public long getTimeSinceLastAutoFollowMillis() { - return timeSinceLastAutoFollowMillis; + public long getTimeSinceLastCheckMillis() { + return timeSinceLastCheckMillis; } public long getLastSeenMetadataVersion() { @@ -272,7 +271,7 @@ public long getLastSeenMetadataVersion() { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeZLong(timeSinceLastAutoFollowMillis); + out.writeZLong(timeSinceLastCheckMillis); out.writeVLong(lastSeenMetadataVersion); } @@ -281,19 +280,19 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AutoFollowedCluster that = (AutoFollowedCluster) o; - return timeSinceLastAutoFollowMillis == that.timeSinceLastAutoFollowMillis && + return timeSinceLastCheckMillis == that.timeSinceLastCheckMillis && lastSeenMetadataVersion == that.lastSeenMetadataVersion; } @Override public int hashCode() { - return Objects.hash(timeSinceLastAutoFollowMillis, lastSeenMetadataVersion); + return Objects.hash(timeSinceLastCheckMillis, lastSeenMetadataVersion); } @Override public String toString() { return "AutoFollowedCluster{" + - "timeSinceLastAutoFollowMillis=" + timeSinceLastAutoFollowMillis + + "timeSinceLastCheckMillis=" + timeSinceLastCheckMillis + ", lastSeenMetadataVersion=" + lastSeenMetadataVersion + '}'; } diff --git a/x-pack/plugin/core/src/main/resources/monitoring-es.json b/x-pack/plugin/core/src/main/resources/monitoring-es.json index 0bbebd24e24ed..c34fed3751679 100644 --- a/x-pack/plugin/core/src/main/resources/monitoring-es.json +++ b/x-pack/plugin/core/src/main/resources/monitoring-es.json @@ -1067,7 +1067,7 @@ "cluster_name": { "type": "keyword" }, - "time_since_last_auto_follow_millis": { + "time_since_last_check_millis": { "type": "long" }, "last_seen_metadata_version": {