aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-hadoop/src/main/java/com/yahoo/vespa/hadoop/mapreduce/util/VespaConfiguration.java
blob: 2a1179dbec61848064c578b7ccdf0e72b36a61d9 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hadoop.mapreduce.util;

import com.yahoo.vespa.http.client.config.FeedParams;
import org.apache.hadoop.conf.Configuration;

import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;

public class VespaConfiguration {

    public static final String ENDPOINT = "vespa.feed.endpoint";
    public static final String DEFAULT_PORT = "vespa.feed.defaultport";
    public static final String USE_SSL = "vespa.feed.ssl";
    public static final String PROXY_HOST = "vespa.feed.proxy.host";
    public static final String PROXY_PORT = "vespa.feed.proxy.port";
    public static final String DRYRUN = "vespa.feed.dryrun";
    public static final String USE_COMPRESSION = "vespa.feed.usecompression";
    public static final String DATA_FORMAT = "vespa.feed.data.format";
    public static final String PROGRESS_REPORT = "vespa.feed.progress.interval";
    public static final String CONNECTIONS = "vespa.feed.connections";
    public static final String THROTTLER_MIN_SIZE = "vespa.feed.throttler.min.size";
    public static final String QUERY_CONNECTION_TIMEOUT = "vespa.query.connection.timeout";
    public static final String ROUTE = "vespa.feed.route";
    public static final String MAX_SLEEP_TIME_MS = "vespa.feed.max.sleep.time.ms";
    public static final String MAX_IN_FLIGHT_REQUESTS = "vespa.feed.max.in.flight.requests";
    public static final String RANDOM_STARTUP_SLEEP = "vespa.feed.random.startup.sleep.ms";
    public static final String NUM_RETRIES = "vespa.feed.num.retries";

    private final Configuration conf;
    private final Properties override;

    private VespaConfiguration(Configuration conf, Properties override) {
        this.conf = conf;
        this.override = override;
    }


    public static VespaConfiguration get(Configuration conf, Properties override) {
        return new VespaConfiguration(conf, override);
    }


    public String endpoint() {
        return getString(ENDPOINT);
    }


    public int defaultPort() {
        return getInt(DEFAULT_PORT, 4080);
    }


    public boolean useSSL() {
        return getBoolean(USE_SSL, false);
    }


    public String proxyHost() {
        return getString(PROXY_HOST);
    }


    public int proxyPort() {
        return getInt(PROXY_PORT, 4080);
    }


    public boolean dryrun() {
        return getBoolean(DRYRUN, false);
    }


    public boolean useCompression() {
        return getBoolean(USE_COMPRESSION, true);
    }


    public int numConnections() {
        return getInt(CONNECTIONS, 1);
    }


    public int throttlerMinSize() {
        return getInt(THROTTLER_MIN_SIZE, 0);
    }


    public int queryConnectionTimeout() {
        return getInt(QUERY_CONNECTION_TIMEOUT, 10000);
    }


    public String route() {
        return getString(ROUTE);
    }


    public int maxSleepTimeMs() {
        return getInt(MAX_SLEEP_TIME_MS, 10000);
    }


    public int maxInFlightRequests() {
        return getInt(MAX_IN_FLIGHT_REQUESTS, 500);
    }


    public int randomStartupSleepMs() {
        return getInt(RANDOM_STARTUP_SLEEP, 30000);
    }


    public int numRetries() {
        return getInt(NUM_RETRIES, 100);
    }


    public FeedParams.DataFormat dataFormat() {
        String format = getString(DATA_FORMAT);
        if ("xml".equalsIgnoreCase(format)) {
            return FeedParams.DataFormat.XML_UTF8;
        }
        return FeedParams.DataFormat.JSON_UTF8;
    }


    public int progressInterval() {
        return getInt(PROGRESS_REPORT, 1000);
    }


    public String getString(String name) {
        if (override != null && override.containsKey(name)) {
            return override.getProperty(name);
        }
        return conf != null ? conf.get(name) : null;
    }


    public int getInt(String name, int defaultValue) {
        if (override != null && override.containsKey(name)) {
            return Integer.parseInt(override.getProperty(name));
        }
        return conf != null ? conf.getInt(name, defaultValue) : defaultValue;
    }


    public boolean getBoolean(String name, boolean defaultValue) {
        if (override != null && override.containsKey(name)) {
            return Boolean.parseBoolean(override.getProperty(name));
        }
        return conf != null ? conf.getBoolean(name, defaultValue) : defaultValue;

    }

    public static Properties loadProperties(String... params) {
        Properties properties = new Properties();
        if (params != null) {
            for (String s : params) {
                try {
                    properties.load(new StringReader(s));
                } catch (IOException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        }
        return properties;
    }


    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(ENDPOINT + ": " + endpoint() + "\n");
        sb.append(DEFAULT_PORT + ": " + defaultPort() + "\n");
        sb.append(USE_SSL + ": " + useSSL() + "\n");
        sb.append(PROXY_HOST + ": " + proxyHost() + "\n");
        sb.append(PROXY_PORT + ": " + proxyPort() + "\n");
        sb.append(DRYRUN + ": " +  dryrun() +"\n");
        sb.append(USE_COMPRESSION + ": " +  useCompression() +"\n");
        sb.append(DATA_FORMAT + ": " +  dataFormat() +"\n");
        sb.append(PROGRESS_REPORT + ": " +  progressInterval() +"\n");
        sb.append(CONNECTIONS + ": " +  numConnections() +"\n");
        sb.append(THROTTLER_MIN_SIZE + ": " +  throttlerMinSize() +"\n");
        sb.append(QUERY_CONNECTION_TIMEOUT + ": " +  queryConnectionTimeout() +"\n");
        sb.append(ROUTE + ": " +  route() +"\n");
        sb.append(MAX_SLEEP_TIME_MS + ": " +  maxSleepTimeMs() +"\n");
        sb.append(MAX_IN_FLIGHT_REQUESTS + ": " +  maxInFlightRequests() +"\n");
        sb.append(RANDOM_STARTUP_SLEEP + ": " +  randomStartupSleepMs() +"\n");
        sb.append(NUM_RETRIES + ": " +  numRetries() +"\n");
        return sb.toString();
    }

}