use of org.elasticsearch.action.ActionResponse in project elasticsearch by elastic.
the class TransportActionFilterChainTests method testTooManyContinueProcessingRequest.
public void testTooManyContinueProcessingRequest() throws ExecutionException, InterruptedException {
final int additionalContinueCount = randomInt(10);
RequestTestFilter testFilter = new RequestTestFilter(randomInt(), new RequestCallback() {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void execute(Task task, String action, Request request, ActionListener<Response> listener, ActionFilterChain<Request, Response> actionFilterChain) {
for (int i = 0; i <= additionalContinueCount; i++) {
actionFilterChain.proceed(task, action, request, listener);
}
}
});
Set<ActionFilter> filters = new HashSet<>();
filters.add(testFilter);
String actionName = randomAsciiOfLength(randomInt(30));
ActionFilters actionFilters = new ActionFilters(filters);
TransportAction<TestRequest, TestResponse> transportAction = new TransportAction<TestRequest, TestResponse>(Settings.EMPTY, actionName, null, actionFilters, null, new TaskManager(Settings.EMPTY)) {
@Override
protected void doExecute(TestRequest request, ActionListener<TestResponse> listener) {
listener.onResponse(new TestResponse());
}
};
final CountDownLatch latch = new CountDownLatch(additionalContinueCount + 1);
final AtomicInteger responses = new AtomicInteger();
final List<Throwable> failures = new CopyOnWriteArrayList<>();
transportAction.execute(new TestRequest(), new ActionListener<TestResponse>() {
@Override
public void onResponse(TestResponse testResponse) {
responses.incrementAndGet();
latch.countDown();
}
@Override
public void onFailure(Exception e) {
failures.add(e);
latch.countDown();
}
});
if (!latch.await(10, TimeUnit.SECONDS)) {
fail("timeout waiting for the filter to notify the listener as many times as expected");
}
assertThat(testFilter.runs.get(), equalTo(1));
assertThat(testFilter.lastActionName, equalTo(actionName));
assertThat(responses.get(), equalTo(1));
assertThat(failures.size(), equalTo(additionalContinueCount));
for (Throwable failure : failures) {
assertThat(failure, instanceOf(IllegalStateException.class));
}
}
use of org.elasticsearch.action.ActionResponse in project elasticsearch by elastic.
the class ParentTaskAssigningClientTests method testSetsParentId.
public void testSetsParentId() {
TaskId[] parentTaskId = new TaskId[] { new TaskId(randomAsciiOfLength(3), randomLong()) };
// This mock will do nothing but verify that parentTaskId is set on all requests sent to it.
NoOpClient mock = new NoOpClient(getTestName()) {
@Override
protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
assertEquals(parentTaskId[0], request.getParentTask());
super.doExecute(action, request, listener);
}
};
try (ParentTaskAssigningClient client = new ParentTaskAssigningClient(mock, parentTaskId[0])) {
// All of these should have the parentTaskId set
client.bulk(new BulkRequest());
client.search(new SearchRequest());
client.clearScroll(new ClearScrollRequest());
// Now lets verify that unwrapped calls don't have the parentTaskId set
parentTaskId[0] = TaskId.EMPTY_TASK_ID;
client.unwrap().bulk(new BulkRequest());
client.unwrap().search(new SearchRequest());
client.unwrap().clearScroll(new ClearScrollRequest());
}
}
use of org.elasticsearch.action.ActionResponse in project samza by apache.
the class ElasticsearchSystemProducer method register.
@Override
public void register(final String source) {
BulkProcessor.Listener listener = new BulkProcessor.Listener() {
@Override
public void beforeBulk(long executionId, BulkRequest request) {
// Nothing to do.
}
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
boolean hasFatalError = false;
//Do not consider version conficts to be errors. Ignore old versions
if (response.hasFailures()) {
for (BulkItemResponse itemResp : response.getItems()) {
if (itemResp.isFailed()) {
if (itemResp.getFailure().getStatus().equals(RestStatus.CONFLICT)) {
LOGGER.info("Failed to index document in Elasticsearch: " + itemResp.getFailureMessage());
} else {
hasFatalError = true;
LOGGER.error("Failed to index document in Elasticsearch: " + itemResp.getFailureMessage());
}
}
}
}
if (hasFatalError) {
sendFailed.set(true);
} else {
updateSuccessMetrics(response);
}
}
@Override
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
LOGGER.error(failure.getMessage());
thrown.compareAndSet(null, failure);
sendFailed.set(true);
}
private void updateSuccessMetrics(BulkResponse response) {
metrics.bulkSendSuccess.inc();
int writes = 0;
for (BulkItemResponse itemResp : response.getItems()) {
if (itemResp.isFailed()) {
if (itemResp.getFailure().getStatus().equals(RestStatus.CONFLICT)) {
metrics.conflicts.inc();
}
} else {
ActionResponse resp = itemResp.getResponse();
if (resp instanceof IndexResponse) {
writes += 1;
if (((IndexResponse) resp).isCreated()) {
metrics.inserts.inc();
} else {
metrics.updates.inc();
}
} else {
LOGGER.error("Unexpected Elasticsearch action response type: " + resp.getClass().getSimpleName());
}
}
}
LOGGER.info(String.format("Wrote %s messages from %s to %s.", writes, source, system));
}
};
sourceBulkProcessor.put(source, bulkProcessorFactory.getBulkProcessor(client, listener));
}
Aggregations