1 // Copyright 2012 Google Inc. All rights reserved.
2 package com
.google
.appengine
.api
.taskqueue
;
4 import com
.google
.appengine
.api
.taskqueue
.TaskQueuePb
.TaskQueueFetchQueueStatsRequest
;
5 import com
.google
.appengine
.api
.taskqueue
.TaskQueuePb
.TaskQueueFetchQueueStatsResponse
;
6 import com
.google
.appengine
.api
.taskqueue
.TaskQueuePb
.TaskQueueScannerQueueInfo
;
7 import com
.google
.apphosting
.api
.ApiProxy
.ApiConfig
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
11 import java
.util
.concurrent
.Future
;
14 * {@link QueueStatistics} allow observation of the rate that tasks on a given
15 * queue are being executed. Note that statistics provided are only approximate,
16 * and some statistics may be delayed or transiently unavailable.
19 public final class QueueStatistics
{
20 private final String queueName
;
21 private final int numTasks
;
22 private final Long oldestEtaUsec
;
23 private final long executedLastMinute
;
24 private final long executedLastHour
;
25 private final int requestsInFlight
;
26 private final double enforcedRate
;
29 * Constructs QueueStatistics data object.
30 * @throws IllegalArgumentException Complete queue statistics were not provided.
32 QueueStatistics(String queueName
, TaskQueueFetchQueueStatsResponse
.QueueStats stats
) {
33 this.queueName
= queueName
;
34 this.numTasks
= stats
.getNumTasks();
35 if (stats
.getOldestEtaUsec() != -1) {
36 this.oldestEtaUsec
= stats
.getOldestEtaUsec();
38 this.oldestEtaUsec
= null;
41 if (stats
.hasScannerInfo()) {
42 TaskQueueScannerQueueInfo scannerInfo
= stats
.getScannerInfo();
44 this.executedLastMinute
= scannerInfo
.getExecutedLastMinute();
45 this.executedLastHour
= scannerInfo
.getExecutedLastHour();
46 if (scannerInfo
.hasRequestsInFlight()) {
47 this.requestsInFlight
= scannerInfo
.getRequestsInFlight();
49 throw new IllegalArgumentException("Queue statistics not supplied");
51 if (scannerInfo
.hasEnforcedRate()) {
52 this.enforcedRate
= scannerInfo
.getEnforcedRate();
54 throw new IllegalArgumentException("Queue statistics not supplied");
57 throw new IllegalArgumentException("Queue statistics not supplied");
62 * @return The name of the {@link Queue}.
64 public String
getQueueName() {
69 * @return The approximate number of non-completed tasks in the queue.
71 public int getNumTasks() {
76 * Returns a recent estimate of the eta of the oldest non-completed task in the queue.
77 * @return The eta of the oldest non-completed task for the queue, or
78 * {@code null} if there were no non-completed tasks found in the queue.
80 public Long
getOldestEtaUsec() {
85 * @return The number of tasks executed in the last minute.
87 public long getExecutedLastMinute() {
88 return executedLastMinute
;
92 * @return The number of tasks executed in the last hour.
94 long getExecutedLastHour() {
95 return executedLastHour
;
99 * The number of requests that the queue has sent but not yet received a
101 * @return The number of tasks currently in flight.
103 public int getRequestsInFlight() {
104 return requestsInFlight
;
108 * Returns the maximum number of tasks per second being run by this queue.
109 * @return The enforced rate in tasks per second.
111 public double getEnforcedRate() {
116 * See {@link Queue#fetchStatistics()}.
118 static Future
<List
<QueueStatistics
>> fetchForQueuesAsync(
119 final List
<Queue
> queues
, QueueApiHelper helper
, double deadlineInSeconds
) {
120 TaskQueueFetchQueueStatsRequest statsRequest
= new TaskQueueFetchQueueStatsRequest();
122 for (Queue queue
: queues
) {
123 statsRequest
.addQueueName(queue
.getQueueName());
126 statsRequest
.setMaxNumTasks(0);
128 ApiConfig apiConfig
= new ApiConfig();
129 apiConfig
.setDeadlineInSeconds(deadlineInSeconds
);
131 Future
<TaskQueueFetchQueueStatsResponse
> responseFuture
= helper
.makeAsyncCall(
132 "FetchQueueStats", statsRequest
, new TaskQueueFetchQueueStatsResponse(), apiConfig
);
133 return new FutureAdapter
<TaskQueueFetchQueueStatsResponse
,
134 List
<QueueStatistics
>>(responseFuture
) {
136 protected List
<QueueStatistics
> wrap(TaskQueueFetchQueueStatsResponse statsResponse
) {
137 if (statsResponse
.queueStatsSize() != queues
.size()) {
138 throw new QueueFailureException("Unable to obtain queue statistics");
141 List
<QueueStatistics
> resultList
= new ArrayList
<QueueStatistics
>();
142 for (int i
= 0; i
< statsResponse
.queueStatsSize(); ++i
) {
143 TaskQueueFetchQueueStatsResponse
.QueueStats stats
= statsResponse
.getQueueStats(i
);
145 if (!stats
.hasScannerInfo()) {
146 throw new TransientFailureException("Queue statistics temporarily unavailable");
148 TaskQueueScannerQueueInfo scannerInfo
= stats
.getScannerInfo();
149 if (!scannerInfo
.hasRequestsInFlight()) {
150 throw new TransientFailureException("Queue statistics temporarily unavailable");
152 if (!scannerInfo
.hasEnforcedRate()) {
153 throw new TransientFailureException("Queue statistics temporarily unavailable");
156 resultList
.add(new QueueStatistics(queues
.get(i
).getQueueName(), stats
));