diff options
author | Steve Smokowski <ss835w@att.com> | 2019-06-18 15:26:10 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-06-18 15:26:10 +0000 |
commit | 7c69f07593a8c7474bd7a69a65979dc0635f1526 (patch) | |
tree | 79ce2336642ada485244a830a5c79011eac54617 /common/src | |
parent | 703517112032025146a968db5c830c7389f5166f (diff) | |
parent | 1a592b9e45f95d694fbf649f41304f148e3c40fe (diff) |
Merge "Changes related to eviction of connections"
Diffstat (limited to 'common/src')
3 files changed, 117 insertions, 0 deletions
diff --git a/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java b/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java new file mode 100644 index 0000000000..8ee63a487b --- /dev/null +++ b/common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java @@ -0,0 +1,20 @@ +package org.onap.so.db.connections; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +@Component +@Scope("singleton") +public class DbDnsIpAddress { + + private String ipAddress; + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + +} diff --git a/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java b/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java new file mode 100644 index 0000000000..14f2f5e9b7 --- /dev/null +++ b/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java @@ -0,0 +1,78 @@ +package org.onap.so.db.connections; + +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Set; +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import com.zaxxer.hikari.HikariPoolMXBean; + +@Component +@Profile("!test") +public class ScheduledDnsLookup { + + private static final String JMX_HIKARI_DB_POOL_LOOKUP = "com.zaxxer.hikari:type=Pool (*,*"; + + private static final String DB_HOST = "DB_HOST"; + + @Autowired + private DbDnsIpAddress dnsIpAddress; + + private static Logger logger = LoggerFactory.getLogger(ScheduledDnsLookup.class); + + @Scheduled(fixedRate = 15000) + public void performDnsLookup() { + + String dnsUrl = System.getenv(DB_HOST); + + try { + if (dnsUrl == null) { + logger.error("Database DNS is not provided. Please verify the configuration"); + return; + } + + InetAddress inetAddress = java.net.InetAddress.getByName(dnsUrl); + String ipAddress = inetAddress.getHostAddress(); + String currentIpAddress = dnsIpAddress.getIpAddress(); + /* This is in initial state */ + if (currentIpAddress == null) { + dnsIpAddress.setIpAddress(ipAddress); + return; + } + + if ((ipAddress != null) && (!ipAddress.equalsIgnoreCase(currentIpAddress))) { + logger.debug("Switched Database IP Address from {} to {}", currentIpAddress, ipAddress); + softEvictConnectionPool(); + dnsIpAddress.setIpAddress(ipAddress); + } + } catch (UnknownHostException e) { + logger.warn("Database DNS %s is not resolvable to an IP Address", dnsUrl); + } + + } + + private void softEvictConnectionPool() { + try { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + ObjectName queryObj = new ObjectName(JMX_HIKARI_DB_POOL_LOOKUP); + Set<ObjectInstance> objects = mBeanServer.queryMBeans(queryObj, null); + for (ObjectInstance objectInstance : objects) { + ObjectName poolObject = objectInstance.getObjectName(); + HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolObject, HikariPoolMXBean.class); + logger.debug("database connection pool is soft evicted for connections"); + poolProxy.softEvictConnections(); + } + } catch (Exception e) { + logger.warn("Error encountered in evicting DB connection pool", e); + } + } +} diff --git a/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java b/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java new file mode 100644 index 0000000000..0dc35a054c --- /dev/null +++ b/common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java @@ -0,0 +1,19 @@ +package org.onap.so.db.connections; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class DbDnsIpAddressTest { + + @Test + public void test() { + final String expectedIpAddress = "10.0.75.1"; + + DbDnsIpAddress dbDnsIpAddress = new DbDnsIpAddress(); + dbDnsIpAddress.setIpAddress(expectedIpAddress); + + assertEquals(expectedIpAddress, dbDnsIpAddress.getIpAddress()); + + } + +} |