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.io.IOException;
30 import java.nio.file.Files;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.Objects;
37 * Main control point for delivering files to destinations.
39 * <p>The Delivery class manages assignment of delivery threads to delivery queues and creation and destruction of
40 * delivery queues as configuration changes. DeliveryQueues are assigned threads based on a modified round-robin
41 * approach giving priority to queues with more work as measured by both bytes to deliver and files to deliver and lower
42 * priority to queues that already have delivery threads working. A delivery thread continues to work for a delivery
43 * queue as long as that queue has more files to deliver.
45 public class Delivery {
47 private static final String TOTAL = " total=";
48 private static final String YELLOW = " yellow=";
49 private static EELFLogger logger = EELFManager.getInstance().getLogger(Delivery.class);
50 private double fdstart;
51 private double fdstop;
53 private int curthreads;
54 private NodeConfigManager config;
55 private HashMap<String, DeliveryQueue> dqs = new HashMap<>();
56 private DeliveryQueue[] queues = new DeliveryQueue[0];
58 private long nextcheck;
61 * Constructs a new Delivery system using the specified configuration manager.
63 * @param config The configuration manager for this delivery system.
65 public Delivery(NodeConfigManager config) {
67 Runnable cmon = this::checkconfig;
68 config.registerConfigTask(cmon);
73 * Reset the retry timer for a delivery queue.
75 public synchronized void resetQueue(String spool) {
77 DeliveryQueue dq = dqs.get(spool);
85 * Mark the task in spool a success.
87 public synchronized boolean markTaskSuccess(String spool, String pubId) {
88 boolean succeeded = false;
90 DeliveryQueue dq = dqs.get(spool);
92 succeeded = dq.markTaskSuccess(pubId);
98 private void cleardir(String dir) {
99 if (dqs.get(dir) != null) {
102 File fdir = new File(dir);
104 for (File junk : fdir.listFiles()) {
106 Files.delete(fdir.toPath());
109 Files.delete(fdir.toPath());
110 } catch (IOException e) {
111 logger.error("Failed to delete file: " + fdir.getPath(), e);
115 private void freeDiskCheck() {
116 File spoolfile = new File(config.getSpoolBase());
117 long tspace = spoolfile.getTotalSpace();
118 long start = (long) (tspace * fdstart);
119 long cur = spoolfile.getUsableSpace();
123 ArrayList<DelItem> cv = new ArrayList<>();
124 for (String sdir : dqs.keySet()) {
125 for (String meta : (new File(sdir)).list()) {
126 if (!meta.endsWith(".M") || meta.charAt(0) == '.') {
129 cv.add(new DelItem(meta.substring(0, meta.length() - 2), sdir));
132 DelItem[] items = cv.toArray(new DelItem[cv.size()]);
134 long stop = (long) (tspace * fdstop);
136 "NODE0501 Free disk space below red threshold. current=" + cur + " red=" + start + TOTAL + tspace);
137 if (determineFreeDiskSpace(spoolfile, tspace, stop, cur, items)) {
140 cur = spoolfile.getUsableSpace();
142 logger.warn("NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
147 "NODE0504 Unable to recover sufficient disk space to reach green status. current=" + cur + YELLOW
148 + stop + TOTAL + tspace);
151 private void cleardirs() {
152 String basedir = config.getSpoolBase();
153 String nbase = basedir + "/n";
154 for (String nodedir : (new File(nbase)).list()) {
155 if (!nodedir.startsWith(".")) {
156 cleardir(nbase + "/" + nodedir);
159 String sxbase = basedir + "/s";
160 for (String sxdir : (new File(sxbase)).list()) {
161 if (sxdir.startsWith(".")) {
164 File sxf = new File(sxbase + "/" + sxdir);
165 for (String sdir : sxf.list()) {
166 if (!sdir.startsWith(".")) {
167 cleardir(sxbase + "/" + sxdir + "/" + sdir);
171 Files.delete(sxf.toPath()); // won't if anything still in it
172 } catch (IOException e) {
173 logger.error("Failed to delete file: " + sxf.getPath(), e);
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 HashMap<String, DeliveryQueue> ndqs = new HashMap<>();
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) {
208 (new Thread("del-thread-" + curthreads) {
219 private void dodelivery() {
221 while ((dq = getNextQueue()) != null) {
226 private synchronized DeliveryQueue getNextQueue() {
228 if (curthreads > threads) {
232 if (qpos < queues.length) {
233 DeliveryQueue dq = queues[qpos++];
234 if (dq.isSkipSet()) {
241 long now = System.currentTimeMillis();
242 if (now < nextcheck) {
244 wait(nextcheck + 500 - now);
245 } catch (Exception e) {
246 logger.error("InterruptedException", e);
248 now = System.currentTimeMillis();
250 if (now >= nextcheck) {
251 nextcheck = now + 5000;
258 private boolean determineFreeDiskSpace(File spoolfile, long tspace, long stop, long cur, DelItem[] items) {
259 for (DelItem item : items) {
260 long amount = dqs.get(item.getSpool()).cancelTask(item.getPublishId());
261 logger.debug("NODE0502 Attempting to discard " + item.getSpool() + "/" + item.getPublishId()
262 + " to free up disk");
266 cur = spoolfile.getUsableSpace();
270 "NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
279 static class DelItem implements Comparable<DelItem> {
281 private String pubid;
282 private String spool;
284 public DelItem(String pubid, String spool) {
289 public int compareTo(DelItem other) {
290 int diff = pubid.compareTo(other.pubid);
292 diff = spool.compareTo(other.spool);
297 public String getPublishId() {
301 public String getSpool() {
306 public boolean equals(Object object) {
307 if (this == object) {
310 if (object == null || getClass() != object.getClass()) {
313 DelItem delItem = (DelItem) object;
314 return Objects.equals(pubid, delItem.pubid)
315 && Objects.equals(getSpool(), delItem.getSpool());
319 public int hashCode() {
320 return Objects.hash(pubid, getSpool());