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
|
|
|
|
|
2024-02-19 17:54:46 -05:00
|
|
|
//go:generate msgp -file=$GOFILE -unexported
|
|
|
|
|
2020-10-09 23:36:00 -04:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2024-02-19 17:54:46 -05:00
|
|
|
//msgp:ignore bucketThrottle Monitor
|
|
|
|
|
2023-09-05 23:21:59 -04:00
|
|
|
type bucketThrottle struct {
|
2021-06-24 21:29:30 -04:00
|
|
|
*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 {
|
2023-09-05 23:21:59 -04:00
|
|
|
tlock sync.RWMutex // mutex for bucket throttling
|
|
|
|
mlock sync.RWMutex // mutex for bucket measurement
|
|
|
|
|
|
|
|
bucketsThrottle map[BucketOptions]*bucketThrottle
|
|
|
|
bucketsMeasurement map[BucketOptions]*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-09-05 23:21:59 -04:00
|
|
|
bucketsMeasurement: make(map[BucketOptions]*bucketMeasurement),
|
|
|
|
bucketsThrottle: make(map[BucketOptions]*bucketThrottle),
|
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-09-05 23:21:59 -04:00
|
|
|
func (m *Monitor) updateMeasurement(opts BucketOptions, bytes uint64) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.Lock()
|
|
|
|
defer m.mlock.Unlock()
|
2023-09-05 23:21:59 -04:00
|
|
|
|
|
|
|
tm, ok := m.bucketsMeasurement[opts]
|
2023-01-19 08:22:16 -05:00
|
|
|
if !ok {
|
2023-09-05 23:21:59 -04:00
|
|
|
tm = &bucketMeasurement{}
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
2023-09-05 23:21:59 -04:00
|
|
|
tm.incrementBytes(bytes)
|
|
|
|
m.bucketsMeasurement[opts] = tm
|
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 {
|
2023-09-05 23:21:59 -04:00
|
|
|
for _, bkt := range buckets {
|
|
|
|
if bkt == 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 {
|
2023-09-05 23:21:59 -04:00
|
|
|
BucketStats map[BucketOptions]Details `json:"bucketStats,omitempty"`
|
2023-01-19 08:22:16 -05:00
|
|
|
}
|
|
|
|
|
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{
|
2023-09-05 23:21:59 -04:00
|
|
|
BucketStats: make(map[BucketOptions]Details),
|
2020-10-09 23:36:00 -04:00
|
|
|
}
|
2023-09-05 23:21:59 -04:00
|
|
|
for bucketOpts, bucketMeasurement := range m.bucketsMeasurement {
|
|
|
|
if !selectBucket(bucketOpts.Name) {
|
2020-10-09 23:36:00 -04:00
|
|
|
continue
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.RLock()
|
2023-09-05 23:21:59 -04:00
|
|
|
if tgtThrottle, ok := m.bucketsThrottle[bucketOpts]; ok {
|
|
|
|
currBw := bucketMeasurement.getExpMovingAvgBytesPerSecond()
|
|
|
|
report.BucketStats[bucketOpts] = Details{
|
|
|
|
LimitInBytesPerSecond: tgtThrottle.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()
|
2023-09-05 23:21:59 -04:00
|
|
|
for _, bucketMeasurement := range m.bucketsMeasurement {
|
|
|
|
bucketMeasurement.updateExponentialMovingAverage(time.Now())
|
2021-06-24 21:29:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 23:21:59 -04:00
|
|
|
func (m *Monitor) init(opts BucketOptions) {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.Lock()
|
|
|
|
defer m.mlock.Unlock()
|
2023-09-05 23:21:59 -04:00
|
|
|
|
|
|
|
_, ok := m.bucketsMeasurement[opts]
|
|
|
|
if !ok {
|
|
|
|
m.bucketsMeasurement[opts] = newBucketMeasurement(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()
|
2023-09-05 23:21:59 -04:00
|
|
|
for opts := range m.bucketsThrottle {
|
|
|
|
if opts.Name == bucket {
|
|
|
|
delete(m.bucketsThrottle, opts)
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.Unlock()
|
2023-09-05 23:21:59 -04:00
|
|
|
|
2021-06-24 21:29:30 -04:00
|
|
|
m.mlock.Lock()
|
2023-09-05 23:21:59 -04:00
|
|
|
for opts := range m.bucketsMeasurement {
|
|
|
|
if opts.Name == bucket {
|
|
|
|
delete(m.bucketsMeasurement, opts)
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
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-09-05 23:21:59 -04:00
|
|
|
delete(m.bucketsThrottle, BucketOptions{Name: bucket, ReplicationARN: arn})
|
2023-01-19 08:22:16 -05:00
|
|
|
m.tlock.Unlock()
|
|
|
|
m.mlock.Lock()
|
2023-09-05 23:21:59 -04:00
|
|
|
delete(m.bucketsMeasurement, BucketOptions{Name: bucket, ReplicationARN: 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-09-05 23:21:59 -04:00
|
|
|
func (m *Monitor) throttle(opts BucketOptions) *bucketThrottle {
|
2021-06-24 21:29:30 -04:00
|
|
|
m.tlock.RLock()
|
|
|
|
defer m.tlock.RUnlock()
|
2023-09-05 23:21:59 -04:00
|
|
|
return m.bucketsThrottle[opts]
|
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()
|
2023-09-05 23:21:59 -04:00
|
|
|
limitBytes := limit / int64(m.NodeCount)
|
|
|
|
throttle, ok := m.bucketsThrottle[BucketOptions{Name: bucket, ReplicationARN: arn}]
|
2023-01-19 08:22:16 -05:00
|
|
|
if !ok {
|
2023-09-05 23:21:59 -04:00
|
|
|
throttle = &bucketThrottle{}
|
2023-01-19 08:22:16 -05:00
|
|
|
}
|
2023-09-05 23:21:59 -04:00
|
|
|
throttle.NodeBandwidthPerSec = limitBytes
|
|
|
|
throttle.Limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
|
|
|
|
m.bucketsThrottle[BucketOptions{Name: bucket, ReplicationARN: arn}] = throttle
|
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-09-05 23:21:59 -04:00
|
|
|
_, ok := m.bucketsThrottle[BucketOptions{Name: bucket, ReplicationARN: arn}]
|
2021-07-28 18:20:01 -04:00
|
|
|
return ok
|
|
|
|
}
|