aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ClusterStatsAggregatorTest.java
blob: eb4455b9492da64a8b2d7d3170aec5db2acd87b1 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

/**
 * @author hakonhall
 * @since 5.34
 */
@RunWith(MockitoJUnitRunner.class)
public class ClusterStatsAggregatorTest {

    final Set<Integer> distributors = new HashSet<>();
    final Set<Integer> storageNodes = new HashSet<>();
    final Map<Integer, String> hostnames = new HashMap<>();
    final MetricUpdater updater = mock(MetricUpdater.class);
    StorageMergeStats storageStats;

    private void addDistributors(Integer... indices) {
        for (Integer i : indices) {
            distributors.add(i);
        }
    }

    private static class StorageNodeSpec {
        public StorageNodeSpec(Integer index, String hostname) {
            this.index = index;
            this.hostname = hostname;
        }
        public Integer index;
        public String hostname;
    }

    private void addStorageNodes(StorageNodeSpec... specs) {
        for (StorageNodeSpec spec : specs) {
            storageNodes.add(spec.index);
            hostnames.put(spec.index, spec.hostname);
        }
        storageStats = new StorageMergeStats(storageNodes);
    }

    private void putStorageStats(int index, int syncing, int copyingIn, int movingOut, int copyingOut) {
        storageStats.getStorageNode(index).set(createStats(index, syncing, copyingIn, movingOut, copyingOut));
    }

    private static NodeMergeStats createStats(int index, int syncing, int copyingIn, int movingOut, int copyingOut) {
        return new NodeMergeStats(
                index,
                new NodeMergeStats.Amount(syncing),
                new NodeMergeStats.Amount(copyingIn),
                new NodeMergeStats.Amount(movingOut),
                new NodeMergeStats.Amount(copyingOut));
    }

    @Test
    public void testSimple() {
        final int distributorIndex = 1;
        addDistributors(distributorIndex);

        final int storageNodeIndex = 11;
        addStorageNodes(new StorageNodeSpec(storageNodeIndex, "storage-node"));

        putStorageStats(storageNodeIndex, 5, 6, 7, 8);

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);
        aggregator.updateForDistributor(hostnames, distributorIndex, storageStats);

        Map<String, NodeMergeStats> expectedStorageNodeStats = new HashMap<>();
        expectedStorageNodeStats.put("storage-node", createStats(storageNodeIndex, 5, 6, 7, 8));

        verify(updater).updateMergeOpMetrics(expectedStorageNodeStats);
    }

    @Test
    public void testComplex() {
        final int distributor1 = 1;
        final int distributor2 = 2;
        addDistributors(distributor1, distributor2);

        final int storageNode1 = 11;
        final int storageNode2 = 12;
        addStorageNodes(
                new StorageNodeSpec(storageNode1, "storage-node-1"),
                new StorageNodeSpec(storageNode2, "storage-node-2"));

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);

        // Distributor 1.
        putStorageStats(storageNode1, 0, 1, 2, 3);
        putStorageStats(storageNode2, 20, 21, 22, 23);
        aggregator.updateForDistributor(hostnames, distributor1, storageStats);

        // Distributor 2.
        putStorageStats(storageNode1, 10, 11, 12, 13);
        putStorageStats(storageNode2, 30, 31, 32, 33);
        aggregator.updateForDistributor(hostnames, distributor2, storageStats);

        Map<String, NodeMergeStats> expectedStorageNodeStats = new HashMap<>();
        expectedStorageNodeStats.put("storage-node-1", createStats(storageNode1, 0 + 10, 1 + 11, 2 + 12, 3 + 13));
        expectedStorageNodeStats.put("storage-node-2", createStats(storageNode2, 20 + 30, 21 + 31, 22 + 32, 23 + 33));

        verify(updater, times(1)).updateMergeOpMetrics(expectedStorageNodeStats);
    }

    @Test
    public void testHashCodeCache() {
        final int distributor1 = 1;
        final int distributor2 = 2;
        addDistributors(distributor1, distributor2);

        final int storageNode1 = 11;
        final int storageNode2 = 12;
        addStorageNodes(
                new StorageNodeSpec(storageNode1, "storage-node-1"),
                new StorageNodeSpec(storageNode2, "storage-node-2"));

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);

        // Distributor 1.
        putStorageStats(storageNode1, 0, 1, 2, 3);
        putStorageStats(storageNode2, 20, 21, 22, 23);
        aggregator.updateForDistributor(hostnames, distributor1, storageStats);

        // Distributor 2.
        putStorageStats(storageNode1, 10, 11, 12, 13);
        putStorageStats(storageNode2, 30, 31, 32, 33);
        aggregator.updateForDistributor(hostnames, distributor2, storageStats);

        // If we add call another updateForDistributor with the same arguments, updateMergeOpMetrics() should not be called.
        // See times(1) below.
        aggregator.updateForDistributor(hostnames, distributor2, storageStats);

        Map<String, NodeMergeStats> expectedStorageNodeStats = new HashMap<>();
        expectedStorageNodeStats.put("storage-node-1", createStats(storageNode1, 0 + 10, 1 + 11, 2 + 12, 3 + 13));
        expectedStorageNodeStats.put("storage-node-2", createStats(storageNode2, 20 + 30, 21 + 31, 22 + 32, 23 + 33));


        verify(updater, times(1)).updateMergeOpMetrics(expectedStorageNodeStats);
    }

    @Test
    public void testUnknownDistributor() {
        final int upDistributor = 1;
        final int DownDistributorIndex = 2;
        addDistributors(upDistributor);

        final int storageNodeIndex = 11;
        addStorageNodes(new StorageNodeSpec(storageNodeIndex, "storage-node"));

        putStorageStats(storageNodeIndex, 5, 6, 7, 8);

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);
        aggregator.updateForDistributor(hostnames, DownDistributorIndex, storageStats);

        verify(updater, never()).updateMergeOpMetrics(any());
    }

    @Test
    public void testMoreStorageNodesThanDistributors() {
        final int distributor1 = 1;
        addDistributors(distributor1);

        final int storageNode1 = 11;
        final int storageNode2 = 12;
        addStorageNodes(
                new StorageNodeSpec(storageNode1, "storage-node-1"),
                new StorageNodeSpec(storageNode2, "storage-node-2"));

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);

        // Distributor 1.
        putStorageStats(storageNode1, 0, 1, 2, 3);
        putStorageStats(storageNode2, 20, 21, 22, 23);
        aggregator.updateForDistributor(hostnames, distributor1, storageStats);

        Map<String, NodeMergeStats> expectedStorageNodeStats = new HashMap<>();
        expectedStorageNodeStats.put("storage-node-1", createStats(storageNode1, 0, 1, 2, 3));
        expectedStorageNodeStats.put("storage-node-2", createStats(storageNode2, 20, 21, 22, 23));

        verify(updater, times(1)).updateMergeOpMetrics(expectedStorageNodeStats);
    }

    @Test
    public void testMoreDistributorsThanStorageNodes() {
        final int distributor1 = 1;
        final int distributor2 = 2;
        addDistributors(distributor1, distributor2);

        final int storageNode1 = 11;
        addStorageNodes(new StorageNodeSpec(storageNode1, "storage-node-1"));

        ClusterStatsAggregator aggregator = new ClusterStatsAggregator(distributors, storageNodes, updater);

        // Distributor 1.
        putStorageStats(storageNode1, 0, 1, 2, 3);
        aggregator.updateForDistributor(hostnames, distributor1, storageStats);

        // Distributor 2.
        putStorageStats(storageNode1, 10, 11, 12, 13);
        aggregator.updateForDistributor(hostnames, distributor2, storageStats);

        Map<String, NodeMergeStats> expectedStorageNodeStats = new HashMap<>();
        expectedStorageNodeStats.put("storage-node-1", createStats(storageNode1, 0 + 10, 1 + 11, 2 + 12, 3 + 13));

        verify(updater, times(1)).updateMergeOpMetrics(expectedStorageNodeStats);
    }
}