Skip to content

Commit

Permalink
receive: memoize exemplar/TSDB clients
Browse files Browse the repository at this point in the history
We call this on each Series() so memoize the creation of this slice.

Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
  • Loading branch information
GiedriusS committed Sep 25, 2024
1 parent 5858994 commit 46df56c
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions pkg/receive/multitsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ type MultiTSDB struct {
allowOutOfOrderUpload bool
hashFunc metadata.HashFunc
hashringConfigs []HashringConfig

tsdbClients []store.Client
tsdbClientsNeedUpdate bool

exemplarClients map[string]*exemplars.TSDB
exemplarClientsNeedUpdate bool
}

// NewMultiTSDB creates new MultiTSDB.
Expand All @@ -84,17 +90,19 @@ func NewMultiTSDB(
}

return &MultiTSDB{
dataDir: dataDir,
logger: log.With(l, "component", "multi-tsdb"),
reg: reg,
tsdbOpts: tsdbOpts,
mtx: &sync.RWMutex{},
tenants: map[string]*tenant{},
labels: labels,
tenantLabelName: tenantLabelName,
bucket: bucket,
allowOutOfOrderUpload: allowOutOfOrderUpload,
hashFunc: hashFunc,
dataDir: dataDir,
logger: log.With(l, "component", "multi-tsdb"),
reg: reg,
tsdbOpts: tsdbOpts,
mtx: &sync.RWMutex{},
tenants: map[string]*tenant{},
labels: labels,
tsdbClientsNeedUpdate: true,
exemplarClientsNeedUpdate: true,
tenantLabelName: tenantLabelName,
bucket: bucket,
allowOutOfOrderUpload: allowOutOfOrderUpload,
hashFunc: hashFunc,
}
}

Expand Down Expand Up @@ -434,6 +442,8 @@ func (t *MultiTSDB) Prune(ctx context.Context) error {

level.Info(t.logger).Log("msg", "Pruned tenant", "tenant", tenantID)
delete(t.tenants, tenantID)
t.tsdbClientsNeedUpdate = true
t.exemplarClientsNeedUpdate = true
}

return merr.Err()
Expand Down Expand Up @@ -595,7 +605,16 @@ func (t *MultiTSDB) RemoveLockFilesIfAny() error {

func (t *MultiTSDB) TSDBLocalClients() []store.Client {
t.mtx.RLock()
defer t.mtx.RUnlock()
if !t.tsdbClientsNeedUpdate {
t.mtx.RUnlock()
return t.tsdbClients
}

t.mtx.Lock()
if !t.tsdbClientsNeedUpdate {
return t.tsdbClients
}
defer t.mtx.Unlock()

res := make([]store.Client, 0, len(t.tenants))
for _, tenant := range t.tenants {
Expand All @@ -605,12 +624,25 @@ func (t *MultiTSDB) TSDBLocalClients() []store.Client {
}
}

return res
t.tsdbClientsNeedUpdate = false
t.tsdbClients = res

return t.tsdbClients
}

func (t *MultiTSDB) TSDBExemplars() map[string]*exemplars.TSDB {
t.mtx.RLock()
defer t.mtx.RUnlock()
if !t.exemplarClientsNeedUpdate {
t.mtx.RUnlock()
return t.exemplarClients
}
t.mtx.RUnlock()
t.mtx.Lock()
defer t.mtx.Unlock()

if !t.exemplarClientsNeedUpdate {
return t.exemplarClients
}

res := make(map[string]*exemplars.TSDB, len(t.tenants))
for k, tenant := range t.tenants {
Expand All @@ -619,7 +651,10 @@ func (t *MultiTSDB) TSDBExemplars() map[string]*exemplars.TSDB {
res[k] = e
}
}
return res

t.exemplarClientsNeedUpdate = false
t.exemplarClients = res
return t.exemplarClients
}

func (t *MultiTSDB) TenantStats(limit int, statsByLabelName string, tenantIDs ...string) []status.TenantStats {
Expand Down Expand Up @@ -695,6 +730,8 @@ func (t *MultiTSDB) startTSDB(logger log.Logger, tenantID string, tenant *tenant
if err != nil {
t.mtx.Lock()
delete(t.tenants, tenantID)
t.tsdbClientsNeedUpdate = true
t.exemplarClientsNeedUpdate = true
t.mtx.Unlock()
return err
}
Expand Down Expand Up @@ -743,6 +780,8 @@ func (t *MultiTSDB) getOrLoadTenant(tenantID string, blockingStart bool) (*tenan

tenant = newTenant()
t.tenants[tenantID] = tenant
t.tsdbClientsNeedUpdate = true
t.exemplarClientsNeedUpdate = true
t.mtx.Unlock()

logger := log.With(t.logger, "tenant", tenantID)
Expand Down

0 comments on commit 46df56c

Please sign in to comment.