-
Notifications
You must be signed in to change notification settings - Fork 20
HYPERFLEET-854 - feat: implement hard deletion for clusters and nodepools #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,9 +12,11 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| type ClusterDao interface { | ||||||||||||||||||||||||||||||||||||||||||||||||
| Get(ctx context.Context, id string) (*api.Cluster, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| GetForUpdate(ctx context.Context, id string) (*api.Cluster, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Create(ctx context.Context, cluster *api.Cluster) (*api.Cluster, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Replace(ctx context.Context, cluster *api.Cluster) (*api.Cluster, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Save(ctx context.Context, cluster *api.Cluster) error | ||||||||||||||||||||||||||||||||||||||||||||||||
| SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error | ||||||||||||||||||||||||||||||||||||||||||||||||
| Delete(ctx context.Context, id string) error | ||||||||||||||||||||||||||||||||||||||||||||||||
| FindByIDs(ctx context.Context, ids []string) (api.ClusterList, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| All(ctx context.Context) (api.ClusterList, error) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -39,6 +41,15 @@ func (d *sqlClusterDao) Get(ctx context.Context, id string) (*api.Cluster, error | |||||||||||||||||||||||||||||||||||||||||||||||
| return &cluster, nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func (d *sqlClusterDao) GetForUpdate(ctx context.Context, id string) (*api.Cluster, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| g2 := (*d.sessionFactory).New(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||
| var cluster api.Cluster | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err := g2.Clauses(clause.Locking{Strength: "UPDATE"}).Take(&cluster, "id = ?", id).Error; err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return &cluster, nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func (d *sqlClusterDao) Create(ctx context.Context, cluster *api.Cluster) (*api.Cluster, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| g2 := (*d.sessionFactory).New(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err := g2.Omit(clause.Associations).Create(cluster).Error; err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -83,6 +94,16 @@ func (d *sqlClusterDao) Save(ctx context.Context, cluster *api.Cluster) error { | |||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func (d *sqlClusterDao) SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error { | ||||||||||||||||||||||||||||||||||||||||||||||||
| g2 := (*d.sessionFactory).New(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||
| result := g2.Model(&api.Cluster{}).Where("id = ?", id).Update("status_conditions", statusConditions) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if result.Error != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| db.MarkForRollback(ctx, result.Error) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return result.Error | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle zero-row status updates as not-found. At Line 99, this returns success even when no row matches Suggested patch func (d *sqlClusterDao) SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error {
g2 := (*d.sessionFactory).New(ctx)
result := g2.Model(&api.Cluster{}).Where("id = ?", id).Update("status_conditions", statusConditions)
if result.Error != nil {
db.MarkForRollback(ctx, result.Error)
return result.Error
}
+ if result.RowsAffected == 0 {
+ err := gorm.ErrRecordNotFound
+ db.MarkForRollback(ctx, err)
+ return err
+ }
return nil
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. False positive for the same reason as the Upsert comment. In the ProcessAdapterStatus flow (the code path added by this PR), the cluster is locked with SELECT ... FOR UPDATE at the start of the transaction, preventing concurrent hard-delete. For the existing UpdateClusterStatusFromAdapters path (which doesn'thold a lock), silently dropping a status condition write for a concurrently hard-deleted cluster is harmless — the cluster no longer exists, so persisting its conditions serves no purpose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| func (d *sqlClusterDao) Delete(ctx context.Context, id string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||
| g2 := (*d.sessionFactory).New(ctx) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err := g2.Omit(clause.Associations).Delete(&api.Cluster{Meta: api.Meta{ID: id}}).Error; err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,18 @@ import ( | |
|
|
||
| type NodePoolDao interface { | ||
| Get(ctx context.Context, id string) (*api.NodePool, error) | ||
| GetForUpdate(ctx context.Context, id string) (*api.NodePool, error) | ||
| Create(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error) | ||
| Replace(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error) | ||
| Save(ctx context.Context, nodePool *api.NodePool) error | ||
| SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error | ||
| Delete(ctx context.Context, id string) error | ||
| FindByIDs(ctx context.Context, ids []string) (api.NodePoolList, error) | ||
| FindByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) | ||
| FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) | ||
| SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error | ||
| UpdateStatusConditionsByIDs(ctx context.Context, updates map[string][]byte) error | ||
| ExistsByOwner(ctx context.Context, ownerID string) (bool, error) | ||
| All(ctx context.Context) (api.NodePoolList, error) | ||
| } | ||
|
|
||
|
|
@@ -44,6 +48,25 @@ func (d *sqlNodePoolDao) Get(ctx context.Context, id string) (*api.NodePool, err | |
| return &nodePool, nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) GetForUpdate(ctx context.Context, id string) (*api.NodePool, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| var nodePool api.NodePool | ||
| if err := g2.Clauses(clause.Locking{Strength: "UPDATE"}).Take(&nodePool, "id = ?", id).Error; err != nil { | ||
| return nil, err | ||
| } | ||
| return &nodePool, nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| result := g2.Model(&api.NodePool{}).Where("id = ?", id).Update("status_conditions", statusConditions) | ||
| if result.Error != nil { | ||
| db.MarkForRollback(ctx, result.Error) | ||
| return result.Error | ||
| } | ||
| return nil | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| func (d *sqlNodePoolDao) Create(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| if err := g2.Omit(clause.Associations).Create(nodePool).Error; err != nil { | ||
|
|
@@ -131,6 +154,15 @@ func (d *sqlNodePoolDao) FindByIDs(ctx context.Context, ids []string) (api.NodeP | |
| return nodePools, nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) FindByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| var nodePools api.NodePoolList | ||
| if err := g2.Where("owner_id = ?", ownerID).Find(&nodePools).Error; err != nil { | ||
| return nil, err | ||
| } | ||
| return nodePools, nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) UpdateStatusConditionsByIDs(ctx context.Context, updates map[string][]byte) error { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| if len(updates) == 0 { | ||
|
|
@@ -149,6 +181,15 @@ func (d *sqlNodePoolDao) UpdateStatusConditionsByIDs(ctx context.Context, update | |
| return nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) ExistsByOwner(ctx context.Context, ownerID string) (bool, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| var count int64 | ||
| if err := g2.Model(&api.NodePool{}).Where("owner_id = ?", ownerID).Limit(1).Count(&count).Error; err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be simplified using raw SQL: |
||
| return false, err | ||
| } | ||
| return count > 0, nil | ||
| } | ||
|
|
||
| func (d *sqlNodePoolDao) All(ctx context.Context) (api.NodePoolList, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| nodePools := api.NodePoolList{} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Differentiate stale report from concurrently deleted row.
At Line 98,
RowsAffected == 0unconditionally returnsexisting. That is correct for stale reports, but incorrect when the record was deleted between read and update. In that case this method reports success with a non-persisted object. Please branch explicitly: stale → returnexisting; missing row → re-read/recreate or return not-found.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scenario CodeRabbit describes (record deleted between read and update) cannot happen in this code. The callers of Upsert in ProcessAdapterStatus hold a SELECT ... FOR UPDATE lock on the parent resource (cluster/nodepool) for the duration of the transaction. No concurrent transaction can delete the adapter status while this one is in progress.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mliptak0, that's a valid point — theSELECT ... FOR UPDATElock on the parent resource serializes all concurrent writes for the duration of the transaction, which makes the delete-between-read-and-update race impossible in this context. I've noted the locking strategy for future reviews.