move the query parser cache to be index level and not node level

This commit is contained in:
kimchy 2010-12-22 12:31:30 +02:00
parent 33339ae4b1
commit eef3a95fa6
21 changed files with 100 additions and 60 deletions

View File

@ -25,7 +25,6 @@ import org.elasticsearch.action.TransportActions;
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.TransportBroadcastOperationAction;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.routing.GroupShardsIterator;
@ -51,12 +50,9 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
private final IndicesService indicesService;
private final QueryParserCache queryParserCache;
@Inject public TransportClearIndicesCacheAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
TransportService transportService, IndicesService indicesService, QueryParserCache queryParserCache) {
TransportService transportService, IndicesService indicesService) {
super(settings, threadPool, clusterService, transportService);
this.queryParserCache = queryParserCache;
this.indicesService = indicesService;
}
@ -116,7 +112,6 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
if (service != null) {
service.cache().clear();
}
queryParserCache.clear();
return new ShardClearIndicesCacheResponse(request.index(), request.shardId());
}

View File

@ -20,7 +20,6 @@
package org.elasticsearch.cache;
import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterStateListener;
@ -37,35 +36,22 @@ public class NodeCache extends AbstractComponent implements ClusterStateListener
private final ByteBufferCache byteBufferCache;
private final QueryParserCache queryParserCache;
@Inject public NodeCache(Settings settings, ByteBufferCache byteBufferCache, QueryParserCache queryParserCache, ClusterService clusterService) {
@Inject public NodeCache(Settings settings, ByteBufferCache byteBufferCache, ClusterService clusterService) {
super(settings);
this.clusterService = clusterService;
this.byteBufferCache = byteBufferCache;
this.queryParserCache = queryParserCache;
clusterService.add(this);
}
public void close() {
clusterService.remove(this);
byteBufferCache.close();
queryParserCache.clear();
}
public ByteBufferCache byteBuffer() {
return byteBufferCache;
}
public QueryParserCache queryParser() {
return queryParserCache;
}
// listen on cluster change events to invalidate the query parser cache
@Override public void clusterChanged(ClusterChangedEvent event) {
// TODO we can do better by detecting just mappings changes
if (event.metaDataChanged()) {
queryParserCache.clear();
}
}
}

View File

@ -20,7 +20,6 @@
package org.elasticsearch.cache;
import org.elasticsearch.cache.memory.ByteBufferCache;
import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.settings.Settings;
@ -38,7 +37,5 @@ public class NodeCacheModule extends AbstractModule {
@Override protected void configure() {
bind(NodeCache.class).asEagerSingleton();
bind(ByteBufferCache.class).asEagerSingleton();
new QueryParserCacheModule(settings).configure(binder());
}
}

View File

@ -20,6 +20,11 @@
package org.elasticsearch.index.cache;
import org.apache.lucene.index.IndexReader;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterStateListener;
import org.elasticsearch.common.component.CloseableComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.AbstractIndexComponent;
@ -27,27 +32,43 @@ import org.elasticsearch.index.Index;
import org.elasticsearch.index.cache.field.data.FieldDataCache;
import org.elasticsearch.index.cache.filter.FilterCache;
import org.elasticsearch.index.cache.id.IdCache;
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
import org.elasticsearch.index.settings.IndexSettings;
import javax.annotation.Nullable;
/**
* @author kimchy (shay.banon)
*/
public class IndexCache extends AbstractIndexComponent {
public class IndexCache extends AbstractIndexComponent implements CloseableComponent, ClusterStateListener {
private final FilterCache filterCache;
private final FieldDataCache fieldDataCache;
private final QueryParserCache queryParserCache;
private final IdCache idCache;
private ClusterService clusterService;
@Inject public IndexCache(Index index, @IndexSettings Settings indexSettings, FilterCache filterCache, FieldDataCache fieldDataCache,
IdCache idCache) {
QueryParserCache queryParserCache, IdCache idCache) {
super(index, indexSettings);
this.filterCache = filterCache;
this.fieldDataCache = fieldDataCache;
this.queryParserCache = queryParserCache;
this.idCache = idCache;
}
@Inject(optional = true)
public void setClusterService(@Nullable ClusterService clusterService) {
this.clusterService = clusterService;
if (clusterService != null) {
clusterService.add(this);
}
}
public FilterCache filter() {
return filterCache;
}
@ -60,6 +81,20 @@ public class IndexCache extends AbstractIndexComponent {
return this.idCache;
}
public QueryParserCache queryParserCache() {
return this.queryParserCache;
}
@Override public void close() throws ElasticSearchException {
filterCache.close();
fieldDataCache.close();
idCache.close();
queryParserCache.close();
if (clusterService != null) {
clusterService.remove(this);
}
}
public void clear(IndexReader reader) {
filterCache.clear(reader);
fieldDataCache.clear(reader);
@ -70,6 +105,7 @@ public class IndexCache extends AbstractIndexComponent {
filterCache.clear();
fieldDataCache.clear();
idCache.clear();
queryParserCache.clear();
}
public void clearUnreferenced() {
@ -77,4 +113,11 @@ public class IndexCache extends AbstractIndexComponent {
fieldDataCache.clearUnreferenced();
idCache.clearUnreferenced();
}
@Override public void clusterChanged(ClusterChangedEvent event) {
// clear the query parser cache if the metadata (mappings) changed...
if (event.metaDataChanged()) {
queryParserCache.clear();
}
}
}

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.cache.field.data.FieldDataCacheModule;
import org.elasticsearch.index.cache.filter.FilterCacheModule;
import org.elasticsearch.index.cache.id.IdCacheModule;
import org.elasticsearch.index.cache.query.parser.QueryParserCacheModule;
/**
* @author kimchy (shay.banon)
@ -40,6 +41,7 @@ public class IndexCacheModule extends AbstractModule {
new FilterCacheModule(settings).configure(binder());
new FieldDataCacheModule(settings).configure(binder());
new IdCacheModule(settings).configure(binder());
new QueryParserCacheModule(settings).configure(binder());
bind(IndexCache.class).asEagerSingleton();
}

View File

@ -51,7 +51,7 @@ public abstract class AbstractConcurrentMapFieldDataCache extends AbstractIndexC
}
@Override public void close() throws ElasticSearchException {
cache.clear();
clear();
}
@Override public void clear() {

View File

@ -55,11 +55,12 @@ public abstract class AbstractDoubleConcurrentMapFilterCache extends AbstractInd
}
@Override public void close() {
cache.clear();
clear();
}
@Override public void clear() {
cache.clear();
weakCache.clear();
}
@Override public void clear(IndexReader reader) {

View File

@ -20,11 +20,12 @@
package org.elasticsearch.index.cache.id;
import org.apache.lucene.index.IndexReader;
import org.elasticsearch.common.component.CloseableComponent;
/**
* @author kimchy (shay.banon)
*/
public interface IdCache extends Iterable<IdReaderCache> {
public interface IdCache extends CloseableComponent, Iterable<IdReaderCache> {
void clear();

View File

@ -24,6 +24,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.util.StringHelper;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.BytesWrap;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.collect.MapMaker;
@ -52,6 +53,10 @@ public class SimpleIdCache implements IdCache {
idReaders = new MapMaker().weakKeys().makeMap();
}
@Override public void close() throws ElasticSearchException {
clear();
}
@Override public void clear() {
idReaders.clear();
}

View File

@ -17,15 +17,16 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser;
package org.elasticsearch.index.cache.query.parser;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.component.CloseableComponent;
/**
* @author kimchy (shay.banon)
*/
public interface QueryParserCache {
public interface QueryParserCache extends CloseableComponent {
Query get(QueryParserSettings queryString);

View File

@ -17,12 +17,12 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser;
package org.elasticsearch.index.cache.query.parser;
import org.elasticsearch.cache.query.parser.weak.WeakQueryParserCache;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Scopes;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.cache.query.parser.weak.WeakQueryParserCache;
/**
* @author kimchy (shay.banon)
@ -37,7 +37,7 @@ public class QueryParserCacheModule extends AbstractModule {
@Override protected void configure() {
bind(QueryParserCache.class)
.to(settings.getAsClass("cache.query.parser.type", WeakQueryParserCache.class, "org.elasticsearch.cache.query.parser.", "QueryParserCache"))
.to(settings.getAsClass("index.cache.query.parser.type", WeakQueryParserCache.class, "org.elasticsearch.index.cache.query.parser.", "QueryParserCache"))
.in(Scopes.SINGLETON);
}
}

View File

@ -17,12 +17,13 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser.none;
package org.elasticsearch.index.cache.query.parser.none;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
/**
* @author kimchy (shay.banon)
@ -41,4 +42,7 @@ public class NoneQueryParserCache implements QueryParserCache {
@Override public void clear() {
}
@Override public void close() throws ElasticSearchException {
}
}

View File

@ -17,21 +17,23 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser.soft;
package org.elasticsearch.index.cache.query.parser.soft;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.common.collect.MapMaker;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.index.settings.IndexSettings;
/**
* @author kimchy (shay.banon)
*/
public class SoftQueryParserCache extends AbstractJvmQueryParserCache {
@Inject public SoftQueryParserCache(Settings settings) {
super(settings, new MapMaker().softValues().<QueryParserSettings, Query>makeMap());
@Inject public SoftQueryParserCache(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings, new MapMaker().softValues().<QueryParserSettings, Query>makeMap());
}
}

View File

@ -17,28 +17,35 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser.support;
package org.elasticsearch.index.cache.query.parser.support;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
import org.elasticsearch.index.settings.IndexSettings;
import java.util.concurrent.ConcurrentMap;
/**
* @author kimchy (shay.banon)
*/
public class AbstractJvmQueryParserCache extends AbstractComponent implements QueryParserCache {
public class AbstractJvmQueryParserCache extends AbstractIndexComponent implements QueryParserCache {
final ConcurrentMap<QueryParserSettings, Query> cache;
protected AbstractJvmQueryParserCache(Settings settings, ConcurrentMap<QueryParserSettings, Query> cache) {
super(settings);
protected AbstractJvmQueryParserCache(Index index, @IndexSettings Settings indexSettings, ConcurrentMap<QueryParserSettings, Query> cache) {
super(index, indexSettings);
this.cache = cache;
}
@Override public void close() throws ElasticSearchException {
clear();
}
@Override public void clear() {
cache.clear();
}

View File

@ -17,21 +17,23 @@
* under the License.
*/
package org.elasticsearch.cache.query.parser.weak;
package org.elasticsearch.index.cache.query.parser.weak;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.common.collect.MapMaker;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.cache.query.parser.support.AbstractJvmQueryParserCache;
import org.elasticsearch.index.settings.IndexSettings;
/**
* @author kimchy (shay.banon)
*/
public class WeakQueryParserCache extends AbstractJvmQueryParserCache {
@Inject public WeakQueryParserCache(Settings settings) {
super(settings, new MapMaker().weakValues().<QueryParserSettings, Query>makeMap());
@Inject public WeakQueryParserCache(Index index, @IndexSettings Settings indexSettings) {
super(index, indexSettings, new MapMaker().weakValues().<QueryParserSettings, Query>makeMap());
}
}

View File

@ -24,13 +24,13 @@ import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParserSettings;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
import org.elasticsearch.index.query.QueryParsingException;
import org.elasticsearch.index.settings.IndexSettings;

View File

@ -24,7 +24,6 @@ import org.apache.lucene.queryParser.MultiFieldQueryParserSettings;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.elasticsearch.cache.query.parser.QueryParserCache;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Inject;
@ -35,6 +34,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.cache.query.parser.QueryParserCache;
import org.elasticsearch.index.mapper.AllFieldMapper;
import org.elasticsearch.index.query.QueryParsingException;
import org.elasticsearch.index.settings.IndexSettings;

View File

@ -40,8 +40,8 @@ import org.elasticsearch.gateway.Gateway;
import org.elasticsearch.index.*;
import org.elasticsearch.index.analysis.AnalysisModule;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.cache.IndexCache;
import org.elasticsearch.index.cache.IndexCacheModule;
import org.elasticsearch.index.cache.filter.FilterCache;
import org.elasticsearch.index.engine.IndexEngine;
import org.elasticsearch.index.engine.IndexEngineModule;
import org.elasticsearch.index.gateway.IndexGateway;
@ -280,7 +280,7 @@ public class InternalIndicesService extends AbstractLifecycleComponent<IndicesSe
indexService.close(delete);
indexInjector.getInstance(FilterCache.class).close();
indexInjector.getInstance(IndexCache.class).close();
indexInjector.getInstance(AnalysisService.class).close();
indexInjector.getInstance(IndexEngine.class).close();
indexInjector.getInstance(IndexServiceManagement.class).close();

View File

@ -23,7 +23,6 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.lucene.search.*;
@ -78,7 +77,6 @@ public class SimpleIndexQueryParserTests {
Index index = new Index("test");
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new QueryParserCacheModule(settings),
new ScriptModule(),
new MapperServiceModule(),
new IndexSettingsModule(settings),

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.query.xcontent.guice;
import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.Settings;
@ -58,7 +57,6 @@ public class IndexQueryParserModuleTests {
Index index = new Index("test");
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new QueryParserCacheModule(settings),
new ScriptModule(),
new IndexSettingsModule(settings),
new IndexCacheModule(settings),

View File

@ -19,7 +19,6 @@
package org.elasticsearch.index.query.xcontent.plugin;
import org.elasticsearch.cache.query.parser.QueryParserCacheModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
@ -64,7 +63,6 @@ public class IndexQueryParserPluginTests {
Injector injector = new ModulesBuilder().add(
new SettingsModule(settings),
new ScriptModule(),
new QueryParserCacheModule(settings),
new IndexSettingsModule(settings),
new IndexCacheModule(settings),
new AnalysisModule(settings),