Search in sources :

Example 51 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class SnapshotShardFailure method fromXContent.

/**
     * Deserializes snapshot failure information from JSON
     *
     * @param parser JSON parser
     * @return snapshot failure information
     */
public static SnapshotShardFailure fromXContent(XContentParser parser) throws IOException {
    SnapshotShardFailure snapshotShardFailure = new SnapshotShardFailure();
    XContentParser.Token token = parser.currentToken();
    String index = null;
    String index_uuid = IndexMetaData.INDEX_UUID_NA_VALUE;
    int shardId = -1;
    if (token == XContentParser.Token.START_OBJECT) {
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String currentFieldName = parser.currentName();
                token = parser.nextToken();
                if (token.isValue()) {
                    if ("index".equals(currentFieldName)) {
                        index = parser.text();
                    } else if ("index_uuid".equals(currentFieldName)) {
                        index_uuid = parser.text();
                    } else if ("node_id".equals(currentFieldName)) {
                        snapshotShardFailure.nodeId = parser.text();
                    } else if ("reason".equals(currentFieldName)) {
                        snapshotShardFailure.reason = parser.text();
                    } else if ("shard_id".equals(currentFieldName)) {
                        shardId = parser.intValue();
                    } else if ("status".equals(currentFieldName)) {
                        snapshotShardFailure.status = RestStatus.valueOf(parser.text());
                    } else {
                        throw new ElasticsearchParseException("unknown parameter [{}]", currentFieldName);
                    }
                }
            } else {
                throw new ElasticsearchParseException("unexpected token [{}]", token);
            }
        }
    } else {
        throw new ElasticsearchParseException("unexpected token [{}]", token);
    }
    if (index == null) {
        throw new ElasticsearchParseException("index name was not set");
    }
    if (shardId == -1) {
        throw new ElasticsearchParseException("index shard was not set");
    }
    snapshotShardFailure.shardId = new ShardId(index, index_uuid, shardId);
    return snapshotShardFailure;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class SnapshotShardsService method snapshot.

/**
     * Creates shard snapshot
     *
     * @param snapshot       snapshot
     * @param snapshotStatus snapshot status
     */
private void snapshot(final IndexShard indexShard, final Snapshot snapshot, final IndexId indexId, final IndexShardSnapshotStatus snapshotStatus) {
    Repository repository = snapshotsService.getRepositoriesService().repository(snapshot.getRepository());
    ShardId shardId = indexShard.shardId();
    if (!indexShard.routingEntry().primary()) {
        throw new IndexShardSnapshotFailedException(shardId, "snapshot should be performed only on primary");
    }
    if (indexShard.routingEntry().relocating()) {
        // do not snapshot when in the process of relocation of primaries so we won't get conflicts
        throw new IndexShardSnapshotFailedException(shardId, "cannot snapshot while relocating");
    }
    if (indexShard.state() == IndexShardState.CREATED || indexShard.state() == IndexShardState.RECOVERING) {
        // shard has just been created, or still recovering
        throw new IndexShardSnapshotFailedException(shardId, "shard didn't fully recover yet");
    }
    try {
        // we flush first to make sure we get the latest writes snapshotted
        IndexCommit snapshotIndexCommit = indexShard.acquireIndexCommit(true);
        try {
            repository.snapshotShard(indexShard, snapshot.getSnapshotId(), indexId, snapshotIndexCommit, snapshotStatus);
            if (logger.isDebugEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("    index    : version [").append(snapshotStatus.indexVersion()).append("], number_of_files [").append(snapshotStatus.numberOfFiles()).append("] with total_size [").append(new ByteSizeValue(snapshotStatus.totalSize())).append("]\n");
                logger.debug("snapshot ({}) completed to {}, took [{}]\n{}", snapshot, repository, TimeValue.timeValueMillis(snapshotStatus.time()), sb);
            }
        } finally {
            indexShard.releaseIndexCommit(snapshotIndexCommit);
        }
    } catch (SnapshotFailedEngineException e) {
        throw e;
    } catch (IndexShardSnapshotFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new IndexShardSnapshotFailedException(shardId, "Failed to snapshot", e);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Repository(org.elasticsearch.repositories.Repository) SnapshotFailedEngineException(org.elasticsearch.index.engine.SnapshotFailedEngineException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) IndexShardSnapshotFailedException(org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException) IndexCommit(org.apache.lucene.index.IndexCommit) IndexShardSnapshotFailedException(org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException) SnapshotFailedEngineException(org.elasticsearch.index.engine.SnapshotFailedEngineException) IOException(java.io.IOException)

Example 53 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class SnapshotShardsService method syncShardStatsOnNewMaster.

/**
     * Checks if any shards were processed that the new master doesn't know about
     */
private void syncShardStatsOnNewMaster(ClusterChangedEvent event) {
    SnapshotsInProgress snapshotsInProgress = event.state().custom(SnapshotsInProgress.TYPE);
    if (snapshotsInProgress == null) {
        return;
    }
    final String localNodeId = event.state().nodes().getLocalNodeId();
    final DiscoveryNode masterNode = event.state().nodes().getMasterNode();
    for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) {
        if (snapshot.state() == State.STARTED || snapshot.state() == State.ABORTED) {
            Map<ShardId, IndexShardSnapshotStatus> localShards = currentSnapshotShards(snapshot.snapshot());
            if (localShards != null) {
                ImmutableOpenMap<ShardId, ShardSnapshotStatus> masterShards = snapshot.shards();
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> localShard : localShards.entrySet()) {
                    ShardId shardId = localShard.getKey();
                    IndexShardSnapshotStatus localShardStatus = localShard.getValue();
                    ShardSnapshotStatus masterShard = masterShards.get(shardId);
                    if (masterShard != null && masterShard.state().completed() == false) {
                        // Master knows about the shard and thinks it has not completed
                        if (localShardStatus.stage() == Stage.DONE) {
                            // but we think the shard is done - we need to make new master know that the shard is done
                            logger.debug("[{}] new master thinks the shard [{}] is not completed but the shard is done locally, updating status on the master", snapshot.snapshot(), shardId);
                            updateIndexShardSnapshotStatus(snapshot.snapshot(), shardId, new ShardSnapshotStatus(localNodeId, State.SUCCESS), masterNode);
                        } else if (localShard.getValue().stage() == Stage.FAILURE) {
                            // but we think the shard failed - we need to make new master know that the shard failed
                            logger.debug("[{}] new master thinks the shard [{}] is not completed but the shard failed locally, updating status on master", snapshot.snapshot(), shardId);
                            updateIndexShardSnapshotStatus(snapshot.snapshot(), shardId, new ShardSnapshotStatus(localNodeId, State.FAILED, localShardStatus.failure()), masterNode);
                        }
                    }
                }
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) Map(java.util.Map) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 54 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class SnapshotsService method endSnapshot.

/**
     * Finalizes the shard in repository and then removes it from cluster state
     * <p>
     * This is non-blocking method that runs on a thread from SNAPSHOT thread pool
     *
     * @param entry   snapshot
     * @param failure failure reason or null if snapshot was successful
     */
private void endSnapshot(final SnapshotsInProgress.Entry entry, final String failure) {
    threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(new Runnable() {

        @Override
        public void run() {
            final Snapshot snapshot = entry.snapshot();
            try {
                final Repository repository = repositoriesService.repository(snapshot.getRepository());
                logger.trace("[{}] finalizing snapshot in repository, state: [{}], failure[{}]", snapshot, entry.state(), failure);
                ArrayList<ShardSearchFailure> failures = new ArrayList<>();
                ArrayList<SnapshotShardFailure> shardFailures = new ArrayList<>();
                for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardStatus : entry.shards()) {
                    ShardId shardId = shardStatus.key;
                    ShardSnapshotStatus status = shardStatus.value;
                    if (status.state().failed()) {
                        failures.add(new ShardSearchFailure(status.reason(), new SearchShardTarget(status.nodeId(), shardId)));
                        shardFailures.add(new SnapshotShardFailure(status.nodeId(), shardId, status.reason()));
                    }
                }
                SnapshotInfo snapshotInfo = repository.finalizeSnapshot(snapshot.getSnapshotId(), entry.indices(), entry.startTime(), failure, entry.shards().size(), Collections.unmodifiableList(shardFailures), entry.getRepositoryStateId());
                removeSnapshotFromClusterState(snapshot, snapshotInfo, null);
            } catch (Exception e) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to finalize snapshot", snapshot), e);
                removeSnapshotFromClusterState(snapshot, null, e);
            }
        }
    });
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId) Repository(org.elasticsearch.repositories.Repository) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure)

Example 55 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class FieldStatsRequestTests method testFieldStatsBWC.

public void testFieldStatsBWC() throws Exception {
    int size = randomIntBetween(5, 20);
    Map<String, FieldStats<?>> stats = new HashMap<>();
    for (int i = 0; i < size; i++) {
        stats.put(Integer.toString(i), FieldStatsTests.randomFieldStats(true));
    }
    FieldStatsShardResponse response = new FieldStatsShardResponse(new ShardId("test", "test", 0), stats);
    for (int i = 0; i < 10; i++) {
        Version version = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0, Version.CURRENT);
        BytesStreamOutput output = new BytesStreamOutput();
        output.setVersion(version);
        response.writeTo(output);
        output.flush();
        StreamInput input = output.bytes().streamInput();
        input.setVersion(version);
        FieldStatsShardResponse deserialized = new FieldStatsShardResponse();
        deserialized.readFrom(input);
        final Map<String, FieldStats<?>> expected;
        if (version.before(Version.V_5_2_0_UNRELEASED)) {
            expected = deserialized.filterNullMinMax();
        } else {
            expected = deserialized.getFieldStats();
        }
        assertEquals(expected.size(), deserialized.getFieldStats().size());
        assertThat(expected, equalTo(deserialized.getFieldStats()));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) HashMap(java.util.HashMap) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

ShardId (org.elasticsearch.index.shard.ShardId)478 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)106 Index (org.elasticsearch.index.Index)101 ClusterState (org.elasticsearch.cluster.ClusterState)98 Test (org.junit.Test)83 ArrayList (java.util.ArrayList)78 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)69 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)67 HashMap (java.util.HashMap)66 IOException (java.io.IOException)61 IndexShard (org.elasticsearch.index.shard.IndexShard)61 Map (java.util.Map)55 List (java.util.List)51 Settings (org.elasticsearch.common.settings.Settings)49 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)45 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)44 HashSet (java.util.HashSet)43 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)43 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)42 Matchers.containsString (org.hamcrest.Matchers.containsString)42