From 84a410d5a89d7ddaaf0ea9f4ecf66a79ddd8421c Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 8 Apr 2019 11:24:06 +0200 Subject: [PATCH] Revert "Change HLRC CCR response tests to use AbstractResponseTestCase base class. (#40257)" This reverts commit c29027d99e969d464fcb461921ae5b0de6f7d086. --- client/rest-high-level/build.gradle | 3 - .../client/ccr/CcrStatsResponseTests.java | 411 ++++++++++++------ .../client/ccr/FollowInfoResponseTests.java | 118 ++--- .../client/ccr/FollowStatsResponseTests.java | 299 +++++++++---- .../GetAutoFollowPatternResponseTests.java | 126 +++--- .../client/ccr/PutFollowResponseTests.java | 37 +- 6 files changed, 621 insertions(+), 373 deletions(-) diff --git a/client/rest-high-level/build.gradle b/client/rest-high-level/build.gradle index 420bd6d7414..44262f09346 100644 --- a/client/rest-high-level/build.gradle +++ b/client/rest-high-level/build.gradle @@ -64,9 +64,6 @@ dependencies { testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" //this is needed to make RestHighLevelClientTests#testApiNamingConventions work from IDEs testCompile "org.elasticsearch:rest-api-spec:${version}" - // Needed for serialization tests: - // (In order to serialize a server side class to a client side class or the other way around) - testCompile "org.elasticsearch.plugin:x-pack-core:${version}" restSpec "org.elasticsearch:rest-api-spec:${version}" } 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 d56b762520c..cb8072f6baf 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,110 +20,50 @@ package org.elasticsearch.client.ccr; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.client.ccr.AutoFollowStats.AutoFollowedCluster; import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats; import org.elasticsearch.common.collect.Tuple; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; -import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; -import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; +import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; +import java.util.concurrent.TimeUnit; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; -public class CcrStatsResponseTests extends AbstractResponseTestCase { +public class CcrStatsResponseTests extends ESTestCase { - @Override - protected CcrStatsAction.Response createServerTestInstance() { - org.elasticsearch.xpack.core.ccr.AutoFollowStats autoFollowStats = new org.elasticsearch.xpack.core.ccr.AutoFollowStats( - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomReadExceptions(), - randomTrackingClusters() - ); - FollowStatsAction.StatsResponses statsResponse = createStatsResponse(); - return new CcrStatsAction.Response(autoFollowStats, statsResponse); + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + CcrStatsResponseTests::createTestInstance, + CcrStatsResponseTests::toXContent, + CcrStatsResponse::fromXContent) + .supportsUnknownFields(true) + .assertEqualsConsumer(CcrStatsResponseTests::assertEqualInstances) + .assertToXContentEquivalence(false) + .test(); } - static NavigableMap> randomReadExceptions() { - final int count = randomIntBetween(0, 16); - final NavigableMap> readExceptions = new TreeMap<>(); - for (int i = 0; i < count; i++) { - readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(), - new ElasticsearchException(new IllegalStateException("index [" + i + "]")))); - } - return readExceptions; - } + // Needed, because exceptions in IndicesFollowStats and AutoFollowStats cannot be compared + private static void assertEqualInstances(CcrStatsResponse expectedInstance, CcrStatsResponse newInstance) { + assertNotSame(expectedInstance, newInstance); - static NavigableMap randomTrackingClusters() { - final int count = randomIntBetween(0, 16); - final NavigableMap readExceptions = new TreeMap<>(); - for (int i = 0; i < count; i++) { - readExceptions.put("" + i, - new org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster(randomLong(), randomNonNegativeLong())); - } - return readExceptions; - } - - static FollowStatsAction.StatsResponses createStatsResponse() { - int numResponses = randomIntBetween(0, 8); - List responses = new ArrayList<>(numResponses); - for (int i = 0; i < numResponses; i++) { - ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( - randomAlphaOfLength(4), - randomAlphaOfLength(4), - randomAlphaOfLength(4), - randomInt(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomIntBetween(0, Integer.MAX_VALUE), - randomIntBetween(0, Integer.MAX_VALUE), - randomIntBetween(0, Integer.MAX_VALUE), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - Collections.emptyNavigableMap(), - randomLong(), - randomBoolean() ? new ElasticsearchException("fatal error") : null); - responses.add(new FollowStatsAction.StatsResponse(status)); - } - return new FollowStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), responses); - } - - @Override - protected CcrStatsResponse doParseToClientInstance(XContentParser parser) throws IOException { - return CcrStatsResponse.fromXContent(parser); - } - - @Override - protected void assertInstances(CcrStatsAction.Response serverTestInstance, CcrStatsResponse clientInstance) { { - AutoFollowStats newAutoFollowStats = clientInstance.getAutoFollowStats(); - org.elasticsearch.xpack.core.ccr.AutoFollowStats expectedAutoFollowStats = serverTestInstance.getAutoFollowStats(); + AutoFollowStats newAutoFollowStats = newInstance.getAutoFollowStats(); + AutoFollowStats expectedAutoFollowStats = expectedInstance.getAutoFollowStats(); assertThat(newAutoFollowStats.getNumberOfSuccessfulFollowIndices(), equalTo(expectedAutoFollowStats.getNumberOfSuccessfulFollowIndices())); assertThat(newAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), @@ -149,69 +89,62 @@ public class CcrStatsResponseTests extends AbstractResponseTestCase> expectedIndicesFollowStats = new TreeMap<>(); - for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getFollowStats().getStatsResponses()) { - expectedIndicesFollowStats.computeIfAbsent( - statsResponse.status().followerIndex(), - k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); - } + IndicesFollowStats newIndicesFollowStats = newInstance.getIndicesFollowStats(); + IndicesFollowStats expectedIndicesFollowStats = expectedInstance.getIndicesFollowStats(); assertThat(newIndicesFollowStats.getShardFollowStats().size(), - equalTo(expectedIndicesFollowStats.size())); + equalTo(expectedIndicesFollowStats.getShardFollowStats().size())); assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), - equalTo(expectedIndicesFollowStats.keySet())); + equalTo(expectedIndicesFollowStats.getShardFollowStats().keySet())); for (Map.Entry> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { List newStats = indexEntry.getValue(); - Map expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); + List expectedStats = expectedIndicesFollowStats.getShardFollowStats(indexEntry.getKey()); assertThat(newStats.size(), equalTo(expectedStats.size())); for (int i = 0; i < newStats.size(); i++) { ShardFollowStats actualShardFollowStats = newStats.get(i); - ShardFollowNodeTaskStatus expectedShardFollowStats = expectedStats.get(actualShardFollowStats.getShardId()).status(); + ShardFollowStats expectedShardFollowStats = expectedStats.get(i); assertThat(actualShardFollowStats.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster())); - assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); - assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); + assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.getLeaderIndex())); + assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.getFollowerIndex())); assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), - equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); - assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); + equalTo(expectedShardFollowStats.getLeaderGlobalCheckpoint())); + assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.getLeaderMaxSeqNo())); assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), - equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); - assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); + equalTo(expectedShardFollowStats.getFollowerGlobalCheckpoint())); + assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.getLastRequestedSeqNo())); assertThat(actualShardFollowStats.getOutstandingReadRequests(), - equalTo(expectedShardFollowStats.outstandingReadRequests())); + equalTo(expectedShardFollowStats.getOutstandingReadRequests())); assertThat(actualShardFollowStats.getOutstandingWriteRequests(), - equalTo(expectedShardFollowStats.outstandingWriteRequests())); + equalTo(expectedShardFollowStats.getOutstandingWriteRequests())); assertThat(actualShardFollowStats.getWriteBufferOperationCount(), - equalTo(expectedShardFollowStats.writeBufferOperationCount())); + equalTo(expectedShardFollowStats.getWriteBufferOperationCount())); assertThat(actualShardFollowStats.getFollowerMappingVersion(), - equalTo(expectedShardFollowStats.followerMappingVersion())); + equalTo(expectedShardFollowStats.getFollowerMappingVersion())); assertThat(actualShardFollowStats.getFollowerSettingsVersion(), - equalTo(expectedShardFollowStats.followerSettingsVersion())); + equalTo(expectedShardFollowStats.getFollowerSettingsVersion())); assertThat(actualShardFollowStats.getTotalReadTimeMillis(), - equalTo(expectedShardFollowStats.totalReadTimeMillis())); + equalTo(expectedShardFollowStats.getTotalReadTimeMillis())); assertThat(actualShardFollowStats.getSuccessfulReadRequests(), - equalTo(expectedShardFollowStats.successfulReadRequests())); - assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); - assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); - assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); + equalTo(expectedShardFollowStats.getSuccessfulReadRequests())); + assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.getFailedReadRequests())); + assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.getOperationsReads())); + assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.getBytesRead())); assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), - equalTo(expectedShardFollowStats.totalWriteTimeMillis())); + equalTo(expectedShardFollowStats.getTotalWriteTimeMillis())); assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), - equalTo(expectedShardFollowStats.successfulWriteRequests())); + equalTo(expectedShardFollowStats.getSuccessfulWriteRequests())); assertThat(actualShardFollowStats.getFailedWriteRequests(), - equalTo(expectedShardFollowStats.failedWriteRequests())); - assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.operationWritten())); + equalTo(expectedShardFollowStats.getFailedWriteRequests())); + assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.getOperationWritten())); assertThat(actualShardFollowStats.getReadExceptions().size(), - equalTo(expectedShardFollowStats.readExceptions().size())); + equalTo(expectedShardFollowStats.getReadExceptions().size())); assertThat(actualShardFollowStats.getReadExceptions().keySet(), - equalTo(expectedShardFollowStats.readExceptions().keySet())); + equalTo(expectedShardFollowStats.getReadExceptions().keySet())); for (final Map.Entry> entry : actualShardFollowStats.getReadExceptions().entrySet()) { final Tuple expectedTuple = - expectedShardFollowStats.readExceptions().get(entry.getKey()); + expectedShardFollowStats.getReadExceptions().get(entry.getKey()); assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); // x-content loses the exception final ElasticsearchException expected = expectedTuple.v2(); @@ -223,10 +156,246 @@ public class CcrStatsResponseTests extends AbstractResponseTestCase> entry : + autoFollowStats.getRecentAutoFollowErrors().entrySet()) { + builder.startObject(); + { + builder.field(AutoFollowStats.LEADER_INDEX.getPreferredName(), entry.getKey()); + builder.field(AutoFollowStats.TIMESTAMP.getPreferredName(), entry.getValue().v1()); + builder.field(AutoFollowStats.AUTO_FOLLOW_EXCEPTION.getPreferredName()); + builder.startObject(); + { + ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, entry.getValue().v2()); + } + builder.endObject(); + } + 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_CHECK_MILLIS.getPreferredName(), + entry.getValue().getTimeSinceLastCheckMillis()); + builder.field(AutoFollowStats.LAST_SEEN_METADATA_VERSION.getPreferredName(), + entry.getValue().getLastSeenMetadataVersion()); + } + builder.endObject(); + } + builder.endArray(); + } + builder.endObject(); + + IndicesFollowStats indicesFollowStats = response.getIndicesFollowStats(); + builder.startObject(CcrStatsResponse.FOLLOW_STATS_FIELD.getPreferredName()); + { + builder.startArray(IndicesFollowStats.INDICES_FIELD.getPreferredName()); + for (Map.Entry> indexEntry : + indicesFollowStats.getShardFollowStats().entrySet()) { + builder.startObject(); + { + builder.field(IndicesFollowStats.INDEX_FIELD.getPreferredName(), indexEntry.getKey()); + builder.startArray(IndicesFollowStats.SHARDS_FIELD.getPreferredName()); + { + for (ShardFollowStats stats : indexEntry.getValue()) { + builder.startObject(); + { + builder.field(ShardFollowStats.LEADER_CLUSTER.getPreferredName(), stats.getRemoteCluster()); + builder.field(ShardFollowStats.LEADER_INDEX.getPreferredName(), stats.getLeaderIndex()); + builder.field(ShardFollowStats.FOLLOWER_INDEX.getPreferredName(), stats.getFollowerIndex()); + builder.field(ShardFollowStats.SHARD_ID.getPreferredName(), stats.getShardId()); + builder.field(ShardFollowStats.LEADER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), + stats.getLeaderGlobalCheckpoint()); + builder.field(ShardFollowStats.LEADER_MAX_SEQ_NO_FIELD.getPreferredName(), stats.getLeaderMaxSeqNo()); + builder.field(ShardFollowStats.FOLLOWER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), + stats.getFollowerGlobalCheckpoint()); + builder.field(ShardFollowStats.FOLLOWER_MAX_SEQ_NO_FIELD.getPreferredName(), + stats.getFollowerMaxSeqNo()); + builder.field(ShardFollowStats.LAST_REQUESTED_SEQ_NO_FIELD.getPreferredName(), + stats.getLastRequestedSeqNo()); + builder.field(ShardFollowStats.OUTSTANDING_READ_REQUESTS.getPreferredName(), + stats.getOutstandingReadRequests()); + builder.field(ShardFollowStats.OUTSTANDING_WRITE_REQUESTS.getPreferredName(), + stats.getOutstandingWriteRequests()); + builder.field(ShardFollowStats.WRITE_BUFFER_OPERATION_COUNT_FIELD.getPreferredName(), + stats.getWriteBufferOperationCount()); + builder.humanReadableField( + ShardFollowStats.WRITE_BUFFER_SIZE_IN_BYTES_FIELD.getPreferredName(), + "write_buffer_size", + new ByteSizeValue(stats.getWriteBufferSizeInBytes())); + builder.field(ShardFollowStats.FOLLOWER_MAPPING_VERSION_FIELD.getPreferredName(), + stats.getFollowerMappingVersion()); + builder.field(ShardFollowStats.FOLLOWER_SETTINGS_VERSION_FIELD.getPreferredName(), + stats.getFollowerSettingsVersion()); + builder.humanReadableField( + ShardFollowStats.TOTAL_READ_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_time", + new TimeValue(stats.getTotalReadTimeMillis(), TimeUnit.MILLISECONDS)); + builder.humanReadableField( + ShardFollowStats.TOTAL_READ_REMOTE_EXEC_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_remote_exec_time", + new TimeValue(stats.getTotalReadRemoteExecTimeMillis(), TimeUnit.MILLISECONDS)); + builder.field(ShardFollowStats.SUCCESSFUL_READ_REQUESTS_FIELD.getPreferredName(), + stats.getSuccessfulReadRequests()); + builder.field(ShardFollowStats.FAILED_READ_REQUESTS_FIELD.getPreferredName(), + stats.getFailedReadRequests()); + builder.field(ShardFollowStats.OPERATIONS_READ_FIELD.getPreferredName(), stats.getOperationsReads()); + builder.humanReadableField( + ShardFollowStats.BYTES_READ.getPreferredName(), + "total_read", + new ByteSizeValue(stats.getBytesRead(), ByteSizeUnit.BYTES)); + builder.humanReadableField( + ShardFollowStats.TOTAL_WRITE_TIME_MILLIS_FIELD.getPreferredName(), + "total_write_time", + new TimeValue(stats.getTotalWriteTimeMillis(), TimeUnit.MILLISECONDS)); + builder.field(ShardFollowStats.SUCCESSFUL_WRITE_REQUESTS_FIELD.getPreferredName(), + stats.getSuccessfulWriteRequests()); + builder.field(ShardFollowStats.FAILED_WRITE_REQUEST_FIELD.getPreferredName(), + stats.getFailedWriteRequests()); + builder.field(ShardFollowStats.OPERATIONS_WRITTEN.getPreferredName(), stats.getOperationWritten()); + builder.startArray(ShardFollowStats.READ_EXCEPTIONS.getPreferredName()); + { + for (final Map.Entry> entry : + stats.getReadExceptions().entrySet()) { + builder.startObject(); + { + builder.field(ShardFollowStats.READ_EXCEPTIONS_ENTRY_FROM_SEQ_NO.getPreferredName(), + entry.getKey()); + builder.field(ShardFollowStats.READ_EXCEPTIONS_RETRIES.getPreferredName(), + entry.getValue().v1()); + builder.field(ShardFollowStats.READ_EXCEPTIONS_ENTRY_EXCEPTION.getPreferredName()); + builder.startObject(); + { + ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, + entry.getValue().v2()); + } + builder.endObject(); + } + builder.endObject(); + } + } + builder.endArray(); + builder.humanReadableField( + ShardFollowStats.TIME_SINCE_LAST_READ_MILLIS_FIELD.getPreferredName(), + "time_since_last_read", + new TimeValue(stats.getTimeSinceLastReadMillis(), TimeUnit.MILLISECONDS)); + if (stats.getFatalException() != null) { + builder.field(ShardFollowStats.FATAL_EXCEPTION.getPreferredName()); + builder.startObject(); + { + ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, + stats.getFatalException()); + } + builder.endObject(); + } + } + builder.endObject(); + } + } + builder.endArray(); + } + builder.endObject(); + } + builder.endArray(); + } + builder.endObject(); + } + builder.endObject(); + } + + private static CcrStatsResponse createTestInstance() { + return new CcrStatsResponse(randomAutoFollowStats(), randomIndicesFollowStats()); + } + + private static AutoFollowStats randomAutoFollowStats() { + final int count = randomIntBetween(0, 16); + final NavigableMap> readExceptions = new TreeMap<>(); + for (int i = 0; i < count; i++) { + readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(), + 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, + autoFollowClusters + ); + } + + static IndicesFollowStats randomIndicesFollowStats() { + int numIndices = randomIntBetween(0, 16); + NavigableMap> shardFollowStats = new TreeMap<>(); + for (int i = 0; i < numIndices; i++) { + String index = randomAlphaOfLength(4); + int numShards = randomIntBetween(0, 5); + List stats = new ArrayList<>(numShards); + shardFollowStats.put(index, stats); + for (int j = 0; j < numShards; j++) { + final int count = randomIntBetween(0, 16); + final NavigableMap> readExceptions = new TreeMap<>(); + for (long k = 0; k < count; k++) { + readExceptions.put(k, new Tuple<>(randomIntBetween(0, Integer.MAX_VALUE), + new ElasticsearchException(new IllegalStateException("index [" + k + "]")))); + } + + stats.add(new ShardFollowStats( + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomInt(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomLong(), + readExceptions, + randomBoolean() ? new ElasticsearchException("fatal error") : null)); + } + } + return new IndicesFollowStats(shardFollowStats); + } + } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java index 2c5bfba5025..5cd327495dc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java @@ -19,89 +19,59 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.client.AbstractResponseTestCase; -import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction; -import org.elasticsearch.xpack.core.ccr.action.FollowParameters; +import org.elasticsearch.client.ccr.FollowInfoResponse.FollowerInfo; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Locale; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; -public class FollowInfoResponseTests extends AbstractResponseTestCase { +public class FollowInfoResponseTests extends ESTestCase { - @Override - protected FollowInfoAction.Response createServerTestInstance() { - int numInfos = randomIntBetween(0, 32); - List infos = new ArrayList<>(numInfos); + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + FollowInfoResponseTests::createTestInstance, + FollowInfoResponseTests::toXContent, + FollowInfoResponse::fromXContent) + .supportsUnknownFields(true) + .test(); + } + + private static void toXContent(FollowInfoResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + builder.startArray(FollowInfoResponse.FOLLOWER_INDICES_FIELD.getPreferredName()); + for (FollowerInfo info : response.getInfos()) { + builder.startObject(); + builder.field(FollowerInfo.FOLLOWER_INDEX_FIELD.getPreferredName(), info.getFollowerIndex()); + builder.field(FollowerInfo.REMOTE_CLUSTER_FIELD.getPreferredName(), info.getRemoteCluster()); + builder.field(FollowerInfo.LEADER_INDEX_FIELD.getPreferredName(), info.getLeaderIndex()); + builder.field(FollowerInfo.STATUS_FIELD.getPreferredName(), info.getStatus().getName()); + if (info.getParameters() != null) { + builder.startObject(FollowerInfo.PARAMETERS_FIELD.getPreferredName()); + { + info.getParameters().toXContentFragment(builder, ToXContent.EMPTY_PARAMS); + } + builder.endObject(); + } + builder.endObject(); + } + builder.endArray(); + builder.endObject(); + } + + private static FollowInfoResponse createTestInstance() { + int numInfos = randomIntBetween(0, 64); + List infos = new ArrayList<>(numInfos); for (int i = 0; i < numInfos; i++) { - FollowParameters followParameters = null; - if (randomBoolean()) { - followParameters = randomFollowParameters(); - } - - infos.add(new FollowInfoAction.Response.FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), - randomFrom(FollowInfoAction.Response.Status.values()), followParameters)); - } - return new FollowInfoAction.Response(infos); - } - - static FollowParameters randomFollowParameters() { - FollowParameters followParameters = new FollowParameters(); - followParameters.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE)); - followParameters.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE)); - followParameters.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE)); - followParameters.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE)); - followParameters.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong())); - followParameters.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong())); - followParameters.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE)); - followParameters.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong())); - followParameters.setMaxRetryDelay(new TimeValue(randomNonNegativeLong())); - followParameters.setReadPollTimeout(new TimeValue(randomNonNegativeLong())); - return followParameters; - } - - @Override - protected FollowInfoResponse doParseToClientInstance(XContentParser parser) throws IOException { - return FollowInfoResponse.fromXContent(parser); - } - - @Override - protected void assertInstances(FollowInfoAction.Response serverTestInstance, FollowInfoResponse clientInstance) { - assertThat(serverTestInstance.getFollowInfos().size(), equalTo(clientInstance.getInfos().size())); - for (int i = 0; i < serverTestInstance.getFollowInfos().size(); i++) { - FollowInfoAction.Response.FollowerInfo serverFollowInfo = serverTestInstance.getFollowInfos().get(i); - FollowInfoResponse.FollowerInfo clientFollowerInfo = clientInstance.getInfos().get(i); - - assertThat(serverFollowInfo.getRemoteCluster(), equalTo(clientFollowerInfo.getRemoteCluster())); - assertThat(serverFollowInfo.getLeaderIndex(), equalTo(clientFollowerInfo.getLeaderIndex())); - assertThat(serverFollowInfo.getFollowerIndex(), equalTo(clientFollowerInfo.getFollowerIndex())); - assertThat(serverFollowInfo.getStatus().toString().toLowerCase(Locale.ROOT), - equalTo(clientFollowerInfo.getStatus().getName().toLowerCase(Locale.ROOT))); - - FollowParameters serverParams = serverFollowInfo.getParameters(); - FollowConfig clientParams = clientFollowerInfo.getParameters(); - if (serverParams != null) { - assertThat(serverParams.getMaxReadRequestOperationCount(), equalTo(clientParams.getMaxReadRequestOperationCount())); - assertThat(serverParams.getMaxWriteRequestOperationCount(), equalTo(clientParams.getMaxWriteRequestOperationCount())); - assertThat(serverParams.getMaxOutstandingReadRequests(), equalTo(clientParams.getMaxOutstandingReadRequests())); - assertThat(serverParams.getMaxOutstandingWriteRequests(), equalTo(clientParams.getMaxOutstandingWriteRequests())); - assertThat(serverParams.getMaxReadRequestSize(), equalTo(clientParams.getMaxReadRequestSize())); - assertThat(serverParams.getMaxWriteRequestSize(), equalTo(clientParams.getMaxWriteRequestSize())); - assertThat(serverParams.getMaxWriteBufferCount(), equalTo(clientParams.getMaxWriteBufferCount())); - assertThat(serverParams.getMaxWriteBufferSize(), equalTo(clientParams.getMaxWriteBufferSize())); - assertThat(serverParams.getMaxRetryDelay(), equalTo(clientParams.getMaxRetryDelay())); - assertThat(serverParams.getReadPollTimeout(), equalTo(clientParams.getReadPollTimeout())); - } else { - assertThat(clientParams, nullValue()); - } + FollowInfoResponse.Status status = randomFrom(FollowInfoResponse.Status.values()); + FollowConfig followConfig = randomBoolean() ? FollowConfigTests.createTestInstance() : null; + infos.add(new FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), status, followConfig)); } + return new FollowInfoResponse(infos); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java index cd7257342c7..5ec3cb4edcf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java @@ -20,115 +20,234 @@ package org.elasticsearch.client.ccr; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats; import org.elasticsearch.common.collect.Tuple; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; -import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; +import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.TreeMap; +import java.util.concurrent.TimeUnit; -import static org.elasticsearch.client.ccr.CcrStatsResponseTests.createStatsResponse; +import static org.elasticsearch.client.ccr.CcrStatsResponseTests.randomIndicesFollowStats; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; -public class FollowStatsResponseTests extends AbstractResponseTestCase { +public class FollowStatsResponseTests extends ESTestCase { - @Override - protected FollowStatsAction.StatsResponses createServerTestInstance() { - return createStatsResponse(); + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + FollowStatsResponseTests::createTestInstance, + FollowStatsResponseTests::toXContent, + FollowStatsResponse::fromXContent) + .supportsUnknownFields(true) + .assertEqualsConsumer(FollowStatsResponseTests::assertEqualInstances) + .assertToXContentEquivalence(false) + .test(); } - @Override - protected FollowStatsResponse doParseToClientInstance(XContentParser parser) throws IOException { - return FollowStatsResponse.fromXContent(parser); - } + // Needed, because exceptions in IndicesFollowStats cannot be compared + private static void assertEqualInstances(FollowStatsResponse expectedInstance, FollowStatsResponse newInstance) { + assertNotSame(expectedInstance, newInstance); + { + IndicesFollowStats newIndicesFollowStats = newInstance.getIndicesFollowStats(); + IndicesFollowStats expectedIndicesFollowStats = expectedInstance.getIndicesFollowStats(); + assertThat(newIndicesFollowStats.getShardFollowStats().size(), + equalTo(expectedIndicesFollowStats.getShardFollowStats().size())); + assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), + equalTo(expectedIndicesFollowStats.getShardFollowStats().keySet())); + for (Map.Entry> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { + List newStats = indexEntry.getValue(); + List expectedStats = expectedIndicesFollowStats.getShardFollowStats(indexEntry.getKey()); + assertThat(newStats.size(), equalTo(expectedStats.size())); + for (int i = 0; i < newStats.size(); i++) { + ShardFollowStats actualShardFollowStats = newStats.get(i); + ShardFollowStats expectedShardFollowStats = expectedStats.get(i); - @Override - protected void assertInstances(FollowStatsAction.StatsResponses serverTestInstance, FollowStatsResponse clientInstance) { - IndicesFollowStats newIndicesFollowStats = clientInstance.getIndicesFollowStats(); - - // sort by index name, then shard ID - final Map> expectedIndicesFollowStats = new TreeMap<>(); - for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getStatsResponses()) { - expectedIndicesFollowStats.computeIfAbsent( - statsResponse.status().followerIndex(), - k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); - } - assertThat(newIndicesFollowStats.getShardFollowStats().size(), - equalTo(expectedIndicesFollowStats.size())); - assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), - equalTo(expectedIndicesFollowStats.keySet())); - for (Map.Entry> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { - List newStats = indexEntry.getValue(); - Map expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); - assertThat(newStats.size(), equalTo(expectedStats.size())); - for (int i = 0; i < newStats.size(); i++) { - ShardFollowStats actualShardFollowStats = newStats.get(i); - ShardFollowNodeTaskStatus expectedShardFollowStats = expectedStats.get(actualShardFollowStats.getShardId()).status(); - - assertThat(actualShardFollowStats.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster())); - assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); - assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); - assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); - assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), - equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); - assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); - assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), - equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); - assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); - assertThat(actualShardFollowStats.getOutstandingReadRequests(), - equalTo(expectedShardFollowStats.outstandingReadRequests())); - assertThat(actualShardFollowStats.getOutstandingWriteRequests(), - equalTo(expectedShardFollowStats.outstandingWriteRequests())); - assertThat(actualShardFollowStats.getWriteBufferOperationCount(), - equalTo(expectedShardFollowStats.writeBufferOperationCount())); - assertThat(actualShardFollowStats.getFollowerMappingVersion(), - equalTo(expectedShardFollowStats.followerMappingVersion())); - assertThat(actualShardFollowStats.getFollowerSettingsVersion(), - equalTo(expectedShardFollowStats.followerSettingsVersion())); - assertThat(actualShardFollowStats.getTotalReadTimeMillis(), - equalTo(expectedShardFollowStats.totalReadTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulReadRequests(), - equalTo(expectedShardFollowStats.successfulReadRequests())); - assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); - assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); - assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); - assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), - equalTo(expectedShardFollowStats.totalWriteTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), - equalTo(expectedShardFollowStats.successfulWriteRequests())); - assertThat(actualShardFollowStats.getFailedWriteRequests(), - equalTo(expectedShardFollowStats.failedWriteRequests())); - assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.operationWritten())); - assertThat(actualShardFollowStats.getReadExceptions().size(), - equalTo(expectedShardFollowStats.readExceptions().size())); - assertThat(actualShardFollowStats.getReadExceptions().keySet(), - equalTo(expectedShardFollowStats.readExceptions().keySet())); - for (final Map.Entry> entry : - actualShardFollowStats.getReadExceptions().entrySet()) { - final Tuple expectedTuple = - expectedShardFollowStats.readExceptions().get(entry.getKey()); - assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); - // x-content loses the exception - final ElasticsearchException expected = expectedTuple.v2(); - assertThat(entry.getValue().v2().getMessage(), containsString(expected.getMessage())); - assertNotNull(entry.getValue().v2().getCause()); - assertThat( - entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); - assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); + assertThat(actualShardFollowStats.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster())); + assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.getLeaderIndex())); + assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.getFollowerIndex())); + assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); + assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), + equalTo(expectedShardFollowStats.getLeaderGlobalCheckpoint())); + assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.getLeaderMaxSeqNo())); + assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), + equalTo(expectedShardFollowStats.getFollowerGlobalCheckpoint())); + assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.getLastRequestedSeqNo())); + assertThat(actualShardFollowStats.getOutstandingReadRequests(), + equalTo(expectedShardFollowStats.getOutstandingReadRequests())); + assertThat(actualShardFollowStats.getOutstandingWriteRequests(), + equalTo(expectedShardFollowStats.getOutstandingWriteRequests())); + assertThat(actualShardFollowStats.getWriteBufferOperationCount(), + equalTo(expectedShardFollowStats.getWriteBufferOperationCount())); + assertThat(actualShardFollowStats.getFollowerMappingVersion(), + equalTo(expectedShardFollowStats.getFollowerMappingVersion())); + assertThat(actualShardFollowStats.getFollowerSettingsVersion(), + equalTo(expectedShardFollowStats.getFollowerSettingsVersion())); + assertThat(actualShardFollowStats.getTotalReadTimeMillis(), + equalTo(expectedShardFollowStats.getTotalReadTimeMillis())); + assertThat(actualShardFollowStats.getSuccessfulReadRequests(), + equalTo(expectedShardFollowStats.getSuccessfulReadRequests())); + assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.getFailedReadRequests())); + assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.getOperationsReads())); + assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.getBytesRead())); + assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), + equalTo(expectedShardFollowStats.getTotalWriteTimeMillis())); + assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), + equalTo(expectedShardFollowStats.getSuccessfulWriteRequests())); + assertThat(actualShardFollowStats.getFailedWriteRequests(), + equalTo(expectedShardFollowStats.getFailedWriteRequests())); + assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.getOperationWritten())); + assertThat(actualShardFollowStats.getReadExceptions().size(), + equalTo(expectedShardFollowStats.getReadExceptions().size())); + assertThat(actualShardFollowStats.getReadExceptions().keySet(), + equalTo(expectedShardFollowStats.getReadExceptions().keySet())); + for (final Map.Entry> entry : + actualShardFollowStats.getReadExceptions().entrySet()) { + final Tuple expectedTuple = + expectedShardFollowStats.getReadExceptions().get(entry.getKey()); + assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); + // x-content loses the exception + final ElasticsearchException expected = expectedTuple.v2(); + assertThat(entry.getValue().v2().getMessage(), containsString(expected.getMessage())); + assertNotNull(entry.getValue().v2().getCause()); + assertThat( + entry.getValue().v2().getCause(), + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); + } + assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(), + equalTo(expectedShardFollowStats.getTimeSinceLastReadMillis())); } - assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(), - equalTo(expectedShardFollowStats.timeSinceLastReadMillis())); } } } + private static void toXContent(FollowStatsResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + { + builder.startArray(IndicesFollowStats.INDICES_FIELD.getPreferredName()); + for (Map.Entry> indexEntry : + response.getIndicesFollowStats().getShardFollowStats().entrySet()) { + builder.startObject(); + { + builder.field(IndicesFollowStats.INDEX_FIELD.getPreferredName(), indexEntry.getKey()); + builder.startArray(IndicesFollowStats.SHARDS_FIELD.getPreferredName()); + { + for (ShardFollowStats stats : indexEntry.getValue()) { + builder.startObject(); + { + builder.field(ShardFollowStats.LEADER_CLUSTER.getPreferredName(), stats.getRemoteCluster()); + builder.field(ShardFollowStats.LEADER_INDEX.getPreferredName(), stats.getLeaderIndex()); + builder.field(ShardFollowStats.FOLLOWER_INDEX.getPreferredName(), stats.getFollowerIndex()); + builder.field(ShardFollowStats.SHARD_ID.getPreferredName(), stats.getShardId()); + builder.field(ShardFollowStats.LEADER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), + stats.getLeaderGlobalCheckpoint()); + builder.field(ShardFollowStats.LEADER_MAX_SEQ_NO_FIELD.getPreferredName(), stats.getLeaderMaxSeqNo()); + builder.field(ShardFollowStats.FOLLOWER_GLOBAL_CHECKPOINT_FIELD.getPreferredName(), + stats.getFollowerGlobalCheckpoint()); + builder.field(ShardFollowStats.FOLLOWER_MAX_SEQ_NO_FIELD.getPreferredName(), + stats.getFollowerMaxSeqNo()); + builder.field(ShardFollowStats.LAST_REQUESTED_SEQ_NO_FIELD.getPreferredName(), + stats.getLastRequestedSeqNo()); + builder.field(ShardFollowStats.OUTSTANDING_READ_REQUESTS.getPreferredName(), + stats.getOutstandingReadRequests()); + builder.field(ShardFollowStats.OUTSTANDING_WRITE_REQUESTS.getPreferredName(), + stats.getOutstandingWriteRequests()); + builder.field(ShardFollowStats.WRITE_BUFFER_OPERATION_COUNT_FIELD.getPreferredName(), + stats.getWriteBufferOperationCount()); + builder.humanReadableField( + ShardFollowStats.WRITE_BUFFER_SIZE_IN_BYTES_FIELD.getPreferredName(), + "write_buffer_size", + new ByteSizeValue(stats.getWriteBufferSizeInBytes())); + builder.field(ShardFollowStats.FOLLOWER_MAPPING_VERSION_FIELD.getPreferredName(), + stats.getFollowerMappingVersion()); + builder.field(ShardFollowStats.FOLLOWER_SETTINGS_VERSION_FIELD.getPreferredName(), + stats.getFollowerSettingsVersion()); + builder.humanReadableField( + ShardFollowStats.TOTAL_READ_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_time", + new TimeValue(stats.getTotalReadTimeMillis(), TimeUnit.MILLISECONDS)); + builder.humanReadableField( + ShardFollowStats.TOTAL_READ_REMOTE_EXEC_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_remote_exec_time", + new TimeValue(stats.getTotalReadRemoteExecTimeMillis(), TimeUnit.MILLISECONDS)); + builder.field(ShardFollowStats.SUCCESSFUL_READ_REQUESTS_FIELD.getPreferredName(), + stats.getSuccessfulReadRequests()); + builder.field(ShardFollowStats.FAILED_READ_REQUESTS_FIELD.getPreferredName(), + stats.getFailedReadRequests()); + builder.field(ShardFollowStats.OPERATIONS_READ_FIELD.getPreferredName(), stats.getOperationsReads()); + builder.humanReadableField( + ShardFollowStats.BYTES_READ.getPreferredName(), + "total_read", + new ByteSizeValue(stats.getBytesRead(), ByteSizeUnit.BYTES)); + builder.humanReadableField( + ShardFollowStats.TOTAL_WRITE_TIME_MILLIS_FIELD.getPreferredName(), + "total_write_time", + new TimeValue(stats.getTotalWriteTimeMillis(), TimeUnit.MILLISECONDS)); + builder.field(ShardFollowStats.SUCCESSFUL_WRITE_REQUESTS_FIELD.getPreferredName(), + stats.getSuccessfulWriteRequests()); + builder.field(ShardFollowStats.FAILED_WRITE_REQUEST_FIELD.getPreferredName(), + stats.getFailedWriteRequests()); + builder.field(ShardFollowStats.OPERATIONS_WRITTEN.getPreferredName(), stats.getOperationWritten()); + builder.startArray(ShardFollowStats.READ_EXCEPTIONS.getPreferredName()); + { + for (final Map.Entry> entry : + stats.getReadExceptions().entrySet()) { + builder.startObject(); + { + builder.field(ShardFollowStats.READ_EXCEPTIONS_ENTRY_FROM_SEQ_NO.getPreferredName(), + entry.getKey()); + builder.field(ShardFollowStats.READ_EXCEPTIONS_RETRIES.getPreferredName(), + entry.getValue().v1()); + builder.field(ShardFollowStats.READ_EXCEPTIONS_ENTRY_EXCEPTION.getPreferredName()); + builder.startObject(); + { + ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, + entry.getValue().v2()); + } + builder.endObject(); + } + builder.endObject(); + } + } + builder.endArray(); + builder.humanReadableField( + ShardFollowStats.TIME_SINCE_LAST_READ_MILLIS_FIELD.getPreferredName(), + "time_since_last_read", + new TimeValue(stats.getTimeSinceLastReadMillis(), TimeUnit.MILLISECONDS)); + if (stats.getFatalException() != null) { + builder.field(ShardFollowStats.FATAL_EXCEPTION.getPreferredName()); + builder.startObject(); + { + ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, + stats.getFatalException()); + } + builder.endObject(); + } + } + builder.endObject(); + } + } + builder.endArray(); + } + builder.endObject(); + } + builder.endArray(); + } + builder.endObject(); + } + + private static FollowStatsResponse createTestInstance() { + return new FollowStatsResponse(randomIndicesFollowStats()); + } + } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java index 65ef3aa062d..f6f0f1747e2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java @@ -19,111 +19,99 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; -import org.elasticsearch.xpack.core.ccr.action.GetAutoFollowPatternAction; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Collections; -import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; +import static org.elasticsearch.client.ccr.PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD; +import static org.elasticsearch.client.ccr.PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD; +import static org.elasticsearch.client.ccr.PutFollowRequest.REMOTE_CLUSTER_FIELD; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; -public class GetAutoFollowPatternResponseTests extends AbstractResponseTestCase< - GetAutoFollowPatternAction.Response, - GetAutoFollowPatternResponse> { +public class GetAutoFollowPatternResponseTests extends ESTestCase { - @Override - protected GetAutoFollowPatternAction.Response createServerTestInstance() { + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + this::createTestInstance, + GetAutoFollowPatternResponseTests::toXContent, + GetAutoFollowPatternResponse::fromXContent) + .supportsUnknownFields(true) + .test(); + } + + private GetAutoFollowPatternResponse createTestInstance() { int numPatterns = randomIntBetween(0, 16); - NavigableMap patterns = new TreeMap<>(); + NavigableMap patterns = new TreeMap<>(); for (int i = 0; i < numPatterns; i++) { - String remoteCluster = randomAlphaOfLength(4); - List leaderIndexPatters = Collections.singletonList(randomAlphaOfLength(4)); - String followIndexNamePattern = randomAlphaOfLength(4); - - Integer maxOutstandingReadRequests = null; + GetAutoFollowPatternResponse.Pattern pattern = new GetAutoFollowPatternResponse.Pattern( + randomAlphaOfLength(4), Collections.singletonList(randomAlphaOfLength(4)), randomAlphaOfLength(4)); if (randomBoolean()) { - maxOutstandingReadRequests = randomIntBetween(0, Integer.MAX_VALUE); + pattern.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE)); } - Integer maxOutstandingWriteRequests = null; if (randomBoolean()) { - maxOutstandingWriteRequests = randomIntBetween(0, Integer.MAX_VALUE); + pattern.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE)); } - Integer maxReadRequestOperationCount = null; if (randomBoolean()) { - maxReadRequestOperationCount = randomIntBetween(0, Integer.MAX_VALUE); + pattern.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE)); } - ByteSizeValue maxReadRequestSize = null; if (randomBoolean()) { - maxReadRequestSize = new ByteSizeValue(randomNonNegativeLong()); + pattern.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong())); } - Integer maxWriteBufferCount = null; if (randomBoolean()) { - maxWriteBufferCount = randomIntBetween(0, Integer.MAX_VALUE); + pattern.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE)); } - ByteSizeValue maxWriteBufferSize = null; if (randomBoolean()) { - maxWriteBufferSize = new ByteSizeValue(randomNonNegativeLong()); + pattern.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong())); } - Integer maxWriteRequestOperationCount = null; if (randomBoolean()) { - maxWriteRequestOperationCount = randomIntBetween(0, Integer.MAX_VALUE); + pattern.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE)); } - ByteSizeValue maxWriteRequestSize = null; if (randomBoolean()) { - maxWriteRequestSize = new ByteSizeValue(randomNonNegativeLong()); + pattern.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong())); } - TimeValue maxRetryDelay = null; if (randomBoolean()) { - maxRetryDelay = new TimeValue(randomNonNegativeLong()); + pattern.setMaxRetryDelay(new TimeValue(randomNonNegativeLong())); } - TimeValue readPollTimeout = null; if (randomBoolean()) { - readPollTimeout = new TimeValue(randomNonNegativeLong()); + pattern.setReadPollTimeout(new TimeValue(randomNonNegativeLong())); } - patterns.put(randomAlphaOfLength(4), new AutoFollowMetadata.AutoFollowPattern(remoteCluster, leaderIndexPatters, - followIndexNamePattern, maxReadRequestOperationCount, maxWriteRequestOperationCount, maxOutstandingReadRequests, - maxOutstandingWriteRequests, maxReadRequestSize, maxWriteRequestSize, maxWriteBufferCount, maxWriteBufferSize, - maxRetryDelay, readPollTimeout)); + patterns.put(randomAlphaOfLength(4), pattern); } - return new GetAutoFollowPatternAction.Response(patterns); + return new GetAutoFollowPatternResponse(patterns); } - @Override - protected GetAutoFollowPatternResponse doParseToClientInstance(XContentParser parser) throws IOException { - return GetAutoFollowPatternResponse.fromXContent(parser); - } - - @Override - protected void assertInstances(GetAutoFollowPatternAction.Response serverTestInstance, GetAutoFollowPatternResponse clientInstance) { - assertThat(serverTestInstance.getAutoFollowPatterns().size(), equalTo(clientInstance.getPatterns().size())); - for (Map.Entry entry : serverTestInstance.getAutoFollowPatterns().entrySet()) { - AutoFollowMetadata.AutoFollowPattern serverPattern = entry.getValue(); - GetAutoFollowPatternResponse.Pattern clientPattern = clientInstance.getPatterns().get(entry.getKey()); - assertThat(clientPattern, notNullValue()); - - assertThat(serverPattern.getRemoteCluster(), equalTo(clientPattern.getRemoteCluster())); - assertThat(serverPattern.getLeaderIndexPatterns(), equalTo(clientPattern.getLeaderIndexPatterns())); - assertThat(serverPattern.getFollowIndexPattern(), equalTo(clientPattern.getFollowIndexNamePattern())); - assertThat(serverPattern.getMaxOutstandingReadRequests(), equalTo(clientPattern.getMaxOutstandingReadRequests())); - assertThat(serverPattern.getMaxOutstandingWriteRequests(), equalTo(clientPattern.getMaxOutstandingWriteRequests())); - assertThat(serverPattern.getMaxReadRequestOperationCount(), equalTo(clientPattern.getMaxReadRequestOperationCount())); - assertThat(serverPattern.getMaxWriteRequestOperationCount(), equalTo(clientPattern.getMaxWriteRequestOperationCount())); - assertThat(serverPattern.getMaxReadRequestSize(), equalTo(clientPattern.getMaxReadRequestSize())); - assertThat(serverPattern.getMaxWriteRequestSize(), equalTo(clientPattern.getMaxWriteRequestSize())); - assertThat(serverPattern.getMaxWriteBufferCount(), equalTo(clientPattern.getMaxWriteBufferCount())); - assertThat(serverPattern.getMaxWriteBufferSize(), equalTo(clientPattern.getMaxWriteBufferSize())); - assertThat(serverPattern.getMaxRetryDelay(), equalTo(clientPattern.getMaxRetryDelay())); - assertThat(serverPattern.getReadPollTimeout(), equalTo(clientPattern.getReadPollTimeout())); + public static void toXContent(GetAutoFollowPatternResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + { + builder.startArray(GetAutoFollowPatternResponse.PATTERNS_FIELD.getPreferredName()); + for (Map.Entry entry : response.getPatterns().entrySet()) { + builder.startObject(); + { + builder.field(GetAutoFollowPatternResponse.NAME_FIELD.getPreferredName(), entry.getKey()); + builder.startObject(GetAutoFollowPatternResponse.PATTERN_FIELD.getPreferredName()); + { + GetAutoFollowPatternResponse.Pattern pattern = entry.getValue(); + builder.field(REMOTE_CLUSTER_FIELD.getPreferredName(), pattern.getRemoteCluster()); + builder.field(LEADER_PATTERNS_FIELD.getPreferredName(), pattern.getLeaderIndexPatterns()); + if (pattern.getFollowIndexNamePattern()!= null) { + builder.field(FOLLOW_PATTERN_FIELD.getPreferredName(), pattern.getFollowIndexNamePattern()); + } + entry.getValue().toXContentFragment(builder, ToXContent.EMPTY_PARAMS); + } + builder.endObject(); + } + builder.endObject(); + } + builder.endArray(); } + builder.endObject(); } - } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowResponseTests.java index 52fe70b3a39..00bcf535f08 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowResponseTests.java @@ -19,30 +19,35 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.client.AbstractResponseTestCase; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; -import static org.hamcrest.Matchers.is; +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; -public class PutFollowResponseTests extends AbstractResponseTestCase { +public class PutFollowResponseTests extends ESTestCase { - @Override - protected PutFollowAction.Response createServerTestInstance() { - return new PutFollowAction.Response(randomBoolean(), randomBoolean(), randomBoolean()); + public void testFromXContent() throws IOException { + xContentTester(this::createParser, + this::createTestInstance, + PutFollowResponseTests::toXContent, + PutFollowResponse::fromXContent) + .supportsUnknownFields(true) + .test(); } - @Override - protected PutFollowResponse doParseToClientInstance(XContentParser parser) throws IOException { - return PutFollowResponse.fromXContent(parser); + private PutFollowResponse createTestInstance() { + return new PutFollowResponse(randomBoolean(), randomBoolean(), randomBoolean()); } - @Override - protected void assertInstances(PutFollowAction.Response serverTestInstance, PutFollowResponse clientInstance) { - assertThat(serverTestInstance.isFollowIndexCreated(), is(clientInstance.isFollowIndexCreated())); - assertThat(serverTestInstance.isFollowIndexShardsAcked(), is(clientInstance.isFollowIndexShardsAcked())); - assertThat(serverTestInstance.isIndexFollowingStarted(), is(clientInstance.isIndexFollowingStarted())); + public static void toXContent(PutFollowResponse response, XContentBuilder builder) throws IOException { + builder.startObject(); + { + builder.field(PutFollowResponse.FOLLOW_INDEX_CREATED.getPreferredName(), response.isFollowIndexCreated()); + builder.field(PutFollowResponse.FOLLOW_INDEX_SHARDS_ACKED.getPreferredName(), response.isFollowIndexShardsAcked()); + builder.field(PutFollowResponse.INDEX_FOLLOWING_STARTED.getPreferredName(), response.isIndexFollowingStarted()); + } + builder.endObject(); } }