aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/onap/so/db/connections/ScheduledDnsLookup.java
blob: 725da97d5b275b835a941ba1f9b3dbad3c2bee0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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.onap.logging.filter.base.ErrorCode;
import org.onap.logging.filter.base.ScheduledLogging;
import org.onap.logging.filter.base.ScheduledTaskException;
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);

    @ScheduledLogging
    @Scheduled(fixedRate = 15000)
    public void performDnsLookup() throws ScheduledTaskException {
        String dnsUrl = System.getenv(DB_HOST);
        try {
            if (dnsUrl == null) {
                throw new ScheduledTaskException(ErrorCode.DataError,
                        "Database DNS is not provided. Please verify the configuration");
            }

            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);
        }
    }
}