Prometheus時(shí)序數(shù)據(jù)庫-數(shù)據(jù)的插入
前言
在之前的文章里,筆者詳細(xì)的闡述了Prometheus時(shí)序數(shù)據(jù)庫在內(nèi)存和磁盤中的存儲結(jié)構(gòu)。有了前面的鋪墊,筆者就可以在本篇文章闡述下數(shù)據(jù)的插入過程。
監(jiān)控?cái)?shù)據(jù)的插入
在這里,筆者并不會去討論P(yáng)romtheus向各個(gè)Endpoint抓取數(shù)據(jù)的過程。而是僅僅圍繞著數(shù)據(jù)是如何插入Prometheus的過程做下闡述。對應(yīng)方法:
- func (a *headAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
 - ......
 - // 如果lset對應(yīng)的series沒有,則建一個(gè)。同時(shí)把新建的series放入倒排Posting映射里面
 - s, created := a.head.getOrCreate(lset.Hash(), lset)
 - if created { // 如果新創(chuàng)建了一個(gè),則將新建的也放到a.series里面
 - a.series = append(a.series, record.RefSeries{
 - Ref: s.ref,
 - Labels: lset,
 - })
 - }
 - return s.ref, a.AddFast(s.ref, t, v)
 - }
 
我們就以下面的add函數(shù)調(diào)用為例:
- app.Add(labels.FromStrings("foo", "bar"), 0, 0)
 
首先是getOrCreate,顧名思義,不存在則創(chuàng)建一個(gè)。創(chuàng)建的過程包含了seriesHashMap/Postings(倒排索引)/LabelIndex的維護(hù)。如下圖所示:
然后是AddFast方法
- func (a *headAppender) AddFast(ref uint64, t int64, v float64) error{
 - // 拿出對應(yīng)的memSeries
 - s := a.head.series.getByID(ref)
 - ......
 - // 設(shè)置為等待提交狀態(tài)
 - s.pendingCommit=true
 - ......
 - // 為了事務(wù)概念,放入temp存儲,等待真正commit時(shí)候再寫入memSeries
 - a.samples = append(a.samples, record.RefSample{Ref: ref,T: t,V: v,})
 - //
 - }
 
Prometheus在add數(shù)據(jù)點(diǎn)的時(shí)候并沒有直接add到memSeries(也就是query所用到的結(jié)構(gòu)體里),而是加入到一個(gè)臨時(shí)的samples切片里面。同時(shí)還將這個(gè)數(shù)據(jù)點(diǎn)對應(yīng)的memSeries同步增加到另一個(gè)sampleSeries里面。
事務(wù)可見性
為什么要這么做呢?就是為了實(shí)現(xiàn)commit語義,只有commit過后數(shù)據(jù)才可見(能被查詢到)。否則,無法見到這些數(shù)據(jù)。而commit的動作主要就是WAL(Write Ahead Log)以及將headerAppender.samples數(shù)據(jù)寫到其對應(yīng)的memSeries中。這樣,查詢就可見這些數(shù)據(jù)了,如下圖所示:
WAL
由于Prometheus最近的數(shù)據(jù)是保存在內(nèi)存里面的,未防止服務(wù)器宕機(jī)丟失數(shù)據(jù)。其在commit之前先寫了日志W(wǎng)AL。等服務(wù)重啟的時(shí)候,再從WAL日志里面獲取信息并重放。
為了性能,Prometheus了另一個(gè)goroutine去做文件的sync操作,所以并不能保證WAL不丟。進(jìn)而也不能保證監(jiān)控?cái)?shù)據(jù)完全不丟。這點(diǎn)也是監(jiān)控業(yè)務(wù)的特性決定的。
寫入代碼為:
- commit()
 - |=>
 - func (a *headAppender) log() error {
 - ......
 - // 往WAL寫入對應(yīng)的series信息
 - if len(a.series) > 0 {
 - rec = enc.Series(a.series, buf)
 - buf = rec[:0]
 - if err := a.head.wal.Log(rec); err != nil {
 - return errors.Wrap(err, "log series")
 - }
 - }
 - ......
 - // 往WAL寫入真正的samples
 - if len(a.samples) > 0 {
 - rec = enc.Samples(a.samples, buf)
 - buf = rec[:0]
 - if err := a.head.wal.Log(rec); err != nil {
 - return errors.Wrap(err, "log samples")
 - }
 - }
 - }
 
對應(yīng)的WAL日志格式為:
Series records
- ┌────────────────────────────────────────────┐
 - │ type = 1 <1b> │
 - ├────────────────────────────────────────────┤
 - │ ┌─────────┬──────────────────────────────┐ │
 - │ │ id <8b> │ n = len(labels) <uvarint> │ │
 - │ ├─────────┴────────────┬─────────────────┤ │
 - │ │ len(str_1) <uvarint> │ str_1 <bytes> │ │
 - │ ├──────────────────────┴─────────────────┤ │
 - │ │ ... │ │
 - │ ├───────────────────────┬────────────────┤ │
 - │ │ len(str_2n) <uvarint> │ str_2n <bytes> │ │
 - │ └───────────────────────┴────────────────┘ │
 - │ . . . │
 - └────────────────────────────────────────────┘
 
Sample records
- ┌──────────────────────────────────────────────────────────────────┐
 - │ type = 2 <1b> │
 - ├──────────────────────────────────────────────────────────────────┤
 - │ ┌────────────────────┬───────────────────────────┐ │
 - │ │ id <8b> │ timestamp <8b> │ │
 - │ └────────────────────┴───────────────────────────┘ │
 - │ ┌────────────────────┬───────────────────────────┬─────────────┐ │
 - │ │ id_delta <uvarint> │ timestamp_delta <uvarint> │ value <8b> │ │
 - │ └────────────────────┴───────────────────────────┴─────────────┘ │
 - │ . . . │
 - └──────────────────────────────────────────────────────────────────┘
 
見Prometheus WAL.md
落盤存儲
之前描述的所有數(shù)據(jù)都是寫到內(nèi)存里面。最終落地是通過compator routine將每兩個(gè)小時(shí)的數(shù)據(jù)打包到一個(gè)Blocks里面。
具體可見筆者之前的博客《Prometheus時(shí)序數(shù)據(jù)庫-磁盤中的存儲結(jié)構(gòu)》
總結(jié)
在這篇文章里,筆者詳細(xì)描述了Prometheus數(shù)據(jù)的插入過程。在下一篇文章里面,筆者會繼續(xù)
闡述Prometheus數(shù)據(jù)的查詢過程。
本文轉(zhuǎn)載自微信公眾號「解Bug之路」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系解Bug之路公眾號。





















 
 
 











 
 
 
 