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 4024931
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/sharding.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Queries against store gateway which are touching large number of blocks (no matt

# Relabelling

Similar to [promtail](https://grafana.com/docs/loki/latest/clients/promtail/configuration/#relabel_configs) this config follows native [Prometheus relabel-config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config) syntax.
Similar to [promtail](https://grafana.com/docs/loki/latest/send-data/promtail/configuration/#relabel_configs) this config follows native [Prometheus relabel-config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config) syntax.

Currently, thanos only supports the following relabel actions:

Expand Down
70 changes: 55 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,17 @@ 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.RUnlock()
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 +625,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 +652,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 +731,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 +781,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 4024931

Please sign in to comment.