Skip to content

Commit

Permalink
Merge pull request #258 from TRON-US/migrate-status-url
Browse files Browse the repository at this point in the history
[v0.2.3] BTFS-1081: Migrate status server url to status.btfs.io
  • Loading branch information
laipogo authored Nov 27, 2019
2 parents 54f71ec + 43024ad commit eeba1e1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 48 deletions.
76 changes: 42 additions & 34 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package analytics
import (
"context"
"fmt"
"github.com/cenkalti/backoff"
"github.com/dustin/go-humanize"
"github.com/gogo/protobuf/proto"
"github.com/tron-us/go-btfs-common/protos/node"
pb "github.com/tron-us/go-btfs-common/protos/status"
"google.golang.org/grpc"
"runtime"
"time"

"github.com/TRON-US/go-btfs/core"
"github.com/tron-us/go-btfs-common/protos/node"
pb "github.com/tron-us/go-btfs-common/protos/status"
cutils "github.com/tron-us/go-btfs-common/utils"

"github.com/cenkalti/backoff"
"github.com/dustin/go-humanize"
"github.com/gogo/protobuf/proto"
"github.com/ipfs/go-bitswap"
logging "github.com/ipfs/go-log"
ic "github.com/libp2p/go-libp2p-crypto"

"github.com/shirou/gopsutil/cpu"
"google.golang.org/grpc"
)

type programInfo struct {
Expand Down Expand Up @@ -46,17 +47,15 @@ type dataCollection struct {
NumPeers uint64 `json:"peers_connected"` //Number of peers
}

//Server URL for data collection
var statusServerDomain string

// other constants
const (
kilobyte = 1024

//HeartBeat is how often we send data to server, at the moment set to 15 Minutes
// HeartBeat is how often we send data to server, at the moment set to 15 Minutes
heartBeat = 15 * time.Minute

maxRetryTimes = 3
// Expotentially delayed retries will be capped at this total time
maxRetryTotal = 10 * time.Minute

dialTimeout = time.Minute

Expand All @@ -76,19 +75,19 @@ func durationToSeconds(duration time.Duration) uint64 {
return uint64(duration.Nanoseconds() / int64(time.Second/time.Nanosecond))
}

var log = logging.Logger("analytics/analytics")

//Initialize starts the process to collect data and starts the GoRoutine for constant collection
func Initialize(n *core.IpfsNode, BTFSVersion, hValue string) {
if n == nil {
return
}
var log = logging.Logger("cmd/btfs")

configuration, err := n.Repo.Config()
if err != nil {
return
}

statusServerDomain = configuration.StatusServerDomain

dc := new(dataCollection)
dc.node = n

Expand Down Expand Up @@ -158,48 +157,56 @@ func (dc *dataCollection) getGrpcConn() (*grpc.ClientConn, context.CancelFunc, e
}

ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
conn, err := grpc.DialContext(ctx, config.StatusServerDomain, grpc.WithInsecure(), grpc.WithDisableRetry())
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to status server: %s", err.Error())
}
return conn, cancel, nil
conn, err := cutils.NewGRPCConn(ctx, config.Services.StatusServerDomain)
return conn, cancel, err
}

func (dc *dataCollection) sendData() {
sm, err := dc.doPrepData()
if err != nil {
log.Error("failed to prepare data for status server: ", err)
dc.reportHealthAlert(err.Error())
return
}

// Retry for connections
retry(func() error {
return dc.doSendData()
err := dc.doSendData(sm)
if err != nil {
log.Error("failed to send data to status server: ", err)
}
return err
})
}

func (dc *dataCollection) doSendData() error {
func (dc *dataCollection) doPrepData() (*pb.SignedMetrics, error) {
dc.update()
payload, err := dc.getPayload()
if err != nil {
dc.reportHealthAlert(fmt.Sprintf("failed to marshal dataCollection object to a byte array: %s", err.Error()))
return err
return nil, fmt.Errorf("failed to marshal dataCollection object to a byte array: %s", err.Error())
}
if dc.node.PrivateKey == nil {
dc.reportHealthAlert("node's private key is null")
return err
return nil, fmt.Errorf("node's private key is null")
}

signature, err := dc.node.PrivateKey.Sign(payload)
if err != nil {
dc.reportHealthAlert(fmt.Sprintf("failed to sign raw data with node private key: %s", err.Error()))
return err
return nil, fmt.Errorf("failed to sign raw data with node private key: %s", err.Error())
}

publicKey, err := ic.MarshalPublicKey(dc.node.PrivateKey.GetPublic())
if err != nil {
dc.reportHealthAlert(fmt.Sprintf("failed to marshal node public key: %s", err.Error()))
return err
return nil, fmt.Errorf("failed to marshal node public key: %s", err.Error())
}

sm := new(pb.SignedMetrics)
sm.Payload = payload
sm.Signature = signature
sm.PublicKey = publicKey
return sm, nil
}

func (dc *dataCollection) doSendData(sm *pb.SignedMetrics) error {
conn, cancel, err := dc.getGrpcConn()
if err != nil {
return err
Expand All @@ -220,7 +227,7 @@ func (dc *dataCollection) doSendData() error {
func (dc *dataCollection) getPayload() ([]byte, error) {
nd := new(node.Node)
now := time.Now().UTC()
nd.TimeCreated = &now
nd.TimeCreated = now
nd.NodeId = dc.NodeID
nd.BtfsVersion = dc.BTFSVersion
nd.ArchType = dc.ArchType
Expand All @@ -242,7 +249,6 @@ func (dc *dataCollection) getPayload() ([]byte, error) {
nd.StorageVolumeCap = storageMax
}
}
nd.Settings = &node.Node_Settings{}
bytes, err := proto.Marshal(nd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -271,7 +277,9 @@ func (dc *dataCollection) collectionAgent() {
}

func retry(f func() error) {
backoff.Retry(f, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetryTimes))
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = maxRetryTotal
backoff.Retry(f, bo)
}

func (dc *dataCollection) reportHealthAlert(failurePoint string) {
Expand All @@ -293,7 +301,7 @@ func (dc *dataCollection) doReportHealthAlert(failurePoint string) error {
n.FailurePoint = failurePoint
n.NodeId = dc.NodeID
now := time.Now().UTC()
n.TimeCreated = &now
n.TimeCreated = now

ctx, cancel := context.WithTimeout(context.Background(), callTimeout)
defer cancel()
Expand Down
12 changes: 11 additions & 1 deletion cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
migrate "github.com/TRON-US/go-btfs/repo/fsrepo/migrations"

cmds "github.com/TRON-US/go-btfs-cmds"
config "github.com/TRON-US/go-btfs-config"
"github.com/hashicorp/go-multierror"
util "github.com/ipfs/go-ipfs-util"
mprome "github.com/ipfs/go-metrics-prometheus"
Expand Down Expand Up @@ -306,6 +307,15 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
return err
}

migrated := config.MigrateConfig(cfg)
if migrated {
// Flush changes if migrated
err = repo.SetConfig(cfg)
if err != nil {
return err
}
}

offline, _ := req.Options[offlineKwd].(bool)
ipnsps, _ := req.Options[enableIPNSPubSubKwd].(bool)
pubsub, _ := req.Options[enablePubSubKwd].(bool)
Expand Down Expand Up @@ -442,7 +452,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment

// BTFS functional test
if runStartupTest {
functest(cfg.StatusServerDomain, cfg.Identity.PeerID, hValue)
functest(cfg.Services.StatusServerDomain, cfg.Identity.PeerID, hValue)
}

//Begin sending analytics to hosted server
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 // indirect
github.com/TRON-US/go-btfs-cmds v0.1.2
github.com/TRON-US/go-btfs-config v0.1.6
github.com/TRON-US/go-btfs-config v0.2.1
github.com/blang/semver v3.5.1+incompatible
github.com/bren2010/proquint v0.0.0-20160323162903-38337c27106d
github.com/cenkalti/backoff v2.1.1+incompatible
Expand Down Expand Up @@ -114,7 +114,7 @@ require (
github.com/prometheus/procfs v0.0.0-20190519111021-9935e8e0588d // indirect
github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7
github.com/syndtr/goleveldb v1.0.0
github.com/tron-us/go-btfs-common v0.0.10
github.com/tron-us/go-btfs-common v0.1.1
github.com/tyler-smith/go-bip32 v0.0.0-20170922074101-2c9cfd177564
github.com/tyler-smith/go-bip39 v1.0.0
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc
Expand All @@ -127,12 +127,12 @@ require (
go.uber.org/goleak v0.10.0 // indirect
go.uber.org/zap v1.10.0
go4.org v0.0.0-20190313082347-94abd6928b1d // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb
golang.org/x/sys v0.0.0-20191010194322-b09406accb47
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
google.golang.org/grpc v1.24.0
gopkg.in/cheggaaa/pb.v1 v1.0.28
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2 v2.2.4
gotest.tools/gotestsum v0.3.4
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
Expand Down
Loading

0 comments on commit eeba1e1

Please sign in to comment.