@@ -24,13 +24,15 @@ import (
2424 coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
2525)
2626
27+ // Source identifies where a rule mutation originated.
28+ // Used for auditing and distinguishing user-initiated changes from system-generated ones.
2729type Source string
2830
2931const (
30- SourceAdmin Source = "ADMIN"
31- SourceUpstream Source = "UPSTREAM"
32- SourceRollback Source = "ROLLBACK"
33- SourceBootstrap Source = "BOOTSTRAP"
32+ SourceAdmin Source = "ADMIN" // User edit via Admin UI/API
33+ SourceUpstream Source = "UPSTREAM" // Registry change detected by subscriber
34+ SourceRollback Source = "ROLLBACK" // Rollback to a previous version
35+ SourceBootstrap Source = "BOOTSTRAP" // Initial version recorded at startup
3436)
3537
3638type Operation string
@@ -41,26 +43,32 @@ const (
4143 OperationDelete Operation = "DELETE"
4244)
4345
46+ // IntentStatus tracks the lifecycle of a mutation intent.
47+ // Intent workflow: PENDING (created) → APPLIED (mutation succeeded) → COMMITTED (version recorded)
48+ // Or: PENDING → FAILED (mutation failed or conflicted)
4449type IntentStatus string
4550
4651const (
47- IntentStatusPending IntentStatus = "PENDING"
48- IntentStatusApplied IntentStatus = "APPLIED"
49- IntentStatusCommitted IntentStatus = "COMMITTED"
50- IntentStatusFailed IntentStatus = "FAILED"
52+ IntentStatusPending IntentStatus = "PENDING" // Intent created, mutation not yet applied
53+ IntentStatusApplied IntentStatus = "APPLIED" // Mutation applied to resource store, awaiting commit
54+ IntentStatusCommitted IntentStatus = "COMMITTED" // Version successfully recorded, intent closed
55+ IntentStatusFailed IntentStatus = "FAILED" // Mutation failed or was rejected
5156)
5257
5358var (
5459 ErrFeatureDisabled = errors .New ("rule versioning is disabled" )
55- ErrVersionConflict = errors .New ("rule version conflict" )
60+ ErrVersionConflict = errors .New ("rule version conflict" ) // ExpectedVersionID mismatch
5661 ErrVersionNotFound = errors .New ("rule version not found" )
5762 ErrVersionIntentNotFound = errors .New ("rule version intent not found" )
58- ErrVersionIntentNotOpen = errors .New ("rule version intent is not open" )
59- ErrVersionIntentPending = errors .New ("rule version intent is pending" )
63+ ErrVersionIntentNotOpen = errors .New ("rule version intent is not open" ) // Intent already committed or failed
64+ ErrVersionIntentPending = errors .New ("rule version intent is pending" ) // Another mutation in progress
6065 ErrRollbackToDelete = errors .New ("cannot roll back to a deleted rule version" )
6166 ErrRollbackToCurrent = errors .New ("cannot roll back to a version identical to current" )
6267)
6368
69+ // Version represents an immutable snapshot of a rule's spec at a point in time.
70+ // Versions are append-only; each mutation creates a new Version record.
71+ // The IsCurrent field is computed at query time by comparing with Meta.CurrentVersion.
6472type Version struct {
6573 ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
6674 RuleKind coremodel.ResourceKind `json:"ruleKind" gorm:"type:varchar(64);not null;index:idx_rk_key_created,priority:1;uniqueIndex:uk_rk_key_ver,priority:1;index:idx_rk_hash,priority:1"`
@@ -83,6 +91,9 @@ func (Version) TableName() string {
8391 return "rule_version"
8492}
8593
94+ // Meta tracks the current version and sequence number for a rule.
95+ // Updated atomically when a new version is committed.
96+ // CurrentVersion may be nil if the rule was deleted.
8697type Meta struct {
8798 RuleKind coremodel.ResourceKind `json:"ruleKind" gorm:"type:varchar(64);primaryKey"`
8899 ResourceKey string `json:"resourceKey" gorm:"type:varchar(512);primaryKey"`
@@ -95,6 +106,12 @@ func (Meta) TableName() string {
95106 return "rule_version_meta"
96107}
97108
109+ // Intent represents a pending mutation to a rule, used to coordinate async writes.
110+ // Why Intents exist:
111+ // - Admin UI mutations are not immediately applied; they create an Intent first
112+ // - The Intent enforces optimistic locking via ExpectedVersionID
113+ // - When the mutation completes, the subscriber commits the Intent to a Version
114+ // - This prevents race conditions when multiple writers target the same rule
98115type Intent struct {
99116 ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
100117 RuleKind coremodel.ResourceKind `json:"ruleKind" gorm:"type:varchar(64);not null;index:idx_intent_rule_status,priority:1;index:idx_intent_hash,priority:1"`
@@ -107,10 +124,10 @@ type Intent struct {
107124 Operation Operation `json:"operation" gorm:"type:varchar(16);not null"`
108125 Author string `json:"author" gorm:"type:varchar(128);not null"`
109126 Reason string `json:"reason,omitempty" gorm:"type:varchar(1024)"`
110- RolledBackFromID * int64 `json:"rolledBackFromId,omitempty"`
111- ExpectedVersionID * int64 `json:"expectedVersionId,omitempty"`
127+ RolledBackFromID * int64 `json:"rolledBackFromId,omitempty"` // Points to the Version being rolled back to
128+ ExpectedVersionID * int64 `json:"expectedVersionId,omitempty"` // Optimistic lock: mutation only proceeds if current version matches
112129 Status IntentStatus `json:"status" gorm:"type:varchar(16);not null;index:idx_intent_rule_status,priority:3"`
113- VersionID * int64 `json:"versionId,omitempty"`
130+ VersionID * int64 `json:"versionId,omitempty"` // Set when committed; points to the created Version
114131 LastError string `json:"lastError,omitempty" gorm:"type:varchar(1024)"`
115132 CreatedAt time.Time `json:"createdAt" gorm:"not null"`
116133 UpdatedAt time.Time `json:"updatedAt" gorm:"not null"`
0 commit comments