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("{}"));
}
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();
}
Aggregations