34.3 m3db-oom的内存火焰图和内存分配器加油模型源码解读

本节重点介绍 :

  • m3dbnode oom时内存火焰图追查源码调用
  • 内存分配器加油模型源码解读
  • 高基数查询导致m3db oom

m3dbnode oom

oom时排查监控曲线

  • 内存火焰图: 80G内存
  • bytes_pool_get_on_empty qps 很高
  • db read qps增长 80%
  • node cpu kernel 暴涨

看图结论

  • m3dbnode 内存oom过程很短,很剧烈:总时间不超过7分钟
  • 内存从27G增长到250G
  • 节点sys态cpu暴涨:因为大量的mem_alloca sys_call
  • 内存增长曲线和db_read_qps曲线和bytes_pool_get_on_empty曲线高度吻合
  • 内存火焰图: 27G的rpc 40G的pool.(*objectPool).tryFill

查看代码,追踪火焰图中这个tryFill

内存分配器

  • 目的很简单:自己管理内存
  • 避免频繁的mem_allocate
  • sys_call提升速度,空间换时间

核心结构 objectPool

  • 位置 D:\go_path\pkg\mod\github.com\m3db\m3@v1.1.0\src\x\pool\object.go
go 复制代码
type objectPool struct {
	opts                ObjectPoolOptions
	values              chan interface{}
	alloc               Allocator
	size                int
	refillLowWatermark  int
	refillHighWatermark int
	filling             int32
	initialized         int32
	dice                int32
	metrics             objectPoolMetrics
}
  • 初始化时调用Init 向池中注入
go 复制代码
func (p *objectPool) Init(alloc Allocator) {
	if !atomic.CompareAndSwapInt32(&p.initialized, 0, 1) {
		p.onPoolAccessErrorFn(errPoolAlreadyInitialized)
		return
	}

	p.values = make(chan interface{}, p.size)
	for i := 0; i < cap(p.values); i++ {
		p.values <- alloc()
	}

	p.alloc = alloc
	p.setGauges()
}

从池中获取对象时

  • 池中还有剩余则直接获取
  • 否则走各自的alloc分配,同时设置这个 bytes_pool_get_on_empty指标+1
go 复制代码
func (p *objectPool) Get() interface{} {
	var (
		metrics = p.metrics
		v       interface{}
	)

	select {
	case v = <-p.values:
	default:
		v = p.alloc()
		metrics.getOnEmpty.Inc(1)
	}

	if unsafe.Fastrandn(sampleObjectPoolLengthEvery) == 0 {
		// inlined setGauges()
		metrics.free.Update(float64(len(p.values)))
		metrics.total.Update(float64(p.size))
	}

	if p.refillLowWatermark > 0 && len(p.values) <= p.refillLowWatermark {
		p.tryFill()
	}

	return v
}

每次Get同时判断池水位,是否加油

go 复制代码
	if p.refillLowWatermark > 0 && len(p.values) <= p.refillLowWatermark {
		p.tryFill()
	}

加油过程

  • 用CompareAndSwapInt32做并发控制标志位
  • 加油加到refillHighWatermark
go 复制代码
func (p *objectPool) tryFill() {
	if !atomic.CompareAndSwapInt32(&p.filling, 0, 1) {
		return
	}

	go func() {
		defer atomic.StoreInt32(&p.filling, 0)

		for len(p.values) < p.refillHighWatermark {
			select {
			case p.values <- p.alloc():
			default:
				return
			}
		}
	}()
}

默认池参数

go 复制代码
	defaultRefillLowWaterMark  = 0.3
	defaultRefillHighWaterMark = 0.6

总结思考

  • 默认池低水位为什么不是0:因为 从水位判断到tryFill中间的并发请求使得最后tryFill开始时低水位可能低于0.3
  • 火焰图中的tryFill消耗了40G内存不是一次性的,类比右侧thriftrpc27,属于累加内存消耗值
  • 一次性的内存消耗肯定没有这么多:每次加油时内存消耗低于初始化
  • 所以可以得到结论,oom是因为在当时byte_pool频繁的get消耗,然后tryFill频繁的加油导致内存分配
  • 所以根本原因还是查询导致的

临时解决办法:限制query资源消耗保护db

  • 首先要明确的几点,因为remote_read是链式的调用
  • 所以限制m3db前面的组件prometheusm3coordinator是没用的
  • 只能限制m3db中关于query的参数,但是这个方法不根治

上面的方法治标不治本,重要的是解决高基数/重查询的问题

本节重点总结 :

  • m3dbnode oom时内存火焰图追查源码调用
  • 内存分配器加油模型源码解读
  • 高基数查询导致m3db oom
相关推荐
梵得儿SHI3 天前
(第二篇)Spring AI 架构设计与优化:可观察性体系,打造全链路可视化的 AI 运维方案
人工智能·微服务·grafana·prometheus·监控·可观察性·spring ai
川石课堂软件测试3 天前
AI如何赋能软件测试行业的发展
人工智能·python·功能测试·网络协议·单元测试·测试用例·prometheus
脏脏a3 天前
监控面板全绿但用户说网站打不开?Prometheus+Blackbox从外部验证服务真实可用性
数据库·prometheus
.柒宇.4 天前
prometheus-入门与安装
运维·服务器·prometheus·监控
川石课堂软件测试6 天前
技术分享|JMeter接口与性能测试实战
数据库·功能测试·测试工具·jmeter·单元测试·postman·prometheus
是店小二呀6 天前
Prometheus监控盲区:Pushgateway如何收拢离线任务指标
prometheus
xmlhcxr7 天前
基于 HAProxy+Keepalived 构建高可用 ZrLog 博客系统及监控平台实现(Prometheus + Grafana)
架构·grafana·prometheus
川石课堂软件测试7 天前
软件测试:典型面试题库
数据库·python·功能测试·mysql·单元测试·grafana·prometheus
何中应9 天前
Promehteus如何指定数据路径
运维·prometheus·监控
Elastic 中国社区官方博客9 天前
使用 Remote Write 将 Prometheus 指标发送到 Elasticsearch
大数据·运维·elasticsearch·搜索引擎·全文检索·prometheus