HA: Insert environment within retryable function

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.
This commit is contained in:
Alvar Penning 2024-10-25 10:39:05 +02:00
parent f8819208ce
commit dd0ca8fb07
No known key found for this signature in database

View file

@ -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)
}
}
@ -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 {
@ -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.