Skip to content

Commit a327ef0

Browse files
authored
Merge pull request #525 from Icinga/save-memory
Save memory during config sync via SyncSubject#FactoryForDelta()
2 parents 9fb9a10 + 6209b5b commit a327ef0

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

pkg/common/sync_subject.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"github.com/icinga/icingadb/pkg/contracts"
5+
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
56
"github.com/icinga/icingadb/pkg/utils"
67
)
78

@@ -47,6 +48,18 @@ func (s SyncSubject) Factory() contracts.EntityFactoryFunc {
4748
return s.factory
4849
}
4950

51+
// FactoryForDelta behaves like Factory() unless s is WithChecksum().
52+
// In the latter case it returns a factory for EntityWithChecksum instead.
53+
// Rationale: Sync#ApplyDelta() uses its input entities which are WithChecksum() only for the delta itself
54+
// and not for insertion into the database, so EntityWithChecksum is enough. And it consumes less memory.
55+
func (s SyncSubject) FactoryForDelta() contracts.EntityFactoryFunc {
56+
if s.withChecksum {
57+
return v1.NewEntityWithChecksum
58+
}
59+
60+
return s.factory
61+
}
62+
5063
// Name returns the declared name of the entity.
5164
func (s SyncSubject) Name() string {
5265
return utils.Name(s.entity)

pkg/icingadb/sync.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func (s Sync) Sync(ctx context.Context, subject *common.SyncSubject) error {
8484
}
8585

8686
actual, dbErrs := s.db.YieldAll(
87-
ctx, subject.Factory(), s.db.BuildSelectStmt(NewScopedEntity(subject.Entity(), e.Meta()), subject.Entity().Fingerprint()), e.Meta())
87+
ctx, subject.FactoryForDelta(),
88+
s.db.BuildSelectStmt(NewScopedEntity(subject.Entity(), e.Meta()), subject.Entity().Fingerprint()), e.Meta(),
89+
)
8890
// Let errors from DB cancel our group.
8991
com.ErrgroupReceive(g, dbErrs)
9092

@@ -184,7 +186,9 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
184186
com.ErrgroupReceive(g, errs)
185187

186188
actualCvs, errs := s.db.YieldAll(
187-
ctx, cv.Factory(), s.db.BuildSelectStmt(NewScopedEntity(cv.Entity(), e.Meta()), cv.Entity().Fingerprint()), e.Meta())
189+
ctx, cv.FactoryForDelta(),
190+
s.db.BuildSelectStmt(NewScopedEntity(cv.Entity(), e.Meta()), cv.Entity().Fingerprint()), e.Meta(),
191+
)
188192
com.ErrgroupReceive(g, errs)
189193

190194
g.Go(func() error {
@@ -194,7 +198,9 @@ func (s Sync) SyncCustomvars(ctx context.Context) error {
194198
flatCv := common.NewSyncSubject(v1.NewCustomvarFlat)
195199

196200
actualFlatCvs, errs := s.db.YieldAll(
197-
ctx, flatCv.Factory(), s.db.BuildSelectStmt(NewScopedEntity(flatCv.Entity(), e.Meta()), flatCv.Entity().Fingerprint()), e.Meta())
201+
ctx, flatCv.FactoryForDelta(),
202+
s.db.BuildSelectStmt(NewScopedEntity(flatCv.Entity(), e.Meta()), flatCv.Entity().Fingerprint()), e.Meta(),
203+
)
198204
com.ErrgroupReceive(g, errs)
199205

200206
g.Go(func() error {

pkg/icingadb/v1/entity.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ type EntityWithChecksum struct {
2222
func (e EntityWithChecksum) Fingerprint() contracts.Fingerprinter {
2323
return e
2424
}
25+
26+
func NewEntityWithChecksum() contracts.Entity {
27+
return &EntityWithChecksum{}
28+
}

pkg/icingaredis/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (c Client) YieldAll(ctx context.Context, subject *common.SyncSubject) (<-ch
222222
// Let errors from HYield cancel the group.
223223
com.ErrgroupReceive(g, errs)
224224

225-
desired, errs := CreateEntities(ctx, subject.Factory(), pairs, runtime.NumCPU())
225+
desired, errs := CreateEntities(ctx, subject.FactoryForDelta(), pairs, runtime.NumCPU())
226226
// Let errors from CreateEntities cancel the group.
227227
com.ErrgroupReceive(g, errs)
228228

0 commit comments

Comments
 (0)