mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
Closes CODAGT-736 Concurrent chat model config writes on a deployment with no default all elect themselves default: at READ COMMITTED neither transaction sees the other's uncommitted default, so both self-promote and `idx_chat_model_configs_single_default` rejects the loser as a spurious 409. The coderd Terraform provider hits this routinely, since a single `terraform apply` creates or deletes many configs in parallel by design. The fix serializes the election with a transaction-scoped advisory lock: the create, update, and delete handlers run their default election inside a transaction that first takes `pg_advisory_xact_lock` on a dedicated `LockIDChatModelConfigDefault`, so elections run one at a time and the index is never contended. The partial unique index stays in place as the schema-level invariant, and the existing 409 mapping remains as a backstop for any writer that bypasses the lock. We considered a singleton pointer table (one row holding a `model_config_id` FK, making a second default unrepresentable), which would remove the race outright, but it needs a migration, new queries, dbauthz rules, and handler/read-path rework. Not proportionate for an experimental endpoint.
29 lines
855 B
Go
29 lines
855 B
Go
package database
|
|
|
|
import "hash/fnv"
|
|
|
|
// Well-known lock IDs for lock functions in the database. These should not
|
|
// change. If locks are deprecated, they should be kept in this list to avoid
|
|
// reusing the same ID.
|
|
const (
|
|
LockIDDeploymentSetup = iota + 1
|
|
LockIDEnterpriseDeploymentSetup
|
|
LockIDDBRollup
|
|
LockIDDBPurge
|
|
LockIDNotificationsReportGenerator
|
|
LockIDCryptoKeyRotation
|
|
LockIDReconcilePrebuilds
|
|
LockIDReconcileSystemRoles
|
|
LockIDBoundaryUsageStats
|
|
LockIDAIProvidersEnvSeed
|
|
LockIDChatModelConfigWrites
|
|
)
|
|
|
|
// GenLockID generates a unique and consistent lock ID from a given string.
|
|
func GenLockID(name string) int64 {
|
|
hash := fnv.New64()
|
|
_, _ = hash.Write([]byte(name))
|
|
// #nosec G115 - Safe conversion as FNV hash should be treated as random value and both uint64/int64 have the same range of unique values
|
|
return int64(hash.Sum64())
|
|
}
|