org.elasticsearch.index.IndexNotFoundException Java Examples

The following examples show how to use org.elasticsearch.index.IndexNotFoundException. 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: AnomalyDetectionIndices.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void deleteIndexIteration(String[] toDelete) {
    for (String index : toDelete) {
        DeleteIndexRequest singleDeleteRequest = new DeleteIndexRequest(index);
        adminClient.indices().delete(singleDeleteRequest, ActionListener.wrap(singleDeleteResponse -> {
            if (!singleDeleteResponse.isAcknowledged()) {
                logger.error("Retrying deleting {} does not succeed.", index);
            }
        }, exception -> {
            if (exception instanceof IndexNotFoundException) {
                logger.info("{} was already deleted.", index);
            } else {
                logger.error(new ParameterizedMessage("Retrying deleting {} does not succeed.", index), exception);
            }
        }));
    }
}
 
Example #2
Source File: AnomalyResultTransportAction.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void findException(Throwable cause, String adID, AtomicReference<AnomalyDetectionException> failure) {
    if (cause instanceof Error) {
        // we cannot do anything with Error.
        LOG.error(new ParameterizedMessage("Error during prediction for {}: ", adID), cause);
        return;
    }

    Exception causeException = (Exception) cause;
    if (isException(causeException, ResourceNotFoundException.class, RESOURCE_NOT_FOUND_EXCEPTION_NAME_UNDERSCORE)
        || (causeException instanceof IndexNotFoundException
            && causeException.getMessage().contains(CommonName.CHECKPOINT_INDEX_NAME))) {
        failure.set(new ResourceNotFoundException(adID, causeException.getMessage()));
    } else if (isException(causeException, LimitExceededException.class, LIMIT_EXCEEDED_EXCEPTION_NAME_UNDERSCORE)) {
        failure.set(new LimitExceededException(adID, causeException.getMessage()));
    } else if (causeException instanceof ElasticsearchTimeoutException) {
        // we can have ElasticsearchTimeoutException when a node tries to load RCF or
        // threshold model
        failure.set(new InternalFailure(adID, causeException));
    } else {
        // some unexpected bugs occur while predicting anomaly
        failure.set(new EndRunException(adID, "We might have bugs.", causeException, false));
    }
}
 
Example #3
Source File: InternalCountOperation.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public long count(String index, int shardId, WhereClause whereClause) throws IOException, InterruptedException {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (PartitionName.isPartition(index)) {
            return 0L;
        }
        throw e;
    }

    IndexShard indexShard = indexService.shardSafe(shardId);
    try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
        LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(
                whereClause, indexService.mapperService(), indexService.fieldData(), indexService.cache());
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        return searcher.searcher().count(queryCtx.query());
    }
}
 
Example #4
Source File: DocTableInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private GroupShardsIterator getShardIterators(WhereClause whereClause,
                                              @Nullable String preference,
                                              ClusterState clusterState) throws IndexNotFoundException {
    String[] routingIndices = concreteIndices;
    if (whereClause.partitions().size() > 0) {
        routingIndices = whereClause.partitions().toArray(new String[whereClause.partitions().size()]);
    }

    Map<String, Set<String>> routingMap = null;
    if (whereClause.clusteredBy().isPresent()) {
        routingMap = indexNameExpressionResolver.resolveSearchRouting(
                clusterState, whereClause.routingValues(), routingIndices);
    }
    return clusterService.operationRouting().searchShards(
            clusterState,
            routingIndices,
            routingMap,
            preference
    );
}
 
Example #5
Source File: DocTableInfo.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
private Routing getRouting(ClusterState state, WhereClause whereClause, String preference, final List<ShardId> missingShards) {
    final Map<String, Map<String, List<Integer>>> locations = new TreeMap<>();
    GroupShardsIterator shardIterators;
    try {
        shardIterators = getShardIterators(whereClause, preference, state);
    } catch (IndexNotFoundException e) {
        return new Routing(locations);
    }

    fillLocationsFromShardIterators(locations, shardIterators, missingShards);

    if (missingShards.isEmpty()) {
        return new Routing(locations);
    } else {
        return null;
    }
}
 
Example #6
Source File: EsRetry2Test.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ドキュメント新規作成時_初回でIndexMissingExceptionが発生した場合にEsClient_EsIndexMissingExceptionが返されること.
 */
@Test(expected = EsClientException.EsIndexMissingException.class)
public void ドキュメント新規作成時_初回でIndexMissingExceptionが発生した場合にEsClient_EsIndexMissingExceptionが返されること() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncIndex()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("dummy");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncIndex(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class),
                    Mockito.any(OpType.class), Mockito.anyLong());
    // メソッド呼び出し
    esTypeObject.create("dummyId", null);
    fail("EsIndexMissingException should be thrown.");
}
 
Example #7
Source File: TransportClusterHealthAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private ClusterHealthResponse clusterHealth(ClusterHealthRequest request, ClusterState clusterState, int numberOfPendingTasks, int numberOfInFlightFetch,
                                            TimeValue pendingTaskTimeInQueue) {
    if (logger.isTraceEnabled()) {
        logger.trace("Calculating health based on state version [{}]", clusterState.version());
    }

    if (request.getHeader(LoginUserContext.TENANT_FILTER) != null) {
        clusterState = AuthService.filterState(clusterState, clusterState.metaData(), (Long) request.getHeader(LoginUserContext.TENANT_FILTER));
    }
    String[] concreteIndices;
    try {
        concreteIndices = indexNameExpressionResolver.concreteIndices(clusterState, request);
    } catch (IndexNotFoundException e) {
        // one of the specified indices is not there - treat it as RED.
        ClusterHealthResponse response = new ClusterHealthResponse(clusterName.value(), Strings.EMPTY_ARRAY, clusterState,
                numberOfPendingTasks, numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState),
                pendingTaskTimeInQueue);
        response.setStatus(ClusterHealthStatus.RED);
        return response;
    }

    return new ClusterHealthResponse(clusterName.value(), concreteIndices, clusterState, numberOfPendingTasks,
            numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState), pendingTaskTimeInQueue);
}
 
Example #8
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void addIndex(String indexName) {
    try {
        elasticSearchClient.admin()
                .indices()
                .prepareGetIndex()
                .addIndices(indexName)
                .execute()
                .actionGet();
    } catch (IndexNotFoundException infe) {
        try {

            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", config.getElasticSearchIndexShardCount())
                    .put("index.number_of_replicas", config.getElasticSearchIndexReplicationCount())
            );

            elasticSearchClient.admin()
                    .indices()
                    .create(createIndexRequest)
                    .actionGet();
        } catch (ResourceAlreadyExistsException done) {
            // no-op
        }
    }
}
 
Example #9
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsIndex_searchメソッドで初回にIndexMissingExceptionが投げられた場合のテスト.
 */
@Test
public void EsIndex_searchメソッドで初回にIndexMissingExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsIndexImpl esIndexObject = Mockito.spy(new EsIndexImpl("dummy", "", 0, 0, null));

    // EsIndex#asyncIndexSearch()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esIndexObject)
            .asyncIndexSearch(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class));
    // メソッド呼び出し
    DcSearchResponse result = esIndexObject.search("dummyRoutingId", (Map<String, Object>) null);
    assertNull(result);
}
 
Example #10
Source File: EsRetryTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * ドキュメント新規作成時_初回でIndexMissingExceptionが発生した場合にEsClient_EsIndexMissingExceptionが返されること.
 */
@Test(expected = EsClientException.EsIndexMissingException.class)
public void ドキュメント新規作成時_初回でIndexMissingExceptionが発生した場合にEsClient_EsIndexMissingExceptionが返されること() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncIndex()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("dummy");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncIndex(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class),
                    Mockito.any(OpType.class), Mockito.anyLong());
    // メソッド呼び出し
    esTypeObject.create("dummyId", null);
    fail("EsIndexMissingException should be thrown.");
}
 
Example #11
Source File: ElasticsearchMetaAlertDaoTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateShouldUpdateOnMissingMetaAlertIndex() throws Exception {
  ElasticsearchDao elasticsearchDao = mock(ElasticsearchDao.class);
  ElasticsearchMetaAlertRetrieveLatestDao elasticsearchMetaAlertRetrieveLatestDao = mock(ElasticsearchMetaAlertRetrieveLatestDao.class);
  MetaAlertConfig metaAlertConfig = mock(MetaAlertConfig.class);
  ElasticsearchMetaAlertUpdateDao emauDao = spy(new ElasticsearchMetaAlertUpdateDao(elasticsearchDao, elasticsearchMetaAlertRetrieveLatestDao, metaAlertConfig, 1));

  doThrow(new IndexNotFoundException(ElasticsearchMetaAlertDao.METAALERTS_INDEX)).when(emauDao).getMetaAlertsForAlert("alert_one");

  Document update = new Document(new HashMap<>(), "alert_one", "", 0L);
  emauDao.update(update, Optional.empty());

  Map<Document, Optional<String>> expectedUpdate = new HashMap<Document, Optional<String>>() {{
    put(update, Optional.empty());
  }};
  verify(elasticsearchDao).batchUpdate(expectedUpdate);
}
 
Example #12
Source File: BlobIntegrationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Nullable
private BlobShard getBlobShard(String digest) {
    Iterable<BlobIndicesService> services = internalCluster().getInstances(BlobIndicesService.class);
    Iterator<BlobIndicesService> it = services.iterator();
    BlobShard blobShard = null;
    while (it.hasNext()) {
        BlobIndicesService nextService = it.next();
        try {
            blobShard = nextService.localBlobShard(".blob_test", digest);
        } catch (ShardNotFoundException | IndexNotFoundException e) {
            continue;
        }
        if (blobShard != null) {
            break;
        }
    }
    return blobShard;
}
 
Example #13
Source File: IndexNameExpressionResolver.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> resolve(Context context, List<String> expressions) {
    IndicesOptions options = context.getOptions();
    MetaData metaData = context.getState().metaData();
    if (options.expandWildcardsClosed() == false && options.expandWildcardsOpen() == false) {
        return expressions;
    }

    if (isEmptyOrTrivialWildcard(expressions)) {
        return resolveEmptyOrTrivialWildcard(options, metaData);
    }

    Set<String> result = innerResolve(context, expressions, options, metaData);

    if (result == null) {
        return expressions;
    }
    if (result.isEmpty() && !options.allowNoIndices()) {
        IndexNotFoundException infe = new IndexNotFoundException((String)null);
        infe.setResources("index_or_alias", expressions.toArray(new String[0]));
        throw infe;
    }
    return new ArrayList<>(result);
}
 
Example #14
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsIndex_searchメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト.
 */
@Test
public void EsIndex_searchメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsIndexImpl esIndexObject = Mockito.spy(new EsIndexImpl("dummy", "", 0, 0, null));

    // EsIndex#asyncIndexSearch()が呼ばれた場合に、IndexMissingExceptionを根本原因に持つ例外を投げる。
    // 送出する例外オブジェクトを作成
    SettingsException toBeThrown = new SettingsException("foo", new IndexNotFoundException("dummy"));
    Mockito.doThrow(toBeThrown)
            .when(esIndexObject)
            .asyncIndexSearch(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class));
    // メソッド呼び出し
    DcSearchResponse result = esIndexObject.search("dummyRoutingId", (Map<String, Object>) null);
    assertNull(result);
}
 
Example #15
Source File: AbstractRetryableEsRequestTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * リトライ対象例外が続き_リトライ5回目でリトライ対象外の例外が発生した場合、EsClientExceptionが投げられること.
 */
@Test(expected = EsClientException.class)
public void リトライ対象例外が続き_リトライ5回目でリトライ対象外の例外が発生した場合_EsClientExceptionが投げられること() {

    TestRequest requestMock = Mockito.spy(new TestRequest());

    ClusterBlockException toBeThrown = Mockito.mock(ClusterBlockException.class);
    IndexNotFoundException toBeThrown2 = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown) // 初回
            .doThrow(toBeThrown) // リトライ1回目
            .doThrow(toBeThrown) // リトライ2回目
            .doThrow(toBeThrown) // リトライ3回目
            .doThrow(toBeThrown) // リトライ4回目
            .doThrow(toBeThrown2) // リトライ5回目
            .when(requestMock)
            .doProcess();

    try {
        requestMock.doRequest();
        fail("Should not return");
    } finally {
        // doProcessが6回呼び出されるはず
        Mockito.verify(requestMock, Mockito.times(6)).doProcess();
        Mockito.verify(requestMock, Mockito.times(0)).onParticularError(Mockito.any(ElasticsearchException.class));
    }
}
 
Example #16
Source File: RetryOnFailureResultReceiverTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryIsInvokedOnIndexNotFoundException() throws Exception {
    AtomicInteger numRetries = new AtomicInteger(0);
    BaseResultReceiver resultReceiver = new BaseResultReceiver();

    ClusterState initialState = clusterService.state();
    RetryOnFailureResultReceiver retryOnFailureResultReceiver = new RetryOnFailureResultReceiver(
        clusterService,
        initialState,
        indexName -> true,
        resultReceiver,
        UUID.randomUUID(),
        (newJobId, receiver) -> numRetries.incrementAndGet());

    // Must have a different cluster state then the initial state to trigger a retry
    clusterService.submitStateUpdateTask("dummy", new DummyUpdate());
    assertBusy(() -> assertThat(initialState, Matchers.not(sameInstance(clusterService.state()))));

    retryOnFailureResultReceiver.fail(new IndexNotFoundException("t1"));

    assertThat(numRetries.get(), is(1));
}
 
Example #17
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_getメソッドで初回にIndexMissingExceptionが投げられた場合のテスト.
 */
@Test
public void EsType_getメソッドで初回にIndexMissingExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncGet()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncGet(Mockito.anyString(), Mockito.anyBoolean());
    // メソッド呼び出し
    DcGetResponse result = esTypeObject.get("dummyId", true);
    assertNull(result);
}
 
Example #18
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_getメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト.
 */
@Test
public void EsType_getメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncGet()が呼ばれた場合に、IndexMissingExceptionを根本原因に持つ例外を投げる。
    // 送出する例外オブジェクトを作成
    SettingsException toBeThrown = new SettingsException("foo", new IndexNotFoundException("dummy"));
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncGet(Mockito.anyString(), Mockito.anyBoolean());
    // メソッド呼び出し
    DcGetResponse result = esTypeObject.get("dummyId", true);
    assertNull(result);
}
 
Example #19
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_updateメソッドで初回にIndexMissingExceptionが投げられた場合のテスト.
 */
@Test
public void EsType_updateメソッドで初回にIndexMissingExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncIndex()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncIndex(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class),
                    (OpType) Mockito.anyObject(), Mockito.anyLong());
    // メソッド呼び出し
    try {
        esTypeObject.update("dummyId", null, 1);
        fail("EsClientException should be thrown.");
    } catch (EsClientException.EsIndexMissingException e) {
        assertTrue(e.getCause() instanceof IndexNotFoundException);
    }
}
 
Example #20
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_updateメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト.
 */
@Test
public void EsType_updateメソッドで初回にIndexMissingExceptionを根本原因に持つ例外が投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncIndex()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトを作成
    SettingsException toBeThrown = new SettingsException("foo", new IndexNotFoundException("dummy"));
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncIndex(Mockito.anyString(), Mockito.anyMapOf(String.class, Object.class),
                    (OpType) Mockito.anyObject(), Mockito.anyLong());
    // メソッド呼び出し
    try {
        esTypeObject.update("dummyId", null, 1);
        fail("EsClientException should be thrown.");
    } catch (EsClientException.EsIndexMissingException e) {
        assertTrue(e.getCause() instanceof SettingsException);
        assertTrue(e.getCause().getCause() instanceof IndexNotFoundException);
    }
}
 
Example #21
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_searchメソッドで初回にIndexNotFoundExceptionが投げられた場合のテスト.
 */
@Test
public void EsType_searchメソッドで初回にIndexNotFoundExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncSearch()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncSearch(Mockito.anyMapOf(String.class, Object.class));
    // メソッド呼び出し
    DcSearchResponse result = esTypeObject.search(null);
    assertTrue(result.isNullResponse());
}
 
Example #22
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_searchメソッドで初回にIndexNotFoundExceptionを根本原因に持つ例外が投げられた場合のテスト.
 */
@Test
public void EsType_searchメソッドで初回にIndexNotFoundExceptionを根本原因に持つ例外が投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncSearch()が呼ばれた場合に、IndexMissingExceptionを根本原因に持つ例外を投げる。
    // 送出する例外オブジェクトを作成
    SettingsException toBeThrown = new SettingsException("foo", new IndexNotFoundException("dummy"));
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncSearch(Mockito.anyMapOf(String.class, Object.class));
    // メソッド呼び出し
    DcSearchResponse result = esTypeObject.search(null);
    assertTrue(result.isNullResponse());
}
 
Example #23
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_deleteメソッドで初回にIndexMissingExceptionが投げられた場合のテスト.
 */
@Test
public void EsType_deleteメソッドで初回にIndexMissingExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncDelete()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown =  new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncDelete(Mockito.anyString(), Mockito.anyLong());
    // メソッド呼び出し
    try {
        esTypeObject.delete("dummyId", 1);
        fail("EsClientException should be thrown.");
    } catch (EsClientException.EsIndexMissingException e) {
        assertTrue(e.getCause() instanceof IndexNotFoundException);
    }
}
 
Example #24
Source File: RoutingTable.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * All the *active* primary shards for the provided indices grouped (each group is a single element, consisting
 * of the primary shard). This is handy for components that expect to get group iterators, but still want in some
 * cases to iterate over all primary shards (and not just one shard in replication group).
 *
 * @param indices The indices to return all the shards (replicas)
 * @return All the primary shards grouped into a single shard element group each
 * @throws IndexNotFoundException If an index passed does not exists
 */
public GroupShardsIterator<ShardIterator> activePrimaryShardsGrouped(String[] indices, boolean includeEmpty) {
    // use list here since we need to maintain identity across shards
    ArrayList<ShardIterator> set = new ArrayList<>();
    for (String index : indices) {
        IndexRoutingTable indexRoutingTable = index(index);
        if (indexRoutingTable == null) {
            throw new IndexNotFoundException(index);
        }
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            ShardRouting primary = indexShardRoutingTable.primaryShard();
            if (primary.active()) {
                set.add(primary.shardsIt());
            } else if (includeEmpty) { // we need this for counting properly, just make it an empty one
                set.add(new PlainShardIterator(primary.shardId(), Collections.<ShardRouting>emptyList()));
            }
        }
    }
    return new GroupShardsIterator<>(set);
}
 
Example #25
Source File: EsRetryOnParticularErrorTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * EsType_putMappingメソッドで初回にIndexMissingExceptionが投げられた場合のテスト.
 */
@Test
public void EsType_putMappingメソッドで初回にIndexMissingExceptionが投げられた場合のテスト() {
    PowerMockito.mockStatic(EsClientException.class);
    EsTypeImpl esTypeObject = Mockito.spy(new EsTypeImpl("dummy", "Test", "TestRoutingId", 0, 0, null));

    // EsType#asyncPutMapping()が呼ばれた場合に、IndexMissingExceptionを投げる。
    // 送出する例外オブジェクトのモックを作成
    IndexNotFoundException toBeThrown = new IndexNotFoundException("abc");
    Mockito.doThrow(toBeThrown)
            .when(esTypeObject)
            .asyncPutMapping(Mockito.anyMapOf(String.class, Object.class));
    // メソッド呼び出し
    try {
        esTypeObject.putMapping(null);
        fail("EsClientException should be thrown.");
    } catch (EsClientException.EsIndexMissingException e) {
        assertTrue(e.getCause() instanceof IndexNotFoundException);
    }
}
 
Example #26
Source File: AnomalyDetectorProfileRunner.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private ActionListener<SearchResponse> onGetLatestAnomalyResult(ActionListener<DetectorProfile> listener, String detectorId) {
    return ActionListener.wrap(searchResponse -> {
        SearchHits hits = searchResponse.getHits();
        if (hits.getHits().length == 0L) {
            listener.onResponse(new DetectorProfile());
        } else {
            SearchHit hit = hits.getAt(0);

            try (
                XContentParser parser = XContentType.JSON
                    .xContent()
                    .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, hit.getSourceAsString())
            ) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
                AnomalyResult result = parser.namedObject(AnomalyResult.class, AnomalyResult.PARSE_FIELD_NAME, null);
                DetectorProfile profile = new DetectorProfile();
                if (result.getError() != null) {
                    profile.setError(result.getError());
                }
                listener.onResponse(profile);

            } catch (IOException | XContentParseException | NullPointerException e) {
                logger.error("Fail to parse anomaly result with " + hit.toString());
                listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, e));
            }
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            listener.onResponse(new DetectorProfile());
        } else {
            logger.error("Fail to find any anomaly result after AD job enabled time for detector {}", detectorId);
            listener.onFailure(new RuntimeException("Fail to find detector error: " + detectorId, exception));
        }
    });
}
 
Example #27
Source File: DailyCron.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(CommonName.CHECKPOINT_INDEX_NAME)
        .setQuery(
            QueryBuilders
                .boolQuery()
                .filter(
                    QueryBuilders
                        .rangeQuery(CheckpointDao.TIMESTAMP)
                        .lte(clock.millis() - checkpointTtl.toMillis())
                        .format(CommonName.EPOCH_MILLIS_FORMAT)
                )
        )
        .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN);
    clientUtil
        .execute(
            DeleteByQueryAction.INSTANCE,
            deleteRequest,
            ActionListener
                .wrap(
                    response -> {
                        // if 0 docs get deleted, it means our query cannot find any matching doc
                        LOG.info("{} " + CHECKPOINT_DELETED_MSG, response.getDeleted());
                    },
                    exception -> {
                        if (exception instanceof IndexNotFoundException) {
                            LOG.info(CHECKPOINT_NOT_EXIST_MSG);
                        } else {
                            // Gonna eventually delete in maintenance window.
                            LOG.error(CANNOT_DELETE_OLD_CHECKPOINT_MSG, exception);
                        }
                    }
                )
        );
}
 
Example #28
Source File: DailyCronTests.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void templateDailyCron(DailyCronTestExecutionMode mode) {
    Clock clock = mock(Clock.class);
    ClientUtil clientUtil = mock(ClientUtil.class);
    DailyCron cron = new DailyCron(clock, Duration.ofHours(24), clientUtil);

    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        assertTrue(String.format("The size of args is %d.  Its content is %s", args.length, Arrays.toString(args)), args.length == 3);
        assertTrue(args[2] instanceof ActionListener);

        ActionListener<BulkByScrollResponse> listener = (ActionListener<BulkByScrollResponse>) args[2];

        if (mode == DailyCronTestExecutionMode.INDEX_NOT_EXIST) {
            listener.onFailure(new IndexNotFoundException("foo", "bar"));
        } else if (mode == DailyCronTestExecutionMode.FAIL) {
            listener.onFailure(new ElasticsearchException("bar"));
        } else {
            BulkByScrollResponse deleteByQueryResponse = mock(BulkByScrollResponse.class);
            when(deleteByQueryResponse.getDeleted()).thenReturn(10L);
            listener.onResponse(deleteByQueryResponse);
        }

        return null;
    }).when(clientUtil).execute(eq(DeleteByQueryAction.INSTANCE), any(), any());

    cron.run();
}
 
Example #29
Source File: ModelsAction.java    From zentity with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve all entity models.
 *
 * @param client The client that will communicate with Elasticsearch.
 * @return The response from Elasticsearch.
 * @throws ForbiddenException
 */
public static SearchResponse getEntityModels(NodeClient client) throws ForbiddenException {
    SearchRequestBuilder request = client.prepareSearch(INDEX_NAME);
    request.setSize(10000);
    try {
        return request.get();
    } catch (IndexNotFoundException e) {
        try {
            SetupAction.createIndex(client);
        } catch (ElasticsearchSecurityException se) {
            throw new ForbiddenException("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup");
        }
        return request.get();
    }
}
 
Example #30
Source File: ModelsAction.java    From zentity with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve one entity model by its type.
 *
 * @param entityType The entity type.
 * @param client     The client that will communicate with Elasticsearch.
 * @return The response from Elasticsearch.
 * @throws ForbiddenException
 */
public static GetResponse getEntityModel(String entityType, NodeClient client) throws ForbiddenException {
    GetRequestBuilder request = client.prepareGet(INDEX_NAME, "doc", entityType);
    try {
        return request.get();
    } catch (IndexNotFoundException e) {
        try {
            SetupAction.createIndex(client);
        } catch (ElasticsearchSecurityException se) {
            throw new ForbiddenException("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup");
        }
        return request.get();
    }
}