2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2011-12-06 02:42:25 +02:00
|
|
|
* Licensed to ElasticSearch and Shay Banon under one
|
2010-02-08 15:30:06 +02:00
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
2011-12-06 02:42:25 +02:00
|
|
|
* regarding copyright ownership. ElasticSearch licenses this
|
2010-02-08 15:30:06 +02:00
|
|
|
* file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2010-02-25 20:20:54 +02:00
|
|
|
package org.elasticsearch.index.service;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
import com.google.common.collect.UnmodifiableIterator;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.ElasticSearchException;
|
2011-02-01 23:10:15 +02:00
|
|
|
import org.elasticsearch.ElasticSearchIllegalStateException;
|
2010-07-19 02:17:59 +03:00
|
|
|
import org.elasticsearch.ElasticSearchInterruptedException;
|
2011-06-10 03:48:43 +03:00
|
|
|
import org.elasticsearch.common.Nullable;
|
2011-12-06 02:42:25 +02:00
|
|
|
import org.elasticsearch.common.inject.*;
|
2010-11-03 15:34:30 +02:00
|
|
|
import org.elasticsearch.common.io.FileSystemUtils;
|
2010-06-15 16:51:38 +03:00
|
|
|
import org.elasticsearch.common.settings.Settings;
|
2010-11-03 15:34:30 +02:00
|
|
|
import org.elasticsearch.env.NodeEnvironment;
|
2010-06-24 00:32:05 +03:00
|
|
|
import org.elasticsearch.gateway.none.NoneGateway;
|
2011-12-06 02:42:25 +02:00
|
|
|
import org.elasticsearch.index.*;
|
2011-05-21 19:53:17 -04:00
|
|
|
import org.elasticsearch.index.aliases.IndexAliasesService;
|
2010-11-21 17:33:35 +02:00
|
|
|
import org.elasticsearch.index.analysis.AnalysisService;
|
2010-04-01 12:25:51 +03:00
|
|
|
import org.elasticsearch.index.cache.IndexCache;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.deletionpolicy.DeletionPolicyModule;
|
|
|
|
import org.elasticsearch.index.engine.Engine;
|
|
|
|
import org.elasticsearch.index.engine.EngineModule;
|
2010-05-19 20:41:29 +03:00
|
|
|
import org.elasticsearch.index.engine.IndexEngine;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.gateway.IndexGateway;
|
|
|
|
import org.elasticsearch.index.gateway.IndexShardGatewayModule;
|
|
|
|
import org.elasticsearch.index.gateway.IndexShardGatewayService;
|
2011-08-30 19:53:07 +03:00
|
|
|
import org.elasticsearch.index.get.ShardGetModule;
|
2011-08-24 11:24:52 +03:00
|
|
|
import org.elasticsearch.index.indexing.ShardIndexingModule;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.mapper.MapperService;
|
|
|
|
import org.elasticsearch.index.merge.policy.MergePolicyModule;
|
2011-03-08 21:53:33 +02:00
|
|
|
import org.elasticsearch.index.merge.policy.MergePolicyProvider;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.merge.scheduler.MergeSchedulerModule;
|
2011-01-13 16:20:31 +02:00
|
|
|
import org.elasticsearch.index.percolator.PercolatorService;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.query.IndexQueryParserService;
|
2011-09-04 23:55:35 +03:00
|
|
|
import org.elasticsearch.index.search.stats.ShardSearchModule;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.settings.IndexSettings;
|
2011-08-14 14:51:04 +03:00
|
|
|
import org.elasticsearch.index.shard.IndexShardCreationException;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.shard.IndexShardManagement;
|
|
|
|
import org.elasticsearch.index.shard.IndexShardModule;
|
|
|
|
import org.elasticsearch.index.shard.ShardId;
|
2010-02-25 18:40:29 +02:00
|
|
|
import org.elasticsearch.index.shard.service.IndexShard;
|
2010-12-23 10:56:37 +02:00
|
|
|
import org.elasticsearch.index.shard.service.InternalIndexShard;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.similarity.SimilarityService;
|
2010-06-23 07:47:18 +03:00
|
|
|
import org.elasticsearch.index.store.IndexStore;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.store.Store;
|
|
|
|
import org.elasticsearch.index.store.StoreModule;
|
2010-07-05 14:13:55 +03:00
|
|
|
import org.elasticsearch.index.translog.Translog;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.translog.TranslogModule;
|
2010-08-11 12:54:00 +03:00
|
|
|
import org.elasticsearch.index.translog.TranslogService;
|
2010-04-07 21:22:43 +03:00
|
|
|
import org.elasticsearch.indices.IndicesLifecycle;
|
|
|
|
import org.elasticsearch.indices.InternalIndicesLifecycle;
|
2010-03-27 23:43:59 +03:00
|
|
|
import org.elasticsearch.plugins.PluginsService;
|
|
|
|
import org.elasticsearch.plugins.ShardsPluginsModule;
|
2010-07-19 02:17:59 +03:00
|
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2010-07-19 02:17:59 +03:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2011-06-10 03:48:43 +03:00
|
|
|
import java.util.concurrent.Executor;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
import static com.google.common.collect.Maps.newHashMap;
|
|
|
|
import static org.elasticsearch.common.collect.MapBuilder.newMapBuilder;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-06 02:42:25 +02:00
|
|
|
*
|
2010-02-08 15:30:06 +02:00
|
|
|
*/
|
|
|
|
public class InternalIndexService extends AbstractIndexComponent implements IndexService {
|
|
|
|
|
|
|
|
private final Injector injector;
|
|
|
|
|
|
|
|
private final Settings indexSettings;
|
|
|
|
|
2010-11-03 15:34:30 +02:00
|
|
|
private final NodeEnvironment nodeEnv;
|
|
|
|
|
2010-07-19 02:17:59 +03:00
|
|
|
private final ThreadPool threadPool;
|
|
|
|
|
2010-03-27 23:43:59 +03:00
|
|
|
private final PluginsService pluginsService;
|
|
|
|
|
2010-04-07 21:22:43 +03:00
|
|
|
private final InternalIndicesLifecycle indicesLifecycle;
|
|
|
|
|
2011-01-13 16:20:31 +02:00
|
|
|
private final PercolatorService percolatorService;
|
|
|
|
|
2010-11-21 17:33:35 +02:00
|
|
|
private final AnalysisService analysisService;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
private final MapperService mapperService;
|
|
|
|
|
|
|
|
private final IndexQueryParserService queryParserService;
|
|
|
|
|
|
|
|
private final SimilarityService similarityService;
|
|
|
|
|
2011-05-21 19:53:17 -04:00
|
|
|
private final IndexAliasesService aliasesService;
|
|
|
|
|
2010-04-01 12:25:51 +03:00
|
|
|
private final IndexCache indexCache;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-05-19 20:41:29 +03:00
|
|
|
private final IndexEngine indexEngine;
|
|
|
|
|
2010-06-24 00:32:05 +03:00
|
|
|
private final IndexGateway indexGateway;
|
|
|
|
|
|
|
|
private final IndexStore indexStore;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
private volatile ImmutableMap<Integer, Injector> shardsInjectors = ImmutableMap.of();
|
|
|
|
|
|
|
|
private volatile ImmutableMap<Integer, IndexShard> shards = ImmutableMap.of();
|
|
|
|
|
2011-02-01 23:10:15 +02:00
|
|
|
private volatile boolean closed = false;
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Inject
|
|
|
|
public InternalIndexService(Injector injector, Index index, @IndexSettings Settings indexSettings, NodeEnvironment nodeEnv, ThreadPool threadPool,
|
|
|
|
PercolatorService percolatorService, AnalysisService analysisService, MapperService mapperService,
|
|
|
|
IndexQueryParserService queryParserService, SimilarityService similarityService, IndexAliasesService aliasesService,
|
|
|
|
IndexCache indexCache, IndexEngine indexEngine, IndexGateway indexGateway, IndexStore indexStore) {
|
2010-02-08 15:30:06 +02:00
|
|
|
super(index, indexSettings);
|
|
|
|
this.injector = injector;
|
2010-11-03 15:34:30 +02:00
|
|
|
this.nodeEnv = nodeEnv;
|
2010-07-19 02:17:59 +03:00
|
|
|
this.threadPool = threadPool;
|
2010-02-08 15:30:06 +02:00
|
|
|
this.indexSettings = indexSettings;
|
2011-01-13 16:20:31 +02:00
|
|
|
this.percolatorService = percolatorService;
|
2010-11-21 17:33:35 +02:00
|
|
|
this.analysisService = analysisService;
|
2010-02-08 15:30:06 +02:00
|
|
|
this.mapperService = mapperService;
|
|
|
|
this.queryParserService = queryParserService;
|
|
|
|
this.similarityService = similarityService;
|
2011-05-21 19:53:17 -04:00
|
|
|
this.aliasesService = aliasesService;
|
2010-04-01 12:25:51 +03:00
|
|
|
this.indexCache = indexCache;
|
2010-05-19 20:41:29 +03:00
|
|
|
this.indexEngine = indexEngine;
|
2010-06-24 00:32:05 +03:00
|
|
|
this.indexGateway = indexGateway;
|
|
|
|
this.indexStore = indexStore;
|
2010-03-27 23:43:59 +03:00
|
|
|
|
|
|
|
this.pluginsService = injector.getInstance(PluginsService.class);
|
2010-04-07 21:22:43 +03:00
|
|
|
this.indicesLifecycle = (InternalIndicesLifecycle) injector.getInstance(IndicesLifecycle.class);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public int numberOfShards() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return shards.size();
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public UnmodifiableIterator<IndexShard> iterator() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return shards.values().iterator();
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasShard(int shardId) {
|
2010-02-08 15:30:06 +02:00
|
|
|
return shards.containsKey(shardId);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexShard shard(int shardId) {
|
2010-02-08 15:30:06 +02:00
|
|
|
return shards.get(shardId);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexShard shardSafe(int shardId) throws IndexShardMissingException {
|
2010-02-08 15:30:06 +02:00
|
|
|
IndexShard indexShard = shard(shardId);
|
|
|
|
if (indexShard == null) {
|
|
|
|
throw new IndexShardMissingException(new ShardId(index, shardId));
|
|
|
|
}
|
|
|
|
return indexShard;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public ImmutableSet<Integer> shardIds() {
|
2010-07-19 02:17:59 +03:00
|
|
|
return ImmutableSet.copyOf(shards.keySet());
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Injector injector() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return injector;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexGateway gateway() {
|
2010-07-05 14:13:55 +03:00
|
|
|
return indexGateway;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexStore store() {
|
2010-06-27 18:19:12 +03:00
|
|
|
return indexStore;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexCache cache() {
|
2010-04-01 12:25:51 +03:00
|
|
|
return indexCache;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public PercolatorService percolateService() {
|
2011-01-13 16:20:31 +02:00
|
|
|
return this.percolatorService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public AnalysisService analysisService() {
|
2010-11-21 17:33:35 +02:00
|
|
|
return this.analysisService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public MapperService mapperService() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return mapperService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexQueryParserService queryParserService() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return queryParserService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public SimilarityService similarityService() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return similarityService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexAliasesService aliasesService() {
|
2011-05-21 19:53:17 -04:00
|
|
|
return aliasesService;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public IndexEngine engine() {
|
2010-05-19 20:41:29 +03:00
|
|
|
return indexEngine;
|
|
|
|
}
|
|
|
|
|
2011-06-10 03:48:43 +03:00
|
|
|
public void close(final boolean delete, final String reason, @Nullable Executor executor) {
|
2011-02-01 23:10:15 +02:00
|
|
|
synchronized (this) {
|
|
|
|
closed = true;
|
|
|
|
}
|
2011-04-09 14:20:18 +03:00
|
|
|
Set<Integer> shardIds = shardIds();
|
|
|
|
final CountDownLatch latch = new CountDownLatch(shardIds.size());
|
|
|
|
for (final int shardId : shardIds) {
|
2011-06-10 03:48:43 +03:00
|
|
|
executor = executor == null ? threadPool.cached() : executor;
|
|
|
|
executor.execute(new Runnable() {
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2011-04-09 14:20:18 +03:00
|
|
|
try {
|
|
|
|
deleteShard(shardId, delete, !delete, delete, reason);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("failed to close shard, delete [{}]", e, delete);
|
|
|
|
} finally {
|
|
|
|
latch.countDown();
|
2010-07-19 02:17:59 +03:00
|
|
|
}
|
2011-04-09 14:20:18 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
latch.await();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new ElasticSearchInterruptedException("interrupted closing index [ " + index().name() + "]", e);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Injector shardInjector(int shardId) throws ElasticSearchException {
|
2010-02-08 15:30:06 +02:00
|
|
|
return shardsInjectors.get(shardId);
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Injector shardInjectorSafe(int shardId) throws IndexShardMissingException {
|
2010-02-08 15:30:06 +02:00
|
|
|
Injector shardInjector = shardInjector(shardId);
|
|
|
|
if (shardInjector == null) {
|
|
|
|
throw new IndexShardMissingException(new ShardId(index, shardId));
|
|
|
|
}
|
|
|
|
return shardInjector;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public synchronized IndexShard createShard(int sShardId) throws ElasticSearchException {
|
2011-02-01 23:10:15 +02:00
|
|
|
if (closed) {
|
|
|
|
throw new ElasticSearchIllegalStateException("Can't create shard [" + index.name() + "][" + sShardId + "], closed");
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
ShardId shardId = new ShardId(index, sShardId);
|
|
|
|
if (shardsInjectors.containsKey(shardId.id())) {
|
|
|
|
throw new IndexShardAlreadyExistsException(shardId + " already exists");
|
|
|
|
}
|
|
|
|
|
2010-04-07 21:22:43 +03:00
|
|
|
indicesLifecycle.beforeIndexShardCreated(shardId);
|
|
|
|
|
2010-06-24 00:32:05 +03:00
|
|
|
logger.debug("creating shard_id [{}]", shardId.id());
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-08-22 10:42:25 +03:00
|
|
|
ModulesBuilder modules = new ModulesBuilder();
|
2010-04-27 23:54:30 +03:00
|
|
|
modules.add(new ShardsPluginsModule(indexSettings, pluginsService));
|
2012-02-05 18:52:56 +02:00
|
|
|
modules.add(new IndexShardModule(indexSettings, shardId));
|
2011-08-24 11:24:52 +03:00
|
|
|
modules.add(new ShardIndexingModule());
|
2011-09-04 23:55:35 +03:00
|
|
|
modules.add(new ShardSearchModule());
|
2011-08-30 19:53:07 +03:00
|
|
|
modules.add(new ShardGetModule());
|
2010-06-23 07:47:18 +03:00
|
|
|
modules.add(new StoreModule(indexSettings, injector.getInstance(IndexStore.class)));
|
2010-04-27 23:54:30 +03:00
|
|
|
modules.add(new DeletionPolicyModule(indexSettings));
|
|
|
|
modules.add(new MergePolicyModule(indexSettings));
|
|
|
|
modules.add(new MergeSchedulerModule(indexSettings));
|
|
|
|
modules.add(new TranslogModule(indexSettings));
|
|
|
|
modules.add(new EngineModule(indexSettings));
|
|
|
|
modules.add(new IndexShardGatewayModule(injector.getInstance(IndexGateway.class)));
|
|
|
|
|
2011-08-14 14:51:04 +03:00
|
|
|
Injector shardInjector;
|
|
|
|
try {
|
|
|
|
shardInjector = modules.createChildInjector(injector);
|
|
|
|
} catch (CreationException e) {
|
|
|
|
throw new IndexShardCreationException(shardId, Injectors.getFirstErrorFailure(e));
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
shardsInjectors = newMapBuilder(shardsInjectors).put(shardId.id(), shardInjector).immutableMap();
|
|
|
|
|
|
|
|
IndexShard indexShard = shardInjector.getInstance(IndexShard.class);
|
|
|
|
|
2010-04-07 21:22:43 +03:00
|
|
|
indicesLifecycle.afterIndexShardCreated(indexShard);
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
shards = newMapBuilder(shards).put(shardId.id(), indexShard).immutableMap();
|
|
|
|
|
|
|
|
return indexShard;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public synchronized void cleanShard(int shardId, String reason) throws ElasticSearchException {
|
2010-12-23 10:56:37 +02:00
|
|
|
deleteShard(shardId, true, false, false, reason);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public synchronized void removeShard(int shardId, String reason) throws ElasticSearchException {
|
2010-12-23 10:56:37 +02:00
|
|
|
deleteShard(shardId, false, false, false, reason);
|
2010-11-14 20:09:27 +02:00
|
|
|
}
|
|
|
|
|
2010-12-23 10:56:37 +02:00
|
|
|
private void deleteShard(int shardId, boolean delete, boolean snapshotGateway, boolean deleteGateway, String reason) throws ElasticSearchException {
|
2010-07-19 02:17:59 +03:00
|
|
|
Injector shardInjector;
|
|
|
|
IndexShard indexShard;
|
|
|
|
synchronized (this) {
|
|
|
|
Map<Integer, Injector> tmpShardInjectors = newHashMap(shardsInjectors);
|
|
|
|
shardInjector = tmpShardInjectors.remove(shardId);
|
|
|
|
if (shardInjector == null) {
|
|
|
|
if (!delete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new IndexShardMissingException(new ShardId(index, shardId));
|
|
|
|
}
|
|
|
|
shardsInjectors = ImmutableMap.copyOf(tmpShardInjectors);
|
|
|
|
if (delete) {
|
|
|
|
logger.debug("deleting shard_id [{}]", shardId);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-07-19 02:17:59 +03:00
|
|
|
Map<Integer, IndexShard> tmpShardsMap = newHashMap(shards);
|
|
|
|
indexShard = tmpShardsMap.remove(shardId);
|
|
|
|
shards = ImmutableMap.copyOf(tmpShardsMap);
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-07-08 19:20:03 +03:00
|
|
|
ShardId sId = new ShardId(index, shardId);
|
|
|
|
|
|
|
|
indicesLifecycle.beforeIndexShardClosed(sId, indexShard, delete);
|
2010-03-27 23:43:59 +03:00
|
|
|
|
2010-03-31 22:45:46 +03:00
|
|
|
for (Class<? extends CloseableIndexComponent> closeable : pluginsService.shardServices()) {
|
2010-07-08 19:20:03 +03:00
|
|
|
try {
|
|
|
|
shardInjector.getInstance(closeable).close(delete);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.debug("failed to clean plugin shard service [{}]", e, closeable);
|
|
|
|
}
|
2010-03-27 23:43:59 +03:00
|
|
|
}
|
|
|
|
|
2010-08-11 12:54:00 +03:00
|
|
|
try {
|
|
|
|
// now we can close the translog service, we need to close it before the we close the shard
|
|
|
|
shardInjector.getInstance(TranslogService.class).close();
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close translog service", e);
|
2010-08-11 12:54:00 +03:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
// close shard actions
|
2010-07-08 19:20:03 +03:00
|
|
|
if (indexShard != null) {
|
|
|
|
shardInjector.getInstance(IndexShardManagement.class).close();
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-07-05 14:13:55 +03:00
|
|
|
// this logic is tricky, we want to close the engine so we rollback the changes done to it
|
|
|
|
// and close the shard so no operations are allowed to it
|
2010-07-08 19:20:03 +03:00
|
|
|
if (indexShard != null) {
|
2011-03-08 21:53:33 +02:00
|
|
|
try {
|
|
|
|
((InternalIndexShard) indexShard).close(reason);
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close index shard", e);
|
2011-03-08 21:53:33 +02:00
|
|
|
// ignore
|
|
|
|
}
|
2010-07-08 19:20:03 +03:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
shardInjector.getInstance(Engine.class).close();
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close engine", e);
|
2010-07-08 19:20:03 +03:00
|
|
|
// ignore
|
|
|
|
}
|
2010-08-20 00:59:37 +03:00
|
|
|
|
2011-03-08 21:53:33 +02:00
|
|
|
try {
|
|
|
|
shardInjector.getInstance(MergePolicyProvider.class).close(delete);
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close merge policy provider", e);
|
2011-03-08 21:53:33 +02:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:20:03 +03:00
|
|
|
try {
|
|
|
|
// now, we can snapshot to the gateway, it will be only the translog
|
2010-08-20 00:59:37 +03:00
|
|
|
if (snapshotGateway) {
|
|
|
|
shardInjector.getInstance(IndexShardGatewayService.class).snapshotOnClose();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to snapshot gateway on close", e);
|
2010-08-20 00:59:37 +03:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
2010-07-08 19:20:03 +03:00
|
|
|
shardInjector.getInstance(IndexShardGatewayService.class).close(deleteGateway);
|
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close index shard gateway", e);
|
2010-07-08 19:20:03 +03:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// now we can close the translog
|
2010-08-30 01:25:45 +03:00
|
|
|
shardInjector.getInstance(Translog.class).close(delete);
|
2010-07-08 19:20:03 +03:00
|
|
|
} catch (Exception e) {
|
2011-03-28 17:20:07 +02:00
|
|
|
logger.debug("failed to close translog", e);
|
2010-07-08 19:20:03 +03:00
|
|
|
// ignore
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-05-16 03:57:26 +03:00
|
|
|
// call this before we close the store, so we can release resources for it
|
2010-07-08 19:20:03 +03:00
|
|
|
indicesLifecycle.afterIndexShardClosed(sId, delete);
|
2010-05-16 03:57:26 +03:00
|
|
|
|
2010-06-24 00:32:05 +03:00
|
|
|
// if we delete or have no gateway or the store is not persistent, clean the store...
|
2010-02-08 15:30:06 +02:00
|
|
|
Store store = shardInjector.getInstance(Store.class);
|
2010-06-24 00:32:05 +03:00
|
|
|
if (delete || indexGateway.type().equals(NoneGateway.TYPE) || !indexStore.persistent()) {
|
|
|
|
try {
|
|
|
|
store.fullDelete();
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.warn("failed to clean store on shard deletion", e);
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2010-06-24 00:32:05 +03:00
|
|
|
// and close it
|
2010-02-08 15:30:06 +02:00
|
|
|
try {
|
|
|
|
store.close();
|
|
|
|
} catch (IOException e) {
|
2010-06-24 00:32:05 +03:00
|
|
|
logger.warn("failed to close store on shard deletion", e);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Injectors.close(injector);
|
2010-11-03 15:34:30 +02:00
|
|
|
|
|
|
|
// delete the shard location if needed
|
|
|
|
if (delete || indexGateway.type().equals(NoneGateway.TYPE)) {
|
2011-09-23 00:35:59 +03:00
|
|
|
FileSystemUtils.deleteRecursively(nodeEnv.shardLocations(sId));
|
2010-11-03 15:34:30 +02:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|