1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.provisioning.utils;
\r
27 import java.io.File;
\r
28 import java.io.FileInputStream;
\r
29 import java.io.FileNotFoundException;
\r
30 import java.io.FileReader;
\r
31 import java.io.FilenameFilter;
\r
32 import java.io.IOException;
\r
33 import java.io.InputStreamReader;
\r
34 import java.io.LineNumberReader;
\r
35 import java.io.Reader;
\r
36 import java.sql.Connection;
\r
37 import java.sql.PreparedStatement;
\r
38 import java.sql.ResultSet;
\r
39 import java.sql.SQLException;
\r
40 import java.sql.Statement;
\r
41 import java.text.ParseException;
\r
42 import java.util.Date;
\r
43 import java.util.HashMap;
\r
44 import java.util.Iterator;
\r
45 import java.util.Map;
\r
46 import java.util.TreeSet;
\r
47 import java.util.zip.GZIPInputStream;
\r
49 import org.apache.log4j.Logger;
\r
51 import com.att.research.datarouter.provisioning.BaseServlet;
\r
52 import com.att.research.datarouter.provisioning.beans.DeliveryExtraRecord;
\r
53 import com.att.research.datarouter.provisioning.beans.DeliveryRecord;
\r
54 import com.att.research.datarouter.provisioning.beans.ExpiryRecord;
\r
55 import com.att.research.datarouter.provisioning.beans.Loadable;
\r
56 import com.att.research.datarouter.provisioning.beans.LogRecord;
\r
57 import com.att.research.datarouter.provisioning.beans.Parameters;
\r
58 import com.att.research.datarouter.provisioning.beans.PubFailRecord;
\r
59 import com.att.research.datarouter.provisioning.beans.PublishRecord;
\r
62 * This class provides methods that run in a separate thread, in order to process logfiles uploaded into the spooldir.
\r
63 * These logfiles are loaded into the MySQL LOG_RECORDS table. In a running provisioning server, there should only be
\r
64 * two places where records can be loaded into this table; here, and in the method DB.retroFit4() which may be run at
\r
65 * startup to load the old (1.0) style log tables into LOG_RECORDS;
\r
66 * <p>This method maintains an {@link RLEBitSet} which can be used to easily see what records are presently in the
\r
68 * This bit set is used to synchronize between provisioning servers.</p>
\r
70 * @author Robert Eby
\r
71 * @version $Id: LogfileLoader.java,v 1.22 2014/03/12 19:45:41 eby Exp $
\r
73 public class LogfileLoader extends Thread {
\r
74 /** Default number of log records to keep when pruning. Keep 10M by default. */
\r
75 public static final long DEFAULT_LOG_RETENTION = 10000000L;
\r
76 /** NOT USED: Percentage of free space required before old records are removed. */
\r
77 public static final int REQUIRED_FREE_PCT = 20;
\r
79 /** This is a singleton -- there is only one LogfileLoader object in the server */
\r
80 private static LogfileLoader p;
\r
83 * Get the singleton LogfileLoader object, and start it if it is not running.
\r
84 * @return the LogfileLoader
\r
86 public static synchronized LogfileLoader getLoader() {
\r
88 p = new LogfileLoader();
\r
94 /** The PreparedStatement which is loaded by a <i>Loadable</i>. */
\r
95 public static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
96 /** Each server can assign this many IDs */
\r
97 private static final long SET_SIZE = (1L << 56);
\r
99 private final Logger logger;
\r
100 private final DB db;
\r
101 private final String spooldir;
\r
102 private final long set_start;
\r
103 private final long set_end;
\r
104 private RLEBitSet seq_set;
\r
105 private long nextid;
\r
106 private boolean idle;
\r
108 private LogfileLoader() {
\r
109 this.logger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");
\r
110 this.db = new DB();
\r
111 this.spooldir = db.getProperties().getProperty("com.att.research.datarouter.provserver.spooldir");
\r
112 this.set_start = getIdRange();
\r
113 this.set_end = set_start + SET_SIZE - 1;
\r
114 this.seq_set = new RLEBitSet();
\r
118 // This is a potentially lengthy operation, so has been moved to run()
\r
119 //initializeNextid();
\r
120 this.setDaemon(true);
\r
121 this.setName("LogfileLoader");
\r
124 private long getIdRange() {
\r
126 if (BaseServlet.isInitialActivePOD())
\r
128 else if (BaseServlet.isInitialStandbyPOD())
\r
132 String r = String.format("[%X .. %X]", n, n+SET_SIZE-1);
\r
133 logger.debug("This server shall assign RECORD_IDs in the range "+r);
\r
137 * Return the bit set representing the record ID's that are loaded in this database.
\r
138 * @return the bit set
\r
140 public RLEBitSet getBitSet() {
\r
144 * True if the LogfileLoader is currently waiting for work.
\r
145 * @return true if idle
\r
147 public boolean isIdle() {
\r
151 * Run continuously to look for new logfiles in the spool directory and import them into the DB.
\r
152 * The spool is checked once per second. If free space on the MySQL filesystem falls below
\r
153 * REQUIRED_FREE_PCT (normally 20%) then the oldest logfile entries are removed and the LOG_RECORDS
\r
154 * table is compacted until free space rises above the threshold.
\r
157 public void run() {
\r
158 initializeNextid(); // moved from the constructor
\r
161 File dirfile = new File(spooldir);
\r
163 // process IN files
\r
164 File[] infiles = dirfile.listFiles(new FilenameFilter() {
\r
166 public boolean accept(File dir, String name) {
\r
167 return name.startsWith("IN.");
\r
171 if (infiles.length == 0) {
\r
174 Thread.sleep(1000L);
\r
175 } catch (InterruptedException e) {
\r
180 if (pruneRecords()) {
\r
181 // Removed at least some entries, recompute the bit map
\r
182 initializeNextid();
\r
185 // Process incoming logfiles
\r
186 for (File f : infiles) {
\r
187 if (logger.isDebugEnabled())
\r
188 logger.debug("PROV8001 Starting " + f + " ...");
\r
189 long time = System.currentTimeMillis();
\r
190 int[] n = process(f);
\r
191 time = System.currentTimeMillis() - time;
\r
193 .format("PROV8000 Processed %s in %d ms; %d of %d records.",
\r
194 f.toString(), time, n[0], n[1]));
\r
199 } catch (Exception e) {
\r
200 logger.warn("PROV0020: Caught exception in LogfileLoader: " + e);
\r
201 e.printStackTrace();
\r
205 private boolean pruneRecords() {
\r
206 boolean did1 = false;
\r
207 long count = countRecords();
\r
208 long threshold = DEFAULT_LOG_RETENTION;
\r
209 Parameters param = Parameters.getParameter(Parameters.PROV_LOG_RETENTION);
\r
210 if (param != null) {
\r
212 long n = Long.parseLong(param.getValue());
\r
213 // This check is to prevent inadvertent errors from wiping the table out
\r
216 } catch (NumberFormatException e) {
\r
220 logger.debug("Pruning LOG_RECORD table: records in DB="+count+", threshold="+threshold);
\r
221 if (count > threshold) {
\r
222 count -= threshold; // we need to remove this many records;
\r
223 Map<Long,Long> hist = getHistogram(); // histogram of records per day
\r
224 // Determine the cutoff point to remove the needed number of records
\r
227 for (Long day : new TreeSet<Long>(hist.keySet())) {
\r
228 sum += hist.get(day);
\r
234 cutoff *= 86400000L; // convert day to ms
\r
235 logger.debug(" Pruning records older than="+(cutoff/86400000L)+" ("+new Date(cutoff)+")");
\r
237 Connection conn = null;
\r
239 // Limit to a million at a time to avoid typing up the DB for too long.
\r
240 conn = db.getConnection();
\r
241 PreparedStatement ps = conn.prepareStatement("DELETE from LOG_RECORDS where EVENT_TIME < ? limit 1000000");
\r
242 ps.setLong(1, cutoff);
\r
243 while (count > 0) {
\r
244 if (!ps.execute()) {
\r
245 int dcount = ps.getUpdateCount();
\r
247 logger.debug(" "+dcount+" rows deleted.");
\r
248 did1 |= (dcount!=0);
\r
250 count = 0; // prevent inf. loops
\r
252 count = 0; // shouldn't happen!
\r
256 Statement stmt = conn.createStatement();
\r
257 stmt.execute("OPTIMIZE TABLE LOG_RECORDS");
\r
259 } catch (SQLException e) {
\r
260 System.err.println(e);
\r
261 e.printStackTrace();
\r
268 private long countRecords() {
\r
270 Connection conn = null;
\r
272 conn = db.getConnection();
\r
273 Statement stmt = conn.createStatement();
\r
274 ResultSet rs = stmt.executeQuery("SELECT COUNT(*) as COUNT from LOG_RECORDS");
\r
276 count = rs.getLong("COUNT");
\r
280 } catch (SQLException e) {
\r
281 System.err.println(e);
\r
282 e.printStackTrace();
\r
288 private Map<Long,Long> getHistogram() {
\r
289 Map<Long,Long> map = new HashMap<Long,Long>();
\r
290 Connection conn = null;
\r
292 logger.debug(" LOG_RECORD table histogram...");
\r
293 conn = db.getConnection();
\r
294 Statement stmt = conn.createStatement();
\r
295 ResultSet rs = stmt.executeQuery("SELECT FLOOR(EVENT_TIME/86400000) AS DAY, COUNT(*) AS COUNT FROM LOG_RECORDS GROUP BY DAY");
\r
296 while (rs.next()) {
\r
297 long day = rs.getLong("DAY");
\r
298 long cnt = rs.getLong("COUNT");
\r
300 logger.debug(" "+day + " "+cnt);
\r
304 } catch (SQLException e) {
\r
305 System.err.println(e);
\r
306 e.printStackTrace();
\r
312 private void initializeNextid() {
\r
313 Connection conn = null;
\r
315 conn = db.getConnection();
\r
316 Statement stmt = conn.createStatement();
\r
317 // Build a bitset of all records in the LOG_RECORDS table
\r
318 // We need to run this SELECT in stages, because otherwise we run out of memory!
\r
319 RLEBitSet nbs = new RLEBitSet();
\r
320 final long stepsize = 6000000L;
\r
321 boolean go_again = true;
\r
322 for (long i = 0; go_again; i += stepsize) {
\r
323 String sql = String.format("select RECORD_ID from LOG_RECORDS LIMIT %d,%d", i, stepsize);
\r
324 ResultSet rs = stmt.executeQuery(sql);
\r
326 while (rs.next()) {
\r
327 long n = rs.getLong("RECORD_ID");
\r
336 // Compare with the range for this server
\r
337 // Determine the next ID for this set of record IDs
\r
338 RLEBitSet tbs = (RLEBitSet) nbs.clone();
\r
339 RLEBitSet idset = new RLEBitSet();
\r
340 idset.set(set_start, set_start+SET_SIZE);
\r
342 long t = tbs.length();
\r
343 nextid = (t == 0) ? set_start : (t - 1);
\r
344 if (nextid >= set_start+SET_SIZE) {
\r
345 // Handle wraparound, when the IDs reach the end of our "range"
\r
346 Long[] last = null;
\r
347 Iterator<Long[]> li = tbs.getRangeIterator();
\r
348 while (li.hasNext()) {
\r
351 if (last != null) {
\r
352 tbs.clear(last[0], last[1]+1);
\r
354 nextid = (t == 0) ? set_start : (t - 1);
\r
357 logger.debug(String.format("initializeNextid, next ID is %d (%x)", nextid, nextid));
\r
358 } catch (SQLException e) {
\r
359 System.err.println(e);
\r
360 e.printStackTrace();
\r
365 // OLD CODE - commented here for historical purposes
\r
367 // private boolean pruneRecordsOldAlgorithm() {
\r
368 // // Determine space available -- available space must be at least 20% under /opt/app/mysql
\r
369 // int pct = getFreePercentage();
\r
370 // boolean did1 = false;
\r
371 // while (pct < REQUIRED_FREE_PCT) {
\r
372 // logger.info("PROV8008: Free space is " + pct + "% - removing old log entries");
\r
373 // boolean didit = removeOldestEntries();
\r
374 // pct = didit ? getFreePercentage() : 100; // don't loop endlessly
\r
379 // private int getFreePercentage() {
\r
380 // FileSystem fs = (Paths.get("/opt/app/mysql")).getFileSystem();
\r
384 // for (FileStore store : fs.getFileStores()) {
\r
385 // total += store.getTotalSpace();
\r
386 // avail += store.getUsableSpace();
\r
388 // } catch (IOException e) {
\r
390 // try { fs.close(); } catch (Exception e) { }
\r
391 // return (int)((avail * 100) / total);
\r
393 // private boolean removeOldestEntries() {
\r
394 // // Remove the last days worth of entries
\r
395 // Connection conn = null;
\r
397 // conn = db.getConnection();
\r
398 // Statement stmt = conn.createStatement();
\r
399 // ResultSet rs = stmt.executeQuery("select min(event_time) as MIN from LOG_RECORDS");
\r
400 // if (rs != null) {
\r
401 // if (rs.next()) {
\r
402 // // Compute the end of the first day of logs
\r
403 // long first = rs.getLong("MIN");
\r
404 // Calendar cal = new GregorianCalendar();
\r
405 // cal.setTime(new Date(first));
\r
406 // cal.add(Calendar.DAY_OF_YEAR, 1);
\r
407 // cal.set(Calendar.HOUR_OF_DAY, 0);
\r
408 // cal.set(Calendar.MINUTE, 0);
\r
409 // cal.set(Calendar.SECOND, 0);
\r
410 // cal.set(Calendar.MILLISECOND, 0);
\r
411 // if (!stmt.execute("delete from LOG_RECORDS where event_time < " + cal.getTimeInMillis())) {
\r
412 // int count = stmt.getUpdateCount();
\r
413 // logger.info("PROV0009: Removed "+count+" old log entries.");
\r
414 // stmt.execute("OPTIMIZE TABLE LOG_RECORDS");
\r
423 // } catch (SQLException e) {
\r
424 // System.err.println(e);
\r
425 // e.printStackTrace();
\r
427 // db.release(conn);
\r
431 @SuppressWarnings("resource")
\r
432 private int[] process(File f) {
\r
433 int ok = 0, total = 0;
\r
435 Connection conn = db.getConnection();
\r
436 PreparedStatement ps = conn.prepareStatement(INSERT_SQL);
\r
437 Reader r = f.getPath().endsWith(".gz")
\r
438 ? new InputStreamReader(new GZIPInputStream(new FileInputStream(f)))
\r
439 : new FileReader(f);
\r
440 LineNumberReader in = new LineNumberReader(r);
\r
442 while ((line = in.readLine()) != null) {
\r
444 for (Loadable rec : buildRecords(line)) {
\r
446 if (rec instanceof LogRecord) {
\r
447 LogRecord lr = ((LogRecord)rec);
\r
448 if (!seq_set.get(lr.getRecordId())) {
\r
449 ps.executeUpdate();
\r
450 seq_set.set(lr.getRecordId());
\r
452 logger.debug("Duplicate record ignored: "+lr.getRecordId());
\r
454 if (++nextid > set_end)
\r
455 nextid = set_start;
\r
456 ps.setLong(18, nextid);
\r
457 ps.executeUpdate();
\r
458 seq_set.set(nextid);
\r
460 ps.clearParameters();
\r
463 } catch (SQLException e) {
\r
464 logger.warn("PROV8003 Invalid value in record: "+line);
\r
466 e.printStackTrace();
\r
467 } catch (NumberFormatException e) {
\r
468 logger.warn("PROV8004 Invalid number in record: "+line);
\r
470 e.printStackTrace();
\r
471 } catch (ParseException e) {
\r
472 logger.warn("PROV8005 Invalid date in record: "+line);
\r
474 e.printStackTrace();
\r
475 } catch (Exception e) {
\r
476 logger.warn("PROV8006 Invalid pattern in record: "+line);
\r
478 e.printStackTrace();
\r
486 } catch (FileNotFoundException e) {
\r
487 logger.warn("PROV8007 Exception reading "+f+": "+e);
\r
488 } catch (IOException e) {
\r
489 logger.warn("PROV8007 Exception reading "+f+": "+e);
\r
490 } catch (SQLException e) {
\r
491 logger.warn("PROV8007 Exception reading "+f+": "+e);
\r
493 return new int[] { ok, total };
\r
495 private Loadable[] buildRecords(String line) throws ParseException {
\r
496 String[] pp = line.split("\\|");
\r
497 if (pp != null && pp.length >= 7) {
\r
498 String rtype = pp[1].toUpperCase();
\r
499 if (rtype.equals("PUB") && pp.length == 11) {
\r
500 // Fields are: date|PUB|pubid|feedid|requrl|method|ctype|clen|srcip|user|status
\r
501 return new Loadable[] { new PublishRecord(pp) };
\r
503 if (rtype.equals("DEL") && pp.length == 12) {
\r
504 // Fields are: date|DEL|pubid|feedid|subid|requrl|method|ctype|clen|user|status|xpubid
\r
505 String[] subs = pp[4].split("\\s+");
\r
506 if (subs != null) {
\r
507 Loadable[] rv = new Loadable[subs.length];
\r
508 for (int i = 0; i < subs.length; i++) {
\r
509 // create a new record for each individual sub
\r
511 rv[i] = new DeliveryRecord(pp);
\r
516 if (rtype.equals("EXP") && pp.length == 11) {
\r
517 // Fields are: date|EXP|pubid|feedid|subid|requrl|method|ctype|clen|reason|attempts
\r
518 ExpiryRecord e = new ExpiryRecord(pp);
\r
519 if (e.getReason().equals("other"))
\r
520 logger.info("Invalid reason '"+pp[9]+"' changed to 'other' for record: "+e.getPublishId());
\r
521 return new Loadable[] { e };
\r
523 if (rtype.equals("PBF") && pp.length == 12) {
\r
524 // Fields are: date|PBF|pubid|feedid|requrl|method|ctype|clen-expected|clen-received|srcip|user|error
\r
525 return new Loadable[] { new PubFailRecord(pp) };
\r
527 if (rtype.equals("DLX") && pp.length == 7) {
\r
528 // Fields are: date|DLX|pubid|feedid|subid|clen-tosend|clen-sent
\r
529 return new Loadable[] { new DeliveryExtraRecord(pp) };
\r
531 if (rtype.equals("LOG") && (pp.length == 19 || pp.length == 20)) {
\r
532 // Fields are: date|LOG|pubid|feedid|requrl|method|ctype|clen|type|feedFileid|remoteAddr|user|status|subid|fileid|result|attempts|reason|record_id
\r
533 return new Loadable[] { new LogRecord(pp) };
\r
536 logger.warn("PROV8002 bad record: "+line);
\r
537 return new Loadable[0];
\r
541 * The LogfileLoader can be run stand-alone by invoking the main() method of this class.
\r
543 * @throws InterruptedException
\r
545 public static void main(String[] a) throws InterruptedException {
\r
546 LogfileLoader.getLoader();
\r
547 Thread.sleep(200000L);
\r