Search in sources :

Example 1 with ShardPath

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

the class IndexStoreTests method doTestStoreDirectory.

private void doTestStoreDirectory(Index index, Path tempDir, String typeSettingValue, IndexModule.Type type) throws IOException {
    Settings.Builder settingsBuilder = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT);
    if (typeSettingValue != null) {
        settingsBuilder.put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), typeSettingValue);
    }
    Settings settings = settingsBuilder.build();
    IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
    FsDirectoryService service = new FsDirectoryService(indexSettings, null, new ShardPath(false, tempDir, tempDir, new ShardId(index, 0)));
    try (Directory directory = service.newFSDirectory(tempDir, NoLockFactory.INSTANCE)) {
        switch(type) {
            case NIOFS:
                assertTrue(type + " " + directory.toString(), directory instanceof NIOFSDirectory);
                break;
            case MMAPFS:
                assertTrue(type + " " + directory.toString(), directory instanceof MMapDirectory);
                break;
            case SIMPLEFS:
                assertTrue(type + " " + directory.toString(), directory instanceof SimpleFSDirectory);
                break;
            case FS:
                if (Constants.JRE_IS_64BIT && MMapDirectory.UNMAP_SUPPORTED) {
                    assertTrue(directory.toString(), directory instanceof MMapDirectory);
                } else if (Constants.WINDOWS) {
                    assertTrue(directory.toString(), directory instanceof SimpleFSDirectory);
                } else {
                    assertTrue(directory.toString(), directory instanceof NIOFSDirectory);
                }
                break;
            default:
                fail();
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexSettings(org.elasticsearch.index.IndexSettings) MMapDirectory(org.apache.lucene.store.MMapDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Example 2 with ShardPath

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

the class TransportNodesListShardStoreMetaData method listStoreMetaData.

private StoreFilesMetaData listStoreMetaData(ShardId shardId) throws IOException {
    logger.trace("listing store meta data for {}", shardId);
    long startTimeNS = System.nanoTime();
    boolean exists = false;
    try {
        IndexService indexService = indicesService.indexService(shardId.getIndex());
        if (indexService != null) {
            IndexShard indexShard = indexService.getShardOrNull(shardId.id());
            if (indexShard != null) {
                exists = true;
                return new StoreFilesMetaData(shardId, indexShard.snapshotStoreMetadata());
            }
        }
        // try and see if we an list unallocated
        IndexMetaData metaData = clusterService.state().metaData().index(shardId.getIndex());
        if (metaData == null) {
            // we may send this requests while processing the cluster state that recovered the index
            // sometimes the request comes in before the local node processed that cluster state
            // in such cases we can load it from disk
            metaData = IndexMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnv.indexPaths(shardId.getIndex()));
        }
        if (metaData == null) {
            logger.trace("{} node doesn't have meta data for the requests index, responding with empty", shardId);
            return new StoreFilesMetaData(shardId, Store.MetadataSnapshot.EMPTY);
        }
        final IndexSettings indexSettings = indexService != null ? indexService.getIndexSettings() : new IndexSettings(metaData, settings);
        final ShardPath shardPath = ShardPath.loadShardPath(logger, nodeEnv, shardId, indexSettings);
        if (shardPath == null) {
            return new StoreFilesMetaData(shardId, Store.MetadataSnapshot.EMPTY);
        }
        //    reuse local resources.
        return new StoreFilesMetaData(shardId, Store.readMetadataSnapshot(shardPath.resolveIndex(), shardId, nodeEnv::shardLock, logger));
    } finally {
        TimeValue took = new TimeValue(System.nanoTime() - startTimeNS, TimeUnit.NANOSECONDS);
        if (exists) {
            logger.debug("{} loaded store meta data (took [{}])", shardId, took);
        } else {
            logger.trace("{} didn't find any store meta data to load (took [{}])", shardId, took);
        }
    }
}
Also used : IndexService(org.elasticsearch.index.IndexService) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexSettings(org.elasticsearch.index.IndexSettings) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 3 with ShardPath

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

the class RestIndicesActionTests method randomIndicesStatsResponse.

private IndicesStatsResponse randomIndicesStatsResponse(final Index[] indices) {
    List<ShardStats> shardStats = new ArrayList<>();
    for (final Index index : indices) {
        for (int i = 0; i < 2; i++) {
            ShardId shardId = new ShardId(index, i);
            Path path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve(String.valueOf(i));
            ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, i == 0, i == 0 ? StoreRecoverySource.EMPTY_STORE_INSTANCE : PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null));
            shardRouting = shardRouting.initialize("node-0", null, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE);
            shardRouting = shardRouting.moveToStarted();
            CommonStats stats = new CommonStats();
            stats.fieldData = new FieldDataStats();
            stats.queryCache = new QueryCacheStats();
            stats.docs = new DocsStats();
            stats.store = new StoreStats();
            stats.indexing = new IndexingStats();
            stats.search = new SearchStats();
            stats.segments = new SegmentsStats();
            stats.merge = new MergeStats();
            stats.refresh = new RefreshStats();
            stats.completion = new CompletionStats();
            stats.requestCache = new RequestCacheStats();
            stats.get = new GetStats();
            stats.flush = new FlushStats();
            stats.warmer = new WarmerStats();
            shardStats.add(new ShardStats(shardRouting, new ShardPath(false, path, path, shardId), stats, null, null));
        }
    }
    return IndicesStatsTests.newIndicesStatsResponse(shardStats.toArray(new ShardStats[shardStats.size()]), shardStats.size(), shardStats.size(), 0, emptyList());
}
Also used : StoreStats(org.elasticsearch.index.store.StoreStats) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) RefreshStats(org.elasticsearch.index.refresh.RefreshStats) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) GetStats(org.elasticsearch.index.get.GetStats) SegmentsStats(org.elasticsearch.index.engine.SegmentsStats) ShardId(org.elasticsearch.index.shard.ShardId) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) FlushStats(org.elasticsearch.index.flush.FlushStats) ShardPath(org.elasticsearch.index.shard.ShardPath) QueryCacheStats(org.elasticsearch.index.cache.query.QueryCacheStats) DocsStats(org.elasticsearch.index.shard.DocsStats) FieldDataStats(org.elasticsearch.index.fielddata.FieldDataStats) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexingStats(org.elasticsearch.index.shard.IndexingStats) WarmerStats(org.elasticsearch.index.warmer.WarmerStats) SearchStats(org.elasticsearch.index.search.stats.SearchStats) MergeStats(org.elasticsearch.index.merge.MergeStats) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RequestCacheStats(org.elasticsearch.index.cache.request.RequestCacheStats) CompletionStats(org.elasticsearch.search.suggest.completion.CompletionStats)

Example 4 with ShardPath

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

the class TransportNodesListGatewayStartedShards method nodeOperation.

@Override
protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
    try {
        final ShardId shardId = request.getShardId();
        logger.trace("{} loading local shard state info", shardId);
        ShardStateMetaData shardStateMetaData = ShardStateMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnv.availableShardPaths(request.shardId));
        if (shardStateMetaData != null) {
            IndexMetaData metaData = clusterService.state().metaData().index(shardId.getIndex());
            if (metaData == null) {
                // we may send this requests while processing the cluster state that recovered the index
                // sometimes the request comes in before the local node processed that cluster state
                // in such cases we can load it from disk
                metaData = IndexMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnv.indexPaths(shardId.getIndex()));
            }
            if (metaData == null) {
                ElasticsearchException e = new ElasticsearchException("failed to find local IndexMetaData");
                e.setShard(request.shardId);
                throw e;
            }
            if (indicesService.getShardOrNull(shardId) == null) {
                // we don't have an open shard on the store, validate the files on disk are openable
                ShardPath shardPath = null;
                try {
                    IndexSettings indexSettings = new IndexSettings(metaData, settings);
                    shardPath = ShardPath.loadShardPath(logger, nodeEnv, shardId, indexSettings);
                    if (shardPath == null) {
                        throw new IllegalStateException(shardId + " no shard path found");
                    }
                    Store.tryOpenIndex(shardPath.resolveIndex(), shardId, nodeEnv::shardLock, logger);
                } catch (Exception exception) {
                    final ShardPath finalShardPath = shardPath;
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("{} can't open index for shard [{}] in path [{}]", shardId, shardStateMetaData, (finalShardPath != null) ? finalShardPath.resolveIndex() : ""), exception);
                    String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
                    return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetaData.primary, exception);
                }
            }
            logger.debug("{} shard state info found: [{}]", shardId, shardStateMetaData);
            String allocationId = shardStateMetaData.allocationId != null ? shardStateMetaData.allocationId.getId() : null;
            return new NodeGatewayStartedShards(clusterService.localNode(), allocationId, shardStateMetaData.primary);
        }
        logger.trace("{} no local shard info found", shardId);
        return new NodeGatewayStartedShards(clusterService.localNode(), null, false);
    } catch (Exception e) {
        throw new ElasticsearchException("failed to load started shards", e);
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FailedNodeException(org.elasticsearch.action.FailedNodeException) IOException(java.io.IOException) ShardStateMetaData(org.elasticsearch.index.shard.ShardStateMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 5 with ShardPath

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

the class NodeEnvironment method findAllShardsForIndex.

private static Set<ShardId> findAllShardsForIndex(Path indexPath, Index index) throws IOException {
    assert indexPath.getFileName().toString().equals(index.getUUID());
    Set<ShardId> shardIds = new HashSet<>();
    if (Files.isDirectory(indexPath)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
            for (Path shardPath : stream) {
                String fileName = shardPath.getFileName().toString();
                if (Files.isDirectory(shardPath) && fileName.chars().allMatch(Character::isDigit)) {
                    int shardId = Integer.parseInt(fileName);
                    ShardId id = new ShardId(index, shardId);
                    shardIds.add(id);
                }
            }
        }
    }
    return shardIds;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) HashSet(java.util.HashSet)

Aggregations

ShardPath (org.elasticsearch.index.shard.ShardPath)17 ShardId (org.elasticsearch.index.shard.ShardId)14 Path (java.nio.file.Path)12 IndexSettings (org.elasticsearch.index.IndexSettings)7 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)6 Settings (org.elasticsearch.common.settings.Settings)5 Index (org.elasticsearch.index.Index)5 Directory (org.apache.lucene.store.Directory)4 MMapDirectory (org.apache.lucene.store.MMapDirectory)4 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 FileSwitchDirectory (org.apache.lucene.store.FileSwitchDirectory)3 SleepingLockWrapper (org.apache.lucene.store.SleepingLockWrapper)3 TimeValue (org.elasticsearch.common.unit.TimeValue)3 IndexService (org.elasticsearch.index.IndexService)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 Supplier (org.apache.logging.log4j.util.Supplier)2