diff --git a/changelog/unreleased/enhancement-nats-jwt-credentials.md b/changelog/unreleased/enhancement-nats-jwt-credentials.md index ac864146917..198bf7efa21 100644 --- a/changelog/unreleased/enhancement-nats-jwt-credentials.md +++ b/changelog/unreleased/enhancement-nats-jwt-credentials.md @@ -45,7 +45,8 @@ the events cluster ID as the NATS address. It now uses the events endpoint. Each health check skips the check when the NATS address is empty. Each health check keeps one NATS connection and uses it again for each probe. Each probe sends a PING to the NATS server, so the check finds a lost server at once. A -slow NATS server does not block the other probes. +slow NATS server does not block the other probes. The health check of a store +succeeds when one node of the store is reachable, like the store connection. The userlog service had no default node for its store. With `OCIS_PERSISTENT_STORE=nats-js-kv` it could not connect to NATS. It now uses diff --git a/ocis-pkg/checks/checknats.go b/ocis-pkg/checks/checknats.go index 91483fd6228..adf928488ef 100644 --- a/ocis-pkg/checks/checknats.go +++ b/ocis-pkg/checks/checknats.go @@ -3,7 +3,7 @@ package checks import ( "context" "fmt" - "sync" + "strings" "time" "github.com/nats-io/nats.go" @@ -30,45 +30,47 @@ func NewNatsCheck(natsCluster string, options ...nats.Option) func(context.Conte const natsCheckTimeout = 2 * time.Second // NewNatsCheckWithConfig checks the reachability of the nats server at address. -// It authenticates and uses TLS like the service connections configured by cfg. +// The address can contain more than one server, separated by commas. +// The check authenticates and uses TLS like the service connections configured by cfg. // The check uses address and ignores the endpoint of cfg. An empty address means -// that the service does not use NATS. The check then always succeeds. +// that the service does not use NATS. The function then returns nil. // // The check keeps one connection and uses it again for each probe. A new connection // for each probe reads the .creds file each time. A probe can then read a rotated file // before the write is complete. Each connection also counts against the connection -// limit of the NATS account. The check connects again only after the connection closes. -// The connection reads the .creds file again on each reconnect. +// limit of the NATS account. The nats client reconnects the connection when it loses +// the server. The connection reads the .creds file again on each reconnect. // // The connection status stays CONNECTED until the client notices a lost server. // Thus each probe sends a PING to the server and waits for the PONG. func NewNatsCheckWithConfig(address string, cfg stream.NatsConfig) func(context.Context) error { if address == "" { - return func(context.Context) error { return nil } + return nil } - c := &natsCheck{address: address, cfg: cfg} + c := &natsCheck{address: address, cfg: cfg, lock: make(chan struct{}, 1)} return c.check } -// NewNatsStoreCheck checks the reachability of the first node of a store. -// The check always succeeds when the store type is not a NATS type or when the store has no nodes. +// NewNatsStoreCheck checks the reachability of the nodes of a store. +// The store connects to any node that it can reach. Thus the check succeeds when +// one node is reachable. A check of each node fails when one node of a healthy +// cluster is down. +// The function returns nil when the store type is not a NATS type or when the store has no nodes. func NewNatsStoreCheck(storeType string, nodes []string, cfg stream.NatsConfig) func(context.Context) error { if !revastore.UsesNats(storeType) || len(nodes) == 0 { - return func(context.Context) error { return nil } + return nil } - return NewNatsCheckWithConfig(nodes[0], cfg) + return NewNatsCheckWithConfig(strings.Join(nodes, ","), cfg) } type natsCheck struct { address string cfg stream.NatsConfig - mu sync.Mutex + // lock holds one token. A probe holds the token while it creates the connection. + // A probe that waits for the token stops when its context ends. + lock chan struct{} conn *nats.Conn - // dial is not nil while a connect is in progress. The connect closes it when it ends. - dial chan struct{} - // dialErr is the error of the last connect. - dialErr error } func (c *natsCheck) check(ctx context.Context) error { @@ -82,60 +84,49 @@ func (c *natsCheck) check(ctx context.Context) error { if err != nil { return err } + if !conn.IsConnected() { + return fmt.Errorf("nats server not connected: status %v, last error %v", conn.Status(), conn.LastError()) + } if err := conn.FlushWithContext(ctx); err != nil { return fmt.Errorf("nats server not reachable: %v", err) } return nil } -// connection returns the open connection. It starts a connect when no connection is open. -// The connect runs without the lock, so a slow server does not block the other probes. -// Each probe waits for the connect only until its context ends. +// connection returns the connection. It creates the connection on the first call. +// The nats client then connects again by itself, so the connection does not close. +// A failed configuration does not create a connection. The next probe tries again. func (c *natsCheck) connection(ctx context.Context) (*nats.Conn, error) { - c.mu.Lock() - if c.conn != nil && !c.conn.IsClosed() { - conn := c.conn - c.mu.Unlock() - return conn, nil - } - if c.dial == nil { - c.dial = make(chan struct{}) - go c.connect(c.dial) - } - dial := c.dial - c.mu.Unlock() - select { - case <-dial: + case c.lock <- struct{}{}: case <-ctx.Done(): return nil, fmt.Errorf("could not connect to nats server: %v", ctx.Err()) } + defer func() { <-c.lock }() - c.mu.Lock() - defer c.mu.Unlock() - if c.conn == nil || c.conn.IsClosed() { - return nil, c.dialErr + if c.conn != nil { + return c.conn, nil } - return c.conn, nil -} -func (c *natsCheck) connect(done chan struct{}) { - conn, err := c.dialNats() - - c.mu.Lock() - defer c.mu.Unlock() - c.conn, c.dialErr, c.dial = conn, err, nil - close(done) -} - -func (c *natsCheck) dialNats() (*nats.Conn, error) { options, err := stream.NatsOptions(c.cfg) if err != nil { return nil, fmt.Errorf("could not configure the nats connection: %v", err) } + // The first connect blocks the probe. The timeout keeps the connect in the probe deadline. + deadline, _ := ctx.Deadline() + timeout := min(time.Until(deadline), natsCheckTimeout) + if timeout <= 0 { + return nil, fmt.Errorf("could not connect to nats server: %v", context.DeadlineExceeded) + } + options = append(options, + nats.Timeout(timeout), + nats.RetryOnFailedConnect(true), + nats.MaxReconnects(-1), + ) conn, err := nats.Connect(c.address, options...) if err != nil { return nil, fmt.Errorf("could not connect to nats server: %v", err) } + c.conn = conn return conn, nil } diff --git a/ocis-pkg/checks/checknats_test.go b/ocis-pkg/checks/checknats_test.go index ac76a7cac96..102da021cbd 100644 --- a/ocis-pkg/checks/checknats_test.go +++ b/ocis-pkg/checks/checknats_test.go @@ -20,7 +20,7 @@ func TestNewNatsCheckWithConfig(t *testing.T) { url := s.ClientURL() t.Run("empty address", func(t *testing.T) { - require.NoError(t, checks.NewNatsCheckWithConfig("", stream.NatsConfig{})(context.Background())) + require.Nil(t, checks.NewNatsCheckWithConfig("", stream.NatsConfig{})) }) credsFile := filepath.Join(t.TempDir(), "user.creds") require.NoError(t, os.WriteFile(credsFile, creds, 0o600)) @@ -136,11 +136,28 @@ func TestNewNatsStoreCheck(t *testing.T) { t.Run("other store types", func(t *testing.T) { for _, storeType := range []string{"memory", "redis-sentinel", "noop", ""} { - require.NoError(t, checks.NewNatsStoreCheck(storeType, []string{url}, stream.NatsConfig{})(context.Background()), storeType) + require.Nil(t, checks.NewNatsStoreCheck(storeType, []string{url}, stream.NatsConfig{}), storeType) } }) t.Run("no nodes", func(t *testing.T) { - require.NoError(t, checks.NewNatsStoreCheck("nats-js-kv", nil, stream.NatsConfig{})(context.Background())) + require.Nil(t, checks.NewNatsStoreCheck("nats-js-kv", nil, stream.NatsConfig{})) + }) + + t.Run("one node is down", func(t *testing.T) { + // Nothing listens on the first node. + l, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + down := "nats://" + l.Addr().String() + require.NoError(t, l.Close()) + + require.NoError(t, checks.NewNatsStoreCheck("nats-js-kv", []string{down, url}, cfg)(context.Background())) + }) + + t.Run("all nodes are down", func(t *testing.T) { + s, creds := natstest.StartOperatorServer(t) + check := checks.NewNatsStoreCheck("nats-js-kv", []string{s.ClientURL()}, stream.NatsConfig{Creds: string(creds)}) + s.Shutdown() + require.Error(t, check(context.Background())) }) } diff --git a/ocis-pkg/handlers/checker.go b/ocis-pkg/handlers/checker.go index 8ef40ebec59..66c99ac644d 100644 --- a/ocis-pkg/handlers/checker.go +++ b/ocis-pkg/handlers/checker.go @@ -43,7 +43,12 @@ func (c CheckHandlerConfiguration) WithLogger(l log.Logger) CheckHandlerConfigur } // WithCheck sets a check for the CheckHandlerConfiguration. +// A nil check means that nothing needs a check. WithCheck then does not add it. func (c CheckHandlerConfiguration) WithCheck(name string, check checker) CheckHandlerConfiguration { + if check == nil { + return c + } + if _, ok := c.checks[name]; ok { c.logger.Panic().Str("check", name).Msg("check already exists") } diff --git a/ocis-pkg/handlers/checker_test.go b/ocis-pkg/handlers/checker_test.go index 871c413f867..d48e21a26bd 100644 --- a/ocis-pkg/handlers/checker_test.go +++ b/ocis-pkg/handlers/checker_test.go @@ -41,6 +41,12 @@ func TestCheckHandlerConfiguration(t *testing.T) { handlerConfiguration.WithCheck("check-1", nopCheck) require.Equal(t, 3, nopCheckCounter) }) + + t.Run("nil check is skipped", func(t *testing.T) { + rec := httptest.NewRecorder() + handlers.NewCheckHandler(handlerConfiguration.WithCheck("check-nil", nil)).ServeHTTP(rec, httptest.NewRequest("GET", "/", nil)) + require.Equal(t, http.StatusOK, rec.Code) + }) } func TestCheckHandler(t *testing.T) {