app/vmselect/prometheus: improve scalability of /federate endpoint on systems with many CPU cores

Minimize usage of global lock inside bufferedwriter.Write() when processing `/federate` data
on systems with many CPU cores
This commit is contained in:
Aliaksandr Valialkin 2022-10-01 20:12:09 +03:00
parent fb1cc3cc94
commit 49311ae977
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -84,19 +84,33 @@ func FederateHandler(startTime time.Time, w http.ResponseWriter, r *http.Request
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
bw := bufferedwriter.Get(w)
defer bufferedwriter.Put(bw)
var m sync.Map
err = rss.RunParallel(nil, func(rs *netstorage.Result, workerID uint) error {
if err := bw.Error(); err != nil {
return err
}
bb := quicktemplate.AcquireByteBuffer()
v, ok := m.Load(workerID)
if !ok {
v = &bytesutil.ByteBuffer{}
m.Store(workerID, v)
}
bb := v.(*bytesutil.ByteBuffer)
WriteFederate(bb, rs)
if len(bb.B) < 1024*1024 {
return nil
}
_, err := bw.Write(bb.B)
quicktemplate.ReleaseByteBuffer(bb)
bb.Reset()
return err
})
if err != nil {
return fmt.Errorf("error during sending data to remote client: %w", err)
}
m.Range(func(k, v interface{}) bool {
bb := v.(*bytesutil.ByteBuffer)
_, err := bw.Write(bb.B)
return err == nil
})
if err := bw.Flush(); err != nil {
return err
}