summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java
blob: 41b195baeb31bb900bf1619bdd7ee20f233ddd42 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import com.yahoo.component.Vtag;
import com.yahoo.container.core.ApplicationMetadataConfig;
import com.yahoo.container.jdisc.config.HealthMonitorConfig;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.Timer;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.application.MetricConsumer;
import com.yahoo.jdisc.handler.BufferedContentChannel;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.test.TestDriver;
import com.yahoo.metrics.MetricsPresentationConfig;
import com.yahoo.vespa.defaults.Defaults;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

/**
 * @author Simon Thoresen Hult
 */
public class StateHandlerTest {

    private final static long SNAPSHOT_INTERVAL = TimeUnit.SECONDS.toMillis(300);
    private final static long META_GENERATION = 69;
    private TestDriver driver;
    private StateMonitor monitor;
    private Metric metric;
    private final AtomicLong currentTimeMillis = new AtomicLong(0);

    @Before
    public void startTestDriver() {
        Timer timer = this.currentTimeMillis::get;
        this.driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() {
            @Override
            protected void configure() {
                bind(Timer.class).toInstance(timer);
            }
        });
        ContainerBuilder builder = driver.newContainerBuilder();
        HealthMonitorConfig healthMonitorConfig =
                new HealthMonitorConfig(
                        new HealthMonitorConfig.Builder()
                                .snapshot_interval(TimeUnit.MILLISECONDS.toSeconds(SNAPSHOT_INTERVAL)));
        ThreadFactory threadFactory = ignored -> mock(Thread.class);
        this.monitor = new StateMonitor(healthMonitorConfig, timer, threadFactory);
        builder.guiceModules().install(new AbstractModule() {

            @Override
            protected void configure() {
                bind(StateMonitor.class).toInstance(monitor);
                bind(MetricConsumer.class).toProvider(MetricConsumerProviders.wrap(monitor));
                bind(ApplicationMetadataConfig.class).toInstance(new ApplicationMetadataConfig(
                        new ApplicationMetadataConfig.Builder().generation(META_GENERATION)));
                bind(MetricsPresentationConfig.class)
                        .toInstance(new MetricsPresentationConfig(new MetricsPresentationConfig.Builder()));
            }
        });
        builder.serverBindings().bind("http://*/*", builder.getInstance(StateHandler.class));
        driver.activateContainer(builder);
        metric = builder.getInstance(Metric.class);
    }

    @After
    public void stopTestDriver() {
        assertTrue(driver.close());
    }

    @Test
    public void testReportPriorToFirstSnapshot() throws Exception {
        metric.add("foo", 1, null);
        metric.set("bar", 4, null);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertFalse(json.toString(), json.get("metrics").has("values"));
    }

    @Test
    public void testReportIncludesMetricsAfterSnapshot() throws Exception {
        metric.add("foo", 1, null);
        metric.set("bar", 4, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json1 = requestAsJson("http://localhost/state/v1/metrics");
        assertEquals(json1.toString(), "up", json1.get("status").get("code").asText());
        assertEquals(json1.toString(), 2, json1.get("metrics").get("values").size());

        metric.add("fuz", 1, metric.createContext(new HashMap<>(0)));
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json2 = requestAsJson("http://localhost/state/v1/metrics");
        assertEquals(json2.toString(), "up", json2.get("status").get("code").asText());
        assertEquals(json2.toString(), 3, json2.get("metrics").get("values").size());
    }

    /**
     * Tests that we restart an metric when it changes type from gauge to counter or back.
     * This may happen in practice on config reloads.
     */
    @Test
    public void testMetricTypeChangeIsAllowed() {
        String metricName = "myMetric";
        Metric.Context metricContext = null;

        {
            // Add a count metric
            metric.add(metricName, 1, metricContext);
            metric.add(metricName, 2, metricContext);
            // Change it to a gauge metric
            metric.set(metricName, 9, metricContext);
            incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
            MetricValue resultingMetric = monitor.snapshot().iterator().next().getValue().get(metricName);
            assertEquals(GaugeMetric.class, resultingMetric.getClass());
            assertEquals("Value was reset and produces the last gauge value",
                         9.0, ((GaugeMetric) resultingMetric).getLast(), 0.000001);
        }

        {
            // Add a gauge metric
            metric.set(metricName, 9, metricContext);
            // Change it to a count metric
            metric.add(metricName, 1, metricContext);
            metric.add(metricName, 2, metricContext);
            incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
            MetricValue resultingMetric = monitor.snapshot().iterator().next().getValue().get(metricName);
            assertEquals(CountMetric.class, resultingMetric.getClass());
            assertEquals("Value was reset, and changed to add semantics giving 1+2",
                         3, ((CountMetric) resultingMetric).getCount());
        }
    }

    @Test
    public void testAverageAggregationOfValues() throws Exception {
        metric.set("bar", 4, null);
        metric.set("bar", 5, null);
        metric.set("bar", 7, null);
        metric.set("bar", 2, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertEquals(json.toString(), 1, json.get("metrics").get("values").size());
        assertEquals(json.toString(), 4.5,
                     json.get("metrics").get("values").get(0).get("values").get("average").asDouble(), 0.001);
    }

    @Test
    public void testSumAggregationOfCounts() throws Exception {
        metric.add("foo", 1, null);
        metric.add("foo", 1, null);
        metric.add("foo", 2, null);
        metric.add("foo", 1, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertEquals(json.toString(), 1, json.get("metrics").get("values").size());
        assertEquals(json.toString(), 5,
                     json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001);
    }

    @Test
    public void testReadabilityOfJsonReport() throws Exception {
        metric.add("foo", 1, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        assertEquals("{\n" +
                     "    \"metrics\": {\n" +
                     "        \"snapshot\": {\n" +
                     "            \"from\": 0,\n" +
                     "            \"to\": 300\n" +
                     "        },\n" +
                     "        \"values\": [{\n" +
                     "            \"name\": \"foo\",\n" +
                     "            \"values\": {\n" +
                     "                \"count\": 1,\n" +
                     "                \"rate\": 0.0033333333333333335\n" +
                     "            }\n" +
                     "        }]\n" +
                     "    },\n" +
                     "    \"status\": {\"code\": \"up\"},\n" +
                     "    \"time\": 300000\n" +
                     "}",
                     requestAsString("http://localhost/state/v1/all"));

        Metric.Context ctx = metric.createContext(Collections.singletonMap("component", "test"));
        metric.set("bar", 2, ctx);
        metric.set("bar", 3, ctx);
        metric.set("bar", 4, ctx);
        metric.set("bar", 5, ctx);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        assertEquals("{\n" +
                     "    \"metrics\": {\n" +
                     "        \"snapshot\": {\n" +
                     "            \"from\": 300,\n" +
                     "            \"to\": 600\n" +
                     "        },\n" +
                     "        \"values\": [\n" +
                     "            {\n" +
                     "                \"name\": \"foo\",\n" +
                     "                \"values\": {\n" +
                     "                    \"count\": 0,\n" +
                     "                    \"rate\": 0\n" +
                     "                }\n" +
                     "            },\n" +
                     "            {\n" +
                     "                \"dimensions\": {\"component\": \"test\"},\n" +
                     "                \"name\": \"bar\",\n" +
                     "                \"values\": {\n" +
                     "                    \"average\": 3.5,\n" +
                     "                    \"count\": 4,\n" +
                     "                    \"last\": 5,\n" +
                     "                    \"max\": 5,\n" +
                     "                    \"min\": 2,\n" +
                     "                    \"rate\": 0.013333333333333334\n" +
                     "                }\n" +
                     "            }\n" +
                     "        ]\n" +
                     "    },\n" +
                     "    \"status\": {\"code\": \"up\"},\n" +
                     "    \"time\": 600000\n" +
                     "}",
                     requestAsString("http://localhost/state/v1/all"));
    }

    @Test
    public void testNotAggregatingCountsBeyondSnapshots() throws Exception {
        metric.add("foo", 1, null);
        metric.add("foo", 1, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        metric.add("foo", 2, null);
        metric.add("foo", 1, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertEquals(json.toString(), 1, json.get("metrics").get("values").size());
        assertEquals(json.toString(), 3,
                     json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001);
    }

    @Test
    public void testSnapshottingTimes() throws Exception {
        metric.add("foo", 1, null);
        metric.set("bar", 3, null);
        // At this time we should not have done any snapshotting
        incrementCurrentTimeAndAssertNoSnapshot(SNAPSHOT_INTERVAL - 1);
        {
            JsonNode json = requestAsJson("http://localhost/state/v1/all");
            assertFalse(json.toString(), json.get("metrics").has("snapshot"));
        }
        // At this time first snapshot should have been generated
        incrementCurrentTimeAndAssertSnapshot(1);
        {
            JsonNode json = requestAsJson("http://localhost/state/v1/all");
            assertTrue(json.toString(), json.get("metrics").has("snapshot"));
            assertEquals(0.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001);
            assertEquals(300.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001);
        }
        // No new snapshot at this time
        incrementCurrentTimeAndAssertNoSnapshot(SNAPSHOT_INTERVAL - 1);
        {
            JsonNode json = requestAsJson("http://localhost/state/v1/all");
            assertTrue(json.toString(), json.get("metrics").has("snapshot"));
            assertEquals(0.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001);
            assertEquals(300.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001);
        }
        // A new snapshot
        incrementCurrentTimeAndAssertSnapshot(1);
        {
            JsonNode json = requestAsJson("http://localhost/state/v1/all");
            assertTrue(json.toString(), json.get("metrics").has("snapshot"));
            assertEquals(300.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001);
            assertEquals(600.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001);
        }
    }

    @Test
    public void testFreshStartOfValuesBeyondSnapshot() throws Exception {
        metric.set("bar", 4, null);
        metric.set("bar", 5, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        metric.set("bar", 4, null);
        metric.set("bar", 2, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertEquals(json.toString(), 1, json.get("metrics").get("values").size());
        assertEquals(json.toString(), 3,
                     json.get("metrics").get("values").get(0).get("values").get("average").asDouble(), 0.001);
    }

    @Test
    public void snapshotsPreserveLastGaugeValue() throws Exception {
        metric.set("bar", 4, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        JsonNode metricValues = getFirstMetricValueNode(json);
        assertEquals(json.toString(), 4, metricValues.get("last").asDouble(), 0.001);
        // Use 'last' as avg/min/max when none has been set explicitly during snapshot period
        assertEquals(json.toString(), 4, metricValues.get("average").asDouble(), 0.001);
        assertEquals(json.toString(), 4, metricValues.get("min").asDouble(), 0.001);
        assertEquals(json.toString(), 4, metricValues.get("max").asDouble(), 0.001);
        // Count is tracked per period.
        assertEquals(json.toString(), 0, metricValues.get("count").asInt());
    }

    private JsonNode getFirstMetricValueNode(JsonNode root) {
        assertEquals(root.toString(), 1, root.get("metrics").get("values").size());
        JsonNode metricValues = root.get("metrics").get("values").get(0).get("values");
        assertTrue(root.toString(), metricValues.has("last"));
        return metricValues;
    }

    @Test
    public void gaugeSnapshotsTracksCountMinMaxAvgPerPeriod() throws Exception {
        metric.set("bar", 10000, null); // Ensure any cross-snapshot noise is visible
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        metric.set("bar", 20, null);
        metric.set("bar", 40, null);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/all");
        JsonNode metricValues = getFirstMetricValueNode(json);
        assertEquals(json.toString(), 40, metricValues.get("last").asDouble(), 0.001);
        // Last snapshot had explicit values set
        assertEquals(json.toString(), 30, metricValues.get("average").asDouble(), 0.001);
        assertEquals(json.toString(), 20, metricValues.get("min").asDouble(), 0.001);
        assertEquals(json.toString(), 40, metricValues.get("max").asDouble(), 0.001);
        assertEquals(json.toString(), 2, metricValues.get("count").asInt());
    }

    @Test
    public void testHealthAggregation() throws Exception {
        Map<String, String> dimensions1 = new TreeMap<>();
        dimensions1.put("port", String.valueOf(Defaults.getDefaults().vespaWebServicePort()));
        Metric.Context context1 = metric.createContext(dimensions1);
        Map<String, String> dimensions2 = new TreeMap<>();
        dimensions2.put("port", "80");
        Metric.Context context2 = metric.createContext(dimensions2);

        metric.add("serverNumSuccessfulResponses", 4, context1);
        metric.add("serverNumSuccessfulResponses", 2, context2);
        metric.set("serverTotalSuccessfulResponseLatency", 20, context1);
        metric.set("serverTotalSuccessfulResponseLatency", 40, context2);
        metric.add("random", 3, context1);
        incrementCurrentTimeAndAssertSnapshot(SNAPSHOT_INTERVAL);
        JsonNode json = requestAsJson("http://localhost/state/v1/health");
        assertEquals(json.toString(), "up", json.get("status").get("code").asText());
        assertEquals(json.toString(), 2, json.get("metrics").get("values").size());
        assertEquals(json.toString(), "requestsPerSecond",
                     json.get("metrics").get("values").get(0).get("name").asText());
        assertEquals(json.toString(), 6,
                     json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001);
        assertEquals(json.toString(), "latencySeconds",
                     json.get("metrics").get("values").get(1).get("name").asText());
        assertEquals(json.toString(), 0.03,
                     json.get("metrics").get("values").get(1).get("values").get("average").asDouble(), 0.001);
    }

    @Test
    public void testStateConfig() throws Exception {
        JsonNode root = requestAsJson("http://localhost/state/v1/config");

        JsonNode config = root.get("config");
        JsonNode container = config.get("container");
        assertEquals(META_GENERATION, container.get("generation").asLong());
    }

    @Test
    public void testStateVersion() throws Exception {
        JsonNode root = requestAsJson("http://localhost/state/v1/version");

        JsonNode version = root.get("version");
        assertEquals(Vtag.currentVersion.toString(), version.asText());
    }

    private void incrementCurrentTimeAndAssertSnapshot(long val) {
        currentTimeMillis.addAndGet(val);
        assertTrue("Expected a new snapshot to be generated", monitor.checkTime());
    }

    private void incrementCurrentTimeAndAssertNoSnapshot(long val) {
        currentTimeMillis.addAndGet(val);
        assertFalse("Expected no snapshot", monitor.checkTime());;
    }

    private String requestAsString(String requestUri) throws Exception {
        final BufferedContentChannel content = new BufferedContentChannel();
        Response response = driver.dispatchRequest(requestUri, new ResponseHandler() {

            @Override
            public ContentChannel handleResponse(Response response) {
                return content;
            }
        }).get(60, TimeUnit.SECONDS);
        assertNotNull(response);
        assertEquals(Response.Status.OK, response.getStatus());
        StringBuilder str = new StringBuilder();
        Reader in = new InputStreamReader(content.toStream(), StandardCharsets.UTF_8);
        for (int c; (c = in.read()) != -1; ) {
            str.append((char)c);
        }
        return str.toString();
    }

    private JsonNode requestAsJson(String requestUri) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readTree(mapper.getFactory().createParser(requestAsString(requestUri)));
    }
}