app/vmselect/netstorage: improve code readability a bit after 6c84b61893

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4364
This commit is contained in:
Aliaksandr Valialkin 2023-07-05 20:35:44 -07:00
parent 11ac551d52
commit 643e99a157
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -2718,17 +2718,37 @@ func initStorageNodes(addrs []string) *storageNodesBucket {
sns := make([]*storageNode, 0, len(addrs)) sns := make([]*storageNode, 0, len(addrs))
var wg sync.WaitGroup var wg sync.WaitGroup
ms := metrics.NewSet() ms := metrics.NewSet()
// initialize connections to storage nodes in parallel in order speed up the initialization
// for big number of storage nodes.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4364
for _, addr := range addrs { for _, addr := range addrs {
wg.Add(1) wg.Add(1)
go func(addr string) { go func(addr string) {
defer wg.Done() defer wg.Done()
sn := newStorageNode(ms, addr)
snsLock.Lock()
sns = append(sns, sn)
snsLock.Unlock()
}(addr)
}
wg.Wait()
metrics.RegisterSet(ms)
return &storageNodesBucket{
sns: sns,
ms: ms,
}
}
func newStorageNode(ms *metrics.Set, addr string) *storageNode {
if _, _, err := net.SplitHostPort(addr); err != nil { if _, _, err := net.SplitHostPort(addr); err != nil {
// Automatically add missing port. // Automatically add missing port.
addr += ":8401" addr += ":8401"
} }
// There is no need in requests compression, since vmselect requests are usually very small.
connPool := netutil.NewConnPool(ms, "vmselect", addr, handshake.VMSelectClient, 0, *vmstorageDialTimeout)
sn := &storageNode{ sn := &storageNode{
// There is no need in requests compression, since they are usually very small. connPool: connPool,
connPool: netutil.NewConnPool(ms, "vmselect", addr, handshake.VMSelectClient, 0, *vmstorageDialTimeout),
concurrentQueries: ms.NewCounter(fmt.Sprintf(`vm_concurrent_queries{name="vmselect", addr=%q}`, addr)), concurrentQueries: ms.NewCounter(fmt.Sprintf(`vm_concurrent_queries{name="vmselect", addr=%q}`, addr)),
@ -2756,17 +2776,7 @@ func initStorageNodes(addrs []string) *storageNodesBucket {
metricBlocksRead: ms.NewCounter(fmt.Sprintf(`vm_metric_blocks_read_total{name="vmselect", addr=%q}`, addr)), metricBlocksRead: ms.NewCounter(fmt.Sprintf(`vm_metric_blocks_read_total{name="vmselect", addr=%q}`, addr)),
metricRowsRead: ms.NewCounter(fmt.Sprintf(`vm_metric_rows_read_total{name="vmselect", addr=%q}`, addr)), metricRowsRead: ms.NewCounter(fmt.Sprintf(`vm_metric_rows_read_total{name="vmselect", addr=%q}`, addr)),
} }
snsLock.Lock() return sn
sns = append(sns, sn)
snsLock.Unlock()
}(addr)
}
wg.Wait()
metrics.RegisterSet(ms)
return &storageNodesBucket{
sns: sns,
ms: ms,
}
} }
func mustStopStorageNodes(snb *storageNodesBucket) { func mustStopStorageNodes(snb *storageNodesBucket) {