Refactor: extract multitenant collection of 'live' reports

To help clarify subsequent changes
This commit is contained in:
Bryan Boreham
2021-03-28 14:06:21 +01:00
parent b9c8cf6998
commit 5d12b7ff65
2 changed files with 35 additions and 16 deletions

View File

@@ -145,13 +145,6 @@ type AWSCollectorConfig struct {
CollectorAddr string
}
// if StoreInterval is set, reports are merged into here and held until flushed to store
type pendingEntry struct {
sync.Mutex
report report.Report
count int
}
type awsCollector struct {
cfg AWSCollectorConfig
db *dynamodb.DynamoDB
@@ -707,16 +700,8 @@ func (c *awsCollector) Add(ctx context.Context, rep report.Report, buf []byte) e
return nil
}
// We are building up a report in memory; merge into that and it will be saved shortly
rep = c.massageReport(userid, rep)
entry := &pendingEntry{report: report.MakeReport()}
if e, found := c.pending.LoadOrStore(userid, entry); found {
entry = e.(*pendingEntry)
}
entry.Lock()
entry.report.UnsafeMerge(rep)
entry.count++
entry.Unlock()
c.addToLive(ctx, userid, rep)
return nil
}

View File

@@ -0,0 +1,34 @@
package multitenant
// Collect reports from probes per-tenant, and supply them to queriers on demand
import (
"sync"
"context"
"github.com/weaveworks/scope/report"
)
// if StoreInterval is set, reports are merged into here and held until flushed to store
type pendingEntry struct {
sync.Mutex
report *report.Report
}
// We are building up a report in memory; merge into that and it will be saved shortly
// NOTE: may retain a reference to rep; must not be used by caller after this.
func (c *awsCollector) addToLive(ctx context.Context, userid string, rep report.Report) {
entry := &pendingEntry{}
if e, found := c.pending.LoadOrStore(userid, entry); found {
entry = e.(*pendingEntry)
}
entry.Lock()
if entry.report == nil {
entry.report = &rep
} else {
entry.report.UnsafeMerge(rep)
}
entry.Unlock()
}