[#156] services/tree: Split syncLoop() in functions

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-03-21 16:51:21 +03:00 committed by Gitea
parent 47e8c5bf23
commit 5368c4207a

View file

@ -246,7 +246,6 @@ func (s *Service) SynchronizeAll() error {
} }
} }
// nolint: funlen, gocognit
func (s *Service) syncLoop(ctx context.Context) { func (s *Service) syncLoop(ctx context.Context) {
for { for {
select { select {
@ -263,31 +262,21 @@ func (s *Service) syncLoop(ctx context.Context) {
continue continue
} }
newMap := make(map[cid.ID]struct{}, len(s.cnrMap)) newMap, cnrsToSync := s.containersToSync(cnrs)
cnrsToSync := make([]cid.ID, 0, len(cnrs))
var removed []cid.ID s.syncContainers(ctx, cnrsToSync)
for _, cnr := range cnrs {
_, pos, err := s.getContainerNodes(cnr) s.removeContainers(ctx, newMap)
if err != nil {
s.log.Error("could not calculate container nodes", s.log.Debug("trees have been synchronized")
zap.Stringer("cid", cnr), }
zap.Error(err)) }
continue
}
if pos < 0 {
// node is not included in the container.
continue
}
newMap[cnr] = struct{}{}
cnrsToSync = append(cnrsToSync, cnr)
} }
func (s *Service) syncContainers(ctx context.Context, cnrs []cid.ID) {
// sync new containers // sync new containers
var wg sync.WaitGroup var wg sync.WaitGroup
for _, cnr := range cnrsToSync { for _, cnr := range cnrs {
wg.Add(1) wg.Add(1)
cnr := cnr cnr := cnr
err := s.syncPool.Submit(func() { err := s.syncPool.Submit(func() {
@ -313,10 +302,15 @@ func (s *Service) syncLoop(ctx context.Context) {
} }
} }
wg.Wait() wg.Wait()
}
func (s *Service) removeContainers(ctx context.Context, newContainers map[cid.ID]struct{}) {
s.cnrMapMtx.Lock() s.cnrMapMtx.Lock()
defer s.cnrMapMtx.Unlock()
var removed []cid.ID
for cnr := range s.cnrMap { for cnr := range s.cnrMap {
if _, ok := newMap[cnr]; ok { if _, ok := newContainers[cnr]; ok {
continue continue
} }
removed = append(removed, cnr) removed = append(removed, cnr)
@ -324,23 +318,41 @@ func (s *Service) syncLoop(ctx context.Context) {
for i := range removed { for i := range removed {
delete(s.cnrMap, removed[i]) delete(s.cnrMap, removed[i])
} }
s.cnrMapMtx.Unlock()
for _, cnr := range removed { for _, cnr := range removed {
s.log.Debug("removing redundant trees...", zap.Stringer("cid", cnr)) s.log.Debug("removing redundant trees...", zap.Stringer("cid", cnr))
err = s.DropTree(ctx, cnr, "") err := s.DropTree(ctx, cnr, "")
if err != nil { if err != nil {
s.log.Error("could not remove redundant tree", s.log.Error("could not remove redundant tree",
zap.Stringer("cid", cnr), zap.Stringer("cid", cnr),
zap.Error(err)) zap.Error(err))
continue }
} }
} }
s.log.Debug("trees have been synchronized") func (s *Service) containersToSync(cnrs []cid.ID) (map[cid.ID]struct{}, []cid.ID) {
newMap := make(map[cid.ID]struct{}, len(s.cnrMap))
cnrsToSync := make([]cid.ID, 0, len(cnrs))
for _, cnr := range cnrs {
_, pos, err := s.getContainerNodes(cnr)
if err != nil {
s.log.Error("could not calculate container nodes",
zap.Stringer("cid", cnr),
zap.Error(err))
continue
} }
if pos < 0 {
// node is not included in the container.
continue
} }
newMap[cnr] = struct{}{}
cnrsToSync = append(cnrsToSync, cnr)
}
return newMap, cnrsToSync
} }
// randomizeNodeOrder shuffles nodes and removes not a `pos` index. // randomizeNodeOrder shuffles nodes and removes not a `pos` index.