fix: check if server_metadata file is empty before unmarshall

sreeram77/fix-failed-restart-server
Sreeram Narayanan 2025-01-22 11:40:16 +05:30
parent 88539f1b7e
commit 28d6655877
3 changed files with 18 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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