Class JdbcNodeServiceAuditor

java.lang.Object
net.solarnetwork.central.common.dao.jdbc.JdbcNodeServiceAuditor
All Implemented Interfaces:
NodeServiceAuditor, net.solarnetwork.service.PingTest, net.solarnetwork.service.ServiceLifecycleObserver
Direct Known Subclasses:
JdbcUserServiceAuditor

public class JdbcNodeServiceAuditor extends Object implements NodeServiceAuditor, net.solarnetwork.service.PingTest, net.solarnetwork.service.ServiceLifecycleObserver
JDBC based implementation of NodeServiceAuditor.

This service coalesces updates per node/service/hour in memory and flushes these to the database via a single "writer" thread after a small delay. This design is meant to support better throughput of audit updates, but has the potential to drop some count values if the service is restarted.

  • Field Details

    • DEFAULT_UPDATE_DELAY

      public static final long DEFAULT_UPDATE_DELAY
      The default value for the updateDelay property.
      See Also:
    • DEFAULT_FLUSH_DELAY

      public static final long DEFAULT_FLUSH_DELAY
      The default value for the flushDelay property.
      See Also:
    • DEFAULT_CONNECTION_RECOVERY_DELAY

      public static final long DEFAULT_CONNECTION_RECOVERY_DELAY
      The default value for the connecitonRecoveryDelay property.
      See Also:
    • DEFAULT_NODE_SERVICE_INCREMENT_SQL

      public static final String DEFAULT_NODE_SERVICE_INCREMENT_SQL
      The default value for the nodeServiceIncrementSql property.
      See Also:
    • CALLABLE_STATEMENT_REGEX

      public static final Pattern CALLABLE_STATEMENT_REGEX
      A regular expression that matches if a JDBC statement is a CallableStatement.
    • log

      protected final org.slf4j.Logger log
      A class-level logger.
  • Constructor Details

    • JdbcNodeServiceAuditor

      public JdbcNodeServiceAuditor(DataSource dataSource)
      Constructor.
      Parameters:
      dataSource - the JDBC DataSource
      Throws:
      IllegalArgumentException - if any argument is null
    • JdbcNodeServiceAuditor

      public JdbcNodeServiceAuditor(DataSource dataSource, ConcurrentMap<net.solarnetwork.domain.datum.DatumId,AtomicInteger> nodeServiceCounters, Clock clock, net.solarnetwork.util.StatTracker statCounter)
      Constructor.
      Parameters:
      dataSource - the JDBC DataSource
      nodeServiceCounters - the node source counters map
      clock - the clock to use
      Throws:
      IllegalArgumentException - if any argument is null
  • Method Details

    • serviceDidStartup

      public void serviceDidStartup()
      Specified by:
      serviceDidStartup in interface net.solarnetwork.service.ServiceLifecycleObserver
    • serviceDidShutdown

      public void serviceDidShutdown()
      Specified by:
      serviceDidShutdown in interface net.solarnetwork.service.ServiceLifecycleObserver
    • getAuditClock

      public Clock getAuditClock()
      Description copied from interface: NodeServiceAuditor
      Get the clock used for auditing.

      This clock may bucket time into discreet intervals.

      Specified by:
      getAuditClock in interface NodeServiceAuditor
      Returns:
      the clock never null
    • auditNodeService

      public void auditNodeService(Long nodeId, String service, int count)
      Description copied from interface: NodeServiceAuditor
      Audit the creation of an instruction for a node.
      Specified by:
      auditNodeService in interface NodeServiceAuditor
      Parameters:
      nodeId - the node ID
      service - the service name
      count - the amount to add
    • reconnectWriter

      public void reconnectWriter()
      Cause the writing thread to re-connect to the database with a new connection.
    • enableWriting

      public void enableWriting()
      Enable writing, and wait until the writing thread is going.
    • disableWriting

      public void disableWriting()
      Disable writing.
    • getPingTestId

      public String getPingTestId()
      Specified by:
      getPingTestId in interface net.solarnetwork.service.PingTest
    • getPingTestName

      public String getPingTestName()
      Specified by:
      getPingTestName in interface net.solarnetwork.service.PingTest
    • getPingTestMaximumExecutionMilliseconds

      public long getPingTestMaximumExecutionMilliseconds()
      Specified by:
      getPingTestMaximumExecutionMilliseconds in interface net.solarnetwork.service.PingTest
    • performPingTest

      public net.solarnetwork.service.PingTest.Result performPingTest() throws Exception
      Specified by:
      performPingTest in interface net.solarnetwork.service.PingTest
      Throws:
      Exception
    • setFlushDelay

      public void setFlushDelay(long flushDelay)
      Set the delay, in milliseconds, between flushing cached audit data.
      Parameters:
      flushDelay - the delay, in milliseconds; defaults to DEFAULT_FLUSH_DELAY
      Throws:
      IllegalArgumentException - if flushDelay is < 0
    • setConnectionRecoveryDelay

      public void setConnectionRecoveryDelay(long connectionRecoveryDelay)
      Set the delay, in milliseconds, to wait after a JDBC connection error before trying to recover and connect again.
      Parameters:
      connectionRecoveryDelay - the delay, in milliseconds; defaults t[ DEFAULT_CONNECTION_RECOVERY_DELAY
      Throws:
      IllegalArgumentException - if connectionRecoveryDelay is < 0
    • setUpdateDelay

      public void setUpdateDelay(long updateDelay)
      Set the delay, in milliseconds, to wait after executing JDBC statements within a loop before executing another statement.
      Parameters:
      updateDelay - the delay, in milliseconds; defaults t[ DEFAULT_UPDATE_DELAY
      Throws:
      IllegalArgumentException - if updateDelay is < 0
    • setNodeServiceIncrementSql

      public void setNodeServiceIncrementSql(String sql)
      The JDBC statement to execute for incrementing a count for a single date, node, and source.

      The statement must accept the following parameters:

      1. long - the node ID
      2. string - the service name
      3. timestamp - the audit date
      4. integer - the instruction count to add
      Parameters:
      sql - the SQL statement to use; defaults to
      invalid reference
      #DEFAULT_NODE_SOURCE_INCREMENT_SQL
    • setStatLogUpdateCount

      public void setStatLogUpdateCount(int statLogUpdateCount)
      Set the statistic log update count.

      Setting this to something greater than 0 will cause INFO level statistic log entries to be emitted every statLogUpdateCount records have been updated in the database.

      Parameters:
      statLogUpdateCount - the update count; defaults to
      invalid reference
      #DEFAULT_STAT_LOG_UPDATE_COUNT