This repository has been archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a configurable ES result size (#685)
- Loading branch information
Showing
17 changed files
with
1,245 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
heroic-component/src/main/java/com/spotify/heroic/suggest/NumSuggestionsLimit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2015 Spotify AB. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.spotify.heroic.suggest; | ||
|
||
import com.spotify.heroic.common.OptionalLimit; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A simple class to centralize logic around limiting the number of suggestions requested from ES by | ||
* Heroic. It defaults to 50 but any request to the backend (e.g. MemoryBackend) can override that | ||
* number. | ||
*/ | ||
public class NumSuggestionsLimit { | ||
|
||
/** No request is allowed to request more than this many tags, keys or tag values. */ | ||
public static final int LIMIT_CEILING = 250; | ||
|
||
/** | ||
* How many suggestions we should request from ES, unless the suggest API request specifies | ||
* otherwise. | ||
* | ||
* <p>This applies to the requests made for keys, tag and tag values. This defaults to 50, | ||
* otherwise * 10,000 is used as the default which is wasteful and could lag the grafana UI. | ||
*/ | ||
public static final int DEFAULT_LIMIT = 50; | ||
|
||
private final int limit; | ||
|
||
private NumSuggestionsLimit() { | ||
limit = DEFAULT_LIMIT; | ||
} | ||
|
||
private NumSuggestionsLimit(int limit) { | ||
int okLimit = Math.min(LIMIT_CEILING, limit); | ||
this.limit = okLimit; | ||
} | ||
|
||
public static NumSuggestionsLimit of(Optional<Integer> limit) { | ||
return limit.isEmpty() ? new NumSuggestionsLimit() : new NumSuggestionsLimit(limit.get()); | ||
} | ||
|
||
public static NumSuggestionsLimit of() { | ||
return new NumSuggestionsLimit(); | ||
} | ||
|
||
public static NumSuggestionsLimit of(int limit) { | ||
return new NumSuggestionsLimit(limit); | ||
} | ||
|
||
public int getLimit() { | ||
return limit; | ||
} | ||
|
||
/** | ||
* use this.limit unless limit is not empty, then return the numeric result. | ||
* | ||
* @param limit the limit to respect if non-empty, usually from a request object | ||
* @return a new NSL object - for fluent coding support | ||
*/ | ||
public NumSuggestionsLimit create(OptionalLimit limit) { | ||
int num = limit.orElse(OptionalLimit.of(this.limit)).asInteger().get(); | ||
return new NumSuggestionsLimit(num); | ||
} | ||
|
||
/** | ||
* use this.limit unless limit is not empty, then return the numeric result. | ||
* | ||
* @param limit the limit to respect if non-empty, usually from a request object | ||
* @return the resulting, updated numeric limit | ||
*/ | ||
public int calculateNewLimit(OptionalLimit limit) { | ||
return create(limit).getLimit(); | ||
} | ||
|
||
public OptionalLimit asOptionalLimit() { | ||
return OptionalLimit.of(limit); | ||
} | ||
|
||
public Optional<Integer> asOptionalInt() { | ||
return Optional.of(getLimit()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
heroic-component/src/test/java/com/spotify/heroic/suggest/NumSuggestionsLimitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.spotify.heroic.suggest; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.spotify.heroic.common.OptionalLimit; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class NumSuggestionsLimitTest { | ||
public static final int LIMIT_FORTY = 40; | ||
private NumSuggestionsLimit limit; | ||
|
||
@Before | ||
public void setup() { | ||
this.limit = NumSuggestionsLimit.of(LIMIT_FORTY); | ||
} | ||
|
||
@Test | ||
public void TestCorrectLimitIsApplied() { | ||
|
||
// Check that a supplied limit is selected | ||
int result = this.limit.calculateNewLimit(OptionalLimit.of(5)); | ||
assertEquals(5, result); | ||
|
||
// Check that none of the above have affected the NSL's stored number | ||
result = this.limit.calculateNewLimit(OptionalLimit.empty()); | ||
assertEquals(LIMIT_FORTY, result); | ||
|
||
// Check that a giant request limit is not respected | ||
result = this.limit.calculateNewLimit(OptionalLimit.of(10_000)); | ||
assertEquals(NumSuggestionsLimit.LIMIT_CEILING, result); | ||
|
||
// Check that none of the above have affected the NSL's stored number. The | ||
// above operations return a new object each time. | ||
assertEquals(LIMIT_FORTY, this.limit.getLimit()); | ||
} | ||
} |
Oops, something went wrong.