2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-10-09 23:36:00 -04:00
|
|
|
|
|
|
|
package bandwidth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
"golang.org/x/time/rate"
|
2020-10-09 23:36:00 -04:00
|
|
|
)
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
type throttle struct {
|
|
|
|
*rate.Limiter
|
|
|
|
NodeBandwidthPerSec int64
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
// Monitor holds the state of the global bucket monitor
|
2020-10-09 23:36:00 -04:00
|
|
|
type Monitor struct {
|
2021-06-24 21:29:30 -04:00
|
|
|
tlock sync.RWMutex // mutex for bucketThrottle
|
2023-01-19 08:22:16 -05:00
|
|
|
bucketThrottle map[string]map[string]*throttle
|
|
|
|
mlock sync.RWMutex // mutex for activeBuckets map
|
|
|
|
activeBuckets map[string]map[string]*bucketMeasurement // Buckets with objects in flight
|
|
|
|
bucketMovingAvgTicker *time.Ticker // Ticker for calculating moving averages
|
|
|
|
ctx context.Context // Context for generate
|
2021-06-24 21:29:30 -04:00
|
|
|
NodeCount uint64
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:28:29 -05:00
|
|
|
// NewMonitor returns a monitor with defaults.
|
2021-06-24 21:29:30 -04:00
|
|
|
func NewMonitor(ctx context.Context, numNodes uint64) *Monitor {
|
2020-10-09 23:36:00 -04:00
|
|
|
m := &Monitor{
|
2023-01-19 08:22:16 -05:00
|
|
|
activeBuckets: make(map[string]map[string]*bucketMeasurement),
|
|
|
|
bucketThrottle: make(map[string]map[string]*throttle),
|
2021-06-24 21:29:30 -04:00
|
|
|
bucketMovingAvgTicker: time.NewTicker(2 * time.Second),
|
|
|
|
ctx: ctx,
|
|
|
|
NodeCount: numNodes,
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2021-04-15 19:20:45 -04:00
|
|
|
go m.trackEWMA()
|
2020-10-09 23:36:00 -04:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) updateMeasurement(bucket, arn string, bytes uint64) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.Lock()
|
|
|
|
defer m.mlock.Unlock()
|
2023-01-19 08:22:16 -05:00
|
|
|
tm, ok := m.activeBuckets[bucket]
|
|
|
|
if !ok {
|
|
|
|
tm = make(map[string]*bucketMeasurement)
|
|
|
|
}
|
|
|
|
measurement, ok := tm[arn]
|
|
|
|
if !ok {
|
|
|
|
measurement = &bucketMeasurement{}
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
2023-01-19 08:22:16 -05:00
|
|
|
measurement.incrementBytes(bytes)
|
|
|
|
m.activeBuckets[bucket][arn] = measurement
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
|
|
|
|
2021-11-16 12:28:29 -05:00
|
|
|
// SelectionFunction for buckets
|
2020-10-09 23:36:00 -04:00
|
|
|
type SelectionFunction func(bucket string) bool
|
|
|
|
|
|
|
|
// SelectBuckets will select all the buckets passed in.
|
|
|
|
func SelectBuckets(buckets ...string) SelectionFunction {
|
2020-10-12 12:04:55 -04:00
|
|
|
if len(buckets) == 0 {
|
|
|
|
return func(bucket string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 23:36:00 -04:00
|
|
|
return func(bucket string) bool {
|
|
|
|
for _, b := range buckets {
|
2020-10-12 12:04:55 -04:00
|
|
|
if b == "" || b == bucket {
|
2020-10-09 23:36:00 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:22:16 -05:00
|
|
|
// Details for the measured bandwidth
|
|
|
|
type Details struct {
|
|
|
|
LimitInBytesPerSecond int64 `json:"limitInBits"`
|
|
|
|
CurrentBandwidthInBytesPerSecond float64 `json:"currentBandwidth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketBandwidthReport captures the details for all buckets.
|
|
|
|
type BucketBandwidthReport struct {
|
|
|
|
BucketStats map[string]map[string]Details `json:"bucketStats,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:36:00 -04:00
|
|
|
// GetReport gets the report for all bucket bandwidth details.
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) GetReport(selectBucket SelectionFunction) *BucketBandwidthReport {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.RLock()
|
|
|
|
defer m.mlock.RUnlock()
|
2020-10-09 23:36:00 -04:00
|
|
|
return m.getReport(selectBucket)
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) getReport(selectBucket SelectionFunction) *BucketBandwidthReport {
|
|
|
|
report := &BucketBandwidthReport{
|
|
|
|
BucketStats: make(map[string]map[string]Details),
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2023-01-19 08:22:16 -05:00
|
|
|
for bucket, bucketMeasurementMap := range m.activeBuckets {
|
2020-10-09 23:36:00 -04:00
|
|
|
if !selectBucket(bucket) {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.RLock()
|
2023-01-19 08:22:16 -05:00
|
|
|
report.BucketStats[bucket] = make(map[string]Details)
|
|
|
|
if tgtThrottle, ok := m.bucketThrottle[bucket]; ok {
|
|
|
|
for arn, throttle := range tgtThrottle {
|
|
|
|
var currBw float64
|
|
|
|
if bucketMeasurement, ok := bucketMeasurementMap[arn]; ok {
|
|
|
|
currBw = bucketMeasurement.getExpMovingAvgBytesPerSecond()
|
|
|
|
}
|
|
|
|
report.BucketStats[bucket][arn] = Details{
|
|
|
|
LimitInBytesPerSecond: throttle.NodeBandwidthPerSec * int64(m.NodeCount),
|
|
|
|
CurrentBandwidthInBytesPerSecond: currBw,
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.RUnlock()
|
|
|
|
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
|
|
|
return report
|
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
|
2021-04-15 19:20:45 -04:00
|
|
|
func (m *Monitor) trackEWMA() {
|
2020-10-09 23:36:00 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.bucketMovingAvgTicker.C:
|
2021-04-15 19:20:45 -04:00
|
|
|
m.updateMovingAvg()
|
2021-06-24 21:29:30 -04:00
|
|
|
case <-m.ctx.Done():
|
2020-10-09 23:36:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
func (m *Monitor) updateMovingAvg() {
|
|
|
|
m.mlock.Lock()
|
|
|
|
defer m.mlock.Unlock()
|
|
|
|
for _, bucketMeasurement := range m.activeBuckets {
|
2023-01-19 08:22:16 -05:00
|
|
|
for _, measurement := range bucketMeasurement {
|
|
|
|
measurement.updateExponentialMovingAverage(time.Now())
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) getBucketMeasurement(bucket, arn string, initTime time.Time) map[string]*bucketMeasurement {
|
2020-10-09 23:36:00 -04:00
|
|
|
bucketTracker, ok := m.activeBuckets[bucket]
|
|
|
|
if !ok {
|
2023-01-19 08:22:16 -05:00
|
|
|
bucketTracker = make(map[string]*bucketMeasurement)
|
|
|
|
bucketTracker[arn] = newBucketMeasurement(initTime)
|
2020-10-09 23:36:00 -04:00
|
|
|
m.activeBuckets[bucket] = bucketTracker
|
|
|
|
}
|
|
|
|
return bucketTracker
|
|
|
|
}
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
// track returns the measurement object for bucket
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) track(bucket, arn string) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.Lock()
|
|
|
|
defer m.mlock.Unlock()
|
2023-01-19 08:22:16 -05:00
|
|
|
m.getBucketMeasurement(bucket, arn, time.Now())
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2020-10-16 20:59:31 -04:00
|
|
|
|
2020-10-17 03:38:54 -04:00
|
|
|
// DeleteBucket deletes monitoring the 'bucket'
|
2020-10-16 20:59:31 -04:00
|
|
|
func (m *Monitor) DeleteBucket(bucket string) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.Lock()
|
2020-10-16 20:59:31 -04:00
|
|
|
delete(m.bucketThrottle, bucket)
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.Unlock()
|
|
|
|
m.mlock.Lock()
|
|
|
|
delete(m.activeBuckets, bucket)
|
|
|
|
m.mlock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:22:16 -05:00
|
|
|
// DeleteBucketThrottle deletes monitoring for a bucket's target
|
|
|
|
func (m *Monitor) DeleteBucketThrottle(bucket, arn string) {
|
|
|
|
m.tlock.Lock()
|
2023-04-15 05:14:26 -04:00
|
|
|
if _, ok := m.bucketThrottle[bucket]; ok {
|
|
|
|
delete(m.bucketThrottle[bucket], arn)
|
|
|
|
}
|
2023-01-19 08:22:16 -05:00
|
|
|
m.tlock.Unlock()
|
|
|
|
m.mlock.Lock()
|
2023-04-15 05:14:26 -04:00
|
|
|
if _, ok := m.activeBuckets[bucket]; ok {
|
|
|
|
delete(m.activeBuckets[bucket], arn)
|
|
|
|
}
|
2023-01-19 08:22:16 -05:00
|
|
|
m.mlock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
// throttle returns currently configured throttle for this bucket
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) throttle(bucket, arn string) *throttle {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.RLock()
|
|
|
|
defer m.tlock.RUnlock()
|
2023-01-19 08:22:16 -05:00
|
|
|
return m.bucketThrottle[bucket][arn]
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBandwidthLimit sets the bandwidth limit for a bucket
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) SetBandwidthLimit(bucket, arn string, limit int64) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.Lock()
|
|
|
|
defer m.tlock.Unlock()
|
|
|
|
bw := limit / int64(m.NodeCount)
|
2023-01-19 08:22:16 -05:00
|
|
|
tgtMap, ok := m.bucketThrottle[bucket]
|
2021-06-24 21:29:30 -04:00
|
|
|
if !ok {
|
2023-01-19 08:22:16 -05:00
|
|
|
tgtMap = make(map[string]*throttle)
|
|
|
|
tgtMap[arn] = &throttle{
|
2021-06-24 21:29:30 -04:00
|
|
|
NodeBandwidthPerSec: bw,
|
|
|
|
}
|
|
|
|
}
|
2023-01-19 08:22:16 -05:00
|
|
|
th, ok := tgtMap[arn]
|
|
|
|
if !ok {
|
|
|
|
th = &throttle{}
|
|
|
|
}
|
|
|
|
th.NodeBandwidthPerSec = bw
|
|
|
|
tgtMap[arn] = th
|
|
|
|
newlimit := rate.Every(time.Second / time.Duration(tgtMap[arn].NodeBandwidthPerSec))
|
|
|
|
tgtMap[arn].Limiter = rate.NewLimiter(newlimit, int(tgtMap[arn].NodeBandwidthPerSec))
|
|
|
|
m.bucketThrottle[bucket] = tgtMap
|
2020-10-16 20:59:31 -04:00
|
|
|
}
|
2021-07-28 18:20:01 -04:00
|
|
|
|
|
|
|
// IsThrottled returns true if a bucket has bandwidth throttling enabled.
|
2023-01-19 08:22:16 -05:00
|
|
|
func (m *Monitor) IsThrottled(bucket, arn string) bool {
|
2021-07-28 18:20:01 -04:00
|
|
|
m.tlock.RLock()
|
|
|
|
defer m.tlock.RUnlock()
|
2023-01-19 08:22:16 -05:00
|
|
|
th, ok := m.bucketThrottle[bucket]
|
|
|
|
if !ok {
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
_, ok = th[arn]
|
2021-07-28 18:20:01 -04:00
|
|
|
return ok
|
|
|
|
}
|