We had an issue today with contention on the
RegexCache in of our services. If you get past the 100 max size, it can become quite a bottleneck on throughput. I was wondering if a PR to make this configurable in some way would be welcome. Ideally, I would add a service loader mechanism to allow the user to pass a more robust (lock less) LRU implementation such as a caffeine cache. Environment variables or Dargs are also possible, but less appealing. Static swap out methods could also work, but are a bit messy. Here's a sample of our thread dump analysis for one of the impacted services.
⚠️ Lock Contention Detected ⚠️
Found 1 lock(s) with threads in BLOCKED state:
Lock 0x00000006c74d0480
Threads waiting for lock: 81
Lock owner: dw-529 - GET [Redacted]
Lock Owner's Stack Trace
at java.util.LinkedHashMap.get(java...@21.0.7/LinkedHashMap.java:539)<
at com.google.i18n.phonenumbers.internal.RegexCache$LRUCache.get(RegexCache.java:67)
- locked <0x00000006c74d0480> (a com.google.i18n.phonenumbers.internal.RegexCache$LRUCache)
at com.google.i18n.phonenumbers.internal.RegexCache.getPatternForRegex(RegexCache.java:36)
at com.google.i18n.phonenumbers.internal.RegexBasedMatcher.matchNationalNumber(RegexBasedMatcher.java:46)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isNumberMatchingDesc(PhoneNumberUtil.java:2361)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberTypeHelper(PhoneNumberUtil.java:2295)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isValidNumberForRegion(PhoneNumberUtil.java:2407)
at com.hubspot.phone.number.utils.FormattedPhoneNumber.validateRegion(FormattedPhoneNumber.java:124)
Example waiting thread: dw-477 - GET [Redacted]
Waiting Thread Stack Trace
at com.google.i18n.phonenumbers.internal.RegexCache$LRUCache.get(RegexCache.java:67)
- waiting to lock <0x00000006c74d0480> (a com.google.i18n.phonenumbers.internal.RegexCache$LRUCache)
at com.google.i18n.phonenumbers.internal.RegexCache.getPatternForRegex(RegexCache.java:36)
at com.google.i18n.phonenumbers.internal.RegexBasedMatcher.matchNationalNumber(RegexBasedMatcher.java:46)
at com.google.i18n.phonenumbers.PhoneNumberUtil.isNumberMatchingDesc(PhoneNumberUtil.java:2361)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberTypeHelper(PhoneNumberUtil.java:2270)
at com.google.i18n.phonenumbers.PhoneNumberUtil.getNumberType(PhoneNumberUtil.java:2262)
at com.hubspot.phone.number.utils.FormattedPhoneNumber.<init>(FormattedPhoneNumber.java:81)