Search in sources :

Example 1 with TaskQueueBulkAddResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse 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;
        }
    };
}
Also used : TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) TaskQueueBulkAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest) HashSet(java.util.HashSet)

Example 2 with TaskQueueBulkAddResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueue method add.

@LatencyPercentiles(latency50th = 4)
public TaskQueueAddResponse add(Status status, TaskQueueAddRequest addRequest) {
    TaskQueueBulkAddRequest.Builder bulkRequest = TaskQueueBulkAddRequest.newBuilder();
    bulkRequest.addAddRequestBuilder().mergeFrom(addRequest);
    TaskQueueAddResponse.Builder addResponse = TaskQueueAddResponse.newBuilder();
    TaskQueueBulkAddResponse bulkResponse = bulkAdd(status, bulkRequest.build());
    if (bulkResponse.getTaskResultCount() != 1) {
        throw new InternalFailureException(String.format("expected 1 result from BulkAdd(), got %d", bulkResponse.getTaskResultCount()));
    }
    int result = bulkResponse.getTaskResult(0).getResult().getNumber();
    if (result != ErrorCode.OK_VALUE) {
        throw new ApiProxy.ApplicationException(result);
    } else if (bulkResponse.getTaskResult(0).hasChosenTaskName()) {
        addResponse.setChosenTaskName(bulkResponse.getTaskResult(0).getChosenTaskName());
    }
    return addResponse.build();
}
Also used : InternalFailureException(com.google.appengine.api.taskqueue.InternalFailureException) TaskQueueAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddResponse) TaskQueueBulkAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) LatencyPercentiles(com.google.appengine.tools.development.LatencyPercentiles)

Example 3 with TaskQueueBulkAddResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse 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();
}
Also used : TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) ApiProxy(com.google.apphosting.api.ApiProxy) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) IdentityHashMap(java.util.IdentityHashMap) ByteString(com.google.protobuf.ByteString) TaskQueueBulkAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest) ErrorCode(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueServiceError.ErrorCode) LatencyPercentiles(com.google.appengine.tools.development.LatencyPercentiles)

Example 4 with TaskQueueBulkAddResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testAddExistingTask.

@Test
public void testAddExistingTask() throws Exception {
    TaskQueueBulkAddResponse response = localService.bulkAdd(new Status(), bulkAddRequest.build());
    assertThat(response).isEqualTo(expectedBulkAddResponse.build());
    addRequest1.setTaskName(ByteString.copyFromUtf8("Unique1"));
    expectedTaskResult2.setResult(ErrorCode.TASK_ALREADY_EXISTS);
    addRequest3.setTaskName(ByteString.copyFromUtf8("Unique3"));
    TaskQueueBulkAddResponse existingTaskresponse = localService.bulkAdd(new Status(), bulkAddRequest.build());
    assertThat(existingTaskresponse).isEqualTo(expectedBulkAddResponse.build());
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) Test(org.junit.Test)

Example 5 with TaskQueueBulkAddResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testBulkAddEmptyRequest.

@Test
public void testBulkAddEmptyRequest() throws Exception {
    bulkAddRequest.clear();
    expectedBulkAddResponse.clear();
    TaskQueueBulkAddResponse response = localService.bulkAdd(new Status(), bulkAddRequest.build());
    assertThat(response).isEqualTo(expectedBulkAddResponse.build());
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) Test(org.junit.Test)

Aggregations

TaskQueueBulkAddResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse)21 Status (com.google.appengine.tools.development.LocalRpcService.Status)18 Test (org.junit.Test)16 ByteString (com.google.protobuf.ByteString)5 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)4 TaskQueueBulkAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest)4 TaskQueueQueryAndOwnTasksRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest)4 TaskQueueQueryAndOwnTasksResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse)4 Clock (com.google.appengine.tools.development.Clock)4 QueueXml (com.google.apphosting.utils.config.QueueXml)3 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)2 InternalFailureException (com.google.appengine.api.taskqueue.InternalFailureException)1 TaskQueueAddResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddResponse)1 TaskQueueModifyTaskLeaseRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseRequest)1 TaskQueueModifyTaskLeaseResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseResponse)1 ErrorCode (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueServiceError.ErrorCode)1 ApiProxy (com.google.apphosting.api.ApiProxy)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1