diff options
author | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2019-06-17 10:51:08 -0400 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2019-06-17 11:03:23 -0400 |
commit | 1a592b9e45f95d694fbf649f41304f148e3c40fe (patch) | |
tree | 90a9749a8b6e59e375b67206529d5118a7facf5e /common/src | |
parent | 984f8da49f06c1563b4496572c54147014aa94bb (diff) |
Changes related to eviction of connections
These are changes related to ASDC controller for evicting database
connection pool when ip address changes
Included other appllications such as requestDB, CatalogDB, API Handler,
BPMN, OpenstackAdapter etc
Change-Id: I9a96ea12fb0c10643a204a58d55360bebba326fe
Issue-ID: SO-2018
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
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()); + + } + +} |