public TaskResult result(DiscoveryNode node, ActionResponse response) throws IOException { if (response instanceof ToXContent) { return new TaskResult(taskInfo(node.getId(), true), (ToXContent) response); } else { throw new IllegalStateException("response has to implement ToXContent to be able to store the results"); } } }
@Override public int hashCode() { /* * Hashing of error and result is done by converting them to a map first. Not efficient but ignores field order and spacing * differences so perfect for testing. */ return Objects.hash(completed, task, getErrorAsMap(), getResponseAsMap()); }
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) { IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString()); try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS); index.setSource(builder); } catch (IOException e) { throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask()); } doStoreResult(STORE_BACKOFF_POLICY.iterator(), index, listener); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); task.innerToXContent(builder, params); builder.endObject(); return builder; }
/** * Construct a {@linkplain TaskResult} for a task that completed with an error. */ public TaskResult(TaskInfo task, Exception error) throws IOException { this(true, task, toXContent(error), null); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); innerToXContent(builder, params); return builder.endObject(); }
/** * Construct a {@linkplain TaskResult} for a task that completed with an error. */ public TaskResult(TaskInfo task, Exception error) throws IOException { this(true, task, toXContent(error), null); }
@Override public boolean equals(Object obj) { if (obj == null || obj.getClass() != TaskResult.class) { return false; } TaskResult other = (TaskResult) obj; /* * Equality of error and result is done by converting them to a map first. Not efficient but ignores field order and spacing * differences so perfect for testing. */ return Objects.equals(completed, other.completed) && Objects.equals(task, other.task) && Objects.equals(getErrorAsMap(), other.getErrorAsMap()) && Objects.equals(getResponseAsMap(), other.getResponseAsMap()); }
/** * Called after waiting for the task to complete. Attempts to load the results of the task from the tasks index. If it isn't in the * index then returns a snapshot of the task taken shortly after completion. */ void waitedForCompletion(Task thisTask, GetTaskRequest request, TaskInfo snapshotOfRunningTask, ActionListener<GetTaskResponse> listener) { getFinishedTaskFromIndex(thisTask, request, new ActionListener<GetTaskResponse>() { @Override public void onResponse(GetTaskResponse response) { // We were able to load the task from the task index. Let's send that back. listener.onResponse(response); } @Override public void onFailure(Exception e) { /* * We couldn't load the task from the task index. Instead of 404 we should use the snapshot we took after it finished. If * the error isn't a 404 then we'll just throw it back to the user. */ if (ExceptionsHelper.unwrap(e, ResourceNotFoundException.class) != null) { listener.onResponse(new GetTaskResponse(new TaskResult(true, snapshotOfRunningTask))); } else { listener.onFailure(e); } } }); }
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) { IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString()); try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS); index.setSource(builder); } catch (IOException e) { throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask()); } index.execute(new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { listener.onResponse(null); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); task.innerToXContent(builder, params); builder.endObject(); return builder; }
/** * Construct a {@linkplain TaskResult} for a task that completed with an error. */ public TaskResult(TaskInfo task, Exception error) throws IOException { this(true, task, toXContent(error), null); }
@Override public int hashCode() { /* * Hashing of error and result is done by converting them to a map first. Not efficient but ignores field order and spacing * differences so perfect for testing. */ return Objects.hash(completed, task, getErrorAsMap(), getResponseAsMap()); }
@Override public void onFailure(Exception e) { /* * We couldn't load the task from the task index. Instead of 404 we should use the snapshot we took after it finished. If * the error isn't a 404 then we'll just throw it back to the user. */ if (ExceptionsHelper.unwrap(e, ResourceNotFoundException.class) != null) { listener.onResponse(new GetTaskResponse(new TaskResult(true, snapshotOfRunningTask))); } else { listener.onFailure(e); } } });
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) { IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString()); try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS); index.setSource(builder); } catch (IOException e) { throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask()); } index.execute(new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { listener.onResponse(null); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); innerToXContent(builder, params); return builder.endObject(); }
/** * Construct a {@linkplain TaskResult} for a task that completed successfully. */ public TaskResult(TaskInfo task, ToXContent response) throws IOException { this(true, task, null, toXContent(response)); }
@Override public int hashCode() { /* * Hashing of error and result is done by converting them to a map first. Not efficient but ignores field order and spacing * differences so perfect for testing. */ return Objects.hash(completed, task, getErrorAsMap(), getResponseAsMap()); }
public TaskResult result(DiscoveryNode node, Exception error) throws IOException { return new TaskResult(taskInfo(node.getId(), true), error); }
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) { IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString()); try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) { taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS); index.setSource(builder); } catch (IOException e) { throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask()); } index.execute(new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { listener.onResponse(null); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); }