2022-01-26 17:33:10 -05:00
|
|
|
// Copyright (c) 2022 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/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2022-01-26 17:33:10 -05:00
|
|
|
)
|
|
|
|
|
2024-02-19 17:54:46 -05:00
|
|
|
//go:generate msgp -file=$GOFILE -unexported
|
|
|
|
|
2022-01-26 17:33:10 -05:00
|
|
|
type lastDayTierStats struct {
|
|
|
|
Bins [24]tierStats
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lastDayTierStats) addStats(ts tierStats) {
|
|
|
|
now := time.Now()
|
|
|
|
l.forwardTo(now)
|
|
|
|
|
|
|
|
nowIdx := now.Hour()
|
|
|
|
l.Bins[nowIdx] = l.Bins[nowIdx].add(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// forwardTo moves time to t, clearing entries between last update and t.
|
|
|
|
func (l *lastDayTierStats) forwardTo(t time.Time) {
|
2022-09-01 16:51:06 -04:00
|
|
|
if t.IsZero() {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:33:10 -05:00
|
|
|
since := t.Sub(l.UpdatedAt).Hours()
|
|
|
|
// within the hour since l.UpdatedAt
|
|
|
|
if since < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, lastIdx := t.Hour(), l.UpdatedAt.Hour()
|
2022-09-01 16:51:06 -04:00
|
|
|
|
|
|
|
l.UpdatedAt = t // update to the latest time index
|
2022-01-26 17:33:10 -05:00
|
|
|
|
|
|
|
if since >= 24 {
|
|
|
|
l.Bins = [24]tierStats{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-01 16:51:06 -04:00
|
|
|
for lastIdx != idx {
|
|
|
|
lastIdx = (lastIdx + 1) % 24
|
|
|
|
l.Bins[lastIdx] = tierStats{}
|
2022-01-26 17:33:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *lastDayTierStats) clone() lastDayTierStats {
|
|
|
|
clone := lastDayTierStats{
|
|
|
|
UpdatedAt: l.UpdatedAt,
|
|
|
|
}
|
|
|
|
copy(clone.Bins[:], l.Bins[:])
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l lastDayTierStats) merge(m lastDayTierStats) (merged lastDayTierStats) {
|
|
|
|
cl := l.clone()
|
|
|
|
cm := m.clone()
|
|
|
|
|
|
|
|
if cl.UpdatedAt.After(cm.UpdatedAt) {
|
|
|
|
cm.forwardTo(cl.UpdatedAt)
|
|
|
|
merged.UpdatedAt = cl.UpdatedAt
|
|
|
|
} else {
|
|
|
|
cl.forwardTo(cm.UpdatedAt)
|
|
|
|
merged.UpdatedAt = cm.UpdatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cl.Bins {
|
|
|
|
merged.Bins[i] = cl.Bins[i].add(cm.Bins[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:21:55 -04:00
|
|
|
// DailyAllTierStats is used to aggregate last day tier stats across MinIO servers
|
|
|
|
type DailyAllTierStats map[string]lastDayTierStats
|
2022-01-26 17:33:10 -05:00
|
|
|
|
2022-04-13 17:21:55 -04:00
|
|
|
func (l DailyAllTierStats) merge(m DailyAllTierStats) {
|
2022-01-26 17:33:10 -05:00
|
|
|
for tier, st := range m {
|
|
|
|
l[tier] = l[tier].merge(st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:21:55 -04:00
|
|
|
func (l DailyAllTierStats) addToTierInfo(tierInfos []madmin.TierInfo) []madmin.TierInfo {
|
2022-01-26 17:33:10 -05:00
|
|
|
for i := range tierInfos {
|
2023-10-30 12:59:51 -04:00
|
|
|
lst, ok := l[tierInfos[i].Name]
|
|
|
|
if !ok {
|
2022-01-26 17:33:10 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for hr, st := range lst.Bins {
|
|
|
|
tierInfos[i].DailyStats.Bins[hr] = madmin.TierStats{
|
|
|
|
TotalSize: st.TotalSize,
|
|
|
|
NumVersions: st.NumVersions,
|
|
|
|
NumObjects: st.NumObjects,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tierInfos[i].DailyStats.UpdatedAt = lst.UpdatedAt
|
|
|
|
}
|
|
|
|
return tierInfos
|
|
|
|
}
|