github.com/calmw/ethereum@v0.1.1/ethdb/pebble/pebble.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 //go:build (arm64 || amd64) && !openbsd 18 19 // Package pebble implements the key-value database layer based on pebble. 20 package pebble 21 22 import ( 23 "bytes" 24 "fmt" 25 "runtime" 26 "sync" 27 "sync/atomic" 28 "time" 29 30 "github.com/calmw/ethereum/common" 31 "github.com/calmw/ethereum/ethdb" 32 "github.com/calmw/ethereum/log" 33 "github.com/calmw/ethereum/metrics" 34 "github.com/cockroachdb/pebble" 35 "github.com/cockroachdb/pebble/bloom" 36 ) 37 38 const ( 39 // minCache is the minimum amount of memory in megabytes to allocate to pebble 40 // read and write caching, split half and half. 41 minCache = 16 42 43 // minHandles is the minimum number of files handles to allocate to the open 44 // database files. 45 minHandles = 16 46 47 // metricsGatheringInterval specifies the interval to retrieve pebble database 48 // compaction, io and pause stats to report to the user. 49 metricsGatheringInterval = 3 * time.Second 50 ) 51 52 // Database is a persistent key-value store based on the pebble storage engine. 53 // Apart from basic data storage functionality it also supports batch writes and 54 // iterating over the keyspace in binary-alphabetical order. 55 type Database struct { 56 fn string // filename for reporting 57 db *pebble.DB // Underlying pebble storage engine 58 59 compTimeMeter metrics.Meter // Meter for measuring the total time spent in database compaction 60 compReadMeter metrics.Meter // Meter for measuring the data read during compaction 61 compWriteMeter metrics.Meter // Meter for measuring the data written during compaction 62 writeDelayNMeter metrics.Meter // Meter for measuring the write delay number due to database compaction 63 writeDelayMeter metrics.Meter // Meter for measuring the write delay duration due to database compaction 64 diskSizeGauge metrics.Gauge // Gauge for tracking the size of all the levels in the database 65 diskReadMeter metrics.Meter // Meter for measuring the effective amount of data read 66 diskWriteMeter metrics.Meter // Meter for measuring the effective amount of data written 67 memCompGauge metrics.Gauge // Gauge for tracking the number of memory compaction 68 level0CompGauge metrics.Gauge // Gauge for tracking the number of table compaction in level0 69 nonlevel0CompGauge metrics.Gauge // Gauge for tracking the number of table compaction in non0 level 70 seekCompGauge metrics.Gauge // Gauge for tracking the number of table compaction caused by read opt 71 manualMemAllocGauge metrics.Gauge // Gauge for tracking amount of non-managed memory currently allocated 72 73 quitLock sync.Mutex // Mutex protecting the quit channel access 74 quitChan chan chan error // Quit channel to stop the metrics collection before closing the database 75 76 log log.Logger // Contextual logger tracking the database path 77 78 activeComp int // Current number of active compactions 79 compStartTime time.Time // The start time of the earliest currently-active compaction 80 compTime atomic.Int64 // Total time spent in compaction in ns 81 level0Comp atomic.Uint32 // Total number of level-zero compactions 82 nonLevel0Comp atomic.Uint32 // Total number of non level-zero compactions 83 writeDelayStartTime time.Time // The start time of the latest write stall 84 writeDelayCount atomic.Int64 // Total number of write stall counts 85 writeDelayTime atomic.Int64 // Total time spent in write stalls 86 } 87 88 func (d *Database) onCompactionBegin(info pebble.CompactionInfo) { 89 if d.activeComp == 0 { 90 d.compStartTime = time.Now() 91 } 92 l0 := info.Input[0] 93 if l0.Level == 0 { 94 d.level0Comp.Add(1) 95 } else { 96 d.nonLevel0Comp.Add(1) 97 } 98 d.activeComp++ 99 } 100 101 func (d *Database) onCompactionEnd(info pebble.CompactionInfo) { 102 if d.activeComp == 1 { 103 d.compTime.Add(int64(time.Since(d.compStartTime))) 104 } else if d.activeComp == 0 { 105 panic("should not happen") 106 } 107 d.activeComp-- 108 } 109 110 func (d *Database) onWriteStallBegin(b pebble.WriteStallBeginInfo) { 111 d.writeDelayStartTime = time.Now() 112 } 113 114 func (d *Database) onWriteStallEnd() { 115 d.writeDelayTime.Add(int64(time.Since(d.writeDelayStartTime))) 116 } 117 118 // New returns a wrapped pebble DB object. The namespace is the prefix that the 119 // metrics reporting should use for surfacing internal stats. 120 func New(file string, cache int, handles int, namespace string, readonly bool) (*Database, error) { 121 // Ensure we have some minimal caching and file guarantees 122 if cache < minCache { 123 cache = minCache 124 } 125 if handles < minHandles { 126 handles = minHandles 127 } 128 logger := log.New("database", file) 129 logger.Info("Allocated cache and file handles", "cache", common.StorageSize(cache*1024*1024), "handles", handles) 130 131 // The max memtable size is limited by the uint32 offsets stored in 132 // internal/arenaskl.node, DeferredBatchOp, and flushableBatchEntry. 133 // Taken from https://github.com/cockroachdb/pebble/blob/master/open.go#L38 134 maxMemTableSize := 4<<30 - 1 // Capped by 4 GB 135 136 // Two memory tables is configured which is identical to leveldb, 137 // including a frozen memory table and another live one. 138 memTableLimit := 2 139 memTableSize := cache * 1024 * 1024 / 2 / memTableLimit 140 if memTableSize > maxMemTableSize { 141 memTableSize = maxMemTableSize 142 } 143 db := &Database{ 144 fn: file, 145 log: logger, 146 quitChan: make(chan chan error), 147 } 148 opt := &pebble.Options{ 149 // Pebble has a single combined cache area and the write 150 // buffers are taken from this too. Assign all available 151 // memory allowance for cache. 152 Cache: pebble.NewCache(int64(cache * 1024 * 1024)), 153 MaxOpenFiles: handles, 154 155 // The size of memory table(as well as the write buffer). 156 // Note, there may have more than two memory tables in the system. 157 MemTableSize: memTableSize, 158 159 // MemTableStopWritesThreshold places a hard limit on the size 160 // of the existent MemTables(including the frozen one). 161 // Note, this must be the number of tables not the size of all memtables 162 // according to https://github.com/cockroachdb/pebble/blob/master/options.go#L738-L742 163 // and to https://github.com/cockroachdb/pebble/blob/master/db.go#L1892-L1903. 164 MemTableStopWritesThreshold: memTableLimit, 165 166 // The default compaction concurrency(1 thread), 167 // Here use all available CPUs for faster compaction. 168 MaxConcurrentCompactions: func() int { return runtime.NumCPU() }, 169 170 // Per-level options. Options for at least one level must be specified. The 171 // options for the last level are used for all subsequent levels. 172 Levels: []pebble.LevelOptions{ 173 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 174 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 175 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 176 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 177 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 178 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 179 {TargetFileSize: 2 * 1024 * 1024, FilterPolicy: bloom.FilterPolicy(10)}, 180 }, 181 ReadOnly: readonly, 182 EventListener: &pebble.EventListener{ 183 CompactionBegin: db.onCompactionBegin, 184 CompactionEnd: db.onCompactionEnd, 185 WriteStallBegin: db.onWriteStallBegin, 186 WriteStallEnd: db.onWriteStallEnd, 187 }, 188 } 189 // Disable seek compaction explicitly. Check https://github.com/ethereum/go-ethereum/pull/20130 190 // for more details. 191 opt.Experimental.ReadSamplingMultiplier = -1 192 193 // Open the db and recover any potential corruptions 194 innerDB, err := pebble.Open(file, opt) 195 if err != nil { 196 return nil, err 197 } 198 db.db = innerDB 199 200 db.compTimeMeter = metrics.NewRegisteredMeter(namespace+"compact/time", nil) 201 db.compReadMeter = metrics.NewRegisteredMeter(namespace+"compact/input", nil) 202 db.compWriteMeter = metrics.NewRegisteredMeter(namespace+"compact/output", nil) 203 db.diskSizeGauge = metrics.NewRegisteredGauge(namespace+"disk/size", nil) 204 db.diskReadMeter = metrics.NewRegisteredMeter(namespace+"disk/read", nil) 205 db.diskWriteMeter = metrics.NewRegisteredMeter(namespace+"disk/write", nil) 206 db.writeDelayMeter = metrics.NewRegisteredMeter(namespace+"compact/writedelay/duration", nil) 207 db.writeDelayNMeter = metrics.NewRegisteredMeter(namespace+"compact/writedelay/counter", nil) 208 db.memCompGauge = metrics.NewRegisteredGauge(namespace+"compact/memory", nil) 209 db.level0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/level0", nil) 210 db.nonlevel0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/nonlevel0", nil) 211 db.seekCompGauge = metrics.NewRegisteredGauge(namespace+"compact/seek", nil) 212 db.manualMemAllocGauge = metrics.NewRegisteredGauge(namespace+"memory/manualalloc", nil) 213 214 // Start up the metrics gathering and return 215 go db.meter(metricsGatheringInterval) 216 return db, nil 217 } 218 219 // Close stops the metrics collection, flushes any pending data to disk and closes 220 // all io accesses to the underlying key-value store. 221 func (d *Database) Close() error { 222 d.quitLock.Lock() 223 defer d.quitLock.Unlock() 224 225 // Allow double closing, simplifies things 226 if d.quitChan == nil { 227 return nil 228 } 229 errc := make(chan error) 230 d.quitChan <- errc 231 if err := <-errc; err != nil { 232 d.log.Error("Metrics collection failed", "err", err) 233 } 234 d.quitChan = nil 235 236 return d.db.Close() 237 } 238 239 // Has retrieves if a key is present in the key-value store. 240 func (d *Database) Has(key []byte) (bool, error) { 241 _, closer, err := d.db.Get(key) 242 if err == pebble.ErrNotFound { 243 return false, nil 244 } else if err != nil { 245 return false, err 246 } 247 closer.Close() 248 return true, nil 249 } 250 251 // Get retrieves the given key if it's present in the key-value store. 252 func (d *Database) Get(key []byte) ([]byte, error) { 253 dat, closer, err := d.db.Get(key) 254 if err != nil { 255 return nil, err 256 } 257 ret := make([]byte, len(dat)) 258 copy(ret, dat) 259 closer.Close() 260 return ret, nil 261 } 262 263 // Put inserts the given value into the key-value store. 264 func (d *Database) Put(key []byte, value []byte) error { 265 return d.db.Set(key, value, pebble.NoSync) 266 } 267 268 // Delete removes the key from the key-value store. 269 func (d *Database) Delete(key []byte) error { 270 return d.db.Delete(key, nil) 271 } 272 273 // NewBatch creates a write-only key-value store that buffers changes to its host 274 // database until a final write is called. 275 func (d *Database) NewBatch() ethdb.Batch { 276 return &batch{ 277 b: d.db.NewBatch(), 278 } 279 } 280 281 // NewBatchWithSize creates a write-only database batch with pre-allocated buffer. 282 // It's not supported by pebble, but pebble has better memory allocation strategy 283 // which turns out a lot faster than leveldb. It's performant enough to construct 284 // batch object without any pre-allocated space. 285 func (d *Database) NewBatchWithSize(_ int) ethdb.Batch { 286 return &batch{ 287 b: d.db.NewBatch(), 288 } 289 } 290 291 // snapshot wraps a pebble snapshot for implementing the Snapshot interface. 292 type snapshot struct { 293 db *pebble.Snapshot 294 } 295 296 // NewSnapshot creates a database snapshot based on the current state. 297 // The created snapshot will not be affected by all following mutations 298 // happened on the database. 299 // Note don't forget to release the snapshot once it's used up, otherwise 300 // the stale data will never be cleaned up by the underlying compactor. 301 func (d *Database) NewSnapshot() (ethdb.Snapshot, error) { 302 snap := d.db.NewSnapshot() 303 return &snapshot{db: snap}, nil 304 } 305 306 // Has retrieves if a key is present in the snapshot backing by a key-value 307 // data store. 308 func (snap *snapshot) Has(key []byte) (bool, error) { 309 _, closer, err := snap.db.Get(key) 310 if err != nil { 311 if err != pebble.ErrNotFound { 312 return false, err 313 } else { 314 return false, nil 315 } 316 } 317 closer.Close() 318 return true, nil 319 } 320 321 // Get retrieves the given key if it's present in the snapshot backing by 322 // key-value data store. 323 func (snap *snapshot) Get(key []byte) ([]byte, error) { 324 dat, closer, err := snap.db.Get(key) 325 if err != nil { 326 return nil, err 327 } 328 ret := make([]byte, len(dat)) 329 copy(ret, dat) 330 closer.Close() 331 return ret, nil 332 } 333 334 // Release releases associated resources. Release should always succeed and can 335 // be called multiple times without causing error. 336 func (snap *snapshot) Release() { 337 snap.db.Close() 338 } 339 340 // upperBound returns the upper bound for the given prefix 341 func upperBound(prefix []byte) (limit []byte) { 342 for i := len(prefix) - 1; i >= 0; i-- { 343 c := prefix[i] 344 if c == 0xff { 345 continue 346 } 347 limit = make([]byte, i+1) 348 copy(limit, prefix) 349 limit[i] = c + 1 350 break 351 } 352 return limit 353 } 354 355 // Stat returns a particular internal stat of the database. 356 func (d *Database) Stat(property string) (string, error) { 357 return "", nil 358 } 359 360 // Compact flattens the underlying data store for the given key range. In essence, 361 // deleted and overwritten versions are discarded, and the data is rearranged to 362 // reduce the cost of operations needed to access them. 363 // 364 // A nil start is treated as a key before all keys in the data store; a nil limit 365 // is treated as a key after all keys in the data store. If both is nil then it 366 // will compact entire data store. 367 func (d *Database) Compact(start []byte, limit []byte) error { 368 // There is no special flag to represent the end of key range 369 // in pebble(nil in leveldb). Use an ugly hack to construct a 370 // large key to represent it. 371 // Note any prefixed database entry will be smaller than this 372 // flag, as for trie nodes we need the 32 byte 0xff because 373 // there might be a shared prefix starting with a number of 374 // 0xff-s, so 32 ensures than only a hash collision could touch it. 375 // https://github.com/cockroachdb/pebble/issues/2359#issuecomment-1443995833 376 if limit == nil { 377 limit = bytes.Repeat([]byte{0xff}, 32) 378 } 379 return d.db.Compact(start, limit, true) // Parallelization is preferred 380 } 381 382 // Path returns the path to the database directory. 383 func (d *Database) Path() string { 384 return d.fn 385 } 386 387 // meter periodically retrieves internal pebble counters and reports them to 388 // the metrics subsystem. 389 func (d *Database) meter(refresh time.Duration) { 390 var errc chan error 391 timer := time.NewTimer(refresh) 392 defer timer.Stop() 393 394 // Create storage and warning log tracer for write delay. 395 var ( 396 compTimes [2]int64 397 writeDelayTimes [2]int64 398 writeDelayCounts [2]int64 399 compWrites [2]int64 400 compReads [2]int64 401 402 nWrites [2]int64 403 ) 404 405 // Iterate ad infinitum and collect the stats 406 for i := 1; errc == nil; i++ { 407 var ( 408 compWrite int64 409 compRead int64 410 nWrite int64 411 412 metrics = d.db.Metrics() 413 compTime = d.compTime.Load() 414 writeDelayCount = d.writeDelayCount.Load() 415 writeDelayTime = d.writeDelayTime.Load() 416 nonLevel0CompCount = int64(d.nonLevel0Comp.Load()) 417 level0CompCount = int64(d.level0Comp.Load()) 418 ) 419 writeDelayTimes[i%2] = writeDelayTime 420 writeDelayCounts[i%2] = writeDelayCount 421 compTimes[i%2] = compTime 422 423 for _, levelMetrics := range metrics.Levels { 424 nWrite += int64(levelMetrics.BytesCompacted) 425 nWrite += int64(levelMetrics.BytesFlushed) 426 compWrite += int64(levelMetrics.BytesCompacted) 427 compRead += int64(levelMetrics.BytesRead) 428 } 429 430 nWrite += int64(metrics.WAL.BytesWritten) 431 432 compWrites[i%2] = compWrite 433 compReads[i%2] = compRead 434 nWrites[i%2] = nWrite 435 436 if d.writeDelayNMeter != nil { 437 d.writeDelayNMeter.Mark(writeDelayCounts[i%2] - writeDelayCounts[(i-1)%2]) 438 } 439 if d.writeDelayMeter != nil { 440 d.writeDelayMeter.Mark(writeDelayTimes[i%2] - writeDelayTimes[(i-1)%2]) 441 } 442 if d.compTimeMeter != nil { 443 d.compTimeMeter.Mark(compTimes[i%2] - compTimes[(i-1)%2]) 444 } 445 if d.compReadMeter != nil { 446 d.compReadMeter.Mark(compReads[i%2] - compReads[(i-1)%2]) 447 } 448 if d.compWriteMeter != nil { 449 d.compWriteMeter.Mark(compWrites[i%2] - compWrites[(i-1)%2]) 450 } 451 if d.diskSizeGauge != nil { 452 d.diskSizeGauge.Update(int64(metrics.DiskSpaceUsage())) 453 } 454 if d.diskReadMeter != nil { 455 d.diskReadMeter.Mark(0) // pebble doesn't track non-compaction reads 456 } 457 if d.diskWriteMeter != nil { 458 d.diskWriteMeter.Mark(nWrites[i%2] - nWrites[(i-1)%2]) 459 } 460 // See https://github.com/cockroachdb/pebble/pull/1628#pullrequestreview-1026664054 461 manuallyAllocated := metrics.BlockCache.Size + int64(metrics.MemTable.Size) + int64(metrics.MemTable.ZombieSize) 462 d.manualMemAllocGauge.Update(manuallyAllocated) 463 d.memCompGauge.Update(metrics.Flush.Count) 464 d.nonlevel0CompGauge.Update(nonLevel0CompCount) 465 d.level0CompGauge.Update(level0CompCount) 466 d.seekCompGauge.Update(metrics.Compact.ReadCount) 467 468 // Sleep a bit, then repeat the stats collection 469 select { 470 case errc = <-d.quitChan: 471 // Quit requesting, stop hammering the database 472 case <-timer.C: 473 timer.Reset(refresh) 474 // Timeout, gather a new set of stats 475 } 476 } 477 errc <- nil 478 } 479 480 // batch is a write-only batch that commits changes to its host database 481 // when Write is called. A batch cannot be used concurrently. 482 type batch struct { 483 b *pebble.Batch 484 size int 485 } 486 487 // Put inserts the given value into the batch for later committing. 488 func (b *batch) Put(key, value []byte) error { 489 b.b.Set(key, value, nil) 490 b.size += len(key) + len(value) 491 return nil 492 } 493 494 // Delete inserts the a key removal into the batch for later committing. 495 func (b *batch) Delete(key []byte) error { 496 b.b.Delete(key, nil) 497 b.size += len(key) 498 return nil 499 } 500 501 // ValueSize retrieves the amount of data queued up for writing. 502 func (b *batch) ValueSize() int { 503 return b.size 504 } 505 506 // Write flushes any accumulated data to disk. 507 func (b *batch) Write() error { 508 return b.b.Commit(pebble.NoSync) 509 } 510 511 // Reset resets the batch for reuse. 512 func (b *batch) Reset() { 513 b.b.Reset() 514 b.size = 0 515 } 516 517 // Replay replays the batch contents. 518 func (b *batch) Replay(w ethdb.KeyValueWriter) error { 519 reader := b.b.Reader() 520 for { 521 kind, k, v, ok := reader.Next() 522 if !ok { 523 break 524 } 525 // The (k,v) slices might be overwritten if the batch is reset/reused, 526 // and the receiver should copy them if they are to be retained long-term. 527 if kind == pebble.InternalKeyKindSet { 528 w.Put(k, v) 529 } else if kind == pebble.InternalKeyKindDelete { 530 w.Delete(k) 531 } else { 532 return fmt.Errorf("unhandled operation, keytype: %v", kind) 533 } 534 } 535 return nil 536 } 537 538 // pebbleIterator is a wrapper of underlying iterator in storage engine. 539 // The purpose of this structure is to implement the missing APIs. 540 type pebbleIterator struct { 541 iter *pebble.Iterator 542 moved bool 543 } 544 545 // NewIterator creates a binary-alphabetical iterator over a subset 546 // of database content with a particular key prefix, starting at a particular 547 // initial key (or after, if it does not exist). 548 func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator { 549 iter := d.db.NewIter(&pebble.IterOptions{ 550 LowerBound: append(prefix, start...), 551 UpperBound: upperBound(prefix), 552 }) 553 iter.First() 554 return &pebbleIterator{iter: iter, moved: true} 555 } 556 557 // Next moves the iterator to the next key/value pair. It returns whether the 558 // iterator is exhausted. 559 func (iter *pebbleIterator) Next() bool { 560 if iter.moved { 561 iter.moved = false 562 return iter.iter.Valid() 563 } 564 return iter.iter.Next() 565 } 566 567 // Error returns any accumulated error. Exhausting all the key/value pairs 568 // is not considered to be an error. 569 func (iter *pebbleIterator) Error() error { 570 return iter.iter.Error() 571 } 572 573 // Key returns the key of the current key/value pair, or nil if done. The caller 574 // should not modify the contents of the returned slice, and its contents may 575 // change on the next call to Next. 576 func (iter *pebbleIterator) Key() []byte { 577 return iter.iter.Key() 578 } 579 580 // Value returns the value of the current key/value pair, or nil if done. The 581 // caller should not modify the contents of the returned slice, and its contents 582 // may change on the next call to Next. 583 func (iter *pebbleIterator) Value() []byte { 584 return iter.iter.Value() 585 } 586 587 // Release releases associated resources. Release should always succeed and can 588 // be called multiple times without causing error. 589 func (iter *pebbleIterator) Release() { iter.iter.Close() }