• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10006

06 Sep 2023 05:15AM CUT coverage: 47.697% (+0.006%) from 47.691%
#10006

push

travis_ci

web-flow
[RatisConsensus] retry cache expiration time should be longer than retriable-client wait duration (#11045) (#11052)

13 of 13 new or added lines in 1 file covered. (100.0%)

80213 of 168172 relevant lines covered (47.7%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/iotdb-client/cli/src/main/java/org/apache/iotdb/cli/Cli.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.cli;
21

22
import org.apache.iotdb.cli.utils.JlineUtils;
23
import org.apache.iotdb.exception.ArgsErrorException;
24
import org.apache.iotdb.jdbc.Config;
25
import org.apache.iotdb.jdbc.IoTDBConnection;
26
import org.apache.iotdb.rpc.RpcUtils;
27

28
import org.apache.commons.cli.CommandLine;
29
import org.apache.commons.cli.CommandLineParser;
30
import org.apache.commons.cli.DefaultParser;
31
import org.apache.commons.cli.HelpFormatter;
32
import org.apache.commons.cli.Options;
33
import org.apache.commons.cli.ParseException;
34
import org.apache.thrift.TException;
35
import org.jline.reader.EndOfFileException;
36
import org.jline.reader.LineReader;
37
import org.jline.reader.UserInterruptException;
38

39
import java.io.IOException;
40
import java.sql.DriverManager;
41
import java.sql.SQLException;
42

43
import static org.apache.iotdb.cli.utils.IoTPrinter.println;
44
import static org.apache.iotdb.jdbc.Config.IOTDB_ERROR_PREFIX;
45

46
/** args[]: -h 127.0.0.1 -p 6667 -u root -pw root */
47
public class Cli extends AbstractCli {
×
48

49
  private static CommandLine commandLine;
50
  private static LineReader lineReader;
51

52
  /**
53
   * IoTDB Client main function.
54
   *
55
   * @param args launch arguments
56
   * @throws ClassNotFoundException ClassNotFoundException
57
   */
58
  public static void main(String[] args) throws ClassNotFoundException, IOException {
59
    Class.forName(Config.JDBC_DRIVER_NAME);
×
60
    Options options = createOptions();
×
61
    HelpFormatter hf = new HelpFormatter();
×
62
    hf.setWidth(MAX_HELP_CONSOLE_WIDTH);
×
63
    commandLine = null;
×
64

65
    if (args == null || args.length == 0) {
×
66
      println(
×
67
          "Require more params input, eg. ./start-cli.sh(start-cli.bat if Windows) "
68
              + "-h xxx.xxx.xxx.xxx -p xxxx -u xxx.");
69
      println("For more information, please check the following hint.");
×
70
      hf.printHelp(SCRIPT_HINT, options, true);
×
71
      System.exit(CODE_ERROR);
×
72
    }
73
    init();
×
74
    String[] newArgs = removePasswordArgs(args);
×
75
    String[] newArgs2 = processExecuteArgs(newArgs);
×
76
    boolean continues = parseCommandLine(options, newArgs2, hf);
×
77
    if (!continues) {
×
78
      System.exit(CODE_ERROR);
×
79
    }
80

81
    try {
82
      host = checkRequiredArg(HOST_ARGS, HOST_NAME, commandLine, false, host);
×
83
      port = checkRequiredArg(PORT_ARGS, PORT_NAME, commandLine, false, port);
×
84
      username = checkRequiredArg(USERNAME_ARGS, USERNAME_NAME, commandLine, true, null);
×
85
    } catch (ArgsErrorException e) {
×
86
      println(IOTDB_ERROR_PREFIX + "Input params error because" + e.getMessage());
×
87
      System.exit(CODE_ERROR);
×
88
    } catch (Exception e) {
×
89
      println(IOTDB_ERROR_PREFIX + "Exit cli with error " + e.getMessage());
×
90
      System.exit(CODE_ERROR);
×
91
    }
×
92

93
    lineReader = JlineUtils.getLineReader(username, host, port);
×
94
    serve();
×
95
  }
×
96

97
  private static boolean parseCommandLine(Options options, String[] newArgs, HelpFormatter hf) {
98
    try {
99
      CommandLineParser parser = new DefaultParser();
×
100
      commandLine = parser.parse(options, newArgs);
×
101
      if (commandLine.hasOption(HELP_ARGS)) {
×
102
        hf.printHelp(SCRIPT_HINT, options, true);
×
103
        return false;
×
104
      }
105
      if (commandLine.hasOption(RPC_COMPRESS_ARGS)) {
×
106
        Config.rpcThriftCompressionEnable = true;
×
107
      }
108
      if (commandLine.hasOption(ISO8601_ARGS)) {
×
109
        timeFormat = RpcUtils.setTimeFormat("long");
×
110
      }
111
      if (commandLine.hasOption(MAX_PRINT_ROW_COUNT_ARGS)) {
×
112
        setMaxDisplayNumber(commandLine.getOptionValue(MAX_PRINT_ROW_COUNT_ARGS));
×
113
      }
114
      if (commandLine.hasOption(TIMEOUT_ARGS)) {
×
115
        setQueryTimeout(commandLine.getOptionValue(TIMEOUT_ARGS));
×
116
      }
117
    } catch (ParseException e) {
×
118
      println(
×
119
          "Require more params input, eg. ./start-cli.sh(start-cli.bat if Windows) "
120
              + "-h xxx.xxx.xxx.xxx -p xxxx -u xxx.");
121
      println("For more information, please check the following hint.");
×
122
      hf.printHelp(IOTDB_CLI_PREFIX, options, true);
×
123
      return false;
×
124
    } catch (NumberFormatException e) {
×
125
      println(
×
126
          IOTDB_ERROR_PREFIX
127
              + ": error format of max print row count, it should be an integer number");
128
      return false;
×
129
    }
×
130
    return true;
×
131
  }
132

133
  private static void serve() {
134
    try {
135
      password = commandLine.getOptionValue(PW_ARGS);
×
136
      if (hasExecuteSQL && password != null) {
×
137
        executeSql();
×
138
      }
139
      if (password == null) {
×
140
        password = lineReader.readLine("please input your password:", '\0');
×
141
      }
142
      receiveCommands(lineReader);
×
143
    } catch (Exception e) {
×
144
      println(IOTDB_ERROR_PREFIX + ": Exit cli with error: " + e.getMessage());
×
145
      System.exit(CODE_ERROR);
×
146
    }
×
147
  }
×
148

149
  private static void executeSql() throws TException {
150
    try (IoTDBConnection connection =
×
151
        (IoTDBConnection)
152
            DriverManager.getConnection(
×
153
                Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
154
      connection.setQueryTimeout(queryTimeout);
×
155
      properties = connection.getServerProperties();
×
156
      timestampPrecision = properties.getTimestampPrecision();
×
157
      AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
×
158
      processCommand(execute, connection);
×
159
      System.exit(lastProcessStatus);
×
160
    } catch (SQLException e) {
×
161
      println(IOTDB_ERROR_PREFIX + "Can't execute sql because" + e.getMessage());
×
162
      System.exit(CODE_ERROR);
×
163
    }
×
164
  }
×
165

166
  private static void receiveCommands(LineReader reader) throws TException {
167
    try (IoTDBConnection connection =
×
168
        (IoTDBConnection)
169
            DriverManager.getConnection(
×
170
                Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
171
      connection.setQueryTimeout(queryTimeout);
×
172
      properties = connection.getServerProperties();
×
173
      AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
×
174
      timestampPrecision = properties.getTimestampPrecision();
×
175

176
      echoStarting();
×
177
      displayLogo(properties.getLogo(), properties.getVersion(), properties.getBuildInfo());
×
178
      println(String.format("Successfully login at %s:%s", host, port));
×
179
      while (true) {
180
        boolean readLine = readerReadLine(reader, connection);
×
181
        if (readLine) {
×
182
          break;
×
183
        }
184
      }
×
185
    } catch (SQLException e) {
×
186
      println(
×
187
          String.format(
×
188
              "%s: %s Host is %s, port is %s.", IOTDB_ERROR_PREFIX, e.getMessage(), host, port));
×
189
      System.exit(CODE_ERROR);
×
190
    }
×
191
  }
×
192

193
  private static boolean readerReadLine(LineReader reader, IoTDBConnection connection) {
194
    String s;
195
    try {
196
      s = reader.readLine(IOTDB_CLI_PREFIX + "> ", null);
×
197
      boolean continues = processCommand(s, connection);
×
198
      if (!continues) {
×
199
        return true;
×
200
      }
201
    } catch (UserInterruptException e) {
×
202
      // Exit on signal INT requires confirmation.
203
      readLine(reader);
×
204
    } catch (EndOfFileException e) {
×
205
      // Exit on EOF (usually by pressing CTRL+D).
206
      System.exit(CODE_OK);
×
207
    }
×
208
    return false;
×
209
  }
210

211
  private static void readLine(LineReader reader) {
212
    try {
213
      reader.readLine("Press CTRL+C again to exit, or press ENTER to continue", '\0');
×
214
    } catch (UserInterruptException | EndOfFileException e2) {
×
215
      System.exit(CODE_OK);
×
216
    }
×
217
  }
×
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc