mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
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:
parent
f8819208ce
commit
dd0ca8fb07
1 changed files with 7 additions and 20 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue