-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
G11n java client [BUG 754] Performance impact on the very first L3 fetch for a non-supported locale #823
G11n java client [BUG 754] Performance impact on the very first L3 fetch for a non-supported locale #823
Conversation
https://sonarcloud.io/project/issues?id=jessiejuachon-java-client-g11n-java-client&issues=AXDG68NFL24bPpi-pS5s&open=AXDG68NFL24bPpi-pS5s https://sonarcloud.io/project/issues?id=jessiejuachon-java-client-g11n-java-client&issues=AXDG68IGL24bPpi-pS2r&open=AXDG68IGL24bPpi-pS2r Signed-off-by: Jessie <jessiejuachon@gmail.com>
Signed-off-by: Jessie <jessiejuachon@gmail.com>
…and return them upstream.
…t in order to get a 304 NOT MODIFIED http response
…ne if component locale's cache has expired
…ingleton into g11n-java-client
…using cacheExpiredTime config
…g file. This means max age from server will be used. Setting VIPCfg.cacheExpiredTime to 0 disables caching.
…t need to be maintained.
* @param fallbackLocalesIter The fallback locale queue to use in case of failure. If null, no locale fallback will be applied. | ||
*/ | ||
private MessageCacheItem getFallbackLocaleMessages(Iterator<Locale> fallbackLocalesIter) { | ||
if (!dto.getLocale().equals(ConstantsKeys.SOURCE) && fallbackLocalesIter != null && fallbackLocalesIter.hasNext()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to judge dto locale is same as the fallback locale?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary, but it is an improvement. If we do not check for dto.getLocale == fallback and it happens, it will just try to fetch for the dto locale again and will fail again, and move on to the next item in the queue. Nonetheless, I have added the check for optimization.
Moreover, I hsve removed the following restriction to allow falling back to SOURCE messages:
!dto.getLocale().equals(ConstantsKeys.SOURCE)
if (!locales.contains(zhLocale) && !zhLocale.toLanguageTag().equals("zh-Hans") && !zhLocale.toLanguageTag().equals("zh-Hant")) { | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!locales.contains(zhLocale) && !zhLocale.toLanguageTag().equals("zh-Hans") && !zhLocale.toLanguageTag().equals("zh-Hant")) { | |
return null; | |
} | |
if (preferredLocale.getLanguage().equals("zh")) { | |
Locale zhLocale = fmtToMappedLocale(preferredLocale); | |
if (!locales.contains(zhLocale)) { | |
String langTag = zhLocale.toLanguageTag(); | |
if (langTag.equals("zh-Hans") || langTag.equals("zh-Hant")) { | |
return null; | |
} | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand you want it to be more readable. There is incorrect logic in your suggestion though. It should be if (!langTag.equals("zh-Hans") && !langTag.equals("zh-Hant"))
in line 160. Please review the new code.
src/main/java/com/vmware/vipclient/i18n/util/LocaleUtility.java
Outdated
Show resolved
Hide resolved
// For any Chinese locale (zh-*) that is not supported (except for zh-Hans and zh-Hant), use the fallback locale even if "zh" is supported. | ||
if (preferredLocale.getLanguage().equals("zh")) { | ||
Locale zhLocale = fmtToMappedLocale(preferredLocale); | ||
if (!locales.contains(zhLocale)) { | ||
String langTag = zhLocale.toLanguageTag(); | ||
if (!langTag.equals("zh-Hans") && !langTag.equals("zh-Hant")) | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// For any Chinese locale (zh-*) that is not supported (except for zh-Hans and zh-Hant), use the fallback locale even if "zh" is supported. | |
if (preferredLocale.getLanguage().equals("zh")) { | |
Locale zhLocale = fmtToMappedLocale(preferredLocale); | |
if (!locales.contains(zhLocale)) { | |
String langTag = zhLocale.toLanguageTag(); | |
if (!langTag.equals("zh-Hans") && !langTag.equals("zh-Hant")) | |
return null; | |
// With Chinese locale which is not configured/supported in web.xml, it | |
// will return 'en_US' as default to meet the usage custom of Chinese, | |
// e.g. for 'zh-HK' from client(browser) which is not | |
// configured/supported yet, it will return 'en_US'; | |
// Other locale, like 'de-DE' 'ja-JP' etc., | |
// it will return 'de' 'ja'(main/parent language). | |
if (bestMatch != null && bestMatch.getLanguage().equals("zh")) { | |
if (!locales.contains(localeObject)) { | |
return null; | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Xiaochao8 , this most recent suggested change fails for test LocaleTest.testPickupLocaleFromListZh.
Isn't it supposed to return "zh" for any non-supported Chinese locale EXCEPT for zh-Hans/zh-Hant/zh-TW/zh-CN/zh-hans-CN/zh-Hant-TW?
I am not familiar with custom usage of Chinese, and so I will rely on you. I will make the suggested change and also update the test . Please double-check the expected behavior for me.
Co-authored-by: Xiaochao Li <48587632+Xiaochao8@users.noreply.github.com>
src/main/java/com/vmware/vipclient/i18n/messages/service/ComponentService.java
Show resolved
Hide resolved
this.doLocaleMatching(); | ||
|
||
CacheService cacheService = new CacheService(dto); | ||
MessageCacheItem cacheItem = cacheService.getCacheOfComponent(); | ||
if (cacheItem != null) { // Item is in cache | ||
if (cacheItem.isExpired()) | ||
refreshCacheItemTask(cacheItem); // Refresh the cacheItem in a separate thread | ||
} else { // Item is not in cache. | ||
ProductService ps = new ProductService(dto); | ||
if (!ps.isSupportedLocale(Locale.forLanguageTag(dto.getLocale()))) { // Requested locale is not supported | ||
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new LinkedList<>(ps.getSupportedLocales()), Locale.forLanguageTag(dto.getLocale())); | ||
if (ps.isSupportedLocale(matchedLocale)) { // Requested locale matches a supported locale (eg. requested locale "fr_CA matches supported locale "fr") | ||
MessagesDTO matchedLocaleDTO = new MessagesDTO(dto.getComponent(), matchedLocale.toLanguageTag(), dto.getProductID(), dto.getVersion()); | ||
return new ComponentService(matchedLocaleDTO).getMessages(); | ||
} | ||
} | ||
Locale locale = Locale.forLanguageTag(dto.getLocale()); | ||
if (ps.isSupportedLocale(locale) || VIPCfg.getInstance().isPseudo()) { | ||
cacheItem = createCacheItem(); // Fetch for the requested locale from data store, create cacheItem and store in cache | ||
if (cacheItem.getCachedData().isEmpty()) // Failed to fetch messages for the requested locale | ||
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter); | ||
} else // Requested locale is not supported and does not match any supported locales | ||
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter); | ||
} | ||
return cacheItem; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest code for this method, then doLocaleMatching can be removed:
Locale matchedLocale = LocaleUtility.pickupLocaleFromList(new ProductService(dto).getSupportedLocales(),
Locale.forLanguageTag(dto.getLocale()));
MessageCacheItem cacheItem = null;
if (matchedLocale != null) {
dto.setLocale(matchedLocale.toLanguageTag());
cacheItem = new CacheService(dto).getCacheOfComponent();
if (cacheItem != null) { // Item is in cache
if (cacheItem.isExpired())
refreshCacheItemTask(cacheItem); // Refresh the cacheItem in a separate thread
} else { // Item is not in cache.
cacheItem = createCacheItem(); // Fetch for the requested locale from data store, create cacheItem and store in cache
}
}
if (cacheItem == null || cacheItem.getCachedData().isEmpty()) // Failed to fetch messages for the requested locale
cacheItem = getFallbackLocaleMessages(fallbackLocalesIter);
return cacheItem;
No description provided.