Skip to content

Commit b2cc3b6

Browse files
committed
refactor: add temp cache for config changes
We don't want to create new config changes with a ON CONFLICT DO NOTHING clause because now we don't want the same config change to take up multiple quotas from the rate limiter. i.e. if an aws scraper is run @every 5m, we'll be trying to insert the same config changes generated by the cloudtrail scraper again & again on every run. The same change will take up one more quota from the rate limiter on every run. By knowing that the change already exist, we can avoid inserting that change in the first place and the rate limiter will be happy about it.
1 parent 21f5c85 commit b2cc3b6

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

api/cache.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type TempCache struct {
1616
ctx context.Context
1717
items map[string]models.ConfigItem
1818
aliases map[string]string
19+
20+
changes map[string]struct{}
1921
}
2022

2123
func (t TempCache) FindExternal(ext v1.ExternalID) (*models.ConfigItem, error) {
@@ -76,6 +78,38 @@ func (t TempCache) Insert(item models.ConfigItem) {
7678
t.items[strings.ToLower(item.ID)] = item
7779
}
7880

81+
func (t TempCache) IsChangePersisted(configID, externalChangeID string) (bool, error) {
82+
if configID == "" || externalChangeID == "" {
83+
return false, nil
84+
}
85+
86+
configID = strings.ToLower(configID)
87+
externalChangeID = strings.ToLower(externalChangeID)
88+
89+
if t.changes == nil {
90+
t.changes = make(map[string]struct{})
91+
}
92+
93+
if _, ok := t.changes[configID+externalChangeID]; ok {
94+
return true, nil
95+
}
96+
97+
var result models.ConfigChange
98+
if err := t.ctx.DB().Select("id").Where("config_id = ?", configID).
99+
Where("external_change_id = ?", externalChangeID).
100+
Limit(1).
101+
Find(&result).Error; err != nil {
102+
return false, err
103+
}
104+
105+
if result.ID != "" {
106+
t.changes[configID+externalChangeID] = struct{}{}
107+
return true, nil
108+
}
109+
110+
return false, nil
111+
}
112+
79113
func (t TempCache) Get(id string) (*models.ConfigItem, error) {
80114
id = strings.ToLower(id)
81115
if id == "" {

db/models/config_change.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
v1 "github.com/flanksource/config-db/api/v1"
88
"github.com/google/uuid"
99
"gorm.io/gorm"
10-
"gorm.io/gorm/clause"
1110
)
1211

1312
// ConfigChange represents the config change database table
@@ -66,6 +65,5 @@ func (c *ConfigChange) BeforeCreate(tx *gorm.DB) (err error) {
6665
c.ID = uuid.New().String()
6766
}
6867

69-
tx.Statement.AddClause(clause.OnConflict{DoNothing: true})
7068
return
7169
}

db/update.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ func extractChanges(ctx api.ScrapeContext, result *v1.ScrapeResult, ci *models.C
269269
if changeResult.UpdateExisting {
270270
updates = append(updates, change)
271271
} else {
272-
newOnes = append(newOnes, change)
272+
if ok, err := ctx.TempCache().IsChangePersisted(change.ConfigID, change.ExternalChangeId); err != nil {
273+
return nil, nil, fmt.Errorf("failed to check if change is persisted: %w", err)
274+
} else if !ok {
275+
newOnes = append(newOnes, change)
276+
}
273277
}
274278
}
275279

0 commit comments

Comments
 (0)