2020-10-09 23:36:00 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package bandwidth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/bandwidth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// throttleBandwidth gets the throttle for bucket with the configured value
|
2020-10-16 12:07:50 -04:00
|
|
|
func (m *Monitor) throttleBandwidth(ctx context.Context, bucket string, bandwidthBytesPerSecond int64, clusterBandwidth int64) *throttle {
|
2020-10-09 23:36:00 -04:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
throttle, ok := m.bucketThrottle[bucket]
|
|
|
|
if !ok {
|
2020-10-16 12:07:50 -04:00
|
|
|
throttle = newThrottle(ctx, bandwidthBytesPerSecond, clusterBandwidth)
|
2020-10-09 23:36:00 -04:00
|
|
|
m.bucketThrottle[bucket] = throttle
|
|
|
|
return throttle
|
|
|
|
}
|
2020-10-16 12:07:50 -04:00
|
|
|
throttle.SetBandwidth(bandwidthBytesPerSecond, clusterBandwidth)
|
2020-10-09 23:36:00 -04:00
|
|
|
return throttle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Monitor implements the monitoring for bandwidth measurements.
|
|
|
|
type Monitor struct {
|
|
|
|
lock sync.Mutex // lock for all updates
|
|
|
|
|
|
|
|
activeBuckets map[string]*bucketMeasurement // Buckets with objects in flight
|
|
|
|
|
|
|
|
bucketMovingAvgTicker *time.Ticker // Ticker for calculating moving averages
|
|
|
|
|
|
|
|
bucketThrottle map[string]*throttle
|
|
|
|
|
|
|
|
doneCh <-chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMonitor returns a monitor with defaults.
|
|
|
|
func NewMonitor(doneCh <-chan struct{}) *Monitor {
|
|
|
|
m := &Monitor{
|
|
|
|
activeBuckets: make(map[string]*bucketMeasurement),
|
2020-10-16 12:07:50 -04:00
|
|
|
bucketMovingAvgTicker: time.NewTicker(2 * time.Second),
|
2020-10-09 23:36:00 -04:00
|
|
|
bucketThrottle: make(map[string]*throttle),
|
|
|
|
doneCh: doneCh,
|
|
|
|
}
|
2021-04-15 19:20:45 -04:00
|
|
|
go m.trackEWMA()
|
2020-10-09 23:36:00 -04:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectionFunction for buckets
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReport gets the report for all bucket bandwidth details.
|
|
|
|
func (m *Monitor) GetReport(selectBucket SelectionFunction) *bandwidth.Report {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
return m.getReport(selectBucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) getReport(selectBucket SelectionFunction) *bandwidth.Report {
|
|
|
|
report := &bandwidth.Report{
|
|
|
|
BucketStats: make(map[string]bandwidth.Details),
|
|
|
|
}
|
|
|
|
for bucket, bucketMeasurement := range m.activeBuckets {
|
|
|
|
if !selectBucket(bucket) {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-06 14:01:53 -04:00
|
|
|
bucketThrottle, ok := m.bucketThrottle[bucket]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-09 23:36:00 -04:00
|
|
|
report.BucketStats[bucket] = bandwidth.Details{
|
2021-04-06 14:01:53 -04:00
|
|
|
LimitInBytesPerSecond: bucketThrottle.clusterBandwidth,
|
2020-10-09 23:36:00 -04:00
|
|
|
CurrentBandwidthInBytesPerSecond: bucketMeasurement.getExpMovingAvgBytesPerSecond(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return report
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
case <-m.doneCh:
|
2020-10-09 23:36:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) getBucketMeasurement(bucket string, initTime time.Time) *bucketMeasurement {
|
|
|
|
bucketTracker, ok := m.activeBuckets[bucket]
|
|
|
|
if !ok {
|
|
|
|
bucketTracker = newBucketMeasurement(initTime)
|
|
|
|
m.activeBuckets[bucket] = bucketTracker
|
|
|
|
}
|
|
|
|
return bucketTracker
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:20:45 -04:00
|
|
|
func (m *Monitor) updateMovingAvg() {
|
2020-10-09 23:36:00 -04:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
for _, bucketMeasurement := range m.activeBuckets {
|
|
|
|
bucketMeasurement.updateExponentialMovingAverage(time.Now())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// track returns the measurement object for bucket and object
|
2021-04-15 19:20:45 -04:00
|
|
|
func (m *Monitor) track(bucket string, object string) *bucketMeasurement {
|
2020-10-09 23:36:00 -04:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
2021-04-15 19:20:45 -04:00
|
|
|
return m.getBucketMeasurement(bucket, 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) {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
delete(m.activeBuckets, bucket)
|
|
|
|
delete(m.bucketThrottle, bucket)
|
|
|
|
}
|