aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/search/searchchain/FederationSearcherTest.java
blob: 98f5bdbcd52eebb59d5b298bd05dde9b5e8e36f3 (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search.searchchain;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.dependencies.Dependencies;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.search.federation.FederationConfig;
import com.yahoo.search.searchchain.model.federation.FederationOptions;
import com.yahoo.search.searchchain.model.federation.FederationSearcherModel;
import com.yahoo.search.searchchain.model.federation.FederationSearcherModel.TargetSpec;
import com.yahoo.vespa.model.ConfigProducer;
import com.yahoo.vespa.model.container.search.searchchain.Source.GroupOption;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Tony Vaagenes
 * @author ollivir
 */
public class FederationSearcherTest {

    private static class FederationFixture {
        FederationSearcher federationSearchWithDefaultSources = newFederationSearcher(true, List.of());
        private final ComponentRegistry<SearchChain> searchChainRegistry = new ComponentRegistry<>();
        private final SourceGroupRegistry sourceGroupRegistry = new SourceGroupRegistry();

        void initializeFederationSearcher(FederationSearcher searcher) {
            searcher.initialize(searchChainRegistry, sourceGroupRegistry);
        }

        void registerProviderWithSources(Provider provider) {
            List<GenericTarget> sources = new ArrayList<>();
            sources.add(provider);
            sources.addAll(provider.getSources());
            for (GenericTarget gt : sources) {
                searchChainRegistry.register(gt.getId(), gt);
            }
            sourceGroupRegistry.addSources(provider);
        }
    }

    private static class ProvidersWithSourceFixture extends FederationFixture {
        Provider provider1 = createProvider(ComponentId.fromString("provider1"));
        Provider provider2 = createProvider(ComponentId.fromString("provider2"));

        private ProvidersWithSourceFixture() {
            super();
            provider1.addSource(createSource(ComponentId.fromString("source"), GroupOption.leader));
            provider2.addSource(createSource(ComponentId.fromString("source"), GroupOption.participant));

            registerProviderWithSources(provider1);
            registerProviderWithSources(provider2);
            initializeFederationSearcher(federationSearchWithDefaultSources);
        }
    }

    @Test
    void default_providers_are_inherited_when_inheritDefaultSources_is_true() throws Exception {
        FederationFixture f = new FederationFixture();

        final String providerId = "providerId";

        f.registerProviderWithSources(createProvider(ComponentId.fromString(providerId)));
        f.initializeFederationSearcher(f.federationSearchWithDefaultSources);

        FederationConfig federationConfig = getConfig(f.federationSearchWithDefaultSources);
        FederationConfig.Target target = federationConfig.target(0);

        assertSame(providerId, target.id()); // by identity
        assertTrue(target.searchChain(0).useByDefault(), "Not used by default");
    }

    @Test
    void source_groups_are_inherited_when_inheritDefaultSources_is_true() throws Exception {
        FederationFixture f = new ProvidersWithSourceFixture();

        FederationConfig federationConfig = getConfig(f.federationSearchWithDefaultSources);
        assertEquals(1, federationConfig.target().size());

        FederationConfig.Target target = federationConfig.target(0);
        assertEquals(target.id(), "source");
        assertTrue(target.useByDefault(), "Not used by default");
        assertEquals(2, target.searchChain().size());
        assertTrue(target.searchChain().stream()
                .map(FederationConfig.Target.SearchChain::providerId)
                .toList().containsAll(List.of("provider1", "provider2")));
    }

    @Test
    void source_groups_are_not_inherited_when_inheritDefaultSources_is_false() throws Exception {
        FederationFixture f = new ProvidersWithSourceFixture();

        FederationSearcher federationSearcherWithoutDefaultSources = newFederationSearcher(false, List.of());
        f.initializeFederationSearcher(federationSearcherWithoutDefaultSources);

        FederationConfig federationConfig = getConfig(federationSearcherWithoutDefaultSources);
        assertEquals(0, federationConfig.target().size());
    }

    @Test
    void leaders_must_be_the_first_search_chain_in_a_target() throws Exception {
        FederationFixture f = new ProvidersWithSourceFixture();

        FederationConfig federationConfig = getConfig(f.federationSearchWithDefaultSources);
        List<FederationConfig.Target.SearchChain> searchChain = federationConfig.target(0).searchChain();

        assertEquals("provider1", searchChain.get(0).providerId());
        assertEquals("provider2", searchChain.get(1).providerId());
    }

    @Test
    void manually_specified_targets_overrides_inherited_targets() throws Exception {
        FederationFixture f = new FederationFixture();

        f.registerProviderWithSources(createProvider(ComponentId.fromString("provider1")));
        FederationSearcher federation = newFederationSearcher(true,
                List.of(new TargetSpec(ComponentSpecification.fromString("provider1"),
                        new FederationOptions().setTimeoutInMilliseconds(12345))));
        f.initializeFederationSearcher(federation);

        FederationConfig federationConfig = getConfig(federation);
        assertEquals(1, federationConfig.target().size());

        FederationConfig.Target target = federationConfig.target(0);
        assertEquals(1, target.searchChain().size());

        FederationConfig.Target.SearchChain searchChain = target.searchChain(0);
        assertEquals(12345, searchChain.timeoutMillis());
    }

    private static FederationSearcher newFederationSearcher(boolean inheritDefaultSources, List<TargetSpec> targets) {
        return new FederationSearcher(new FederationSearcherModel(ComponentSpecification.fromString("federation"),
                Dependencies.emptyDependencies(), targets, inheritDefaultSources), Optional.empty());
    }

    private static ChainSpecification searchChainSpecification(ComponentId id) {
        return new ChainSpecification(id, new ChainSpecification.Inheritance(null, null), List.of(), Set.of());
    }

    private static Provider createProvider(ComponentId id) {
        return new Provider(searchChainSpecification(id), new FederationOptions());
    }

    private static Source createSource(ComponentId id, GroupOption groupOption) {
        return new Source(searchChainSpecification(id), new FederationOptions(), groupOption);
    }

    private static FederationConfig getConfig(ConfigProducer configProducer) throws Exception {
        Optional<Class<?>> builderClassOpt = Arrays.stream(FederationConfig.class.getDeclaredClasses())
                .filter(c -> c.getSimpleName().equals("Builder")).findFirst();
        if ( builderClassOpt.isEmpty()) {
            throw new RuntimeException("No Builder class in ConfigInstance.");
        }
        Class<?> builderClass = builderClassOpt.get();

        Object builder = builderClass.getDeclaredConstructor().newInstance();
        Method getConfigMethod = configProducer.getClass().getMethod("getConfig", builderClass);

        getConfigMethod.invoke(configProducer, builder);

        return FederationConfig.class.getConstructor(builderClass).newInstance(builder);
    }
}