Esempio n. 1
0
  /**
   * Builds the Elasticsearch client using the properties this connection was instantiated with
   *
   * @return
   * @throws SQLException
   */
  private Client buildClient() throws SQLException {
    if (props.containsKey("test")) { // used for integration tests
      return ESIntegTestCase.client();
    } else
      try {
        Settings.Builder settingsBuilder = Settings.settingsBuilder();
        for (Object key : this.props.keySet()) {
          settingsBuilder.put(key, this.props.get(key));
        }
        Settings settings = settingsBuilder.build();
        TransportClient client =
            TransportClient.builder()
                .settings(settings)
                .build()
                .addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(host), port));

        // add additional hosts if set in URL query part
        if (this.props.containsKey("es.hosts"))
          for (String hostPort : this.props.getProperty("es.hosts").split(",")) {
            String newHost = hostPort.split(":")[0].trim();
            int newPort =
                (hostPort.split(":").length > 1
                    ? Integer.parseInt(hostPort.split(":")[1])
                    : Utils.PORT);
            client.addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName(newHost), newPort));
            logger.info("Adding additional ES host: " + hostPort);
          }

        // check if index exists
        if (index != null) {
          boolean indexExists =
              client
                  .admin()
                  .indices()
                  .exists(new IndicesExistsRequest(index))
                  .actionGet()
                  .isExists();
          if (!indexExists) throw new SQLException("Index or Alias '" + index + "' does not exist");
        }
        return client;
      } catch (UnknownHostException e) {
        throw new SQLException("Unable to connect to " + host, e);
      } catch (Throwable t) {
        throw new SQLException("Unable to connect to database", t);
      }
  }
  @Override
  protected void beforeIndexDeletion() throws Exception {
    super.beforeIndexDeletion();
    assertBusy(
        () -> {
          IndicesStatsResponse stats = client().admin().indices().prepareStats().clear().get();
          for (IndexStats indexStats : stats.getIndices().values()) {
            for (IndexShardStats indexShardStats : indexStats.getIndexShards().values()) {
              Optional<ShardStats> maybePrimary =
                  Stream.of(indexShardStats.getShards())
                      .filter(s -> s.getShardRouting().active() && s.getShardRouting().primary())
                      .findFirst();
              if (maybePrimary.isPresent() == false) {
                continue;
              }
              ShardStats primary = maybePrimary.get();
              final SeqNoStats primarySeqNoStats = primary.getSeqNoStats();
              assertThat(
                  primary.getShardRouting() + " should have set the global checkpoint",
                  primarySeqNoStats.getGlobalCheckpoint(),
                  not(equalTo(SequenceNumbersService.UNASSIGNED_SEQ_NO)));
              for (ShardStats shardStats : indexShardStats) {
                final SeqNoStats seqNoStats = shardStats.getSeqNoStats();
                assertThat(
                    shardStats.getShardRouting() + " local checkpoint mismatch",
                    seqNoStats.getLocalCheckpoint(),
                    equalTo(primarySeqNoStats.getLocalCheckpoint()));

                assertThat(
                    shardStats.getShardRouting() + " global checkpoint mismatch",
                    seqNoStats.getGlobalCheckpoint(),
                    equalTo(primarySeqNoStats.getGlobalCheckpoint()));
                assertThat(
                    shardStats.getShardRouting() + " max seq no mismatch",
                    seqNoStats.getMaxSeqNo(),
                    equalTo(primarySeqNoStats.getMaxSeqNo()));
              }
            }
          }
        });
  }