aboutsummaryrefslogtreecommitdiffstats
path: root/statistics/src/main/java/com/yahoo/statistics/CounterGroup.java
blob: 39d8ad19f296ad64a0325420dbb7d8035c1cf021 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.statistics;


import com.yahoo.container.StatisticsConfig;
import com.yahoo.log.event.Event;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;


/**
 * A set of associated counters.
 *
 * @author  <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
@Deprecated
public class CounterGroup extends Group {
    private final boolean resetCounter;

    // A map for names of subevents and Value instances
    private Map<String, Counter> subEvents = new HashMap<>();

    /**
     * @param name The symbolic name of this group of counters.
     */
    public CounterGroup(String name, Statistics manager) {
        this(name, manager, true);
    }

    /**
     * Create a basic group of counter which may or may not depend on config.
     *
     * @param name
     *            The symbolic name of this group of counters.
     * @param manager
     *            the statistics manager acquired by injection
     * @param fetchParametersFromConfig
     *            Whether this Group should be configured from config.
     */
    public CounterGroup(String name, Statistics manager,
            boolean fetchParametersFromConfig) {
        this(name, manager, fetchParametersFromConfig, null, false);
    }

    /**
     * Create a group of counters with a callback included.
     *
     * @param name
     *            The symbolic name of this group of counters.
     * @param manager
     *            the statistics manager acquired by injection
     * @param fetchParametersFromConfig
     *            Whether this Group should be configured from config.
     * @param callback
     *            will be invoked each time data is written to the log
     * @param resetCounter
     *            Control for if this group should be reset between each
     *            logging interval.
     */
    public CounterGroup(String name, Statistics manager,
            boolean fetchParametersFromConfig, Callback callback, boolean resetCounter) {

        super(name, manager, callback);
        if (fetchParametersFromConfig) {
            StatisticsConfig config = manager.getConfig();
            this.resetCounter = getResetCounter(name, config);
        } else {
            this.resetCounter = resetCounter;
        }
        manager.register(this);
    }

    private static boolean getResetCounter(String name, StatisticsConfig config) {
        for (int i = 0; i < config.counterresets().size(); i++) {
            String configName = config.counterresets(i).name();
            if (configName.equals(name)) {
                return true;
            }
        }
        return false;
    }


    /**
     * Increment named contained counter by 1.
     */
    public void increment(String name) {
        Counter c = getCounter(name);
        c.increment();
    }

    /**
     * Increment named contained counter by n.
     */
    public void increment(String name, long n) {
        Counter c = getCounter(name);
        c.increment(n);
    }

    /**
     * Get a counter with a given name, creates a new counter if no
     * counter with the name given exists.
     */
    synchronized Counter getCounter(String name) {
        Counter c = subEvents.get(name);
        if (c == null) {
            c = getNewCounter(name);
        }
        return c;
    }

    private Counter getNewCounter(String subName) {
        Counter c = Counter.intializeUnregisteredCounter(subName, resetCounter);
        subEvents.put(subName, c);
        return c;
    }

    /**
     * Dump contained counters to log and reset.
     */
    @Override
    public void runHandle() {
        StringBuilder multi = new StringBuilder();
        CounterProxy[] proxies;
        int i = 0;

        // this is to make sure the number of events does not change while logging
        synchronized (this) {
            proxies = new CounterProxy[subEvents.size()];
            i = 0;
            for (Iterator<Counter> j = subEvents.values().iterator(); j
                    .hasNext();) {
                Counter c = j.next();
                proxies[i] = c.getProxyAndReset();
                i++;
            }
        }

        while (i > 0) {
            i--;
            if (multi.length() > 0) {
                multi.append(", ");
            }
            multi.append(proxies[i].getName());
            multi.append("=");
            multi.append(proxies[i].getRaw());
        }

        Event.countGroup(getName(), multi.toString());
    }

    @Override
    public boolean equals(Object o) {
        if (o.getClass() != this.getClass()) {
            return false;
        }
        CounterGroup other = (CounterGroup) o;
        return getName().equals(other.getName());
    }

    @Override
    public int hashCode() {
        return getName().hashCode() + 31 * "CounterGroup".hashCode();
    }
}