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 com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
33 * Main control point for delivering files to destinations.
35 * The Delivery class manages assignment of delivery threads to delivery
36 * queues and creation and destruction of delivery queues as
37 * configuration changes. DeliveryQueues are assigned threads based on a
38 * modified round-robin approach giving priority to queues with more work
39 * as measured by both bytes to deliver and files to deliver and lower
40 * priority to queues that already have delivery threads working.
41 * A delivery thread continues to work for a delivery queue as long as
42 * that queue has more files to deliver.
44 public class Delivery {
45 private static EELFLogger logger = EELFManager.getInstance().getLogger(Delivery.class);
47 private static class DelItem implements Comparable<DelItem> {
51 public int compareTo(DelItem x) {
52 int i = pubid.compareTo(x.pubid);
54 i = spool.compareTo(x.spool);
59 public String getPublishId() {
63 public String getSpool() {
67 public DelItem(String pubid, String spool) {
73 private double fdstart;
74 private double fdstop;
76 private int curthreads;
77 private NodeConfigManager config;
78 private Hashtable<String, DeliveryQueue> dqs = new Hashtable<String, DeliveryQueue>();
79 private DeliveryQueue[] queues = new DeliveryQueue[0];
81 private long nextcheck;
82 private Runnable cmon = new Runnable() {
89 * Constructs a new Delivery system using the specified configuration manager.
91 * @param config The configuration manager for this delivery system.
93 public Delivery(NodeConfigManager config) {
95 config.registerConfigTask(cmon);
99 private void cleardir(String dir) {
100 if (dqs.get(dir) != null) {
103 File fdir = new File(dir);
104 for (File junk : fdir.listFiles()) {
112 private void freeDiskCheck() {
113 File spoolfile = new File(config.getSpoolBase());
114 long tspace = spoolfile.getTotalSpace();
115 long start = (long) (tspace * fdstart);
116 long stop = (long) (tspace * fdstop);
117 long cur = spoolfile.getUsableSpace();
121 Vector<DelItem> cv = new Vector<DelItem>();
122 for (String sdir : dqs.keySet()) {
123 for (String meta : (new File(sdir)).list()) {
124 if (!meta.endsWith(".M") || meta.charAt(0) == '.') {
127 cv.add(new DelItem(meta.substring(0, meta.length() - 2), sdir));
130 DelItem[] items = cv.toArray(new DelItem[cv.size()]);
132 logger.info("NODE0501 Free disk space below red threshold. current=" + cur + " red=" + start + " total=" + tspace);
133 for (DelItem item : items) {
134 long amount = dqs.get(item.getSpool()).cancelTask(item.getPublishId());
135 logger.info("NODE0502 Attempting to discard " + item.getSpool() + "/" + item.getPublishId() + " to free up disk");
139 cur = spoolfile.getUsableSpace();
142 logger.info("NODE0503 Free disk space at or above yellow threshold. current=" + cur + " yellow=" + stop + " total=" + tspace);
147 cur = spoolfile.getUsableSpace();
149 logger.info("NODE0503 Free disk space at or above yellow threshold. current=" + cur + " yellow=" + stop + " total=" + tspace);
152 logger.warn("NODE0504 Unable to recover sufficient disk space to reach green status. current=" + cur + " yellow=" + stop + " total=" + tspace);
155 private void cleardirs() {
156 String basedir = config.getSpoolBase();
157 String nbase = basedir + "/n";
158 for (String nodedir : (new File(nbase)).list()) {
159 if (!nodedir.startsWith(".")) {
160 cleardir(nbase + "/" + nodedir);
163 String sxbase = basedir + "/s";
164 for (String sxdir : (new File(sxbase)).list()) {
165 if (sxdir.startsWith(".")) {
168 File sxf = new File(sxbase + "/" + sxdir);
169 for (String sdir : sxf.list()) {
170 if (!sdir.startsWith(".")) {
171 cleardir(sxbase + "/" + sxdir + "/" + sdir);
174 sxf.delete(); // won't if anything still in it
178 private synchronized void checkconfig() {
179 if (!config.isConfigured()) {
182 fdstart = config.getFreeDiskStart();
183 fdstop = config.getFreeDiskStop();
184 threads = config.getDeliveryThreads();
188 DestInfo[] alldis = config.getAllDests();
189 DeliveryQueue[] nqs = new DeliveryQueue[alldis.length];
191 Hashtable<String, DeliveryQueue> ndqs = new Hashtable<String, DeliveryQueue>();
192 for (DestInfo di : alldis) {
193 String spl = di.getSpool();
194 DeliveryQueue dq = dqs.get(spl);
196 dq = new DeliveryQueue(config, di);
206 while (curthreads < threads) {
210 setName("Delivery Thread");
222 private void dodelivery() {
224 while ((dq = getNextQueue()) != null) {
229 private synchronized DeliveryQueue getNextQueue() {
231 if (curthreads > threads) {
235 if (qpos < queues.length) {
236 DeliveryQueue dq = queues[qpos++];
237 if (dq.isSkipSet()) {
244 long now = System.currentTimeMillis();
245 if (now < nextcheck) {
247 wait(nextcheck + 500 - now);
248 } catch (Exception e) {
250 now = System.currentTimeMillis();
252 if (now >= nextcheck) {
253 nextcheck = now + 5000;
261 * Reset the retry timer for a delivery queue
263 public synchronized void resetQueue(String spool) {
265 DeliveryQueue dq = dqs.get(spool);
273 * Mark the task in spool a success
275 public synchronized boolean markTaskSuccess(String spool, String pubId) {
276 boolean succeeded = false;
278 DeliveryQueue dq = dqs.get(spool);
280 succeeded = dq.markTaskSuccess(pubId);