自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Prometheus時(shí)序數(shù)據(jù)庫-數(shù)據(jù)的插入

運(yùn)維 數(shù)據(jù)庫運(yùn)維
筆者詳細(xì)的闡述了Prometheus時(shí)序數(shù)據(jù)庫在內(nèi)存和磁盤中的存儲(chǔ)結(jié)構(gòu)。有了前面的鋪墊,筆者就可以在本篇文章闡述下數(shù)據(jù)的插入過程。

[[385979]]

前言

在之前的文章里,筆者詳細(xì)的闡述了Prometheus時(shí)序數(shù)據(jù)庫在內(nèi)存和磁盤中的存儲(chǔ)結(jié)構(gòu)。有了前面的鋪墊,筆者就可以在本篇文章闡述下數(shù)據(jù)的插入過程。

監(jiān)控?cái)?shù)據(jù)的插入

在這里,筆者并不會(huì)去討論P(yáng)romtheus向各個(gè)Endpoint抓取數(shù)據(jù)的過程。而是僅僅圍繞著數(shù)據(jù)是如何插入Prometheus的過程做下闡述。對(duì)應(yīng)方法:

  1. func (a *headAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) { 
  2.     ...... 
  3.     // 如果lset對(duì)應(yīng)的series沒有,則建一個(gè)。同時(shí)把新建的series放入倒排Posting映射里面 
  4.     s, created := a.head.getOrCreate(lset.Hash(), lset)  
  5.     if created { // 如果新創(chuàng)建了一個(gè),則將新建的也放到a.series里面 
  6.         a.series = append(a.series, record.RefSeries{ 
  7.             Ref:    s.ref, 
  8.             Labels: lset, 
  9.         }) 
  10.     } 
  11.     return s.ref, a.AddFast(s.ref, t, v) 

我們就以下面的add函數(shù)調(diào)用為例:

  1. app.Add(labels.FromStrings("foo""bar"), 0, 0) 

首先是getOrCreate,顧名思義,不存在則創(chuàng)建一個(gè)。創(chuàng)建的過程包含了seriesHashMap/Postings(倒排索引)/LabelIndex的維護(hù)。如下圖所示:

然后是AddFast方法

  1. func (a *headAppender) AddFast(ref uint64, t int64, v float64) error{ 
  2.         // 拿出對(duì)應(yīng)的memSeries 
  3.         s := a.head.series.getByID(ref) 
  4.         ...... 
  5.         // 設(shè)置為等待提交狀態(tài) 
  6.         s.pendingCommit=true 
  7.         ...... 
  8.         // 為了事務(wù)概念,放入temp存儲(chǔ),等待真正commit時(shí)候再寫入memSeries 
  9.         a.samples = append(a.samples, record.RefSample{Ref: ref,T:   t,V:   v,}) 
  10.         //  

Prometheus在add數(shù)據(jù)點(diǎn)的時(shí)候并沒有直接add到memSeries(也就是query所用到的結(jié)構(gòu)體里),而是加入到一個(gè)臨時(shí)的samples切片里面。同時(shí)還將這個(gè)數(shù)據(jù)點(diǎn)對(duì)應(yīng)的memSeries同步增加到另一個(gè)sampleSeries里面。

事務(wù)可見性

為什么要這么做呢?就是為了實(shí)現(xiàn)commit語義,只有commit過后數(shù)據(jù)才可見(能被查詢到)。否則,無法見到這些數(shù)據(jù)。而commit的動(dòng)作主要就是WAL(Write Ahead Log)以及將headerAppender.samples數(shù)據(jù)寫到其對(duì)應(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ù)的特性決定的。

寫入代碼為:

  1. commit() 
  2. |=> 
  3. func (a *headAppender) log() error { 
  4.     ...... 
  5.     // 往WAL寫入對(duì)應(yīng)的series信息 
  6.     if len(a.series) > 0 { 
  7.         rec = enc.Series(a.series, buf) 
  8.         buf = rec[:0] 
  9.  
  10.         if err := a.head.wal.Log(rec); err != nil { 
  11.             return errors.Wrap(err, "log series"
  12.         } 
  13.     } 
  14.     ...... 
  15.     // 往WAL寫入真正的samples 
  16.     if len(a.samples) > 0 { 
  17.         rec = enc.Samples(a.samples, buf) 
  18.         buf = rec[:0] 
  19.  
  20.         if err := a.head.wal.Log(rec); err != nil { 
  21.             return errors.Wrap(err, "log samples"
  22.         } 
  23.     } 

對(duì)應(yīng)的WAL日志格式為:

Series records

  1. ┌────────────────────────────────────────────┐ 
  2. │ type = 1 <1b>                              │ 
  3. ├────────────────────────────────────────────┤ 
  4. │ ┌─────────┬──────────────────────────────┐ │ 
  5. │ │ id <8b> │ n = len(labels) <uvarint>    │ │ 
  6. │ ├─────────┴────────────┬─────────────────┤ │ 
  7. │ │ len(str_1) <uvarint> │ str_1 <bytes>   │ │ 
  8. │ ├──────────────────────┴─────────────────┤ │ 
  9. │ │  ...                                   │ │ 
  10. │ ├───────────────────────┬────────────────┤ │ 
  11. │ │ len(str_2n) <uvarint> │ str_2n <bytes> │ │ 
  12. │ └───────────────────────┴────────────────┘ │ 
  13. │                  . . .                     │ 
  14. └────────────────────────────────────────────┘ 

 

Sample records

  1. ┌──────────────────────────────────────────────────────────────────┐ 
  2. │ type = 2 <1b>                                                    │ 
  3. ├──────────────────────────────────────────────────────────────────┤ 
  4. │ ┌────────────────────┬───────────────────────────┐               │ 
  5. │ │ id <8b>            │ timestamp <8b>            │               │ 
  6. │ └────────────────────┴───────────────────────────┘               │ 
  7. │ ┌────────────────────┬───────────────────────────┬─────────────┐ │ 
  8. │ │ id_delta <uvarint> │ timestamp_delta <uvarint> │ value <8b>  │ │ 
  9. │ └────────────────────┴───────────────────────────┴─────────────┘ │ 
  10. │                              . . .                               │ 
  11. └──────────────────────────────────────────────────────────────────┘ 

見Prometheus WAL.md

落盤存儲(chǔ)

之前描述的所有數(shù)據(jù)都是寫到內(nèi)存里面。最終落地是通過compator routine將每?jī)蓚€(gè)小時(shí)的數(shù)據(jù)打包到一個(gè)Blocks里面。

具體可見筆者之前的博客《Prometheus時(shí)序數(shù)據(jù)庫-磁盤中的存儲(chǔ)結(jié)構(gòu)》

總結(jié)

在這篇文章里,筆者詳細(xì)描述了Prometheus數(shù)據(jù)的插入過程。在下一篇文章里面,筆者會(huì)繼續(xù)

闡述Prometheus數(shù)據(jù)的查詢過程。

本文轉(zhuǎn)載自微信公眾號(hào)「解Bug之路」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系解Bug之路公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 解Bug之路
相關(guān)推薦

2021-03-15 10:10:29

數(shù)據(jù)庫數(shù)據(jù)查詢

2021-03-01 10:20:52

存儲(chǔ)

2021-02-22 10:37:47

存儲(chǔ)Prometheus

2022-07-06 15:41:55

數(shù)據(jù)庫

2022-09-23 07:44:48

時(shí)序數(shù)據(jù)庫物聯(lián)網(wǎng)

2017-11-20 11:37:19

時(shí)序數(shù)據(jù)數(shù)據(jù)存儲(chǔ)HBase

2021-09-26 10:08:33

TSDB時(shí)序數(shù)據(jù)庫壓縮解壓

2022-07-11 10:45:12

數(shù)據(jù)庫分析

2020-03-11 09:50:21

時(shí)序數(shù)據(jù)庫快速檢索

2022-07-11 11:12:32

數(shù)據(jù)分析

2022-12-18 19:38:31

時(shí)序數(shù)據(jù)庫數(shù)據(jù)庫

2021-08-31 14:01:59

時(shí)序數(shù)據(jù)庫數(shù)據(jù)庫數(shù)據(jù)

2022-07-07 12:23:29

數(shù)據(jù)庫

2022-06-10 17:37:37

數(shù)據(jù)庫

2022-07-07 12:37:27

數(shù)據(jù)

2021-08-04 05:49:40

數(shù)據(jù)庫數(shù)時(shí)序數(shù)據(jù)庫技術(shù)

2017-09-05 14:45:14

時(shí)序數(shù)據(jù)數(shù)據(jù)庫大數(shù)據(jù)

2018-06-26 09:37:07

時(shí)序數(shù)據(jù)庫FacebookNoSQL

2019-05-30 08:31:39

數(shù)據(jù)庫QTSDB分布式

2018-04-16 08:44:51

InfluxDB TS時(shí)序數(shù)據(jù)庫存儲(chǔ)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)