|
37 | 37 | import org.opensearch.action.search.SearchPhaseName;
|
38 | 38 | import org.opensearch.action.search.SearchRequestOperationsListenerSupport;
|
39 | 39 | import org.opensearch.action.search.SearchRequestStats;
|
| 40 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
40 | 41 | import org.opensearch.common.settings.ClusterSettings;
|
41 | 42 | import org.opensearch.common.settings.Settings;
|
42 | 43 | import org.opensearch.index.search.stats.SearchStats.Stats;
|
|
52 | 53 |
|
53 | 54 | public class SearchStatsTests extends OpenSearchTestCase implements SearchRequestOperationsListenerSupport {
|
54 | 55 |
|
| 56 | + public void testNegativeRequestStats() throws Exception { |
| 57 | + SearchStats searchStats = new SearchStats(new Stats(), 0, new HashMap<>()); |
| 58 | + |
| 59 | + long paramValue = randomIntBetween(2, 50); |
| 60 | + |
| 61 | + // Testing for request stats |
| 62 | + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 63 | + SearchRequestStats testRequestStats = new SearchRequestStats(clusterSettings); |
| 64 | + SearchPhaseContext ctx = mock(SearchPhaseContext.class); |
| 65 | + for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) { |
| 66 | + SearchPhase mockSearchPhase = mock(SearchPhase.class); |
| 67 | + when(ctx.getCurrentPhase()).thenReturn(mockSearchPhase); |
| 68 | + when(mockSearchPhase.getStartTimeInNanos()).thenReturn(System.nanoTime() - TimeUnit.SECONDS.toNanos(paramValue)); |
| 69 | + when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName); |
| 70 | + for (int iterator = 0; iterator < paramValue; iterator++) { |
| 71 | + onPhaseStart(testRequestStats, ctx); |
| 72 | + onPhaseEnd(testRequestStats, ctx); |
| 73 | + onPhaseEnd(testRequestStats, ctx); // call onPhaseEnd() twice to make 'current' negative |
| 74 | + } |
| 75 | + } |
| 76 | + searchStats.setSearchRequestStats(testRequestStats); |
| 77 | + for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) { |
| 78 | + assertEquals( |
| 79 | + -1 * paramValue, // current is negative, equals -1 * paramValue (num loop iterations) |
| 80 | + searchStats.getTotal().getRequestStatsLongHolder().getRequestStatsHolder().get(searchPhaseName.getName()).current |
| 81 | + ); |
| 82 | + assertEquals( |
| 83 | + 2 * paramValue, |
| 84 | + searchStats.getTotal().getRequestStatsLongHolder().getRequestStatsHolder().get(searchPhaseName.getName()).total |
| 85 | + ); |
| 86 | + assertThat( |
| 87 | + searchStats.getTotal().getRequestStatsLongHolder().getRequestStatsHolder().get(searchPhaseName.getName()).timeInMillis, |
| 88 | + greaterThanOrEqualTo(paramValue) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + // Ensure writeTo() does not throw error with negative 'current' |
| 93 | + searchStats.writeTo(new BytesStreamOutput(10)); |
| 94 | + } |
| 95 | + |
55 | 96 | // https://github.com/elastic/elasticsearch/issues/7644
|
56 | 97 | public void testShardLevelSearchGroupStats() throws Exception {
|
57 | 98 | // let's create two dummy search stats with groups
|
|
0 commit comments