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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Parameters given to a {@link com.yahoo.vespa.http.client.FeedClientFactory}
 * when creating {@link com.yahoo.vespa.http.client.FeedClient}s. This class is immutable
 * and has no public constructor - to instantiate one, use a {@link Builder}.
 *
 * @author Einar M R Rosenvinge
 * @see com.yahoo.vespa.http.client.FeedClientFactory
 * @see Builder
 */
public final class SessionParams {

    /**
     * Interface for handling serious errors with connection.
     */
    public interface  ErrorReporter {
        void onSessionError(Endpoint endpoint, String oldSessionID, String newSessionId);
    }

    /**
     * Mutable class used to instantiate a {@link SessionParams}. A builder
     * instance will at the very least contain cluster settings (
     * {@link #addCluster(Cluster)}), for supporting SSL and similar transport
     * settings, use {@link #setConnectionParams(ConnectionParams)}.
     */
    public static final class Builder {

        private final List<Cluster> clusters = new LinkedList<>();
        private FeedParams feedParams = new FeedParams.Builder().build();
        private ConnectionParams connectionParams = new ConnectionParams.Builder().build();
        private int clientQueueSize = 10000;
        private ErrorReporter errorReporter = null;
        private int throttlerMinSize = 0;

        /**
         * Add a Vespa installation for feeding documents into.
         *
         * @return this Builder instance, to support chaining
         */
        public Builder addCluster(Cluster cluster) {
            clusters.add(cluster);
            return this;
        }

        /**
         * Set parameters used for feeding the documents in the receiving
         * cluster. Reasonable defaults are supplied, so setting this should not
         * be necessary for testing.
         *
         * @return this builder instance to support chaining
         */
        public Builder setFeedParams(FeedParams feedParams) {
            this.feedParams = feedParams;
            return this;
        }

        /**
         * Transport parameters, like custom HTTP headers.
         *
         * @return this Builder instance, to support chaining
         */
        public Builder setConnectionParams(ConnectionParams connectionParams) {
            this.connectionParams = connectionParams;
            return this;
        }

        /**
         * Sets an error reporter that is invoked in case of serious errors.
         *
         * @param errorReporter the handler
         * @return pointer to builder.
         */
        public Builder setErrorReporter(ErrorReporter errorReporter) {
            this.errorReporter = errorReporter;
            return this;
        }

        /**
         * Sets the maximum number of document operations to hold in memory, waiting to be
         * sent to Vespa. When this threshold is reached, {@link java.io.OutputStream#close()} will block.
         *
         * @param clientQueueSize the maximum number of document operations to hold in memory.
         * @return pointer to builder.
         */
        public Builder setClientQueueSize(int clientQueueSize) {
            this.clientQueueSize = clientQueueSize;
            return this;
        }

        /**
         * Sets the minimum queue size of the throttler. If this is zero, it means that dynamic throttling is
         * not enabled. Otherwise it is the minimum size of the throttler for how many parallel requests that are
         * accepted. The max size of the throttler is the clientQueueSize.
         * @return the minimum number of requests to be used in throttler or zero if throttler is static.
         *
         * @param throttlerMinSize the value of the min size.
         */
        public Builder setThrottlerMinSize(int throttlerMinSize) {
            this.throttlerMinSize = throttlerMinSize;
            return this;
        }

        /**
         * Instantiates a {@link SessionParams} that can be given to a {@link com.yahoo.vespa.http.client.FeedClientFactory}.
         *
         * @return a SessionParams object with the parameters of this Builder
         */
        public SessionParams build() {
            return new SessionParams(
                    clusters, feedParams, connectionParams, clientQueueSize, errorReporter, throttlerMinSize);
        }

        public FeedParams getFeedParams() {
            return feedParams;
        }
        public ConnectionParams getConnectionParams() {
            return connectionParams;
        }
        public int getClientQueueSize() {
            return clientQueueSize;
        }
        public int getThrottlerMinSize() {
            return throttlerMinSize;
        }
    }

    // NOTE! See toBuilder at the end of this class if you add fields here

    private final List<Cluster> clusters;
    private final FeedParams feedParams;
    private final ConnectionParams connectionParams;
    private final int clientQueueSize;
    private final ErrorReporter errorReport;
    private int throttlerMinSize;

    private SessionParams(Collection<Cluster> clusters,
                          FeedParams feedParams,
                          ConnectionParams connectionParams,
                          int clientQueueSize,
                          ErrorReporter errorReporter,
                          int throttlerMinSize) {
        this.clusters = Collections.unmodifiableList(new ArrayList<>(clusters));
        this.feedParams = feedParams;
        this.connectionParams = connectionParams;
        this.clientQueueSize = clientQueueSize;
        this.errorReport = errorReporter;
        this.throttlerMinSize = throttlerMinSize;
    }

    public List<Cluster> getClusters() {
        return clusters;
    }

    public FeedParams getFeedParams() {
        return feedParams;
    }

    public ConnectionParams getConnectionParams() {
        return connectionParams;
    }

    public int getClientQueueSize() {
        return clientQueueSize;
    }

    public int getThrottlerMinSize() {
        return throttlerMinSize;
    }

    public ErrorReporter getErrorReport() {
        return errorReport;
    }

    public Builder toBuilder() {
        Builder b = new Builder();
        clusters.forEach(c -> b.addCluster(c));
        b.setFeedParams(feedParams);
        b.setConnectionParams(connectionParams);
        b.setClientQueueSize(clientQueueSize);
        b.setErrorReporter(errorReport);
        b.setThrottlerMinSize(throttlerMinSize);
        return b;
    }

}