use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project elasticsearch by elastic.
the class ReplicationOperationTests method testReplicationWithShadowIndex.
public void testReplicationWithShadowIndex() throws Exception {
final String index = "test";
final ShardId shardId = new ShardId(index, "_na_", 0);
final ClusterState state = stateWithActivePrimary(index, true, randomInt(5));
final long primaryTerm = state.getMetaData().index(index).primaryTerm(0);
final IndexShardRoutingTable indexShardRoutingTable = state.getRoutingTable().shardRoutingTable(shardId);
final ShardRouting primaryShard = indexShardRoutingTable.primaryShard();
Request request = new Request(shardId);
PlainActionFuture<TestPrimary.Result> listener = new PlainActionFuture<>();
final TestReplicationOperation op = new TestReplicationOperation(request, new TestPrimary(primaryShard, primaryTerm), listener, false, new TestReplicaProxy(), () -> state, logger, "test");
op.execute();
assertThat("request was not processed on primary", request.processedOnPrimary.get(), equalTo(true));
assertThat(request.processedOnReplicas, equalTo(Collections.emptySet()));
assertTrue("listener is not marked as done", listener.isDone());
ShardInfo shardInfo = listener.actionGet().getShardInfo();
assertThat(shardInfo.getFailed(), equalTo(0));
assertThat(shardInfo.getFailures(), arrayWithSize(0));
assertThat(shardInfo.getSuccessful(), equalTo(1));
assertThat(shardInfo.getTotal(), equalTo(indexShardRoutingTable.getSize()));
}
use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project elasticsearch by elastic.
the class ReplicationResponseTests method testShardInfoToString.
public void testShardInfoToString() {
final int total = 5;
final int successful = randomIntBetween(1, total);
final ShardInfo shardInfo = new ShardInfo(total, successful);
assertEquals(String.format(Locale.ROOT, "ShardInfo{total=5, successful=%d, failures=[]}", successful), shardInfo.toString());
}
use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project elasticsearch by elastic.
the class ReplicationResponseTests method assertShardInfo.
public static void assertShardInfo(ShardInfo expected, ShardInfo actual) {
if (expected == null) {
assertNull(actual);
} else {
assertEquals(expected.getTotal(), actual.getTotal());
assertEquals(expected.getSuccessful(), actual.getSuccessful());
assertEquals(expected.getFailed(), actual.getFailed());
ReplicationResponse.ShardInfo.Failure[] expectedFailures = expected.getFailures();
ReplicationResponse.ShardInfo.Failure[] actualFailures = actual.getFailures();
assertEquals(expectedFailures.length, actualFailures.length);
for (int i = 0; i < expectedFailures.length; i++) {
ReplicationResponse.ShardInfo.Failure expectedFailure = expectedFailures[i];
ReplicationResponse.ShardInfo.Failure actualFailure = actualFailures[i];
assertEquals(expectedFailure.fullShardId(), actualFailure.fullShardId());
assertEquals(expectedFailure.status(), actualFailure.status());
assertEquals(expectedFailure.nodeId(), actualFailure.nodeId());
assertEquals(expectedFailure.primary(), actualFailure.primary());
ElasticsearchException expectedCause = (ElasticsearchException) expectedFailure.getCause();
ElasticsearchException actualCause = (ElasticsearchException) actualFailure.getCause();
assertDeepEquals(expectedCause, actualCause);
}
}
}
use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project elasticsearch by elastic.
the class ReplicationResponseTests method testShardInfoToAndFromXContent.
public void testShardInfoToAndFromXContent() throws IOException {
final Tuple<ShardInfo, ShardInfo> tuple = RandomObjects.randomShardInfo(random());
ShardInfo shardInfo = tuple.v1();
ShardInfo expectedShardInfo = tuple.v2();
final XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(shardInfo, xContentType, humanReadable);
// Shuffle the XContent fields
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
}
}
ShardInfo parsedShardInfo;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
// Move to the first start object
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedShardInfo = ShardInfo.fromXContent(parser);
assertNull(parser.nextToken());
}
assertShardInfo(expectedShardInfo, parsedShardInfo);
BytesReference expectedFinalBytes = toXContent(expectedShardInfo, xContentType, humanReadable);
BytesReference finalBytes = toXContent(parsedShardInfo, xContentType, humanReadable);
assertToXContentEquivalent(expectedFinalBytes, finalBytes, xContentType);
}
use of org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo in project elasticsearch by elastic.
the class RandomObjects method randomShardInfo.
/**
* Returns a tuple that contains a randomized {@link ShardInfo} value (left side) and its corresponding
* value (right side) after it has been printed out as a {@link ToXContent} and parsed back using a parsing
* method like {@link ShardInfo#fromXContent(XContentParser)}. A `withShardFailures` parameter indicates if
* the randomized ShardInfo must or must not contain shard failures.
*
* @param random Random generator
* @param withShardFailures indicates if the generated ShardInfo must contain shard failures
*/
public static Tuple<ShardInfo, ShardInfo> randomShardInfo(Random random, boolean withShardFailures) {
int total = randomIntBetween(random, 1, 10);
if (withShardFailures == false) {
return Tuple.tuple(new ShardInfo(total, total), new ShardInfo(total, total));
}
int successful = randomIntBetween(random, 1, Math.max(1, (total - 1)));
int failures = Math.max(1, (total - successful));
Failure[] actualFailures = new Failure[failures];
Failure[] expectedFailures = new Failure[failures];
for (int i = 0; i < failures; i++) {
Tuple<Failure, Failure> failure = randomShardInfoFailure(random);
actualFailures[i] = failure.v1();
expectedFailures[i] = failure.v2();
}
return Tuple.tuple(new ShardInfo(total, successful, actualFailures), new ShardInfo(total, successful, expectedFailures));
}
Aggregations