Search in sources :

Example 31 with ImmutableOpenMap

use of org.elasticsearch.common.collect.ImmutableOpenMap in project elasticsearch by elastic.

the class Engine method getSegmentFileSizes.

private ImmutableOpenMap<String, Long> getSegmentFileSizes(SegmentReader segmentReader) {
    Directory directory = null;
    SegmentCommitInfo segmentCommitInfo = segmentReader.getSegmentInfo();
    boolean useCompoundFile = segmentCommitInfo.info.getUseCompoundFile();
    if (useCompoundFile) {
        try {
            directory = engineConfig.getCodec().compoundFormat().getCompoundReader(segmentReader.directory(), segmentCommitInfo.info, IOContext.READ);
        } catch (IOException e) {
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Error when opening compound reader for Directory [{}] and SegmentCommitInfo [{}]", segmentReader.directory(), segmentCommitInfo), e);
            return ImmutableOpenMap.of();
        }
    } else {
        directory = segmentReader.directory();
    }
    assert directory != null;
    String[] files;
    if (useCompoundFile) {
        try {
            files = directory.listAll();
        } catch (IOException e) {
            final Directory finalDirectory = directory;
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Couldn't list Compound Reader Directory [{}]", finalDirectory), e);
            return ImmutableOpenMap.of();
        }
    } else {
        try {
            files = segmentReader.getSegmentInfo().files().toArray(new String[] {});
        } catch (IOException e) {
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Couldn't list Directory from SegmentReader [{}] and SegmentInfo [{}]", segmentReader, segmentReader.getSegmentInfo()), e);
            return ImmutableOpenMap.of();
        }
    }
    ImmutableOpenMap.Builder<String, Long> map = ImmutableOpenMap.builder();
    for (String file : files) {
        String extension = IndexFileNames.getExtension(file);
        long length = 0L;
        try {
            length = directory.fileLength(file);
        } catch (NoSuchFileException | FileNotFoundException e) {
            final Directory finalDirectory = directory;
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Tried to query fileLength but file is gone [{}] [{}]", finalDirectory, file), e);
        } catch (IOException e) {
            final Directory finalDirectory = directory;
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Error when trying to query fileLength [{}] [{}]", finalDirectory, file), e);
        }
        if (length == 0L) {
            continue;
        }
        map.put(extension, length);
    }
    if (useCompoundFile && directory != null) {
        try {
            directory.close();
        } catch (IOException e) {
            final Directory finalDirectory = directory;
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("Error when closing compound reader on Directory [{}]", finalDirectory), e);
        }
    }
    return map.build();
}
Also used : SegmentCommitInfo(org.apache.lucene.index.SegmentCommitInfo) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Directory(org.apache.lucene.store.Directory)

Example 32 with ImmutableOpenMap

use of org.elasticsearch.common.collect.ImmutableOpenMap in project elasticsearch by elastic.

the class ESIntegTestCase method assertMappingOnMaster.

/**
     * Waits for the given mapping type to exists on the master node.
     */
public void assertMappingOnMaster(final String index, final String type, final String... fieldNames) throws Exception {
    GetMappingsResponse response = client().admin().indices().prepareGetMappings(index).setTypes(type).get();
    ImmutableOpenMap<String, MappingMetaData> mappings = response.getMappings().get(index);
    assertThat(mappings, notNullValue());
    MappingMetaData mappingMetaData = mappings.get(type);
    assertThat(mappingMetaData, notNullValue());
    Map<String, Object> mappingSource = mappingMetaData.getSourceAsMap();
    assertFalse(mappingSource.isEmpty());
    assertTrue(mappingSource.containsKey("properties"));
    for (String fieldName : fieldNames) {
        Map<String, Object> mappingProperties = (Map<String, Object>) mappingSource.get("properties");
        if (fieldName.indexOf('.') != -1) {
            fieldName = fieldName.replace(".", ".properties.");
        }
        assertThat("field " + fieldName + " doesn't exists in mapping " + mappingMetaData.source().string(), XContentMapValues.extractValue(fieldName, mappingProperties), notNullValue());
    }
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map) XContentTestUtils.convertToMap(org.elasticsearch.test.XContentTestUtils.convertToMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 33 with ImmutableOpenMap

use of org.elasticsearch.common.collect.ImmutableOpenMap in project elasticsearch by elastic.

the class SnapshotsService method processWaitingShards.

private ImmutableOpenMap<ShardId, ShardSnapshotStatus> processWaitingShards(ImmutableOpenMap<ShardId, ShardSnapshotStatus> snapshotShards, RoutingTable routingTable) {
    boolean snapshotChanged = false;
    ImmutableOpenMap.Builder<ShardId, ShardSnapshotStatus> shards = ImmutableOpenMap.builder();
    for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : snapshotShards) {
        ShardSnapshotStatus shardStatus = shardEntry.value;
        ShardId shardId = shardEntry.key;
        if (shardStatus.state() == State.WAITING) {
            IndexRoutingTable indexShardRoutingTable = routingTable.index(shardId.getIndex());
            if (indexShardRoutingTable != null) {
                IndexShardRoutingTable shardRouting = indexShardRoutingTable.shard(shardId.id());
                if (shardRouting != null && shardRouting.primaryShard() != null) {
                    if (shardRouting.primaryShard().started()) {
                        // Shard that we were waiting for has started on a node, let's process it
                        snapshotChanged = true;
                        logger.trace("starting shard that we were waiting for [{}] on node [{}]", shardId, shardStatus.nodeId());
                        shards.put(shardId, new ShardSnapshotStatus(shardRouting.primaryShard().currentNodeId()));
                        continue;
                    } else if (shardRouting.primaryShard().initializing() || shardRouting.primaryShard().relocating()) {
                        // Shard that we were waiting for hasn't started yet or still relocating - will continue to wait
                        shards.put(shardId, shardStatus);
                        continue;
                    }
                }
            }
            // Shard that we were waiting for went into unassigned state or disappeared - giving up
            snapshotChanged = true;
            logger.warn("failing snapshot of shard [{}] on unassigned shard [{}]", shardId, shardStatus.nodeId());
            shards.put(shardId, new ShardSnapshotStatus(shardStatus.nodeId(), State.FAILED, "shard is unassigned"));
        } else {
            shards.put(shardId, shardStatus);
        }
    }
    if (snapshotChanged) {
        return shards.build();
    } else {
        return null;
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

Example 34 with ImmutableOpenMap

use of org.elasticsearch.common.collect.ImmutableOpenMap in project elasticsearch by elastic.

the class SnapshotsService method shards.

/**
     * Calculates the list of shards that should be included into the current snapshot
     *
     * @param clusterState cluster state
     * @param indices      list of indices to be snapshotted
     * @return list of shard to be included into current snapshot
     */
private ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards(ClusterState clusterState, List<IndexId> indices) {
    ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
    MetaData metaData = clusterState.metaData();
    for (IndexId index : indices) {
        final String indexName = index.getName();
        IndexMetaData indexMetaData = metaData.index(indexName);
        if (indexMetaData == null) {
            // The index was deleted before we managed to start the snapshot - mark it as missing.
            builder.put(new ShardId(indexName, IndexMetaData.INDEX_UUID_NA_VALUE, 0), new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "missing index"));
        } else if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
            for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
                ShardId shardId = new ShardId(indexMetaData.getIndex(), i);
                builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "index is closed"));
            }
        } else {
            IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(indexName);
            for (int i = 0; i < indexMetaData.getNumberOfShards(); i++) {
                ShardId shardId = new ShardId(indexMetaData.getIndex(), i);
                if (indexRoutingTable != null) {
                    ShardRouting primary = indexRoutingTable.shard(i).primaryShard();
                    if (primary == null || !primary.assignedToNode()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "primary shard is not allocated"));
                    } else if (primary.relocating() || primary.initializing()) {
                        // The WAITING state was introduced in V1.2.0 - don't use it if there are nodes with older version in the cluster
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), State.WAITING));
                    } else if (!primary.started()) {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId(), State.MISSING, "primary shard hasn't been started yet"));
                    } else {
                        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(primary.currentNodeId()));
                    }
                } else {
                    builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(null, State.MISSING, "missing routing table"));
                }
            }
        }
    }
    return builder.build();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexId(org.elasticsearch.repositories.IndexId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 35 with ImmutableOpenMap

use of org.elasticsearch.common.collect.ImmutableOpenMap in project elasticsearch by elastic.

the class SnapshotsInProgressSerializationTests method randomSnapshot.

private Entry randomSnapshot() {
    Snapshot snapshot = new Snapshot(randomAsciiOfLength(10), new SnapshotId(randomAsciiOfLength(10), randomAsciiOfLength(10)));
    boolean includeGlobalState = randomBoolean();
    boolean partial = randomBoolean();
    State state = randomFrom(State.values());
    int numberOfIndices = randomIntBetween(0, 10);
    List<IndexId> indices = new ArrayList<>();
    for (int i = 0; i < numberOfIndices; i++) {
        indices.add(new IndexId(randomAsciiOfLength(10), randomAsciiOfLength(10)));
    }
    long startTime = randomLong();
    long repositoryStateId = randomLong();
    ImmutableOpenMap.Builder<ShardId, SnapshotsInProgress.ShardSnapshotStatus> builder = ImmutableOpenMap.builder();
    int shardsCount = randomIntBetween(0, 10);
    for (int j = 0; j < shardsCount; j++) {
        ShardId shardId = new ShardId(new Index(randomAsciiOfLength(10), randomAsciiOfLength(10)), randomIntBetween(0, 10));
        String nodeId = randomAsciiOfLength(10);
        State shardState = randomFrom(State.values());
        builder.put(shardId, new SnapshotsInProgress.ShardSnapshotStatus(nodeId, shardState));
    }
    ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards = builder.build();
    return new Entry(snapshot, includeGlobalState, partial, state, indices, startTime, repositoryStateId, shards);
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) Entry(org.elasticsearch.cluster.SnapshotsInProgress.Entry) State(org.elasticsearch.cluster.SnapshotsInProgress.State) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress)

Aggregations

ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)38 Settings (org.elasticsearch.common.settings.Settings)16 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)15 ClusterState (org.elasticsearch.cluster.ClusterState)14 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)13 MetaData (org.elasticsearch.cluster.metadata.MetaData)10 IOException (java.io.IOException)9 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)9 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)9 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)9 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)8 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)8 ShardId (org.elasticsearch.index.shard.ShardId)8 Map (java.util.Map)7 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)7 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)7 DiskUsage (org.elasticsearch.cluster.DiskUsage)7 DevNullClusterInfo (org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo)7 DiskThresholdSettings (org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)7 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)7