summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/debug/BackendStatistics.java
blob: f8d46ec6851438587fe455e4dbf356b835dcd553 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.debug;

import static com.yahoo.search.debug.SearcherUtils.clusterSearchers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

import com.yahoo.fs4.mplex.Backend;
import com.yahoo.jrt.Int32Array;
import com.yahoo.jrt.Request;
import com.yahoo.jrt.StringArray;
import com.yahoo.jrt.Value;
import com.yahoo.jrt.Values;
import com.yahoo.prelude.cluster.ClusterSearcher;
import com.yahoo.yolean.Exceptions;

/**
 * @author tonytv
 */
public class BackendStatistics implements DebugMethodHandler {
    public JrtMethodSignature getSignature() {
        String returnTypes = "" + (char)Value.STRING_ARRAY + (char)Value.INT32_ARRAY + (char)Value.INT32_ARRAY;
        String parametersTypes = "" + (char)Value.STRING;

        return new JrtMethodSignature(returnTypes, parametersTypes);
    }

    public void invoke(Request request) {
        try {
            Collection<ClusterSearcher> searchers = clusterSearchers(request);
            List<String> backendIdentificators = new ArrayList<>();
            List<Integer> activeConnections = new ArrayList<>();
            List<Integer> totalConnections = new ArrayList<>();

            for (ClusterSearcher searcher : searchers) {
                for (Map.Entry<String,Backend.BackendStatistics> statistics : searcher.getBackendStatistics().entrySet()) {
                    backendIdentificators.add(statistics.getKey());
                    activeConnections.add(statistics.getValue().activeConnections);
                    totalConnections.add(statistics.getValue().totalConnections());
                }
            }
            Values returnValues = request.returnValues();
            returnValues.add(new StringArray(backendIdentificators.toArray(new String[0])));
            addInt32Array(returnValues, activeConnections);
            addInt32Array(returnValues, totalConnections);

        } catch (Exception e) {
            request.setError(1000, Exceptions.toMessageString(e));
        }
    }

    private void addInt32Array(Values returnValues, List<Integer> ints) {
        returnValues.add(new Int32Array(ArrayUtils.toPrimitive(ints.toArray(new Integer[0]))));
    }
}