aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/clients/ContainerDocumentApi.java
blob: 9801eab9f2b3b93b3d04632b006ad31cd5e3617c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.clients;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.Phase;
import com.yahoo.component.chain.dependencies.Dependencies;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.component.chain.model.ChainedComponentModel;
import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.vespa.model.container.ContainerCluster;
import com.yahoo.vespa.model.container.component.Handler;
import com.yahoo.vespa.model.container.component.chain.ProcessingHandler;
import com.yahoo.vespa.model.container.search.ContainerSearch;
import com.yahoo.vespa.model.container.search.searchchain.SearchChain;
import com.yahoo.vespa.model.container.search.searchchain.SearchChains;
import com.yahoo.vespa.model.container.search.searchchain.Searcher;
import com.yahoo.vespaclient.config.FeederConfig;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Einar M R Rosenvinge
 */
public class ContainerDocumentApi implements FeederConfig.Producer {

    public static final String vespaClientBundleSpecification = "vespaclient-container-plugin";
    private final Options options;

    public ContainerDocumentApi(ContainerCluster cluster, Options options) {
        this.options = options;
        setupLegacySearchers(cluster);
        setupHandlers(cluster);
    }

    private void setupHandlers(ContainerCluster cluster) {
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandler", "feed"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerRemove", "remove"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerRemoveLocation", "removelocation"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerGet", "get"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerVisit", "visit"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.document.restapi.resource.RestApi", "document/v1/*"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerCompatibility", "document"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.feedhandler.VespaFeedHandlerStatus", "feedstatus"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.vespa.http.server.FeedHandler", ContainerCluster.RESERVED_URI_PREFIX + "/feedapi"));
    }

    private void setupLegacySearchers(ContainerCluster cluster) {
        Set<ComponentSpecification> inherited = new TreeSet<>();

        SearchChain vespaGetChain = new SearchChain(new ChainSpecification(new ComponentId("vespaget"),
                new ChainSpecification.Inheritance(inherited, null), new ArrayList<>(), new TreeSet<>()));
        vespaGetChain.addInnerComponent(newVespaClientSearcher("com.yahoo.storage.searcher.GetSearcher"));

        SearchChain vespaVisitChain = new SearchChain(new ChainSpecification(new ComponentId("vespavisit"),
                new ChainSpecification.Inheritance(inherited, null), new ArrayList<>(), new TreeSet<>()));
        vespaVisitChain.addInnerComponent(newVespaClientSearcher("com.yahoo.storage.searcher.VisitSearcher"));

        SearchChains chains;
        if (cluster.getSearch() != null) {
            chains = cluster.getSearchChains();
        } else {
            chains = new SearchChains(cluster, "searchchain");
        }
        chains.add(vespaGetChain);
        chains.add(vespaVisitChain);

        if (cluster.getSearch() == null) {
            ContainerSearch containerSearch = new ContainerSearch(cluster, chains, new ContainerSearch.Options());
            cluster.setSearch(containerSearch);

            ProcessingHandler<SearchChains> searchHandler = new ProcessingHandler<>(chains, 
                                                                                    "com.yahoo.search.handler.SearchHandler");
            searchHandler.addServerBindings("http://*/search/*", "https://*/search/*");
            cluster.addComponent(searchHandler);
        }
    }

    private Handler newVespaClientHandler(String componentId, String bindingSuffix) {
        Handler<AbstractConfigProducer<?>> handler = new Handler<>(new ComponentModel(
                BundleInstantiationSpecification.getFromStrings(componentId, null, vespaClientBundleSpecification), ""));

        for (String rootBinding : options.bindings) {
            handler.addServerBindings(rootBinding + bindingSuffix,
                    rootBinding + bindingSuffix + '/');
        }
        return handler;
    }

    private Searcher newVespaClientSearcher(String componentSpec) {
        return new Searcher<>(new ChainedComponentModel(
                BundleInstantiationSpecification.getFromStrings(componentSpec, null, vespaClientBundleSpecification),
                new Dependencies(null, null, null)));
    }

    @Override
    public void getConfig(FeederConfig.Builder builder) {
        if (options.abortondocumenterror != null)
            builder.abortondocumenterror(options.abortondocumenterror);
        if (options.route!= null)
            builder.route(options.route);
        if (options.maxpendingdocs != null)
            builder.maxpendingdocs(options.maxpendingdocs);
        if (options.maxpendingbytes != null)
            builder.maxpendingbytes(options.maxpendingbytes);
        if (options.retryenabled != null)
            builder.retryenabled(options.retryenabled);
        if (options.retrydelay != null)
            builder.retrydelay(options.retrydelay);
        if (options.timeout != null)
            builder.timeout(options.timeout);
        if (options.tracelevel != null)
            builder.tracelevel(options.tracelevel);
        if (options.mbusport != null)
            builder.mbusport(options.mbusport);
        if (options.docprocChain != null)
            builder.docprocchain(options.docprocChain);
    }

    public static final class Options {
        private final Collection<String> bindings;
        private final Boolean abortondocumenterror;
        private final String route;
        private final Integer maxpendingdocs;
        private final Integer maxpendingbytes;
        private final Boolean retryenabled;
        private final Double retrydelay;
        private final Double timeout;
        private final Integer tracelevel;
        private final Integer mbusport;
        private final String docprocChain;

        public Options(Collection<String> bindings,
                       Boolean abortondocumenterror,
                       String route,
                       Integer maxpendingdocs,
                       Integer maxpendingbytes,
                       Boolean retryenabled,
                       Double retrydelay,
                       Double timeout,
                       Integer tracelevel,
                       Integer mbusport,
                       String docprocChain) {

            this.bindings = Collections.unmodifiableCollection(bindings);
            this.abortondocumenterror = abortondocumenterror;
            this.route = route;
            this.maxpendingdocs = maxpendingdocs;
            this.maxpendingbytes = maxpendingbytes;
            this.retryenabled = retryenabled;
            this.retrydelay = retrydelay;
            this.timeout = timeout;
            this.tracelevel = tracelevel;
            this.mbusport = mbusport;
            this.docprocChain = docprocChain;
        }
    }

}