Java Code Examples for org.elasticsearch.index.IndexService#shard()

The following examples show how to use org.elasticsearch.index.IndexService#shard() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndicesClusterStateService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void onFailedEngine(final ShardId shardId, final String reason, final @Nullable Throwable failure) {
    ShardRouting shardRouting = null;
    final IndexService indexService = indicesService.indexService(shardId.index().name());
    if (indexService != null) {
        IndexShard indexShard = indexService.shard(shardId.id());
        if (indexShard != null) {
            shardRouting = indexShard.routingEntry();
        }
    }
    if (shardRouting == null) {
        logger.warn("[{}][{}] engine failed, but can't find index shard. failure reason: [{}]", failure,
                shardId.index().name(), shardId.id(), reason);
        return;
    }
    final ShardRouting fShardRouting = shardRouting;
    threadPool.generic().execute(new Runnable() {
        @Override
        public void run() {
            synchronized (mutex) {
                failAndRemoveShard(fShardRouting, indexService, true, "engine failure, reason [" + reason + "]", failure);
            }
        }
    });
}
 
Example 2
Source File: TransportReplicaShardIngestAction.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
private void failReplicaIfNeeded(String index, int shardId, Throwable t) {
    if (!ignoreReplicaException(t)) {
        IndexService indexService = indicesService.indexService(index);
        if (indexService == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("ignoring failed replica [{}][{}] because index was already removed", index, shardId);
            }
            return;
        }
        IndexShard indexShard = indexService.shard(shardId);
        if (indexShard == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("ignoring failed replica [{}][{}] because index was already removed", index, shardId);
            }
            return;
        }
        indexShard.failShard(transportAction + " failed on replica", t);
    }
}
 
Example 3
Source File: IndicesStore.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private IndexShard getShard(ShardActiveRequest request) {
    ClusterName thisClusterName = clusterService.state().getClusterName();
    if (!thisClusterName.equals(request.clusterName)) {
        logger.trace("shard exists request meant for cluster[{}], but this is cluster[{}], ignoring request", request.clusterName, thisClusterName);
        return null;
    }

    ShardId shardId = request.shardId;
    IndexService indexService = indicesService.indexService(shardId.index().getName());
    if (indexService != null && indexService.indexUUID().equals(request.indexUUID)) {
        return indexService.shard(shardId.id());
    }
    return null;
}
 
Example 4
Source File: IndicesWarmer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void warmInternal(final WarmerContext context, boolean topReader) {
    final IndexMetaData indexMetaData = clusterService.state().metaData().index(context.shardId().index().name());
    if (indexMetaData == null) {
        return;
    }
    if (!indexMetaData.getSettings().getAsBoolean(INDEX_WARMER_ENABLED, settings.getAsBoolean(INDEX_WARMER_ENABLED, true))) {
        return;
    }
    IndexService indexService = indicesService.indexService(context.shardId().index().name());
    if (indexService == null) {
        return;
    }
    final IndexShard indexShard = indexService.shard(context.shardId().id());
    if (indexShard == null) {
        return;
    }
    if (logger.isTraceEnabled()) {
        if (topReader) {
            logger.trace("[{}][{}] top warming [{}]", context.shardId().index().name(), context.shardId().id(), context);
        } else {
            logger.trace("[{}][{}] warming [{}]", context.shardId().index().name(), context.shardId().id(), context);
        }
    }
    indexShard.warmerService().onPreWarm();
    long time = System.nanoTime();
    final List<TerminationHandle> terminationHandles = new ArrayList<>();
    // get a handle on pending tasks
    for (final Listener listener : listeners) {
        if (topReader) {
            terminationHandles.add(listener.warmTopReader(indexShard, indexMetaData, context, threadPool));
        } else {
            terminationHandles.add(listener.warmNewReaders(indexShard, indexMetaData, context, threadPool));
        }
    }
    // wait for termination
    for (TerminationHandle terminationHandle : terminationHandles) {
        try {
            terminationHandle.awaitTermination();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            if (topReader) {
                logger.warn("top warming has been interrupted", e);
            } else {
                logger.warn("warming has been interrupted", e);
            }
            break;
        }
    }
    long took = System.nanoTime() - time;
    indexShard.warmerService().onPostWarm(took);
    if (indexShard.warmerService().logger().isTraceEnabled()) {
        if (topReader) {
            indexShard.warmerService().logger().trace("top warming took [{}]", new TimeValue(took, TimeUnit.NANOSECONDS));
        } else {
            indexShard.warmerService().logger().trace("warming took [{}]", new TimeValue(took, TimeUnit.NANOSECONDS));
        }
    }
}
 
Example 5
Source File: TransportNodesListShardStoreMetaData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
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.index().name());
        if (indexService != null) {
            IndexShard indexShard = indexService.shard(shardId.id());
            if (indexShard != null) {
                final Store store = indexShard.store();
                store.incRef();
                try {
                    exists = true;
                    return new StoreFilesMetaData(true, shardId, store.getMetadataOrEmpty());
                } finally {
                    store.decRef();
                }
            }
        }
        // try and see if we an list unallocated
        IndexMetaData metaData = clusterService.state().metaData().index(shardId.index().name());
        if (metaData == null) {
            return new StoreFilesMetaData(false, shardId, Store.MetadataSnapshot.EMPTY);
        }
        String storeType = metaData.getSettings().get(IndexStoreModule.STORE_TYPE, "fs");
        if (!storeType.contains("fs")) {
            return new StoreFilesMetaData(false, shardId, Store.MetadataSnapshot.EMPTY);
        }
        final ShardPath shardPath = ShardPath.loadShardPath(logger, nodeEnv, shardId, metaData.getSettings());
        if (shardPath == null) {
            return new StoreFilesMetaData(false, shardId, Store.MetadataSnapshot.EMPTY);
        }
        return new StoreFilesMetaData(false, shardId, Store.readMetadataSnapshot(shardPath.resolveIndex(), 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);
        }
    }
}