Skip to content

Commit

Permalink
SONAR-6089 Only clear lookup cache of current View
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Feb 5, 2015
1 parent 8f1b67e commit 3601d3a
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 198 deletions.
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder; import org.elasticsearch.action.admin.cluster.state.ClusterStateRequestBuilder;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequestBuilder; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequestBuilder;
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder; import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
Expand All @@ -48,25 +49,7 @@
import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.picocontainer.Startable; import org.picocontainer.Startable;
import org.sonar.core.profiling.Profiling; import org.sonar.core.profiling.Profiling;
import org.sonar.server.es.request.ProxyBulkRequestBuilder; import org.sonar.server.es.request.*;
import org.sonar.server.es.request.ProxyClusterHealthRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStateRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStatsRequestBuilder;
import org.sonar.server.es.request.ProxyCountRequestBuilder;
import org.sonar.server.es.request.ProxyCreateIndexRequestBuilder;
import org.sonar.server.es.request.ProxyDeleteByQueryRequestBuilder;
import org.sonar.server.es.request.ProxyDeleteRequestBuilder;
import org.sonar.server.es.request.ProxyFlushRequestBuilder;
import org.sonar.server.es.request.ProxyGetRequestBuilder;
import org.sonar.server.es.request.ProxyIndexRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesExistsRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesStatsRequestBuilder;
import org.sonar.server.es.request.ProxyMultiGetRequestBuilder;
import org.sonar.server.es.request.ProxyNodesStatsRequestBuilder;
import org.sonar.server.es.request.ProxyPutMappingRequestBuilder;
import org.sonar.server.es.request.ProxyRefreshRequestBuilder;
import org.sonar.server.es.request.ProxySearchRequestBuilder;
import org.sonar.server.es.request.ProxySearchScrollRequestBuilder;
import org.sonar.server.search.ClusterHealth; import org.sonar.server.search.ClusterHealth;
import org.sonar.server.search.SearchClient; import org.sonar.server.search.SearchClient;


Expand Down Expand Up @@ -178,7 +161,6 @@ public DeleteRequestBuilder prepareDelete(String index, String type, String id)
return new ProxyDeleteRequestBuilder(profiling, client, index).setType(type).setId(id); return new ProxyDeleteRequestBuilder(profiling, client, index).setType(type).setId(id);
} }



public DeleteByQueryRequestBuilder prepareDeleteByQuery(String... indices) { public DeleteByQueryRequestBuilder prepareDeleteByQuery(String... indices) {
return new ProxyDeleteByQueryRequestBuilder(client, profiling).setIndices(indices); return new ProxyDeleteByQueryRequestBuilder(client, profiling).setIndices(indices);
} }
Expand All @@ -194,6 +176,9 @@ public OptimizeRequestBuilder prepareOptimize(String indexName) {
.setWaitForMerge(true); .setWaitForMerge(true);
} }


public ClearIndicesCacheRequestBuilder prepareClearCache(String... indices) {
return new ProxyClearCacheRequestBuilder(client, profiling).setIndices(indices);
}


public long getLastUpdatedAt(String indexName, String typeName) { public long getLastUpdatedAt(String indexName, String typeName) {
SearchRequestBuilder request = prepareSearch(indexName) SearchRequestBuilder request = prepareSearch(indexName)
Expand Down
@@ -0,0 +1,96 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.es.request;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequestBuilder;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.sonar.core.profiling.Profiling;
import org.sonar.core.profiling.StopWatch;

public class ProxyClearCacheRequestBuilder extends ClearIndicesCacheRequestBuilder {

private final Profiling profiling;

public ProxyClearCacheRequestBuilder(Client client, Profiling profiling) {
super(client.admin().indices());
this.profiling = profiling;
}

@Override
public ClearIndicesCacheResponse get() {
StopWatch fullProfile = profiling.start("clear cache", Profiling.Level.FULL);
try {
return super.execute().actionGet();
} catch (Exception e) {
throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
} finally {
if (profiling.isProfilingEnabled(Profiling.Level.FULL)) {
fullProfile.stop("%s", toString());
}
}
}

@Override
public ClearIndicesCacheResponse get(TimeValue timeout) {
throw new IllegalStateException("Not yet implemented");
}

@Override
public ClearIndicesCacheResponse get(String timeout) {
throw new IllegalStateException("Not yet implemented");
}

@Override
public ListenableActionFuture<ClearIndicesCacheResponse> execute() {
throw new UnsupportedOperationException("execute() should not be called as it's used for asynchronous");
}

@Override
public String toString() {
StringBuilder message = new StringBuilder();
message.append("ES clear cache request");
if (request.indices().length > 0) {
message.append(String.format(" on indices '%s'", StringUtils.join(request.indices(), ",")));
}
String[] fields = request.fields();
if (fields != null && fields.length > 0) {
message.append(String.format(" on fields '%s'", StringUtils.join(fields, ",")));
}
String[] filterKeys = request.filterKeys();
if (filterKeys != null && filterKeys.length > 0) {
message.append(String.format(" on filterKeys '%s'", StringUtils.join(filterKeys, ",")));
}
if (request.filterCache()) {
message.append(" with filter cache");
}
if (request.fieldDataCache()) {
message.append(" with field data cache");
}
if (request.idCache()) {
message.append(" with id cache");
}
return message.toString();
}
}
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.issue.index; package org.sonar.server.issue.index;


import com.google.common.collect.Maps;
import org.sonar.api.issue.Issue; import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueComment; import org.sonar.api.issue.IssueComment;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
Expand All @@ -38,6 +39,10 @@ public IssueDoc(Map<String, Object> fields) {
super(fields); super(fields);
} }


public IssueDoc() {
super(Maps.<String, Object>newHashMap());
}

@Override @Override
public String key() { public String key() {
return getField(IssueIndexDefinition.FIELD_ISSUE_KEY); return getField(IssueIndexDefinition.FIELD_ISSUE_KEY);
Expand Down
Expand Up @@ -375,12 +375,12 @@ private FilterBuilder viewFilter(Collection<String> viewUuids) {
.lookupType(ViewIndexDefinition.TYPE_VIEW) .lookupType(ViewIndexDefinition.TYPE_VIEW)
.lookupId(viewUuid) .lookupId(viewUuid)
.lookupPath(ViewIndexDefinition.FIELD_PROJECTS)) .lookupPath(ViewIndexDefinition.FIELD_PROJECTS))
.cacheKey(cacheKey(viewUuid)); .cacheKey(viewsLookupCacheKey(viewUuid));
} }
return viewsFilter; return viewsFilter;
} }


public static String cacheKey(String viewUuid) { public static String viewsLookupCacheKey(String viewUuid) {
return IssueIndexDefinition.TYPE_ISSUE + viewUuid + ViewIndexDefinition.TYPE_VIEW; return IssueIndexDefinition.TYPE_ISSUE + viewUuid + ViewIndexDefinition.TYPE_VIEW;
} }


Expand Down
Expand Up @@ -20,17 +20,17 @@
package org.sonar.server.platform; package org.sonar.server.platform;


import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.DbUtils;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent; import org.sonar.api.ServerComponent;
import org.sonar.core.persistence.DatabaseVersion; import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis; import org.sonar.core.persistence.MyBatis;
import org.sonar.server.es.EsClient;
import org.sonar.server.issue.index.IssueIndexDefinition; import org.sonar.server.issue.index.IssueIndexDefinition;
import org.sonar.server.search.IndexDefinition; import org.sonar.server.search.IndexDefinition;
import org.sonar.server.search.SearchClient;
import org.sonar.server.source.index.SourceLineIndexDefinition; import org.sonar.server.source.index.SourceLineIndexDefinition;
import org.sonar.server.view.index.ViewIndexDefinition;


import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
Expand All @@ -45,11 +45,11 @@ public class BackendCleanup implements ServerComponent {
private static final String[] RESOURCE_RELATED_TABLES = { private static final String[] RESOURCE_RELATED_TABLES = {
"group_roles", "user_roles", "properties" "group_roles", "user_roles", "properties"
}; };
private final SearchClient searchClient; private final EsClient esClient;
private final MyBatis myBatis; private final MyBatis myBatis;


public BackendCleanup(SearchClient searchClient, MyBatis myBatis) { public BackendCleanup(EsClient esClient, MyBatis myBatis) {
this.searchClient = searchClient; this.esClient = esClient;
this.myBatis = myBatis; this.myBatis = myBatis;
} }


Expand Down Expand Up @@ -81,18 +81,17 @@ public void clearDb() {
public void clearIndexes() { public void clearIndexes() {
LoggerFactory.getLogger(getClass()).info("Truncate Elasticsearch indices"); LoggerFactory.getLogger(getClass()).info("Truncate Elasticsearch indices");
try { try {
searchClient.admin().indices() esClient.prepareClearCache()
.clearCache(new ClearIndicesCacheRequest())
.get(); .get();
searchClient.prepareDeleteByQuery(searchClient.prepareState().get() esClient.prepareDeleteByQuery(esClient.prepareState().get()
.getState().getMetaData().concreteAllIndices()) .getState().getMetaData().concreteAllIndices())
.setQuery(QueryBuilders.matchAllQuery()) .setQuery(QueryBuilders.matchAllQuery())
.get(); .get();
searchClient.prepareRefresh(searchClient.prepareState().get() esClient.prepareRefresh(esClient.prepareState().get()
.getState().getMetaData().concreteAllIndices()) .getState().getMetaData().concreteAllIndices())
.setForce(true) .setForce(true)
.get(); .get();
searchClient.prepareFlush(searchClient.prepareState().get() esClient.prepareFlush(esClient.prepareState().get()
.getState().getMetaData().concreteAllIndices()) .getState().getMetaData().concreteAllIndices())
.get(); .get();
} catch (Exception e) { } catch (Exception e) {
Expand Down Expand Up @@ -130,6 +129,7 @@ public void resetData() {
// Clear inspection indexes // Clear inspection indexes
clearIndex(IssueIndexDefinition.INDEX); clearIndex(IssueIndexDefinition.INDEX);
clearIndex(SourceLineIndexDefinition.INDEX); clearIndex(SourceLineIndexDefinition.INDEX);
clearIndex(ViewIndexDefinition.INDEX);


} finally { } finally {
dbSession.close(); dbSession.close();
Expand Down Expand Up @@ -161,7 +161,7 @@ private void deleteManualRules(Connection connection) {
* Completely remove a index with all types * Completely remove a index with all types
*/ */
public void clearIndex(String indexName) { public void clearIndex(String indexName) {
searchClient.prepareDeleteByQuery(searchClient.prepareState().get() esClient.prepareDeleteByQuery(esClient.prepareState().get()
.getState().getMetaData().concreteIndices(new String[] {indexName})) .getState().getMetaData().concreteIndices(new String[] {indexName}))
.setQuery(QueryBuilders.matchAllQuery()) .setQuery(QueryBuilders.matchAllQuery())
.get(); .get();
Expand All @@ -171,7 +171,7 @@ public void clearIndex(String indexName) {
* Remove only the type of an index * Remove only the type of an index
*/ */
public void clearIndexType(IndexDefinition indexDefinition) { public void clearIndexType(IndexDefinition indexDefinition) {
searchClient.prepareDeleteByQuery(searchClient.prepareState().get() esClient.prepareDeleteByQuery(esClient.prepareState().get()
.getState().getMetaData().concreteIndices(new String[] {indexDefinition.getIndexName()})).setTypes(indexDefinition.getIndexType()) .getState().getMetaData().concreteIndices(new String[] {indexDefinition.getIndexName()})).setTypes(indexDefinition.getIndexType())
.setQuery(QueryBuilders.matchAllQuery()) .setQuery(QueryBuilders.matchAllQuery())
.get(); .get();
Expand Down
Expand Up @@ -48,7 +48,7 @@ public class RuleDoc extends BaseDoc implements Rule {


public static final String MANUAL_REPOSITORY = "manual"; public static final String MANUAL_REPOSITORY = "manual";


RuleDoc(Map<String, Object> fields) { public RuleDoc(Map<String, Object> fields) {
super(fields); super(fields);
} }


Expand Down Expand Up @@ -326,4 +326,5 @@ public boolean isManual() {
public String toString() { public String toString() {
return ReflectionToStringBuilder.toString(this); return ReflectionToStringBuilder.toString(this);
} }

} }
Expand Up @@ -20,6 +20,7 @@
package org.sonar.server.source.index; package org.sonar.server.source.index;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.sonar.server.search.BaseDoc; import org.sonar.server.search.BaseDoc;
import org.sonar.server.search.BaseNormalizer; import org.sonar.server.search.BaseNormalizer;
import org.sonar.server.search.IndexUtils; import org.sonar.server.search.IndexUtils;
Expand All @@ -29,7 +30,6 @@


import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;


public class SourceLineDoc extends BaseDoc { public class SourceLineDoc extends BaseDoc {
Expand All @@ -40,7 +40,7 @@ public SourceLineDoc(Map<String, Object> fields) {


// For testing purpose // For testing purpose
public SourceLineDoc() { public SourceLineDoc() {
this(new HashMap<String, Object>()); this(Maps.<String, Object>newHashMap());
} }


public String projectUuid() { public String projectUuid() {
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.user.index; package org.sonar.server.user.index;


import com.google.common.collect.Maps;
import org.sonar.api.user.User; import org.sonar.api.user.User;
import org.sonar.server.search.BaseDoc; import org.sonar.server.search.BaseDoc;


Expand All @@ -33,6 +34,10 @@ public UserDoc(Map<String, Object> fields) {
super(fields); super(fields);
} }


public UserDoc() {
this(Maps.<String, Object>newHashMap());
}

@Override @Override
public String login() { public String login() {
return getField(UserIndexDefinition.FIELD_LOGIN); return getField(UserIndexDefinition.FIELD_LOGIN);
Expand Down Expand Up @@ -66,31 +71,38 @@ public long updatedAt() {
return getField(UserIndexDefinition.FIELD_UPDATED_AT); return getField(UserIndexDefinition.FIELD_UPDATED_AT);
} }


public void setLogin(@Nullable String s) { public UserDoc setLogin(@Nullable String s) {
setField(UserIndexDefinition.FIELD_LOGIN, s); setField(UserIndexDefinition.FIELD_LOGIN, s);
return this;
} }


public void setName(@Nullable String s) { public UserDoc setName(@Nullable String s) {
setField(UserIndexDefinition.FIELD_NAME, s); setField(UserIndexDefinition.FIELD_NAME, s);
return this;
} }


public void setEmail(@Nullable String s) { public UserDoc setEmail(@Nullable String s) {
setField(UserIndexDefinition.FIELD_EMAIL, s); setField(UserIndexDefinition.FIELD_EMAIL, s);
return this;
} }


public void setActive(boolean b) { public UserDoc setActive(boolean b) {
setField(UserIndexDefinition.FIELD_ACTIVE, b); setField(UserIndexDefinition.FIELD_ACTIVE, b);
return this;
} }


public void setScmAccounts(@Nullable List<String> s) { public UserDoc setScmAccounts(@Nullable List<String> s) {
setField(UserIndexDefinition.FIELD_SCM_ACCOUNTS, s); setField(UserIndexDefinition.FIELD_SCM_ACCOUNTS, s);
return this;
} }


public void setCreatedAt(long l) { public UserDoc setCreatedAt(long l) {
setField(UserIndexDefinition.FIELD_CREATED_AT, l); setField(UserIndexDefinition.FIELD_CREATED_AT, l);
return this;
} }


public void setUpdatedAt(long l) { public UserDoc setUpdatedAt(long l) {
setField(UserIndexDefinition.FIELD_UPDATED_AT, l); setField(UserIndexDefinition.FIELD_UPDATED_AT, l);
return this;
} }
} }

0 comments on commit 3601d3a

Please sign in to comment.