Skip to content
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

Merged

Conversation

jessiejuachon
Copy link
Contributor

No description provided.

jessiejuachon and others added 30 commits March 10, 2020 17:00
Signed-off-by: Jessie <jessiejuachon@gmail.com>
…t in order to get a 304 NOT MODIFIED http response
…g file. This means max age from server will be used. Setting VIPCfg.cacheExpiredTime to 0 disables caching.
@jessiejuachon jessiejuachon marked this pull request as ready for review October 14, 2020 21:46
* @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()) {
Copy link
Contributor

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?

Copy link
Contributor Author

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)

Comment on lines 152 to 154
if (!locales.contains(zhLocale) && !zhLocale.toLanguageTag().equals("zh-Hans") && !zhLocale.toLanguageTag().equals("zh-Hant")) {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}
}
}

Copy link
Contributor Author

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.

Comment on lines 149 to 155
// 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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;
}
}

Copy link
Contributor Author

@jessiejuachon jessiejuachon Oct 20, 2020

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.

jessiejuachon and others added 2 commits October 20, 2020 10:00
Co-authored-by: Xiaochao Li <48587632+Xiaochao8@users.noreply.github.com>
Xiaochao8
Xiaochao8 previously approved these changes Oct 21, 2020
Comment on lines +104 to +121
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;
Copy link
Contributor

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;

@jessiejuachon jessiejuachon merged commit 9a80266 into vmware:g11n-java-client Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants