Java Code Examples for org.elasticsearch.action.ActionFuture#actionGet()

The following examples show how to use org.elasticsearch.action.ActionFuture#actionGet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * Check health of this specific index.
 */
@Override
public Health getIndexHealth() {

    try {
        String[] indexNames = this.getIndexes();
        final ActionFuture<ClusterHealthResponse> future =  esProvider.getClient().admin().cluster().health(
            new ClusterHealthRequest( indexNames  ) );

        //only wait 2 seconds max
        ClusterHealthResponse chr = future.actionGet(2000);
        return Health.valueOf( chr.getStatus().name() );
    }
    catch ( Exception ex ) {
        logger.error( "Error connecting to ElasticSearch", ex.getMessage() );
    }

    // this is bad, red alert!
    return Health.RED;
}
 
Example 2
Source File: ElasticsearchAssertions.java    From crate with Apache License 2.0 6 votes vote down vote up
public static void assertThrows(ActionFuture future, RestStatus status, String extraInfo) {
    boolean fail = false;
    extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": ";
    extraInfo += "expected a " + status + " status exception to be thrown";

    try {
        future.actionGet();
        fail = true;
    } catch (Exception e) {
        assertThat(extraInfo, ExceptionsHelper.status(e), equalTo(status));
    }
    // has to be outside catch clause to get a proper message
    if (fail) {
        throw new AssertionError(extraInfo);
    }
}
 
Example 3
Source File: ElasticsearchAssertions.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Run future.actionGet() and check that it throws an exception of the right type, optionally checking the exception's rest status
 *
 * @param exceptionClass expected exception class
 * @param status         {@link org.elasticsearch.rest.RestStatus} to check for. Can be null to disable the check
 * @param extraInfo      extra information to add to the failure message. Can be null.
 */
public static <E extends Throwable> void assertThrows(ActionFuture future, Class<E> exceptionClass,
        @Nullable RestStatus status, @Nullable String extraInfo) {
    boolean fail = false;
    extraInfo = extraInfo == null || extraInfo.isEmpty() ? "" : extraInfo + ": ";
    extraInfo += "expected a " + exceptionClass + " exception to be thrown";

    if (status != null) {
        extraInfo += " with status [" + status + "]";
    }

    try {
        future.actionGet();
        fail = true;

    } catch (ElasticsearchException esException) {
        assertThat(extraInfo, esException.unwrapCause(), instanceOf(exceptionClass));
        if (status != null) {
            assertThat(extraInfo, ExceptionsHelper.status(esException), equalTo(status));
        }
    } catch (Exception e) {
        assertThat(extraInfo, e, instanceOf(exceptionClass));
        if (status != null) {
            assertThat(extraInfo, ExceptionsHelper.status(e), equalTo(status));
        }
    }
    // has to be outside catch clause to get a proper message
    if (fail) {
        throw new AssertionError(extraInfo);
    }
}
 
Example 4
Source File: SysCheckerIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectingConcurrentlyFromSysCheckPassesWithoutExceptions() {
    internalCluster().startNode();
    internalCluster().startNode();
    internalCluster().ensureAtLeastNumDataNodes(2);

    ArrayList<ActionFuture<SQLResponse>> responses = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        responses.add(sqlExecutor.execute("select * from sys.checks", null));
    }
    for (ActionFuture<SQLResponse> response : responses) {
        response.actionGet(5, TimeUnit.SECONDS);
    }
}
 
Example 5
Source File: DLBasedEngine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    try {
        if (isPrimaryInRouting) {
            ActionFuture<IndexShardStatsResponse>  result = indexShardStatsAction.execute(new IndexShardStatsRequest(shardId.index().getName(), shardId.id()));
            IndexShardStatsResponse response = result.actionGet(10000);
            if (response.getTotalShards() == response.getSuccessfulShards() && response.getTotalShards() > 0) {
                DLSN miniumDlsn = new DLSN(Long.MAX_VALUE, 0, 0);
                boolean hasInvalidShardStats = false;
                for (ShardStats shardStats : response.getShards()) {
                    boolean isInvalid = true;
                    CommonStats commonStats = shardStats.getStats();
                    if (commonStats != null) {
                        DLStats dlStats = commonStats.getDLStats();
                        if (dlStats != null && !dlStats.getCommitDlsn().equals(DLSN.InvalidDLSN)) {
                            isInvalid = false;
                            if (dlStats.getCommitDlsn().compareTo(miniumDlsn) < 0) {
                                miniumDlsn = dlStats.getCommitDlsn();
                            }
                        } else {
                            logger.debug("dl [{}] status is invalid", dlStats);
                        }
                    } else {
                        logger.debug("common stats is null");
                    }
                    if (isInvalid) {
                        hasInvalidShardStats = true;
                        break;
                    }
                }
                if (!hasInvalidShardStats) {
                    if (!miniumDlsn.equals(DLSN.InitialDLSN)) {
                        logger.info("try to truncate log before [{}]", miniumDlsn);
                        dlTranslog.truncateLogBeforeDLSN(miniumDlsn);
                    }
                }
            } else {
                logger.warn("some shards failed to get stats: total shards [{}], shards give reponse [{}], failure exceptions [{}], maybe some shard is abnormal so skip truncate log", 
                        response.getTotalShards(), 
                        response.getSuccessfulShards(),
                        response.getShardFailures().length > 0 ? response.getShardFailures()[0] : "");
            }
        }
    } catch (Throwable t) {
        logger.error("catch exception when sleep to next lease check time", t);
    } finally {
        if (!isClosed.get()) {
            // check in 1 minutes
            threadPool.schedule(TimeValue.timeValueMinutes(1), ThreadPool.Names.GENERIC, this);
        }
    }
}
 
Example 6
Source File: ElasticSearchBulk.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private boolean processBatch( boolean makeNew ) throws KettleStepException {


    ActionFuture<BulkResponse> actionFuture = currentRequest.execute();
    boolean responseOk = false;

    BulkResponse response = null;
    try {
      if ( timeout != null && timeoutUnit != null ) {
        response = actionFuture.actionGet( timeout, timeoutUnit );
      } else {
        response = actionFuture.actionGet();
      }
    } catch ( ElasticsearchException e ) {
      String msg = BaseMessages.getString( PKG, "ElasticSearchBulk.Error.BatchExecuteFail", e.getLocalizedMessage() );
      if ( e instanceof ElasticsearchTimeoutException ) {
        msg = BaseMessages.getString( PKG, "ElasticSearchBulk.Error.Timeout" );
      }
      logError( msg );
      rejectAllRows( msg );
    }

    if ( response != null ) {
      responseOk = handleResponse( response );
      requestsBuffer.clear();
    } else { // have to assume all failed
      numberOfErrors += currentRequest.numberOfActions();
      setErrors( numberOfErrors );
    }
    // duration += response.getTookInMillis(); //just in trunk..

    if ( makeNew ) {
      currentRequest = client.prepareBulk();
      data.nextBufferRowIdx = 0;
      data.inputRowBuffer = new Object[batchSize][];
    } else {
      currentRequest = null;
      data.inputRowBuffer = null;
    }

    return responseOk;
  }