mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-02 08:31:11 -08:00
Simplify creation of new series
This commit is contained in:
parent
0ca755b4ae
commit
ca5791efbc
44
head.go
44
head.go
|
@ -261,12 +261,12 @@ var (
|
||||||
func (h *headBlock) appendBatch(samples []hashedSample) (int, error) {
|
func (h *headBlock) appendBatch(samples []hashedSample) (int, error) {
|
||||||
// Find head chunks for all samples and allocate new IDs/refs for
|
// Find head chunks for all samples and allocate new IDs/refs for
|
||||||
// ones we haven't seen before.
|
// ones we haven't seen before.
|
||||||
|
|
||||||
var (
|
var (
|
||||||
newSeries []labels.Labels
|
newSeries = map[uint64][]*hashedSample{}
|
||||||
newSamples []*hashedSample
|
newLabels []labels.Labels
|
||||||
newHashes []uint64
|
|
||||||
uniqueHashes = map[uint64]uint32{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
h.mtx.RLock()
|
h.mtx.RLock()
|
||||||
defer h.mtx.RUnlock()
|
defer h.mtx.RUnlock()
|
||||||
|
|
||||||
|
@ -289,19 +289,9 @@ func (h *headBlock) appendBatch(samples []hashedSample) (int, error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// There may be several samples for a new series in a batch.
|
// TODO(fabxc): technically there's still collision probability here.
|
||||||
// We don't want to reserve a new space for each.
|
// Extract the hashmap of the head block and use an instance of it here as well.
|
||||||
if ref, ok := uniqueHashes[s.hash]; ok {
|
newSeries[s.hash] = append(newSeries[s.hash], s)
|
||||||
s.ref = ref
|
|
||||||
newSamples = append(newSamples, s)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
s.ref = uint32(len(newSeries))
|
|
||||||
uniqueHashes[s.hash] = s.ref
|
|
||||||
|
|
||||||
newSeries = append(newSeries, s.labels)
|
|
||||||
newHashes = append(newHashes, s.hash)
|
|
||||||
newSamples = append(newSamples, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// After the samples were successfully written to the WAL, there may
|
// After the samples were successfully written to the WAL, there may
|
||||||
|
@ -316,19 +306,27 @@ func (h *headBlock) appendBatch(samples []hashedSample) (int, error) {
|
||||||
|
|
||||||
base := len(h.series)
|
base := len(h.series)
|
||||||
|
|
||||||
for i, s := range newSeries {
|
for hash, ser := range newSeries {
|
||||||
h.create(newHashes[i], s)
|
h.create(hash, ser[0].labels)
|
||||||
}
|
|
||||||
for _, s := range newSamples {
|
|
||||||
s.ref = uint32(base) + s.ref
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h.mtx.Unlock()
|
h.mtx.Unlock()
|
||||||
h.mtx.RLock()
|
h.mtx.RLock()
|
||||||
|
|
||||||
|
newLabels = make([]labels.Labels, 0, len(newSeries))
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for _, ser := range newSeries {
|
||||||
|
newLabels = append(newLabels, ser[0].labels)
|
||||||
|
for _, s := range ser {
|
||||||
|
s.ref = uint32(base + i)
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Write all new series and samples to the WAL and add it to the
|
// Write all new series and samples to the WAL and add it to the
|
||||||
// in-mem database on success.
|
// in-mem database on success.
|
||||||
if err := h.wal.Log(newSeries, samples); err != nil {
|
if err := h.wal.Log(newLabels, samples); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue