github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/utils/dbutil/compactdb/log.go (about)

     1  package compactdb
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  	"time"
     7  
     8  	"github.com/status-im/keycard-go/hexutils"
     9  	"github.com/unicornultrafoundation/go-helios/u2udb"
    10  	"github.com/unicornultrafoundation/go-u2u/log"
    11  )
    12  
    13  type loggedCompacter struct {
    14  	u2udb.Store
    15  	name string
    16  
    17  	currentOp atomic.Value
    18  
    19  	wg   sync.WaitGroup
    20  	quit chan struct{}
    21  }
    22  
    23  type pair struct {
    24  	p, l []byte
    25  }
    26  
    27  func (s *loggedCompacter) Compact(prev []byte, limit []byte) error {
    28  	s.currentOp.Store(pair{prev, limit})
    29  	if err := s.Store.Compact(prev, limit); err != nil {
    30  		log.Error("Compaction error", "name", s.name, "err", err)
    31  		return err
    32  	}
    33  	return nil
    34  }
    35  
    36  func (s *loggedCompacter) StartLogging() {
    37  	s.wg.Add(1)
    38  	go func() {
    39  		defer s.wg.Done()
    40  		ticker := time.NewTicker(time.Minute)
    41  		for {
    42  			select {
    43  			case <-ticker.C:
    44  				opI := s.currentOp.Load()
    45  				if opI != nil {
    46  					op := opI.(pair)
    47  					// trim keys for nicer human-readable logging
    48  					op.p, op.l = trimAfterDiff(op.p, op.l, 2)
    49  					untilStr := hexutils.BytesToHex(op.l)
    50  					if len(op.l) == 0 {
    51  						untilStr = "end"
    52  					}
    53  					log.Info("Compacting DB", "name", s.name, "until", untilStr)
    54  				}
    55  			case <-s.quit:
    56  				return
    57  			}
    58  		}
    59  	}()
    60  }
    61  
    62  func (s *loggedCompacter) StopLogging() {
    63  	close(s.quit)
    64  	s.wg.Wait()
    65  }