Revert "Change HLRC CCR response tests to use AbstractResponseTestCase base class. (#40257)"

This reverts commit c29027d99e.
This commit is contained in:
Martijn van Groningen 2019-04-08 11:24:06 +02:00
parent ddf17dfb1e
commit 84a410d5a8
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
6 changed files with 621 additions and 373 deletions

View File

@ -64,9 +64,6 @@ dependencies {
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}" testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
//this is needed to make RestHighLevelClientTests#testApiNamingConventions work from IDEs //this is needed to make RestHighLevelClientTests#testApiNamingConventions work from IDEs
testCompile "org.elasticsearch:rest-api-spec:${version}" 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}" restSpec "org.elasticsearch:rest-api-spec:${version}"
} }

View File

@ -20,110 +20,50 @@
package org.elasticsearch.client.ccr; package org.elasticsearch.client.ccr;
import org.elasticsearch.ElasticsearchException; 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.client.ccr.IndicesFollowStats.ShardFollowStats;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NavigableMap; import java.util.NavigableMap;
import java.util.TreeMap; 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.anyOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
public class CcrStatsResponseTests extends AbstractResponseTestCase<CcrStatsAction.Response, CcrStatsResponse> { public class CcrStatsResponseTests extends ESTestCase {
@Override public void testFromXContent() throws IOException {
protected CcrStatsAction.Response createServerTestInstance() { xContentTester(this::createParser,
org.elasticsearch.xpack.core.ccr.AutoFollowStats autoFollowStats = new org.elasticsearch.xpack.core.ccr.AutoFollowStats( CcrStatsResponseTests::createTestInstance,
randomNonNegativeLong(), CcrStatsResponseTests::toXContent,
randomNonNegativeLong(), CcrStatsResponse::fromXContent)
randomNonNegativeLong(), .supportsUnknownFields(true)
randomReadExceptions(), .assertEqualsConsumer(CcrStatsResponseTests::assertEqualInstances)
randomTrackingClusters() .assertToXContentEquivalence(false)
); .test();
FollowStatsAction.StatsResponses statsResponse = createStatsResponse();
return new CcrStatsAction.Response(autoFollowStats, statsResponse);
} }
static NavigableMap<String, Tuple<Long, ElasticsearchException>> randomReadExceptions() { // Needed, because exceptions in IndicesFollowStats and AutoFollowStats cannot be compared
final int count = randomIntBetween(0, 16); private static void assertEqualInstances(CcrStatsResponse expectedInstance, CcrStatsResponse newInstance) {
final NavigableMap<String, Tuple<Long, ElasticsearchException>> readExceptions = new TreeMap<>(); assertNotSame(expectedInstance, newInstance);
for (int i = 0; i < count; i++) {
readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(),
new ElasticsearchException(new IllegalStateException("index [" + i + "]"))));
}
return readExceptions;
}
static NavigableMap<String, org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster> randomTrackingClusters() {
final int count = randomIntBetween(0, 16);
final NavigableMap<String, org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster> 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<FollowStatsAction.StatsResponse> 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(); AutoFollowStats newAutoFollowStats = newInstance.getAutoFollowStats();
org.elasticsearch.xpack.core.ccr.AutoFollowStats expectedAutoFollowStats = serverTestInstance.getAutoFollowStats(); AutoFollowStats expectedAutoFollowStats = expectedInstance.getAutoFollowStats();
assertThat(newAutoFollowStats.getNumberOfSuccessfulFollowIndices(), assertThat(newAutoFollowStats.getNumberOfSuccessfulFollowIndices(),
equalTo(expectedAutoFollowStats.getNumberOfSuccessfulFollowIndices())); equalTo(expectedAutoFollowStats.getNumberOfSuccessfulFollowIndices()));
assertThat(newAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), assertThat(newAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests(),
@ -149,69 +89,62 @@ public class CcrStatsResponseTests extends AbstractResponseTestCase<CcrStatsActi
} }
} }
{ {
IndicesFollowStats newIndicesFollowStats = clientInstance.getIndicesFollowStats(); IndicesFollowStats newIndicesFollowStats = newInstance.getIndicesFollowStats();
IndicesFollowStats expectedIndicesFollowStats = expectedInstance.getIndicesFollowStats();
// sort by index name, then shard ID
final Map<String, Map<Integer, FollowStatsAction.StatsResponse>> expectedIndicesFollowStats = new TreeMap<>();
for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getFollowStats().getStatsResponses()) {
expectedIndicesFollowStats.computeIfAbsent(
statsResponse.status().followerIndex(),
k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse);
}
assertThat(newIndicesFollowStats.getShardFollowStats().size(), assertThat(newIndicesFollowStats.getShardFollowStats().size(),
equalTo(expectedIndicesFollowStats.size())); equalTo(expectedIndicesFollowStats.getShardFollowStats().size()));
assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), assertThat(newIndicesFollowStats.getShardFollowStats().keySet(),
equalTo(expectedIndicesFollowStats.keySet())); equalTo(expectedIndicesFollowStats.getShardFollowStats().keySet()));
for (Map.Entry<String, List<ShardFollowStats>> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { for (Map.Entry<String, List<ShardFollowStats>> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) {
List<ShardFollowStats> newStats = indexEntry.getValue(); List<ShardFollowStats> newStats = indexEntry.getValue();
Map<Integer, FollowStatsAction.StatsResponse> expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); List<ShardFollowStats> expectedStats = expectedIndicesFollowStats.getShardFollowStats(indexEntry.getKey());
assertThat(newStats.size(), equalTo(expectedStats.size())); assertThat(newStats.size(), equalTo(expectedStats.size()));
for (int i = 0; i < newStats.size(); i++) { for (int i = 0; i < newStats.size(); i++) {
ShardFollowStats actualShardFollowStats = newStats.get(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.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster()));
assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.getLeaderIndex()));
assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.getFollowerIndex()));
assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId()));
assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(),
equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); equalTo(expectedShardFollowStats.getLeaderGlobalCheckpoint()));
assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.getLeaderMaxSeqNo()));
assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(),
equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); equalTo(expectedShardFollowStats.getFollowerGlobalCheckpoint()));
assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.getLastRequestedSeqNo()));
assertThat(actualShardFollowStats.getOutstandingReadRequests(), assertThat(actualShardFollowStats.getOutstandingReadRequests(),
equalTo(expectedShardFollowStats.outstandingReadRequests())); equalTo(expectedShardFollowStats.getOutstandingReadRequests()));
assertThat(actualShardFollowStats.getOutstandingWriteRequests(), assertThat(actualShardFollowStats.getOutstandingWriteRequests(),
equalTo(expectedShardFollowStats.outstandingWriteRequests())); equalTo(expectedShardFollowStats.getOutstandingWriteRequests()));
assertThat(actualShardFollowStats.getWriteBufferOperationCount(), assertThat(actualShardFollowStats.getWriteBufferOperationCount(),
equalTo(expectedShardFollowStats.writeBufferOperationCount())); equalTo(expectedShardFollowStats.getWriteBufferOperationCount()));
assertThat(actualShardFollowStats.getFollowerMappingVersion(), assertThat(actualShardFollowStats.getFollowerMappingVersion(),
equalTo(expectedShardFollowStats.followerMappingVersion())); equalTo(expectedShardFollowStats.getFollowerMappingVersion()));
assertThat(actualShardFollowStats.getFollowerSettingsVersion(), assertThat(actualShardFollowStats.getFollowerSettingsVersion(),
equalTo(expectedShardFollowStats.followerSettingsVersion())); equalTo(expectedShardFollowStats.getFollowerSettingsVersion()));
assertThat(actualShardFollowStats.getTotalReadTimeMillis(), assertThat(actualShardFollowStats.getTotalReadTimeMillis(),
equalTo(expectedShardFollowStats.totalReadTimeMillis())); equalTo(expectedShardFollowStats.getTotalReadTimeMillis()));
assertThat(actualShardFollowStats.getSuccessfulReadRequests(), assertThat(actualShardFollowStats.getSuccessfulReadRequests(),
equalTo(expectedShardFollowStats.successfulReadRequests())); equalTo(expectedShardFollowStats.getSuccessfulReadRequests()));
assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.getFailedReadRequests()));
assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.getOperationsReads()));
assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.getBytesRead()));
assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), assertThat(actualShardFollowStats.getTotalWriteTimeMillis(),
equalTo(expectedShardFollowStats.totalWriteTimeMillis())); equalTo(expectedShardFollowStats.getTotalWriteTimeMillis()));
assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), assertThat(actualShardFollowStats.getSuccessfulWriteRequests(),
equalTo(expectedShardFollowStats.successfulWriteRequests())); equalTo(expectedShardFollowStats.getSuccessfulWriteRequests()));
assertThat(actualShardFollowStats.getFailedWriteRequests(), assertThat(actualShardFollowStats.getFailedWriteRequests(),
equalTo(expectedShardFollowStats.failedWriteRequests())); equalTo(expectedShardFollowStats.getFailedWriteRequests()));
assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.operationWritten())); assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.getOperationWritten()));
assertThat(actualShardFollowStats.getReadExceptions().size(), assertThat(actualShardFollowStats.getReadExceptions().size(),
equalTo(expectedShardFollowStats.readExceptions().size())); equalTo(expectedShardFollowStats.getReadExceptions().size()));
assertThat(actualShardFollowStats.getReadExceptions().keySet(), assertThat(actualShardFollowStats.getReadExceptions().keySet(),
equalTo(expectedShardFollowStats.readExceptions().keySet())); equalTo(expectedShardFollowStats.getReadExceptions().keySet()));
for (final Map.Entry<Long, Tuple<Integer, ElasticsearchException>> entry : for (final Map.Entry<Long, Tuple<Integer, ElasticsearchException>> entry :
actualShardFollowStats.getReadExceptions().entrySet()) { actualShardFollowStats.getReadExceptions().entrySet()) {
final Tuple<Integer, ElasticsearchException> expectedTuple = final Tuple<Integer, ElasticsearchException> expectedTuple =
expectedShardFollowStats.readExceptions().get(entry.getKey()); expectedShardFollowStats.getReadExceptions().get(entry.getKey());
assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1()));
// x-content loses the exception // x-content loses the exception
final ElasticsearchException expected = expectedTuple.v2(); final ElasticsearchException expected = expectedTuple.v2();
@ -223,10 +156,246 @@ public class CcrStatsResponseTests extends AbstractResponseTestCase<CcrStatsActi
assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage()));
} }
assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(), assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(),
equalTo(expectedShardFollowStats.timeSinceLastReadMillis())); equalTo(expectedShardFollowStats.getTimeSinceLastReadMillis()));
} }
} }
} }
} }
private static void toXContent(CcrStatsResponse response, XContentBuilder builder) throws IOException {
builder.startObject();
{
AutoFollowStats autoFollowStats = response.getAutoFollowStats();
builder.startObject(CcrStatsResponse.AUTO_FOLLOW_STATS_FIELD.getPreferredName());
{
builder.field(AutoFollowStats.NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED.getPreferredName(),
autoFollowStats.getNumberOfSuccessfulFollowIndices());
builder.field(AutoFollowStats.NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS.getPreferredName(),
autoFollowStats.getNumberOfFailedRemoteClusterStateRequests());
builder.field(AutoFollowStats.NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED.getPreferredName(),
autoFollowStats.getNumberOfFailedFollowIndices());
builder.startArray(AutoFollowStats.RECENT_AUTO_FOLLOW_ERRORS.getPreferredName());
for (Map.Entry<String, Tuple<Long, ElasticsearchException>> 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<String, AutoFollowedCluster> 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<String, List<ShardFollowStats>> 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<Long, Tuple<Integer, ElasticsearchException>> 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<String, Tuple<Long, ElasticsearchException>> readExceptions = new TreeMap<>();
for (int i = 0; i < count; i++) {
readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(),
new ElasticsearchException(new IllegalStateException("index [" + i + "]"))));
}
final NavigableMap<String, AutoFollowedCluster> 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<String, List<ShardFollowStats>> shardFollowStats = new TreeMap<>();
for (int i = 0; i < numIndices; i++) {
String index = randomAlphaOfLength(4);
int numShards = randomIntBetween(0, 5);
List<ShardFollowStats> stats = new ArrayList<>(numShards);
shardFollowStats.put(index, stats);
for (int j = 0; j < numShards; j++) {
final int count = randomIntBetween(0, 16);
final NavigableMap<Long, Tuple<Integer, ElasticsearchException>> 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);
}
} }

View File

@ -19,89 +19,59 @@
package org.elasticsearch.client.ccr; package org.elasticsearch.client.ccr;
import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.client.ccr.FollowInfoResponse.FollowerInfo;
import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction;
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import static org.hamcrest.Matchers.equalTo; import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
import static org.hamcrest.Matchers.nullValue;
public class FollowInfoResponseTests extends AbstractResponseTestCase<FollowInfoAction.Response, FollowInfoResponse> { public class FollowInfoResponseTests extends ESTestCase {
@Override public void testFromXContent() throws IOException {
protected FollowInfoAction.Response createServerTestInstance() { xContentTester(this::createParser,
int numInfos = randomIntBetween(0, 32); FollowInfoResponseTests::createTestInstance,
List<FollowInfoAction.Response.FollowerInfo> infos = new ArrayList<>(numInfos); 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<FollowerInfo> infos = new ArrayList<>(numInfos);
for (int i = 0; i < numInfos; i++) { for (int i = 0; i < numInfos; i++) {
FollowParameters followParameters = null; FollowInfoResponse.Status status = randomFrom(FollowInfoResponse.Status.values());
if (randomBoolean()) { FollowConfig followConfig = randomBoolean() ? FollowConfigTests.createTestInstance() : null;
followParameters = randomFollowParameters(); infos.add(new FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), status, followConfig));
}
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());
}
} }
return new FollowInfoResponse(infos);
} }
} }

View File

@ -20,115 +20,234 @@
package org.elasticsearch.client.ccr; package org.elasticsearch.client.ccr;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.client.AbstractResponseTestCase;
import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats; import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; 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.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; 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.anyOf;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
public class FollowStatsResponseTests extends AbstractResponseTestCase<FollowStatsAction.StatsResponses, FollowStatsResponse> { public class FollowStatsResponseTests extends ESTestCase {
@Override public void testFromXContent() throws IOException {
protected FollowStatsAction.StatsResponses createServerTestInstance() { xContentTester(this::createParser,
return createStatsResponse(); FollowStatsResponseTests::createTestInstance,
FollowStatsResponseTests::toXContent,
FollowStatsResponse::fromXContent)
.supportsUnknownFields(true)
.assertEqualsConsumer(FollowStatsResponseTests::assertEqualInstances)
.assertToXContentEquivalence(false)
.test();
} }
@Override // Needed, because exceptions in IndicesFollowStats cannot be compared
protected FollowStatsResponse doParseToClientInstance(XContentParser parser) throws IOException { private static void assertEqualInstances(FollowStatsResponse expectedInstance, FollowStatsResponse newInstance) {
return FollowStatsResponse.fromXContent(parser); 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<String, List<ShardFollowStats>> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) {
List<ShardFollowStats> newStats = indexEntry.getValue();
List<ShardFollowStats> 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 assertThat(actualShardFollowStats.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster()));
protected void assertInstances(FollowStatsAction.StatsResponses serverTestInstance, FollowStatsResponse clientInstance) { assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.getLeaderIndex()));
IndicesFollowStats newIndicesFollowStats = clientInstance.getIndicesFollowStats(); assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.getFollowerIndex()));
assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId()));
// sort by index name, then shard ID assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(),
final Map<String, Map<Integer, FollowStatsAction.StatsResponse>> expectedIndicesFollowStats = new TreeMap<>(); equalTo(expectedShardFollowStats.getLeaderGlobalCheckpoint()));
for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getStatsResponses()) { assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.getLeaderMaxSeqNo()));
expectedIndicesFollowStats.computeIfAbsent( assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(),
statsResponse.status().followerIndex(), equalTo(expectedShardFollowStats.getFollowerGlobalCheckpoint()));
k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.getLastRequestedSeqNo()));
} assertThat(actualShardFollowStats.getOutstandingReadRequests(),
assertThat(newIndicesFollowStats.getShardFollowStats().size(), equalTo(expectedShardFollowStats.getOutstandingReadRequests()));
equalTo(expectedIndicesFollowStats.size())); assertThat(actualShardFollowStats.getOutstandingWriteRequests(),
assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), equalTo(expectedShardFollowStats.getOutstandingWriteRequests()));
equalTo(expectedIndicesFollowStats.keySet())); assertThat(actualShardFollowStats.getWriteBufferOperationCount(),
for (Map.Entry<String, List<ShardFollowStats>> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { equalTo(expectedShardFollowStats.getWriteBufferOperationCount()));
List<ShardFollowStats> newStats = indexEntry.getValue(); assertThat(actualShardFollowStats.getFollowerMappingVersion(),
Map<Integer, FollowStatsAction.StatsResponse> expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); equalTo(expectedShardFollowStats.getFollowerMappingVersion()));
assertThat(newStats.size(), equalTo(expectedStats.size())); assertThat(actualShardFollowStats.getFollowerSettingsVersion(),
for (int i = 0; i < newStats.size(); i++) { equalTo(expectedShardFollowStats.getFollowerSettingsVersion()));
ShardFollowStats actualShardFollowStats = newStats.get(i); assertThat(actualShardFollowStats.getTotalReadTimeMillis(),
ShardFollowNodeTaskStatus expectedShardFollowStats = expectedStats.get(actualShardFollowStats.getShardId()).status(); equalTo(expectedShardFollowStats.getTotalReadTimeMillis()));
assertThat(actualShardFollowStats.getSuccessfulReadRequests(),
assertThat(actualShardFollowStats.getRemoteCluster(), equalTo(expectedShardFollowStats.getRemoteCluster())); equalTo(expectedShardFollowStats.getSuccessfulReadRequests()));
assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.getFailedReadRequests()));
assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.getOperationsReads()));
assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.getBytesRead()));
assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), assertThat(actualShardFollowStats.getTotalWriteTimeMillis(),
equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); equalTo(expectedShardFollowStats.getTotalWriteTimeMillis()));
assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); assertThat(actualShardFollowStats.getSuccessfulWriteRequests(),
assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), equalTo(expectedShardFollowStats.getSuccessfulWriteRequests()));
equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); assertThat(actualShardFollowStats.getFailedWriteRequests(),
assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); equalTo(expectedShardFollowStats.getFailedWriteRequests()));
assertThat(actualShardFollowStats.getOutstandingReadRequests(), assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.getOperationWritten()));
equalTo(expectedShardFollowStats.outstandingReadRequests())); assertThat(actualShardFollowStats.getReadExceptions().size(),
assertThat(actualShardFollowStats.getOutstandingWriteRequests(), equalTo(expectedShardFollowStats.getReadExceptions().size()));
equalTo(expectedShardFollowStats.outstandingWriteRequests())); assertThat(actualShardFollowStats.getReadExceptions().keySet(),
assertThat(actualShardFollowStats.getWriteBufferOperationCount(), equalTo(expectedShardFollowStats.getReadExceptions().keySet()));
equalTo(expectedShardFollowStats.writeBufferOperationCount())); for (final Map.Entry<Long, Tuple<Integer, ElasticsearchException>> entry :
assertThat(actualShardFollowStats.getFollowerMappingVersion(), actualShardFollowStats.getReadExceptions().entrySet()) {
equalTo(expectedShardFollowStats.followerMappingVersion())); final Tuple<Integer, ElasticsearchException> expectedTuple =
assertThat(actualShardFollowStats.getFollowerSettingsVersion(), expectedShardFollowStats.getReadExceptions().get(entry.getKey());
equalTo(expectedShardFollowStats.followerSettingsVersion())); assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1()));
assertThat(actualShardFollowStats.getTotalReadTimeMillis(), // x-content loses the exception
equalTo(expectedShardFollowStats.totalReadTimeMillis())); final ElasticsearchException expected = expectedTuple.v2();
assertThat(actualShardFollowStats.getSuccessfulReadRequests(), assertThat(entry.getValue().v2().getMessage(), containsString(expected.getMessage()));
equalTo(expectedShardFollowStats.successfulReadRequests())); assertNotNull(entry.getValue().v2().getCause());
assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); assertThat(
assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); entry.getValue().v2().getCause(),
assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)));
assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage()));
equalTo(expectedShardFollowStats.totalWriteTimeMillis())); }
assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(),
equalTo(expectedShardFollowStats.successfulWriteRequests())); equalTo(expectedShardFollowStats.getTimeSinceLastReadMillis()));
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<Long, Tuple<Integer, ElasticsearchException>> entry :
actualShardFollowStats.getReadExceptions().entrySet()) {
final Tuple<Integer, ElasticsearchException> 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.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<String, List<ShardFollowStats>> 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<Long, Tuple<Integer, ElasticsearchException>> 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());
}
} }

View File

@ -19,111 +19,99 @@
package org.elasticsearch.client.ccr; package org.elasticsearch.client.ccr;
import org.elasticsearch.client.AbstractResponseTestCase;
import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ccr.action.GetAutoFollowPatternAction; import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NavigableMap; import java.util.NavigableMap;
import java.util.TreeMap; import java.util.TreeMap;
import static org.hamcrest.Matchers.equalTo; import static org.elasticsearch.client.ccr.PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD;
import static org.hamcrest.Matchers.notNullValue; 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< public class GetAutoFollowPatternResponseTests extends ESTestCase {
GetAutoFollowPatternAction.Response,
GetAutoFollowPatternResponse> {
@Override public void testFromXContent() throws IOException {
protected GetAutoFollowPatternAction.Response createServerTestInstance() { xContentTester(this::createParser,
this::createTestInstance,
GetAutoFollowPatternResponseTests::toXContent,
GetAutoFollowPatternResponse::fromXContent)
.supportsUnknownFields(true)
.test();
}
private GetAutoFollowPatternResponse createTestInstance() {
int numPatterns = randomIntBetween(0, 16); int numPatterns = randomIntBetween(0, 16);
NavigableMap<String, AutoFollowMetadata.AutoFollowPattern> patterns = new TreeMap<>(); NavigableMap<String, GetAutoFollowPatternResponse.Pattern> patterns = new TreeMap<>();
for (int i = 0; i < numPatterns; i++) { for (int i = 0; i < numPatterns; i++) {
String remoteCluster = randomAlphaOfLength(4); GetAutoFollowPatternResponse.Pattern pattern = new GetAutoFollowPatternResponse.Pattern(
List<String> leaderIndexPatters = Collections.singletonList(randomAlphaOfLength(4)); randomAlphaOfLength(4), Collections.singletonList(randomAlphaOfLength(4)), randomAlphaOfLength(4));
String followIndexNamePattern = randomAlphaOfLength(4);
Integer maxOutstandingReadRequests = null;
if (randomBoolean()) { if (randomBoolean()) {
maxOutstandingReadRequests = randomIntBetween(0, Integer.MAX_VALUE); pattern.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
} }
Integer maxOutstandingWriteRequests = null;
if (randomBoolean()) { if (randomBoolean()) {
maxOutstandingWriteRequests = randomIntBetween(0, Integer.MAX_VALUE); pattern.setMaxOutstandingWriteRequests(randomIntBetween(0, Integer.MAX_VALUE));
} }
Integer maxReadRequestOperationCount = null;
if (randomBoolean()) { if (randomBoolean()) {
maxReadRequestOperationCount = randomIntBetween(0, Integer.MAX_VALUE); pattern.setMaxReadRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
} }
ByteSizeValue maxReadRequestSize = null;
if (randomBoolean()) { if (randomBoolean()) {
maxReadRequestSize = new ByteSizeValue(randomNonNegativeLong()); pattern.setMaxReadRequestSize(new ByteSizeValue(randomNonNegativeLong()));
} }
Integer maxWriteBufferCount = null;
if (randomBoolean()) { if (randomBoolean()) {
maxWriteBufferCount = randomIntBetween(0, Integer.MAX_VALUE); pattern.setMaxWriteBufferCount(randomIntBetween(0, Integer.MAX_VALUE));
} }
ByteSizeValue maxWriteBufferSize = null;
if (randomBoolean()) { if (randomBoolean()) {
maxWriteBufferSize = new ByteSizeValue(randomNonNegativeLong()); pattern.setMaxWriteBufferSize(new ByteSizeValue(randomNonNegativeLong()));
} }
Integer maxWriteRequestOperationCount = null;
if (randomBoolean()) { if (randomBoolean()) {
maxWriteRequestOperationCount = randomIntBetween(0, Integer.MAX_VALUE); pattern.setMaxWriteRequestOperationCount(randomIntBetween(0, Integer.MAX_VALUE));
} }
ByteSizeValue maxWriteRequestSize = null;
if (randomBoolean()) { if (randomBoolean()) {
maxWriteRequestSize = new ByteSizeValue(randomNonNegativeLong()); pattern.setMaxWriteRequestSize(new ByteSizeValue(randomNonNegativeLong()));
} }
TimeValue maxRetryDelay = null;
if (randomBoolean()) { if (randomBoolean()) {
maxRetryDelay = new TimeValue(randomNonNegativeLong()); pattern.setMaxRetryDelay(new TimeValue(randomNonNegativeLong()));
} }
TimeValue readPollTimeout = null;
if (randomBoolean()) { if (randomBoolean()) {
readPollTimeout = new TimeValue(randomNonNegativeLong()); pattern.setReadPollTimeout(new TimeValue(randomNonNegativeLong()));
} }
patterns.put(randomAlphaOfLength(4), new AutoFollowMetadata.AutoFollowPattern(remoteCluster, leaderIndexPatters, patterns.put(randomAlphaOfLength(4), pattern);
followIndexNamePattern, maxReadRequestOperationCount, maxWriteRequestOperationCount, maxOutstandingReadRequests,
maxOutstandingWriteRequests, maxReadRequestSize, maxWriteRequestSize, maxWriteBufferCount, maxWriteBufferSize,
maxRetryDelay, readPollTimeout));
} }
return new GetAutoFollowPatternAction.Response(patterns); return new GetAutoFollowPatternResponse(patterns);
} }
@Override public static void toXContent(GetAutoFollowPatternResponse response, XContentBuilder builder) throws IOException {
protected GetAutoFollowPatternResponse doParseToClientInstance(XContentParser parser) throws IOException { builder.startObject();
return GetAutoFollowPatternResponse.fromXContent(parser); {
} builder.startArray(GetAutoFollowPatternResponse.PATTERNS_FIELD.getPreferredName());
for (Map.Entry<String, GetAutoFollowPatternResponse.Pattern> entry : response.getPatterns().entrySet()) {
@Override builder.startObject();
protected void assertInstances(GetAutoFollowPatternAction.Response serverTestInstance, GetAutoFollowPatternResponse clientInstance) { {
assertThat(serverTestInstance.getAutoFollowPatterns().size(), equalTo(clientInstance.getPatterns().size())); builder.field(GetAutoFollowPatternResponse.NAME_FIELD.getPreferredName(), entry.getKey());
for (Map.Entry<String, AutoFollowMetadata.AutoFollowPattern> entry : serverTestInstance.getAutoFollowPatterns().entrySet()) { builder.startObject(GetAutoFollowPatternResponse.PATTERN_FIELD.getPreferredName());
AutoFollowMetadata.AutoFollowPattern serverPattern = entry.getValue(); {
GetAutoFollowPatternResponse.Pattern clientPattern = clientInstance.getPatterns().get(entry.getKey()); GetAutoFollowPatternResponse.Pattern pattern = entry.getValue();
assertThat(clientPattern, notNullValue()); builder.field(REMOTE_CLUSTER_FIELD.getPreferredName(), pattern.getRemoteCluster());
builder.field(LEADER_PATTERNS_FIELD.getPreferredName(), pattern.getLeaderIndexPatterns());
assertThat(serverPattern.getRemoteCluster(), equalTo(clientPattern.getRemoteCluster())); if (pattern.getFollowIndexNamePattern()!= null) {
assertThat(serverPattern.getLeaderIndexPatterns(), equalTo(clientPattern.getLeaderIndexPatterns())); builder.field(FOLLOW_PATTERN_FIELD.getPreferredName(), pattern.getFollowIndexNamePattern());
assertThat(serverPattern.getFollowIndexPattern(), equalTo(clientPattern.getFollowIndexNamePattern())); }
assertThat(serverPattern.getMaxOutstandingReadRequests(), equalTo(clientPattern.getMaxOutstandingReadRequests())); entry.getValue().toXContentFragment(builder, ToXContent.EMPTY_PARAMS);
assertThat(serverPattern.getMaxOutstandingWriteRequests(), equalTo(clientPattern.getMaxOutstandingWriteRequests())); }
assertThat(serverPattern.getMaxReadRequestOperationCount(), equalTo(clientPattern.getMaxReadRequestOperationCount())); builder.endObject();
assertThat(serverPattern.getMaxWriteRequestOperationCount(), equalTo(clientPattern.getMaxWriteRequestOperationCount())); }
assertThat(serverPattern.getMaxReadRequestSize(), equalTo(clientPattern.getMaxReadRequestSize())); builder.endObject();
assertThat(serverPattern.getMaxWriteRequestSize(), equalTo(clientPattern.getMaxWriteRequestSize())); }
assertThat(serverPattern.getMaxWriteBufferCount(), equalTo(clientPattern.getMaxWriteBufferCount())); builder.endArray();
assertThat(serverPattern.getMaxWriteBufferSize(), equalTo(clientPattern.getMaxWriteBufferSize()));
assertThat(serverPattern.getMaxRetryDelay(), equalTo(clientPattern.getMaxRetryDelay()));
assertThat(serverPattern.getReadPollTimeout(), equalTo(clientPattern.getReadPollTimeout()));
} }
builder.endObject();
} }
} }

View File

@ -19,30 +19,35 @@
package org.elasticsearch.client.ccr; package org.elasticsearch.client.ccr;
import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.ccr.action.PutFollowAction;
import java.io.IOException; import java.io.IOException;
import static org.hamcrest.Matchers.is; import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
public class PutFollowResponseTests extends AbstractResponseTestCase<PutFollowAction.Response, PutFollowResponse> { public class PutFollowResponseTests extends ESTestCase {
@Override public void testFromXContent() throws IOException {
protected PutFollowAction.Response createServerTestInstance() { xContentTester(this::createParser,
return new PutFollowAction.Response(randomBoolean(), randomBoolean(), randomBoolean()); this::createTestInstance,
PutFollowResponseTests::toXContent,
PutFollowResponse::fromXContent)
.supportsUnknownFields(true)
.test();
} }
@Override private PutFollowResponse createTestInstance() {
protected PutFollowResponse doParseToClientInstance(XContentParser parser) throws IOException { return new PutFollowResponse(randomBoolean(), randomBoolean(), randomBoolean());
return PutFollowResponse.fromXContent(parser);
} }
@Override public static void toXContent(PutFollowResponse response, XContentBuilder builder) throws IOException {
protected void assertInstances(PutFollowAction.Response serverTestInstance, PutFollowResponse clientInstance) { builder.startObject();
assertThat(serverTestInstance.isFollowIndexCreated(), is(clientInstance.isFollowIndexCreated())); {
assertThat(serverTestInstance.isFollowIndexShardsAcked(), is(clientInstance.isFollowIndexShardsAcked())); builder.field(PutFollowResponse.FOLLOW_INDEX_CREATED.getPreferredName(), response.isFollowIndexCreated());
assertThat(serverTestInstance.isIndexFollowingStarted(), is(clientInstance.isIndexFollowingStarted())); builder.field(PutFollowResponse.FOLLOW_INDEX_SHARDS_ACKED.getPreferredName(), response.isFollowIndexShardsAcked());
builder.field(PutFollowResponse.INDEX_FOLLOWING_STARTED.getPreferredName(), response.isIndexFollowingStarted());
}
builder.endObject();
} }
} }