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);
72 * Reset the retry timer for a delivery queue.
74 public synchronized void resetQueue(String spool) {
76 DeliveryQueue dq = dqs.get(spool);
84 * Mark the task in spool a success.
86 public synchronized boolean markTaskSuccess(String spool, String pubId) {
87 boolean succeeded = false;
89 DeliveryQueue dq = dqs.get(spool);
91 succeeded = dq.markTaskSuccess(pubId);
97 private void cleardir(String dir) {
98 if (dqs.get(dir) != null) {
101 File fdir = new File(dir);
103 for (File junk : fdir.listFiles()) {
105 Files.delete(fdir.toPath());
108 Files.delete(fdir.toPath());
109 } catch (IOException e) {
110 logger.error("Failed to delete file: " + fdir.getPath(), e);
114 private void freeDiskCheck() {
115 File spoolfile = new File(config.getSpoolBase());
116 long tspace = spoolfile.getTotalSpace();
117 long start = (long) (tspace * fdstart);
118 long cur = spoolfile.getUsableSpace();
122 ArrayList<DelItem> cv = new ArrayList<>();
123 for (String sdir : dqs.keySet()) {
124 for (String meta : (new File(sdir)).list()) {
125 if (!meta.endsWith(".M") || meta.charAt(0) == '.') {
128 cv.add(new DelItem(meta.substring(0, meta.length() - 2), sdir));
131 DelItem[] items = cv.toArray(new DelItem[cv.size()]);
133 long stop = (long) (tspace * fdstop);
135 "NODE0501 Free disk space below red threshold. current=" + cur + " red=" + start + TOTAL + tspace);
136 if (determineFreeDiskSpace(spoolfile, tspace, stop, cur, items)) {
139 cur = spoolfile.getUsableSpace();
141 logger.warn("NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
146 "NODE0504 Unable to recover sufficient disk space to reach green status. current=" + cur + YELLOW
147 + stop + TOTAL + tspace);
150 private void cleardirs() {
151 String basedir = config.getSpoolBase();
152 String nbase = basedir + "/n";
153 for (String nodedir : (new File(nbase)).list()) {
154 if (!nodedir.startsWith(".")) {
155 cleardir(nbase + "/" + nodedir);
158 String sxbase = basedir + "/s";
159 for (String sxdir : (new File(sxbase)).list()) {
160 if (sxdir.startsWith(".")) {
163 File sxf = new File(sxbase + File.separator + sxdir);
164 for (String sdir : sxf.list()) {
165 if (!sdir.startsWith(".")) {
166 cleardir(sxbase + "/" + sxdir + "/" + sdir);
170 if (sxf.list().length == 0) {
171 Files.delete(sxf.toPath()); // won't if anything still in it
173 } catch (IOException e) {
174 logger.error("Failed to delete file: " + sxf.getPath(), e);
179 private synchronized void checkconfig() {
180 if (!config.isConfigured()) {
183 fdstart = config.getFreeDiskStart();
184 fdstop = config.getFreeDiskStop();
185 threads = config.getDeliveryThreads();
189 DestInfo[] alldis = config.getAllDests();
190 DeliveryQueue[] nqs = new DeliveryQueue[alldis.length];
192 HashMap<String, DeliveryQueue> ndqs = new HashMap<>();
193 for (DestInfo di : alldis) {
194 String spl = di.getSpool();
195 DeliveryQueue dq = dqs.get(spl);
197 dq = new DeliveryQueue(config, di);
207 while (curthreads < threads) {
209 (new Thread("del-thread-" + curthreads) {
220 private void dodelivery() {
222 while ((dq = getNextQueue()) != null) {
227 private synchronized DeliveryQueue getNextQueue() {
229 if (curthreads > threads) {
233 if (qpos < queues.length) {
234 DeliveryQueue dq = queues[qpos++];
235 if (dq.isSkipSet()) {
242 long now = System.currentTimeMillis();
243 if (now < nextcheck) {
245 wait(nextcheck + 500 - now);
246 } catch (Exception e) {
247 logger.error("InterruptedException", e);
249 now = System.currentTimeMillis();
251 if (now >= nextcheck) {
252 nextcheck = now + 5000;
259 private boolean determineFreeDiskSpace(File spoolfile, long tspace, long stop, long cur, DelItem[] items) {
260 for (DelItem item : items) {
261 long amount = dqs.get(item.getSpool()).cancelTask(item.getPublishId());
262 logger.debug("NODE0502 Attempting to discard " + item.getSpool() + "/" + item.getPublishId()
263 + " to free up disk");
267 cur = spoolfile.getUsableSpace();
271 "NODE0503 Free disk space at or above yellow threshold. current=" + cur + YELLOW + stop
280 static class DelItem implements Comparable<DelItem> {
282 private String pubid;
283 private String spool;
285 public DelItem(String pubid, String spool) {
290 public int compareTo(DelItem other) {
291 int diff = pubid.compareTo(other.pubid);
293 diff = spool.compareTo(other.spool);
298 public String getPublishId() {
302 public String getSpool() {
307 public boolean equals(Object object) {
308 if (this == object) {
311 if (object == null || getClass() != object.getClass()) {
314 DelItem delItem = (DelItem) object;
315 return Objects.equals(pubid, delItem.pubid)
316 && Objects.equals(getSpool(), delItem.getSpool());
320 public int hashCode() {
321 return Objects.hash(pubid, getSpool());