Browse Source

增加检查节点状态任务

gitlink
Sydonian 2 years ago
parent
commit
2071a9fbae
3 changed files with 93 additions and 0 deletions
  1. +1
    -0
      internal/config/config.go
  2. +86
    -0
      internal/task/agent_check_state.go
  3. +6
    -0
      internal/task/check_rep_count.go

+ 1
- 0
internal/config/config.go View File

@@ -9,6 +9,7 @@ import (


type Config struct { type Config struct {
MinAvailableRepProportion float32 `json:"minAvailableRepProportion"` // 可用的备份至少要占所有备份的比例,向上去整 MinAvailableRepProportion float32 `json:"minAvailableRepProportion"` // 可用的备份至少要占所有备份的比例,向上去整
NodeUnavailableSeconds int `json:"nodeUnavailableSeconds"` // 如果节点上次上报时间超过这个值,则认为节点已经不可用


Logger log.Config `json:"logger"` Logger log.Config `json:"logger"`
DB db.Config `json:"db"` DB db.Config `json:"db"`


+ 86
- 0
internal/task/agent_check_state.go View File

@@ -0,0 +1,86 @@
package task

import (
"database/sql"
"time"

"github.com/samber/lo"
"gitlink.org.cn/cloudream/common/consts"
"gitlink.org.cn/cloudream/db/model"
mysql "gitlink.org.cn/cloudream/db/sql"
agtcli "gitlink.org.cn/cloudream/rabbitmq/client/agent"
agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
agttsk "gitlink.org.cn/cloudream/rabbitmq/message/agent/task"
"gitlink.org.cn/cloudream/scanner/internal/config"
"gitlink.org.cn/cloudream/utils/logger"
)

type AgentCheckStateTask struct {
NodeIDs []int
}

func NewAgentCheckStateTask(nodeIDs []int) AgentCheckStateTask {
return AgentCheckStateTask{
NodeIDs: nodeIDs,
}
}

func (t *AgentCheckStateTask) TryMerge(other Task) bool {
chkTask, ok := other.(*AgentCheckStateTask)
if !ok {
return false
}

t.NodeIDs = lo.Union(t.NodeIDs, chkTask.NodeIDs)
return true
}

func (t *AgentCheckStateTask) Execute(execCtx *ExecuteContext, execOpts ExecuteOption) {
for _, nodeID := range t.NodeIDs {
node, err := mysql.Node.GetByID(execCtx.DB.SQLCtx(), nodeID)
if err == sql.ErrNoRows {
continue
}

if err != nil {
logger.WithField("NodeID", nodeID).Warnf("get node by id failed, err: %s", err.Error())
continue
}

if node.State != consts.NODE_STATE_NORMAL {
continue
}

// 检查上次上报时间,超时的设置为不可用
if time.Since(node.LastReportTime) > time.Duration(config.Cfg().NodeUnavailableSeconds)*time.Second {
err := mysql.Node.ChangeState(execCtx.DB.SQLCtx(), nodeID, consts.NODE_STATE_UNAVAILABLE)
if err != nil {
logger.WithField("NodeID", nodeID).Warnf("set node state failed, err: %s", err.Error())
continue
}

caches, err := mysql.Cache.GetNodeCaches(execCtx.DB.SQLCtx(), nodeID)
if err != nil {
logger.WithField("NodeID", nodeID).Warnf("get node caches failed, err: %s", err.Error())
continue
}

// 补充备份数
execCtx.Executor.Post(NewCheckRepCountTask(lo.Map(caches, func(ch model.Cache, index int) string { return ch.HashValue })))
continue
}

agentClient, err := agtcli.NewAgentClient(nodeID, &config.Cfg().RabbitMQ)
if err != nil {
logger.WithField("NodeID", nodeID).Warnf("create agent client failed, err: %s", err.Error())
continue
}
defer agentClient.Close()

// 紧急任务
err = agentClient.PostTask(agtmsg.NewPostTaskBody(agttsk.NewCheckStateTask(), true, true))
if err != nil {
logger.WithField("NodeID", nodeID).Warnf("request to agent failed, err: %s", err.Error())
}
}
}

+ 6
- 0
internal/task/check_rep_count.go View File

@@ -21,6 +21,12 @@ type CheckRepCountTask struct {
FileHashes []string FileHashes []string
} }


func NewCheckRepCountTask(fileHashes []string) *CheckRepCountTask {
return &CheckRepCountTask{
FileHashes: fileHashes,
}
}

func (t *CheckRepCountTask) TryMerge(other Task) bool { func (t *CheckRepCountTask) TryMerge(other Task) bool {
chkTask, ok := other.(*CheckRepCountTask) chkTask, ok := other.(*CheckRepCountTask)
if !ok { if !ok {


Loading…
Cancel
Save