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 ******************************************************************************/
24 package org.onap.dmaap.datarouter.node;
29 import org.apache.log4j.Logger;
32 * Main control point for delivering files to destinations.
34 * The Delivery class manages assignment of delivery threads to delivery
35 * queues and creation and destruction of delivery queues as
36 * configuration changes. DeliveryQueues are assigned threads based on a
37 * modified round-robin approach giving priority to queues with more work
38 * as measured by both bytes to deliver and files to deliver and lower
39 * priority to queues that already have delivery threads working.
40 * A delivery thread continues to work for a delivery queue as long as
41 * that queue has more files to deliver.
43 public class Delivery {
44 private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.Delivery");
46 private static class DelItem implements Comparable<DelItem> {
50 public int compareTo(DelItem x) {
51 int i = pubid.compareTo(x.pubid);
53 i = spool.compareTo(x.spool);
58 public String getPublishId() {
62 public String getSpool() {
66 public DelItem(String pubid, String spool) {
72 private double fdstart;
73 private double fdstop;
75 private int curthreads;
76 private NodeConfigManager config;
77 private Hashtable<String, DeliveryQueue> dqs = new Hashtable<String, DeliveryQueue>();
78 private DeliveryQueue[] queues = new DeliveryQueue[0];
80 private long nextcheck;
81 private Runnable cmon = new Runnable() {
88 * Constructs a new Delivery system using the specified configuration manager.
90 * @param config The configuration manager for this delivery system.
92 public Delivery(NodeConfigManager config) {
94 config.registerConfigTask(cmon);
98 private void cleardir(String dir) {
99 if (dqs.get(dir) != null) {
102 File fdir = new File(dir);
103 for (File junk : fdir.listFiles()) {
111 private void freeDiskCheck() {
112 File spoolfile = new File(config.getSpoolBase());
113 long tspace = spoolfile.getTotalSpace();
114 long start = (long) (tspace * fdstart);
115 long stop = (long) (tspace * fdstop);
116 long cur = spoolfile.getUsableSpace();
120 Vector<DelItem> cv = new Vector<DelItem>();
121 for (String sdir : dqs.keySet()) {
122 for (String meta : (new File(sdir)).list()) {
123 if (!meta.endsWith(".M") || meta.charAt(0) == '.') {
126 cv.add(new DelItem(meta.substring(0, meta.length() - 2), sdir));
129 DelItem[] items = cv.toArray(new DelItem[cv.size()]);
131 logger.info("NODE0501 Free disk space below red threshold. current=" + cur + " red=" + start + " total=" + tspace);
132 for (DelItem item : items) {
133 long amount = dqs.get(item.getSpool()).cancelTask(item.getPublishId());
134 logger.info("NODE0502 Attempting to discard " + item.getSpool() + "/" + item.getPublishId() + " to free up disk");
138 cur = spoolfile.getUsableSpace();
141 logger.info("NODE0503 Free disk space at or above yellow threshold. current=" + cur + " yellow=" + stop + " total=" + tspace);
146 cur = spoolfile.getUsableSpace();
148 logger.info("NODE0503 Free disk space at or above yellow threshold. current=" + cur + " yellow=" + stop + " total=" + tspace);
151 logger.warn("NODE0504 Unable to recover sufficient disk space to reach green status. current=" + cur + " yellow=" + stop + " total=" + tspace);
154 private void cleardirs() {
155 String basedir = config.getSpoolBase();
156 String nbase = basedir + "/n";
157 for (String nodedir : (new File(nbase)).list()) {
158 if (!nodedir.startsWith(".")) {
159 cleardir(nbase + "/" + nodedir);
162 String sxbase = basedir + "/s";
163 for (String sxdir : (new File(sxbase)).list()) {
164 if (sxdir.startsWith(".")) {
167 File sxf = new File(sxbase + "/" + sxdir);
168 for (String sdir : sxf.list()) {
169 if (!sdir.startsWith(".")) {
170 cleardir(sxbase + "/" + sxdir + "/" + sdir);
173 sxf.delete(); // won't if anything still in it
177 private synchronized void checkconfig() {
178 if (!config.isConfigured()) {
181 fdstart = config.getFreeDiskStart();
182 fdstop = config.getFreeDiskStop();
183 threads = config.getDeliveryThreads();
187 DestInfo[] alldis = config.getAllDests();
188 DeliveryQueue[] nqs = new DeliveryQueue[alldis.length];
190 Hashtable<String, DeliveryQueue> ndqs = new Hashtable<String, DeliveryQueue>();
191 for (DestInfo di : alldis) {
192 String spl = di.getSpool();
193 DeliveryQueue dq = dqs.get(spl);
195 dq = new DeliveryQueue(config, di);
205 while (curthreads < threads) {
209 setName("Delivery Thread");
221 private void dodelivery() {
223 while ((dq = getNextQueue()) != null) {
228 private synchronized DeliveryQueue getNextQueue() {
230 if (curthreads > threads) {
234 if (qpos < queues.length) {
235 DeliveryQueue dq = queues[qpos++];
236 if (dq.isSkipSet()) {
243 long now = System.currentTimeMillis();
244 if (now < nextcheck) {
246 wait(nextcheck + 500 - now);
247 } catch (Exception e) {
249 now = System.currentTimeMillis();
251 if (now >= nextcheck) {
252 nextcheck = now + 5000;
260 * Reset the retry timer for a delivery queue
262 public synchronized void resetQueue(String spool) {
264 DeliveryQueue dq = dqs.get(spool);