aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/cluster/test/HasherTestCase.java
blob: d74bfc9a21a6a59dc6bc0e48be0a3553a1bde095 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.cluster.test;

import com.yahoo.container.handler.VipStatus;
import com.yahoo.fs4.QueryPacket;
import com.yahoo.prelude.cluster.Hasher;
import com.yahoo.prelude.fastsearch.CacheKey;
import com.yahoo.prelude.fastsearch.VespaBackEndSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.searchchain.Execution;

/**
 * Tests the Hashing/failover/whatever functionality.
 *
 * @author bratseth
 * @author Steinar Knutsen
 */
public class HasherTestCase extends junit.framework.TestCase {

    public HasherTestCase (String name) {
        super(name);
    }

    public void testEmptyHasher() {
        Hasher hasher=new Hasher();
        assertNull(hasher.select(0));
    }

    private static class MockBackend extends VespaBackEndSearcher {

        @Override
        protected Result doSearch2(Query query, QueryPacket queryPacket,
                CacheKey cacheKey, Execution execution) {
            return null;
        }

        @Override
        protected void doPartialFill(Result result, String summaryClass) {
        }
    }

    public void testOneHasher() {
        Hasher hasher = new Hasher();
        VespaBackEndSearcher o1 = new MockBackend();
        hasher.add(o1);
        assertSame(o1, hasher.select(0));
        assertSame(o1, hasher.select(1));

        hasher.remove(o1);
        assertNull(hasher.select(0));
    }

    public void testAddAndRemove() {
        Hasher hasher = new Hasher();
        VespaBackEndSearcher v0 = new MockBackend();
        VespaBackEndSearcher v1 = new MockBackend();
        VespaBackEndSearcher v2 = new MockBackend();
        VespaBackEndSearcher v3 = new MockBackend();
        v1.setLocalDispatching(false);
        v3.setLocalDispatching(false);
        hasher.add(v1);
        hasher.add(v0);
        assertSame(v0, hasher.select(0));
        hasher.add(v2);
        VespaBackEndSearcher tmp1 = hasher.select(0);
        assertTrue(v0 == tmp1 || v2 == tmp1);
        if (tmp1 == v0) {
            assertSame(v2, hasher.select(0));
            assertSame(v0, hasher.select(0));
        } else {
            assertSame(v0, hasher.select(0));
            assertSame(v2, hasher.select(0));
        }
        hasher.remove(v2);
        hasher.remove(v2);
        assertEquals(2, hasher.getNodeCount());
        assertSame(v0, hasher.select(0));
        hasher.remove(v0);
        assertEquals(1, hasher.getNodeCount());
        assertSame(v1, hasher.select(0));
        hasher.add(v3);
        hasher.add(v0);
        assertSame(v0, hasher.select(0));
    }

    public void testPreferLocal() {
        Hasher hasher = new Hasher();
        VespaBackEndSearcher v0 = new MockBackend();
        VespaBackEndSearcher v1 = new MockBackend();
        VespaBackEndSearcher v2 = new MockBackend();
        v1.setLocalDispatching(false);
        v2.setLocalDispatching(false);

        hasher.add(v1);
        hasher.add(v2);
        hasher.add(v0);
        assertTrue(hasher.select(0).isLocalDispatching());

        hasher = new Hasher();
        hasher.add(v1);
        hasher.add(v0);
        hasher.add(v2);
        assertTrue(hasher.select(0).isLocalDispatching());


        hasher = new Hasher();
        hasher.add(v0);
        hasher.add(v1);
        hasher.add(v2);
        assertTrue(hasher.select(0).isLocalDispatching());

        hasher = new Hasher();
        hasher.add(v0);
        hasher.add(v1);
        hasher.add(v2);
        hasher.remove(v1);
        assertTrue(hasher.select(0).isLocalDispatching());

        hasher = new Hasher();
        hasher.add(v0);
        hasher.add(v1);
        assertTrue(hasher.select(0).isLocalDispatching());
        hasher.add(v2);
        assertTrue(hasher.select(0).isLocalDispatching());
    }

}