Simplify access to local node name (#11907)

The local node name is heavily used in tracing, create a new global 
variable to store it. Multiple goroutines can access it since it won't be
changed later.
This commit is contained in:
Anis Elleuch 2021-03-26 19:37:58 +01:00 committed by GitHub
parent 9efcb9e15c
commit 2c296652f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 48 additions and 53 deletions

View File

@ -517,7 +517,7 @@ func (a adminAPIHandlers) StartProfilingHandler(w http.ResponseWriter, r *http.R
vars := mux.Vars(r)
profiles := strings.Split(vars["profilerType"], ",")
thisAddr, err := xnet.ParseHost(GetLocalPeer(globalEndpoints))
thisAddr, err := xnet.ParseHost(globalLocalNodeName)
if err != nil {
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
return

View File

@ -31,7 +31,7 @@ func getLocalServerProperty(endpointServerPools EndpointServerPools, r *http.Req
var localEndpoints Endpoints
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(endpointServerPools)
addr = globalLocalNodeName
}
network := make(map[string]string)
for _, ep := range endpointServerPools {

View File

@ -40,17 +40,6 @@ type HTTPConsoleLoggerSys struct {
logBuf *ring.Ring
}
func mustGetNodeName(endpointServerPools EndpointServerPools) (nodeName string) {
host, err := xnet.ParseHost(GetLocalPeer(endpointServerPools))
if err != nil {
logger.FatalIf(err, "Unable to start console logging subsystem")
}
if globalIsDistErasure {
nodeName = host.Name
}
return nodeName
}
// NewConsoleLogger - creates new HTTPConsoleLoggerSys with all nodes subscribed to
// the console logging pub sub system
func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys {
@ -63,8 +52,18 @@ func NewConsoleLogger(ctx context.Context) *HTTPConsoleLoggerSys {
}
// SetNodeName - sets the node name if any after distributed setup has initialized
func (sys *HTTPConsoleLoggerSys) SetNodeName(endpointServerPools EndpointServerPools) {
sys.nodeName = mustGetNodeName(endpointServerPools)
func (sys *HTTPConsoleLoggerSys) SetNodeName(nodeName string) {
if !globalIsDistErasure {
sys.nodeName = ""
return
}
host, err := xnet.ParseHost(globalLocalNodeName)
if err != nil {
logger.FatalIf(err, "Unable to start console logging subsystem")
}
sys.nodeName = host.Name
}
// HasLogListeners returns true if console log listeners are registered

View File

@ -761,7 +761,7 @@ func CreateEndpoints(serverAddr string, foundLocal bool, args ...[]string) (Endp
// the first element from the set of peers which indicate that
// they are local. There is always one entry that is local
// even with repeated server endpoints.
func GetLocalPeer(endpointServerPools EndpointServerPools) (localPeer string) {
func GetLocalPeer(endpointServerPools EndpointServerPools, host, port string) (localPeer string) {
peerSet := set.NewStringSet()
for _, ep := range endpointServerPools {
for _, endpoint := range ep.Endpoints {
@ -776,11 +776,11 @@ func GetLocalPeer(endpointServerPools EndpointServerPools) (localPeer string) {
if peerSet.IsEmpty() {
// Local peer can be empty in FS or Erasure coded mode.
// If so, return globalMinioHost + globalMinioPort value.
if globalMinioHost != "" {
return net.JoinHostPort(globalMinioHost, globalMinioPort)
if host != "" {
return net.JoinHostPort(host, port)
}
return net.JoinHostPort("127.0.0.1", globalMinioPort)
return net.JoinHostPort("127.0.0.1", port)
}
return peerSet.ToSlice()[0]
}

View File

@ -334,15 +334,6 @@ func TestCreateEndpoints(t *testing.T) {
// So it means that if you have say localhost:9000 and localhost:9001 as endpointArgs then localhost:9001
// is considered a remote service from localhost:9000 perspective.
func TestGetLocalPeer(t *testing.T) {
tempGlobalMinioAddr := globalMinioAddr
tempGlobalMinioPort := globalMinioPort
defer func() {
globalMinioAddr = tempGlobalMinioAddr
globalMinioPort = tempGlobalMinioPort
}()
globalMinioAddr = ":9000"
globalMinioPort = "9000"
testCases := []struct {
endpointArgs []string
expectedResult string
@ -363,9 +354,9 @@ func TestGetLocalPeer(t *testing.T) {
t.Fatalf("error: expected = <nil>, got = %v", err)
}
}
remotePeer := GetLocalPeer(zendpoints)
if remotePeer != testCase.expectedResult {
t.Fatalf("Test %d: expected: %v, got: %v", i+1, testCase.expectedResult, remotePeer)
localPeer := GetLocalPeer(zendpoints, "", "9000")
if localPeer != testCase.expectedResult {
t.Fatalf("Test %d: expected: %v, got: %v", i+1, testCase.expectedResult, localPeer)
}
}
}

View File

@ -355,7 +355,7 @@ func newErasureSets(ctx context.Context, endpoints Endpoints, storageDisks []Sto
sets: make([]*erasureObjects, setCount),
erasureDisks: make([][]StorageAPI, setCount),
erasureLockers: make([][]dsync.NetLocker, setCount),
erasureLockOwner: GetLocalPeer(globalEndpoints),
erasureLockOwner: globalLocalNodeName,
endpoints: endpoints,
endpointStrings: endpointStrings,
setCount: setCount,

View File

@ -194,6 +194,9 @@ var (
globalEndpoints EndpointServerPools
// The name of this local node, fetched from arguments
globalLocalNodeName string
globalRemoteEndpoints map[string]Endpoint
// Global server's network statistics

View File

@ -541,7 +541,7 @@ func errorResponseHandler(w http.ResponseWriter, r *http.Request) {
// gets host name for current node
func getHostName(r *http.Request) (hostName string) {
if globalIsDistErasure {
hostName = GetLocalPeer(globalEndpoints)
hostName = globalLocalNodeName
} else {
hostName = r.Host
}

View File

@ -35,7 +35,7 @@ import (
func getLocalCPUInfo(ctx context.Context, r *http.Request) madmin.ServerCPUInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
info, err := cpuhw.InfoWithContext(ctx)
@ -106,7 +106,7 @@ func getLocalDrives(ctx context.Context, parallel bool, endpointServerPools Endp
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(endpointServerPools)
addr = globalLocalNodeName
}
if parallel {
return madmin.ServerDrivesInfo{
@ -123,7 +123,7 @@ func getLocalDrives(ctx context.Context, parallel bool, endpointServerPools Endp
func getLocalMemInfo(ctx context.Context, r *http.Request) madmin.ServerMemInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
swap, err := memhw.SwapMemoryWithContext(ctx)
@ -152,7 +152,7 @@ func getLocalMemInfo(ctx context.Context, r *http.Request) madmin.ServerMemInfo
func getLocalProcInfo(ctx context.Context, r *http.Request) madmin.ServerProcInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
errProcInfo := func(tag string, err error) madmin.ServerProcInfo {

View File

@ -34,7 +34,7 @@ import (
func getLocalOsInfo(ctx context.Context, r *http.Request) madmin.ServerOsInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
srvrOsInfo := madmin.ServerOsInfo{Addr: addr}
@ -65,7 +65,7 @@ func getLocalOsInfo(ctx context.Context, r *http.Request) madmin.ServerOsInfo {
func getLocalDiskHwInfo(ctx context.Context, r *http.Request) madmin.ServerDiskHwInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
parts, err := diskhw.PartitionsWithContext(ctx, true)

View File

@ -30,7 +30,7 @@ import (
func getLocalDiskHwInfo(ctx context.Context, r *http.Request) madmin.ServerDiskHwInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
return madmin.ServerDiskHwInfo{
@ -42,7 +42,7 @@ func getLocalDiskHwInfo(ctx context.Context, r *http.Request) madmin.ServerDiskH
func getLocalOsInfo(ctx context.Context, r *http.Request) madmin.ServerOsInfo {
addr := r.Host
if globalIsDistErasure {
addr = GetLocalPeer(globalEndpoints)
addr = globalLocalNodeName
}
return madmin.ServerOsInfo{

View File

@ -127,7 +127,7 @@ func WebTrace(ri *jsonrpc.RequestInfo) trace.Info {
t := trace.Info{FuncName: name}
t.NodeName = r.Host
if globalIsDistErasure {
t.NodeName = GetLocalPeer(globalEndpoints)
t.NodeName = globalLocalNodeName
}
// strip port from the host address
@ -191,7 +191,7 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ
r.Body = ioutil.NopCloser(reqBodyRecorder)
t.NodeName = r.Host
if globalIsDistErasure {
t.NodeName = GetLocalPeer(globalEndpoints)
t.NodeName = globalLocalNodeName
}
// strip port from the host address
if host, _, err := net.SplitHostPort(t.NodeName); err == nil {

View File

@ -1406,7 +1406,7 @@ func ReportMetrics(ctx context.Context, generators func() []MetricsGenerator) <-
if m.VariableLabels == nil {
m.VariableLabels = make(map[string]string)
}
m.VariableLabels[serverName] = GetLocalPeer(globalEndpoints)
m.VariableLabels[serverName] = globalLocalNodeName
for {
select {
case ch <- m:
@ -1453,7 +1453,7 @@ func (c *minioCollectorV2) Collect(ch chan<- prometheus.Metric) {
populateAndPublish(c.generator, func(metric Metric) bool {
labels, values := getOrderedLabelValueArrays(metric.VariableLabels)
values = append(values, GetLocalPeer(globalEndpoints))
values = append(values, globalLocalNodeName)
labels = append(labels, serverName)
if metric.Description.Type == histogramMetric {

View File

@ -538,7 +538,7 @@ func storageMetricsPrometheus(ch chan<- prometheus.Metric) {
}
server := getLocalServerProperty(globalEndpoints, &http.Request{
Host: GetLocalPeer(globalEndpoints),
Host: globalLocalNodeName,
})
onlineDisks, offlineDisks := getOnlineOfflineDisksStats(server.Disks)

View File

@ -354,7 +354,7 @@ func (sys *NotificationSys) DownloadProfilingData(ctx context.Context, writer io
}
// Local host
thisAddr, err := xnet.ParseHost(GetLocalPeer(globalEndpoints))
thisAddr, err := xnet.ParseHost(globalLocalNodeName)
if err != nil {
logger.LogIf(ctx, err)
return profilingDataFound
@ -889,7 +889,7 @@ func (sys *NotificationSys) NetInfo(ctx context.Context) madmin.ServerNetHealthI
}
for i := 0; i < len(sortedGlobalEndpoints); i++ {
if sortedGlobalEndpoints[i] != GetLocalPeer(globalEndpoints) {
if sortedGlobalEndpoints[i] != globalLocalNodeName {
continue
}
for j := 0; j < len(sortedGlobalEndpoints); j++ {
@ -922,7 +922,7 @@ func (sys *NotificationSys) NetInfo(ctx context.Context) madmin.ServerNetHealthI
}
return madmin.ServerNetHealthInfo{
Net: netInfos,
Addr: GetLocalPeer(globalEndpoints),
Addr: globalLocalNodeName,
}
}
@ -997,7 +997,7 @@ func (sys *NotificationSys) NetPerfParallelInfo(ctx context.Context) madmin.Serv
wg.Wait()
return madmin.ServerNetHealthInfo{
Net: netInfos,
Addr: GetLocalPeer(globalEndpoints),
Addr: globalLocalNodeName,
}
}

View File

@ -140,18 +140,20 @@ func serverHandleCmdArgs(ctx *cli.Context) {
globalMinioHost, globalMinioPort = mustSplitHostPort(globalMinioAddr)
globalEndpoints, setupType, err = createServerEndpoints(globalCLIContext.Addr, serverCmdArgs(ctx)...)
logger.FatalIf(err, "Invalid command line arguments")
globalLocalNodeName = GetLocalPeer(globalEndpoints, globalMinioHost, globalMinioPort)
globalRemoteEndpoints = make(map[string]Endpoint)
for _, z := range globalEndpoints {
for _, ep := range z.Endpoints {
if ep.IsLocal {
globalRemoteEndpoints[GetLocalPeer(globalEndpoints)] = ep
globalRemoteEndpoints[globalLocalNodeName] = ep
} else {
globalRemoteEndpoints[ep.Host] = ep
}
}
}
logger.FatalIf(err, "Invalid command line arguments")
// allow transport to be HTTP/1.1 for proxying.
globalProxyTransport = newCustomHTTPProxyTransport(&tls.Config{
@ -417,7 +419,7 @@ func serverMain(ctx *cli.Context) {
serverHandleEnvVars()
// Set node name, only set for distributed setup.
globalConsoleSys.SetNodeName(globalEndpoints)
globalConsoleSys.SetNodeName(globalLocalNodeName)
// Initialize all help
initHelp()