Skip to content

Commit 6601f28

Browse files
committed
HYPERFLEET-856 - fix: use SoftDeleteByOwner row count for metrics to prevent overcounting
1 parent b4ffe3e commit 6601f28

4 files changed

Lines changed: 22 additions & 13 deletions

File tree

pkg/dao/mocks/node_pool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ func (d *nodePoolDaoMock) Delete(ctx context.Context, id string) error {
5858
return errors.NotImplemented("NodePool").AsError()
5959
}
6060

61-
func (d *nodePoolDaoMock) SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error {
62-
return errors.NotImplemented("NodePool").AsError()
61+
func (d *nodePoolDaoMock) SoftDeleteByOwner(
62+
ctx context.Context, ownerID string, t time.Time, deletedBy string,
63+
) (int64, error) {
64+
return 0, errors.NotImplemented("NodePool").AsError()
6365
}
6466

6567
func (d *nodePoolDaoMock) FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) {

pkg/dao/node_pool.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type NodePoolDao interface {
2323
FindByIDs(ctx context.Context, ids []string) (api.NodePoolList, error)
2424
FindByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error)
2525
FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error)
26-
SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error
26+
SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) (int64, error)
2727
UpdateStatusConditionsByIDs(ctx context.Context, updates map[string][]byte) error
2828
ExistsByOwner(ctx context.Context, ownerID string) (bool, error)
2929
All(ctx context.Context) (api.NodePoolList, error)
@@ -120,7 +120,9 @@ func (d *sqlNodePoolDao) Delete(ctx context.Context, id string) error {
120120
return nil
121121
}
122122

123-
func (d *sqlNodePoolDao) SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error {
123+
func (d *sqlNodePoolDao) SoftDeleteByOwner(
124+
ctx context.Context, ownerID string, t time.Time, deletedBy string,
125+
) (int64, error) {
124126
g2 := (*d.sessionFactory).New(ctx)
125127
result := g2.Model(&api.NodePool{}).
126128
Where("owner_id = ? AND deleted_time IS NULL", ownerID).
@@ -131,9 +133,9 @@ func (d *sqlNodePoolDao) SoftDeleteByOwner(ctx context.Context, ownerID string,
131133
})
132134
if result.Error != nil {
133135
db.MarkForRollback(ctx, result.Error)
134-
return result.Error
136+
return 0, result.Error
135137
}
136-
return nil
138+
return result.RowsAffected, nil
137139
}
138140

139141
func (d *sqlNodePoolDao) FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) {

pkg/services/cluster.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,20 @@ func (s *sqlClusterService) SoftDelete(ctx context.Context, id string) (*api.Clu
120120

121121
metrics.RecordPendingDeletion("cluster")
122122

123-
if cascadeErr := s.nodePoolDao.SoftDeleteByOwner(ctx, id, t, deletedBy); cascadeErr != nil {
123+
cascadeCount, cascadeErr := s.nodePoolDao.SoftDeleteByOwner(ctx, id, t, deletedBy)
124+
if cascadeErr != nil {
124125
return nil, handleSoftDeleteError("NodePool", cascadeErr)
125126
}
126127

128+
for range cascadeCount {
129+
metrics.RecordPendingDeletion("nodepool")
130+
}
131+
127132
nodePools, err := s.nodePoolDao.FindSoftDeletedByOwner(ctx, id)
128133
if err != nil {
129134
return nil, errors.GeneralError("Failed to fetch cascade-deleted nodepools: %s", err)
130135
}
131136

132-
for range nodePools {
133-
metrics.RecordPendingDeletion("nodepool")
134-
}
135-
136137
cluster, svcErr := s.UpdateClusterStatusFromAdapters(ctx, cluster.ID)
137138
if svcErr != nil {
138139
return nil, svcErr

pkg/services/node_pool_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,20 @@ func (d *mockNodePoolDao) Delete(ctx context.Context, id string) error {
8787
return nil
8888
}
8989

90-
func (d *mockNodePoolDao) SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error {
90+
func (d *mockNodePoolDao) SoftDeleteByOwner(
91+
ctx context.Context, ownerID string, t time.Time, deletedBy string,
92+
) (int64, error) {
93+
var count int64
9194
for id, np := range d.nodePools {
9295
if np.OwnerID == ownerID && np.DeletedTime == nil {
9396
np.DeletedTime = &t
9497
np.DeletedBy = &deletedBy
9598
np.Generation++
9699
d.nodePools[id] = np
100+
count++
97101
}
98102
}
99-
return nil
103+
return count, nil
100104
}
101105

102106
func (d *mockNodePoolDao) FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) {

0 commit comments

Comments
 (0)