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

Try to eager load Servlets at startup. #23970

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ public interface Wrapper extends Container {
*/
public void load() throws ServletException;

public void tryLoad() throws ServletException;


/**
* Remove the specified initialization parameter from this servlet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>())
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
loadOnStartupServlets.computeIfAbsent(loadOnStartup, e -> new ArrayList<>())
loadOnStartupServlets.computeIfAbsent(loadOnStartup, ArrayList::new)

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 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.

Copy link
Contributor

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

Copy link
Contributor

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 😄

.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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,23 @@ public synchronized void load() throws ServletException {
initServlet(instance);
}

@Override
public synchronized void tryLoad() throws ServletException {
try {
instance = loadServlet();
initServlet(instance);
} catch (Throwable t) {
instance = null;
available = 0l;
classLoadTime = 0;
loadTime = 0l;
instanceInitialized = false;

throw t;
}
}


/**
* Creates an instance of the servlet, if there is not already at least one initialized instance.
*/
Expand Down