aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/autoscale/MetricsV2MetricsFetcherTest.java
blob: 384e8dd843939ed483fe96c3d3fbe97e05d35295 (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
// 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.autoscale;

import com.yahoo.collections.Pair;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.transaction.Mutex;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import com.yahoo.vespa.hosted.provision.testutils.OrchestratorMock;
import com.yahoo.vespa.applicationmodel.HostName;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

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

public class MetricsV2MetricsFetcherTest {

    private static final double delta = 0.00000001;

    @Test
    public void testMetricsFetch() throws Exception {
        NodeResources resources = new NodeResources(1, 10, 100, 1);
        ProvisioningTester tester = new ProvisioningTester.Builder().build();
        OrchestratorMock orchestrator = new OrchestratorMock();
        MockHttpClient httpClient = new MockHttpClient();
        MetricsV2MetricsFetcher fetcher = new MetricsV2MetricsFetcher(tester.nodeRepository(), orchestrator, httpClient);

        tester.makeReadyNodes(4, resources); // Creates (in order) host-1.yahoo.com, host-2.yahoo.com, host-3.yahoo.com, host-4.yahoo.com
        tester.activateTenantHosts();

        ApplicationId application1 = ProvisioningTester.applicationId();
        ApplicationId application2 = ProvisioningTester.applicationId();
        tester.deploy(application1, Capacity.from(new ClusterResources(2, 1, resources))); // host-1.yahoo.com, host-2.yahoo.com
        tester.deploy(application2, Capacity.from(new ClusterResources(2, 1, resources))); // host-4.yahoo.com, host-3.yahoo.com

        orchestrator.suspend(new HostName("host-4.yahoo.com"));

        {
            httpClient.cannedResponse = cannedResponseForApplication1;
            List<Pair<String, MetricSnapshot>> values = new ArrayList<>(fetcher.fetchMetrics(application1).get().metrics());
            assertEquals("http://host-1.yahoo.com:4080/metrics/v2/values?consumer=autoscaling",
                         httpClient.requestsReceived.get(0));
            assertEquals(2, values.size());

            assertEquals("host-1.yahoo.com", values.get(0).getFirst());
            assertEquals(0.162, values.get(0).getSecond().cpu(), delta);
            assertEquals(0.231, values.get(0).getSecond().memory(), delta);
            assertEquals(0.820, values.get(0).getSecond().disk(), delta);

            assertEquals("host-2.yahoo.com", values.get(1).getFirst());
            assertEquals(0.2, values.get(1).getSecond().cpu(), delta);
            assertEquals(0.0, values.get(1).getSecond().memory(), delta);
            assertEquals(0.4, values.get(1).getSecond().disk(), delta);
        }

        {
            httpClient.cannedResponse = cannedResponseForApplication2;
            List<Pair<String, MetricSnapshot>> values = new ArrayList<>(fetcher.fetchMetrics(application2).get().metrics());
            assertEquals("http://host-3.yahoo.com:4080/metrics/v2/values?consumer=autoscaling",
                         httpClient.requestsReceived.get(1));
            assertEquals(1, values.size());
            assertEquals("host-3.yahoo.com", values.get(0).getFirst());
            assertEquals(0.10, values.get(0).getSecond().cpu(), delta);
            assertEquals(0.15, values.get(0).getSecond().memory(), delta);
            assertEquals(0.20, values.get(0).getSecond().disk(), delta);
            assertEquals(3, values.get(0).getSecond().generation(), delta);
            assertTrue(values.get(0).getSecond().stable());
        }

        {
            httpClient.cannedResponse = cannedResponseForApplication2;
            try (Mutex lock = tester.nodeRepository().nodes().lock(application1)) {
                tester.nodeRepository().nodes().write(tester.nodeRepository().nodes().list(Node.State.active).owner(application2)
                        .first().get().retire(tester.clock().instant()), lock);
            }
            List<Pair<String, MetricSnapshot>> values = new ArrayList<>(fetcher.fetchMetrics(application2).get().metrics());
            assertFalse(values.get(0).getSecond().stable());
        }
    }

    private static class MockHttpClient implements MetricsV2MetricsFetcher.AsyncHttpClient {

        List<String> requestsReceived = new ArrayList<>();

        String cannedResponse = null;

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

        @Override
        public void close() { }

    }

    final String cannedResponseForApplication1 =
            "{\n" +
            "  \"nodes\": [\n" +
            "    {\n" +
            "      \"hostname\": \"host-1.yahoo.com\",\n" +
            "      \"role\": \"role0\",\n" +
            "      \"node\": {\n" +
            "        \"timestamp\": 1234,\n" +
            "        \"metrics\": [\n" +
            "          {\n" +
            "            \"values\": {\n" +
            "              \"cpu.util\": 16.2,\n" +
            "              \"mem.util\": 23.1,\n" +
            "              \"disk.util\": 82\n" +
            "            },\n" +
            "            \"dimensions\": {\n" +
            "              \"state\": \"active\"\n" +
            "            }\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    },\n" +
            "    {\n" +
            "      \"hostname\": \"host-2.yahoo.com\",\n" +
            "      \"role\": \"role1\",\n" +
            "      \"node\": {\n" +
            "        \"timestamp\": 1200,\n" +
            "        \"metrics\": [\n" +
            "          {\n" +
            "            \"values\": {\n" +
            "              \"cpu.util\": 20,\n" +
            "              \"disk.util\": 40\n" +
            "            },\n" +
            "            \"dimensions\": {\n" +
            "              \"state\": \"active\"\n" +
            "            }\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    }\n" +
            "  ]\n" +
            "}\n";

    final String cannedResponseForApplication2 =
            "{\n" +
            "  \"nodes\": [\n" +
            "    {\n" +
            "      \"hostname\": \"host-3.yahoo.com\",\n" +
            "      \"role\": \"role0\",\n" +
            "      \"node\": {\n" +
            "        \"timestamp\": 1300,\n" +
            "        \"metrics\": [\n" +
            "          {\n" +
            "            \"values\": {\n" +
            "              \"cpu.util\": 10,\n" +
            "              \"mem.util\": 15,\n" +
            "              \"disk.util\": 20,\n" +
            "              \"application_generation\": 3\n" +
            "            },\n" +
            "            \"dimensions\": {\n" +
            "              \"state\": \"active\"\n" +
            "            }\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    }\n" +
            "  ]\n" +
            "}\n";

}