Skip to content

Commit

Permalink
HA: Abort Transaction Commit after Timeout
Browse files Browse the repository at this point in the history
A strange HA behavior was reported in #787, resulting in both instances
being active.

The logs contained an entry of the previous active instance exiting the
HA.realize() method successfully after 1m9s. This, however, should not
be possible as the method's context is deadlined to a minute after the
heartbeat was received.

However, as it turns out, executing COMMIT on a database transaction is
not bound to the transaction's context, allowing to survive longer. To
mitigate this, another context watch was introduced. Doing so allows
directly handing over, while the other instance can now take over due to
the expired heartbeat in the database.
  • Loading branch information
oxzi committed Oct 25, 2024
1 parent c2d8bd6 commit 8b95d25
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ func (h *HA) controller() {

// realize a HA cycle triggered by a heartbeat event.
//
// The context passed is expected to have a deadline, otherwise the method will panic. This deadline is strictly
// enforced to abort the realization logic the moment the context expires.
//
// shouldLogRoutineEvents indicates if recurrent events should be logged.
//
// The internal, retryable function always fetches the last received heartbeat's timestamp instead of reusing the one
Expand Down Expand Up @@ -388,8 +391,39 @@ func (h *HA) realize(
}
}

if err := tx.Commit(); err != nil {
return errors.Wrap(err, "can't commit transaction")
// In general, cancellation does not work for COMMIT and ROLLBACK. Some database drivers may support a
// context-based abort, but only if the DBMS allows it. This was also discussed in the initial issue about
// context support to Go's sql package: https://github.com/golang/go/issues/15123#issuecomment-245882486
//
// This paragraph is implementation knowledge, not covered by the API specification. Go's sql.Tx.Commit() -
// which is not being overridden by sqlx.Tx - performs a preflight check on the context before handing over
// to the driver's Commit() method. Drivers may behave differently. For example, the used
// github.com/go-sql-driver/mysql package calls its internal exec() method with a COMMIT query, writing and
// reading packets without honoring the context.
//
// In a nutshell, one cannot expect a Tx.Commit() call to be covered by the transaction context. For this
// reason, the following Commit() call has been moved to its own goroutine, which communicates back via a
// channel selected along with the context. If the context ends before Commit(), this retryable function
// returns with a non-retryable error.
//
// However, while the COMMIT continues in the background, it may still succeed. In this case, the state of
// the database does not match the state of Icinga DB, specifically the database says that this instance is
// active while this instance thinks otherwise. Fortunately, this mismatch is not critical because when this
// function is re-entered, the initial SELECT query would be empty for this Icinga DB node and imply the
// presence of another active instance for the other node. Effectively, this could result in a single HA
// cycle with no active node. Afterwards, either this instance takes over due to the false impression that
// no other node is active, or the other instances does so as the inserted heartbeat has already expired.
// Not great, not terrible.
commitErrCh := make(chan error, 1)
go func() { commitErrCh <- tx.Commit() }()

select {
case err := <-commitErrCh:
if err != nil {
return errors.Wrap(err, "can't commit transaction")
}
case <-ctx.Done():
return ctx.Err()
}

return nil
Expand Down

0 comments on commit 8b95d25

Please sign in to comment.