Search in sources :

Example 1 with Cancellable

use of org.elasticsearch.client.Cancellable in project openk9 by smclab.

the class IndexWriterEndpoins method _insertEntity.

private Publisher<Void> _insertEntity(HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
    RestHighLevelClient restHighLevelClient = _restHighLevelClientProvider.get();
    Mono<List<DocumentEntityRequest>> request = Mono.from(ReactorNettyUtils.aggregateBodyAsByteArray(httpRequest)).map(json -> _jsonFactory.fromJsonList(json, DocumentEntityRequest.class));
    Mono<BulkResponse> elasticResponse = request.flatMapIterable(Function.identity()).map(entity -> {
        IndexRequest indexRequest = new IndexRequest(entity.getTenantId() + "-entity");
        return indexRequest.source(_jsonFactory.toJson(entity), XContentType.JSON);
    }).reduce(new BulkRequest(), BulkRequest::add).flatMap(bulkRequest -> Mono.create(sink -> {
        bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        Cancellable cancellable = restHighLevelClient.bulkAsync(bulkRequest, RequestOptions.DEFAULT, new ReactorActionListener<>(sink));
        sink.onCancel(cancellable::cancel);
    }));
    return _httpResponseWriter.write(httpResponse, elasticResponse.thenReturn("{}"));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) JsonFactory(io.openk9.json.api.JsonFactory) DocumentEntityRequest(io.openk9.index.writer.entity.model.DocumentEntityRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ReactorNettyUtils(io.openk9.reactor.netty.util.ReactorNettyUtils) IndexRequest(org.elasticsearch.action.index.IndexRequest) EventBus(io.vertx.core.eventbus.EventBus) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) PluginDriverManagerClient(io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) CountRequest(org.elasticsearch.client.core.CountRequest) TimeValue(org.elasticsearch.core.TimeValue) SearchHit(org.elasticsearch.search.SearchHit) ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Message(io.vertx.core.eventbus.Message) DatasourceClient(io.openk9.datasource.client.api.DatasourceClient) Scroll(org.elasticsearch.search.Scroll) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) HttpServerRequest(reactor.netty.http.server.HttpServerRequest) List(java.util.List) QueryBuilders.matchQuery(org.elasticsearch.index.query.QueryBuilders.matchQuery) Builder(lombok.Builder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RestHighLevelClientProvider(io.openk9.search.client.api.RestHighLevelClientProvider) HttpServerRoutes(reactor.netty.http.server.HttpServerRoutes) HttpServerResponse(reactor.netty.http.server.HttpServerResponse) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) XContentType(org.elasticsearch.common.xcontent.XContentType) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) HashMap(java.util.HashMap) SearchRequest(org.elasticsearch.action.search.SearchRequest) HttpResponseWriter(io.openk9.http.util.HttpResponseWriter) Function(java.util.function.Function) ClearScrollResponse(org.elasticsearch.action.search.ClearScrollResponse) CountResponse(org.elasticsearch.client.core.CountResponse) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WriteRequest(org.elasticsearch.action.support.WriteRequest) Component(org.osgi.service.component.annotations.Component) Operator(org.elasticsearch.index.query.Operator) Cancellable(org.elasticsearch.client.Cancellable) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Activate(org.osgi.service.component.annotations.Activate) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) RouterHandler(io.openk9.http.web.RouterHandler) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) Datasource(io.openk9.model.Datasource) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Data(lombok.Data) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) AllArgsConstructor(lombok.AllArgsConstructor) Reference(org.osgi.service.component.annotations.Reference) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) NoArgsConstructor(lombok.NoArgsConstructor) ReactorActionListener(io.openk9.search.client.api.ReactorActionListener) DocumentEntityRequest(io.openk9.index.writer.entity.model.DocumentEntityRequest) Cancellable(org.elasticsearch.client.Cancellable) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) List(java.util.List) ArrayList(java.util.ArrayList) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 2 with Cancellable

use of org.elasticsearch.client.Cancellable in project apm-agent-java by elastic.

the class ElasticsearchRestClientInstrumentationIT method testCancelScenario.

@Test
public void testCancelScenario() throws InterruptedException, ExecutionException, IOException {
    // When spans are cancelled, we can't know the actual address, because there is no response, and we set the outcome as UNKNOWN
    reporter.disableCheckDestinationAddress();
    reporter.disableCheckUnknownOutcome();
    disableHttpUrlCheck();
    createDocument();
    reporter.reset();
    SearchRequest searchRequest = defaultSearchRequest();
    Cancellable cancellable = client.searchAsync(searchRequest, RequestOptions.DEFAULT, new ActionListener<SearchResponse>() {

        @Override
        public void onResponse(SearchResponse searchResponse) {
            throw new IllegalStateException("This should not be called, ofFailure should be called by cancel first");
        }

        @Override
        public void onFailure(Exception e) {
        // nothing to do - we wrap this listener and synchronously end the span
        }
    });
    // This ends the span synchronously
    cancellable.cancel();
    Span searchSpan = reporter.getFirstSpan(500);
    validateSpanContent(searchSpan, String.format("Elasticsearch: POST /%s/_search", INDEX), -1, "POST");
    assertThat(searchSpan.getOutcome()).describedAs("span outcome should be unknown when cancelled").isEqualTo(Outcome.UNKNOWN);
    deleteDocument();
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Cancellable(org.elasticsearch.client.Cancellable) Span(co.elastic.apm.agent.impl.transaction.Span) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) AbstractEs6_4ClientInstrumentationTest(co.elastic.apm.agent.esrestclient.v6_4.AbstractEs6_4ClientInstrumentationTest) Test(org.junit.Test)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 Cancellable (org.elasticsearch.client.Cancellable)2 AbstractEs6_4ClientInstrumentationTest (co.elastic.apm.agent.esrestclient.v6_4.AbstractEs6_4ClientInstrumentationTest)1 Span (co.elastic.apm.agent.impl.transaction.Span)1 DatasourceClient (io.openk9.datasource.client.api.DatasourceClient)1 HttpResponseWriter (io.openk9.http.util.HttpResponseWriter)1 RouterHandler (io.openk9.http.web.RouterHandler)1 DocumentEntityRequest (io.openk9.index.writer.entity.model.DocumentEntityRequest)1 JsonFactory (io.openk9.json.api.JsonFactory)1 Datasource (io.openk9.model.Datasource)1 PluginDriverManagerClient (io.openk9.plugin.driver.manager.client.api.PluginDriverManagerClient)1 ReactorNettyUtils (io.openk9.reactor.netty.util.ReactorNettyUtils)1 ReactorActionListener (io.openk9.search.client.api.ReactorActionListener)1 RestHighLevelClientProvider (io.openk9.search.client.api.RestHighLevelClientProvider)1 EventBus (io.vertx.core.eventbus.EventBus)1 Message (io.vertx.core.eventbus.Message)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1