2022-06-06 19:14:52 -04:00
|
|
|
// Copyright (c) 2015-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 (
|
2022-11-15 16:53:05 -05:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2022-06-06 19:14:52 -04:00
|
|
|
"context"
|
2022-11-15 16:53:05 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-06-06 19:14:52 -04:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2022-11-15 16:53:05 -05:00
|
|
|
"net/url"
|
2022-06-06 19:14:52 -04:00
|
|
|
"time"
|
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2022-06-06 19:14:52 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
|
|
|
)
|
|
|
|
|
2022-10-27 03:20:01 -04:00
|
|
|
var callhomeLeaderLockTimeout = newDynamicTimeout(30*time.Second, 10*time.Second)
|
2022-06-06 19:14:52 -04:00
|
|
|
|
|
|
|
// initCallhome will start the callhome task in the background.
|
|
|
|
func initCallhome(ctx context.Context, objAPI ObjectLayer) {
|
2022-10-27 03:20:01 -04:00
|
|
|
if !globalCallhomeConfig.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:14:52 -04:00
|
|
|
go func() {
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
// Leader node (that successfully acquires the lock inside runCallhome)
|
|
|
|
// will keep performing the callhome. If the leader goes down for some reason,
|
|
|
|
// the lock will be released and another node will acquire it and take over
|
|
|
|
// because of this loop.
|
|
|
|
for {
|
2022-10-27 03:20:01 -04:00
|
|
|
if !globalCallhomeConfig.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !runCallhome(ctx, objAPI) {
|
|
|
|
// callhome was disabled or context was canceled
|
2022-06-06 19:14:52 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// callhome running on a different node.
|
|
|
|
// sleep for some time and try again.
|
2022-10-27 03:20:01 -04:00
|
|
|
duration := time.Duration(r.Float64() * float64(globalCallhomeConfig.FrequencyDur()))
|
2022-06-06 19:14:52 -04:00
|
|
|
if duration < time.Second {
|
|
|
|
// Make sure to sleep atleast a second to avoid high CPU ticks.
|
|
|
|
duration = time.Second
|
|
|
|
}
|
|
|
|
time.Sleep(duration)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-10-27 03:20:01 -04:00
|
|
|
func runCallhome(ctx context.Context, objAPI ObjectLayer) bool {
|
2022-06-06 19:14:52 -04:00
|
|
|
// Make sure only 1 callhome is running on the cluster.
|
|
|
|
locker := objAPI.NewNSLock(minioMetaBucket, "callhome/runCallhome.lock")
|
|
|
|
lkctx, err := locker.GetLock(ctx, callhomeLeaderLockTimeout)
|
|
|
|
if err != nil {
|
2022-10-27 03:20:01 -04:00
|
|
|
// lock timedout means some other node is the leader,
|
|
|
|
// cycle back return 'true'
|
|
|
|
return true
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx = lkctx.Context()
|
2022-12-23 22:49:07 -05:00
|
|
|
defer locker.Unlock(lkctx)
|
2022-06-06 19:14:52 -04:00
|
|
|
|
2022-10-27 03:20:01 -04:00
|
|
|
callhomeTimer := time.NewTimer(globalCallhomeConfig.FrequencyDur())
|
2022-06-06 19:14:52 -04:00
|
|
|
defer callhomeTimer.Stop()
|
|
|
|
|
|
|
|
for {
|
2022-10-27 03:20:01 -04:00
|
|
|
if !globalCallhomeConfig.Enabled() {
|
|
|
|
// Stop the processing as callhome got disabled
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:14:52 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-10-27 03:20:01 -04:00
|
|
|
// indicates that we do not need to run callhome anymore
|
|
|
|
return false
|
2022-06-06 19:14:52 -04:00
|
|
|
case <-callhomeTimer.C:
|
2022-10-27 03:20:01 -04:00
|
|
|
if !globalCallhomeConfig.Enabled() {
|
2022-06-06 19:14:52 -04:00
|
|
|
// Stop the processing as callhome got disabled
|
2022-10-27 03:20:01 -04:00
|
|
|
return false
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
2022-10-27 03:20:01 -04:00
|
|
|
|
2022-06-06 19:14:52 -04:00
|
|
|
performCallhome(ctx)
|
|
|
|
|
|
|
|
// Reset the timer for next cycle.
|
2022-10-27 03:20:01 -04:00
|
|
|
callhomeTimer.Reset(globalCallhomeConfig.FrequencyDur())
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func performCallhome(ctx context.Context) {
|
2022-11-15 16:53:05 -05:00
|
|
|
deadline := 10 * time.Second // Default deadline is 10secs for callhome
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
logger.LogIf(ctx, errors.New("Callhome: object layer not ready"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
healthCtx, healthCancel := context.WithTimeout(ctx, deadline)
|
|
|
|
defer healthCancel()
|
|
|
|
|
|
|
|
healthInfoCh := make(chan madmin.HealthInfo)
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
for _, k := range madmin.HealthDataTypesList {
|
|
|
|
query.Set(string(k), "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
healthInfo := madmin.HealthInfo{
|
2022-11-23 10:11:22 -05:00
|
|
|
TimeStamp: time.Now().UTC(),
|
|
|
|
Version: madmin.HealthInfoVersion,
|
2022-11-15 16:53:05 -05:00
|
|
|
Minio: madmin.MinioHealthInfo{
|
|
|
|
Info: madmin.MinioInfo{
|
2023-10-18 11:06:57 -04:00
|
|
|
DeploymentID: globalDeploymentID(),
|
2022-11-15 16:53:05 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go fetchHealthInfo(healthCtx, objectAPI, &query, healthInfoCh, healthInfo)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case hi, hasMore := <-healthInfoCh:
|
|
|
|
if !hasMore {
|
|
|
|
// Received all data. Send to SUBNET and return
|
|
|
|
err := sendHealthInfo(ctx, healthInfo)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Unable to perform callhome: %w", err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
healthInfo = hi
|
|
|
|
case <-healthCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
2022-06-06 19:14:52 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 06:58:25 -04:00
|
|
|
|
|
|
|
const (
|
2022-11-15 16:53:05 -05:00
|
|
|
healthURL = "https://subnet.min.io/api/health/upload"
|
|
|
|
healthURLDev = "http://localhost:9000/api/health/upload"
|
2022-06-27 06:58:25 -04:00
|
|
|
)
|
|
|
|
|
2022-11-15 16:53:05 -05:00
|
|
|
func sendHealthInfo(ctx context.Context, healthInfo madmin.HealthInfo) error {
|
|
|
|
url := healthURL
|
2022-06-27 06:58:25 -04:00
|
|
|
if globalIsCICD {
|
2022-11-15 16:53:05 -05:00
|
|
|
url = healthURLDev
|
2022-06-27 06:58:25 -04:00
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
filename := fmt.Sprintf("health_%s.json.gz", UTCNow().Format("20060102150405"))
|
2023-03-23 03:24:58 -04:00
|
|
|
url += "?filename=" + filename
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
_, err := globalSubnetConfig.Upload(url, filename, createHealthJSONGzip(ctx, healthInfo))
|
2022-06-27 06:58:25 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-15 16:53:05 -05:00
|
|
|
|
|
|
|
func createHealthJSONGzip(ctx context.Context, healthInfo madmin.HealthInfo) []byte {
|
|
|
|
var b bytes.Buffer
|
|
|
|
gzWriter := gzip.NewWriter(&b)
|
|
|
|
|
|
|
|
header := struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}{Version: healthInfo.Version}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(gzWriter)
|
|
|
|
if e := enc.Encode(header); e != nil {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Could not encode health info header: %w", e))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := enc.Encode(healthInfo); e != nil {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Could not encode health info: %w", e))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gzWriter.Flush()
|
|
|
|
gzWriter.Close()
|
|
|
|
|
|
|
|
return b.Bytes()
|
|
|
|
}
|