add stop services logic

pull/526/head
Minh Doan 6 years ago committed by Minh Doan
parent 1b20f36b20
commit e3aeeb3f98
  1. 13
      api/service/manager.go
  2. 57
      node/service_setup.go

@ -31,6 +31,7 @@ const (
BlockProposal BlockProposal
NetworkInfo NetworkInfo
PeerDiscovery PeerDiscovery
Resharding
Staking Staking
Test Test
Done Done
@ -56,6 +57,8 @@ func (t Type) String() string {
return "Staking" return "Staking"
case PeerDiscovery: case PeerDiscovery:
return "PeerDiscovery" return "PeerDiscovery"
case Resharding:
return "Resharding"
case Test: case Test:
return "Test" return "Test"
case Done: case Done:
@ -102,6 +105,10 @@ func (m *Manager) Register(t Type, service Interface) {
if m.services == nil { if m.services == nil {
m.services = make(map[Type]Interface) m.services = make(map[Type]Interface)
} }
if _, ok := m.services[t]; ok {
utils.GetLogInstance().Error("This service is already included: ", "servie", t)
return
}
m.services[t] = service m.services[t] = service
} }
@ -185,3 +192,9 @@ func (m *Manager) SetupServiceMessageChan(mapServiceTypeChan map[Type]chan *msg_
service.SetMessageChan(mapServiceTypeChan[serviceType]) service.SetMessageChan(mapServiceTypeChan[serviceType])
} }
} }
func (m *Manager) StopService(t Type) {
if service, ok := m.services[t]; ok {
service.StopService()
}
}

@ -35,6 +35,16 @@ func (node *Node) setupForShardLeader() {
node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand))
} }
func (node *Node) stopShardLeaderServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
node.serviceManager.StopService(service.SupportExplorer)
node.serviceManager.StopService(service.Consensus)
node.serviceManager.StopService(service.BlockProposal)
node.serviceManager.StopService(service.ClientSupport)
node.serviceManager.StopService(service.Randomness)
}
func (node *Node) setupForShardValidator() { func (node *Node) setupForShardValidator() {
nodeConfig, chanPeer := node.initNodeConfiguration() nodeConfig, chanPeer := node.initNodeConfiguration()
@ -44,6 +54,11 @@ func (node *Node) setupForShardValidator() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
} }
func (node *Node) stopShardValidatorServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
}
func (node *Node) setupForBeaconLeader() { func (node *Node) setupForBeaconLeader() {
nodeConfig, chanPeer := node.initBeaconNodeConfiguration() nodeConfig, chanPeer := node.initBeaconNodeConfiguration()
@ -61,6 +76,15 @@ func (node *Node) setupForBeaconLeader() {
node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand))
} }
func (node *Node) stopBeaconLeaderServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
node.serviceManager.StopService(service.Consensus)
node.serviceManager.StopService(service.BlockProposal)
node.serviceManager.StopService(service.ClientSupport)
node.serviceManager.StopService(service.Randomness)
}
func (node *Node) setupForBeaconValidator() { func (node *Node) setupForBeaconValidator() {
nodeConfig, chanPeer := node.initBeaconNodeConfiguration() nodeConfig, chanPeer := node.initBeaconNodeConfiguration()
@ -70,6 +94,11 @@ func (node *Node) setupForBeaconValidator() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
} }
func (node *Node) stopBeaconValidatorServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
}
func (node *Node) setupForNewNode() { func (node *Node) setupForNewNode() {
nodeConfig, chanPeer := node.initNodeConfiguration() nodeConfig, chanPeer := node.initNodeConfiguration()
@ -83,6 +112,12 @@ func (node *Node) setupForNewNode() {
// TODO: how to restart networkinfo and discovery service after receiving shard id info from beacon chain? // TODO: how to restart networkinfo and discovery service after receiving shard id info from beacon chain?
} }
func (node *Node) stopNewNodeServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
node.serviceManager.StopService(service.Staking)
}
func (node *Node) setupForClientNode() { func (node *Node) setupForClientNode() {
nodeConfig, chanPeer := node.initNodeConfiguration() nodeConfig, chanPeer := node.initNodeConfiguration()
@ -92,6 +127,11 @@ func (node *Node) setupForClientNode() {
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil)) node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, chanPeer, nil))
} }
func (node *Node) stopClientNodeServices() {
node.serviceManager.StopService(service.PeerDiscovery)
node.serviceManager.StopService(service.NetworkInfo)
}
// ServiceManagerSetup setups service store. // ServiceManagerSetup setups service store.
func (node *Node) ServiceManagerSetup() { func (node *Node) ServiceManagerSetup() {
node.serviceManager = &service.Manager{} node.serviceManager = &service.Manager{}
@ -121,3 +161,20 @@ func (node *Node) RunServices() {
} }
node.serviceManager.RunServices() node.serviceManager.RunServices()
} }
func (node *Node) StopServicesByRole(role Role) {
switch role {
case ShardLeader:
node.stopShardLeaderServices()
case ShardValidator:
node.stopShardValidatorServices()
case BeaconLeader:
node.stopBeaconLeaderServices()
case BeaconValidator:
node.stopBeaconValidatorServices()
case NewNode:
node.stopNewNodeServices()
case ClientNode:
node.stopClientNodeServices()
}
}

Loading…
Cancel
Save