victoriaMetrics中的一些Sao操作
victoriaMetrics中的一些Sao操作
快速获取当前时间
victoriaMetrics中有一个fasttime库,用于快速获取当前的Unix时间,实现其实挺简单,就是在后台使用一个goroutine不断以1s为周期刷新表示当前时间的变量currentTimestamp,获取的时候直接原子加载该变量即可。其性能约是time.Now()的8倍。
其核心方式就是将主要任务放到后台运行,通过一个中间变量来传递运算结果,以此来通过异步的方式提升性能,但需要业务能包容一定的精度偏差。
func init() {
	go func() {
		ticker := time.NewTicker(time.Second)
		defer ticker.Stop()
		for tm := range ticker.C {
			t := uint64(tm.Unix())
			atomic.StoreUint64(¤tTimestamp, t)
		}
	}()
}
var currentTimestamp = uint64(time.Now().Unix())
// UnixTimestamp returns the current unix timestamp in seconds.
//
// It is faster than time.Now().Unix()
func UnixTimestamp() uint64 {
	return atomic.LoadUint64(¤tTimestamp)
}
计算结构体的哈希值
hashUint64函数中使用xxhash.Sum64计算了结构体Key的哈希值。通过unsafe.Pointer将指针转换为*[]byte类型,byte数组的长度为unsafe.Sizeof(*k),unsafe.Sizeof()返回结构体的字节大小。
如果一个数据为固定的长度,如h的类型为uint64,则可以直接指定长度为8进行转换,如:bp:=([8]byte)(unsafe.Pointer(&h))
需要注意的是
unsafe.Sizeof()返回的是数据结构的大小而不是其指向内容的数据大小,如下返回的slice大小为24,为slice首部数据结构SliceHeader的大小,而不是其引用的数据大小(可以使用len获取slice引用的数据大小)。此外如果结构体中有指针,则转换成的byte中存储的也是指针存储的地址。slice := []int{1,2,3,4,5,6,7,8,9,10}
fmt.Println(unsafe.Sizeof(slice)) //24
type Key struct {
	Part interface{}
	Offset uint64
}
func (k *Key) hashUint64() uint64 {
	buf := (*[unsafe.Sizeof(*k)]byte)(unsafe.Pointer(k))
	return xxhash.Sum64(buf[:])
}
将字符串添加到已有的[]byte中
使用如下方式即可:
str := "1231445"
arr := []byte{1, 2, 3}
arr = append(arr, str...)
将int64的数组转换为byte数组
直接操作了底层的SliceHeader
func int64ToByteSlice(a []int64) (b []byte) {
   sh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
   sh.Data = uintptr(unsafe.Pointer(&a[0]))
   sh.Len = len(a) * int(unsafe.Sizeof(a[0]))
   sh.Cap = sh.Len
   return
}
并发访问的sync.WaitGroup
并发访问的sync.WaitGroup的目的是为了在运行时添加需要等待的goroutine
// WaitGroup wraps sync.WaitGroup and makes safe to call Add/Wait
// from concurrent goroutines.
//
// An additional limitation is that call to Wait prohibits further calls to Add
// until return.
type WaitGroup struct {
	sync.WaitGroup
	mu sync.Mutex
}
// Add registers n additional workers. Add may be called from concurrent goroutines.
func (wg *WaitGroup) Add(n int) {
	wg.mu.Lock()
	wg.WaitGroup.Add(n)
	wg.mu.Unlock()
}
// Wait waits until all the goroutines call Done.
//
// Wait may be called from concurrent goroutines.
//
// Further calls to Add are blocked until return from Wait.
func (wg *WaitGroup) Wait() {
	wg.mu.Lock()
	wg.WaitGroup.Wait()
	wg.mu.Unlock()
}
// WaitAndBlock waits until all the goroutines call Done and then prevents
// from new goroutines calling Add.
//
// Further calls to Add are always blocked. This is useful for graceful shutdown
// when other goroutines calling Add must be stopped.
//
// wg cannot be used after this call.
func (wg *WaitGroup) WaitAndBlock() {
	wg.mu.Lock()
	wg.WaitGroup.Wait()
	// Do not unlock wg.mu, so other goroutines calling Add are blocked.
}
// There is no need in wrapping WaitGroup.Done, since it is already goroutine-safe.
时间池
高频次创建timer会消耗一定的性能,为了减少某些情况下的性能损耗,可以使用sync.Pool来回收利用创建的timer
// Get returns a timer for the given duration d from the pool.
//
// Return back the timer to the pool with Put.
func Get(d time.Duration) *time.Timer {
	if v := timerPool.Get(); v != nil {
		t := v.(*time.Timer)
		if t.Reset(d) {
			logger.Panicf("BUG: active timer trapped to the pool!")
		}
		return t
	}
	return time.NewTimer(d)
}
// Put returns t to the pool.
//
// t cannot be accessed after returning to the pool.
func Put(t *time.Timer) {
	if !t.Stop() {
		// Drain t.C if it wasn't obtained by the caller yet.
		select {
		case <-t.C:
		default:
		}
	}
	timerPool.Put(t)
}
var timerPool sync.Pool
访问限速
victoriaMetrics的vminsert作为vmagent和vmstorage之间的组件,接收vmagent的流量并将其转发到vmstorage。在vmstorage卡死、处理过慢或下线的情况下,有可能会导致无法转发流量,进而造成vminsert CPU和内存飙升,造成组件故障。为了防止这种情况,vminsert使用了限速器,当接收到的流量激增时,可以在牺牲一部分数据的情况下保证系统的稳定性。
victoriaMetrics的源码中对限速器有如下描述:
Limit the number of conurrent f calls in order to prevent from excess memory usage and CPU thrashing
限速器使用了两个参数:maxConcurrentInserts和maxQueueDuration,前者给出了突发情况下可以处理的最大请求数,后者给出了某个请求的最大超时时间。需要注意的是Do(f func() error)是异步执行的,而ch又是全局的,因此会异步等待其他请求释放资源(struct{})。
可以看到限速器使用了指标来指示当前的限速状态。同时使用cgroup.AvailableCPUs()*4 (即runtime.GOMAXPROCS(-1)*4)来设置默认的maxConcurrentInserts长度。
当该限速器用在处理如http请求时,该限速器并不能限制底层上送的请求,其限制的是对请求的处理。在高流量业务处理中,这也是最消耗内存的地方,通常包含数据读取、内存申请拷贝等。底层的数据受
/proc/sys/net/core/somaxconn和socket缓存区的限制。
var (
	maxConcurrentInserts = flag.Int("maxConcurrentInserts", cgroup.AvailableCPUs()*4, "The maximum number of concurrent inserts. Default value should work for most cases, "+
		"since it minimizes the overhead for concurrent inserts. This option is tigthly coupled with -insert.maxQueueDuration")
	maxQueueDuration = flag.Duration("insert.maxQueueDuration", time.Minute, "The maximum duration for waiting in the queue for insert requests due to -maxConcurrentInserts")
)
// ch is the channel for limiting concurrent calls to Do.
var ch chan struct{}
// Init initializes concurrencylimiter.
//
// Init must be called after flag.Parse call.
func Init() {
	ch = make(chan struct{}, *maxConcurrentInserts) //初始化limiter,最大突发并行请求量为maxConcurrentInserts
}
// Do calls f with the limited concurrency.
func Do(f func() error) error {
	// Limit the number of conurrent f calls in order to prevent from excess
	// memory usage and CPU thrashing.
	select {
	case ch <- struct{}{}: //在channel中添加一个元素,表示开始处理一个请求
		err := f() //阻塞等大请求处理结束
		<-ch //请求处理完之后释放channel中的一个元素,释放出的空间可以用于处理下一个请求
		return err
	default:
	}
    //如果当前达到处理上限maxConcurrentInserts,则需要等到其他Do(f func() error)释放资源。
	// All the workers are busy.
	// Sleep for up to *maxQueueDuration.
	concurrencyLimitReached.Inc()
	t := timerpool.Get(*maxQueueDuration) //获取一个timer,设置等待超时时间为 maxQueueDuration
	select {
	case ch <- struct{}{}: //在maxQueueDuration时间内等待其他请求释放资源,如果获取到资源,则回收timer,继续处理
		timerpool.Put(t)
		err := f()
		<-
		return err
	case <-t.C: //在maxQueueDuration时间内没有获取到资源,定时器超时后回收timer,丢弃请求并返回错误信息
		timerpool.Put(t)
		concurrencyLimitTimeout.Inc()
		return &httpserver.ErrorWithStatusCode{
			Err: fmt.Errorf("cannot handle more than %d concurrent inserts during %s; possible solutions: "+
				"increase `-insert.maxQueueDuration`, increase `-maxConcurrentInserts`, increase server capacity", *maxConcurrentInserts, *maxQueueDuration),
			StatusCode: http.StatusServiceUnavailable,
		}
	}
}
var (
	concurrencyLimitReached = metrics.NewCounter(`vm_concurrent_insert_limit_reached_total`)
	concurrencyLimitTimeout = metrics.NewCounter(`vm_concurrent_insert_limit_timeout_total`)
	_ = metrics.NewGauge(`vm_concurrent_insert_capacity`, func() float64 {
		return float64(cap(ch))
	})
	_ = metrics.NewGauge(`vm_concurrent_insert_current`, func() float64 {
		return float64(len(ch))
	})
)
优先级控制
victoriaMetrics的pacelimiter库实现了优先级控制。主要方法由Inc、Dec和WaitIfNeeded。低优先级任务需要调用WaitIfNeeded方法,如果此时有高优先级任务(调用Inc方法),则低优先级任务需要等待高优先级任务结束(调用Dec方法)之后才能继续执行。
// PaceLimiter throttles WaitIfNeeded callers while the number of Inc calls is bigger than the number of Dec calls.
//
// It is expected that Inc is called before performing high-priority work,
// while Dec is called when the work is done.
// WaitIfNeeded must be called inside the work which must be throttled (i.e. lower-priority work).
// It may be called in the loop before performing a part of low-priority work.
type PaceLimiter struct {
	mu          sync.Mutex
	cond        *sync.Cond
	delaysTotal uint64
	n           int32
}
// New returns pace limiter that throttles WaitIfNeeded callers while the number of Inc calls is bigger than the number of Dec calls.
func New() *PaceLimiter {
	var pl PaceLimiter
	pl.cond = sync.NewCond(&pl.mu)
	return &pl
}
// Inc increments pl.
func (pl *PaceLimiter) Inc() {
	atomic.AddInt32(&pl.n, 1)
}
// Dec decrements pl.
func (pl *PaceLimiter) Dec() {
	if atomic.AddInt32(&pl.n, -1) == 0 {
		// Wake up all the goroutines blocked in WaitIfNeeded,
		// since the number of Dec calls equals the number of Inc calls.
		pl.cond.Broadcast()
	}
}
// WaitIfNeeded blocks while the number of Inc calls is bigger than the number of Dec calls.
func (pl *PaceLimiter) WaitIfNeeded() {
	if atomic.LoadInt32(&pl.n) <= 0 {
		// Fast path - there is no need in lock.
		return
	}
	// Slow path - wait until Dec is called.
	pl.mu.Lock()
	for atomic.LoadInt32(&pl.n) > 0 {
		pl.delaysTotal++
		pl.cond.Wait()
	}
	pl.mu.Unlock()
}
// DelaysTotal returns the number of delays inside WaitIfNeeded.
func (pl *PaceLimiter) DelaysTotal() uint64 {
	pl.mu.Lock()
	n := pl.delaysTotal
	pl.mu.Unlock()
	return n
}
												
											victoriaMetrics中的一些Sao操作的更多相关文章
- JavaScript jQuery 中定义数组与操作及jquery数组操作
		
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
 - IE7中使用Jquery动态操作name问题
		
问题:IE7中无法使用Jquery动态操作页面元素的name属性. 在项目中有出现问题,某些客户的机器偶尔会有,后台取不到前台的数据值. 然开发和测试环境总是不能重现问题.坑爹之处就在于此,不能重现就 ...
 - JavaScript中常见的数组操作函数及用法
		
JavaScript中常见的数组操作函数及用法 昨天写了个帖子,汇总了下常见的JavaScript中的字符串操作函数及用法.今天正好有时间,也去把JavaScript中常见的数组操作函数及用法总结一下 ...
 - JavaScript中常见的字符串操作函数及用法
		
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
 - 无法更新 EntitySet“SoreInfo_Table”,因为它有一个 DefiningQuery,而 <ModificationFunctionMapping> 元素中没有支持当前操作的 <InsertFunction> 元素。
		
无法更新 EntitySet"SoreInfo_Table",因为它有一个 DefiningQuery,而 <ModificationFunctionMapping> ...
 - [转]js中confirm实现执行操作前弹出确认框的方法
		
原文地址:http://www.jb51.net/article/56986.htm 本文实例讲述了js中confirm实现执行操作前弹出确认框的方法.分享给大家供大家参考.具体实现方法如下: 现在在 ...
 - 传递给数据库 'master' 中的日志扫描操作的日志扫描号无效
		
错误:连接数据库的时候提示:SQL Server 检测到基于一致性的逻辑 I/O 错误 校验和不正确 C:\Documents and Settings\Administrator>" ...
 - 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
		
原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...
 - php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
		
2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作 阅读:次 时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...
 
随机推荐
- IDEA terminal无法从vim的编辑模式转换为命令模式
			
Git 修改最后一次的commit历史记录:https://www.baidu.com/link?url=2WF8yFd0iBuVmXLWfutmSoXa12K9D143e_B0A3PTYYHEP9r ...
 - CSS 网站布局
			
Flex:https://www.html.cn/archives/8629 Grid:https://www.html.cn/archives/8510/ http://www.ruanyif ...
 - 用 Java 写一个折半查找?
			
折半查找,也称二分查找.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束:如果某一特定元素大于或者小于中间元素,则 ...
 - 说一下 jvm 有哪些垃圾回收器?
			
新生代收集器: SerialParNewParallel Scavenge 老年代收集器: Serial OldCMSParallel Old 堆内存垃圾收集器: G1 参考链接:JVM常见的垃圾回收 ...
 - ArrayList分别与LinkedList、Vector、Array的区别
			
ArrayList与LinkedList的区别 ArrayList底层是一个动态数组,LinkedList底层是双向链表 当随机访问List时(get和set操作),ArrayList比LinkedL ...
 - Eclipse创建Spring XML配置文件插件
			
引用:https://www.cnblogs.com/lideqiang/p/9067219.html 第一步:在 Eclipse Marketplace仓库中,搜索sts 第二步:安装Spring ...
 - JRE、JDK、JVM 及 JIT 之间有什么不同?
			
JRE 代表 Java 运行时(Java run-time),是运行 Java 引用所必须的.JDK 代 表 Java 开发工具(Java development kit),是 Java 程序的开发工 ...
 - 列举 IoC 的一些好处?
			
IoC 的一些好处是:它将最小化应用程序中的代码量.它将使您的应用程序易于测试,因为它不需要单元测试用例中的任何单例 或 JNDI 查找机制.它以最小的影响和最少的侵入机制促进松耦合.它支持即时的实例 ...
 - 32 位和 64 位的 JVM,int 类型变量的长度是多数?
			
32 位和 64 位的 JVM 中,int 类型变量的长度是相同的,都是 32 位或者 4个字节.
 - Java 中,编写多线程程序的时候你会遵循哪些最佳实践?
			
这是我在写 Java 并发程序的时候遵循的一些最佳实践: a)给线程命名,这样可以帮助调试. b)最小化同步的范围,而不是将整个方法同步,只对关键部分做同步. c)如果可以,更偏向于使用 volati ...