From 73e4e9994228b0574b9604b731b229eff25776ae Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 12 Sep 2019 11:06:12 -0700 Subject: [PATCH] Hosts should be skipped, when calculating local info (#8191) endpoint.IsLocal will not have .Host entries so using them to skip double entries will never work. change the code such that we look for endpoint.Host outside of endpoint.IsLocal logic to skip double hosts appropriately. Move these functions to their appropriate file. --- cmd/admin-handlers.go | 6 +-- cmd/admin-server-info.go | 112 +++++++++++++++++++++++++++++++++++++++ cmd/endpoint.go | 89 +------------------------------ cmd/peer-rest-server.go | 6 +-- 4 files changed, 119 insertions(+), 94 deletions(-) create mode 100644 cmd/admin-server-info.go diff --git a/cmd/admin-handlers.go b/cmd/admin-handlers.go index d59e9dd54..29f15dffd 100644 --- a/cmd/admin-handlers.go +++ b/cmd/admin-handlers.go @@ -412,7 +412,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request return } // Get drive performance details from local server's drive(s) - dp := localEndpointsDrivePerf(globalEndpoints, r) + dp := getLocalDrivesPerf(globalEndpoints, r) // Notify all other MinIO peers to report drive performance numbers dps := globalNotificationSys.DrivePerfInfo() @@ -430,7 +430,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request writeSuccessResponseJSON(w, jsonBytes) case "cpu": // Get CPU load details from local server's cpu(s) - cpu := localEndpointsCPULoad(globalEndpoints, r) + cpu := getLocalCPULoad(globalEndpoints, r) // Notify all other MinIO peers to report cpu load numbers cpus := globalNotificationSys.CPULoadInfo() cpus = append(cpus, cpu) @@ -447,7 +447,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request writeSuccessResponseJSON(w, jsonBytes) case "mem": // Get mem usage details from local server(s) - m := localEndpointsMemUsage(globalEndpoints, r) + m := getLocalMemUsage(globalEndpoints, r) // Notify all other MinIO peers to report mem usage numbers mems := globalNotificationSys.MemUsageInfo() mems = append(mems, m) diff --git a/cmd/admin-server-info.go b/cmd/admin-server-info.go new file mode 100644 index 000000000..4ed09422a --- /dev/null +++ b/cmd/admin-server-info.go @@ -0,0 +1,112 @@ +/* + * MinIO Cloud Storage, (C) 2019 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 cmd + +import ( + "net/http" + "os" + + "github.com/minio/minio-go/pkg/set" + "github.com/minio/minio/pkg/cpu" + "github.com/minio/minio/pkg/disk" + "github.com/minio/minio/pkg/mem" +) + +// getLocalMemUsage - returns ServerMemUsageInfo for only the +// local endpoints from given list of endpoints +func getLocalMemUsage(endpoints EndpointList, r *http.Request) ServerMemUsageInfo { + var memUsages []mem.Usage + var historicUsages []mem.Usage + seenHosts := set.NewStringSet() + for _, endpoint := range endpoints { + if seenHosts.Contains(endpoint.Host) { + continue + } + seenHosts.Add(endpoint.Host) + + // Only proceed for local endpoints + if endpoint.IsLocal { + memUsages = append(memUsages, mem.GetUsage()) + historicUsages = append(historicUsages, mem.GetHistoricUsage()) + } + } + addr := r.Host + if globalIsDistXL { + addr = GetLocalPeer(endpoints) + } + return ServerMemUsageInfo{ + Addr: addr, + Usage: memUsages, + HistoricUsage: historicUsages, + } +} + +// getLocalCPULoad - returns ServerCPULoadInfo for only the +// local endpoints from given list of endpoints +func getLocalCPULoad(endpoints EndpointList, r *http.Request) ServerCPULoadInfo { + var cpuLoads []cpu.Load + var historicLoads []cpu.Load + seenHosts := set.NewStringSet() + for _, endpoint := range endpoints { + if seenHosts.Contains(endpoint.Host) { + continue + } + seenHosts.Add(endpoint.Host) + + // Only proceed for local endpoints + if endpoint.IsLocal { + cpuLoads = append(cpuLoads, cpu.GetLoad()) + historicLoads = append(historicLoads, cpu.GetHistoricLoad()) + } + } + addr := r.Host + if globalIsDistXL { + addr = GetLocalPeer(endpoints) + } + return ServerCPULoadInfo{ + Addr: addr, + Load: cpuLoads, + HistoricLoad: historicLoads, + } +} + +// getLocalDrivesPerf - returns ServerDrivesPerfInfo for only the +// local endpoints from given list of endpoints +func getLocalDrivesPerf(endpoints EndpointList, r *http.Request) ServerDrivesPerfInfo { + var dps []disk.Performance + for _, endpoint := range endpoints { + // Only proceed for local endpoints + if endpoint.IsLocal { + if _, err := os.Stat(endpoint.Path); err != nil { + // Since this drive is not available, add relevant details and proceed + dps = append(dps, disk.Performance{Path: endpoint.Path, Error: err.Error()}) + continue + } + dp := disk.GetPerformance(pathJoin(endpoint.Path, minioMetaTmpBucket, mustGetUUID())) + dp.Path = endpoint.Path + dps = append(dps, dp) + } + } + addr := r.Host + if globalIsDistXL { + addr = GetLocalPeer(endpoints) + } + return ServerDrivesPerfInfo{ + Addr: addr, + Perf: dps, + } +} diff --git a/cmd/endpoint.go b/cmd/endpoint.go index c5f79d9d9..80bbe13fa 100644 --- a/cmd/endpoint.go +++ b/cmd/endpoint.go @@ -1,5 +1,5 @@ /* - * MinIO Cloud Storage, (C) 2017 MinIO, Inc. + * MinIO Cloud Storage, (C) 2017-2019 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import ( "context" "fmt" "net" - "net/http" "net/url" "os" "path" @@ -34,9 +33,6 @@ import ( "github.com/minio/cli" "github.com/minio/minio-go/v6/pkg/set" "github.com/minio/minio/cmd/logger" - "github.com/minio/minio/pkg/cpu" - "github.com/minio/minio/pkg/disk" - "github.com/minio/minio/pkg/mem" "github.com/minio/minio/pkg/mountinfo" ) @@ -287,89 +283,6 @@ func (endpoints EndpointList) UpdateIsLocal() error { } -// localEndpointsMemUsage - returns ServerMemUsageInfo for only the -// local endpoints from given list of endpoints -func localEndpointsMemUsage(endpoints EndpointList, r *http.Request) ServerMemUsageInfo { - var memUsages []mem.Usage - var historicUsages []mem.Usage - scratchSpace := map[string]bool{} - for _, endpoint := range endpoints { - // Only proceed for local endpoints - if endpoint.IsLocal { - if _, ok := scratchSpace[endpoint.Host]; ok { - continue - } - memUsages = append(memUsages, mem.GetUsage()) - historicUsages = append(historicUsages, mem.GetHistoricUsage()) - scratchSpace[endpoint.Host] = true - } - } - addr := r.Host - if globalIsDistXL { - addr = GetLocalPeer(endpoints) - } - return ServerMemUsageInfo{ - Addr: addr, - Usage: memUsages, - HistoricUsage: historicUsages, - } -} - -// localEndpointsCPULoad - returns ServerCPULoadInfo for only the -// local endpoints from given list of endpoints -func localEndpointsCPULoad(endpoints EndpointList, r *http.Request) ServerCPULoadInfo { - var cpuLoads []cpu.Load - var historicLoads []cpu.Load - scratchSpace := map[string]bool{} - for _, endpoint := range endpoints { - // Only proceed for local endpoints - if endpoint.IsLocal { - if _, ok := scratchSpace[endpoint.Host]; ok { - continue - } - cpuLoads = append(cpuLoads, cpu.GetLoad()) - historicLoads = append(historicLoads, cpu.GetHistoricLoad()) - scratchSpace[endpoint.Host] = true - } - } - addr := r.Host - if globalIsDistXL { - addr = GetLocalPeer(endpoints) - } - return ServerCPULoadInfo{ - Addr: addr, - Load: cpuLoads, - HistoricLoad: historicLoads, - } -} - -// localEndpointsDrivePerf - returns ServerDrivesPerfInfo for only the -// local endpoints from given list of endpoints -func localEndpointsDrivePerf(endpoints EndpointList, r *http.Request) ServerDrivesPerfInfo { - var dps []disk.Performance - for _, endpoint := range endpoints { - // Only proceed for local endpoints - if endpoint.IsLocal { - if _, err := os.Stat(endpoint.Path); err != nil { - // Since this drive is not available, add relevant details and proceed - dps = append(dps, disk.Performance{Path: endpoint.Path, Error: err.Error()}) - continue - } - dp := disk.GetPerformance(pathJoin(endpoint.Path, minioMetaTmpBucket, mustGetUUID())) - dp.Path = endpoint.Path - dps = append(dps, dp) - } - } - addr := r.Host - if globalIsDistXL { - addr = GetLocalPeer(endpoints) - } - return ServerDrivesPerfInfo{ - Addr: addr, - Perf: dps, - } -} - // NewEndpointList - returns new endpoint list based on input args. func NewEndpointList(args ...string) (endpoints EndpointList, err error) { var endpointType EndpointType diff --git a/cmd/peer-rest-server.go b/cmd/peer-rest-server.go index cfe534d05..b29f8007f 100644 --- a/cmd/peer-rest-server.go +++ b/cmd/peer-rest-server.go @@ -418,7 +418,7 @@ func (s *peerRESTServer) CPULoadInfoHandler(w http.ResponseWriter, r *http.Reque } ctx := newContext(r, w, "CPULoadInfo") - info := localEndpointsCPULoad(globalEndpoints, r) + info := getLocalCPULoad(globalEndpoints, r) defer w.(http.Flusher).Flush() logger.LogIf(ctx, gob.NewEncoder(w).Encode(info)) @@ -432,7 +432,7 @@ func (s *peerRESTServer) DrivePerfInfoHandler(w http.ResponseWriter, r *http.Req } ctx := newContext(r, w, "DrivePerfInfo") - info := localEndpointsDrivePerf(globalEndpoints, r) + info := getLocalDrivesPerf(globalEndpoints, r) defer w.(http.Flusher).Flush() logger.LogIf(ctx, gob.NewEncoder(w).Encode(info)) @@ -445,7 +445,7 @@ func (s *peerRESTServer) MemUsageInfoHandler(w http.ResponseWriter, r *http.Requ return } ctx := newContext(r, w, "MemUsageInfo") - info := localEndpointsMemUsage(globalEndpoints, r) + info := getLocalMemUsage(globalEndpoints, r) defer w.(http.Flusher).Flush() logger.LogIf(ctx, gob.NewEncoder(w).Encode(info))