summaryrefslogtreecommitdiffstats
path: root/statistics/src/main/java/com/yahoo/statistics/Counter.java
blob: f4b477636aa0f59c3acdb3cdd3693e9d35c3afa6 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.statistics;


import java.util.concurrent.atomic.AtomicLong;

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


/**
 * A single integer value which can be incremented.
 *
 * @author  Steinar Knutsen
 */
public class Counter extends Handle {
    // The current value of this counter
    private AtomicLong current = new AtomicLong(0L);

    // Whether or not this counter shall be reset between each logging
    // interval
    private final boolean resetCounter;

    /**
     * A monotonically increasing 64 bit integer value.
     *
     * @param name
     *            The name of this counter, for use in logging.
     * @param manager
     *            the statistics manager acquired by injection
     * @param fetchParametersFromConfig
     *            Whether or not this counter should be initialized from config.
     */
    public Counter(String name, Statistics manager, boolean fetchParametersFromConfig) {
        this(name, manager, fetchParametersFromConfig, null, false, true);
    }

    /**
     * A monotonically increasing 64 bit integer value.
     *
     * @param name
     *            The name of this counter, for use in logging.
     * @param manager
     *            the statistics manager acquired by injection
     * @param fetchParametersFromConfig
     *            Whether or not this counter should be initialized from config.
     * @param callback
     *            will be invoked each time this counter is written to the log
     * @param resetCounter
     *            Control for if this Counter should be reset between each
     *            logging interval.
     */
    public Counter(String name, Statistics manager,
                   boolean fetchParametersFromConfig, Callback callback, boolean resetCounter) {
                this(name, manager, fetchParametersFromConfig, callback,
                        resetCounter, true);
            }

    /**
     * A monotonically increasing 64 bit integer value. Do not make this
     * constructor public, it is used for creating unregistered counters.
     *
     * @param name
     *            The name of this counter, for use in logging.
     * @param manager
     *            the statistics manager acquired by injection
     * @param fetchParametersFromConfig
     *            Whether or not this counter should be initialized from config.
     * @param callback
     *            will be invoked each time this counter is written to the log
     * @param resetCounter
     *            Control for if this Counter should be reset between each
     *            logging interval.
     * @param register
     *            Whether to register the counter in the statistics manager
     */
    private Counter(String name, Statistics manager,
            boolean fetchParametersFromConfig, Callback callback,
            boolean resetCounter, boolean register) {
        super(name, manager, callback);
        if (fetchParametersFromConfig) {
            StatisticsConfig config = manager.getConfig();
            this.resetCounter = getResetCounter(name, config);
        } else {
            this.resetCounter = resetCounter;
        }
        if (register) {
            manager.register(this);
        }
    }


    /**
     * Get a Counter instance not registered in the statistics manager. This is
     * used by CounterGroup and should not be made public.
     *
     * @param name
     *            The name of this counter, for use in logging.
     * @param resetCounter
     *            Control for if this Counter should be reset between each
     *            logging interval.
     */
    static Counter intializeUnregisteredCounter(String name,
            boolean resetCounter) {
        return new Counter(name, null, false, null, resetCounter, false);
    }

    /**
     * If this Counter is set up to read config, configure it
     * according to the config given.
     */
    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 by 1.
     */
    public void increment() {
        current.incrementAndGet();
    }

    /**
     * Increment by n.
     */
    public void increment(long n) {
        current.addAndGet(n);
    }

    /**
     * @return current value of this counter
     */
    public long get() {
        return current.get();
    }

    /**
     * The reset counter is true if this is counter is reset to 0 between each
     * logging interval.
     *
     * @return whether this counter is reset between each logging interval.
     */
    public boolean getResetCounter() {
        return resetCounter;
    }

    /**
     * If this counter should be set to 0 between each logging interval,
     * do that.
     */
    public void reset() {
        if (resetCounter) {
            current.set(0L);
        }
    }

    /**
     * Log current state and reset.
     */
    @Override
    public void runHandle() {
        String name = getName();
        long lastCurrent;
        boolean resetState = getResetCounter();

        if (resetState) {
            lastCurrent = current.getAndSet(0L);
            Event.value(name, lastCurrent);
        } else {
            lastCurrent = current.get();
            Event.count(name, lastCurrent);
        }
    }

    @Override
    public String toString() {
        return super.toString() + " " + getName() + " " + current;
    }

    CounterProxy getProxyAndReset() {
        CounterProxy c = new CounterProxy(getName());
        if (getResetCounter()) {
            c.setRaw(current.getAndSet(0L));
        } else {
            c.setRaw(current.get());
        }
        return c;
    }

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

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

}