Search in sources :

Example 1 with ShardsSyncedFlushResult

use of org.elasticsearch.indices.flush.ShardsSyncedFlushResult in project elasticsearch by elastic.

the class SyncedFlushResponse method calculateShardCounts.

static ShardCounts calculateShardCounts(Iterable<ShardsSyncedFlushResult> results) {
    int total = 0, successful = 0, failed = 0;
    for (ShardsSyncedFlushResult result : results) {
        total += result.totalShards();
        successful += result.successfulShards();
        if (result.failed()) {
            // treat all shard copies as failed
            failed += result.totalShards();
        } else {
            // some shards may have failed during the sync phase
            failed += result.failedShards().size();
        }
    }
    return new ShardCounts(total, successful, failed);
}
Also used : ShardsSyncedFlushResult(org.elasticsearch.indices.flush.ShardsSyncedFlushResult)

Example 2 with ShardsSyncedFlushResult

use of org.elasticsearch.indices.flush.ShardsSyncedFlushResult in project elasticsearch by elastic.

the class SyncedFlushResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields._SHARDS);
    shardCounts.toXContent(builder, params);
    builder.endObject();
    for (Map.Entry<String, List<ShardsSyncedFlushResult>> indexEntry : shardsResultPerIndex.entrySet()) {
        List<ShardsSyncedFlushResult> indexResult = indexEntry.getValue();
        builder.startObject(indexEntry.getKey());
        ShardCounts indexShardCounts = calculateShardCounts(indexResult);
        indexShardCounts.toXContent(builder, params);
        if (indexShardCounts.failed > 0) {
            builder.startArray(Fields.FAILURES);
            for (ShardsSyncedFlushResult shardResults : indexResult) {
                if (shardResults.failed()) {
                    builder.startObject();
                    builder.field(Fields.SHARD, shardResults.shardId().id());
                    builder.field(Fields.REASON, shardResults.failureReason());
                    builder.endObject();
                    continue;
                }
                Map<ShardRouting, SyncedFlushService.ShardSyncedFlushResponse> failedShards = shardResults.failedShards();
                for (Map.Entry<ShardRouting, SyncedFlushService.ShardSyncedFlushResponse> shardEntry : failedShards.entrySet()) {
                    builder.startObject();
                    builder.field(Fields.SHARD, shardResults.shardId().id());
                    builder.field(Fields.REASON, shardEntry.getValue().failureReason());
                    builder.field(Fields.ROUTING, shardEntry.getKey());
                    builder.endObject();
                }
            }
            builder.endArray();
        }
        builder.endObject();
    }
    return builder;
}
Also used : ShardsSyncedFlushResult(org.elasticsearch.indices.flush.ShardsSyncedFlushResult) ArrayList(java.util.ArrayList) List(java.util.List) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 3 with ShardsSyncedFlushResult

use of org.elasticsearch.indices.flush.ShardsSyncedFlushResult in project elasticsearch by elastic.

the class SyncedFlushResponse method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    shardCounts.writeTo(out);
    out.writeInt(shardsResultPerIndex.size());
    for (Map.Entry<String, List<ShardsSyncedFlushResult>> entry : shardsResultPerIndex.entrySet()) {
        out.writeString(entry.getKey());
        out.writeInt(entry.getValue().size());
        for (ShardsSyncedFlushResult shardsSyncedFlushResult : entry.getValue()) {
            shardsSyncedFlushResult.writeTo(out);
        }
    }
}
Also used : ShardsSyncedFlushResult(org.elasticsearch.indices.flush.ShardsSyncedFlushResult) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 4 with ShardsSyncedFlushResult

use of org.elasticsearch.indices.flush.ShardsSyncedFlushResult in project elasticsearch by elastic.

the class SyncedFlushUnitTests method testResponseStreaming.

public void testResponseStreaming() throws IOException {
    final TestPlan testPlan = createTestPlan();
    assertThat(testPlan.result.totalShards(), equalTo(testPlan.totalCounts.total));
    assertThat(testPlan.result.successfulShards(), equalTo(testPlan.totalCounts.successful));
    assertThat(testPlan.result.failedShards(), equalTo(testPlan.totalCounts.failed));
    assertThat(testPlan.result.restStatus(), equalTo(testPlan.totalCounts.failed > 0 ? RestStatus.CONFLICT : RestStatus.OK));
    BytesStreamOutput out = new BytesStreamOutput();
    testPlan.result.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    SyncedFlushResponse readResponse = new SyncedFlushResponse();
    readResponse.readFrom(in);
    assertThat(readResponse.totalShards(), equalTo(testPlan.totalCounts.total));
    assertThat(readResponse.successfulShards(), equalTo(testPlan.totalCounts.successful));
    assertThat(readResponse.failedShards(), equalTo(testPlan.totalCounts.failed));
    assertThat(readResponse.restStatus(), equalTo(testPlan.totalCounts.failed > 0 ? RestStatus.CONFLICT : RestStatus.OK));
    assertThat(readResponse.shardsResultPerIndex.size(), equalTo(testPlan.result.getShardsResultPerIndex().size()));
    for (Map.Entry<String, List<ShardsSyncedFlushResult>> entry : readResponse.getShardsResultPerIndex().entrySet()) {
        List<ShardsSyncedFlushResult> originalShardsResults = testPlan.result.getShardsResultPerIndex().get(entry.getKey());
        assertNotNull(originalShardsResults);
        List<ShardsSyncedFlushResult> readShardsResults = entry.getValue();
        assertThat(readShardsResults.size(), equalTo(originalShardsResults.size()));
        for (int i = 0; i < readShardsResults.size(); i++) {
            ShardsSyncedFlushResult originalShardResult = originalShardsResults.get(i);
            ShardsSyncedFlushResult readShardResult = readShardsResults.get(i);
            assertThat(originalShardResult.failureReason(), equalTo(readShardResult.failureReason()));
            assertThat(originalShardResult.failed(), equalTo(readShardResult.failed()));
            assertThat(originalShardResult.getShardId(), equalTo(readShardResult.getShardId()));
            assertThat(originalShardResult.successfulShards(), equalTo(readShardResult.successfulShards()));
            assertThat(originalShardResult.syncId(), equalTo(readShardResult.syncId()));
            assertThat(originalShardResult.totalShards(), equalTo(readShardResult.totalShards()));
            assertThat(originalShardResult.failedShards().size(), equalTo(readShardResult.failedShards().size()));
            for (Map.Entry<ShardRouting, SyncedFlushService.ShardSyncedFlushResponse> shardEntry : originalShardResult.failedShards().entrySet()) {
                SyncedFlushService.ShardSyncedFlushResponse readShardResponse = readShardResult.failedShards().get(shardEntry.getKey());
                assertNotNull(readShardResponse);
                SyncedFlushService.ShardSyncedFlushResponse originalShardResponse = shardEntry.getValue();
                assertThat(originalShardResponse.failureReason(), equalTo(readShardResponse.failureReason()));
                assertThat(originalShardResponse.success(), equalTo(readShardResponse.success()));
            }
            assertThat(originalShardResult.shardResponses().size(), equalTo(readShardResult.shardResponses().size()));
            for (Map.Entry<ShardRouting, SyncedFlushService.ShardSyncedFlushResponse> shardEntry : originalShardResult.shardResponses().entrySet()) {
                SyncedFlushService.ShardSyncedFlushResponse readShardResponse = readShardResult.shardResponses().get(shardEntry.getKey());
                assertNotNull(readShardResponse);
                SyncedFlushService.ShardSyncedFlushResponse originalShardResponse = shardEntry.getValue();
                assertThat(originalShardResponse.failureReason(), equalTo(readShardResponse.failureReason()));
                assertThat(originalShardResponse.success(), equalTo(readShardResponse.success()));
            }
        }
    }
}
Also used : ShardsSyncedFlushResult(org.elasticsearch.indices.flush.ShardsSyncedFlushResult) SyncedFlushService(org.elasticsearch.indices.flush.SyncedFlushService) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ArrayList(java.util.ArrayList) List(java.util.List) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) XContentTestUtils.convertToMap(org.elasticsearch.test.XContentTestUtils.convertToMap) ObjectIntMap(com.carrotsearch.hppc.ObjectIntMap) HashMap(java.util.HashMap) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) Map(java.util.Map)

Example 5 with ShardsSyncedFlushResult

use of org.elasticsearch.indices.flush.ShardsSyncedFlushResult in project elasticsearch by elastic.

the class SyncedFlushUnitTests method createTestPlan.

protected TestPlan createTestPlan() {
    final TestPlan testPlan = new TestPlan();
    final Map<String, List<ShardsSyncedFlushResult>> indicesResults = new HashMap<>();
    final int indexCount = randomIntBetween(1, 10);
    int totalShards = 0;
    int totalSuccesful = 0;
    int totalFailed = 0;
    for (int i = 0; i < indexCount; i++) {
        final String index = "index_" + i;
        int shards = randomIntBetween(1, 4);
        int replicas = randomIntBetween(0, 2);
        int successful = 0;
        int failed = 0;
        int failures = 0;
        List<ShardsSyncedFlushResult> shardsResults = new ArrayList<>();
        for (int shard = 0; shard < shards; shard++) {
            final ShardId shardId = new ShardId(index, "_na_", shard);
            if (randomInt(5) < 2) {
                // total shard failure
                failed += replicas + 1;
                failures++;
                shardsResults.add(new ShardsSyncedFlushResult(shardId, replicas + 1, "simulated total failure"));
            } else {
                Map<ShardRouting, SyncedFlushService.ShardSyncedFlushResponse> shardResponses = new HashMap<>();
                for (int copy = 0; copy < replicas + 1; copy++) {
                    final ShardRouting shardRouting = TestShardRouting.newShardRouting(index, shard, "node_" + shardId + "_" + copy, null, copy == 0, ShardRoutingState.STARTED);
                    if (randomInt(5) < 2) {
                        // shard copy failure
                        failed++;
                        failures++;
                        shardResponses.put(shardRouting, new SyncedFlushService.ShardSyncedFlushResponse("copy failure " + shardId));
                    } else {
                        successful++;
                        shardResponses.put(shardRouting, new SyncedFlushService.ShardSyncedFlushResponse());
                    }
                }
                shardsResults.add(new ShardsSyncedFlushResult(shardId, "_sync_id_" + shard, replicas + 1, shardResponses));
            }
        }
        indicesResults.put(index, shardsResults);
        testPlan.countsPerIndex.put(index, new SyncedFlushResponse.ShardCounts(shards * (replicas + 1), successful, failed));
        testPlan.expectedFailuresPerIndex.put(index, failures);
        totalFailed += failed;
        totalShards += shards * (replicas + 1);
        totalSuccesful += successful;
    }
    testPlan.result = new SyncedFlushResponse(indicesResults);
    testPlan.totalCounts = new SyncedFlushResponse.ShardCounts(totalShards, totalSuccesful, totalFailed);
    return testPlan;
}
Also used : HashMap(java.util.HashMap) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) ArrayList(java.util.ArrayList) ShardsSyncedFlushResult(org.elasticsearch.indices.flush.ShardsSyncedFlushResult) SyncedFlushService(org.elasticsearch.indices.flush.SyncedFlushService) ShardCounts(org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse.ShardCounts) ShardId(org.elasticsearch.index.shard.ShardId) ArrayList(java.util.ArrayList) List(java.util.List) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting)

Aggregations

ShardsSyncedFlushResult (org.elasticsearch.indices.flush.ShardsSyncedFlushResult)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)3 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)3 ObjectIntHashMap (com.carrotsearch.hppc.ObjectIntHashMap)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)2 SyncedFlushService (org.elasticsearch.indices.flush.SyncedFlushService)2 ObjectIntMap (com.carrotsearch.hppc.ObjectIntMap)1 ShardCounts (org.elasticsearch.action.admin.indices.flush.SyncedFlushResponse.ShardCounts)1 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 ShardId (org.elasticsearch.index.shard.ShardId)1 XContentTestUtils.convertToMap (org.elasticsearch.test.XContentTestUtils.convertToMap)1