From d8f4cba4896797af01957ac921cc9bc637835575 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Fri, 2 Aug 2019 20:30:49 +0800 Subject: [PATCH] add --- toolbar/precog/common/const.go | 4 ++-- toolbar/precog/database/orm/node.go | 25 +++++++++++++------------ toolbar/precog/monitor/monitor.go | 28 +++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/toolbar/precog/common/const.go b/toolbar/precog/common/const.go index 3962b779..d22f93b1 100644 --- a/toolbar/precog/common/const.go +++ b/toolbar/precog/common/const.go @@ -4,7 +4,7 @@ const ( NodeUnknownStatus uint8 = iota NodeHealthyStatus NodeCongestedStatus - NodeBusyStatus + NodeOrphanStatus NodeOfflineStatus ) @@ -12,6 +12,6 @@ var StatusMap = map[uint8]string{ NodeUnknownStatus: "unknown", NodeHealthyStatus: "healthy", NodeCongestedStatus: "congested", - NodeBusyStatus: "busy", + NodeOrphanStatus: "orphan", NodeOfflineStatus: "offline", } diff --git a/toolbar/precog/database/orm/node.go b/toolbar/precog/database/orm/node.go index 7e29c596..a470f262 100644 --- a/toolbar/precog/database/orm/node.go +++ b/toolbar/precog/database/orm/node.go @@ -10,7 +10,7 @@ import ( type Node struct { Alias string - PubKey chainkd.XPub + PubKey string Host string Port uint16 BestHeight uint64 @@ -23,22 +23,23 @@ type Node struct { func (n *Node) MarshalJSON() ([]byte, error) { status := common.StatusMap[n.Status] - var lantency uint64 + var lantency int64 var activeMinutes uint64 - if n.Status != common.NodeOfflineStatus { - lantency = n.LantencyMS + switch n.Status { + case common.NodeHealthyStatus, common.NodeCongestedStatus, common.NodeOrphanStatus: + lantency = n.LantencyMS.Int64 activeMinutes = uint64(time.Since(n.ActiveBeginTime).Minutes()) } return json.Marshal(&struct { - Alias string `json:"alias"` - PubKey chainkd.XPub `json:"pubkey"` - Host string `json:"host"` - Port uint16 `json:"port"` - BestHeight uint64 `json:"best_height"` - LantencyMS uint64 `json:"lantency_ms,omitempty"` - ActiveMinutes uint64 `json:"active_minutes,omitempty"` - Status string `json:"status"` + Alias string `json:"alias"` + PubKey string `json:"pubkey"` + Host string `json:"host"` + Port uint16 `json:"port"` + BestHeight uint64 `json:"best_height"` + LantencyMS int64 `json:"lantency_ms,omitempty"` + ActiveMinutes uint64 `json:"active_minutes,omitempty"` + Status string `json:"status"` }{ Alias: n.Alias, PubKey: n.PubKey, diff --git a/toolbar/precog/monitor/monitor.go b/toolbar/precog/monitor/monitor.go index d5a3df4c..15dffc10 100644 --- a/toolbar/precog/monitor/monitor.go +++ b/toolbar/precog/monitor/monitor.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/vapor/toolbar/precog/config" + "github.com/vapor/toolbar/precog/database/orm" ) type monitor struct { @@ -23,9 +24,10 @@ func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor { func (m *monitor) Run() { if err := m.updateBootstrapNodes(); err != nil { - log.Fatal(err) + log.Error(err) } + go m.discovery() ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second) for ; true; <-ticker.C { // TODO: lock? @@ -33,12 +35,32 @@ func (m *monitor) Run() { } } +// create or update: https://github.com/jinzhu/gorm/issues/1307 func (m *monitor) updateBootstrapNodes() error { - // TODO: updated existed nodes - // TODO: add new nodes + for _, node := range m.cfg.Nodes { + ormNode := &orm.Node{ + PubKey: node.PubKey.String(), + Alias: node.Alias, + Host: node.Host, + Port: node.Port, + } + if err := m.db.Where(&orm.Node{PubKey: ormNode.PubKey}). + Assign(&orm.Node{ + Alias: node.Alias, + Host: node.Host, + Port: node.Port, + }).FirstOrCreate(ormNode).Error; err != nil { + return err + } + } + return nil } +// TODO: +func (m *monitor) discovery() { +} + func (m *monitor) monitorRountine() error { // TODO: dail, get lantency & best_height // TODO: decide check_height("best best_height" - "confirmations") -- 2.11.0