summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/CommandLineArguments.java
blob: d90a708622adb2a836f0e581a5c48b03a778d2df (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.runner;

import com.google.common.annotations.Beta;

import com.google.common.base.Splitter;
import com.yahoo.vespa.http.client.config.Cluster;
import com.yahoo.vespa.http.client.config.ConnectionParams;
import com.yahoo.vespa.http.client.config.Endpoint;
import com.yahoo.vespa.http.client.config.FeedParams;
import com.yahoo.vespa.http.client.config.SessionParams;
import io.airlift.command.Command;
import io.airlift.command.HelpOption;
import io.airlift.command.Option;
import io.airlift.command.SingleCommand;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

/**
 * Commandline interface for the binary.
 * @author dybis
 */
@Beta
@Command(name = "vespa-http-client",
        description = "This is a tool for feeding xml or json data to a Vespa application.")
public class CommandLineArguments {

    /**
     * Creates a CommandLineArguments instance and populates it with data.
     * @param args array of arguments.
     * @return null on failure or if help option is set to true.
     */
    static CommandLineArguments build(String[] args) {
        final CommandLineArguments cmdArgs;
        try {
            cmdArgs =  SingleCommand.singleCommand(CommandLineArguments.class).parse(args);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.err.println("Use --help to show usage.\n");
            return null;
        }
        if (cmdArgs.helpOption.showHelpIfRequested()) {
            return null;
        }

        if (cmdArgs.hostArg == null) {
            System.err.println("'--host' not set.");
            return null;
        }
        if (cmdArgs.priorityArg != null && ! checkPriorityFlag(cmdArgs.priorityArg)) {
            return null;
        }

        return cmdArgs;
    }

    private static boolean checkPriorityFlag(String priorityArg) {
        switch (priorityArg) {
            case "HIGHEST":
            case "VERY_HIGH":
            case "HIGH_1":
            case "HIGH_2":
            case "HIGH_3":
            case "NORMAL_1":
            case "NORMAL_2":
            case "NORMAL_3":
            case "NORMAL_4":
            case "NORMAL_5":
            case "NORMAL_6":
            case "LOW_1":
            case "LOW_2":
            case "LOW_3":
            case "VERY_LOW":
            case "LOWEST":
                return true;
            default:
                System.err.println("Not valid value for priority. Allowed values are HIGHEST, VERY_HIGH, HIGH_[1-3], " +
                        "NORMAL_[1-6], LOW_[1-3], VERY_LOW, and LOWEST.");
                return false;
        }
    }

    @Inject
    private HelpOption helpOption;

    @Option(name = {"--useV3Protocol"}, description = "Not used anymore, see useV2Protocol.")
    private boolean notUsedBoolean = true;

    @Option(name = {"--useV2Protocol"}, description = "Use old V2 protocol to gateway.")
    private boolean enableV2Protocol = false;

    @Option(name = {"--file"},
            description = "The name of the input file to read.")
    private String fileArg = null;

    @Option(name = {"--add-root-element-to-xml"},
            description = "Add <vespafeed> tag to XML document, makes it easier to feed raw data.")
    private boolean addRootElementToXml = false;

    @Option(name = {"--route"},
            description = "(=default)The route to send the data to.")
    private String routeArg = "default";

    @Option(name = {"--host"},
            description = "The host(s) for the gateway. If using several, use comma to sepparate them.")
    private String hostArg;

    @Option(name = {"--port"},
            description = "The port for the host of the gateway.")
    private int portArg = 4080;

    @Option(name = {"--timeout"},
            description = "(=180) The time (in seconds) allowed for sending operations.")
    private long timeoutArg = 180;

    @Option(name = {"--useCompression"},
            description = "Use compression over network.")
    private boolean useCompressionArg = false;

    @Option(name = {"--useDynamicThrottling"},
            description = "Try to maximize throughput by using dynamic throttling.")
    private boolean useDynamicThrottlingArg = false;

    @Option(name = {"--maxpending"},
            description = "The maximum number of operations that are allowed " +
                    "to be pending at any given time.")
    private int maxPendingOperationCountArg = 10000;

    @Option(name = {"--debugport"},
            description = "Deprecated, not used.")
    private int debugportArg = 9988;

    @Option(name = {"-v", "--verbose"},
            description = "Enable verbose output of progress.")
    private boolean verboaseArg = false;

    @Option(name = {"--noretry"},
            description = "Turns off retries of recoverable failures..")
    private boolean noRetryArg = false;

    @Option(name = {"--retrydelay"},
            description = "The time (in seconds) to wait between retries of a failed operation.")
    private int retrydelayArg = 1;

    @Option(name = {"--trace"},
            description = "(=0 (=off)) The trace level of network traffic.")
    private int traceArg = 0;

    @Option(name = {"--printTraceEveryXOperation"},
            description = "(=1) How often to to tracing.")
    private int traceEveryXOperation = 1;

    @Option(name = {"--validate"},
            description = "Run validation tool on input files instead of feeding them.")
    private boolean validateArg = false;

    @Option(name = {"--priority"},
            description = "Specify priority of sent messages, see documentation ")
    private String priorityArg = null;

    @Option(name = {"--numPersistentConnectionsPerEndpoint"},
            description = "How many tcp connections to establish per endoint.)")
    private int numPersistentConnectionsPerEndpoint = 16;

    @Option(name = {"--maxChunkSizeBytes"},
            description = "How much data to send to gateway in each message.")
    private int maxChunkSizeBytes = 20 * 1024;

    @Option(name = {"--whenVerboseEnabledPrintMessageForEveryXDocuments"},
            description = "How often to print verbose message.)")
    private int whenVerboseEnabledPrintMessageForEveryXDocuments = 1000;

    int getWhenVerboseEnabledPrintMessageForEveryXDocuments() {
        return whenVerboseEnabledPrintMessageForEveryXDocuments;
    }

    public String getFile() { return fileArg; };

    public boolean getVerbose() { return verboaseArg; }

    public boolean getAddRootElementToXml() { return addRootElementToXml; }

    SessionParams createSessionParams(boolean useJson) {
        final int minThrottleValue = useDynamicThrottlingArg ? 10 : 0;
        SessionParams.Builder builder = new SessionParams.Builder()
                .setFeedParams(
                        new FeedParams.Builder()
                                .setDataFormat(useJson
                                        ? FeedParams.DataFormat.JSON_UTF8
                                        : FeedParams.DataFormat.XML_UTF8)
                                .setRoute(routeArg)
                                .setMaxInFlightRequests(maxPendingOperationCountArg)
                                .setClientTimeout(timeoutArg, TimeUnit.SECONDS)
                                .setServerTimeout(timeoutArg, TimeUnit.SECONDS)
                                .setLocalQueueTimeOut(timeoutArg * 1000)
                                .setPriority(priorityArg)
                                .setMaxChunkSizeBytes(maxChunkSizeBytes)
                                .build()
                )
                .setConnectionParams(
                        new ConnectionParams.Builder()
                                .setNumPersistentConnectionsPerEndpoint(16)
                                .setEnableV3Protocol(! enableV2Protocol)
                                .setUseCompression(useCompressionArg)
                                .setMaxRetries(noRetryArg ? 0 : 100)
                                .setMinTimeBetweenRetries(retrydelayArg, TimeUnit.SECONDS)
                                .setDryRun(validateArg)
                                .setTraceLevel(traceArg)
                                .setTraceEveryXOperation(traceEveryXOperation)
                                .setPrintTraceToStdErr(traceArg > 0)
                                .setNumPersistentConnectionsPerEndpoint(numPersistentConnectionsPerEndpoint)
                                .build()
                )
                        // Enable dynamic throttling.
                .setThrottlerMinSize(minThrottleValue)
                .setClientQueueSize(maxPendingOperationCountArg);
        Iterable<String> hosts = Splitter.on(',').trimResults().split(hostArg);
        for (String host : hosts) {
            builder.addCluster(new Cluster.Builder()
                    .addEndpoint(Endpoint.create(host, portArg, false))
                    .build());
        }
        return builder.build();
    }
}