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;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.Objects;
35 * Main control point for delivering files to destinations.
37 * <p>The Delivery class manages assignment of delivery threads to delivery queues and creation and destruction of
38 * delivery queues as configuration changes. DeliveryQueues are assigned threads based on a modified round-robin
39 * approach giving priority to queues with more work as measured by both bytes to deliver and files to deliver and lower
40 * priority to queues that already have delivery threads working. A delivery thread continues to work for a delivery
41 * queue as long as that queue has more files to deliver.
43 public class Delivery {
45 private static final String TOTAL = " total=";
46 private static final String YELLOW = " yellow=";
47 private static EELFLogger logger = EELFManager.getInstance().getLogger(Delivery.class);
48 private double fdstart;
49 private double fdstop;
51 private int curthreads;
52 private NodeConfigManager config;
53 private HashMap<String, DeliveryQueue> dqs = new HashMap<>();
54 private DeliveryQueue[] queues = new DeliveryQueue[0];
56 private long nextcheck;
59 * Constructs a new Delivery system using the specified configuration manager.
61 * @param config The configuration manager for this delivery system.
63 public Delivery(NodeConfigManager config) {
65 Runnable cmon = this::checkconfig;
66 config.registerConfigTask(cmon);
71 * Reset the retry timer for a delivery queue.
73 public synchronized void resetQueue(String spool) {
75 DeliveryQueue dq = dqs.get(spool);
83 * Mark the task in spool a success.
85 public synchronized boolean markTaskSuccess(String spool, String pubId) {
86 boolean succeeded = false;
88 DeliveryQueue dq = dqs.get(spool);
90 succeeded = dq.markTaskSuccess(pubId);
96 private void cleardir(String dir) {
97 if (dqs.get(dir) != null) {
100 File fdir = new File(dir);
101 for (File junk : fdir.listFiles()) {
109 private void freeDiskCheck() {
110 File spoolfile = new File(config.getSpoolBase());
111 long tspace = spoolfile.getTotalSpace();
112 long start = (long) (tspace * fdstart);
113 long cur = spoolfile.getUsableSpace();
117 ArrayList<DelItem> cv = new ArrayList<>();
118 for (String sdir : dqs.keySet()) {
119 for (String meta : (new File(sdir)).list()) {
120 if (!meta.endsWith(".M") || meta.charAt(0) == '.') {
123 cv.add(new DelItem(meta.substring(0, meta.length() - 2), sdir));
126 DelItem[] items = cv.toArray(new DelItem[cv.size()]);
128 long stop = (long) (tspace * fdstop);
130 "NODE0501 Free disk space below red threshold. current=" + cur + " red=" + start + TOTAL + tspace);
131 if (determineFreeDiskSpace(spoolfile, tspace, stop, cur, items)) {
134 cur = spoolfile.getUsableSpace();
136 logger.info("NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
141 "NODE0504 Unable to recover sufficient disk space to reach green status. current=" + cur + YELLOW
142 + stop + TOTAL + tspace);
145 private void cleardirs() {
146 String basedir = config.getSpoolBase();
147 String nbase = basedir + "/n";
148 for (String nodedir : (new File(nbase)).list()) {
149 if (!nodedir.startsWith(".")) {
150 cleardir(nbase + "/" + nodedir);
153 String sxbase = basedir + "/s";
154 for (String sxdir : (new File(sxbase)).list()) {
155 if (sxdir.startsWith(".")) {
158 File sxf = new File(sxbase + "/" + sxdir);
159 for (String sdir : sxf.list()) {
160 if (!sdir.startsWith(".")) {
161 cleardir(sxbase + "/" + sxdir + "/" + sdir);
164 sxf.delete(); // won't if anything still in it
168 private synchronized void checkconfig() {
169 if (!config.isConfigured()) {
172 fdstart = config.getFreeDiskStart();
173 fdstop = config.getFreeDiskStop();
174 threads = config.getDeliveryThreads();
178 DestInfo[] alldis = config.getAllDests();
179 DeliveryQueue[] nqs = new DeliveryQueue[alldis.length];
181 HashMap<String, DeliveryQueue> ndqs = new HashMap<>();
182 for (DestInfo di : alldis) {
183 String spl = di.getSpool();
184 DeliveryQueue dq = dqs.get(spl);
186 dq = new DeliveryQueue(config, di);
196 while (curthreads < threads) {
198 (new Thread("del-thread-" + curthreads) {
209 private void dodelivery() {
211 while ((dq = getNextQueue()) != null) {
216 private synchronized DeliveryQueue getNextQueue() {
218 if (curthreads > threads) {
222 if (qpos < queues.length) {
223 DeliveryQueue dq = queues[qpos++];
224 if (dq.isSkipSet()) {
231 long now = System.currentTimeMillis();
232 if (now < nextcheck) {
234 wait(nextcheck + 500 - now);
235 } catch (Exception e) {
236 logger.error("InterruptedException", e);
238 now = System.currentTimeMillis();
240 if (now >= nextcheck) {
241 nextcheck = now + 5000;
248 private boolean determineFreeDiskSpace(File spoolfile, long tspace, long stop, long cur, DelItem[] items) {
249 for (DelItem item : items) {
250 long amount = dqs.get(item.getSpool()).cancelTask(item.getPublishId());
251 logger.info("NODE0502 Attempting to discard " + item.getSpool() + "/" + item.getPublishId()
252 + " to free up disk");
256 cur = spoolfile.getUsableSpace();
260 "NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
269 static class DelItem implements Comparable<DelItem> {
271 private String pubid;
272 private String spool;
274 public DelItem(String pubid, String spool) {
279 public int compareTo(DelItem other) {
280 int diff = pubid.compareTo(other.pubid);
282 diff = spool.compareTo(other.spool);
287 public String getPublishId() {
291 public String getSpool() {
296 public boolean equals(Object object) {
297 if (this == object) {
300 if (object == null || getClass() != object.getClass()) {
303 DelItem delItem = (DelItem) object;
304 return Objects.equals(pubid, delItem.pubid)
305 && Objects.equals(getSpool(), delItem.getSpool());
309 public int hashCode() {
310 return Objects.hash(pubid, getSpool());