prometheus 创建db
openDBWithMetrics
参数dir 是data路径,默认是data
目录,也可在cfg.localStoragePath
指定。open
调用os.MkdirAll
创建数据目录open
执行walDir := filepath.Join(dir, "wal")
生成wal目录MigrateWAL
Migrate old WAL if one exists, 暂不清楚什么情况会migrate&DB
dir 赋值给db,并创建dbwal.NewSize
创建wal,这里指定了wal目录和segmentSize
,默认是*DefaultSegmentSize* = 128 * 1024 * 1024 // 128 MB
。创建成功返回wlog
NewHead
创建head,这里传入前面生成的wlog
db.reload()
reload reloads blocks and truncates the head and its WALdb.Blocks()
获取了磁盘上的blocks,并通过blocks[len(blocks)-1].Meta().MaxTime
指定了minValidTime
db.head.Init(minValidTime)
使用上一步的minValidTime
来初始化head最后启动db
go db.run()
并返回db对象。
New Wal
NewSize
传入了wal dir 和segmentSize
listSegments
执行具体findSegments
文件操作,并将文件名排序check
segmentSize%*pageSize* != 0
,pageSize
是32kbos.MkdirAll
创建wal dirSegments
根据传入的wal dir 获取其中的Segments
writeSegmentIndex = last + 1
确定最新的SegmentIndex
CreateSegment(w.Dir(), writeSegmentIndex)
根据wal dir 和最新的SegmentIndex
,新建segment
文件w.setSegment(segment)
将segment
赋值给wal,w.donePages = int(stat.Size() / *pageSize*)
初始化了当前的pageSize
New head
NewHead
传入了wal对象和HeadOptions
,HeadOptions
包含了head_chunk 的root 目录,即data目录、StripeSize
、db.chunkPool
mmappedChunksDir
在data目录下创建chunks_head
目录openMMapFiles
MemPostings
make([]map[uint64]*memSeries, stripeSize)
make([]seriesHashmap, stripeSize)
make([]stripeLock, stripeSize)
newStripeSeries
NewUnorderedMemPostings
创建postingh.minTime.Store(math.*MaxInt64*)
、h.maxTime.Store(math.*MinInt64*)
设置head的max、min 时间NewChunkDiskMapper
mmap head_chunk
db reload
reload
reloads blocks and truncates the head and its WALtruncateMemory
首先removes old data before mint from the head,主要是head_chunktruncateWAL
removes old data before mint from the WAL.调用
h.series.gc
deleteh.postings.Delete
delete posting:= h.chunkDiskMapper.Truncate(mint);
Truncate the chunk m-mapperinitialize := h.MinTime() == math.*MaxInt64
* 来判断是否是初始化的head,如果是就不用truncate调用
h.gc()
进行deletewal.Segments(h.wal.Dir())
获取wal 所有文件根据要删除的index
h.wal.Truncate(last + 1)
执行truncate首先通过
blockDirs
加载目录deletableULIDs := db.blocksToDelete(loadable)
check 初加载的blocks,判断哪些block 可删除基于初加载的blocks和可删除的blocks,确认最终加载的blocks。并赋值给db.blocks
db.blocks = toLoad
db.deleteBlocks(deletable)
delete 废弃的blocks通过
readMetaFile
读取元文件如果这个block 未打开,通过
OpenBlock
打开该blockopenBlocks
加载blocks目录中 blocks,主要是其meta文件Truncate
removes old data before mint from the head and WAL