|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Licensed to Elasticsearch under one or more contributor |
| 11 | + * license agreements. See the NOTICE file distributed with |
| 12 | + * this work for additional information regarding copyright |
| 13 | + * ownership. Elasticsearch licenses this file to you under |
| 14 | + * the Apache License, Version 2.0 (the "License"); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, |
| 21 | + * software distributed under the License is distributed on an |
| 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | + * KIND, either express or implied. See the License for the |
| 24 | + * specific language governing permissions and limitations |
| 25 | + * under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | + * Modifications Copyright OpenSearch Contributors. See |
| 30 | + * GitHub history for details. |
| 31 | + */ |
| 32 | + |
| 33 | +package org.opensearch.search.msearch; |
| 34 | + |
| 35 | +import org.opensearch.ExceptionsHelper; |
| 36 | +import org.opensearch.action.admin.indices.stats.SearchResponseStatusStats; |
| 37 | +import org.opensearch.action.search.MultiSearchRequestBuilder; |
| 38 | +import org.opensearch.action.search.MultiSearchResponse; |
| 39 | +import org.opensearch.action.search.SearchResponse; |
| 40 | +import org.opensearch.index.query.QueryBuilders; |
| 41 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 42 | +import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
| 43 | +import org.opensearch.test.OpenSearchIntegTestCase.Scope; |
| 44 | + |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.Comparator; |
| 47 | +import java.util.concurrent.atomic.LongAdder; |
| 48 | + |
| 49 | +@ClusterScope(scope = Scope.TEST, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) |
| 50 | +public class MultiSearchStatsIT extends OpenSearchIntegTestCase { |
| 51 | + private final SearchResponseStatusStats expectedSearchResponseStatusStats = new SearchResponseStatusStats(); |
| 52 | + |
| 53 | + public void testNodeIndicesStatsDocStatusStatsIndexBulk() { |
| 54 | + createIndex("test"); |
| 55 | + ensureGreen(); |
| 56 | + client().prepareIndex("test").setId("1").setSource("field", "xxx").get(); |
| 57 | + client().prepareIndex("test").setId("2").setSource("field", "yyy").get(); |
| 58 | + refresh(); |
| 59 | + |
| 60 | + int failureCount = randomIntBetween(0, 50); |
| 61 | + int successCount = randomIntBetween(0, 50); |
| 62 | + |
| 63 | + MultiSearchRequestBuilder multiSearchRequestBuilder = client().prepareMultiSearch(); |
| 64 | + |
| 65 | + for (int i = 0; i < failureCount; i++) { |
| 66 | + multiSearchRequestBuilder.add(client().prepareSearch("noIndex").setQuery(QueryBuilders.termQuery("field", "yyy"))); |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = 0; i < successCount; i++) { |
| 70 | + multiSearchRequestBuilder.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())); |
| 71 | + } |
| 72 | + |
| 73 | + MultiSearchResponse response = multiSearchRequestBuilder.get(); |
| 74 | + |
| 75 | + for (MultiSearchResponse.Item item : response) { |
| 76 | + if (item.isFailure()) { |
| 77 | + updateExpectedDocStatusCounter(expectedSearchResponseStatusStats, item.getFailure()); |
| 78 | + } else { |
| 79 | + updateExpectedDocStatusCounter(expectedSearchResponseStatusStats, item.getResponse()); |
| 80 | + } |
| 81 | + } |
| 82 | + assertSearchResponseStatusStats(); |
| 83 | + } |
| 84 | + |
| 85 | + private void assertSearchResponseStatusStats() { |
| 86 | + SearchResponseStatusStats searchResponseStatusStats = client().admin() |
| 87 | + .cluster() |
| 88 | + .prepareNodesStats() |
| 89 | + .execute() |
| 90 | + .actionGet() |
| 91 | + .getNodes() |
| 92 | + .get(0) |
| 93 | + .getIndices() |
| 94 | + .getStatusCounterStats() |
| 95 | + .getSearchResponseStatusStats(); |
| 96 | + |
| 97 | + Arrays.equals( |
| 98 | + searchResponseStatusStats.getSearchResponseStatusCounter(), |
| 99 | + expectedSearchResponseStatusStats.getSearchResponseStatusCounter(), |
| 100 | + Comparator.comparingLong(LongAdder::longValue) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private void updateExpectedDocStatusCounter(SearchResponseStatusStats expectedSearchResponseStatusStats, SearchResponse r) { |
| 105 | + expectedSearchResponseStatusStats.inc(r.status()); |
| 106 | + } |
| 107 | + |
| 108 | + private void updateExpectedDocStatusCounter(SearchResponseStatusStats expectedSearchResponseStatusStats, Exception e) { |
| 109 | + expectedSearchResponseStatusStats.inc(ExceptionsHelper.status(e)); |
| 110 | + } |
| 111 | +} |
0 commit comments