aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/pom.xml4
-rw-r--r--common/src/main/java/org/onap/so/db/connections/DbDnsIpAddress.java20
-rw-r--r--common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java78
-rw-r--r--common/src/test/java/org/onap/so/db/connections/DbDnsIpAddressTest.java19
4 files changed, 121 insertions, 0 deletions
diff --git a/common/pom.xml b/common/pom.xml
index 2742afee1a..316cad1da7 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -20,6 +20,10 @@
<dependencies>
<dependency>
+ <groupId>hikari-cp</groupId>
+ <artifactId>hikari-cp</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
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());
+
+ }
+
+}