aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-core/src/main/java/com/yahoo/feedapi/FeederOptions.java
blob: e74c43690dda2f99395377b390139fe9c522775b (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.feedapi;

import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.messagebus.DynamicThrottlePolicy;
import com.yahoo.messagebus.RateThrottlingPolicy;
import com.yahoo.messagebus.SourceSessionParams;
import com.yahoo.messagebus.StaticThrottlePolicy;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.vespaclient.config.FeederConfig;

/**
 * A wrapper for feeder options, from config or HTTP parameters.
 *
 * @author Einar M R Rosenvinge
 */
public class FeederOptions {

    // These default values are here basically just for convenience in test cases,
    // they are overridden by real config values in all other cases.
    private boolean abortOnDocumentError = true;
    private boolean abortOnSendError = true;
    private boolean retryEnabled = true;
    private double timeout = 60;
    private int maxPendingDocs = 0;
    private double maxFeedRate = 0.0;
    private String route = "default";
    private int traceLevel;
    private int mbusPort;
    private DocumentProtocol.Priority priority = DocumentProtocol.Priority.NORMAL_3;

    /** Constructs an options object with all default values. */
    FeederOptions() {
        // empty
    }

    /** Constructor that sets values from config. */
    FeederOptions(FeederConfig config) {
        setAbortOnDocumentError(config.abortondocumenterror());
        setAbortOnSendError(config.abortonsenderror());
        setMaxPendingDocs(config.maxpendingdocs());
        setRetryEnabled(config.retryenabled());
        setRoute(config.route());
        setTimeout(config.timeout());
        setTraceLevel(config.tracelevel());
        setMessageBusPort(config.mbusport());
        setMaxFeedRate(config.maxfeedrate());
    }

    void setMaxFeedRate(double feedRate) {
        maxFeedRate = feedRate;
    }

    boolean getRetryEnabled() {
        return retryEnabled;
    }

    private void setRetryEnabled(boolean retryEnabled) {
        this.retryEnabled = retryEnabled;
    }

    public double getTimeout() {
        return timeout;
    }

    public void setTimeout(double timeout) {
        this.timeout = timeout;
    }

    private void setMaxPendingDocs(int maxPendingDocs) {
        this.maxPendingDocs = maxPendingDocs;
    }

    boolean abortOnDocumentError() {
        return abortOnDocumentError;
    }

    void setAbortOnDocumentError(boolean abortOnDocumentError) {
        this.abortOnDocumentError = abortOnDocumentError;
    }

    boolean abortOnSendError() {
        return abortOnSendError;
    }

    private void setAbortOnSendError(boolean abortOnSendError) {
        this.abortOnSendError = abortOnSendError;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public String getRoute() {
        return route;
    }

    public DocumentProtocol.Priority getPriority() {
        return priority;
    }

    int getTraceLevel() {
        return traceLevel;
    }

    public void setTraceLevel(int traceLevel) {
        this.traceLevel = traceLevel;
    }

    private void setMessageBusPort(int mbusPort) {
        this.mbusPort = mbusPort;
    }

    public void setPriority(DocumentProtocol.Priority priority) {
        this.priority = priority;
    }

    /**
     * Creates a source session params object with parameters set as these options
     * dictate.
     */
    SourceSessionParams toSourceSessionParams() {
        SourceSessionParams params = new SourceSessionParams();

        StaticThrottlePolicy policy;
        if (maxFeedRate > 0.0) {
            policy = new RateThrottlingPolicy(maxFeedRate);
        } else if (maxPendingDocs == 0) {
            policy = new DynamicThrottlePolicy();
        } else {
            policy = new StaticThrottlePolicy();
        }
        if (maxPendingDocs > 0) {
            policy.setMaxPendingCount(maxPendingDocs);
        }

        params.setThrottlePolicy(policy);

        params.setTimeout(getTimeout());
        return params;
    }

    RPCNetworkParams getNetworkParams() {
        try {
            RPCNetworkParams networkParams = new RPCNetworkParams();
            if (mbusPort != -1) {
                networkParams.setListenPort(mbusPort);
            }
            return networkParams;
        } catch (Exception e) {
        }

        return null;
    }

    @Override
    public String toString() {
        return "FeederOptions{" +
               "abortOnDocumentError=" + abortOnDocumentError +
               ", abortOnSendError=" + abortOnSendError +
               ", retryEnabled=" + retryEnabled +
               ", timeout=" + timeout +
               ", maxPendingDocs=" + maxPendingDocs +
               ", route='" + route + '\'' +
               ", traceLevel=" + traceLevel +
               ", mbusPort=" + mbusPort +
               ", priority=" + priority.name() +
               '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof FeederOptions)) return false;

        FeederOptions that = (FeederOptions) o;

        if (abortOnDocumentError != that.abortOnDocumentError) return false;
        if (abortOnSendError != that.abortOnSendError) return false;
        if (maxPendingDocs != that.maxPendingDocs) return false;
        if (maxFeedRate != that.maxFeedRate) return false;
        if (mbusPort != that.mbusPort) return false;
        if (retryEnabled != that.retryEnabled) return false;
        if (Double.compare(that.timeout, timeout) != 0) return false;
        if (traceLevel != that.traceLevel) return false;
        if (priority != that.priority) return false;
        if (route != null ? !route.equals(that.route) : that.route != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = (abortOnDocumentError ? 1 : 0);
        result = 31 * result + (abortOnSendError ? 1 : 0);
        result = 31 * result + (retryEnabled ? 1 : 0);
        temp = timeout != +0.0d ? Double.doubleToLongBits(timeout) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + maxPendingDocs;
        result = 31 * result + ((int)(maxFeedRate * 1000));
        result = 31 * result + (route != null ? route.hashCode() : 0);
        result = 31 * result + traceLevel;
        result = 31 * result + mbusPort;
        result = 31 * result + (priority != null ? priority.hashCode() : 0);
        return result;
    }
}