Fix host address returned in admin API calls (#7846)

master
poornas 5 years ago committed by Harshavardhana
parent 22bc15d89b
commit 0505ef83b5
  1. 16
      cmd/admin-handlers.go
  2. 3
      cmd/admin-handlers_test.go
  3. 26
      cmd/endpoint.go
  4. 15
      cmd/handler-utils.go
  5. 14
      cmd/http-tracer.go
  6. 6
      cmd/peer-rest-server.go

@ -241,8 +241,7 @@ func (a adminAPIHandlers) ServerInfoHandler(w http.ResponseWriter, r *http.Reque
if objectAPI == nil { if objectAPI == nil {
return return
} }
hostName, err := getHostName(r)
thisAddr, err := xnet.ParseHost(GetLocalPeer(globalEndpoints))
if err != nil { if err != nil {
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL) writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
return return
@ -252,7 +251,7 @@ func (a adminAPIHandlers) ServerInfoHandler(w http.ResponseWriter, r *http.Reque
// Once we have received all the ServerInfo from peers // Once we have received all the ServerInfo from peers
// add the local peer server info as well. // add the local peer server info as well.
serverInfo = append(serverInfo, ServerInfo{ serverInfo = append(serverInfo, ServerInfo{
Addr: thisAddr.String(), Addr: hostName,
Data: &ServerInfoData{ Data: &ServerInfoData{
StorageInfo: objectAPI.StorageInfo(ctx), StorageInfo: objectAPI.StorageInfo(ctx),
ConnStats: globalConnStats.toServerConnStats(), ConnStats: globalConnStats.toServerConnStats(),
@ -330,7 +329,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
return return
} }
// Get drive performance details from local server's drive(s) // Get drive performance details from local server's drive(s)
dp := localEndpointsDrivePerf(globalEndpoints) dp := localEndpointsDrivePerf(globalEndpoints, r)
// Notify all other MinIO peers to report drive performance numbers // Notify all other MinIO peers to report drive performance numbers
dps := globalNotificationSys.DrivePerfInfo() dps := globalNotificationSys.DrivePerfInfo()
@ -348,7 +347,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
writeSuccessResponseJSON(w, jsonBytes) writeSuccessResponseJSON(w, jsonBytes)
case "cpu": case "cpu":
// Get CPU load details from local server's cpu(s) // Get CPU load details from local server's cpu(s)
cpu := localEndpointsCPULoad(globalEndpoints) cpu := localEndpointsCPULoad(globalEndpoints, r)
// Notify all other MinIO peers to report cpu load numbers // Notify all other MinIO peers to report cpu load numbers
cpus := globalNotificationSys.CPULoadInfo() cpus := globalNotificationSys.CPULoadInfo()
cpus = append(cpus, cpu) cpus = append(cpus, cpu)
@ -365,7 +364,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
writeSuccessResponseJSON(w, jsonBytes) writeSuccessResponseJSON(w, jsonBytes)
case "mem": case "mem":
// Get mem usage details from local server(s) // Get mem usage details from local server(s)
m := localEndpointsMemUsage(globalEndpoints) m := localEndpointsMemUsage(globalEndpoints, r)
// Notify all other MinIO peers to report mem usage numbers // Notify all other MinIO peers to report mem usage numbers
mems := globalNotificationSys.MemUsageInfo() mems := globalNotificationSys.MemUsageInfo()
mems = append(mems, m) mems = append(mems, m)
@ -443,8 +442,7 @@ func (a adminAPIHandlers) TopLocksHandler(w http.ResponseWriter, r *http.Request
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL) writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL)
return return
} }
hostName, err := getHostName(r)
thisAddr, err := xnet.ParseHost(GetLocalPeer(globalEndpoints))
if err != nil { if err != nil {
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL) writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
return return
@ -455,7 +453,7 @@ func (a adminAPIHandlers) TopLocksHandler(w http.ResponseWriter, r *http.Request
// add the local peer locks list as well. // add the local peer locks list as well.
localLocks := globalLockServer.ll.DupLockMap() localLocks := globalLockServer.ll.DupLockMap()
peerLocks = append(peerLocks, &PeerLocks{ peerLocks = append(peerLocks, &PeerLocks{
Addr: thisAddr.String(), Addr: hostName,
Locks: localLocks, Locks: localLocks,
}) })

@ -706,9 +706,6 @@ func TestAdminServerInfo(t *testing.T) {
} }
for _, serverInfo := range results { for _, serverInfo := range results {
if len(serverInfo.Addr) == 0 {
t.Error("Expected server address to be non empty")
}
if serverInfo.Error != "" { if serverInfo.Error != "" {
t.Errorf("Unexpected error = %v\n", serverInfo.Error) t.Errorf("Unexpected error = %v\n", serverInfo.Error)
} }

@ -20,6 +20,7 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
@ -288,7 +289,7 @@ func (endpoints EndpointList) UpdateIsLocal() error {
// localEndpointsMemUsage - returns ServerMemUsageInfo for only the // localEndpointsMemUsage - returns ServerMemUsageInfo for only the
// local endpoints from given list of endpoints // local endpoints from given list of endpoints
func localEndpointsMemUsage(endpoints EndpointList) ServerMemUsageInfo { func localEndpointsMemUsage(endpoints EndpointList, r *http.Request) ServerMemUsageInfo {
var memUsages []mem.Usage var memUsages []mem.Usage
var historicUsages []mem.Usage var historicUsages []mem.Usage
scratchSpace := map[string]bool{} scratchSpace := map[string]bool{}
@ -303,8 +304,12 @@ func localEndpointsMemUsage(endpoints EndpointList) ServerMemUsageInfo {
scratchSpace[endpoint.Host] = true scratchSpace[endpoint.Host] = true
} }
} }
addr := r.Host
if globalIsDistXL {
addr = GetLocalPeer(endpoints)
}
return ServerMemUsageInfo{ return ServerMemUsageInfo{
Addr: GetLocalPeer(endpoints), Addr: addr,
Usage: memUsages, Usage: memUsages,
HistoricUsage: historicUsages, HistoricUsage: historicUsages,
} }
@ -312,7 +317,7 @@ func localEndpointsMemUsage(endpoints EndpointList) ServerMemUsageInfo {
// localEndpointsCPULoad - returns ServerCPULoadInfo for only the // localEndpointsCPULoad - returns ServerCPULoadInfo for only the
// local endpoints from given list of endpoints // local endpoints from given list of endpoints
func localEndpointsCPULoad(endpoints EndpointList) ServerCPULoadInfo { func localEndpointsCPULoad(endpoints EndpointList, r *http.Request) ServerCPULoadInfo {
var cpuLoads []cpu.Load var cpuLoads []cpu.Load
var historicLoads []cpu.Load var historicLoads []cpu.Load
scratchSpace := map[string]bool{} scratchSpace := map[string]bool{}
@ -327,8 +332,12 @@ func localEndpointsCPULoad(endpoints EndpointList) ServerCPULoadInfo {
scratchSpace[endpoint.Host] = true scratchSpace[endpoint.Host] = true
} }
} }
addr := r.Host
if globalIsDistXL {
addr = GetLocalPeer(endpoints)
}
return ServerCPULoadInfo{ return ServerCPULoadInfo{
Addr: GetLocalPeer(endpoints), Addr: addr,
Load: cpuLoads, Load: cpuLoads,
HistoricLoad: historicLoads, HistoricLoad: historicLoads,
} }
@ -336,7 +345,7 @@ func localEndpointsCPULoad(endpoints EndpointList) ServerCPULoadInfo {
// localEndpointsDrivePerf - returns ServerDrivesPerfInfo for only the // localEndpointsDrivePerf - returns ServerDrivesPerfInfo for only the
// local endpoints from given list of endpoints // local endpoints from given list of endpoints
func localEndpointsDrivePerf(endpoints EndpointList) ServerDrivesPerfInfo { func localEndpointsDrivePerf(endpoints EndpointList, r *http.Request) ServerDrivesPerfInfo {
var dps []disk.Performance var dps []disk.Performance
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
// Only proceed for local endpoints // Only proceed for local endpoints
@ -351,9 +360,12 @@ func localEndpointsDrivePerf(endpoints EndpointList) ServerDrivesPerfInfo {
dps = append(dps, dp) dps = append(dps, dp)
} }
} }
addr := r.Host
if globalIsDistXL {
addr = GetLocalPeer(endpoints)
}
return ServerDrivesPerfInfo{ return ServerDrivesPerfInfo{
Addr: GetLocalPeer(endpoints), Addr: addr,
Perf: dps, Perf: dps,
} }
} }

@ -31,6 +31,7 @@ import (
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/auth" "github.com/minio/minio/pkg/auth"
"github.com/minio/minio/pkg/handlers" "github.com/minio/minio/pkg/handlers"
xnet "github.com/minio/minio/pkg/net"
) )
// Parses location constraint from the incoming reader. // Parses location constraint from the incoming reader.
@ -384,3 +385,17 @@ func notFoundHandlerJSON(w http.ResponseWriter, r *http.Request) {
func notFoundHandler(w http.ResponseWriter, r *http.Request) { func notFoundHandler(w http.ResponseWriter, r *http.Request) {
writeErrorResponse(context.Background(), w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL, guessIsBrowserReq(r)) writeErrorResponse(context.Background(), w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL, guessIsBrowserReq(r))
} }
// gets host name for current node
func getHostName(r *http.Request) (hostName string, err error) {
var thisAddr *xnet.Host
hostName = r.Host
if globalIsDistXL {
thisAddr, err = xnet.ParseHost(GetLocalPeer(globalEndpoints))
if err != nil {
return
}
hostName = thisAddr.String()
}
return
}

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"reflect" "reflect"
"runtime" "runtime"
@ -28,7 +29,6 @@ import (
"strings" "strings"
"time" "time"
xnet "github.com/minio/minio/pkg/net"
trace "github.com/minio/minio/pkg/trace" trace "github.com/minio/minio/pkg/trace"
) )
@ -145,11 +145,15 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ
t := trace.Info{FuncName: name} t := trace.Info{FuncName: name}
reqBodyRecorder = &recordRequest{Reader: r.Body, logBody: logBody} reqBodyRecorder = &recordRequest{Reader: r.Body, logBody: logBody}
r.Body = ioutil.NopCloser(reqBodyRecorder) r.Body = ioutil.NopCloser(reqBodyRecorder)
t.NodeName = r.Host
host, err := xnet.ParseHost(GetLocalPeer(globalEndpoints)) if globalIsDistXL {
if err == nil { t.NodeName = GetLocalPeer(globalEndpoints)
t.NodeName = host.Name }
// strip port from the host address
if host, _, err := net.SplitHostPort(t.NodeName); err == nil {
t.NodeName = host
} }
rq := trace.RequestInfo{Time: time.Now().UTC(), Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery, Client: r.RemoteAddr} rq := trace.RequestInfo{Time: time.Now().UTC(), Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery, Client: r.RemoteAddr}
rq.Headers = cloneHeader(r.Header) rq.Headers = cloneHeader(r.Header)
rq.Headers.Set("Content-Length", strconv.Itoa(int(r.ContentLength))) rq.Headers.Set("Content-Length", strconv.Itoa(int(r.ContentLength)))

@ -332,7 +332,7 @@ func (s *peerRESTServer) CPULoadInfoHandler(w http.ResponseWriter, r *http.Reque
} }
ctx := newContext(r, w, "CPULoadInfo") ctx := newContext(r, w, "CPULoadInfo")
info := localEndpointsCPULoad(globalEndpoints) info := localEndpointsCPULoad(globalEndpoints, r)
defer w.(http.Flusher).Flush() defer w.(http.Flusher).Flush()
logger.LogIf(ctx, gob.NewEncoder(w).Encode(info)) logger.LogIf(ctx, gob.NewEncoder(w).Encode(info))
@ -346,7 +346,7 @@ func (s *peerRESTServer) DrivePerfInfoHandler(w http.ResponseWriter, r *http.Req
} }
ctx := newContext(r, w, "DrivePerfInfo") ctx := newContext(r, w, "DrivePerfInfo")
info := localEndpointsDrivePerf(globalEndpoints) info := localEndpointsDrivePerf(globalEndpoints, r)
defer w.(http.Flusher).Flush() defer w.(http.Flusher).Flush()
logger.LogIf(ctx, gob.NewEncoder(w).Encode(info)) logger.LogIf(ctx, gob.NewEncoder(w).Encode(info))
@ -359,7 +359,7 @@ func (s *peerRESTServer) MemUsageInfoHandler(w http.ResponseWriter, r *http.Requ
return return
} }
ctx := newContext(r, w, "MemUsageInfo") ctx := newContext(r, w, "MemUsageInfo")
info := localEndpointsMemUsage(globalEndpoints) info := localEndpointsMemUsage(globalEndpoints, r)
defer w.(http.Flusher).Flush() defer w.(http.Flusher).Flush()
logger.LogIf(ctx, gob.NewEncoder(w).Encode(info)) logger.LogIf(ctx, gob.NewEncoder(w).Encode(info))

Loading…
Cancel
Save