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();
}
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();
}
Aggregations