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

daemon: Fix slow start with Ruby 3 #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

daipom
Copy link

@daipom daipom commented Feb 26, 2024

Description

Issues Resolved

Check List

Comparing the time to start a service

  • Current 2.3.2
    • Ave: 11.14 seconds
    • Data(seconds): [8.0, 17.0, 11.0, 11.0, 16.0, 9.0, 9.0, 15.0, 6.0, 9.0, 12.0, 6.0, 16.0, 9.0, 14.0, 9.0, 7.0, 12.0, 8.0, 9.0, 13.0, 9.0, 15.0, 11.0, 11.0, 8.0, 11.0, 22.0, 11.0, 7.0, 19.0, 11.0, 8.0, 12.0, 9.0]
  • This PR
    • Ave: 0.6982 seconds
    • Data(seconds): [0.6772, 0.7022, 0.7018, 0.7012, 0.718, 0.7113, 0.6792, 0.7016, 0.686, 0.6857, 0.6859, 0.7014, 0.701, 0.6794, 0.6861, 0.7019, 0.7171, 0.8173, 0.6702, 0.6874, 0.6868, 0.6859, 0.6873, 0.6861]

Env:

  • Ruby 3.2.2
  • Windows 10 Home 64bit
  • 4 core CPU, 8 logical processors
    • Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz 1.99GHz
  • 16GB RAM

Service code:

require "win32/daemon"

class TestService < Win32::Daemon
  def service_main
    while running?
      sleep 10
    end
  end
end

TestService.new.mainloop

Measurement steps are the same as #84.

This does not change any specification.
This just changes to use a Ruby thread instead of `CreateThread`.
It is very very fast to use a Ruby thread from the beginning.

Service startup is noticeably slower with Ruby 3.
(Please see chef#84).

Windows service needs to finish `StartServiceCtrlDispatcher` in
30 seconds by default.
With Ruby 3 or later, this problem can cause timeout errors.

The more CPUs, the slower it tends to be.
Maybe some implementation about threading has changed with Ruby 3 series.

The current implementation switches between multiple Ruby threads and
non-Ruby threads.
This complex process may be unsuitable for Ruby 3.
`CreateThread` makes a non-Ruby thread, and it calls Ruby Proc, and it
makes a Ruby thread, and ...

Signed-off-by: Daijiro Fukuda <[email protected]>
@daipom daipom requested review from a team as code owners February 26, 2024 09:00
Copy link

sonarcloud bot commented Feb 26, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

This was referenced Feb 26, 2024
@daipom
Copy link
Author

daipom commented Feb 27, 2024

Only applied this change (inserting sleep).

--- a/lib/win32/daemon.rb
+++ b/lib/win32/daemon.rb
@@ -265,6 +265,7 @@ module Win32
       events.put_pointer(FFI::Pointer.size, FFI::Pointer.new(@@hStartEvent))

       while (index = WaitForMultipleObjects(2, events, 0, 1000)) == WAIT_TIMEOUT
+        sleep 0.1
       end

       if index == WAIT_FAILED
  • Ave: 1.6871 seconds
  • Data(seconds): [1.7057, 1.7195, 1.6887, 1.6875, 1.6644, 1.657, 1.6802, 1.6723, 1.689, 1.7023, 1.6811, 1.6883, 1.7033, 1.6819, 1.6742, 1.6725, 1.6911, 1.6746, 1.7199, 1.6891]

I don't know why, but it's one second slower than this PR.

kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 7, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 11, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service", branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 12, 2024
Until chef/win32-service#85 is merged,

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 12, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to kenhys/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See fluent#618

Closes: fluent#630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]

Signed-off-by: Kentaro Hayashi <[email protected]>
kenhys added a commit to fluent/fluent-package-builder that referenced this pull request Mar 13, 2024
Until chef/win32-service#85 is merged,
use forked version of win32-service.

This fix should be applied to fluent-package not to block starting
fluentdwinsvc service on Windows.

See #618

Closes: #630

NOTE: even though just putting the following line
does not install forked version of win32-service, so
install it explicitly as same as fluentd gem.

```
  gem "win32-service", github: "fluent-plugins-nursery/win32-service",
  branch: "fluent-package", platforms: [:mingw, :x64_mingw]
```

Signed-off-by: Kentaro Hayashi <[email protected]>
@daipom
Copy link
Author

daipom commented Apr 8, 2024

We use our fork version including this patch for fluent-package. (Please see fluent/fluent-package-builder#618 for details.)

We have confirmed this patch dramatically stabilizes service startup time.

Screenshot from 2024-04-08 14-00-50

@tpowell-progress tpowell-progress self-assigned this Apr 9, 2024
@adfoster-r7
Copy link

As an additional datapoint - we were hitting these timeout issues after upgrading to Ruby 3.1 and running on AWS. Applying this patch has fixed the startup issues we were hitting - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow service start with Ruby 3
3 participants