From 2aa060a8c6c874a5a5388abeb2828b1b5a6fd584 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 30 Jul 2021 22:21:04 +0300 Subject: [PATCH] take advantage of MarkSet/Visitor unification to atomically check+mark --- blockstore/splitstore/splitstore_compact.go | 93 ++++++++++++++++----- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/blockstore/splitstore/splitstore_compact.go b/blockstore/splitstore/splitstore_compact.go index 2ae5c9e68b7..ac65e6e68a4 100644 --- a/blockstore/splitstore/splitstore_compact.go +++ b/blockstore/splitstore/splitstore_compact.go @@ -296,17 +296,33 @@ func (s *SplitStore) doTxnProtect(root cid.Cid, markSet MarkSet) error { return errStopWalk } - mark, err := markSet.Has(c) - if err != nil { - return xerrors.Errorf("error checking markset: %w", err) - } + visitor, ok := markSet.(MarkSetVisitor) + if ok { + visit, err := visitor.Visit(c) + if err != nil { + return xerrors.Errorf("error visiting object: %w", err) + } - // it's marked, nothing to do - if mark { - return errStopWalk + if !visit { + return errStopWalk + } + } else { + mark, err := markSet.Has(c) + if err != nil { + return xerrors.Errorf("error checking markset: %w", err) + } + + // it's marked, nothing to do + if mark { + return errStopWalk + } + + if err = markSet.Mark(c); err != nil { + return xerrors.Errorf("error marking object: %w", err) + } } - return markSet.Mark(c) + return nil }, func(c cid.Cid) error { if s.txnMissing != nil { @@ -416,17 +432,32 @@ func (s *SplitStore) doCompact(curTs *types.TipSet) error { return errStopWalk } - has, err := markSet.Has(c) - if err != nil { - return xerrors.Errorf("error checking markset: %w", err) - } + if visitor, ok := markSet.(MarkSetVisitor); ok { + visit, err := visitor.Visit(c) + if err != nil { + return xerrors.Errorf("error visiting object: %w", err) + } - if has { - return errStopWalk + if !visit { + return errStopWalk + } + } else { + has, err := markSet.Has(c) + if err != nil { + return xerrors.Errorf("error checking markset: %w", err) + } + + if has { + return errStopWalk + } + + if err = markSet.Mark(c); err != nil { + return xerrors.Errorf("error marking: %w", err) + } } count++ - return markSet.Mark(c) + return nil }) if err != nil { @@ -1111,17 +1142,33 @@ func (s *SplitStore) waitForMissingRefs(markSet MarkSet) { return errStopWalk } - mark, err := markSet.Has(c) - if err != nil { - return xerrors.Errorf("error checking markset for %s: %w", c, err) - } - - if mark { - return errStopWalk + visitor, ok := markSet.(MarkSetVisitor) + if ok { + visit, err := visitor.Visit(c) + if err != nil { + return xerrors.Errorf("error visiting object: %w", err) + } + + if !visit { + return errStopWalk + } + } else { + mark, err := markSet.Has(c) + if err != nil { + return xerrors.Errorf("error checking markset for %s: %w", c, err) + } + + if mark { + return errStopWalk + } + + if err = markSet.Mark(c); err != nil { + return xerrors.Errorf("error marking object: %w", err) + } } count++ - return markSet.Mark(c) + return nil }, func(c cid.Cid) error { missing[c] = struct{}{}