Fix /healthz?js-enabled=true behavior

When js-enabled is set to true, the condition was only checked if
the `getJetStream()` call returned `nil`. However, if it non-nil,
all remaining checks were executed, including assessing the health
of the assets (streams and consumers).

This change addresses two issues:

- Switch to use `js.isEnabled()` which will check whether the value
  is nil OR `js.disabled = true` which can occur if the subsystem
  is temporarily disabled (insufficient resources).
- Correctly exit the check after the assertion and before meta and
  asset checks are performed.

In addition, the option has been renamed to `js-enabled-only` to align
with the `js-server-only` naming. The previous `js-enabled` name still
works, but is mapped to this new option. A warning is emitted noting
the previous option is deprecated.

Fix #3703

Signed-off-by: Byron Ruth <b@devel.io>
This commit is contained in:
Byron Ruth
2022-12-09 14:23:35 -05:00
parent 5b42cda4dd
commit 566d1adfa7
4 changed files with 41 additions and 21 deletions

View File

@@ -2631,8 +2631,8 @@ type JSzOptions struct {
// HealthzOptions are options passed to Healthz
type HealthzOptions struct {
JSEnabled bool `json:"js-enabled,omitempty"`
JSServerOnly bool `json:"js-server-only,omitempty"`
JSEnabledOnly bool `json:"js-enabled-only,omitempty"`
JSServerOnly bool `json:"js-server-only,omitempty"`
}
type StreamDetail struct {
@@ -2972,14 +2972,21 @@ func (s *Server) HandleHealthz(w http.ResponseWriter, r *http.Request) {
if err != nil {
return
}
if jsEnabled {
s.Warnf("Healthcheck: js-enabled deprecated, use js-enabled-only instead")
}
jsEnabledOnly, err := decodeBool(w, r, "js-enabled-only")
if err != nil {
return
}
jsServerOnly, err := decodeBool(w, r, "js-server-only")
if err != nil {
return
}
hs := s.healthz(&HealthzOptions{
JSEnabled: jsEnabled,
JSServerOnly: jsServerOnly,
JSEnabledOnly: jsEnabledOnly || jsEnabled,
JSServerOnly: jsServerOnly,
})
if hs.Error != _EMPTY_ {
s.Warnf("Healthcheck failed: %q", hs.Error)
@@ -3008,15 +3015,23 @@ func (s *Server) healthz(opts *HealthzOptions) *HealthStatus {
return health
}
// Check JetStream
sopts := s.getOpts()
// If JS is not enabled in the config, we stop.
if !sopts.JetStream {
return health
}
// Access the Jetstream state to perform additional checks.
js := s.getJetStream()
if js == nil {
// If JetStream should be enabled then return error status.
if opts.JSEnabled {
health.Status = "unavailable"
health.Error = NewJSNotEnabledError().Error()
return health
}
if !js.isEnabled() {
health.Status = "unavailable"
health.Error = NewJSNotEnabledError().Error()
return health
}
// Only check if JS is enabled, skip meta and asset check.
if opts.JSEnabledOnly {
return health
}