From aea32b0b4bd2daace4b2908c19520d29f58cfec6 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Wed, 24 Sep 2014 16:55:45 +0200 Subject: [PATCH] Avoid redundant fingerprint calculation. Change-Id: Ief8a165dcfa5030226953346ec9dfe4a7787df1f --- storage/local/series.go | 4 +--- storage/local/storage.go | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/storage/local/series.go b/storage/local/series.go index 9e8401f6d..0d0d43889 100644 --- a/storage/local/series.go +++ b/storage/local/series.go @@ -131,7 +131,7 @@ func newMemorySeries(m clientmodel.Metric) *memorySeries { } } -func (s *memorySeries) add(v *metric.SamplePair, persistQueue chan *persistRequest) { +func (s *memorySeries) add(fp clientmodel.Fingerprint, v *metric.SamplePair, persistQueue chan *persistRequest) { s.mtx.Lock() defer s.mtx.Unlock() @@ -148,8 +148,6 @@ func (s *memorySeries) add(v *metric.SamplePair, persistQueue chan *persistReque s.head().chunk = chunks[0] if len(chunks) > 1 { - fp := s.metric.Fingerprint() - queuePersist := func(cd *chunkDesc) { persistQueue <- &persistRequest{ fingerprint: fp, diff --git a/storage/local/storage.go b/storage/local/storage.go index 4ea81b700..8537d9bc2 100644 --- a/storage/local/storage.go +++ b/storage/local/storage.go @@ -98,15 +98,6 @@ type persistRequest struct { // AppendSamples implements Storage. func (s *memorySeriesStorage) AppendSamples(samples clientmodel.Samples) { - /* - s.mtx.Lock() - defer s.mtx.Unlock() - if s.state != storageServing { - panic("storage is not serving") - } - s.mtx.Unlock() - */ - for _, sample := range samples { s.appendSample(sample) } @@ -118,15 +109,20 @@ func (s *memorySeriesStorage) appendSample(sample *clientmodel.Sample) { s.mtx.Lock() defer s.mtx.Unlock() - series := s.getOrCreateSeries(sample.Metric) - series.add(&metric.SamplePair{ + if s.state != storageServing { + panic("storage is not serving") + } + + fp := sample.Metric.Fingerprint() + series := s.getOrCreateSeries(fp, sample.Metric) + // TODO: Can we release s.mtx here already? + series.add(fp, &metric.SamplePair{ Value: sample.Value, Timestamp: sample.Timestamp, }, s.persistQueue) } -func (s *memorySeriesStorage) getOrCreateSeries(m clientmodel.Metric) *memorySeries { - fp := m.Fingerprint() +func (s *memorySeriesStorage) getOrCreateSeries(fp clientmodel.Fingerprint, m clientmodel.Metric) *memorySeries { series, ok := s.fingerprintToSeries[fp] if !ok { @@ -185,7 +181,7 @@ func (s *memorySeriesStorage) preloadChunksForRange(fp clientmodel.Fingerprint, if err != nil { return nil, err } - series = s.getOrCreateSeries(metric) + series = s.getOrCreateSeries(fp, metric) } } return series.preloadChunksForRange(from, through, s.persistence)