github.com/chain5j/chain5j-pkg@v1.0.7/database/kvstore/leveldb/leveldb.go (about)

     1  package leveldb
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/chain5j/chain5j-pkg/database/kvstore"
    11  	"github.com/chain5j/chain5j-pkg/types"
    12  	"github.com/chain5j/chain5j-pkg/util/hexutil"
    13  	"github.com/chain5j/logger"
    14  	"github.com/syndtr/goleveldb/leveldb"
    15  	"github.com/syndtr/goleveldb/leveldb/errors"
    16  	"github.com/syndtr/goleveldb/leveldb/filter"
    17  	"github.com/syndtr/goleveldb/leveldb/opt"
    18  	"github.com/syndtr/goleveldb/leveldb/util"
    19  )
    20  
    21  var (
    22  	EnableLogPrint = false
    23  )
    24  
    25  const (
    26  	// degradationWarnInterval specifies how often warning should be printed if the
    27  	// leveldb database cannot keep up with requested writes.
    28  	degradationWarnInterval = time.Minute
    29  
    30  	// minCache is the minimum amount of memory in megabytes to allocate to leveldb
    31  	// read and write caching, split half and half.
    32  	minCache = 16
    33  
    34  	// minHandles is the minimum number of files handles to allocate to the open
    35  	// database files.
    36  	minHandles = 16
    37  
    38  	// metricsGatheringInterval specifies the interval to retrieve leveldb database
    39  	// compaction, io and pause stats to report to the user.
    40  	metricsGatheringInterval = 3 * time.Second
    41  )
    42  
    43  // Database is a persistent key-value store. Apart from basic data storage
    44  // functionality it also supports batch writes and iterating over the keyspace in
    45  // binary-alphabetical order.
    46  type Database struct {
    47  	fn string      // filename for reporting
    48  	db *leveldb.DB // LevelDB instance
    49  
    50  	quitLock sync.Mutex      // Mutex protecting the quit channel access
    51  	quitChan chan chan error // Quit channel to stop the metrics collection before closing the database
    52  
    53  	log logger.Logger // Contextual logger tracking the database path
    54  }
    55  
    56  // New returns a wrapped LevelDB object. The namespace is the prefix that the
    57  // metrics reporting should use for surfacing internal stats.
    58  func New(file string, cache int, handles int, namespace string) (*Database, error) {
    59  	// Ensure we have some minimal caching and file guarantees
    60  	if cache < minCache {
    61  		cache = minCache
    62  	}
    63  	if handles < minHandles {
    64  		handles = minHandles
    65  	}
    66  	logger := logger.New("levelDB")
    67  	logger.Debug("Allocated cache and file handles", "database", file, "cache", types.StorageSize(cache*1024*1024), "handles", handles)
    68  	// Open the db and recover any potential corruptions
    69  	db, err := leveldb.OpenFile(file, &opt.Options{
    70  		OpenFilesCacheCapacity: handles,
    71  		BlockCacheCapacity:     cache / 2 * opt.MiB,
    72  		WriteBuffer:            cache / 4 * opt.MiB, // Two of these are used internally
    73  		Filter:                 filter.NewBloomFilter(10),
    74  		DisableSeeksCompaction: true,
    75  		// Compression:            opt.NoCompression,
    76  	})
    77  	if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
    78  		db, err = leveldb.RecoverFile(file, nil)
    79  	}
    80  	if err != nil {
    81  		logger.Error("leveldb.OpenFile err", "err", err)
    82  		return nil, err
    83  	}
    84  	// Assemble the wrapper with all the registered metrics
    85  	ldb := &Database{
    86  		fn:       file,
    87  		db:       db,
    88  		log:      logger,
    89  		quitChan: make(chan chan error),
    90  	}
    91  
    92  	// Start up the metrics gathering and return
    93  	go ldb.meter(metricsGatheringInterval)
    94  	return ldb, nil
    95  }
    96  
    97  // Close stops the metrics collection, flushes any pending data to disk and closes
    98  // all io accesses to the underlying key-value store.
    99  func (db *Database) Close() error {
   100  	db.quitLock.Lock()
   101  	defer db.quitLock.Unlock()
   102  
   103  	if db.quitChan != nil {
   104  		errc := make(chan error)
   105  		db.quitChan <- errc
   106  		if err := <-errc; err != nil {
   107  			db.log.Error("Metrics collection failed", "err", err)
   108  		}
   109  		db.quitChan = nil
   110  	}
   111  	return db.db.Close()
   112  }
   113  
   114  // Has retrieves if a key is present in the key-value store.
   115  func (db *Database) Has(key []byte) (bool, error) {
   116  	return db.db.Has(key, nil)
   117  }
   118  
   119  // Get retrieves the given key if it's present in the key-value store.
   120  func (db *Database) Get(key []byte) ([]byte, error) {
   121  	dat, err := db.db.Get(key, nil)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	if EnableLogPrint {
   126  		fmt.Println("leveldb database get", "key", hexutil.Encode(key), "val", hexutil.Encode(dat))
   127  	}
   128  	return dat, nil
   129  }
   130  
   131  // Put inserts the given value into the key-value store.
   132  func (db *Database) Put(key []byte, value []byte) error {
   133  	return db.db.Put(key, value, nil)
   134  }
   135  
   136  // Delete removes the key from the key-value store.
   137  func (db *Database) Delete(key []byte) error {
   138  	return db.db.Delete(key, nil)
   139  }
   140  
   141  // NewBatch creates a write-only key-value store that buffers changes to its host
   142  // database until a final write is called.
   143  func (db *Database) NewBatch() kvstore.Batch {
   144  	return &batch{
   145  		db: db.db,
   146  		b:  new(leveldb.Batch),
   147  	}
   148  }
   149  
   150  // NewIterator creates a binary-alphabetical iterator over the entire keyspace
   151  // contained within the leveldb database.
   152  func (db *Database) NewIterator() kvstore.Iterator {
   153  	return db.db.NewIterator(new(util.Range), nil)
   154  }
   155  
   156  // NewIteratorWithStart creates a binary-alphabetical iterator over a subset of
   157  // database content starting at a particular initial key (or after, if it does
   158  // not exist).
   159  func (db *Database) NewIteratorWithStart(start []byte) kvstore.Iterator {
   160  	return db.db.NewIterator(&util.Range{Start: start}, nil)
   161  }
   162  
   163  // NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
   164  // of database content with a particular key prefix.
   165  func (db *Database) NewIteratorWithPrefix(prefix []byte) kvstore.Iterator {
   166  	return db.db.NewIterator(util.BytesPrefix(prefix), nil)
   167  }
   168  
   169  // Stat returns a particular internal stat of the database.
   170  func (db *Database) Stat(property string) (string, error) {
   171  	return db.db.GetProperty(property)
   172  }
   173  
   174  // Compact flattens the underlying data store for the given key range. In essence,
   175  // deleted and overwritten versions are discarded, and the data is rearranged to
   176  // reduce the cost of operations needed to access them.
   177  //
   178  // A nil start is treated as a key before all keys in the data store; a nil limit
   179  // is treated as a key after all keys in the data store. If both is nil then it
   180  // will compact entire data store.
   181  func (db *Database) Compact(start []byte, limit []byte) error {
   182  	return db.db.CompactRange(util.Range{Start: start, Limit: limit})
   183  }
   184  
   185  // Path returns the path to the database directory.
   186  func (db *Database) Path() string {
   187  	return db.fn
   188  }
   189  
   190  // meter periodically retrieves internal leveldb counters and reports them to
   191  // the metrics subsystem.
   192  //
   193  // This is how a LevelDB stats table looks like (currently):
   194  //
   195  //	Compactions
   196  //	 Level |   Tables   |    Size(MB)   |    Time(sec)  |    Read(MB)   |   Write(MB)
   197  //	-------+------------+---------------+---------------+---------------+---------------
   198  //	   0   |          0 |       0.00000 |       1.27969 |       0.00000 |      12.31098
   199  //	   1   |         85 |     109.27913 |      28.09293 |     213.92493 |     214.26294
   200  //	   2   |        523 |    1000.37159 |       7.26059 |      66.86342 |      66.77884
   201  //	   3   |        570 |    1113.18458 |       0.00000 |       0.00000 |       0.00000
   202  //
   203  // This is how the write delay look like (currently):
   204  // DelayN:5 Delay:406.604657ms Paused: false
   205  //
   206  // This is how the iostats look like (currently):
   207  // Read(MB):3895.04860 Write(MB):3654.64712
   208  func (db *Database) meter(refresh time.Duration) {
   209  	// Create the counters to store current and previous compaction values
   210  	compactions := make([][]float64, 2)
   211  	for i := 0; i < 2; i++ {
   212  		compactions[i] = make([]float64, 4)
   213  	}
   214  	// Create storage for iostats.
   215  	var iostats [2]float64
   216  
   217  	// Create storage and warning log tracer for write delay.
   218  	var (
   219  		delaystats      [2]int64
   220  		lastWritePaused time.Time
   221  	)
   222  
   223  	var (
   224  		errc chan error
   225  		merr error
   226  	)
   227  
   228  	// Iterate ad infinitum and collect the stats
   229  	for i := 1; errc == nil && merr == nil; i++ {
   230  		// Retrieve the database stats
   231  		stats, err := db.db.GetProperty("leveldb.stats")
   232  		if err != nil {
   233  			db.log.Error("Failed to read database stats", "err", err)
   234  			merr = err
   235  			continue
   236  		}
   237  		// Find the compaction table, skip the header
   238  		lines := strings.Split(stats, "\n")
   239  		for len(lines) > 0 && strings.TrimSpace(lines[0]) != "Compactions" {
   240  			lines = lines[1:]
   241  		}
   242  		if len(lines) <= 3 {
   243  			db.log.Error("Compaction leveldbTable not found")
   244  			merr = errors.New("compaction leveldbTable not found")
   245  			continue
   246  		}
   247  		lines = lines[3:]
   248  
   249  		// Iterate over all the leveldbTable rows, and accumulate the entries
   250  		for j := 0; j < len(compactions[i%2]); j++ {
   251  			compactions[i%2][j] = 0
   252  		}
   253  		for _, line := range lines {
   254  			parts := strings.Split(line, "|")
   255  			if len(parts) != 6 {
   256  				break
   257  			}
   258  			for idx, counter := range parts[2:] {
   259  				value, err := strconv.ParseFloat(strings.TrimSpace(counter), 64)
   260  				if err != nil {
   261  					db.log.Error("Compaction entry parsing failed", "err", err)
   262  					merr = err
   263  					continue
   264  				}
   265  				compactions[i%2][idx] += value
   266  			}
   267  		}
   268  		// Retrieve the write delay statistic
   269  		writedelay, err := db.db.GetProperty("leveldb.writedelay")
   270  		if err != nil {
   271  			db.log.Error("Failed to read database write delay statistic", "err", err)
   272  			merr = err
   273  			continue
   274  		}
   275  		var (
   276  			delayN        int64
   277  			delayDuration string
   278  			duration      time.Duration
   279  			paused        bool
   280  		)
   281  		if n, err := fmt.Sscanf(writedelay, "DelayN:%d Delay:%s Paused:%t", &delayN, &delayDuration, &paused); n != 3 || err != nil {
   282  			db.log.Error("Write delay statistic not found")
   283  			merr = err
   284  			continue
   285  		}
   286  		duration, err = time.ParseDuration(delayDuration)
   287  		if err != nil {
   288  			db.log.Error("Failed to parse delay duration", "err", err)
   289  			merr = err
   290  			continue
   291  		}
   292  		// If a warning that db is performing compaction has been displayed, any subsequent
   293  		// warnings will be withheld for one minute not to overwhelm the user.
   294  		if paused && delayN-delaystats[0] == 0 && duration.Nanoseconds()-delaystats[1] == 0 &&
   295  			time.Now().After(lastWritePaused.Add(degradationWarnInterval)) {
   296  			db.log.Warn("Database compacting, degraded performance")
   297  			lastWritePaused = time.Now()
   298  		}
   299  		delaystats[0], delaystats[1] = delayN, duration.Nanoseconds()
   300  
   301  		// Retrieve the database iostats.
   302  		ioStats, err := db.db.GetProperty("leveldb.iostats")
   303  		if err != nil {
   304  			db.log.Error("Failed to read database iostats", "err", err)
   305  			merr = err
   306  			continue
   307  		}
   308  		var nRead, nWrite float64
   309  		parts := strings.Split(ioStats, " ")
   310  		if len(parts) < 2 {
   311  			db.log.Error("Bad syntax of ioStats", "ioStats", ioStats)
   312  			merr = fmt.Errorf("bad syntax of ioStats %s", ioStats)
   313  			continue
   314  		}
   315  		if n, err := fmt.Sscanf(parts[0], "Read(MB):%f", &nRead); n != 1 || err != nil {
   316  			db.log.Error("Bad syntax of read entry", "entry", parts[0])
   317  			merr = err
   318  			continue
   319  		}
   320  		if n, err := fmt.Sscanf(parts[1], "Write(MB):%f", &nWrite); n != 1 || err != nil {
   321  			db.log.Error("Bad syntax of write entry", "entry", parts[1])
   322  			merr = err
   323  			continue
   324  		}
   325  		iostats[0], iostats[1] = nRead, nWrite
   326  
   327  		compCount, err := db.db.GetProperty("leveldb.compcount")
   328  		if err != nil {
   329  			db.log.Error("Failed to read database iostats", "err", err)
   330  			merr = err
   331  			continue
   332  		}
   333  
   334  		var (
   335  			memComp       uint32
   336  			level0Comp    uint32
   337  			nonLevel0Comp uint32
   338  			seekComp      uint32
   339  		)
   340  		if n, err := fmt.Sscanf(compCount, "MemComp:%d Level0Comp:%d NonLevel0Comp:%d SeekComp:%d", &memComp, &level0Comp, &nonLevel0Comp, &seekComp); n != 4 || err != nil {
   341  			db.log.Error("Compaction count statistic not found")
   342  			merr = err
   343  			continue
   344  		}
   345  
   346  		// Sleep a bit, then repeat the stats collection
   347  		select {
   348  		case errc = <-db.quitChan:
   349  			// Quit requesting, stop hammering the database
   350  		case <-time.After(refresh):
   351  			// Timeout, gather a new set of stats
   352  		}
   353  	}
   354  
   355  	if errc == nil {
   356  		errc = <-db.quitChan
   357  	}
   358  	errc <- merr
   359  }
   360  
   361  // batch is a write-only leveldb batch that commits changes to its host database
   362  // when Write is called. A batch cannot be used concurrently.
   363  type batch struct {
   364  	db   *leveldb.DB
   365  	b    *leveldb.Batch
   366  	size int
   367  }
   368  
   369  // Put inserts the given value into the batch for later committing.
   370  func (b *batch) Put(key, value []byte) error {
   371  	b.b.Put(key, value)
   372  	b.size += len(value)
   373  	return nil
   374  }
   375  
   376  // Delete inserts the a key removal into the batch for later committing.
   377  func (b *batch) Delete(key []byte) error {
   378  	b.b.Delete(key)
   379  	b.size += len(key)
   380  	return nil
   381  }
   382  
   383  // ValueSize retrieves the amount of data queued up for writing.
   384  func (b *batch) ValueSize() int {
   385  	return b.size
   386  }
   387  
   388  // Write flushes any accumulated data to disk.
   389  func (b *batch) Write() error {
   390  	return b.db.Write(b.b, nil)
   391  }
   392  
   393  // Reset resets the batch for reuse.
   394  func (b *batch) Reset() {
   395  	b.b.Reset()
   396  	b.size = 0
   397  }
   398  
   399  // Replay replays the batch contents.
   400  func (b *batch) Replay(w kvstore.KeyValueWriter) error {
   401  	return b.b.Replay(&replayer{writer: w})
   402  }
   403  
   404  // replayer is a small wrapper to implement the correct replay methods.
   405  type replayer struct {
   406  	writer  kvstore.KeyValueWriter
   407  	failure error
   408  }
   409  
   410  // Put inserts the given value into the key-value data store.
   411  func (r *replayer) Put(key, value []byte) {
   412  	// If the replay already failed, stop executing ops
   413  	if r.failure != nil {
   414  		return
   415  	}
   416  	r.failure = r.writer.Put(key, value)
   417  }
   418  
   419  // Delete removes the key from the key-value data store.
   420  func (r *replayer) Delete(key []byte) {
   421  	// If the replay already failed, stop executing ops
   422  	if r.failure != nil {
   423  		return
   424  	}
   425  	r.failure = r.writer.Delete(key)
   426  }