aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/searchcluster/SearchGroupsImpl.java
blob: c49a140804c6247c493e9152493e21e2aff8e602 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch.searchcluster;

import com.google.common.math.Quantiles;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class SearchGroupsImpl implements SearchGroups {

    private final Map<Integer, Group> groups;
    private final double minActivedocsPercentage;

    public SearchGroupsImpl(Map<Integer, Group> groups, double minActivedocsPercentage) {
        this.groups = Map.copyOf(groups);
        this.minActivedocsPercentage = minActivedocsPercentage;
    }

    @Override public Group get(int id) { return groups.get(id); }
    @Override public Set<Integer> keys() { return groups.keySet();}
    @Override public Collection<Group> groups() { return groups.values(); }
    @Override public int size() { return groups.size(); }

    @Override
    public boolean isPartialGroupCoverageSufficient(Collection<Node> nodes) {
        if (size() == 1)
            return true;
        long activeDocuments = nodes.stream().mapToLong(Node::getActiveDocuments).sum();
        return isGroupCoverageSufficient(activeDocuments, medianDocumentsPerGroup());
    }

    public boolean isGroupCoverageSufficient(long activeDocuments, long medianDocuments) {
        if (medianDocuments <= 0) return true;
        double documentCoverage = 100.0 * (double) activeDocuments / medianDocuments;
        return documentCoverage >= minActivedocsPercentage;
    }

    public long medianDocumentsPerGroup() {
        if (isEmpty()) return 0;
        double[] activeDocuments = groups().stream().mapToDouble(Group::activeDocuments).toArray();
        return (long) Quantiles.median().computeInPlace(activeDocuments);
    }

}