Exemple #1
0
  /**
   * Constructor that initializes the framework portions of the client. Creates a Volt client and
   * connects it to all the hosts provided on the command line with the specified username and
   * password
   *
   * @param args
   */
  public ClientMain(String args[]) {
    /*
     * Input parameters: HOST=host:port (may occur multiple times)
     * USER=username PASSWORD=password
     */

    // default values
    String username = "";
    String password = "";
    ControlState state = ControlState.PREPARING; // starting state
    String reason = ""; // and error string
    int transactionRate = -1;
    int id = 0;
    boolean exitOnCompletion = true;
    float checkTransaction = 0;
    boolean checkTables = false;
    String statsDatabaseURL = null;
    String deploymentFilePath = null;
    int statsPollInterval = 10000;
    Integer maxOutstanding = null;

    // scan the inputs once to read everything but host names
    for (final String arg : args) {
      final String[] parts = arg.split("=", 2);
      if (parts.length == 1) {
        state = ControlState.ERROR;
        reason = "Invalid parameter: " + arg;
        break;
      } else if (parts[1].startsWith("${")) {
        continue;
      } else if (parts[0].equals("USER")) {
        username = parts[1];
      } else if (parts[0].equals("PASSWORD")) {
        password = parts[1];
      } else if (parts[0].equals("EXITONCOMPLETION")) {
        exitOnCompletion = Boolean.parseBoolean(parts[1]);
      } else if (parts[0].equals("TXNRATE")) {
        transactionRate = Integer.parseInt(parts[1]);
      } else if (parts[0].equals("ID")) {
        id = Integer.parseInt(parts[1]);
      } else if (parts[0].equals("CHECKTRANSACTION")) {
        checkTransaction = Float.parseFloat(parts[1]);
      } else if (parts[0].equals("CHECKTABLES")) {
        checkTables = Boolean.parseBoolean(parts[1]);
      } else if (parts[0].equals("STATSDATABASEURL")) {
        statsDatabaseURL = parts[1];
      } else if (parts[0].equals("STATSPOLLINTERVAL")) {
        statsPollInterval = Integer.parseInt(parts[1]);
      } else if (parts[0].equals("DEPLOYMENTFILEPATH")) {
        deploymentFilePath = parts[1];
      } else if (parts[0].equals("MAXOUTSTANDING")) {
        maxOutstanding = Integer.parseInt(parts[1]);
      }
    }
    StatsUploaderSettings statsSettings = null;
    if (statsDatabaseURL != null) {
      try {
        statsSettings =
            new StatsUploaderSettings(
                statsDatabaseURL, getApplicationName(), getSubApplicationName(), statsPollInterval);
      } catch (Exception e) {
        System.err.println(e.getMessage());
        // e.printStackTrace();
        statsSettings = null;
      }
    }
    ClientConfig clientConfig = new ClientConfig(username, password);
    clientConfig.setExpectedOutgoingMessageSize(getExpectedOutgoingMessageSize());
    clientConfig.setHeavyweight(useHeavyweightClient());
    clientConfig.setStatsUploaderSettings(statsSettings);
    if (maxOutstanding != null) {
      clientConfig.setMaxOutstandingTxns(maxOutstanding);
    }

    m_voltClient = ClientFactory.createClient(clientConfig);

    m_id = id;
    m_exitOnCompletion = exitOnCompletion;
    m_username = username;
    m_password = password;
    m_txnRate = transactionRate;
    m_txnsPerMillisecond = transactionRate / 1000.0;
    m_deploymentFilePath = deploymentFilePath;

    // report any errors that occurred before the client was instantiated
    if (state != ControlState.PREPARING) setState(state, reason);

    // scan the inputs again looking for host connections
    boolean atLeastOneConnection = false;
    for (final String arg : args) {
      final String[] parts = arg.split("=", 2);
      if (parts.length == 1) {
        continue;
      } else if (parts[0].equals("HOST")) {
        final String hostnport[] = parts[1].split("\\:", 2);
        m_host = hostnport[0];
        String host = hostnport[0];
        int port;
        if (hostnport.length < 2) {
          port = Client.VOLTDB_SERVER_PORT;
        } else {
          port = Integer.valueOf(hostnport[1]);
        }
        try {
          System.err.println("Creating connection to  " + host + ":" + port);
          createConnection(host, port);
          System.err.println("Created connection.");
          atLeastOneConnection = true;
        } catch (final Exception ex) {
          setState(
              ControlState.ERROR, "createConnection to " + arg + " failed: " + ex.getMessage());
        }
      }
    }
    if (!atLeastOneConnection) setState(ControlState.ERROR, "No HOSTS specified on command line.");
    m_checkTransaction = checkTransaction;
    m_checkTables = checkTables;
    m_constraints = new LinkedHashMap<Pair<String, Integer>, Expression>();

    m_countDisplayNames = getTransactionDisplayNames();
    m_counts = new AtomicLong[m_countDisplayNames.length];
    for (int ii = 0; ii < m_counts.length; ii++) {
      m_counts[ii] = new AtomicLong(0);
    }
  }