Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions silence/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,6 @@ func (s *Silences) Set(sil *pb.Silence) (string, error) {
}
}

// If we got here it's either a new silence or a replacing one.
if s.limits.MaxSilences > 0 {
if len(s.st)+1 > s.limits.MaxSilences {
return "", fmt.Errorf("exceeded maximum number of silences: %d (limit: %d)", len(s.st), s.limits.MaxSilences)
}
}

uid, err := uuid.NewV4()
if err != nil {
return "", fmt.Errorf("generate uuid: %w", err)
Expand All @@ -638,11 +631,14 @@ func (s *Silences) Set(sil *pb.Silence) (string, error) {
sil.StartsAt = now
}

if err = s.setSilence(sil, now, false); err != nil {
return "", err
// If we got here it's either a new silence or a replacing one.
if s.limits.MaxSilences > 0 {
if len(s.st)+1 > s.limits.MaxSilences {
return sil.Id, fmt.Errorf("exceeded maximum number of silences: %d (limit: %d)", len(s.st), s.limits.MaxSilences)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I return sil.Id here as that's what happens if s.setSilence returns an error too. s.setSilence can return an error if the silence is invalid or maximum size has been exceeded.

}
}

return sil.Id, nil
return sil.Id, s.setSilence(sil, now, false)
Copy link
Copy Markdown
Collaborator Author

@grobinson-grafana grobinson-grafana Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how it was before #3852.

}

// canUpdate returns true if silence a can be updated to b without
Expand Down
6 changes: 4 additions & 2 deletions silence/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func TestSilenceLimits(t *testing.T) {
}
id2, err := s.Set(sil2)
require.EqualError(t, err, "exceeded maximum number of silences: 1 (limit: 1)")
require.Equal(t, "", id2)
require.NotEqual(t, "", id2)

// Expire sil1 and run the GC. This should allow sil2 to be
// inserted.
Expand All @@ -496,6 +496,8 @@ func TestSilenceLimits(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, n)

// Need to erase the previous Id as the insert failed.
sil2.Id = ""
id2, err = s.Set(sil2)
require.NoError(t, err)
require.NotEqual(t, "", id2)
Expand Down Expand Up @@ -532,7 +534,7 @@ func TestSilenceLimits(t *testing.T) {
// Do not check the exact size as it can change between consecutive runs
// due to padding.
require.Contains(t, err.Error(), "silence exceeded maximum size")
require.Equal(t, "", id3)
require.NotEqual(t, "", id3)
}

func TestSetActiveSilence(t *testing.T) {
Expand Down