use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest in project appengine-java-standard by GoogleCloudPlatform.
the class QueueImpl method addAsync.
/**
* See {@link Queue#addAsync(com.google.appengine.api.datastore.Transaction, Iterable)}.
*/
@Override
public Future<List<TaskHandle>> addAsync(Transaction txn, Iterable<TaskOptions> taskOptions) {
final List<TaskOptions> taskOptionsList = new ArrayList<>();
Set<String> taskNames = new HashSet<>();
final TaskQueueBulkAddRequest.Builder bulkAddRequest = TaskQueueBulkAddRequest.newBuilder();
boolean hasPushTask = false;
boolean hasPullTask = false;
for (TaskOptions option : taskOptions) {
TaskQueueAddRequest.Builder addRequest = bulkAddRequest.addAddRequestBuilder();
validateAndFillAddRequest(txn, option, addRequest);
if (addRequest.getMode() == TaskQueueMode.Mode.PULL) {
hasPullTask = true;
} else {
hasPushTask = true;
}
taskOptionsList.add(option);
if (option.getTaskName() != null && !option.getTaskName().isEmpty()) {
if (!taskNames.add(option.getTaskName())) {
throw new IllegalArgumentException(String.format("Identical task names in request : \"%s\" duplicated", option.getTaskName()));
}
}
}
if (bulkAddRequest.getAddRequestCount() > QueueConstants.maxTasksPerAdd()) {
throw new IllegalArgumentException(String.format("No more than %d tasks can be added in a single add call", QueueConstants.maxTasksPerAdd()));
}
if (hasPullTask && hasPushTask) {
throw new IllegalArgumentException("May not add both push tasks and pull tasks in the same call.");
}
TaskQueueBulkAddRequest builtRequest = bulkAddRequest.build();
if (txn != null && builtRequest.getSerializedSize() > QueueConstants.maxTransactionalRequestSizeBytes()) {
throw new IllegalArgumentException(String.format("Transactional add may not be larger than %d bytes: %d bytes requested.", QueueConstants.maxTransactionalRequestSizeBytes(), builtRequest.getSerializedSize()));
}
Future<TaskQueueBulkAddResponse> responseFuture = makeAsyncCall("BulkAdd", builtRequest, TaskQueueBulkAddResponse.getDefaultInstance());
return new FutureAdapter<TaskQueueBulkAddResponse, List<TaskHandle>>(responseFuture) {
@Override
protected List<TaskHandle> wrap(TaskQueueBulkAddResponse bulkAddResponse) {
if (bulkAddResponse.getTaskResultCount() != bulkAddRequest.getAddRequestCount()) {
throw new InternalFailureException(String.format("expected %d results from BulkAdd(), got %d", bulkAddRequest.getAddRequestCount(), bulkAddResponse.getTaskResultCount()));
}
List<TaskHandle> tasks = new ArrayList<>();
RuntimeException taskqueueException = null;
for (int i = 0; i < bulkAddResponse.getTaskResultCount(); ++i) {
TaskQueueBulkAddResponse.TaskResult.Builder taskResult = bulkAddResponse.toBuilder().getTaskResultBuilder(i);
TaskQueueAddRequest addRequest = bulkAddRequest.getAddRequest(i);
TaskOptions options = taskOptionsList.get(i);
if (taskResult.getResult() == TaskQueueServiceError.ErrorCode.OK) {
String taskName = options.getTaskName();
if (taskResult.hasChosenTaskName()) {
taskName = taskResult.getChosenTaskName().toStringUtf8();
}
TaskOptions taskResultOptions = new TaskOptions(options);
taskResultOptions.taskName(taskName).payload(addRequest.getBody().toByteArray());
TaskHandle handle = new TaskHandle(taskResultOptions, queueName);
tasks.add(handle.etaUsec(addRequest.getEtaUsec()));
} else if (taskResult.getResult() != TaskQueueServiceError.ErrorCode.SKIPPED) {
// not successfully added.
if (taskqueueException == null || taskqueueException instanceof TaskAlreadyExistsException) {
int result = taskResult.getResult().getNumber();
String detail = (result == TaskQueueServiceError.ErrorCode.UNKNOWN_QUEUE_VALUE) ? queueName : options.getTaskName();
RuntimeException e = QueueApiHelper.translateError(result, detail);
if (e instanceof TaskAlreadyExistsException) {
if (taskqueueException == null) {
taskqueueException = e;
}
TaskAlreadyExistsException taee = (TaskAlreadyExistsException) taskqueueException;
taee.appendTaskName(options.getTaskName());
} else {
taskqueueException = e;
}
}
}
}
if (taskqueueException != null) {
throw taskqueueException;
}
return tasks;
}
};
}
use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest in project appengine-java-standard by GoogleCloudPlatform.
the class LocalTaskQueue method bulkAdd.
/**
* BulkAdd RPC implementation.
*/
@LatencyPercentiles(latency50th = 4)
public TaskQueueBulkAddResponse bulkAdd(Status status, TaskQueueBulkAddRequest bulkAddRequest) {
TaskQueueBulkAddResponse.Builder bulkAddResponse = TaskQueueBulkAddResponse.newBuilder();
if (bulkAddRequest.getAddRequestCount() == 0) {
return bulkAddResponse.build();
}
TaskQueueBulkAddRequest.Builder bulkAddRequestBuilder = bulkAddRequest.toBuilder();
DevQueue queue = getQueueByName(bulkAddRequestBuilder.getAddRequest(0).getQueueName().toStringUtf8());
Map<TaskQueueBulkAddResponse.TaskResult.Builder, String> chosenNames = new IdentityHashMap<>();
boolean errorFound = false;
for (TaskQueueAddRequest.Builder addRequest : bulkAddRequestBuilder.getAddRequestBuilderList()) {
TaskQueueBulkAddResponse.TaskResult.Builder taskResult = bulkAddResponse.addTaskResultBuilder();
ErrorCode error = validateAddRequest(addRequest);
taskResult.setResult(error);
if (error == ErrorCode.OK) {
if (!addRequest.hasTaskName() || addRequest.getTaskName().isEmpty()) {
addRequest.setTaskName(ByteString.copyFromUtf8(DevQueue.genTaskName()));
chosenNames.put(taskResult, addRequest.getTaskName().toStringUtf8());
}
// Initialize the result as SKIPPED - this will be set to the actual result value if the
// request does not contain any errors and proceeds to the AddActions/BulkAdd stage.
taskResult.setResult(ErrorCode.SKIPPED);
} else {
taskResult.setResult(error);
errorFound = true;
}
}
if (errorFound) {
return bulkAddResponse.build();
}
if (bulkAddRequestBuilder.getAddRequest(0).hasTransaction() || bulkAddRequestBuilder.getAddRequest(0).hasDatastoreTransaction()) {
// rather than an apphosting_datastore_v3.AddActionsRequest.
try {
ApiProxy.makeSyncCall("datastore_v3", "addActions", bulkAddRequestBuilder.build().toByteArray());
} catch (ApiProxy.ApplicationException exception) {
throw new ApiProxy.ApplicationException(exception.getApplicationError() + ErrorCode.DATASTORE_ERROR_VALUE, exception.getErrorDetail());
}
} else {
for (int i = 0; i < bulkAddRequestBuilder.getAddRequestCount(); ++i) {
TaskQueueAddRequest.Builder addRequest = bulkAddRequestBuilder.getAddRequestBuilder(i);
TaskQueueBulkAddResponse.TaskResult.Builder taskResult = bulkAddResponse.getTaskResultBuilder(i);
try {
// Validation of task mode will be performed in DevQueue object.
queue.add(addRequest);
} catch (ApiProxy.ApplicationException exception) {
taskResult.setResult(ErrorCode.forNumber(exception.getApplicationError()));
}
}
}
for (TaskQueueBulkAddResponse.TaskResult.Builder taskResult : bulkAddResponse.getTaskResultBuilderList()) {
if (taskResult.getResult() == ErrorCode.SKIPPED) {
taskResult.setResult(ErrorCode.OK);
if (chosenNames.containsKey(taskResult)) {
taskResult.setChosenTaskName(ByteString.copyFromUtf8(chosenNames.get(taskResult)));
}
}
}
return bulkAddResponse.build();
}
Aggregations