1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
31 * Mechanism for monitoring and controlling delivery of files to a destination.
33 * The DeliveryQueue class maintains lists of DeliveryTasks for a single
34 * destination (a subscription or another data router node) and assigns
35 * delivery threads to try to deliver them. It also maintains a delivery
36 * status that causes it to back off on delivery attempts after a failure.
38 * If the most recent delivery result was a failure, then no more attempts
39 * will be made for a period of time. Initially, and on the first failure
40 * following a success, this delay will be DeliveryQueueHelper.getInitFailureTimer() (milliseconds).
41 * If, after this delay, additional failures occur, each failure will
42 * multiply the delay by DeliveryQueueHelper.getFailureBackoff() up to a
43 * maximum delay specified by DeliveryQueueHelper.getMaxFailureTimer().
44 * Note that this behavior applies to the delivery queue as a whole and not
45 * to individual files in the queue. If multiple files are being
46 * delivered and one fails, the delay will be started. If a second
47 * delivery fails while the delay was active, it will not change the delay
48 * or change the duration of any subsequent delay.
49 * If, however, it succeeds, it will cancel the delay.
51 * The queue maintains 3 collections of files to deliver: A todo list of
52 * files that will be attempted, a working set of files that are being
53 * attempted, and a retry set of files that were attempted and failed.
54 * Whenever the todo list is empty and needs to be refilled, a scan of the
55 * spool directory is made and the file names sorted. Any files in the working set are ignored.
56 * If a DeliveryTask for the file is in the retry set, then that delivery
57 * task is placed on the todo list. Otherwise, a new DeliveryTask for the
58 * file is created and placed on the todo list.
59 * If, when a DeliveryTask is about to be removed from the todo list, its
60 * age exceeds DeliveryQueueHelper.getExpirationTimer(), then it is instead
63 * A delivery queue also maintains a skip flag. This flag is true if the
64 * failure timer is active or if no files are found in a directory scan.
66 public class DeliveryQueue implements Runnable, DeliveryTaskHelper {
67 private DeliveryQueueHelper deliveryQueueHelper;
68 private DestInfo destinationInfo;
69 private Hashtable<String, DeliveryTask> working = new Hashtable<>();
70 private Hashtable<String, DeliveryTask> retry = new Hashtable<>();
71 private int todoindex;
72 private boolean failed;
73 private long failduration;
74 private long resumetime;
76 private Vector<DeliveryTask> todo = new Vector<>();
79 * Try to cancel a delivery task.
81 * @return The length of the task in bytes or 0 if the task cannot be cancelled.
83 public synchronized long cancelTask(String pubid) {
84 if (working.get(pubid) != null) {
87 DeliveryTask dt = retry.get(pubid);
89 for (int i = todoindex; i < todo.size(); i++) {
90 DeliveryTask xdt = todo.get(i);
91 if (xdt.getPublishId().equals(pubid)) {
98 dt = new DeliveryTask(this, pubid);
99 if (dt.getFileId() == null) {
103 if (dt.isCleaned()) {
106 StatusLog.logExp(dt.getPublishId(), dt.getFeedId(), dt.getSubId(), dt.getURL(), dt.getMethod(), dt.getCType(), dt.getLength(), "diskFull", dt.getAttempts());
108 return (dt.getLength());
112 * Mark that a delivery task has succeeded.
114 public synchronized void markSuccess(DeliveryTask task) {
115 working.remove(task.getPublishId());
122 * Mark that a delivery task has expired.
124 public synchronized void markExpired(DeliveryTask task) {
129 * Mark that a delivery task has failed permanently.
131 public synchronized void markFailNoRetry(DeliveryTask task) {
132 working.remove(task.getPublishId());
138 private void fdupdate() {
141 if (failduration == 0) {
142 if (destinationInfo.isPrivilegedSubscriber()) {
143 failduration = deliveryQueueHelper.getWaitForFileProcessFailureTimer();
145 failduration = deliveryQueueHelper.getInitFailureTimer();
148 resumetime = System.currentTimeMillis() + failduration;
149 long maxdur = deliveryQueueHelper.getMaxFailureTimer();
150 failduration = (long) (failduration * deliveryQueueHelper.getFailureBackoff());
151 if (failduration > maxdur) {
152 failduration = maxdur;
158 * Mark that a delivery task has been redirected.
160 public synchronized void markRedirect(DeliveryTask task) {
161 working.remove(task.getPublishId());
162 retry.put(task.getPublishId(), task);
166 * Mark that a delivery task has temporarily failed.
168 public synchronized void markFailWithRetry(DeliveryTask task) {
169 working.remove(task.getPublishId());
170 retry.put(task.getPublishId(), task);
177 public synchronized DeliveryTask getNext() {
178 DeliveryTask ret = peekNext();
181 working.put(ret.getPublishId(), ret);
187 * Peek at the next task.
189 public synchronized DeliveryTask peekNext() {
190 long now = System.currentTimeMillis();
191 long mindate = now - deliveryQueueHelper.getExpirationTimer();
193 if (now > resumetime) {
200 if (todoindex >= todo.size()) {
202 todo = new Vector<DeliveryTask>();
203 String[] files = dir.list();
205 for (String fname : files) {
206 if (!fname.endsWith(".M")) {
209 String fname2 = fname.substring(0, fname.length() - 2);
211 int dot = fname2.indexOf('.');
216 pidtime = Long.parseLong(fname2.substring(0, dot));
217 } catch (Exception e) {
219 if (pidtime < 1000000000000L) {
222 if (working.get(fname2) != null) {
225 DeliveryTask dt = retry.get(fname2);
227 dt = new DeliveryTask(this, fname2);
231 retry = new Hashtable<String, DeliveryTask>();
233 if (todoindex < todo.size()) {
234 DeliveryTask dt = todo.get(todoindex);
235 if (dt.isCleaned()) {
239 if (dt.getDate() >= mindate) {
251 * Create a delivery queue for a given destination info
253 public DeliveryQueue(DeliveryQueueHelper deliveryQueueHelper, DestInfo destinationInfo) {
254 this.deliveryQueueHelper = deliveryQueueHelper;
255 this.destinationInfo = destinationInfo;
256 dir = new File(destinationInfo.getSpool());
261 * Update the destination info for this delivery queue
263 public void config(DestInfo destinationInfo) {
264 this.destinationInfo = destinationInfo;
270 public DestInfo getDestinationInfo() {
271 return (destinationInfo);
275 * Get the config manager
277 public DeliveryQueueHelper getConfig() {
278 return (deliveryQueueHelper);
282 * Exceptional condition occurred during delivery
284 public void reportDeliveryExtra(DeliveryTask task, long sent) {
285 StatusLog.logDelExtra(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getLength(), sent);
289 * Message too old to deliver
291 public void reportExpiry(DeliveryTask task) {
292 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "retriesExhausted", task.getAttempts());
297 * Completed a delivery attempt
299 public void reportStatus(DeliveryTask task, int status, String xpubid, String location) {
301 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, xpubid);
302 if (destinationInfo.isPrivilegedSubscriber()) {
303 markFailWithRetry(task);
307 } else if (status < 400 && deliveryQueueHelper.isFollowRedirects()) {
308 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
309 if (deliveryQueueHelper.handleRedirection(destinationInfo, location, task.getFileId())) {
312 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "notRetryable", task.getAttempts());
313 markFailNoRetry(task);
315 } else if (status < 500 && status != 429) { // Status 429 is the standard response for Too Many Requests and indicates that a file needs to be delivered again at a later time.
316 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
317 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "notRetryable", task.getAttempts());
318 markFailNoRetry(task);
320 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
321 markFailWithRetry(task);
326 * Delivery failed by reason of an exception
328 public void reportException(DeliveryTask task, Exception exception) {
329 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), -1, exception.toString());
330 deliveryQueueHelper.handleUnreachable(destinationInfo);
331 markFailWithRetry(task);
335 * Get the feed ID for a subscription
337 * @param subid The subscription ID
338 * @return The feed ID
340 public String getFeedId(String subid) {
341 return (deliveryQueueHelper.getFeedId(subid));
345 * Get the URL to deliver a message to given the file ID
347 public String getDestURL(String fileid) {
348 return (deliveryQueueHelper.getDestURL(destinationInfo, fileid));
352 * Deliver files until there's a failure or there are no more
357 long endtime = System.currentTimeMillis() + deliveryQueueHelper.getFairTimeLimit();
358 int filestogo = deliveryQueueHelper.getFairFileLimit();
359 while ((t = getNext()) != null) {
361 if (--filestogo <= 0 || System.currentTimeMillis() > endtime) {
368 * Is there no work to do for this queue right now?
370 public synchronized boolean isSkipSet() {
371 return (peekNext() == null);
375 * Reset the retry timer
377 public void resetQueue() {
378 resumetime = System.currentTimeMillis();
382 * Get task if in queue and mark as success
384 public boolean markTaskSuccess(String pubId) {
385 DeliveryTask task = working.get(pubId);
390 task = retry.get(pubId);