Search in sources :

Example 6 with ShardInfo

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()));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 7 with ShardInfo

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());
}
Also used : ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 8 with ShardInfo

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);
        }
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 9 with ShardInfo

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 10 with ShardInfo

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));
}
Also used : Failure(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo.Failure) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Aggregations

ShardInfo (org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)11 ShardId (org.elasticsearch.index.shard.ShardId)5 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 ExecutionException (java.util.concurrent.ExecutionException)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 UnavailableShardsException (org.elasticsearch.action.UnavailableShardsException)3 IndexShardNotStartedException (org.elasticsearch.index.shard.IndexShardNotStartedException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 ReplicationGroup (org.elasticsearch.index.shard.ReplicationGroup)2 NodeClosedException (org.elasticsearch.node.NodeClosedException)2 SendRequestTransportException (org.elasticsearch.transport.SendRequestTransportException)2