From 2ddff50fd7dd6852055fe0b84d9999d684aed44c Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Fri, 1 Mar 2019 16:21:01 -0800 Subject: [PATCH] add stop all services in service managers except the liveServices list --- api/service/manager.go | 14 +++++++++ api/service/manager_test.go | 30 ++++++++++++++++++ node/service_setup.go | 61 ++++--------------------------------- 3 files changed, 50 insertions(+), 55 deletions(-) diff --git a/api/service/manager.go b/api/service/manager.go index 8c5c40f73..e2fcad4d9 100644 --- a/api/service/manager.go +++ b/api/service/manager.go @@ -199,3 +199,17 @@ func (m *Manager) StopService(t Type) { service.StopService() } } + +// StopServicesByRole stops all service of the given role. +func (m *Manager) StopServicesByRole(liveServices []Type) { + marked := make(map[Type]bool) + for _, s := range liveServices { + marked[s] = true + } + + for t := range m.GetServices() { + if _, ok := marked[t]; !ok { + m.StopService(t) + } + } +} diff --git a/api/service/manager_test.go b/api/service/manager_test.go index 8574e11df..28b809965 100644 --- a/api/service/manager_test.go +++ b/api/service/manager_test.go @@ -10,14 +10,21 @@ import ( type SupportSyncingTest struct { msgChan chan *msg_pb.Message + status *int } func (s *SupportSyncingTest) StartService() { fmt.Println("SupportSyncingTest starting") + if s.status != nil { + *s.status = 1 + } } func (s *SupportSyncingTest) StopService() { fmt.Println("SupportSyncingTest stopping") + if s.status != nil { + *s.status = 2 + } } func (s *SupportSyncingTest) NotifyService(data map[string]interface{}) { @@ -65,3 +72,26 @@ func TestMessageChan(t *testing.T) { m.SendAction(&Action{ServiceType: Done}) } + +func TestStopServices(t *testing.T) { + m := &Manager{} + m.SetupServiceManager() + status := 0 + m.RegisterService(SupportSyncing, &SupportSyncingTest{status: &status}) + msgChans := make(map[Type]chan *msg_pb.Message) + m.SetupServiceMessageChan(msgChans) + + m.SendAction(&Action{ + Action: Notify, + ServiceType: SupportSyncing, + Params: map[string]interface{}{ + "chan": msgChans[SupportSyncing], + "test": t, + }, + }) + + m.StopServicesByRole([]Type{}) + if status != 2 { + t.Error("Service did not stop") + } +} diff --git a/node/service_setup.go b/node/service_setup.go index 19d20243d..6bdc20b49 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -35,16 +35,6 @@ func (node *Node) setupForShardLeader() { 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() { nodeConfig, chanPeer := node.initNodeConfiguration() @@ -54,11 +44,6 @@ func (node *Node) setupForShardValidator() { 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() { nodeConfig, chanPeer := node.initBeaconNodeConfiguration() @@ -76,15 +61,6 @@ func (node *Node) setupForBeaconLeader() { 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() { nodeConfig, chanPeer := node.initBeaconNodeConfiguration() @@ -94,11 +70,6 @@ func (node *Node) setupForBeaconValidator() { 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() { nodeConfig, chanPeer := node.initNodeConfiguration() @@ -112,12 +83,6 @@ func (node *Node) setupForNewNode() { // 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() { nodeConfig, chanPeer := node.initNodeConfiguration() @@ -127,11 +92,6 @@ func (node *Node) setupForClientNode() { 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. func (node *Node) ServiceManagerSetup() { node.serviceManager = &service.Manager{} @@ -162,20 +122,11 @@ func (node *Node) RunServices() { node.serviceManager.RunServices() } -// StopServicesByRole stops all service of the given role. -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() +// StopServices runs registered services. +func (node *Node) StopServices() { + if node.serviceManager == nil { + utils.GetLogInstance().Info("Service manager is not set up yet.") + return } + node.serviceManager.StopServicesByRole([]service.Type{}) }