/******************************************************************************* * ============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.provisioning.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeSet; import java.util.zip.GZIPInputStream; import org.apache.log4j.Logger; import org.onap.dmaap.datarouter.provisioning.BaseServlet; import org.onap.dmaap.datarouter.provisioning.beans.DeliveryExtraRecord; import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord; import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord; import org.onap.dmaap.datarouter.provisioning.beans.Loadable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.onap.dmaap.datarouter.provisioning.beans.PubFailRecord; import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord; /** * This class provides methods that run in a separate thread, in order to process logfiles uploaded into the spooldir. * These logfiles are loaded into the MariaDB LOG_RECORDS table. In a running provisioning server, there should only be * two places where records can be loaded into this table; here, and in the method DB.retroFit4() which may be run at * startup to load the old (1.0) style log tables into LOG_RECORDS; *
This method maintains an {@link RLEBitSet} which can be used to easily see what records are presently in the * database. * This bit set is used to synchronize between provisioning servers.
* * @author Robert Eby * @version $Id: LogfileLoader.java,v 1.22 2014/03/12 19:45:41 eby Exp $ */ public class LogfileLoader extends Thread { /** * Default number of log records to keep when pruning. Keep 10M by default. */ public static final long DEFAULT_LOG_RETENTION = 10000000L; /** * NOT USED: Percentage of free space required before old records are removed. */ public static final int REQUIRED_FREE_PCT = 20; /** * This is a singleton -- there is only one LogfileLoader object in the server */ private static LogfileLoader p; /** * Get the singleton LogfileLoader object, and start it if it is not running. * * @return the LogfileLoader */ public static synchronized LogfileLoader getLoader() { if (p == null) p = new LogfileLoader(); if (!p.isAlive()) p.start(); return p; } /** * The PreparedStatement which is loaded by a Loadable. */ public static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; /** * Each server can assign this many IDs */ private static final long SET_SIZE = (1L << 56); private final Logger logger; private final DB db; private final String spooldir; private final long set_start; private final long set_end; private RLEBitSet seq_set; private long nextid; private boolean idle; private LogfileLoader() { this.logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); this.db = new DB(); this.spooldir = db.getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir"); this.set_start = getIdRange(); this.set_end = set_start + SET_SIZE - 1; this.seq_set = new RLEBitSet(); this.nextid = 0; this.idle = false; // This is a potentially lengthy operation, so has been moved to run() //initializeNextid(); this.setDaemon(true); this.setName("LogfileLoader"); } private long getIdRange() { long n; if (BaseServlet.isInitialActivePOD()) n = 0; else if (BaseServlet.isInitialStandbyPOD()) n = SET_SIZE; else n = SET_SIZE * 2; String r = String.format("[%X .. %X]", n, n + SET_SIZE - 1); logger.debug("This server shall assign RECORD_IDs in the range " + r); return n; } /** * Return the bit set representing the record ID's that are loaded in this database. * * @return the bit set */ public RLEBitSet getBitSet() { return seq_set; } /** * True if the LogfileLoader is currently waiting for work. * * @return true if idle */ public boolean isIdle() { return idle; } /** * Run continuously to look for new logfiles in the spool directory and import them into the DB. * The spool is checked once per second. If free space on the MariaDB filesystem falls below * REQUIRED_FREE_PCT (normally 20%) then the oldest logfile entries are removed and the LOG_RECORDS * table is compacted until free space rises above the threshold. */ @Override public void run() { initializeNextid(); // moved from the constructor while (true) { try { File dirfile = new File(spooldir); while (true) { // process IN files File[] infiles = dirfile.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith("IN."); } }); if (infiles.length == 0) { idle = true; try { Thread.sleep(1000L); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } idle = false; } else { // Remove old rows if (pruneRecords()) { // Removed at least some entries, recompute the bit map initializeNextid(); } // Process incoming logfiles for (File f : infiles) { if (logger.isDebugEnabled()) logger.debug("PROV8001 Starting " + f + " ..."); long time = System.currentTimeMillis(); int[] n = process(f); time = System.currentTimeMillis() - time; logger.info(String .format("PROV8000 Processed %s in %d ms; %d of %d records.", f.toString(), time, n[0], n[1])); f.delete(); } } } } catch (Exception e) { logger.warn("PROV0020: Caught exception in LogfileLoader: " + e); } } } private boolean pruneRecords() { boolean did1 = false; long count = countRecords(); long threshold = DEFAULT_LOG_RETENTION; Parameters param = Parameters.getParameter(Parameters.PROV_LOG_RETENTION); if (param != null) { try { long n = Long.parseLong(param.getValue()); // This check is to prevent inadvertent errors from wiping the table out if (n > 1000000L) threshold = n; } catch (NumberFormatException e) { // ignore } } logger.debug("Pruning LOG_RECORD table: records in DB=" + count + ", threshold=" + threshold); if (count > threshold) { count -= threshold; // we need to remove this many records; Map