Cleanup addressing PR review comments.

This commit is contained in:
Ann Priestman 2018-04-09 13:50:49 -04:00
parent d5f1064e88
commit 1a437d8686
2 changed files with 41 additions and 41 deletions

View File

@ -30,11 +30,11 @@ import org.sleuthkit.autopsy.casemodule.Case;
*/ */
final class HealthMonitorCaseEventListener implements PropertyChangeListener { final class HealthMonitorCaseEventListener implements PropertyChangeListener {
private final ExecutorService jobProcessingExecutor; private final ExecutorService healthMonitorExecutor;
private static final String CASE_EVENT_THREAD_NAME = "Health-Monitor-Event-Listener-%d"; private static final String HEALTH_MONITOR_EVENT_THREAD_NAME = "Health-Monitor-Event-Listener-%d";
HealthMonitorCaseEventListener() { HealthMonitorCaseEventListener() {
jobProcessingExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat(CASE_EVENT_THREAD_NAME).build()); healthMonitorExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat(HEALTH_MONITOR_EVENT_THREAD_NAME).build());
} }
@Override @Override
@ -45,7 +45,7 @@ final class HealthMonitorCaseEventListener implements PropertyChangeListener {
case CURRENT_CASE: case CURRENT_CASE:
if ((null == evt.getNewValue()) && (evt.getOldValue() instanceof Case)) { if ((null == evt.getNewValue()) && (evt.getOldValue() instanceof Case)) {
// When a case is closed, write the current metrics to the database // When a case is closed, write the current metrics to the database
jobProcessingExecutor.submit(new ServicesHealthMonitor.DatabaseWriteTask()); healthMonitorExecutor.submit(new ServicesHealthMonitor.DatabaseWriteTask());
} }
break; break;
} }

View File

@ -60,10 +60,11 @@ public class ServicesHealthMonitor {
private static final AtomicBoolean isEnabled = new AtomicBoolean(false); private static final AtomicBoolean isEnabled = new AtomicBoolean(false);
private static ServicesHealthMonitor instance; private static ServicesHealthMonitor instance;
private ScheduledThreadPoolExecutor periodicTasksExecutor; private ScheduledThreadPoolExecutor healthMonitorOutputTimer;
private final Map<String, TimingInfo> timingInfoMap; private final Map<String, TimingInfo> timingInfoMap;
private static final int CONN_POOL_SIZE = 10; private static final int CONN_POOL_SIZE = 10;
private BasicDataSource connectionPool = null; private BasicDataSource connectionPool = null;
private String hostName;
private ServicesHealthMonitor() throws HealthMonitorException { private ServicesHealthMonitor() throws HealthMonitorException {
@ -71,6 +72,15 @@ public class ServicesHealthMonitor {
// of whether the monitor is enabled. // of whether the monitor is enabled.
timingInfoMap = new HashMap<>(); timingInfoMap = new HashMap<>();
// Get the host name
try {
hostName = java.net.InetAddress.getLocalHost().getHostName();
} catch (java.net.UnknownHostException ex) {
// Continue on but log a warning
logger.log(Level.WARNING, "Unable to look up host name");
hostName = "unknown";
}
// Read from module settings to determine if the module is enabled // Read from module settings to determine if the module is enabled
if (ModuleSettings.settingExists(MODULE_NAME, IS_ENABLED_KEY)) { if (ModuleSettings.settingExists(MODULE_NAME, IS_ENABLED_KEY)) {
if(ModuleSettings.getConfigSetting(MODULE_NAME, IS_ENABLED_KEY).equals("true")){ if(ModuleSettings.getConfigSetting(MODULE_NAME, IS_ENABLED_KEY).equals("true")){
@ -172,20 +182,20 @@ public class ServicesHealthMonitor {
* Start the ScheduledThreadPoolExecutor that will handle the database writes. * Start the ScheduledThreadPoolExecutor that will handle the database writes.
*/ */
private synchronized void startTimer() { private synchronized void startTimer() {
if(periodicTasksExecutor != null) { if(healthMonitorOutputTimer != null) {
// Make sure the previous executor (if it exists) has been stopped // Make sure the previous executor (if it exists) has been stopped
periodicTasksExecutor.shutdown(); healthMonitorOutputTimer.shutdown();
} }
periodicTasksExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder().setNameFormat("health_monitor_timer").build()); healthMonitorOutputTimer = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder().setNameFormat("health_monitor_timer").build());
periodicTasksExecutor.scheduleWithFixedDelay(new DatabaseWriteTask(), DATABASE_WRITE_INTERVAL, DATABASE_WRITE_INTERVAL, TimeUnit.MINUTES); healthMonitorOutputTimer.scheduleWithFixedDelay(new DatabaseWriteTask(), DATABASE_WRITE_INTERVAL, DATABASE_WRITE_INTERVAL, TimeUnit.MINUTES);
} }
/** /**
* Stop the ScheduledThreadPoolExecutor to prevent further database writes. * Stop the ScheduledThreadPoolExecutor to prevent further database writes.
*/ */
private synchronized void stopTimer() { private synchronized void stopTimer() {
if(periodicTasksExecutor != null) { if(healthMonitorOutputTimer != null) {
periodicTasksExecutor.shutdown(); healthMonitorOutputTimer.shutdown();
} }
} }
@ -301,15 +311,6 @@ public class ServicesHealthMonitor {
timingInfoMap.clear(); timingInfoMap.clear();
} }
logger.log(Level.INFO, "Writing health monitor metrics to database"); logger.log(Level.INFO, "Writing health monitor metrics to database");
String hostName;
try {
hostName = java.net.InetAddress.getLocalHost().getHostName();
} catch (java.net.UnknownHostException ex) {
// Write it to the database but log a warning
logger.log(Level.WARNING, "Unable to look up host name");
hostName = "unknown";
}
// Check if there's anything to report (right now we only have the timing map) // Check if there's anything to report (right now we only have the timing map)
if(timingMapCopy.keySet().isEmpty()) { if(timingMapCopy.keySet().isEmpty()) {
@ -587,26 +588,26 @@ public class ServicesHealthMonitor {
try (Statement statement = conn.createStatement()) { try (Statement statement = conn.createStatement()) {
conn.setAutoCommit(false); conn.setAutoCommit(false);
StringBuilder createTimingTable = new StringBuilder(); String createTimingTable =
createTimingTable.append("CREATE TABLE IF NOT EXISTS timing_data ("); "CREATE TABLE IF NOT EXISTS timing_data (" +
createTimingTable.append("id SERIAL PRIMARY KEY,"); "id SERIAL PRIMARY KEY," +
createTimingTable.append("name text NOT NULL,"); "name text NOT NULL," +
createTimingTable.append("host text NOT NULL,"); "host text NOT NULL," +
createTimingTable.append("timestamp bigint NOT NULL,"); "timestamp bigint NOT NULL," +
createTimingTable.append("count bigint NOT NULL,"); "count bigint NOT NULL," +
createTimingTable.append("average bigint NOT NULL,"); "average bigint NOT NULL," +
createTimingTable.append("max bigint NOT NULL,"); "max bigint NOT NULL," +
createTimingTable.append("min bigint NOT NULL"); "min bigint NOT NULL" +
createTimingTable.append(")"); ")";
statement.execute(createTimingTable.toString()); statement.execute(createTimingTable);
StringBuilder createDbInfoTable = new StringBuilder(); String createDbInfoTable =
createDbInfoTable.append("CREATE TABLE IF NOT EXISTS db_info ("); "CREATE TABLE IF NOT EXISTS db_info (" +
createDbInfoTable.append("id SERIAL PRIMARY KEY NOT NULL,"); "id SERIAL PRIMARY KEY NOT NULL," +
createDbInfoTable.append("name text NOT NULL,"); "name text NOT NULL," +
createDbInfoTable.append("value text NOT NULL"); "value text NOT NULL" +
createDbInfoTable.append(")"); ")";
statement.execute(createDbInfoTable.toString()); statement.execute(createDbInfoTable);
statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMajor() + "')"); statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMajor() + "')");
statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_MINOR_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMinor() + "')"); statement.execute("INSERT INTO db_info (name, value) VALUES ('SCHEMA_MINOR_VERSION', '" + CURRENT_DB_SCHEMA_VERSION.getMinor() + "')");
@ -655,8 +656,7 @@ public class ServicesHealthMonitor {
*/ */
private CoordinationService.Lock getExclusiveDbLock() throws HealthMonitorException{ private CoordinationService.Lock getExclusiveDbLock() throws HealthMonitorException{
try { try {
String databaseNodeName = DATABASE_NAME; CoordinationService.Lock lock = CoordinationService.getInstance().tryGetExclusiveLock(CoordinationService.CategoryNode.HEALTH_MONITOR, DATABASE_NAME, 5, TimeUnit.MINUTES);
CoordinationService.Lock lock = CoordinationService.getInstance().tryGetExclusiveLock(CoordinationService.CategoryNode.HEALTH_MONITOR, databaseNodeName, 5, TimeUnit.MINUTES);
if(lock != null){ if(lock != null){
return lock; return lock;