github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/metrics-v3-cache.go (about) 1 // Copyright (c) 2015-2024 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "time" 22 23 "github.com/minio/madmin-go/v3" 24 "github.com/minio/minio/internal/cachevalue" 25 ) 26 27 // metricsCache - cache for metrics. 28 // 29 // When serving metrics, this cache is passed to the MetricsLoaderFn. 30 // 31 // This cache is used for metrics that would result in network/storage calls. 32 type metricsCache struct { 33 dataUsageInfo *cachevalue.Cache[DataUsageInfo] 34 esetHealthResult *cachevalue.Cache[HealthResult] 35 driveMetrics *cachevalue.Cache[storageMetrics] 36 clusterDriveMetrics *cachevalue.Cache[storageMetrics] 37 nodesUpDown *cachevalue.Cache[nodesOnline] 38 } 39 40 func newMetricsCache() *metricsCache { 41 return &metricsCache{ 42 dataUsageInfo: newDataUsageInfoCache(), 43 esetHealthResult: newESetHealthResultCache(), 44 driveMetrics: newDriveMetricsCache(), 45 clusterDriveMetrics: newClusterStorageInfoCache(), 46 nodesUpDown: newNodesUpDownCache(), 47 } 48 } 49 50 type nodesOnline struct { 51 Online, Offline int 52 } 53 54 func newNodesUpDownCache() *cachevalue.Cache[nodesOnline] { 55 loadNodesUpDown := func() (v nodesOnline, err error) { 56 v.Online, v.Offline = globalNotificationSys.GetPeerOnlineCount() 57 return 58 } 59 return cachevalue.NewFromFunc(1*time.Minute, 60 cachevalue.Opts{ReturnLastGood: true}, 61 loadNodesUpDown) 62 } 63 64 type storageMetrics struct { 65 storageInfo madmin.StorageInfo 66 onlineDrives, offlineDrives, totalDrives int 67 } 68 69 func newDataUsageInfoCache() *cachevalue.Cache[DataUsageInfo] { 70 loadDataUsage := func() (u DataUsageInfo, err error) { 71 objLayer := newObjectLayerFn() 72 if objLayer == nil { 73 return 74 } 75 76 // Collect cluster level object metrics. 77 u, err = loadDataUsageFromBackend(GlobalContext, objLayer) 78 return 79 } 80 return cachevalue.NewFromFunc(1*time.Minute, 81 cachevalue.Opts{ReturnLastGood: true}, 82 loadDataUsage) 83 } 84 85 func newESetHealthResultCache() *cachevalue.Cache[HealthResult] { 86 loadHealth := func() (r HealthResult, err error) { 87 objLayer := newObjectLayerFn() 88 if objLayer == nil { 89 return 90 } 91 92 r = objLayer.Health(GlobalContext, HealthOptions{}) 93 return 94 } 95 return cachevalue.NewFromFunc(1*time.Minute, 96 cachevalue.Opts{ReturnLastGood: true}, 97 loadHealth, 98 ) 99 } 100 101 func newDriveMetricsCache() *cachevalue.Cache[storageMetrics] { 102 loadDriveMetrics := func() (v storageMetrics, err error) { 103 objLayer := newObjectLayerFn() 104 if objLayer == nil { 105 return 106 } 107 108 storageInfo := objLayer.LocalStorageInfo(GlobalContext, true) 109 onlineDrives, offlineDrives := getOnlineOfflineDisksStats(storageInfo.Disks) 110 totalDrives := onlineDrives.Merge(offlineDrives) 111 v = storageMetrics{ 112 storageInfo: storageInfo, 113 onlineDrives: onlineDrives.Sum(), 114 offlineDrives: offlineDrives.Sum(), 115 totalDrives: totalDrives.Sum(), 116 } 117 return 118 } 119 return cachevalue.NewFromFunc(1*time.Minute, 120 cachevalue.Opts{ReturnLastGood: true}, 121 loadDriveMetrics) 122 } 123 124 func newClusterStorageInfoCache() *cachevalue.Cache[storageMetrics] { 125 loadStorageInfo := func() (v storageMetrics, err error) { 126 objLayer := newObjectLayerFn() 127 if objLayer == nil { 128 return storageMetrics{}, nil 129 } 130 storageInfo := objLayer.StorageInfo(GlobalContext, true) 131 onlineDrives, offlineDrives := getOnlineOfflineDisksStats(storageInfo.Disks) 132 totalDrives := onlineDrives.Merge(offlineDrives) 133 v = storageMetrics{ 134 storageInfo: storageInfo, 135 onlineDrives: onlineDrives.Sum(), 136 offlineDrives: offlineDrives.Sum(), 137 totalDrives: totalDrives.Sum(), 138 } 139 return 140 } 141 return cachevalue.NewFromFunc(1*time.Minute, 142 cachevalue.Opts{ReturnLastGood: true}, 143 loadStorageInfo, 144 ) 145 }