From 71199e2c93c1ff73c62fe13eeac130cce3a5eba2 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Tue, 18 Jun 2013 14:08:58 +0200 Subject: [PATCH] Cache disk fingerprint->metric lookups in memory. --- storage/metric/memory.go | 25 ++++++++++++++++++------- storage/metric/tiered.go | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/storage/metric/memory.go b/storage/metric/memory.go index 1a4af813d..1197e58fc 100644 --- a/storage/metric/memory.go +++ b/storage/metric/memory.go @@ -175,14 +175,28 @@ func (s *memorySeriesStorage) AppendSample(sample model.Sample) error { s.Lock() defer s.Unlock() - metric := sample.Metric - fingerprint := model.NewFingerprintFromMetric(metric) - series, ok := s.fingerprintToSeries[*fingerprint] + fingerprint := model.NewFingerprintFromMetric(sample.Metric) + series := s.getOrCreateSeries(sample.Metric, fingerprint) + series.add(sample.Timestamp, sample.Value) if s.wmCache != nil { s.wmCache.Set(fingerprint, &Watermarks{High: sample.Timestamp}) } + return nil +} + +func (s *memorySeriesStorage) CreateEmptySeries(metric model.Metric) { + s.Lock() + defer s.Unlock() + + fingerprint := model.NewFingerprintFromMetric(metric) + s.getOrCreateSeries(metric, fingerprint) +} + +func (s *memorySeriesStorage) getOrCreateSeries(metric model.Metric, fingerprint *model.Fingerprint) *stream { + series, ok := s.fingerprintToSeries[*fingerprint] + if !ok { series = newStream(metric) s.fingerprintToSeries[*fingerprint] = series @@ -201,10 +215,7 @@ func (s *memorySeriesStorage) AppendSample(sample model.Sample) error { s.labelNameToFingerprints[k] = labelNameValues } } - - series.add(sample.Timestamp, sample.Value) - - return nil + return series } // Append raw samples, bypassing indexing. Only used to add data to views, diff --git a/storage/metric/tiered.go b/storage/metric/tiered.go index bee4ce5f1..c550c75e5 100644 --- a/storage/metric/tiered.go +++ b/storage/metric/tiered.go @@ -666,6 +666,7 @@ func (t *TieredStorage) GetMetricForFingerprint(f *model.Fingerprint) (model.Met } if m == nil { m, err = t.DiskStorage.GetMetricForFingerprint(f) + t.memoryArena.CreateEmptySeries(m) } return m, err }