aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/test/java/com/yahoo/vespa/config/JRTConnectionPoolTest.java
blob: 8186347f998d2da70143f624f343edb12a5f9f95 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.config.subscription.ConfigSourceSet;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Tests for the JRTConnectionPool class.
 *
 * @author Gunnar Gauslaa Bergem
 * @author hmusum
 */
public class JRTConnectionPoolTest {

    private static final ConfigSourceSet sources = new ConfigSourceSet(List.of("host0", "host1", "host2"));

    @Test
    public void test_random_selection_of_source() {
        JRTConnectionPool sourcePool = new JRTConnectionPool(sources);
        assertEquals("host0,host1,host2",
                     sourcePool.getSources().stream().map(JRTConnection::getAddress).collect(Collectors.joining(",")));

        Map<String, Integer> sourceOccurrences = new HashMap<>();
        for (int i = 0; i < 1000; i++) {
            String address = sourcePool.switchConnection().getAddress();
            if (sourceOccurrences.containsKey(address)) {
                sourceOccurrences.put(address, sourceOccurrences.get(address) + 1);
            } else {
                sourceOccurrences.put(address, 1);
            }
        }
        for (int i = 0; i < sourcePool.getSize(); i++) {
            assertTrue(sourceOccurrences.get(sourcePool.getSources().get(i).getAddress()) > 200);
        }
    }

    /**
     * Tests that when there are two sources and several clients
     * the sources will be chosen with about the same probability.
     */
    @Test
    public void testManySources() {
        Map<String, Integer> timesUsed = new LinkedHashMap<>();

        ConfigSourceSet twoSources = new ConfigSourceSet(List.of("host0", "host1"));
        JRTConnectionPool sourcePool = new JRTConnectionPool(twoSources);

        int count = 1000;
        for (int i = 0; i < count; i++) {
            String address = sourcePool.switchConnection().getAddress();
            if (timesUsed.containsKey(address)) {
                int times = timesUsed.get(address);
                timesUsed.put(address, times + 1);
            } else {
                timesUsed.put(address, 1);
            }
        }
        assertConnectionDistributionIsFair(timesUsed);
        sourcePool.close();
    }

    // Tests that the number of times each connection is used is close to equal
    private void assertConnectionDistributionIsFair(Map<String, Integer> connectionsUsedPerHost) {
        double deviationDueToRandomSourceSelection = 0.15;
        final int size = 1000;
        int minHostCount = (int) (size/2 * (1 - deviationDueToRandomSourceSelection));
        int maxHostCount = (int) (size/2 * (1 + deviationDueToRandomSourceSelection));

        for (Map.Entry<String, Integer> entry : connectionsUsedPerHost.entrySet()) {
            Integer timesUsed = entry.getValue();
            assertTrue("Host 0 used " + timesUsed + " times, expected to be < " + maxHostCount, timesUsed < maxHostCount);
            assertTrue("Host 0 used " + timesUsed + " times, expected to be > " + minHostCount, timesUsed > minHostCount);
        }
    }

    /**
     * Tests that updating config sources works.
     */
    @Test
    public void updateSources() {
        ConfigSourceSet twoSources = new ConfigSourceSet(List.of("host0", "host1"));
        JRTConnectionPool sourcePool = new JRTConnectionPool(twoSources);

        ConfigSourceSet sourcesBefore = sourcePool.getSourceSet();

        // Update to the same set, should be equal
        sourcePool.updateSources(twoSources);
        assertEquals(sourcePool.getSourceSet(), sourcesBefore);

        // Update to new set
        List<String> newSources = new ArrayList<>();
        newSources.add("host2");
        newSources.add("host3");
        sourcePool.updateSources(newSources);
        ConfigSourceSet newSourceSet = sourcePool.getSourceSet();
        assertNotNull(newSourceSet);
        assertEquals(2, newSourceSet.getSources().size());
        assertNotEquals(sourcesBefore, newSourceSet);
        assertTrue(newSourceSet.getSources().contains("host2"));
        assertTrue(newSourceSet.getSources().contains("host3"));

        // Update to new set with just one host
        List<String> newSources2 = new ArrayList<>();
        newSources2.add("host4");
        sourcePool.updateSources(newSources2);
        ConfigSourceSet newSourceSet2 = sourcePool.getSourceSet();
        assertNotNull(newSourceSet2);
        assertEquals(1, newSourceSet2.getSources().size());
        assertNotEquals(newSourceSet, newSourceSet2);
        assertTrue(newSourceSet2.getSources().contains("host4"));

        sourcePool.close();
    }

    @Test
    public void testFailingSources() {
        ConfigSourceSet sources = new ConfigSourceSet(List.of("host0", "host1"));
        JRTConnectionPool connectionPool = new JRTConnectionPool(sources);

        Connection firstConnection = connectionPool.getCurrent();

        // Should change connection, not getting first connection as new
        JRTConnection secondConnection = failAndGetNewConnection(connectionPool, firstConnection);
        assertNotEquals(firstConnection, secondConnection);

        // Should change connection, not getting second connection as new
        JRTConnection thirdConnection = failAndGetNewConnection(connectionPool, secondConnection);
        // Fail a few more times with old connection, as will happen when there are multiple subscribers
        // Connection should not change
        assertEquals(thirdConnection, failAndGetNewConnection(connectionPool, secondConnection));
        assertEquals(thirdConnection, failAndGetNewConnection(connectionPool, secondConnection));
        assertEquals(thirdConnection, failAndGetNewConnection(connectionPool, secondConnection));
        assertNotEquals(secondConnection, thirdConnection);

        // Should change connection, not getting third connection as new
        JRTConnection currentConnection = failAndGetNewConnection(connectionPool, thirdConnection);
        assertNotEquals(thirdConnection, currentConnection);

        // Should change connection, not getting current connection as new
        JRTConnection currentConnection2 = failAndGetNewConnection(connectionPool, currentConnection);
        assertNotEquals(currentConnection, currentConnection2);

        connectionPool.close();
    }

    private JRTConnection failAndGetNewConnection(JRTConnectionPool connectionPool, Connection failingConnection) {
        connectionPool.switchConnection(failingConnection);
        return connectionPool.getCurrent();
    }

}