聊聊tempo的ExclusiveQueues

本文详细介绍了tempo的ExclusiveQueues结构及其核心方法,包括Enqueue、Requeue、Dequeue、Clear和Stop。ExclusiveQueues维护了一个由PriorityQueue组成的队列数组,用于高效管理和调度操作。Enqueue确保每个key唯一,Requeue计算队列索引并添加操作,Dequeue从指定队列中取出操作,Clear释放操作,而Stop关闭所有队列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要研究一下tempo的ExclusiveQueues

ExclusiveQueues

tempo/pkg/flushqueues/exclusivequeues.go

type ExclusiveQueues struct {
	queues     []*util.PriorityQueue
	index      *atomic.Int32
	activeKeys sync.Map
}

ExclusiveQueues定义了queues、index、activeKeys属性

New

tempo/pkg/flushqueues/exclusivequeues.go

// New creates a new set of flush queues with a prom gauge to track current depth
func New(queues int, metric prometheus.Gauge) *ExclusiveQueues {
	f := &ExclusiveQueues{
		queues: make([]*util.PriorityQueue, queues),
		index:  atomic.NewInt32(0),
	}

	for j := 0; j < queues; j++ {
		f.queues[j] = util.NewPriorityQueue(metric)
	}

	return f
}

New方法先创建ExclusiveQueues,然后根据指定的queue个数通过util.NewPriorityQueue(metric)创建PriorityQueue

Enqueue

tempo/pkg/flushqueues/exclusivequeues.go

// Enqueue adds the op to the next queue and prevents any other items to be added with this key
func (f *ExclusiveQueues) Enqueue(op util.Op) {
	_, ok := f.activeKeys.Load(op.Key())
	if ok {
		return
	}

	f.activeKeys.Store(op.Key(), struct{}{})
	f.Requeue(op)
}

Enqueue方法先从activeKeys查找指定的key,若已经存在则提前返回,不存在则放入activeKeys中,然后执行f.Requeue(op)

Requeue

tempo/pkg/flushqueues/exclusivequeues.go

// Requeue adds an op that is presumed to already be covered by activeKeys
func (f *ExclusiveQueues) Requeue(op util.Op) {
	flushQueueIndex := int(f.index.Inc()) % len(f.queues)
	f.queues[flushQueueIndex].Enqueue(op)
}

Requeue方法首先通过int(f.index.Inc()) % len(f.queues)计算flushQueueIndex,然后找到对应的queue,执行Enqueue方法

Dequeue

tempo/pkg/flushqueues/exclusivequeues.go

// Dequeue removes the next op from the requested queue.  After dequeueing the calling
//  process either needs to call ClearKey or Requeue
func (f *ExclusiveQueues) Dequeue(q int) util.Op {
	return f.queues[q].Dequeue()
}

Dequeue方法执行f.queues[q]对应queue的Dequeue

Clear

tempo/pkg/flushqueues/exclusivequeues.go

// Clear unblocks the requested op.  This should be called only after a flush has been successful
func (f *ExclusiveQueues) Clear(op util.Op) {
	f.activeKeys.Delete(op.Key())
}

Clear方法将指定key从activeKeys中移除

Stop

tempo/pkg/flushqueues/exclusivequeues.go

// Stop closes all queues
func (f *ExclusiveQueues) Stop() {
	for _, q := range f.queues {
		q.Close()
	}
}

Stop方法遍历f.queues,挨个执行q.Close()

小结

tempo的ExclusiveQueues定义了queues、index、activeKeys属性;它提供了Enqueue、Requeue、Dequeue、Clear、Stop方法。

doc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值