diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-11-14 15:17:07 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-11-15 10:08:46 +0100 |
commit | ff20b3c6df653a9e57b80735c7084b8f38373d7d (patch) | |
tree | 47ef544e073e75a307c04eb7f70185ed4c3487db /server | |
parent | 513c88cf9eabec3ba2359f642350458fe6dd654c (diff) | |
download | sonarqube-ff20b3c6df653a9e57b80735c7084b8f38373d7d.tar.gz sonarqube-ff20b3c6df653a9e57b80735c7084b8f38373d7d.zip |
SONAR-8005 Limit ES results to the first 10'000
Diffstat (limited to 'server')
4 files changed, 39 insertions, 18 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/SearchOptions.java b/server/sonar-server/src/main/java/org/sonar/server/es/SearchOptions.java index c40ae5fd0d3..505f59d297c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/SearchOptions.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/SearchOptions.java @@ -19,19 +19,18 @@ */ package org.sonar.server.es; -import com.google.common.base.Preconditions; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; - -import javax.annotation.Nullable; - import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; +import javax.annotation.Nullable; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.text.JsonWriter; + +import static com.google.common.base.Preconditions.checkArgument; /** * Various Elasticsearch request options: paging, fields and facets @@ -41,6 +40,7 @@ public class SearchOptions { public static final int DEFAULT_OFFSET = 0; public static final int DEFAULT_LIMIT = 10; public static final int MAX_LIMIT = 500; + private static final int MAX_RETURNABLE_RESULTS = 10_000; private int offset = DEFAULT_OFFSET; private int limit = DEFAULT_LIMIT; @@ -58,7 +58,7 @@ public class SearchOptions { * Sets the offset of the first result to return (zero-based). */ public SearchOptions setOffset(int offset) { - Preconditions.checkArgument(offset >= 0, "Offset must be positive"); + checkArgument(offset >= 0, "Offset must be positive"); this.offset = offset; return this; } @@ -68,7 +68,9 @@ public class SearchOptions { * {@link #MAX_LIMIT} is used. */ public SearchOptions setPage(int page, int pageSize) { - Preconditions.checkArgument(page >= 1, "Page must be greater or equal to 1 (got " + page + ")"); + checkArgument(page >= 1, "Page must be greater or equal to 1 (got " + page + ")"); + int lastResultIndex = page * pageSize; + checkArgument(lastResultIndex <= MAX_RETURNABLE_RESULTS, "Can return only the first %s results. %sth result asked.", MAX_RETURNABLE_RESULTS, lastResultIndex); setLimit(pageSize); setOffset((page * this.limit) - this.limit); return this; diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java index 6c4ed168846..248f9655292 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java @@ -19,7 +19,6 @@ */ package org.sonar.server.issue; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import java.util.Collection; import java.util.Collections; @@ -32,6 +31,7 @@ import org.sonar.api.rule.RuleKey; import org.sonar.server.search.QueryContext; import org.sonar.server.user.UserSession; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Sets.newHashSet; /** @@ -475,8 +475,7 @@ public class IssueQuery { public IssueQuery build() { if (issueKeys != null) { - Preconditions.checkArgument(issueKeys.size() <= QueryContext.MAX_LIMIT, "Number of issue keys must be less than " + QueryContext.MAX_LIMIT + " (got " + issueKeys.size() - + ")"); + checkArgument(issueKeys.size() <= QueryContext.MAX_LIMIT, "Number of issue keys must be less than " + QueryContext.MAX_LIMIT + " (got " + issueKeys.size() + ")"); } return new IssueQuery(this); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java index c82408e3c59..a275599d41a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchAction.java @@ -291,9 +291,7 @@ public class SearchAction implements IssuesWsAction { private SearchWsResponse doHandle(SearchWsRequest request, Request wsRequest) { // prepare the Elasticsearch request - SearchOptions options = new SearchOptions(); - options.setPage(request.getPage(), request.getPageSize()); - options.addFacets(request.getFacets()); + SearchOptions options = createSearchOptionsFromRequest(request); EnumSet<SearchAdditionalField> additionalFields = SearchAdditionalField.getFromRequest(request); IssueQuery query = issueQueryService.createFromRequest(request); @@ -329,7 +327,15 @@ public class SearchAction implements IssuesWsAction { return searchResponseFormat.formatSearch(additionalFields, data, paging, facets); } - private Facets reorderFacets(@Nullable Facets facets, Collection<String> orderedNames) { + private static SearchOptions createSearchOptionsFromRequest(SearchWsRequest request) { + SearchOptions options = new SearchOptions(); + options.setPage(request.getPage(), request.getPageSize()); + options.addFacets(request.getFacets()); + + return options; + } + + private static Facets reorderFacets(@Nullable Facets facets, Collection<String> orderedNames) { if (facets == null) { return null; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java index 24ca9cfeed8..d3a45193714 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java @@ -19,18 +19,24 @@ */ package org.sonar.server.es; +import java.io.StringWriter; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.search.QueryContext; import org.sonar.test.JsonAssert; -import java.io.StringWriter; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; public class SearchOptionsTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private SearchOptions underTest = new SearchOptions(); + @Test public void defaults() { SearchOptions options = new SearchOptions(); @@ -153,4 +159,12 @@ public class SearchOptionsTest { JsonAssert.assertJson(json.toString()).isSimilarTo("{\"paging\": {\"pageIndex\": 3, \"pageSize\": 10, \"total\": 30, \"fTotal\": \"30\", \"pages\": 3}}"); } + + @Test + public void fail_if_result_after_first_10_000() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Can return only the first 10000 results. 10500th result asked."); + + underTest.setPage(21, 500); + } } |