-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use sync/atomic#Pointer
instead of our own wrapper
#812
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ import ( | |
"encoding/hex" | ||
"github.com/google/uuid" | ||
"github.com/icinga/icinga-go-library/backoff" | ||
"github.com/icinga/icinga-go-library/com" | ||
"github.com/icinga/icinga-go-library/database" | ||
"github.com/icinga/icinga-go-library/logging" | ||
"github.com/icinga/icinga-go-library/retry" | ||
|
@@ -19,6 +18,7 @@ import ( | |
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
|
@@ -35,7 +35,7 @@ type haState struct { | |
|
||
// HA provides high availability and indicates whether a Takeover or Handover must be made. | ||
type HA struct { | ||
state com.Atomic[haState] | ||
state atomic.Pointer[haState] | ||
ctx context.Context | ||
cancelCtx context.CancelFunc | ||
instanceId types.Binary | ||
|
@@ -71,6 +71,8 @@ func NewHA(ctx context.Context, db *database.DB, heartbeat *icingaredis.Heartbea | |
done: make(chan struct{}), | ||
} | ||
|
||
ha.state.Store(&haState{}) | ||
|
||
go ha.controller() | ||
|
||
return ha | ||
|
@@ -121,7 +123,8 @@ func (h *HA) Takeover() chan string { | |
|
||
// State returns the status quo. | ||
func (h *HA) State() (responsibleTsMilli int64, responsible, otherResponsible bool) { | ||
state, _ := h.state.Load() | ||
state := h.state.Load() | ||
|
||
return state.responsibleTsMilli, state.responsible, state.otherResponsible | ||
} | ||
|
||
|
@@ -428,9 +431,12 @@ func (h *HA) realize( | |
|
||
h.signalTakeover(takeover) | ||
} else if otherResponsible { | ||
if state, _ := h.state.Load(); !state.otherResponsible { | ||
state.otherResponsible = true | ||
h.state.Store(state) | ||
if state := h.state.Load(); !state.otherResponsible { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we're now loading by-ref, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
// Dereference pointer to create a copy of the value it points to. | ||
// Ensures that any modifications do not directly affect the original data unless explicitly stored back. | ||
newState := *state | ||
newState.otherResponsible = true | ||
h.state.Store(&newState) | ||
} | ||
} | ||
|
||
|
@@ -496,7 +502,7 @@ func (h *HA) removeOldInstances(s *icingaredisv1.IcingaStatus, envId types.Binar | |
// signalHandover gives up HA.responsible and notifies the HA.Handover chan. | ||
func (h *HA) signalHandover(reason string) { | ||
if h.responsible { | ||
h.state.Store(haState{ | ||
h.state.Store(&haState{ | ||
responsibleTsMilli: time.Now().UnixMilli(), | ||
responsible: false, | ||
otherResponsible: false, | ||
|
@@ -514,7 +520,7 @@ func (h *HA) signalHandover(reason string) { | |
// signalTakeover claims HA.responsible and notifies the HA.Takeover chan. | ||
func (h *HA) signalTakeover(reason string) { | ||
if !h.responsible { | ||
h.state.Store(haState{ | ||
h.state.Store(&haState{ | ||
Al2Klimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
responsibleTsMilli: time.Now().UnixMilli(), | ||
responsible: true, | ||
otherResponsible: false, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been a zero value if uninitialized, now it's a nil ptr in this case. Now we have to make sure HA#State() don't get a nil ptr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.