Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix incorrect detector count in stats APIs (#129)
Browse files Browse the repository at this point in the history
Previously, we use index stats to retrieve doc count. But the stats are emitted by Lucene and include nested doc count.  Since our detector index uses nested type for the feature_attributes field, stats API would overcount the number of docs.

This PR restores to use search all of the index docs and fetch total hits as doc count.

Testing done:
* manually verified by starting a local cluster
  • Loading branch information
kaituo authored May 20, 2020
1 parent 72a59eb commit 9f01f13
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import com.google.common.collect.ImmutableList;
import com.amazon.opendistroforelasticsearch.ad.util.DiscoveryNodeFilterer;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.service.ClusterService;
Expand Down Expand Up @@ -176,11 +175,13 @@ private void getClusterStats(
ADStatsResponse adStatsResponse = new ADStatsResponse();
if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName())) {
if (clusterService.state().getRoutingTable().hasIndex(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest().docs(true);
client.execute(IndicesStatsAction.INSTANCE, indicesStatsRequest, ActionListener.wrap(indicesStatsResponse -> {
adStats
.getStat(StatNames.DETECTOR_COUNT.getName())
.setValue(indicesStatsResponse.getIndex(AnomalyDetector.ANOMALY_DETECTORS_INDEX).getPrimaries().docs.getCount());
final SearchRequest request = client
.prepareSearch(AnomalyDetector.ANOMALY_DETECTORS_INDEX)
.setSize(0)
.setTrackTotalHits(true)
.request();
client.search(request, ActionListener.wrap(indicesStatsResponse -> {
adStats.getStat(StatNames.DETECTOR_COUNT.getName()).setValue(indicesStatsResponse.getHits().getTotalHits().value);
adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
listener.onResponse(adStatsResponse);
}, e -> listener.onFailure(new RuntimeException("Failed to get AD cluster stats", e))));
Expand Down

0 comments on commit 9f01f13

Please sign in to comment.