App Engine Java SDK version 1.9.8
[gae.git] / java / src / main / com / google / appengine / api / taskqueue / QueueStatistics.java
blobc34eaee6b7fd8cc6bd963158a6978f89da0ee84f
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;
13 /**
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;
28 /**
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();
37 } else {
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();
48 } else {
49 throw new IllegalArgumentException("Queue statistics not supplied");
51 if (scannerInfo.hasEnforcedRate()) {
52 this.enforcedRate = scannerInfo.getEnforcedRate();
53 } else {
54 throw new IllegalArgumentException("Queue statistics not supplied");
56 } else {
57 throw new IllegalArgumentException("Queue statistics not supplied");
61 /**
62 * @return The name of the {@link Queue}.
64 public String getQueueName() {
65 return queueName;
68 /**
69 * @return The approximate number of non-completed tasks in the queue.
71 public int getNumTasks() {
72 return numTasks;
75 /**
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() {
81 return oldestEtaUsec;
84 /**
85 * @return The number of tasks executed in the last minute.
87 public long getExecutedLastMinute() {
88 return executedLastMinute;
91 /**
92 * @return The number of tasks executed in the last hour.
94 long getExecutedLastHour() {
95 return executedLastHour;
98 /**
99 * The number of requests that the queue has sent but not yet received a
100 * reply for.
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() {
112 return enforcedRate;
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) {
135 @Override
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));
158 return resultList;