-
Notifications
You must be signed in to change notification settings - Fork 143
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
Try to eager load Servlets at startup. #23970
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4977,37 +4977,58 @@ public boolean alternateResourcesStop() { | |
*/ | ||
public void loadOnStartup(Container children[]) throws LifecycleException { | ||
// Collect "load on startup" servlets that need to be initialized | ||
Map<Integer, List<Wrapper>> map = new TreeMap<>(); | ||
Map<Integer, List<Wrapper>> loadOnStartupServlets = new TreeMap<>(); | ||
List<Wrapper> nonLoadOnStartupServlets = new ArrayList<>(); | ||
|
||
for (Container aChildren : children) { | ||
Wrapper wrapper = (Wrapper) aChildren; | ||
|
||
int loadOnStartup = wrapper.getLoadOnStartup(); | ||
if (loadOnStartup < 0) { | ||
continue; | ||
nonLoadOnStartupServlets.add(wrapper); | ||
} else { | ||
loadOnStartupServlets.computeIfAbsent(loadOnStartup, e -> new ArrayList<>()) | ||
.add(wrapper); | ||
} | ||
} | ||
|
||
Integer key = loadOnStartup; | ||
List<Wrapper> list = map.get(key); | ||
if (list == null) { | ||
list = new ArrayList<>(); | ||
map.put(key, list); | ||
// Combine the load on startup and non load on startup in one list, with the | ||
// latter loading after the ones with an explicit priority (load level). | ||
List<Wrapper> allServlets = new ArrayList<>(); | ||
for (List<Wrapper> samePriorityServlets : loadOnStartupServlets.values()) { | ||
for (Wrapper wrapper : samePriorityServlets) { | ||
allServlets.add(wrapper); | ||
} | ||
list.add(wrapper); | ||
} | ||
|
||
// Load the collected "load on startup" servlets | ||
for (List<Wrapper> list : map.values()) { | ||
for (Wrapper wrapper : list) { | ||
try { | ||
wrapper.load(); | ||
} catch (ServletException e) { | ||
getServletContext().log( | ||
format(rb.getString(SERVLET_LOAD_EXCEPTION), neutralizeForLog(getName())), | ||
StandardWrapper.getRootCause(e)); | ||
for (Wrapper wrapper : allServlets) { | ||
try { | ||
wrapper.load(); | ||
} catch (ServletException e) { | ||
getServletContext().log( | ||
format(rb.getString(SERVLET_LOAD_EXCEPTION), neutralizeForLog(getName())), | ||
StandardWrapper.getRootCause(e)); | ||
|
||
// NOTE: load errors (including a servlet that throws | ||
// UnavailableException from the init() method) are NOT | ||
// fatal to application startup | ||
throw new LifecycleException(StandardWrapper.getRootCause(e)); | ||
} | ||
} | ||
|
||
if (Boolean.parseBoolean(System.getProperty("glassfish.try.preload.servlets", "true"))) { | ||
// Also load the other servlets, which is one way to pass the CDI TCK, specifically | ||
// ContainerEventTest#testProcessInjectionTargetEventFiredForServlet and adhere to the rule | ||
// that injection points for Servlets have to be processed during start. | ||
for (Wrapper wrapper : nonLoadOnStartupServlets) { | ||
try { | ||
wrapper.tryLoad(); | ||
} catch (Throwable t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One day I will remove all these dangerous "catch-all" blocks. ;-) |
||
// Only log errors, don't fail anything. | ||
getServletContext().log( | ||
format(rb.getString(SERVLET_LOAD_EXCEPTION), neutralizeForLog(getName())), | ||
t); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 think arraylist::new will not work, as the key will be input into the ctor. With the key being an integer and the ctor setting the size, they will cause an out of memory with the huge key values being used.
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.
Wow, Arjan is right, I had the same idea, we discussed it in a chat and I started googling:
https://community.sonarsource.com/t/java-bug-using-collectors-tomap-with-arraylist-new/7661
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'm happy to learn it now and not from the GF bug 😄