Search in sources :

Example 6 with LatencyPercentiles

use of com.google.appengine.tools.development.LatencyPercentiles in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueue method queryAndOwnTasks.

@LatencyPercentiles(latency50th = 8)
public TaskQueueQueryAndOwnTasksResponse queryAndOwnTasks(Status status, TaskQueueQueryAndOwnTasksRequest request) {
    String queueName = request.getQueueName().toStringUtf8();
    validateQueueName(queueName);
    // getQueueByName will throw UNKNOWN_QUEUE if the queue does not exist.
    DevQueue queue = getQueueByName(queueName);
    if (queue.getMode() != Mode.PULL) {
        throw new ApiProxy.ApplicationException(ErrorCode.INVALID_QUEUE_MODE_VALUE);
    }
    DevPullQueue pullQueue = (DevPullQueue) queue;
    List<TaskQueueAddRequest.Builder> results = pullQueue.queryAndOwnTasks(request.getLeaseSeconds(), request.getMaxTasks(), request.hasGroupByTag(), request.getTag().toByteArray());
    TaskQueueQueryAndOwnTasksResponse.Builder response = TaskQueueQueryAndOwnTasksResponse.newBuilder();
    for (TaskQueueAddRequest.Builder task : results) {
        TaskQueueQueryAndOwnTasksResponse.Task.Builder responseTask = response.addTaskBuilder().setTaskName(task.getTaskName()).setBody(task.getBody()).setEtaUsec(task.getEtaUsec());
        if (task.hasTag()) {
            responseTask.setTag(task.getTag());
        }
    // TODO: To keep track of retry count, we can replace TaskQueueAddRequest with
    // TaskQueueQueryTasksResponse to represent a task.
    }
    return response.build();
}
Also used : TaskQueueAddRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest) ByteString(com.google.protobuf.ByteString) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) LatencyPercentiles(com.google.appengine.tools.development.LatencyPercentiles)

Example 7 with LatencyPercentiles

use of com.google.appengine.tools.development.LatencyPercentiles in project appengine-java-standard by GoogleCloudPlatform.

the class LocalURLFetchService method fetch.

@LatencyPercentiles(latency50th = 5)
public URLFetchResponse fetch(Status status, URLFetchRequest request) {
    if (status == null) {
        throw new NullPointerException("status cannot be null.");
    }
    if (request == null) {
        throw new NullPointerException("request cannot be null.");
    }
    if (!hasValidURL(request)) {
        throw new ApplicationException(ErrorCode.INVALID_URL.getNumber(), "Invalid URL: " + request.getUrl());
    }
    MethodFactory methodFactory = METHOD_FACTORY_MAP.get(request.getMethod());
    if (methodFactory == null) {
        throw new ApplicationException(ErrorCode.INVALID_URL.getNumber(), "Unsupported method: " + request.getMethod());
    }
    HttpRequestBase method = methodFactory.buildMethod(request);
    HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, request.getFollowRedirects());
    // TODO set these timeouts according to the RPC deadline.
    // see http://b/1488459 for more info
    // how long we'll wait to establish a connection
    HttpConnectionParams.setConnectionTimeout(params, timeoutInMs);
    // how long we'll let the socket stay open
    HttpConnectionParams.setSoTimeout(params, timeoutInMs);
    method.setParams(params);
    boolean sawContentType = false;
    for (URLFetchRequest.Header pbHeader : request.getHeaderList()) {
        // an exception, and this behavior matches production.
        if (pbHeader.getKey().equalsIgnoreCase("Content-Length")) {
            continue;
        }
        method.addHeader(pbHeader.getKey(), pbHeader.getValue());
        if (pbHeader.getKey().equalsIgnoreCase("Content-Type")) {
            sawContentType = true;
        }
    }
    // TODO: Should we check on PUT/PATCH? What would the default be?
    if (!sawContentType && (request.getMethod() == RequestMethod.POST) && request.hasPayload()) {
        method.addHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    URLFetchResponse.Builder response = URLFetchResponse.newBuilder();
    try {
        HttpResponse httpResponse = doPrivilegedExecute(request, method, response);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        if (responseCode < 100 || responseCode >= 600) {
            // Note, response codes in the range [100, 600) are valid.
            throw new ApplicationException(ErrorCode.FETCH_ERROR.getNumber(), "Status code " + responseCode + " unknown when making " + method.getMethod() + " request to URL: " + request.getUrl());
        }
        HttpEntity responseEntity = httpResponse.getEntity();
        if (responseEntity != null) {
            byte[] responseBuffer = responseToByteArray(responseEntity);
            if (responseBuffer.length > maxResponseLength) {
                responseBuffer = Arrays.copyOf(responseBuffer, maxResponseLength);
                response.setContentWasTruncated(true);
            }
            response.setContent(ByteString.copyFrom(responseBuffer));
        }
        httpclientHeadersToPbHeaders(httpResponse.getAllHeaders(), response);
    } catch (SocketTimeoutException ste) {
        throw new ApplicationException(ErrorCode.DEADLINE_EXCEEDED.getNumber(), "http method " + method.getMethod() + " against URL " + request.getUrl() + " timed out.");
    } catch (SSLException e) {
        throw new ApplicationException(ErrorCode.SSL_CERTIFICATE_ERROR.getNumber(), "Couldn't validate the server's SSL certificate for URL " + request.getUrl() + ": " + e.getMessage());
    } catch (IOException e) {
        if (e.getCause() != null && e.getCause().getMessage().matches("Maximum redirects \\([0-9]+\\) exceeded")) {
            throw new ApplicationException(ErrorCode.TOO_MANY_REDIRECTS.getNumber(), "Received exception executing http method " + method.getMethod() + " against URL " + request.getUrl() + ": " + e.getCause().getMessage());
        } else {
            throw new ApplicationException(ErrorCode.FETCH_ERROR.getNumber(), "Received exception executing http method " + method.getMethod() + " against URL " + request.getUrl() + ": " + e.getMessage());
        }
    }
    return response.build();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) URLFetchResponse(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse) ApplicationException(com.google.apphosting.api.ApiProxy.ApplicationException) SocketTimeoutException(java.net.SocketTimeoutException) BasicHttpParams(org.apache.http.params.BasicHttpParams) URLFetchRequest(com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest) LatencyPercentiles(com.google.appengine.tools.development.LatencyPercentiles)

Aggregations

LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)7 ByteString (com.google.protobuf.ByteString)4 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)2 TaskQueueBulkAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest)2 TaskQueueBulkAddResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse)2 MemcacheDeleteRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteRequest)1 MemcacheDeleteResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteResponse)1 InternalFailureException (com.google.appengine.api.taskqueue.InternalFailureException)1 TaskQueueAddResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddResponse)1 TaskQueueFetchQueueStatsResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueFetchQueueStatsResponse)1 TaskQueuePurgeQueueResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueuePurgeQueueResponse)1 TaskQueueQueryAndOwnTasksResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse)1 TaskQueueScannerQueueInfo (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueScannerQueueInfo)1 ErrorCode (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueServiceError.ErrorCode)1 URLFetchRequest (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchRequest)1 URLFetchResponse (com.google.appengine.api.urlfetch.URLFetchServicePb.URLFetchResponse)1 ApiProxy (com.google.apphosting.api.ApiProxy)1 ApplicationException (com.google.apphosting.api.ApiProxy.ApplicationException)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1