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 code clean up - remove logic of adding a cacheItem with an empty dataMap to cache for non-supported locales #807

Merged
merged 211 commits into from
Oct 13, 2020

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
Copy link
Contributor Author

jessiejuachon commented Sep 25, 2020

@lyiyu66 , tagging you just for your information.

@jessiejuachon
Copy link
Contributor Author

This is just the first step (code clean up) in solving 754. Here, I have removed the logic of adding a cacheItem with an empty dataMap to the cache for non-supported locales. We do not need this anymore because we are now first getting the list of supported locales to check if a locale is supported or not.

Next step is to use the fallback locale immediately to avoid fetch from service. This will be in another PR.

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(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems recursive call is not good.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Sep 28, 2020

Choose a reason for hiding this comment

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

There is no recursive call in this line. Notice that getMessages is invoked on a separate instance of ComponentService. Could you be more specific as to what you mean by "not good"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, this logic (lines 110-117) is not new. I just moved it out of the createCacheItem method that is called in line 120 into here for code clean up.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant line 115. 'not good' means that it gets the messages twice: one for supported locale, another or not supported locale. The logic is duplicated, the locale should be handled before call getMessages.

Secondly, when i read 'return new ComponentService(matchedLocaleDTO).getMessages(null);', i don't get what it means by passing a null value.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Sep 29, 2020

Choose a reason for hiding this comment

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

Reminder: This code has been there before this PR. It was for a different issue (#781), so this comment is out of the scope of this PR. Please do not let it block this PR.

The intention of this line is exactly what you mentioned. It is not a duplicate. If requested locale is not found in cache, we want to get a matched locale, if any, instead of the actual requested locale. It is the same strategy for getting messages for a fallback locale: new ComponentService(fallbackLocaleDTO).getMessages().
Part of the reason this looks messy is because the locale is currently part of the DTO. This is very old code that I did not want to change especially not in this PR as it is out of scope. In the future, I want to pass the locale to getMessages instead of having it in the DTO. If you do want to track this future improvement, please open another issue, because this PR is for code clean up only, not for changing any logic.

Secondly, passing a null here is unnecessary. I have changed it to getMessages().

huihuiw01
huihuiw01 previously approved these changes Sep 29, 2020
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(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's obscure to pass null to getMessages. Hard to understand.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Sep 29, 2020

Choose a reason for hiding this comment

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

First, this has been there since before this PR, so based on suggestions from retrospective, we should open another issue to address this specific concern. This PR is for code clean up of caching an empty datamap only.

To understand the behavior when passing null to getMessages, you can read the javadoc of getMessages:

 * @param fallbackLocalesIter The locale fallback queue to be used on failure. If null, there will be no fallback mechanism on failure so the message map will be empty.

I have logged the issue with description here: #811

}
}

//Create and store cacheItem for the requested locale
cacheItem = createCacheItem(fallbackLocalesIter);
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 pass fallbackLocalesIter? I think createCacheItem shouldn't be affected by fallbackLocalesIter.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Sep 29, 2020

Choose a reason for hiding this comment

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

You have already reviewed this code a long time ago though. Yes, it is necessary for now because I don't want to change any code logic in this PR. Please revisit this part in next PR.

Comment on lines +111 to +113
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")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't imagine getSupportedLocales is invoked three times in three code lines.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Sep 29, 2020

Choose a reason for hiding this comment

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

Where? It is only directly invoked in line 112. Do you mean ps.isSupportedLocale? Please clarify your comment (I don't understand what you can't imagine 😄 )

Note that there is formatting logic in ps.isSupportedLocale. For "cleaner" code, I put it in a separate method instead of having to call LocaleUtility.fmtToMappedLocale (which developers may forget to do in the future):

Set<Locale> supportedLocales = getSupportedLocales();
supportedLocales.contains(LocaleUtility.fmtToMappedLocale(...))

Let me know if you have abetter suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can improve the LocaleUtility.pickupLocaleFromList to get enough information by only one call. Picking up once to get the supported locale or empty locale.

Copy link
Contributor Author

@jessiejuachon jessiejuachon Oct 5, 2020

Choose a reason for hiding this comment

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

Ahhh yes! I agree. I already have that lined up. However, as I said, this PR is only to clean up / remove any code related to adding an empty data set into the cache.

Speaking of, I am also removing "locale" property from MessageCacheItem, so more changes have been added to this PR. This is actually related to this code clean up because "locale" was added for the purposes of adding an empty data map into cache for non-supported locales (which we are not doing anymore).

…nentService.java

Co-authored-by: Xiaochao Li <48587632+Xiaochao8@users.noreply.github.com>
@jessiejuachon jessiejuachon added the kind/enhancement Enhancement to existing features label Sep 29, 2020
@jessiejuachon jessiejuachon removed the request for review from linr211 October 5, 2020 21:24
@jessiejuachon
Copy link
Contributor Author

@Xiaochao8 , @huihuiw01 , please focus on the intent of the PR. There are already 12 files modified which is quite a lot, so please refrain from discussing changes that are not related to the PR's intent (i.e. clean up code that is related to caching an empty data map for non-supported locales).

@jessiejuachon jessiejuachon merged commit 370e4e0 into vmware:g11n-java-client Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] g11n-java-client - Performance impact on the very first L3 fetch for a non-supported locale
5 participants