From b3211e87a2b57a8b37f6a2db69655224f542a863 Mon Sep 17 00:00:00 2001 From: zhoujunhao <18853925545@163.com> Date: Fri, 27 Jul 2018 12:55:18 +0800 Subject: [PATCH] [FMT] Optimized structure catalog --- node/nodem/healthy/health_manager.go | 92 +++++++++++++------ node/nodem/healthy/{ => probe}/http_probe.go | 36 ++++---- .../healthy/{ => probe}/http_probe_test.go | 2 +- node/nodem/healthy/{ => probe}/shell_probe.go | 33 +++---- node/nodem/healthy/{ => probe}/tcp_probe.go | 33 +++---- 5 files changed, 114 insertions(+), 82 deletions(-) rename node/nodem/healthy/{ => probe}/http_probe.go (70%) rename node/nodem/healthy/{ => probe}/http_probe_test.go (97%) rename node/nodem/healthy/{ => probe}/shell_probe.go (71%) rename node/nodem/healthy/{ => probe}/tcp_probe.go (69%) diff --git a/node/nodem/healthy/health_manager.go b/node/nodem/healthy/health_manager.go index 6ea3a7139..2a7cc5e73 100644 --- a/node/nodem/healthy/health_manager.go +++ b/node/nodem/healthy/health_manager.go @@ -26,6 +26,8 @@ import ( "time" "github.com/goodrain/rainbond/node/nodem/client" "sync" + "github.com/goodrain/rainbond/node/nodem/healthy/probe" + "errors" ) //Manager Manager @@ -42,6 +44,7 @@ type Watcher interface { Watch() <-chan *service.HealthStatus Close() error } + type watcher struct { manager Manager statusChan chan *service.HealthStatus @@ -93,44 +96,41 @@ func (p *probeManager) Start(hostNode *client.HostNode) (error) { logrus.Info("health mode start") go p.HandleStatus() - logrus.Info("services length===>",len(p.services)) - for _,v :=range p.services{ - logrus.Info("Need to check===>",v.ServiceHealth.Name,v.ServiceHealth.Model) - } + logrus.Info("services length===>", len(p.services)) for _, v := range p.services { if v.ServiceHealth.Model == "http" { - h := &HttpProbe{ - name: v.ServiceHealth.Name, - address: v.ServiceHealth.Address, - ctx: p.ctx, - cancel: p.cancel, - resultsChan: p.statusChan, + h := &probe.HttpProbe{ + Name: v.ServiceHealth.Name, + Address: v.ServiceHealth.Address, + Ctx: p.ctx, + Cancel: p.cancel, + ResultsChan: p.statusChan, TimeInterval: v.ServiceHealth.TimeInterval, - hostNode: hostNode, + HostNode: hostNode, } - go h.Check() + go h.HttpCheck() } if v.ServiceHealth.Model == "tcp" { - t := &TcpProbe{ - name: v.ServiceHealth.Name, - address: v.ServiceHealth.Address, - ctx: p.ctx, - cancel: p.cancel, - resultsChan: p.statusChan, + t := &probe.TcpProbe{ + Name: v.ServiceHealth.Name, + Address: v.ServiceHealth.Address, + Ctx: p.ctx, + Cancel: p.cancel, + ResultsChan: p.statusChan, TimeInterval: v.ServiceHealth.TimeInterval, - hostNode: hostNode, + HostNode: hostNode, } go t.TcpCheck() } if v.ServiceHealth.Model == "cmd" { - s := &ShellProbe{ - name: v.ServiceHealth.Name, - address: v.ServiceHealth.Address, - ctx: p.ctx, - cancel: p.cancel, - resultsChan: p.statusChan, + s := &probe.ShellProbe{ + Name: v.ServiceHealth.Name, + Address: v.ServiceHealth.Address, + Ctx: p.ctx, + Cancel: p.cancel, + ResultsChan: p.statusChan, TimeInterval: v.ServiceHealth.TimeInterval, - hostNode: hostNode, + HostNode: hostNode, } go s.ShellCheck() } @@ -219,3 +219,43 @@ func (p *probeManager) WatchServiceHealthy(serviceName string) Watcher { } return w } + +func (p *probeManager) GetCurrentServiceHealthy(serviceName string) (*service.HealthStatus, error) { + + for _, v := range p.services { + if v.Name == serviceName { + + if v.ServiceHealth.Model == "http" { + statusMap := probe.GetHttpHealth(v.ServiceHealth.Address) + result := &service.HealthStatus{ + Name: v.Name, + Status: statusMap["status"], + Info: statusMap["info"], + } + return result, nil + } + if v.ServiceHealth.Model == "tcp" { + statusMap := probe.GetTcpHealth(v.ServiceHealth.Address) + result := &service.HealthStatus{ + Name: v.Name, + Status: statusMap["status"], + Info: statusMap["info"], + } + return result, nil + + } + if v.ServiceHealth.Model == "cmd" { + statusMap := probe.GetShellHealth(v.ServiceHealth.Address) + result := &service.HealthStatus{ + Name: v.Name, + Status: statusMap["status"], + Info: statusMap["info"], + } + return result, nil + } + } else { + return nil, errors.New("the service does not exist") + } + } + return nil, errors.New("service list is empty") +} diff --git a/node/nodem/healthy/http_probe.go b/node/nodem/healthy/probe/http_probe.go similarity index 70% rename from node/nodem/healthy/http_probe.go rename to node/nodem/healthy/probe/http_probe.go index d2d5945ba..931f4545a 100644 --- a/node/nodem/healthy/http_probe.go +++ b/node/nodem/healthy/probe/http_probe.go @@ -1,4 +1,4 @@ -package healthy +package probe import ( "context" @@ -9,49 +9,47 @@ import ( "github.com/goodrain/rainbond/node/nodem/client" ) -type Probe interface { - Check() -} + type HttpProbe struct { - name string - address string - resultsChan chan *service.HealthStatus - ctx context.Context - cancel context.CancelFunc + Name string + Address string + ResultsChan chan *service.HealthStatus + Ctx context.Context + Cancel context.CancelFunc TimeInterval int - hostNode *client.HostNode + HostNode *client.HostNode } -func (h *HttpProbe) Check() { +func (h *HttpProbe) HttpCheck() { - util.Exec(h.ctx, func() error { - HealthMap := GetHttpHealth(h.address) + util.Exec(h.Ctx, func() error { + HealthMap := GetHttpHealth(h.Address) result := &service.HealthStatus{ - Name: h.name, + Name: h.Name, Status: HealthMap["status"], Info: HealthMap["info"], } if HealthMap["status"] != service.Stat_healthy { v := client.NodeCondition{ - Type: client.NodeConditionType(h.name), + Type: client.NodeConditionType(h.Name), Status: client.ConditionFalse, LastHeartbeatTime: time.Now(), LastTransitionTime: time.Now(), Message: result.Info, } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } if HealthMap["status"] == service.Stat_healthy { v := client.NodeCondition{ - Type: client.NodeConditionType(h.name), + Type: client.NodeConditionType(h.Name), Status: client.ConditionTrue, LastHeartbeatTime: time.Now(), LastTransitionTime: time.Now(), } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } - h.resultsChan <- result + h.ResultsChan <- result return nil }, time.Second*time.Duration(h.TimeInterval)) diff --git a/node/nodem/healthy/http_probe_test.go b/node/nodem/healthy/probe/http_probe_test.go similarity index 97% rename from node/nodem/healthy/http_probe_test.go rename to node/nodem/healthy/probe/http_probe_test.go index 7955ae164..1b5a65744 100644 --- a/node/nodem/healthy/http_probe_test.go +++ b/node/nodem/healthy/probe/http_probe_test.go @@ -1,4 +1,4 @@ -package healthy +package probe // //import ( // "testing" diff --git a/node/nodem/healthy/shell_probe.go b/node/nodem/healthy/probe/shell_probe.go similarity index 71% rename from node/nodem/healthy/shell_probe.go rename to node/nodem/healthy/probe/shell_probe.go index 9b1f7c1c8..8aeefb629 100644 --- a/node/nodem/healthy/shell_probe.go +++ b/node/nodem/healthy/probe/shell_probe.go @@ -1,4 +1,4 @@ -package healthy +package probe import ( "context" @@ -11,49 +11,46 @@ import ( "github.com/goodrain/rainbond/node/nodem/client" ) -type SHELLProbe interface { - Check() -} type ShellProbe struct { - name string - address string - resultsChan chan *service.HealthStatus - ctx context.Context - cancel context.CancelFunc + Name string + Address string + ResultsChan chan *service.HealthStatus + Ctx context.Context + Cancel context.CancelFunc TimeInterval int - hostNode *client.HostNode + HostNode *client.HostNode } func (h *ShellProbe) ShellCheck() { - util.Exec(h.ctx, func() error { - HealthMap := GetShellHealth(h.address) + util.Exec(h.Ctx, func() error { + HealthMap := GetShellHealth(h.Address) result := &service.HealthStatus{ - Name: h.name, + Name: h.Name, Status: HealthMap["status"], Info: HealthMap["info"], } if HealthMap["status"] != service.Stat_healthy { v := client.NodeCondition{ - Type: client.NodeConditionType(h.name), + Type: client.NodeConditionType(h.Name), Status: client.ConditionFalse, LastHeartbeatTime:time.Now(), LastTransitionTime:time.Now(), Message: result.Info, } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } if HealthMap["status"] == service.Stat_healthy{ v := client.NodeCondition{ - Type:client.NodeConditionType(h.name), + Type:client.NodeConditionType(h.Name), Status:client.ConditionTrue, LastHeartbeatTime:time.Now(), LastTransitionTime:time.Now(), } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } - h.resultsChan <- result + h.ResultsChan <- result return nil }, time.Second*time.Duration(h.TimeInterval)) diff --git a/node/nodem/healthy/tcp_probe.go b/node/nodem/healthy/probe/tcp_probe.go similarity index 69% rename from node/nodem/healthy/tcp_probe.go rename to node/nodem/healthy/probe/tcp_probe.go index 6d2cfb3ea..1ffe5a982 100644 --- a/node/nodem/healthy/tcp_probe.go +++ b/node/nodem/healthy/probe/tcp_probe.go @@ -1,4 +1,4 @@ -package healthy +package probe import ( "context" @@ -9,49 +9,46 @@ import ( "github.com/goodrain/rainbond/node/nodem/client" ) -type TCPProbe interface { - TcpCheck() -} type TcpProbe struct { - name string - address string - resultsChan chan *service.HealthStatus - ctx context.Context - cancel context.CancelFunc + Name string + Address string + ResultsChan chan *service.HealthStatus + Ctx context.Context + Cancel context.CancelFunc TimeInterval int - hostNode *client.HostNode + HostNode *client.HostNode } func (h *TcpProbe) TcpCheck() { - util.Exec(h.ctx, func() error { - HealthMap := GetTcpHealth(h.address) + util.Exec(h.Ctx, func() error { + HealthMap := GetTcpHealth(h.Address) result := &service.HealthStatus{ - Name: h.name, + Name: h.Name, Status: HealthMap["status"], Info: HealthMap["info"], } if HealthMap["status"] != service.Stat_healthy { v := client.NodeCondition{ - Type: client.NodeConditionType(h.name), + Type: client.NodeConditionType(h.Name), Status: client.ConditionFalse, LastHeartbeatTime:time.Now(), LastTransitionTime:time.Now(), Message: result.Info, } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } if HealthMap["status"] == service.Stat_healthy{ v := client.NodeCondition{ - Type:client.NodeConditionType(h.name), + Type:client.NodeConditionType(h.Name), Status:client.ConditionTrue, LastHeartbeatTime:time.Now(), LastTransitionTime:time.Now(), } - h.hostNode.UpdataCondition(v) + h.HostNode.UpdataCondition(v) } - h.resultsChan <- result + h.ResultsChan <- result return nil }, time.Second*time.Duration(h.TimeInterval)) }