Skip to content

Commit

Permalink
HSEARCH-3352 Get took and timedOut with Lucene query
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Dec 13, 2019
1 parent 3b96bc3 commit 179fe73
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package org.hibernate.search.backend.elasticsearch.search.query;

import java.time.Duration;

import org.hibernate.search.engine.search.query.SearchResult;

import com.google.gson.JsonObject;
Expand All @@ -30,14 +28,4 @@ public interface ElasticsearchSearchResult<H> extends SearchResult<H> {
*/
JsonObject getResponseBody();

/**
* @return the time the Elasticsearch server took to process the request, as a {@link Duration}
*/
Duration getTook();

/**
* @return whether or not a timeout occurred on the server side processing the request.
*/
boolean isTimedOut();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.search.backend.elasticsearch.search.query.impl;

import java.time.Duration;
import java.util.List;
import java.util.Map;

Expand All @@ -21,29 +20,14 @@ class ElasticsearchSearchResultImpl<H> extends SimpleSearchResult<H>

private final JsonObject responseBody;

private final Duration took;
private final boolean timedOut;

ElasticsearchSearchResultImpl(JsonObject responseBody,
long hitCount, List<H> hits, Map<AggregationKey<?>, ?> aggregationResults, Integer took, Boolean timedOut) {
super( hitCount, hits, aggregationResults );
super( hitCount, hits, aggregationResults, took, timedOut );
this.responseBody = responseBody;
this.took = Duration.ofMillis( took );
this.timedOut = ( timedOut != null ) ? timedOut : false;
}

@Override
public JsonObject getResponseBody() {
return responseBody;
}

@Override
public Duration getTook() {
return took;
}

@Override
public boolean isTimedOut() {
return timedOut;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ public class LuceneLoadableSearchResult<H> {
private final long hitCount;
private List<Object> extractedData;
private final Map<AggregationKey<?>, ?> extractedAggregations;
private Integer took;
private Boolean timedOut;

LuceneLoadableSearchResult(LuceneSearchQueryExtractContext extractContext,
LuceneSearchProjection<?, H> rootProjection,
long hitCount, List<Object> extractedData,
Map<AggregationKey<?>, ?> extractedAggregations) {
Map<AggregationKey<?>, ?> extractedAggregations,
Integer took, boolean timedOut) {
this.extractContext = extractContext;
this.rootProjection = rootProjection;
this.hitCount = hitCount;
this.extractedData = extractedData;
this.extractedAggregations = extractedAggregations;
this.took = took;
this.timedOut = timedOut;
}

LuceneSearchResult<H> loadBlocking() {
Expand Down Expand Up @@ -82,6 +87,6 @@ LuceneSearchResult<H> loadBlocking() {
// Make sure that if someone uses this object incorrectly, it will always fail, and will fail early.
extractedData = null;

return new LuceneSearchResultImpl<>( hitCount, loadedHits, extractedAggregations );
return new LuceneSearchResultImpl<>( hitCount, loadedHits, extractedAggregations, took, timedOut );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

class LuceneSearchResultImpl<H> extends SimpleSearchResult<H>
implements LuceneSearchResult<H> {
LuceneSearchResultImpl(long hitCount, List<H> hits, Map<AggregationKey<?>, ?> aggregationResults) {
super( hitCount, hits, aggregationResults );
LuceneSearchResultImpl(long hitCount, List<H> hits, Map<AggregationKey<?>, ?> aggregationResults,
Integer took, Boolean timedOut) {
super( hitCount, hits, aggregationResults, took, timedOut );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public LuceneLoadableSearchResult<H> search(IndexSearcher indexSearcher, int off
extractContext, rootProjection,
luceneCollectors.getTotalHits(),
extractedData,
extractedAggregations
extractedAggregations,
timeoutManager.getTookTimeInMilliseconds(),
timeoutManager.isTimedOut()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface TimeoutManager {

Type getType();

Integer getTookTimeInMilliseconds();

enum Type {
NONE,
EXCEPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package org.hibernate.search.backend.lucene.search.timeout.impl;

import java.lang.invoke.MethodHandles;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

Expand All @@ -22,6 +24,7 @@
public class TimeoutManagerImpl implements TimeoutManager {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
public static final int NANOS_IN_ONE_MILLI = 1000000;

private final Query query;

Expand All @@ -41,10 +44,10 @@ public TimeoutManagerImpl(Query query) {
*/
@Override
public void start() {
this.start = System.nanoTime();
if ( timeout == null ) {
return;
}
this.start = System.nanoTime();
this.partialResults = false;
}

Expand Down Expand Up @@ -178,5 +181,12 @@ public boolean hasPartialResults() {
public Type getType() {
return type;
}

@Override
public Integer getTookTimeInMilliseconds() {
return BigDecimal.valueOf( System.nanoTime() - start )
.divide( BigDecimal.valueOf( NANOS_IN_ONE_MILLI ), RoundingMode.CEILING )
.intValue();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.search.engine.search.query;

import java.time.Duration;
import java.util.List;

import org.hibernate.search.engine.search.aggregation.AggregationKey;
Expand Down Expand Up @@ -34,4 +35,14 @@ public interface SearchResult<H> {
*/
<A> A getAggregation(AggregationKey<A> key);

/**
* @return the time taken to process the request, as a {@link Duration}
*/
Duration getTook();

/**
* @return whether or not a timeout occurred processing the request.
*/
boolean isTimedOut();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package org.hibernate.search.engine.search.query.spi;

import java.lang.invoke.MethodHandles;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.engine.search.aggregation.AggregationKey;
Expand All @@ -22,11 +24,16 @@ public class SimpleSearchResult<H> implements SearchResult<H> {
private final long hitCount;
private final List<H> hits;
private final Map<AggregationKey<?>, ?> aggregationResults;
private final Duration took;
private final boolean timedOut;

public SimpleSearchResult(long hitCount, List<H> hits, Map<AggregationKey<?>, ?> aggregationResults) {
public SimpleSearchResult(long hitCount, List<H> hits, Map<AggregationKey<?>, ?> aggregationResults,
Integer took, Boolean timedOut) {
this.hitCount = hitCount;
this.hits = hits;
this.aggregationResults = aggregationResults;
this.took = ( took == null ) ? null : Duration.ofMillis( took );
this.timedOut = ( timedOut != null ) && timedOut;
}

@Override
Expand All @@ -49,12 +56,24 @@ public <T> T getAggregation(AggregationKey<T> key) {
return (T) aggregationResult;
}

@Override
public Duration getTook() {
return took;
}

@Override
public boolean isTimedOut() {
return timedOut;
}

@Override
public String toString() {
return getClass().getSimpleName() + "["
+ "hitCount=" + hitCount
+ ", hits=" + hits
+ ", aggregations=" + aggregationResults
+ "]";
return new StringJoiner( ", ", SimpleSearchResult.class.getSimpleName() + "[", "]" )
.add( "hitCount=" + hitCount )
.add( "hits=" + hits )
.add( "aggregationResults=" + aggregationResults )
.add( "took=" + took )
.add( "timedOut=" + timedOut )
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.search.integrationtest.backend.lucene.search.query;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMapperUtils.referenceProvider;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -99,6 +100,9 @@ public void timeout_smallQuery_largeTimeout() {

SearchResult<DocumentReference> result = query.fetchAll();
SearchResultAssert.assertThat( result ).hasNoHits();

assertThat( result.getTook() ).isNotNull();
assertThat( result.isTimedOut() ).isFalse();
}

private void initData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public <U> CallBehavior<SearchResult<U>> verify(SearchWorkCall<U> actualCall) {
actualCall.rootProjection,
behavior.getRawHits()
),
Collections.emptyMap()
Collections.emptyMap(),
0, false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public <T> SearchResult<T> executeSearchWork(Set<String> indexNames, StubSearchW
new SearchWorkCall<>( indexNames, work, projectionContext, loadingContext, rootProjection ),
(call1, call2) -> call1.verify( call2 ),
noExpectationsBehavior( () -> new SimpleSearchResult<>(
0L, Collections.emptyList(), Collections.emptyMap()
0L, Collections.emptyList(), Collections.emptyMap(), 0, false
) )
);
}
Expand Down

0 comments on commit 179fe73

Please sign in to comment.