spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Using devtools with Reactive application results in slower restarts

Open shelajev opened this issue 3 years ago • 8 comments

When I use devtools reloading on a reactive application (webflux) it results in much slower restart times.

Minimal reproducer is here It's a sample application returning a string response.

I don't know how to trigger devtools properly from the command line, so I do it from the IDE by changing a String value in the controller class.

You can run the app with: ./mvnw spring-boot:run and opening http://localhost:8080/greeting/Oleg.

The app starts very fast -- on my machine Started DemoApplication in 0.65 seconds (JVM running for 0.992).

When I trigger a reload, the app is stopping for 2s during the reload process. In the debug logs I see:

2022-01-21 21:15:15.545 DEBUG 96273 --- [       Thread-5] o.s.boot.devtools.restart.Restarter      : Stopping application
2022-01-21 21:15:15.545 DEBUG 96273 --- [       Thread-5] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed from ACCEPTING_TRAFFIC to REFUSING_TRAFFIC
2022-01-21 21:15:15.545 DEBUG 96273 --- [       Thread-5] onfigReactiveWebServerApplicationContext : Closing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@5a555b15, started on Fri Jan 21 21:14:48 EET 2022
2022-01-21 21:15:15.550 DEBUG 96273 --- [       Thread-5] ySourcesPropertyResolver$DefaultResolver : Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
2022-01-21 21:15:17.691 DEBUG 96273 --- [       Thread-5] o.s.b.d.r.c.RestartClassLoader           : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@63aaaaf3

Closing at 15.545 and created new classloader at 15:17.691.

Affecter SpringBoot version is 2.6.3, it also reproduces on 3.0.0-M1.

shelajev avatar Jan 21 '22 19:01 shelajev

Thanks very much for the sample, @shelajev. This is caused by the default two second shutdown quiet period of the ReactorResourceFactory. It can be set to zero using a system property -Dreactor.netty.ioShutdownQuietPeriod=0. This removes the two second delay:

2022-01-21 20:22:53.681 DEBUG 83595 --- [       Thread-8] onfigReactiveWebServerApplicationContext : Closing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@4e479084, started on Fri Jan 21 20:22:44 GMT 2022
2022-01-21 20:22:53.730 DEBUG 83595 --- [       Thread-8] o.s.b.d.r.c.RestartClassLoader           : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2cc4e52b

wilkinsona avatar Jan 21 '22 20:01 wilkinsona

Can this property be set to 0 (or a very small value close to 0, like 5ms) when devtools reload is enabled?

shelajev avatar Jan 22 '22 19:01 shelajev

I can confirm that with the property specified I see no delay in reloading.

shelajev avatar Jan 22 '22 19:01 shelajev

Can this property be set to 0 (or a very small value close to 0, like 5ms) when devtools reload is enabled?

Yes, I think we can have better defaults for this. We'll keep this one open to investigate that.

philwebb avatar Jan 24 '22 18:01 philwebb

I found two ways of doing this:

  • Use a BeanPostProcessor to call setShutdownQuietPeriod(Duration.ZERO); on all ReactorResourceFactory beans
  • Use a ApplicationContextInitializer to set the system property reactor.netty.ioShutdownQuietPeriod to 0.

Which one would be the preferred solution? You can see both ways here: https://github.com/mhalbritter/spring-boot/commit/cda49b602845afec4033a032dcba32aa3ba2f3e4

mhalbritter avatar Jan 17 '23 10:01 mhalbritter

Thanks for looking at this, @mhalbritter.

It's unfortunate that we can't set it by adding an entry to devtools-property-defaults.properties. If setting the quiet period is useful, and it appears that it is, I wonder if we should add a configuration property for it that's applied by the auto-configuration and can then be set to 0 by DevTools.

wilkinsona avatar Jan 17 '23 10:01 wilkinsona

Ah, good idea. I'll look into this.

mhalbritter avatar Jan 17 '23 10:01 mhalbritter

Created a PR: https://github.com/spring-projects/spring-boot/pull/33855

Not sure if we should release this as a patch release.

mhalbritter avatar Jan 17 '23 12:01 mhalbritter

Superseded by #33855.

mhalbritter avatar Feb 01 '23 08:02 mhalbritter