Class HikariStatusProvider

java.lang.Object
com.github.jpmsilva.jsystemd.DelayableHealthProvider
com.github.jpmsilva.jsystemd.spring.HikariStatusProvider
All Implemented Interfaces:
HealthProvider, StatusProvider, SmartInitializingSingleton

public class HikariStatusProvider extends DelayableHealthProvider implements StatusProvider, SmartInitializingSingleton
Provides information regarding the HikariCP connection pools of the application: as a StatusProvider, for each pool, named after its data source bean, the connections in use out of the most the pool opens, and the threads waiting for a connection, such as dataSource: 7/10 connections, 3 waiting, and as a HealthProvider, for the systemd watchdog, reporting the application as unhealthy while a pool is starved, if enabled.

The pools are those of the DataSource beans that are, or wrap, a HikariDataSource, found once all singleton beans are created, so that lazy ones are not created. A data source whose pool is not started yet, as Hikari only starts it on the first connection, is left out until then.

The health is enabled on its own, as it lets systemd restart the application: while disabled, the application is always healthy. When enabled, it is unhealthy while any pool is starved: all its connections are in use, and threads are waiting for one, naming the starved data sources in the health details, under starvedDataSources. A starved pool usually means connections leak, as code that takes a connection never gives it back, which only a restart recovers from. As a burst of requests can also starve a pool for a while, the unhealthy state should be delayed, with DelayableHealthProvider.setUnhealthyDelay(java.time.Duration): the pool must then stay starved, every time the health is evaluated, for that long.

A database that cannot be reached does not starve the pools: their threads wait for a connection, but the connections are not in use, as the pools cannot open them. That is left for the health indicators of Spring Boot Actuator.

Author:
Joao Silva
  • Constructor Details

    • HikariStatusProvider

      public HikariStatusProvider(ConfigurableListableBeanFactory beanFactory, boolean health)
      Creates a new instance that finds the data sources in the given bean factory.
      Parameters:
      beanFactory - the bean factory to find the data sources in
      health - true to report the application as unhealthy while any pool is starved
  • Method Details

    • afterSingletonsInstantiated

      public void afterSingletonsInstantiated()
      Finds the HikariCP connection pools among the data source beans that are already created.
      Specified by:
      afterSingletonsInstantiated in interface SmartInitializingSingleton
    • status

      public String status()
      Description copied from interface: StatusProvider
      Exposes status information to send to systemd.
      Specified by:
      status in interface StatusProvider
      Returns:
      the string message to add to the service status
    • evaluateHealth

      protected HealthProvider.Health evaluateHealth()
      Provides the health of the application, without the delay: unhealthy while any pool is starved, if enabled.
      Specified by:
      evaluateHealth in class DelayableHealthProvider
      Returns:
      the health of the application