improve handling of memory caching with file system, only force compound file when really needed (when an extension that exists within the compound file is part of the memory cached extensions)

This commit is contained in:
kimchy 2010-07-06 18:21:36 +03:00
parent ac01bb86e8
commit 267016758d
5 changed files with 49 additions and 15 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.store.fs;
import org.apache.lucene.store.*; import org.apache.lucene.store.*;
import org.elasticsearch.cache.memory.ByteBufferCache; import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.lucene.store.SwitchDirectory; import org.elasticsearch.common.lucene.store.SwitchDirectory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -73,7 +74,7 @@ public abstract class FsStore extends AbstractStore {
return lockFactory; return lockFactory;
} }
protected SwitchDirectory buildSwitchDirectoryIfNeeded(Directory fsDirectory, ByteBufferCache byteBufferCache) { protected Tuple<SwitchDirectory, Boolean> buildSwitchDirectoryIfNeeded(Directory fsDirectory, ByteBufferCache byteBufferCache) {
boolean cache = componentSettings.getAsBoolean("memory.enabled", false); boolean cache = componentSettings.getAsBoolean("memory.enabled", false);
if (!cache) { if (!cache) {
return null; return null;
@ -81,6 +82,17 @@ public abstract class FsStore extends AbstractStore {
Directory memDir = new ByteBufferDirectory(byteBufferCache); Directory memDir = new ByteBufferDirectory(byteBufferCache);
// see http://lucene.apache.org/java/3_0_1/fileformats.html // see http://lucene.apache.org/java/3_0_1/fileformats.html
String[] primaryExtensions = componentSettings.getAsArray("memory.extensions", new String[]{"", "del", "gen"}); String[] primaryExtensions = componentSettings.getAsArray("memory.extensions", new String[]{"", "del", "gen"});
return new SwitchDirectory(ImmutableSet.copyOf(primaryExtensions), memDir, fsDirectory, true); if (primaryExtensions == null || primaryExtensions.length == 0) {
return null;
}
Boolean forceUseCompound = null;
for (String extension : primaryExtensions) {
if (!("".equals(extension) || "del".equals(extension) || "gen".equals(extension))) {
// caching internal CFS extension, don't use compound file extension
forceUseCompound = false;
}
}
return new Tuple<SwitchDirectory, Boolean>(new SwitchDirectory(ImmutableSet.copyOf(primaryExtensions), memDir, fsDirectory, true), forceUseCompound);
} }
} }

View File

@ -24,6 +24,7 @@ import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockFactory; import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.MMapDirectory; import org.apache.lucene.store.MMapDirectory;
import org.elasticsearch.cache.memory.ByteBufferCache; import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.store.SwitchDirectory; import org.elasticsearch.common.lucene.store.SwitchDirectory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -52,16 +53,21 @@ public class MmapFsStore extends FsStore {
location.mkdirs(); location.mkdirs();
this.fsDirectory = new MMapDirectory(location, lockFactory); this.fsDirectory = new MMapDirectory(location, lockFactory);
SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache); boolean suggestUseCompoundFile;
Tuple<SwitchDirectory, Boolean> switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache);
if (switchDirectory != null) { if (switchDirectory != null) {
suggestUseCompoundFile = false; suggestUseCompoundFile = true;
logger.debug("Using [mmap_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); if (switchDirectory.v2() != null) {
directory = wrapDirectory(switchDirectory); suggestUseCompoundFile = switchDirectory.v2();
}
logger.debug("Using [mmap_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.v1().primaryExtensions());
directory = wrapDirectory(switchDirectory.v1());
} else { } else {
suggestUseCompoundFile = true; suggestUseCompoundFile = true;
directory = wrapDirectory(fsDirectory); directory = wrapDirectory(fsDirectory);
logger.debug("Using [mmap_fs] Store with path [{}]", fsDirectory.getFile()); logger.debug("Using [mmap_fs] Store with path [{}]", fsDirectory.getFile());
} }
this.suggestUseCompoundFile = suggestUseCompoundFile;
} }
@Override public FSDirectory fsDirectory() { @Override public FSDirectory fsDirectory() {

View File

@ -24,6 +24,7 @@ import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockFactory; import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.NIOFSDirectory; import org.apache.lucene.store.NIOFSDirectory;
import org.elasticsearch.cache.memory.ByteBufferCache; import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.store.SwitchDirectory; import org.elasticsearch.common.lucene.store.SwitchDirectory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -52,16 +53,21 @@ public class NioFsStore extends FsStore {
location.mkdirs(); location.mkdirs();
this.fsDirectory = new NIOFSDirectory(location, lockFactory); this.fsDirectory = new NIOFSDirectory(location, lockFactory);
SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache); boolean suggestUseCompoundFile;
Tuple<SwitchDirectory, Boolean> switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache);
if (switchDirectory != null) { if (switchDirectory != null) {
suggestUseCompoundFile = false; suggestUseCompoundFile = true;
logger.debug("Using [nio_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); if (switchDirectory.v2() != null) {
directory = wrapDirectory(switchDirectory); suggestUseCompoundFile = switchDirectory.v2();
}
logger.debug("Using [nio_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.v1().primaryExtensions());
directory = wrapDirectory(switchDirectory.v1());
} else { } else {
suggestUseCompoundFile = true; suggestUseCompoundFile = true;
directory = wrapDirectory(fsDirectory); directory = wrapDirectory(fsDirectory);
logger.debug("Using [nio_fs] Store with path [{}]", fsDirectory.getFile()); logger.debug("Using [nio_fs] Store with path [{}]", fsDirectory.getFile());
} }
this.suggestUseCompoundFile = suggestUseCompoundFile;
} }
@Override public FSDirectory fsDirectory() { @Override public FSDirectory fsDirectory() {

View File

@ -24,6 +24,7 @@ import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockFactory; import org.apache.lucene.store.LockFactory;
import org.apache.lucene.store.SimpleFSDirectory; import org.apache.lucene.store.SimpleFSDirectory;
import org.elasticsearch.cache.memory.ByteBufferCache; import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.store.SwitchDirectory; import org.elasticsearch.common.lucene.store.SwitchDirectory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -52,16 +53,21 @@ public class SimpleFsStore extends FsStore {
location.mkdirs(); location.mkdirs();
this.fsDirectory = new SimpleFSDirectory(location, lockFactory); this.fsDirectory = new SimpleFSDirectory(location, lockFactory);
SwitchDirectory switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache); boolean suggestUseCompoundFile;
Tuple<SwitchDirectory, Boolean> switchDirectory = buildSwitchDirectoryIfNeeded(fsDirectory, byteBufferCache);
if (switchDirectory != null) { if (switchDirectory != null) {
suggestUseCompoundFile = false; suggestUseCompoundFile = true;
logger.debug("Using [simple_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.primaryExtensions()); if (switchDirectory.v2() != null) {
directory = wrapDirectory(switchDirectory); suggestUseCompoundFile = switchDirectory.v2();
}
logger.debug("Using [simple_fs] Store with path [{}], cache [true] with extensions [{}]", fsDirectory.getFile(), switchDirectory.v1().primaryExtensions());
directory = wrapDirectory(switchDirectory.v1());
} else { } else {
suggestUseCompoundFile = true; suggestUseCompoundFile = true;
directory = wrapDirectory(fsDirectory); directory = wrapDirectory(fsDirectory);
logger.debug("Using [simple_fs] Store with path [{}]", fsDirectory.getFile()); logger.debug("Using [simple_fs] Store with path [{}]", fsDirectory.getFile());
} }
this.suggestUseCompoundFile = suggestUseCompoundFile;
} }
@Override public FSDirectory fsDirectory() { @Override public FSDirectory fsDirectory() {

View File

@ -167,7 +167,11 @@ public abstract class AbstractStore extends AbstractIndexShardComponent implemen
synchronized (mutex) { synchronized (mutex) {
MapBuilder<String, StoreFileMetaData> builder = MapBuilder.newMapBuilder(); MapBuilder<String, StoreFileMetaData> builder = MapBuilder.newMapBuilder();
for (String file : delegate.listAll()) { for (String file : delegate.listAll()) {
try {
builder.put(file, new StoreFileMetaData(file, delegate.fileLength(file), delegate.fileModified(file), preComputedMd5(file))); builder.put(file, new StoreFileMetaData(file, delegate.fileLength(file), delegate.fileModified(file), preComputedMd5(file)));
} catch (FileNotFoundException e) {
// ignore
}
} }
filesMetadata = builder.immutableMap(); filesMetadata = builder.immutableMap();
files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]); files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);