et-go 20240919重建
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.3 KiB

package agg_worker
import (
"et_analyze"
"gitea.anxinyun.cn/container/common_models"
"github.com/google/uuid"
"log"
"net/rpc"
"node/et_worker/et_recv"
"os"
"time"
)
type AggNode struct {
recvDataHandler *et_recv.RecvDataHanler
}
func NewAggWorker() *AggNode {
return &AggNode{
recvDataHandler: et_recv.NewRecvDataHanler(),
}
}
// Handler 是 RPC 接口,由 master 远程调用
func (the *AggNode) Handler(aggData common_models.AggData, replay *bool) error {
*replay = true
err := the.ConsumerProcess(&aggData)
if err != nil {
return err
}
return nil
}
// ConsumerProcess 处理阈值判断业务
func (the *AggNode) ConsumerProcess(aggData *common_models.AggData) error {
aggHandler := et_analyze.NewAggThresholdHandler()
aggHandler.ProcessData(aggData)
log.Printf("rpc聚集阈值分析[%d]-time[%s]-[%v]", aggData.SensorId, aggData.Date, aggData.Agg)
return nil
}
// RegisterToMaster 调用 master 发布的RPC服务方法 master.NodeRegister
func (the *AggNode) RegisterToMaster() {
connectCount := 0
for {
connectCount++
if connectCount > 3 {
log.Printf("RegisterToMaster 失败 超过%d次,准备退出", connectCount-1)
time.Sleep(time.Second * 10)
os.Exit(1)
}
masterAddr := os.Getenv("masterAddr")
if masterAddr == "" {
masterAddr = "127.0.0.1:50000"
}
time.Sleep(time.Second * 1)
master, err := rpc.Dial("tcp", masterAddr)
if err != nil {
log.Printf("链接失败-> node[%s]", masterAddr)
continue
}
//todo 获取node自己地址
nodeAddr := "127.0.0.1:40001"
status := `{"health_status":"healthy","load_average":{"1_min":0.75,"5_min":1.2,"15_min":0.9},"availability":"available","last_check_time":"2022-01-01T12:00:00Z"}`
resources := `{"cpu":{"cores":4,"usage":"50%","temperature":"60°C"},"memory":{"total":"8GB","used":"4GB","available":"4GB"},"storage":{"total":"256GB","used":"100GB","available":"156GB"}}`
nodeArgs := &common_models.NodeArgs{
ID: uuid.New().String(),
NodeType: "aggNode",
Status: status,
Resources: resources,
Addr: nodeAddr,
ThingIds: []string{},
}
var result bool
err = master.Call("master.NodeRegister", &nodeArgs, &result)
if err != nil {
log.Printf("node[%s]注册到master[%s]异常:%v", masterAddr, nodeAddr, result)
continue
}
break
}
}