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

Remove classpath scanning to improve JerseyService startup speed #10005

Merged
merged 10 commits into from Feb 22, 2021

Conversation

bernd
Copy link
Member

@bernd bernd commented Feb 2, 2021

This reduces the startup time of the JerseyService by 83% on my development machine when running the production artifacts.
(3.5s vs 20.5s)

Since scanning package paths for resources is pretty slow in Jersey, we now register all REST resources explicitly via addSystemRestResource() in the guice bindings. This will also allow us to disable resources via feature flags and runtime profiles in the future.

I compared the output of the PrintModelProcessor with both implementations and we get the same resources registered with the new one. (tested in a production artifact deployment including enterprise plugins)

In addition to the resource changes I also had to adjust the full-backend tests to get rid of a race condition that was shadowed by the slow server startup before.

This reduces the startup time of the JerseyService by 83% on my
development machine when running the production artifacts.
(3.5s vs 20.5s)

Since scanning package paths for resources is pretty slow in Jersey,
this replaces the usage of ResourceConfig#packages with
ResourceConfig#registerClasses.

We now scan for resource classes by using the classgraph library.
@bernd bernd added this to the 4.1.0 milestone Feb 2, 2021
With the server startup now being way faster, we run into a race
condition where we run the full-backend tests before we calculated the
index ranges. That fails any test that is searching for messages.

- We now wait for the ES container to be started before starting the
  Graylog node
- We now wait until the index ranges for the default index have been
  calculated before declaring the Graylog node as started
@bernd
Copy link
Member Author

bernd commented Feb 2, 2021

@dennisoelkers @mpfz0r @thll The server startup is now so fast that the full-backend tests failed due to a race condition. 😂 I pushed a fix for that. (see: 832cfd6)

@thll thll self-assigned this Feb 4, 2021
@bernd bernd marked this pull request as draft February 4, 2021 10:02
@thll
Copy link
Contributor

thll commented Feb 4, 2021

Code looks good and I didn't find any issues when trying it out 👍

I would approve the PR but as we've started a discussion about abandoning the scanning and start binding resource classes explicitly, I'll leave it be for now.

@thll thll removed their assignment Feb 4, 2021
@bernd bernd changed the title Use classgraph library to improve JerseyService startup speed Remove classpath scanning to improve JerseyService startup speed Feb 11, 2021
@bernd
Copy link
Member Author

bernd commented Feb 11, 2021

@dennisoelkers @mpfz0r @thll I updated the PR to replace package scanning with explicit resource bindings.

@bernd bernd marked this pull request as ready for review February 11, 2021 13:33
@dennisoelkers dennisoelkers self-assigned this Feb 11, 2021
@bernd bernd marked this pull request as draft February 11, 2021 14:19
@bernd
Copy link
Member Author

bernd commented Feb 11, 2021

This needs more work. The API browser is broken with this because it's also doing package scanning.

This makes the API documentation browser work with the explicitly bound
resource classes we now use.
@bernd bernd marked this pull request as ready for review February 12, 2021 09:26
@bernd
Copy link
Member Author

bernd commented Feb 12, 2021

@dennisoelkers @mpfz0r @thll Okay, this should be ready for review again. I adjusted the API documentation browser to use the list of resource classes instead of doing package scanning.

*
* @param restResourceClass the resource to add
*/
protected void addSystemRestResource(Class<?> restResourceClass) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to extend Graylog2Module with something like CoreModule that PluginModule does not extend from and put addSystemResource in there? I am trying to avoid that plugin authors make use of this methods because it's available and e.g. suggested in the IDE.

Copy link
Member Author

@bernd bernd Feb 18, 2021

Choose a reason for hiding this comment

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

Yeah, that's a good point.

PluginModule currently extends Graylog2Module and lots of modules in the server extend Graylog2Module. We also have several modules in the server which extend PluginModule.

So we would need to introduce a CoreModule that extends Graylog2Module and then switch all server modules, those that extend Graylog2Module and those that extend PluginModule, over to CoreModule to get access to the API resource binding helper. That means we probably have to push down some helpers from PluginModule to Graylog2Module, depending on what the server modules that currently extend PluginModule need.

Correct?

Sounds like a separate PR. 😄

Copy link
Member

Choose a reason for hiding this comment

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

Alright, I am fine with doing that in a separate PR.

@dennisoelkers dennisoelkers merged commit 8b952cb into master Feb 22, 2021
@dennisoelkers dennisoelkers deleted the improve-jersey-startup branch February 22, 2021 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants