X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Futils%2FLogfileLoader.java;h=8223188434ab82437c98d303d95032c22db5273c;hb=refs%2Fchanges%2F51%2F78851%2F11;hp=ff7893d5a491e6ae68743015870003632bebcba9;hpb=8cbe8a88bc6dfe8673a33a017fe6a5a3e7ce86c3;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java index ff7893d5..82231884 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java @@ -46,7 +46,8 @@ import java.util.Map; import java.util.TreeSet; import java.util.zip.GZIPInputStream; -import org.apache.log4j.Logger; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.onap.dmaap.datarouter.provisioning.BaseServlet; import org.onap.dmaap.datarouter.provisioning.beans.DeliveryExtraRecord; import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord; @@ -82,7 +83,7 @@ public class LogfileLoader extends Thread { /** * This is a singleton -- there is only one LogfileLoader object in the server */ - private static LogfileLoader p; + private static LogfileLoader logfileLoader; /** * Get the singleton LogfileLoader object, and start it if it is not running. @@ -90,23 +91,23 @@ public class LogfileLoader extends Thread { * @return the LogfileLoader */ public static synchronized LogfileLoader getLoader() { - if (p == null) - p = new LogfileLoader(); - if (!p.isAlive()) - p.start(); - return p; + if (logfileLoader == null) + logfileLoader = new LogfileLoader(); + if (!logfileLoader.isAlive()) + logfileLoader.start(); + return logfileLoader; } /** * The PreparedStatement which is loaded by a Loadable. */ - public static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + 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 EELFLogger logger; private final DB db; private final String spooldir; private final long set_start; @@ -116,7 +117,7 @@ public class LogfileLoader extends Thread { private boolean idle; private LogfileLoader() { - this.logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + this.logger = EELFManager.getInstance().getLogger("InternalLog"); this.db = new DB(); this.spooldir = db.getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir"); this.set_start = getIdRange(); @@ -188,6 +189,7 @@ public class LogfileLoader extends Thread { try { Thread.sleep(1000L); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } idle = false; } else { @@ -213,12 +215,11 @@ public class LogfileLoader extends Thread { } } catch (Exception e) { logger.warn("PROV0020: Caught exception in LogfileLoader: " + e); - e.printStackTrace(); } } } - private boolean pruneRecords() { + boolean pruneRecords() { boolean did1 = false; long count = countRecords(); long threshold = DEFAULT_LOG_RETENTION; @@ -254,27 +255,27 @@ public class LogfileLoader extends Thread { try { // Limit to a million at a time to avoid typing up the DB for too long. conn = db.getConnection(); - PreparedStatement ps = conn.prepareStatement("DELETE from LOG_RECORDS where EVENT_TIME < ? limit 1000000"); - ps.setLong(1, cutoff); - while (count > 0) { - if (!ps.execute()) { - int dcount = ps.getUpdateCount(); - count -= dcount; - logger.debug(" " + dcount + " rows deleted."); - did1 |= (dcount != 0); - if (dcount == 0) - count = 0; // prevent inf. loops - } else { - count = 0; // shouldn't happen! + try(PreparedStatement ps = conn.prepareStatement("DELETE from LOG_RECORDS where EVENT_TIME < ? limit 1000000")) { + ps.setLong(1, cutoff); + while (count > 0) { + if (!ps.execute()) { + int dcount = ps.getUpdateCount(); + count -= dcount; + logger.debug(" " + dcount + " rows deleted."); + did1 |= (dcount != 0); + if (dcount == 0) + count = 0; // prevent inf. loops + } else { + count = 0; // shouldn't happen! + } } } - ps.close(); - Statement stmt = conn.createStatement(); - stmt.execute("OPTIMIZE TABLE LOG_RECORDS"); - stmt.close(); + try(Statement stmt = conn.createStatement()) { + stmt.execute("OPTIMIZE TABLE LOG_RECORDS"); + } } catch (SQLException e) { System.err.println(e); - e.printStackTrace(); + logger.error(e.toString()); } finally { db.release(conn); } @@ -282,46 +283,46 @@ public class LogfileLoader extends Thread { return did1; } - private long countRecords() { + long countRecords() { long count = 0; Connection conn = null; try { conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT COUNT(*) as COUNT from LOG_RECORDS"); - if (rs.next()) { - count = rs.getLong("COUNT"); - } - rs.close(); - stmt.close(); - } catch (SQLException e) { + try(Statement stmt = conn.createStatement()) { + try(ResultSet rs = stmt.executeQuery("SELECT COUNT(*) as COUNT from LOG_RECORDS")) { + if (rs.next()) { + count = rs.getLong("COUNT"); + } + } + } + } catch (SQLException e) { System.err.println(e); - e.printStackTrace(); + logger.error(e.toString()); } finally { db.release(conn); } return count; } - private Map getHistogram() { + Map getHistogram() { Map map = new HashMap(); Connection conn = null; try { logger.debug(" LOG_RECORD table histogram..."); conn = db.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT FLOOR(EVENT_TIME/86400000) AS DAY, COUNT(*) AS COUNT FROM LOG_RECORDS GROUP BY DAY"); - while (rs.next()) { - long day = rs.getLong("DAY"); - long cnt = rs.getLong("COUNT"); - map.put(day, cnt); - logger.debug(" " + day + " " + cnt); + try(Statement stmt = conn.createStatement()) { + try(ResultSet rs = stmt.executeQuery("SELECT FLOOR(EVENT_TIME/86400000) AS DAY, COUNT(*) AS COUNT FROM LOG_RECORDS GROUP BY DAY")) { + while (rs.next()) { + long day = rs.getLong("DAY"); + long cnt = rs.getLong("COUNT"); + map.put(day, cnt); + logger.debug(" " + day + " " + cnt); + } + } } - rs.close(); - stmt.close(); - } catch (SQLException e) { + } catch (SQLException e) { System.err.println(e); - e.printStackTrace(); + logger.error(e.toString()); } finally { db.release(conn); } @@ -332,26 +333,25 @@ public class LogfileLoader extends Thread { Connection conn = null; try { conn = db.getConnection(); - Statement stmt = conn.createStatement(); - // Build a bitset of all records in the LOG_RECORDS table - // We need to run this SELECT in stages, because otherwise we run out of memory! RLEBitSet nbs = new RLEBitSet(); - final long stepsize = 6000000L; - boolean go_again = true; - for (long i = 0; go_again; i += stepsize) { - String sql = String.format("select RECORD_ID from LOG_RECORDS LIMIT %d,%d", i, stepsize); - ResultSet rs = stmt.executeQuery(sql); - go_again = false; - while (rs.next()) { - long n = rs.getLong("RECORD_ID"); - nbs.set(n); - go_again = true; + try(Statement stmt = conn.createStatement()) { + // Build a bitset of all records in the LOG_RECORDS table + // We need to run this SELECT in stages, because otherwise we run out of memory! + final long stepsize = 6000000L; + boolean go_again = true; + for (long i = 0; go_again; i += stepsize) { + String sql = String.format("select RECORD_ID from LOG_RECORDS LIMIT %d,%d", i, stepsize); + try (ResultSet rs = stmt.executeQuery(sql)) { + go_again = false; + while (rs.next()) { + long n = rs.getLong("RECORD_ID"); + nbs.set(n); + go_again = true; + } + } } - rs.close(); } - stmt.close(); seq_set = nbs; - // Compare with the range for this server // Determine the next ID for this set of record IDs RLEBitSet tbs = (RLEBitSet) nbs.clone(); @@ -376,14 +376,14 @@ public class LogfileLoader extends Thread { logger.debug(String.format("initializeNextid, next ID is %d (%x)", nextid, nextid)); } catch (SQLException e) { System.err.println(e); - e.printStackTrace(); + logger.error(e.toString()); } finally { db.release(conn); } } @SuppressWarnings("resource") - private int[] process(File f) { + int[] process(File f) { int ok = 0, total = 0; try { Connection conn = db.getConnection(); @@ -391,49 +391,45 @@ public class LogfileLoader extends Thread { Reader r = f.getPath().endsWith(".gz") ? new InputStreamReader(new GZIPInputStream(new FileInputStream(f))) : new FileReader(f); - LineNumberReader in = new LineNumberReader(r); - String line; - while ((line = in.readLine()) != null) { - try { - for (Loadable rec : buildRecords(line)) { - rec.load(ps); - if (rec instanceof LogRecord) { - LogRecord lr = ((LogRecord) rec); - if (!seq_set.get(lr.getRecordId())) { + try(LineNumberReader in = new LineNumberReader(r)) { + String line; + while ((line = in.readLine()) != null) { + try { + for (Loadable rec : buildRecords(line)) { + rec.load(ps); + if (rec instanceof LogRecord) { + LogRecord lr = ((LogRecord) rec); + if (!seq_set.get(lr.getRecordId())) { + ps.executeUpdate(); + seq_set.set(lr.getRecordId()); + } else + logger.debug("Duplicate record ignored: " + lr.getRecordId()); + } else { + if (++nextid > set_end) + nextid = set_start; + ps.setLong(18, nextid); ps.executeUpdate(); - seq_set.set(lr.getRecordId()); - } else - logger.debug("Duplicate record ignored: " + lr.getRecordId()); - } else { - if (++nextid > set_end) - nextid = set_start; - ps.setLong(18, nextid); - ps.executeUpdate(); - seq_set.set(nextid); + seq_set.set(nextid); + } + ps.clearParameters(); + ok++; } - ps.clearParameters(); - ok++; + } catch (SQLException e) { + logger.warn("PROV8003 Invalid value in record: " + line); + logger.debug(e.toString()); + } catch (NumberFormatException e) { + logger.warn("PROV8004 Invalid number in record: " + line); + logger.debug(e.toString()); + } catch (ParseException e) { + logger.warn("PROV8005 Invalid date in record: " + line); + logger.debug(e.toString()); + } catch (Exception e) { + logger.warn("PROV8006 Invalid pattern in record: " + line); + logger.debug(e.toString()); } - } catch (SQLException e) { - logger.warn("PROV8003 Invalid value in record: " + line); - logger.debug(e); - e.printStackTrace(); - } catch (NumberFormatException e) { - logger.warn("PROV8004 Invalid number in record: " + line); - logger.debug(e); - e.printStackTrace(); - } catch (ParseException e) { - logger.warn("PROV8005 Invalid date in record: " + line); - logger.debug(e); - e.printStackTrace(); - } catch (Exception e) { - logger.warn("PROV8006 Invalid pattern in record: " + line); - logger.debug(e); - e.printStackTrace(); + total++; } - total++; } - in.close(); ps.close(); db.release(conn); conn = null; @@ -447,7 +443,7 @@ public class LogfileLoader extends Thread { return new int[]{ok, total}; } - private Loadable[] buildRecords(String line) throws ParseException { + Loadable[] buildRecords(String line) throws ParseException { String[] pp = line.split("\\|"); if (pp != null && pp.length >= 7) { String rtype = pp[1].toUpperCase();