mirror of https://github.com/hashicorp/consul
fix: check if server_metadata file is empty before unmarshall
parent
88539f1b7e
commit
28d6655877
|
@ -4708,6 +4708,11 @@ func (a *Agent) checkServerLastSeen(readFn consul.ServerMetadataReadFunc) error
|
|||
return fmt.Errorf("error reading server metadata: %w", err)
|
||||
}
|
||||
|
||||
// Return early if the file exists, but content is empty.
|
||||
if md == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
maxAge := a.config.ServerRejoinAgeMax
|
||||
if md.IsLastSeenStale(maxAge) {
|
||||
return fmt.Errorf("refusing to rejoin cluster because server has been offline for more than the configured server_rejoin_age_max (%s) - consider wiping your data dir", maxAge)
|
||||
|
|
|
@ -6412,6 +6412,15 @@ func TestAgent_checkServerLastSeen(t *testing.T) {
|
|||
require.ErrorContains(t, err, "refusing to rejoin cluster because server has been offline for more than the configured server_rejoin_age_max")
|
||||
})
|
||||
|
||||
t.Run("TestIsLastSeenEmptyFile", func(t *testing.T) {
|
||||
readFn := func(filename string) (*consul.ServerMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
err := agent.checkServerLastSeen(readFn)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
// Test that a server with a 6h old last seen timestamp is not treated as an error.
|
||||
t.Run("TestNoErr", func(t *testing.T) {
|
||||
agent.config.ServerRejoinAgeMax = 24 * 7 * time.Hour
|
||||
|
|
|
@ -46,6 +46,10 @@ func ReadServerMetadata(filename string) (*ServerMetadata, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if len(b) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var md ServerMetadata
|
||||
if err := json.Unmarshal(b, &md); err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in New Issue