diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/robin/RobinEngine.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/robin/RobinEngine.java index 0c082cef59d..263bafb11e4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/robin/RobinEngine.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/robin/RobinEngine.java @@ -116,6 +116,20 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine, this.mergeScheduler = mergeScheduler; this.analysisService = analysisService; this.similarityService = similarityService; + + // clear the index dir by creating a new index + try { + // release locks when started + if (IndexWriter.isLocked(store.directory())) { + logger.trace("Shard is locked, releasing lock"); + store.directory().clearLock(IndexWriter.WRITE_LOCK_NAME); + } + IndexWriter writer = new IndexWriter(store.directory(), analysisService.defaultIndexAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); + writer.commit(); + writer.close(); + } catch (IOException e) { + logger.warn("Failed to clean the index", e); + } } @Override public void start() throws EngineException { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/MmapFsStore.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/MmapFsStore.java index 5a3c3f27198..1c9ebbf7d6b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/MmapFsStore.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/MmapFsStore.java @@ -19,9 +19,7 @@ package org.elasticsearch.index.store.fs; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; -import org.apache.lucene.store.MMapDirectory; +import org.apache.lucene.store.*; import org.elasticsearch.env.Environment; import org.elasticsearch.index.LocalNodeId; import org.elasticsearch.index.settings.IndexSettings; @@ -53,12 +51,19 @@ public class MmapFsStore extends AbstractFsStore { super(shardId, indexSettings); // by default, we don't need to sync to disk, since we use the gateway this.syncToDisk = componentSettings.getAsBoolean("sync_to_disk", false); - this.fsDirectory = new CustomMMapDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), syncToDisk); + String fsLock = componentSettings.get("use_fs_lock", "none"); + LockFactory lockFactory = new NoLockFactory(); + if (fsLock.equals("native")) { + lockFactory = new NativeFSLockFactory(); + } else if (fsLock.equals("simple")) { + lockFactory = new SimpleFSLockFactory(); + } + this.fsDirectory = new CustomMMapDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), lockFactory, syncToDisk); SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory); if (switchDirectory != null) { suggestUseCompoundFile = false; - logger.debug("Using [mmap_fs] Store with path [{}], cache [true] with extensions [{}]", new Object[]{fsDirectory.getFile(), switchDirectory.primaryExtensions()}); + logger.debug("Using [mmap_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); directory = switchDirectory; } else { suggestUseCompoundFile = true; @@ -83,8 +88,8 @@ public class MmapFsStore extends AbstractFsStore { private final boolean syncToDisk; - private CustomMMapDirectory(File path, boolean syncToDisk) throws IOException { - super(path); + private CustomMMapDirectory(File path, LockFactory lockFactory, boolean syncToDisk) throws IOException { + super(path, lockFactory); this.syncToDisk = syncToDisk; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/NioFsStore.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/NioFsStore.java index 2ecba6f080c..4037f7396d7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/NioFsStore.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/NioFsStore.java @@ -19,9 +19,7 @@ package org.elasticsearch.index.store.fs; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; -import org.apache.lucene.store.NIOFSDirectory; +import org.apache.lucene.store.*; import org.elasticsearch.env.Environment; import org.elasticsearch.index.LocalNodeId; import org.elasticsearch.index.settings.IndexSettings; @@ -53,12 +51,19 @@ public class NioFsStore extends AbstractFsStore { super(shardId, indexSettings); // by default, we don't need to sync to disk, since we use the gateway this.syncToDisk = componentSettings.getAsBoolean("sync_to_disk", false); - this.fsDirectory = new CustomNioFSDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), syncToDisk); + String fsLock = componentSettings.get("use_fs_lock", "none"); + LockFactory lockFactory = new NoLockFactory(); + if (fsLock.equals("native")) { + lockFactory = new NativeFSLockFactory(); + } else if (fsLock.equals("simple")) { + lockFactory = new SimpleFSLockFactory(); + } + this.fsDirectory = new CustomNioFSDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), lockFactory, syncToDisk); SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory); if (switchDirectory != null) { suggestUseCompoundFile = false; - logger.debug("Using [nio_fs] Store with path [{}], cache [true] with extensions [{}]", new Object[]{fsDirectory.getFile(), switchDirectory.primaryExtensions()}); + logger.debug("Using [nio_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); directory = switchDirectory; } else { suggestUseCompoundFile = true; @@ -83,8 +88,8 @@ public class NioFsStore extends AbstractFsStore { private final boolean syncToDisk; - private CustomNioFSDirectory(File path, boolean syncToDisk) throws IOException { - super(path); + private CustomNioFSDirectory(File path, LockFactory lockFactory, boolean syncToDisk) throws IOException { + super(path, lockFactory); this.syncToDisk = syncToDisk; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/SimpleFsStore.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/SimpleFsStore.java index 2f0a5e3d346..cf9735b88c1 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/SimpleFsStore.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/store/fs/SimpleFsStore.java @@ -19,9 +19,7 @@ package org.elasticsearch.index.store.fs; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; -import org.apache.lucene.store.SimpleFSDirectory; +import org.apache.lucene.store.*; import org.elasticsearch.env.Environment; import org.elasticsearch.index.LocalNodeId; import org.elasticsearch.index.settings.IndexSettings; @@ -53,12 +51,19 @@ public class SimpleFsStore extends AbstractFsStore { super(shardId, indexSettings); // by default, we don't need to sync to disk, since we use the gateway this.syncToDisk = componentSettings.getAsBoolean("sync_to_disk", false); - this.fsDirectory = new CustomSimpleFSDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), syncToDisk); + String fsLock = componentSettings.get("use_fs_lock", "none"); + LockFactory lockFactory = new NoLockFactory(); + if (fsLock.equals("native")) { + lockFactory = new NativeFSLockFactory(); + } else if (fsLock.equals("simple")) { + lockFactory = new SimpleFSLockFactory(); + } + this.fsDirectory = new CustomSimpleFSDirectory(createStoreFilePath(environment.workWithClusterFile(), localNodeId, shardId, MAIN_INDEX_SUFFIX), lockFactory, syncToDisk); SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory); if (switchDirectory != null) { suggestUseCompoundFile = false; - logger.debug("Using [simple_fs] Store with path [{}], cache [true] with extensions [{}]", new Object[]{fsDirectory.getFile(), switchDirectory.primaryExtensions()}); + logger.debug("Using [simple_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); directory = switchDirectory; } else { suggestUseCompoundFile = true; @@ -83,8 +88,8 @@ public class SimpleFsStore extends AbstractFsStore { private final boolean syncToDisk; - private CustomSimpleFSDirectory(File path, boolean syncToDisk) throws IOException { - super(path); + private CustomSimpleFSDirectory(File path, LockFactory lockFactory, boolean syncToDisk) throws IOException { + super(path, lockFactory); this.syncToDisk = syncToDisk; } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/AbstractSimpleEngineTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/AbstractSimpleEngineTests.java index 63e42eb16d0..b97dfcde62c 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/AbstractSimpleEngineTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/AbstractSimpleEngineTests.java @@ -212,18 +212,6 @@ public abstract class AbstractSimpleEngineTests { searchResult.release(); engine.close(); - - // TODO check that operations on engine will throw an EngineAlreadyClosed exception (and while you are at it, create the exception as well) - - // now create a new engine, it should see the flushed changes - engine = createEngine(store); - engine.start(); - - searchResult = engine.searcher(); - assertThat(searchResult, engineSearcherTotalHits(1)); - assertThat(searchResult, engineSearcherTotalHits(new TermQuery(new Term("value", "test")), 0)); - assertThat(searchResult, engineSearcherTotalHits(new TermQuery(new Term("value", "test1")), 1)); - searchResult.release(); } @Test public void testSearchResultRelease() throws Exception {