aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/maintenance/NodeMetricsDbMaintainerTest.java
blob: 5af787092d56fbedfe1094984c00fabe181d3647 (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.maintenance;

import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.hosted.provision.autoscale.NodeMetricSnapshot;
import com.yahoo.vespa.hosted.provision.autoscale.MetricsDb;
import com.yahoo.vespa.hosted.provision.autoscale.MetricsV2MetricsFetcher;
import com.yahoo.vespa.hosted.provision.autoscale.NodeTimeseries;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import com.yahoo.vespa.hosted.provision.testutils.OrchestratorMock;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 */
public class NodeMetricsDbMaintainerTest {

    @Test
    public void testNodeMetricsDbMaintainer() {
        NodeResources resources = new NodeResources(1, 10, 100, 1);
        ProvisioningTester tester = new ProvisioningTester.Builder().build();
        tester.clock().setInstant(Instant.ofEpochMilli(1400));
        tester.makeReadyNodes(2, resources);
        tester.activateTenantHosts();
        tester.deploy(ProvisioningTester.applicationId("test"),
                      Capacity.from(new ClusterResources(2, 1, resources)));
        OrchestratorMock orchestrator = new OrchestratorMock();
        MockHttpClient httpClient = new MockHttpClient();
        MetricsV2MetricsFetcher fetcher = new MetricsV2MetricsFetcher(tester.nodeRepository(), orchestrator, httpClient);
        MetricsDb db = MetricsDb.createTestInstance(tester.nodeRepository());
        NodeMetricsDbMaintainer maintainer = new NodeMetricsDbMaintainer(tester.nodeRepository(),
                                                                         fetcher,
                                                                         db,
                                                                         Duration.ofHours(1),
                                                                         new TestMetric());
        assertTrue(maintainer.maintain());
        List<NodeTimeseries> timeseriesList = db.getNodeTimeseries(Duration.ofDays(1),
                                                                   Set.of("host-1.yahoo.com", "host-2.yahoo.com"));
        assertEquals(2, timeseriesList.size());
        List<NodeMetricSnapshot> allSnapshots = timeseriesList.stream()
                                                              .flatMap(timeseries -> timeseries.asList().stream())
                                                              .collect(Collectors.toList());
        assertTrue(allSnapshots.stream().anyMatch(snapshot -> snapshot.inService()));
        assertTrue(allSnapshots.stream().anyMatch(snapshot -> ! snapshot.inService()));
    }

    private static class MockHttpClient implements MetricsV2MetricsFetcher.AsyncHttpClient {

        final String cannedResponse =
                "{\n" +
                "  \"nodes\": [\n" +
                "    {\n" +
                "      \"hostname\": \"host-1.yahoo.com\",\n" +
                "      \"role\": \"role0\",\n" +
                "      \"node\": {\n" +
                "        \"timestamp\": 1300,\n" +
                "        \"metrics\": [\n" +
                "          {\n" +
                "            \"values\": {\n" +
                "              \"cpu.util\": 14,\n" + // this value asserted on above
                "              \"mem_total.util\": 15,\n" +
                "              \"disk.util\": 20,\n" +
                "              \"application_generation\": 3,\n" +
                "              \"in_service\": 1\n" +
                "            },\n" +
                "            \"dimensions\": {\n" +
                "              \"state\": \"active\"\n" +
                "            }\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      \"hostname\": \"host-2.yahoo.com\",\n" +
                "      \"role\": \"role0\",\n" +
                "      \"node\": {\n" +
                "        \"timestamp\": 1300,\n" +
                "        \"metrics\": [\n" +
                "          {\n" +
                "            \"values\": {\n" +
                "              \"cpu.util\": 1,\n" +
                "              \"mem_total.util\": 2,\n" +
                "              \"disk.util\": 3,\n" +
                "              \"application_generation\": 3,\n" +
                "              \"in_service\": 0\n" +
                "            },\n" +
                "            \"dimensions\": {\n" +
                "              \"state\": \"active\"\n" +
                "            }\n" +
                "          }\n" +
                "        ]\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}\n";

        @Override
        public CompletableFuture<String> get(String url) {
            return CompletableFuture.completedFuture(cannedResponse);
        }

        @Override
        public void close() { }

    }

}