Skip to content

Commit

Permalink
HA: Insert environment within retryable function
Browse files Browse the repository at this point in the history
The HA.insertEnvironment() method was inlined into the retryable
function to use the deadlined context. Otherwise, this might block
afterwards, as it was used within HA.realize(), but without the passed
context.
  • Loading branch information
oxzi committed Oct 25, 2024
1 parent f881920 commit dd0ca8f
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,14 @@ func (h *HA) realize(

if takeover != "" {
stmt := h.db.Rebind("UPDATE icingadb_instance SET responsible = ? WHERE environment_id = ? AND id <> ?")
_, err := tx.ExecContext(ctx, stmt, "n", envId, h.instanceId)
if _, err := tx.ExecContext(ctx, stmt, "n", envId, h.instanceId); err != nil {
return database.CantPerformQuery(err, stmt)
}

if err != nil {
// Insert the environment after each heartbeat takeover if it does not already exist in the database
// as the environment may have changed, although this is likely to happen very rarely.
stmt, _ = h.db.BuildInsertIgnoreStmt(h.environment)
if _, err := h.db.NamedExecContext(ctx, stmt, h.environment); err != nil {
return database.CantPerformQuery(err, stmt)
}
}
Expand Down Expand Up @@ -424,12 +429,6 @@ func (h *HA) realize(
}

if takeover != "" {
// Insert the environment after each heartbeat takeover if it does not already exist in the database
// as the environment may have changed, although this is likely to happen very rarely.
if err := h.insertEnvironment(); err != nil {
return errors.Wrap(err, "can't insert environment")
}

h.signalTakeover(takeover)
} else if otherResponsible {
if state := h.state.Load(); !state.otherResponsible {
Expand All @@ -452,18 +451,6 @@ func (h *HA) realizeLostHeartbeat() {
}
}

// insertEnvironment inserts the environment from the specified state into the database if it does not already exist.
func (h *HA) insertEnvironment() error {
// Instead of checking whether the environment already exists, use an INSERT statement that does nothing if it does.
stmt, _ := h.db.BuildInsertIgnoreStmt(h.environment)

if _, err := h.db.NamedExecContext(h.ctx, stmt, h.environment); err != nil {
return database.CantPerformQuery(err, stmt)
}

return nil
}

func (h *HA) removeInstance(ctx context.Context) {
h.logger.Debugw("Removing our row from icingadb_instance", zap.String("instance_id", hex.EncodeToString(h.instanceId)))
// Intentionally not using h.ctx here as it's already cancelled.
Expand Down

0 comments on commit dd0ca8f

Please sign in to comment.