Ali Druid is known as the best connection pool, and its powerful monitoring capabilities are an important feature we are looking for. But in the actual situation, there are many pits, let’s talk about a pit I recently encountered!

Fault 1: Error logs at the error level are constantly displayed

session ip change too many

The following is the key source of the error

com.alibaba.druid.support.http.stat.WebSessionStat#addRemoteAddress

public void addRemoteAddress(String ip) { if (remoteAddresses == null) { this.remoteAddresses = ip; return; } if (remoteAddresses.contains(ip)) { return; } if (remoteAddresses.length() > 256) { LOG.error("session ip change too many"); return; } remoteAddresses += '; ' + ip; }Copy the code

Let’s take a look at the Druid connection pool to get an IP address

com.alibaba.druid.util.DruidWebUtils

public static String getRemoteAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }

    return ip;
}
Copy the code

Analyze its source code

This is the session monitoring function of the Druid connection pool. It records all access IP addresses of the same session ID. When the length exceeds 256 characters, the error log is printed, but the actual function is not affected.

Saw the Druid session monitoring page, the same number of session request is not much, but there is a problem, record the IP will hold a request most multistage agency formed many blocks of IP (such as 192.168.1.2 instead, 192.168.1.3 192.168.1.4), So five or six requests would have stretched the access IP beyond the 256 length to print this error.

The solution

1. If session monitoring is not used, disable this function;

Github.com/alibaba/dru…

<init-param>
  <param-name>sessionStatEnable</param-name>
  <param-value>false</param-value>
</init-param>
Copy the code

2, modify the source code, if there are multiple IP, intercept the first paragraph, and modify the record access IP (256 bits) length;

The author went to see Ali’s latest bag, and the problem remains.

[image upload failed… (image-d1e9AE-1518428788548)]

The Druid official error report on Github also has the same problem. Ali has no intention of fixing it, so we have temporarily disabled the session monitoring function.

Problem 2: The DruidStatView class is abnormal

java.util.ConcurrentModificationException
    at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:394)
    at java.util.LinkedHashMap$ValueIterator.next(LinkedHashMap.java:409)
    at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1067)
    at com.alibaba.druid.support.http.stat.WebAppStat.getSessionStatDataList(WebAppStat.java:504)
    at com.alibaba.druid.support.http.stat.WebAppStatUtils.getSessionStatDataList(WebAppStatUtils.java:64)
    at com.alibaba.druid.support.http.stat.WebAppStatManager.getSessionStatData(WebAppStatManager.java:100)
    at com.alibaba.druid.stat.DruidStatService.getWebSessionStatDataList(DruidStatService.java:205)
    at com.alibaba.druid.stat.DruidStatService.service(DruidStatService.java:161)
    at com.alibaba.druid.support.http.StatViewServlet.process(StatViewServlet.java:162)
    at com.alibaba.druid.support.http.ResourceServlet.service(ResourceServlet.java:253)
Copy the code

Look at the source code, it is found that the session monitoring pit

Unable to ridicule..

[image upload failed… (image-2ea705-1518428788548)]

For loop again defined inside the Map, the change may have elements in other places, causing a ConcurrentModificationException anomalies.

So, session monitoring is finally turned off.

Curious, are ali engineers all that good? Or to be lazy?

Recommended reading


Resources: Ten stages of learning to become an architect!

Tutorials: The most powerful Spring Boot & Cloud tutorials ever

Tools: Recommended an online creation flow chart, mind mapping software

Scan and follow our wechat official account, reply “666” to get a set of Java concurrent programming HD video tutorial.