2016-04-13 07:08:22 -07:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-05-06 07:53:12 -07:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2015-05-21 08:50:06 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-05-06 07:53:12 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-05-06 07:53:12 -07:00
|
|
|
)
|
|
|
|
|
2015-05-08 09:10:56 -07:00
|
|
|
const maxMappedFP = 1 << 20 // About 1M fingerprints reserved for mapping.
|
2015-05-06 07:53:12 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
var separatorString = string([]byte{model.SeparatorByte})
|
2015-05-06 07:53:12 -07:00
|
|
|
|
|
|
|
// fpMappings maps original fingerprints to a map of string representations of
|
|
|
|
// metrics to the truly unique fingerprint.
|
2015-08-20 08:18:46 -07:00
|
|
|
type fpMappings map[model.Fingerprint]map[string]model.Fingerprint
|
2015-05-06 07:53:12 -07:00
|
|
|
|
|
|
|
// fpMapper is used to map fingerprints in order to work around fingerprint
|
|
|
|
// collisions.
|
|
|
|
type fpMapper struct {
|
2015-05-20 16:37:04 -07:00
|
|
|
// highestMappedFP has to be aligned for atomic operations.
|
2015-08-20 08:18:46 -07:00
|
|
|
highestMappedFP model.Fingerprint
|
2015-05-20 16:37:04 -07:00
|
|
|
|
2015-05-08 04:35:39 -07:00
|
|
|
mtx sync.RWMutex // Protects mappings.
|
2015-05-06 07:53:12 -07:00
|
|
|
mappings fpMappings
|
|
|
|
|
2015-05-20 16:37:04 -07:00
|
|
|
fpToSeries *seriesMap
|
|
|
|
p *persistence
|
2015-05-21 08:50:06 -07:00
|
|
|
|
|
|
|
mappingsCounter prometheus.Counter
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFPMapper loads the collision map from the persistence and
|
2015-05-08 04:35:39 -07:00
|
|
|
// returns an fpMapper ready to use.
|
2015-05-06 07:53:12 -07:00
|
|
|
func newFPMapper(fpToSeries *seriesMap, p *persistence) (*fpMapper, error) {
|
2015-05-21 08:50:06 -07:00
|
|
|
m := &fpMapper{
|
2015-05-06 07:53:12 -07:00
|
|
|
fpToSeries: fpToSeries,
|
|
|
|
p: p,
|
2015-05-21 08:50:06 -07:00
|
|
|
mappingsCounter: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "fingerprint_mappings_total",
|
|
|
|
Help: "The total number of fingerprints being mapped to avoid collisions.",
|
|
|
|
}),
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
mappings, nextFP, err := p.loadFPMappings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-21 08:50:06 -07:00
|
|
|
m.mappings = mappings
|
|
|
|
m.mappingsCounter.Set(float64(len(m.mappings)))
|
|
|
|
m.highestMappedFP = nextFP
|
|
|
|
return m, nil
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
// checkpoint persists the current mappings. The caller has to ensure that the
|
|
|
|
// provided mappings are not changed concurrently. This method is only called
|
|
|
|
// upon shutdown, when no samples are ingested anymore.
|
|
|
|
func (m *fpMapper) checkpoint() error {
|
|
|
|
return m.p.checkpointFPMappings(m.mappings)
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:53:12 -07:00
|
|
|
// mapFP takes a raw fingerprint (as returned by Metrics.FastFingerprint) and
|
|
|
|
// returns a truly unique fingerprint. The caller must have locked the raw
|
|
|
|
// fingerprint.
|
|
|
|
//
|
|
|
|
// If an error is encountered, it is returned together with the unchanged raw
|
|
|
|
// fingerprint.
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
func (m *fpMapper) mapFP(fp model.Fingerprint, metric model.Metric) model.Fingerprint {
|
2015-05-06 07:53:12 -07:00
|
|
|
// First check if we are in the reserved FP space, in which case this is
|
|
|
|
// automatically a collision that has to be mapped.
|
|
|
|
if fp <= maxMappedFP {
|
2015-05-21 08:50:06 -07:00
|
|
|
return m.maybeAddMapping(fp, metric)
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then check the most likely case: This fp belongs to a series that is
|
|
|
|
// already in memory.
|
2015-05-21 08:50:06 -07:00
|
|
|
s, ok := m.fpToSeries.get(fp)
|
2015-05-06 07:53:12 -07:00
|
|
|
if ok {
|
|
|
|
// FP exists in memory, but is it for the same metric?
|
2015-05-21 08:50:06 -07:00
|
|
|
if metric.Equal(s.metric) {
|
2015-05-06 07:53:12 -07:00
|
|
|
// Yupp. We are done.
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
return fp
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
// Collision detected!
|
2015-05-21 08:50:06 -07:00
|
|
|
return m.maybeAddMapping(fp, metric)
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
2015-05-08 07:36:46 -07:00
|
|
|
// Metric is not in memory. Before doing the expensive archive lookup,
|
|
|
|
// check if we have a mapping for this metric in place already.
|
2015-05-21 08:50:06 -07:00
|
|
|
m.mtx.RLock()
|
|
|
|
mappedFPs, fpAlreadyMapped := m.mappings[fp]
|
|
|
|
m.mtx.RUnlock()
|
2015-05-08 07:36:46 -07:00
|
|
|
if fpAlreadyMapped {
|
|
|
|
// We indeed have mapped fp historically.
|
2015-05-21 08:50:06 -07:00
|
|
|
ms := metricToUniqueString(metric)
|
2015-05-08 07:36:46 -07:00
|
|
|
// fp is locked by the caller, so no further locking of
|
|
|
|
// 'collisions' required (it is specific to fp).
|
|
|
|
mappedFP, ok := mappedFPs[ms]
|
|
|
|
if ok {
|
|
|
|
// Historical mapping found, return the mapped FP.
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
return mappedFP
|
2015-05-08 07:36:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we are here, FP does not exist in memory and is either not mapped
|
|
|
|
// at all, or existing mappings for FP are not for m. Check if we have
|
|
|
|
// something for FP in the archive.
|
2015-05-21 08:50:06 -07:00
|
|
|
archivedMetric, err := m.p.archivedMetric(fp)
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
if err != nil || archivedMetric == nil {
|
|
|
|
// Either the archive lookup has returend an error, or fp does
|
|
|
|
// not exist in the archive. In the former case, the storage has
|
|
|
|
// been marked as dirty already. We just carry on for as long as
|
|
|
|
// it goes, assuming that fp does not exist. In either case,
|
|
|
|
// since now we know (or assume) now that fp does not exist,
|
|
|
|
// neither in memory nor in archive, we can safely keep it
|
|
|
|
// unmapped.
|
|
|
|
return fp
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
// FP exists in archive, but is it for the same metric?
|
|
|
|
if metric.Equal(archivedMetric) {
|
|
|
|
// Yupp. We are done.
|
|
|
|
return fp
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
// Collision detected!
|
|
|
|
return m.maybeAddMapping(fp, metric)
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybeAddMapping is only used internally. It takes a detected collision and
|
|
|
|
// adds it to the collisions map if not yet there. In any case, it returns the
|
|
|
|
// truly unique fingerprint for the colliding metric.
|
2015-05-21 08:50:06 -07:00
|
|
|
func (m *fpMapper) maybeAddMapping(
|
2015-08-20 08:18:46 -07:00
|
|
|
fp model.Fingerprint,
|
|
|
|
collidingMetric model.Metric,
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
) model.Fingerprint {
|
2015-05-06 07:53:12 -07:00
|
|
|
ms := metricToUniqueString(collidingMetric)
|
2015-05-21 08:50:06 -07:00
|
|
|
m.mtx.RLock()
|
|
|
|
mappedFPs, ok := m.mappings[fp]
|
|
|
|
m.mtx.RUnlock()
|
2015-05-06 07:53:12 -07:00
|
|
|
if ok {
|
|
|
|
// fp is locked by the caller, so no further locking required.
|
|
|
|
mappedFP, ok := mappedFPs[ms]
|
|
|
|
if ok {
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
return mappedFP // Existing mapping.
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
// A new mapping has to be created.
|
2015-05-21 08:50:06 -07:00
|
|
|
mappedFP = m.nextMappedFP()
|
2015-05-06 07:53:12 -07:00
|
|
|
mappedFPs[ms] = mappedFP
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Infof(
|
2015-05-06 07:53:12 -07:00
|
|
|
"Collision detected for fingerprint %v, metric %v, mapping to new fingerprint %v.",
|
|
|
|
fp, collidingMetric, mappedFP,
|
|
|
|
)
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
return mappedFP
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
// This is the first collision for fp.
|
2015-05-21 08:50:06 -07:00
|
|
|
mappedFP := m.nextMappedFP()
|
2015-08-20 08:18:46 -07:00
|
|
|
mappedFPs = map[string]model.Fingerprint{ms: mappedFP}
|
2015-05-21 08:50:06 -07:00
|
|
|
m.mtx.Lock()
|
|
|
|
m.mappings[fp] = mappedFPs
|
|
|
|
m.mappingsCounter.Inc()
|
|
|
|
m.mtx.Unlock()
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Infof(
|
2015-05-06 07:53:12 -07:00
|
|
|
"Collision detected for fingerprint %v, metric %v, mapping to new fingerprint %v.",
|
|
|
|
fp, collidingMetric, mappedFP,
|
|
|
|
)
|
Checkpoint fingerprint mappings only upon shutdown
Before, we checkpointed after every newly detected fingerprint
collision, which is not a problem as long as collisions are
rare. However, with a sufficient number of metrics or particular
nature of the data set, there might be a lot of collisions, all to be
detected upon the first set of scrapes, and then the checkpointing
after each detection will take a quite long time (it's O(n²),
essentially).
Since we are rebuilding the fingerprint mapping during crash recovery,
the previous, very conservative approach didn't even buy us
anything. We only ever read from the checkpoint file after a clean
shutdown, so the only time we need to write the checkpoint file is
during a clean shutdown.
2016-04-14 07:02:37 -07:00
|
|
|
return mappedFP
|
2015-05-06 07:53:12 -07:00
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
func (m *fpMapper) nextMappedFP() model.Fingerprint {
|
|
|
|
mappedFP := model.Fingerprint(atomic.AddUint64((*uint64)(&m.highestMappedFP), 1))
|
2015-05-06 07:53:12 -07:00
|
|
|
if mappedFP > maxMappedFP {
|
|
|
|
panic(fmt.Errorf("more than %v fingerprints mapped in collision detection", maxMappedFP))
|
|
|
|
}
|
|
|
|
return mappedFP
|
|
|
|
}
|
|
|
|
|
2015-05-21 08:50:06 -07:00
|
|
|
// Describe implements prometheus.Collector.
|
|
|
|
func (m *fpMapper) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- m.mappingsCounter.Desc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements prometheus.Collector.
|
|
|
|
func (m *fpMapper) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
ch <- m.mappingsCounter
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:53:12 -07:00
|
|
|
// metricToUniqueString turns a metric into a string in a reproducible and
|
|
|
|
// unique way, i.e. the same metric will always create the same string, and
|
|
|
|
// different metrics will always create different strings. In a way, it is the
|
|
|
|
// "ideal" fingerprint function, only that it is more expensive than the
|
|
|
|
// FastFingerprint function, and its result is not suitable as a key for maps
|
|
|
|
// and indexes as it might become really large, causing a lot of hashing effort
|
|
|
|
// in maps and a lot of storage overhead in indexes.
|
2015-08-20 08:18:46 -07:00
|
|
|
func metricToUniqueString(m model.Metric) string {
|
2015-05-06 07:53:12 -07:00
|
|
|
parts := make([]string, 0, len(m))
|
|
|
|
for ln, lv := range m {
|
|
|
|
parts = append(parts, string(ln)+separatorString+string(lv))
|
|
|
|
}
|
|
|
|
sort.Strings(parts)
|
|
|
|
return strings.Join(parts, separatorString)
|
|
|
|
}
|