You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/05/21 03:14:37 UTC

[james-project] 05/06: JAMES-2765 Avoid scrolling for quota search

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 2e361b291cfa75ad3bcb0250cacb212b18310864
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon May 20 15:54:25 2019 +0700

    JAMES-2765 Avoid scrolling for quota search
    
    We can easily rely on from+limit at the request level and avoid the scroll entirely.
    
    Scrolling is expensive so should rather be avoided - scroll context is kept for a long time, waisting ES server resources.
---
 .../elasticsearch/ElasticSearchQuotaSearcher.java  | 34 +++++++++-------------
 1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/mailbox/plugin/quota-search-elasticsearch-v6/src/main/java/org/apache/james/quota/search/elasticsearch/ElasticSearchQuotaSearcher.java b/mailbox/plugin/quota-search-elasticsearch-v6/src/main/java/org/apache/james/quota/search/elasticsearch/ElasticSearchQuotaSearcher.java
index 19cefef..b08c4d8 100644
--- a/mailbox/plugin/quota-search-elasticsearch-v6/src/main/java/org/apache/james/quota/search/elasticsearch/ElasticSearchQuotaSearcher.java
+++ b/mailbox/plugin/quota-search-elasticsearch-v6/src/main/java/org/apache/james/quota/search/elasticsearch/ElasticSearchQuotaSearcher.java
@@ -21,20 +21,19 @@ package org.apache.james.quota.search.elasticsearch;
 
 import static org.apache.james.quota.search.elasticsearch.json.JsonMessageConstants.USER;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
-import java.util.stream.Stream;
 
 import org.apache.james.backends.es.v6.AliasName;
 import org.apache.james.backends.es.v6.NodeMappingFactory;
 import org.apache.james.backends.es.v6.ReadAliasName;
-import org.apache.james.backends.es.v6.search.ScrollIterable;
 import org.apache.james.core.User;
 import org.apache.james.quota.search.QuotaQuery;
 import org.apache.james.quota.search.QuotaSearcher;
 import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.client.RequestOptions;
 import org.elasticsearch.client.RestHighLevelClient;
-import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
 import org.elasticsearch.search.sort.SortBuilders;
@@ -43,8 +42,6 @@ import org.elasticsearch.search.sort.SortOrder;
 import com.github.steveash.guavate.Guavate;
 
 public class ElasticSearchQuotaSearcher implements QuotaSearcher {
-    private static final TimeValue TIMEOUT = TimeValue.timeValueMinutes(1);
-
     private final RestHighLevelClient client;
     private final AliasName readAlias;
     private final QuotaQueryConverter quotaQueryConverter;
@@ -57,25 +54,24 @@ public class ElasticSearchQuotaSearcher implements QuotaSearcher {
 
     @Override
     public List<User> search(QuotaQuery query) {
-        Stream<User> results = new ScrollIterable(client, prepareSearch(query))
-            .stream()
-            .flatMap(searchResponse -> Arrays.stream(searchResponse.getHits()
-                .getHits()))
-            .map(SearchHit::getId)
-            .map(User::fromUsername)
-            .skip(query.getOffset().getValue());
-
-        return query.getLimit().getValue()
-            .map(results::limit)
-            .orElse(results)
-            .collect(Guavate.toImmutableList());
+        try {
+            return Arrays.stream(client.search(prepareSearch(query), RequestOptions.DEFAULT)
+                .getHits()
+                .getHits())
+                .map(SearchHit::getId)
+                .map(User::fromUsername)
+                .collect(Guavate.toImmutableList());
+        } catch (IOException e) {
+            throw new RuntimeException("Unexpected exception while executing " + query, e);
+        }
     }
 
     private SearchRequest prepareSearch(QuotaQuery query) {
         SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
             .query(quotaQueryConverter.from(query))
             .sort(SortBuilders.fieldSort(USER)
-                .order(SortOrder.ASC));
+                .order(SortOrder.ASC))
+            .from(query.getOffset().getValue());
 
         query.getLimit()
             .getValue()
@@ -83,8 +79,6 @@ public class ElasticSearchQuotaSearcher implements QuotaSearcher {
 
         return new SearchRequest(readAlias.getValue())
             .types(NodeMappingFactory.DEFAULT_MAPPING_NAME)
-            .scroll(TIMEOUT)
             .source(sourceBuilder);
     }
-
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org