X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FLogManager.java;h=ade47730b3fd0cc1733f31f3d88f0a2b24a014a3;hp=0e89f4170bc7f644a839d59893b4ce6c344ef713;hb=1841cb5d8da7b21996f8faad9d24f858e6ce8a41;hpb=4261823d84c2b911b68cdf4cb4dc3be429ebe285 diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java index 0e89f417..ade47730 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/LogManager.java @@ -1,159 +1,198 @@ -/******************************************************************************* - * ============LICENSE_START================================================== - * * org.onap.dmaap - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * ============LICENSE_END==================================================== - * * - * * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * * - ******************************************************************************/ -package org.onap.dmaap.datarouter.node; - -import java.util.*; -import java.util.regex.*; -import java.io.*; -import java.nio.file.*; -import java.text.*; - -/** - * Cleanup of old log files. - *

- * Periodically scan the log directory for log files that are older than - * the log file retention interval, and delete them. In a future release, - * This class will also be responsible for uploading events logs to the - * log server to support the log query APIs. - */ - -public class LogManager extends TimerTask { - private NodeConfigManager config; - private Matcher isnodelog; - private Matcher iseventlog; - private Uploader worker; - private String uploaddir; - private String logdir; - private class Uploader extends Thread implements DeliveryQueueHelper { - public long getInitFailureTimer() { return(10000L); } - public double getFailureBackoff() { return(2.0); } - public long getMaxFailureTimer() { return(150000L); } - public long getExpirationTimer() { return(604800000L); } - public int getFairFileLimit() { return(10000); } - public long getFairTimeLimit() { return(86400000); } - public String getDestURL(DestInfo dest, String fileid) { - return(config.getEventLogUrl()); - } - public void handleUnreachable(DestInfo dest) {} - public boolean handleRedirection(DestInfo dest, String location, String fileid) { return(false); } - public boolean isFollowRedirects() { return(false); } - public String getFeedId(String subid) { return(null); } - private DeliveryQueue dq; - public Uploader() { - dq = new DeliveryQueue(this, new DestInfo("LogUpload", uploaddir, null, null, null, config.getMyName(), config.getMyAuth(), false, false)); - setDaemon(true); - setName("Log Uploader"); - start(); - } - private synchronized void snooze() { - try { - wait(10000); - } catch (Exception e) { - } - } - private synchronized void poke() { - notify(); - } - public void run() { - while (true) { - scan(); - dq.run(); - snooze(); - } - } - private void scan() { - long threshold = System.currentTimeMillis() - config.getLogRetention(); - File dir = new File(logdir); - String[] fns = dir.list(); - Arrays.sort(fns); - String lastqueued = "events-000000000000.log"; - String curlog = StatusLog.getCurLogFile(); - curlog = curlog.substring(curlog.lastIndexOf('/') + 1); - try { - Writer w = new FileWriter(uploaddir + "/.meta"); - w.write("POST\tlogdata\nContent-Type\ttext/plain\n"); - w.close(); - BufferedReader br = new BufferedReader(new FileReader(uploaddir + "/.lastqueued")); - lastqueued = br.readLine(); - br.close(); - } catch (Exception e) { - } - for (String fn: fns) { - if (!isnodelog.reset(fn).matches()) { - if (!iseventlog.reset(fn).matches()) { - continue; - } - if (lastqueued.compareTo(fn) < 0 && curlog.compareTo(fn) > 0) { - lastqueued = fn; - try { - String pid = config.getPublishId(); - Files.createLink(Paths.get(uploaddir + "/" + pid), Paths.get(logdir + "/" + fn)); - Files.createLink(Paths.get(uploaddir + "/" + pid + ".M"), Paths.get(uploaddir + "/.meta")); - } catch (Exception e) { - } - } - } - File f = new File(dir, fn); - if (f.lastModified() < threshold) { - f.delete(); - } - } - try { - (new File(uploaddir + "/.meta")).delete(); - Writer w = new FileWriter(uploaddir + "/.lastqueued"); - w.write(lastqueued + "\n"); - w.close(); - } catch (Exception e) { - } - } - } - /** - * Construct a log manager - *

- * The log manager will check for expired log files every 5 minutes - * at 20 seconds after the 5 minute boundary. (Actually, the - * interval is the event log rollover interval, which - * defaults to 5 minutes). - */ - public LogManager(NodeConfigManager config) { - this.config = config; - try { - isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher(""); - iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher(""); - } catch (Exception e) {} - logdir = config.getLogDir(); - uploaddir = logdir + "/.spool"; - (new File(uploaddir)).mkdirs(); - long now = System.currentTimeMillis(); - long intvl = StatusLog.parseInterval(config.getEventLogInterval(), 300000); - long when = now - now % intvl + intvl + 20000L; - config.getTimer().scheduleAtFixedRate(this, when - now, intvl); - worker = new Uploader(); - } - /** - * Trigger check for expired log files and log files to upload - */ - public void run() { - worker.poke(); - } -} +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * * ============LICENSE_END==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.dmaap.datarouter.node; + +import java.util.*; +import java.util.regex.*; +import java.io.*; +import java.nio.file.*; +import java.text.*; + +/** + * Cleanup of old log files. + *

+ * Periodically scan the log directory for log files that are older than + * the log file retention interval, and delete them. In a future release, + * This class will also be responsible for uploading events logs to the + * log server to support the log query APIs. + */ + +public class LogManager extends TimerTask { + private NodeConfigManager config; + private Matcher isnodelog; + private Matcher iseventlog; + private Uploader worker; + private String uploaddir; + private String logdir; + + private class Uploader extends Thread implements DeliveryQueueHelper { + public long getInitFailureTimer() { + return (10000L); + } + + public double getFailureBackoff() { + return (2.0); + } + + public long getMaxFailureTimer() { + return (150000L); + } + + public long getExpirationTimer() { + return (604800000L); + } + + public int getFairFileLimit() { + return (10000); + } + + public long getFairTimeLimit() { + return (86400000); + } + + public String getDestURL(DestInfo dest, String fileid) { + return (config.getEventLogUrl()); + } + + public void handleUnreachable(DestInfo dest) { + } + + public boolean handleRedirection(DestInfo dest, String location, String fileid) { + return (false); + } + + public boolean isFollowRedirects() { + return (false); + } + + public String getFeedId(String subid) { + return (null); + } + + private DeliveryQueue dq; + + public Uploader() { + dq = new DeliveryQueue(this, new DestInfo("LogUpload", uploaddir, null, null, null, config.getMyName(), config.getMyAuth(), false, false)); + setDaemon(true); + setName("Log Uploader"); + start(); + } + + private synchronized void snooze() { + try { + wait(10000); + } catch (Exception e) { + } + } + + private synchronized void poke() { + notify(); + } + + public void run() { + while (true) { + scan(); + dq.run(); + snooze(); + } + } + + private void scan() { + long threshold = System.currentTimeMillis() - config.getLogRetention(); + File dir = new File(logdir); + String[] fns = dir.list(); + Arrays.sort(fns); + String lastqueued = "events-000000000000.log"; + String curlog = StatusLog.getCurLogFile(); + curlog = curlog.substring(curlog.lastIndexOf('/') + 1); + try { + Writer w = new FileWriter(uploaddir + "/.meta"); + w.write("POST\tlogdata\nContent-Type\ttext/plain\n"); + w.close(); + BufferedReader br = new BufferedReader(new FileReader(uploaddir + "/.lastqueued")); + lastqueued = br.readLine(); + br.close(); + } catch (Exception e) { + } + for (String fn : fns) { + if (!isnodelog.reset(fn).matches()) { + if (!iseventlog.reset(fn).matches()) { + continue; + } + if (lastqueued.compareTo(fn) < 0 && curlog.compareTo(fn) > 0) { + lastqueued = fn; + try { + String pid = config.getPublishId(); + Files.createLink(Paths.get(uploaddir + "/" + pid), Paths.get(logdir + "/" + fn)); + Files.createLink(Paths.get(uploaddir + "/" + pid + ".M"), Paths.get(uploaddir + "/.meta")); + } catch (Exception e) { + } + } + } + File f = new File(dir, fn); + if (f.lastModified() < threshold) { + f.delete(); + } + } + try { + (new File(uploaddir + "/.meta")).delete(); + Writer w = new FileWriter(uploaddir + "/.lastqueued"); + w.write(lastqueued + "\n"); + w.close(); + } catch (Exception e) { + } + } + } + + /** + * Construct a log manager + *

+ * The log manager will check for expired log files every 5 minutes + * at 20 seconds after the 5 minute boundary. (Actually, the + * interval is the event log rollover interval, which + * defaults to 5 minutes). + */ + public LogManager(NodeConfigManager config) { + this.config = config; + try { + isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher(""); + iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher(""); + } catch (Exception e) { + } + logdir = config.getLogDir(); + uploaddir = logdir + "/.spool"; + (new File(uploaddir)).mkdirs(); + long now = System.currentTimeMillis(); + long intvl = StatusLog.parseInterval(config.getEventLogInterval(), 300000); + long when = now - now % intvl + intvl + 20000L; + config.getTimer().scheduleAtFixedRate(this, when - now, intvl); + worker = new Uploader(); + } + + /** + * Trigger check for expired log files and log files to upload + */ + public void run() { + worker.poke(); + } +}