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;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
31 import org.jetbrains.annotations.Nullable;
34 * Mechanism for monitoring and controlling delivery of files to a destination.
36 * The DeliveryQueue class maintains lists of DeliveryTasks for a single
37 * destination (a subscription or another data router node) and assigns
38 * delivery threads to try to deliver them. It also maintains a delivery
39 * status that causes it to back off on delivery attempts after a failure.
41 * If the most recent delivery result was a failure, then no more attempts
42 * will be made for a period of time. Initially, and on the first failure
43 * following a success, this delay will be DeliveryQueueHelper.getInitFailureTimer() (milliseconds).
44 * If, after this delay, additional failures occur, each failure will
45 * multiply the delay by DeliveryQueueHelper.getFailureBackoff() up to a
46 * maximum delay specified by DeliveryQueueHelper.getMaxFailureTimer().
47 * Note that this behavior applies to the delivery queue as a whole and not
48 * to individual files in the queue. If multiple files are being
49 * delivered and one fails, the delay will be started. If a second
50 * delivery fails while the delay was active, it will not change the delay
51 * or change the duration of any subsequent delay.
52 * If, however, it succeeds, it will cancel the delay.
54 * The queue maintains 3 collections of files to deliver: A todo list of
55 * files that will be attempted, a working set of files that are being
56 * attempted, and a retry set of files that were attempted and failed.
57 * Whenever the todo list is empty and needs to be refilled, a scan of the
58 * spool directory is made and the file names sorted. Any files in the working set are ignored.
59 * If a DeliveryTask for the file is in the retry set, then that delivery
60 * task is placed on the todo list. Otherwise, a new DeliveryTask for the
61 * file is created and placed on the todo list.
62 * If, when a DeliveryTask is about to be removed from the todo list, its
63 * age exceeds DeliveryQueueHelper.getExpirationTimer(), then it is instead
66 * A delivery queue also maintains a skip flag. This flag is true if the
67 * failure timer is active or if no files are found in a directory scan.
69 public class DeliveryQueue implements Runnable, DeliveryTaskHelper {
70 private static EELFLogger logger = EELFManager.getInstance().getLogger(DeliveryQueue.class);
71 private DeliveryQueueHelper deliveryQueueHelper;
72 private DestInfo destinationInfo;
73 private Hashtable<String, DeliveryTask> working = new Hashtable<>();
74 private Hashtable<String, DeliveryTask> retry = new Hashtable<>();
75 private int todoindex;
76 private boolean failed;
77 private long failduration;
78 private long resumetime;
80 private Vector<DeliveryTask> todo = new Vector<>();
83 * Try to cancel a delivery task.
85 * @return The length of the task in bytes or 0 if the task cannot be cancelled.
87 synchronized long cancelTask(String pubid) {
88 if (working.get(pubid) != null) {
91 DeliveryTask dt = retry.get(pubid);
93 for (int i = todoindex; i < todo.size(); i++) {
94 DeliveryTask xdt = todo.get(i);
95 if (xdt.getPublishId().equals(pubid)) {
102 dt = new DeliveryTask(this, pubid);
103 if (dt.getFileId() == null) {
107 if (dt.isCleaned()) {
110 StatusLog.logExp(dt.getPublishId(), dt.getFeedId(), dt.getSubId(), dt.getURL(), dt.getMethod(), dt.getCType(), dt.getLength(), "diskFull", dt.getAttempts());
112 return (dt.getLength());
116 * Mark that a delivery task has succeeded.
118 private synchronized void markSuccess(DeliveryTask task) {
119 working.remove(task.getPublishId());
120 logger.debug(task.getPublishId() + " marked as success.");
127 * Mark that a delivery task has expired.
129 private synchronized void markExpired(DeliveryTask task) {
130 logger.debug(task.getPublishId() + " marked as expired.");
135 * Mark that a delivery task has failed permanently.
137 private synchronized void markFailNoRetry(DeliveryTask task) {
138 working.remove(task.getPublishId());
139 logger.debug(task.getPublishId() + " marked as failed permanently");
145 private void fdupdate() {
148 if (failduration == 0) {
149 if (destinationInfo.isPrivilegedSubscriber()) {
150 failduration = deliveryQueueHelper.getWaitForFileProcessFailureTimer();
152 failduration = deliveryQueueHelper.getInitFailureTimer();
155 resumetime = System.currentTimeMillis() + failduration;
156 long maxdur = deliveryQueueHelper.getMaxFailureTimer();
157 failduration = (long) (failduration * deliveryQueueHelper.getFailureBackoff());
158 if (failduration > maxdur) {
159 failduration = maxdur;
165 * Mark that a delivery task has been redirected.
167 private synchronized void markRedirect(DeliveryTask task) {
168 working.remove(task.getPublishId());
169 logger.debug(task.getPublishId() + " marked as redirected.");
170 retry.put(task.getPublishId(), task);
174 * Mark that a delivery task has temporarily failed.
176 private synchronized void markFailWithRetry(DeliveryTask task) {
177 working.remove(task.getPublishId());
178 logger.debug(task.getPublishId() + " marked as temporarily failed.");
179 retry.put(task.getPublishId(), task);
186 synchronized DeliveryTask getNext() {
187 DeliveryTask ret = peekNext();
190 working.put(ret.getPublishId(), ret);
196 * Peek at the next task.
198 synchronized DeliveryTask peekNext() {
199 long now = System.currentTimeMillis();
200 long mindate = now - deliveryQueueHelper.getExpirationTimer();
202 if (now > resumetime) {
209 if (todoindex >= todo.size()) {
211 todo = new Vector<>();
212 String[] files = dir.list();
214 scanForNextTask(files);
215 retry = new Hashtable<>();
217 DeliveryTask dt = getDeliveryTask(mindate);
227 * Create a delivery queue for a given destination info
229 DeliveryQueue(DeliveryQueueHelper deliveryQueueHelper, DestInfo destinationInfo) {
230 this.deliveryQueueHelper = deliveryQueueHelper;
231 this.destinationInfo = destinationInfo;
232 dir = new File(destinationInfo.getSpool());
237 * Update the destination info for this delivery queue
239 public void config(DestInfo destinationInfo) {
240 this.destinationInfo = destinationInfo;
246 public DestInfo getDestinationInfo() {
247 return (destinationInfo);
251 * Get the config manager
253 public DeliveryQueueHelper getConfig() {
254 return (deliveryQueueHelper);
258 * Exceptional condition occurred during delivery
260 public void reportDeliveryExtra(DeliveryTask task, long sent) {
261 StatusLog.logDelExtra(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getLength(), sent);
265 * Message too old to deliver
267 void reportExpiry(DeliveryTask task) {
268 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "retriesExhausted", task.getAttempts());
273 * Completed a delivery attempt
275 public void reportStatus(DeliveryTask task, int status, String xpubid, String location) {
277 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, xpubid);
278 if (destinationInfo.isPrivilegedSubscriber()) {
279 task.setResumeTime(System.currentTimeMillis() + deliveryQueueHelper.getWaitForFileProcessFailureTimer());
280 markFailWithRetry(task);
284 } else if (status < 400 && deliveryQueueHelper.isFollowRedirects()) {
285 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
286 if (deliveryQueueHelper.handleRedirection(destinationInfo, location, task.getFileId())) {
289 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "notRetryable", task.getAttempts());
290 markFailNoRetry(task);
292 } 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.
293 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
294 StatusLog.logExp(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), "notRetryable", task.getAttempts());
295 markFailNoRetry(task);
297 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), status, location);
298 markFailWithRetry(task);
303 * Delivery failed by reason of an exception
305 public void reportException(DeliveryTask task, Exception exception) {
306 StatusLog.logDel(task.getPublishId(), task.getFeedId(), task.getSubId(), task.getURL(), task.getMethod(), task.getCType(), task.getLength(), destinationInfo.getAuthUser(), -1, exception.toString());
307 deliveryQueueHelper.handleUnreachable(destinationInfo);
308 markFailWithRetry(task);
312 * Get the feed ID for a subscription
314 * @param subid The subscription ID
315 * @return The feed ID
317 public String getFeedId(String subid) {
318 return (deliveryQueueHelper.getFeedId(subid));
322 * Get the URL to deliver a message to given the file ID
324 public String getDestURL(String fileid) {
325 return (deliveryQueueHelper.getDestURL(destinationInfo, fileid));
329 * Deliver files until there's a failure or there are no more
334 long endtime = System.currentTimeMillis() + deliveryQueueHelper.getFairTimeLimit();
335 int filestogo = deliveryQueueHelper.getFairFileLimit();
336 while ((task = getNext()) != null) {
337 logger.debug("Processing file: " + task.getPublishId());
339 if (--filestogo <= 0 || System.currentTimeMillis() > endtime) {
346 * Is there no work to do for this queue right now?
348 synchronized boolean isSkipSet() {
349 return (peekNext() == null);
353 * Reset the retry timer
356 resumetime = System.currentTimeMillis();
360 * Get task if in queue and mark as success
362 boolean markTaskSuccess(String pubId) {
363 DeliveryTask task = working.get(pubId);
368 task = retry.get(pubId);
378 private void scanForNextTask(String[] files) {
379 for (String fname : files) {
380 String pubId = getPubId(fname);
384 DeliveryTask dt = retry.get(pubId);
386 dt = new DeliveryTask(this, pubId);
393 private DeliveryTask getDeliveryTask(long mindate) {
394 if (todoindex < todo.size()) {
395 DeliveryTask dt = todo.get(todoindex);
396 if (dt.isCleaned()) {
399 if (destinationInfo.isPrivilegedSubscriber() && dt.getResumeTime() > System.currentTimeMillis()) {
400 retry.put(dt.getPublishId(), dt);
403 if (dt.getDate() >= mindate) {
413 private String getPubId(String fname) {
414 if (!fname.endsWith(".M")) {
417 String fname2 = fname.substring(0, fname.length() - 2);
419 int dot = fname2.indexOf('.');
424 pidtime = Long.parseLong(fname2.substring(0, dot));
425 } catch (Exception e) {
426 logger.error("Exception", e);
428 if (pidtime < 1000000000000L) {
431 if (working.get(fname2) != null) {