github.com/hxy-daniel/soda-study@v0.0.0-20210825114314-29b7dcb300f1/SODA_code/go-ethereum/core/rawdb/freezer_table.go (about) 1 // Copyright 2019 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 package rawdb 18 19 import ( 20 "encoding/binary" 21 "errors" 22 "fmt" 23 "io" 24 "os" 25 "path/filepath" 26 "sync" 27 "sync/atomic" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/log" 31 "github.com/ethereum/go-ethereum/metrics" 32 "github.com/golang/snappy" 33 ) 34 35 var ( 36 // errClosed is returned if an operation attempts to read from or write to the 37 // freezer table after it has already been closed. 38 errClosed = errors.New("closed") 39 40 // errOutOfBounds is returned if the item requested is not contained within the 41 // freezer table. 42 errOutOfBounds = errors.New("out of bounds") 43 44 // errNotSupported is returned if the database doesn't support the required operation. 45 errNotSupported = errors.New("this operation is not supported") 46 ) 47 48 // indexEntry contains the number/id of the file that the data resides in, aswell as the 49 // offset within the file to the end of the data 50 // In serialized form, the filenum is stored as uint16. 51 type indexEntry struct { 52 filenum uint32 // stored as uint16 ( 2 bytes) 53 offset uint32 // stored as uint32 ( 4 bytes) 54 } 55 56 const indexEntrySize = 6 57 58 // unmarshallBinary deserializes binary b into the rawIndex entry. 59 func (i *indexEntry) unmarshalBinary(b []byte) error { 60 i.filenum = uint32(binary.BigEndian.Uint16(b[:2])) 61 i.offset = binary.BigEndian.Uint32(b[2:6]) 62 return nil 63 } 64 65 // marshallBinary serializes the rawIndex entry into binary. 66 func (i *indexEntry) marshallBinary() []byte { 67 b := make([]byte, indexEntrySize) 68 binary.BigEndian.PutUint16(b[:2], uint16(i.filenum)) 69 binary.BigEndian.PutUint32(b[2:6], i.offset) 70 return b 71 } 72 73 // freezerTable represents a single chained data table within the freezer (e.g. blocks). 74 // It consists of a data file (snappy encoded arbitrary data blobs) and an indexEntry 75 // file (uncompressed 64 bit indices into the data file). 76 type freezerTable struct { 77 // WARNING: The `items` field is accessed atomically. On 32 bit platforms, only 78 // 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned, 79 // so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG). 80 items uint64 // Number of items stored in the table (including items removed from tail) 81 82 noCompression bool // if true, disables snappy compression. Note: does not work retroactively 83 maxFileSize uint32 // Max file size for data-files 84 name string 85 path string 86 87 head *os.File // File descriptor for the data head of the table 88 files map[uint32]*os.File // open files 89 headId uint32 // number of the currently active head file 90 tailId uint32 // number of the earliest file 91 index *os.File // File descriptor for the indexEntry file of the table 92 93 // In the case that old items are deleted (from the tail), we use itemOffset 94 // to count how many historic items have gone missing. 95 itemOffset uint32 // Offset (number of discarded items) 96 97 headBytes uint32 // Number of bytes written to the head file 98 readMeter metrics.Meter // Meter for measuring the effective amount of data read 99 writeMeter metrics.Meter // Meter for measuring the effective amount of data written 100 sizeCounter metrics.Counter // Counter for tracking the combined size of all freezer tables 101 102 logger log.Logger // Logger with database path and table name ambedded 103 lock sync.RWMutex // Mutex protecting the data file descriptors 104 } 105 106 // newTable opens a freezer table with default settings - 2G files 107 func newTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, disableSnappy bool) (*freezerTable, error) { 108 return newCustomTable(path, name, readMeter, writeMeter, sizeCounter, 2*1000*1000*1000, disableSnappy) 109 } 110 111 // openFreezerFileForAppend opens a freezer table file and seeks to the end 112 func openFreezerFileForAppend(filename string) (*os.File, error) { 113 // Open the file without the O_APPEND flag 114 // because it has differing behaviour during Truncate operations 115 // on different OS's 116 file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644) 117 if err != nil { 118 return nil, err 119 } 120 // Seek to end for append 121 if _, err = file.Seek(0, io.SeekEnd); err != nil { 122 return nil, err 123 } 124 return file, nil 125 } 126 127 // openFreezerFileForReadOnly opens a freezer table file for read only access 128 func openFreezerFileForReadOnly(filename string) (*os.File, error) { 129 return os.OpenFile(filename, os.O_RDONLY, 0644) 130 } 131 132 // openFreezerFileTruncated opens a freezer table making sure it is truncated 133 func openFreezerFileTruncated(filename string) (*os.File, error) { 134 return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) 135 } 136 137 // truncateFreezerFile resizes a freezer table file and seeks to the end 138 func truncateFreezerFile(file *os.File, size int64) error { 139 if err := file.Truncate(size); err != nil { 140 return err 141 } 142 // Seek to end for append 143 if _, err := file.Seek(0, io.SeekEnd); err != nil { 144 return err 145 } 146 return nil 147 } 148 149 // newCustomTable opens a freezer table, creating the data and index files if they are 150 // non existent. Both files are truncated to the shortest common length to ensure 151 // they don't go out of sync. 152 func newCustomTable(path string, name string, readMeter metrics.Meter, writeMeter metrics.Meter, sizeCounter metrics.Counter, maxFilesize uint32, noCompression bool) (*freezerTable, error) { 153 // Ensure the containing directory exists and open the indexEntry file 154 if err := os.MkdirAll(path, 0755); err != nil { 155 return nil, err 156 } 157 var idxName string 158 if noCompression { 159 // Raw idx 160 idxName = fmt.Sprintf("%s.ridx", name) 161 } else { 162 // Compressed idx 163 idxName = fmt.Sprintf("%s.cidx", name) 164 } 165 offsets, err := openFreezerFileForAppend(filepath.Join(path, idxName)) 166 if err != nil { 167 return nil, err 168 } 169 // Create the table and repair any past inconsistency 170 tab := &freezerTable{ 171 index: offsets, 172 files: make(map[uint32]*os.File), 173 readMeter: readMeter, 174 writeMeter: writeMeter, 175 sizeCounter: sizeCounter, 176 name: name, 177 path: path, 178 logger: log.New("database", path, "table", name), 179 noCompression: noCompression, 180 maxFileSize: maxFilesize, 181 } 182 if err := tab.repair(); err != nil { 183 tab.Close() 184 return nil, err 185 } 186 // Initialize the starting size counter 187 size, err := tab.sizeNolock() 188 if err != nil { 189 tab.Close() 190 return nil, err 191 } 192 tab.sizeCounter.Inc(int64(size)) 193 194 return tab, nil 195 } 196 197 // repair cross checks the head and the index file and truncates them to 198 // be in sync with each other after a potential crash / data loss. 199 func (t *freezerTable) repair() error { 200 // Create a temporary offset buffer to init files with and read indexEntry into 201 buffer := make([]byte, indexEntrySize) 202 203 // If we've just created the files, initialize the index with the 0 indexEntry 204 stat, err := t.index.Stat() 205 if err != nil { 206 return err 207 } 208 if stat.Size() == 0 { 209 if _, err := t.index.Write(buffer); err != nil { 210 return err 211 } 212 } 213 // Ensure the index is a multiple of indexEntrySize bytes 214 if overflow := stat.Size() % indexEntrySize; overflow != 0 { 215 truncateFreezerFile(t.index, stat.Size()-overflow) // New file can't trigger this path 216 } 217 // Retrieve the file sizes and prepare for truncation 218 if stat, err = t.index.Stat(); err != nil { 219 return err 220 } 221 offsetsSize := stat.Size() 222 223 // Open the head file 224 var ( 225 firstIndex indexEntry 226 lastIndex indexEntry 227 contentSize int64 228 contentExp int64 229 ) 230 // Read index zero, determine what file is the earliest 231 // and what item offset to use 232 t.index.ReadAt(buffer, 0) 233 firstIndex.unmarshalBinary(buffer) 234 235 t.tailId = firstIndex.offset 236 t.itemOffset = firstIndex.filenum 237 238 t.index.ReadAt(buffer, offsetsSize-indexEntrySize) 239 lastIndex.unmarshalBinary(buffer) 240 t.head, err = t.openFile(lastIndex.filenum, openFreezerFileForAppend) 241 if err != nil { 242 return err 243 } 244 if stat, err = t.head.Stat(); err != nil { 245 return err 246 } 247 contentSize = stat.Size() 248 249 // Keep truncating both files until they come in sync 250 contentExp = int64(lastIndex.offset) 251 252 for contentExp != contentSize { 253 // Truncate the head file to the last offset pointer 254 if contentExp < contentSize { 255 t.logger.Warn("Truncating dangling head", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize)) 256 if err := truncateFreezerFile(t.head, contentExp); err != nil { 257 return err 258 } 259 contentSize = contentExp 260 } 261 // Truncate the index to point within the head file 262 if contentExp > contentSize { 263 t.logger.Warn("Truncating dangling indexes", "indexed", common.StorageSize(contentExp), "stored", common.StorageSize(contentSize)) 264 if err := truncateFreezerFile(t.index, offsetsSize-indexEntrySize); err != nil { 265 return err 266 } 267 offsetsSize -= indexEntrySize 268 t.index.ReadAt(buffer, offsetsSize-indexEntrySize) 269 var newLastIndex indexEntry 270 newLastIndex.unmarshalBinary(buffer) 271 // We might have slipped back into an earlier head-file here 272 if newLastIndex.filenum != lastIndex.filenum { 273 // Release earlier opened file 274 t.releaseFile(lastIndex.filenum) 275 t.head, err = t.openFile(newLastIndex.filenum, openFreezerFileForAppend) 276 if stat, err = t.head.Stat(); err != nil { 277 // TODO, anything more we can do here? 278 // A data file has gone missing... 279 return err 280 } 281 contentSize = stat.Size() 282 } 283 lastIndex = newLastIndex 284 contentExp = int64(lastIndex.offset) 285 } 286 } 287 // Ensure all reparation changes have been written to disk 288 if err := t.index.Sync(); err != nil { 289 return err 290 } 291 if err := t.head.Sync(); err != nil { 292 return err 293 } 294 // Update the item and byte counters and return 295 t.items = uint64(t.itemOffset) + uint64(offsetsSize/indexEntrySize-1) // last indexEntry points to the end of the data file 296 t.headBytes = uint32(contentSize) 297 t.headId = lastIndex.filenum 298 299 // Close opened files and preopen all files 300 if err := t.preopen(); err != nil { 301 return err 302 } 303 t.logger.Debug("Chain freezer table opened", "items", t.items, "size", common.StorageSize(t.headBytes)) 304 return nil 305 } 306 307 // preopen opens all files that the freezer will need. This method should be called from an init-context, 308 // since it assumes that it doesn't have to bother with locking 309 // The rationale for doing preopen is to not have to do it from within Retrieve, thus not needing to ever 310 // obtain a write-lock within Retrieve. 311 func (t *freezerTable) preopen() (err error) { 312 // The repair might have already opened (some) files 313 t.releaseFilesAfter(0, false) 314 // Open all except head in RDONLY 315 for i := t.tailId; i < t.headId; i++ { 316 if _, err = t.openFile(i, openFreezerFileForReadOnly); err != nil { 317 return err 318 } 319 } 320 // Open head in read/write 321 t.head, err = t.openFile(t.headId, openFreezerFileForAppend) 322 return err 323 } 324 325 // truncate discards any recent data above the provided threshold number. 326 func (t *freezerTable) truncate(items uint64) error { 327 t.lock.Lock() 328 defer t.lock.Unlock() 329 330 // If our item count is correct, don't do anything 331 if atomic.LoadUint64(&t.items) <= items { 332 return nil 333 } 334 // We need to truncate, save the old size for metrics tracking 335 oldSize, err := t.sizeNolock() 336 if err != nil { 337 return err 338 } 339 // Something's out of sync, truncate the table's offset index 340 t.logger.Warn("Truncating freezer table", "items", t.items, "limit", items) 341 if err := truncateFreezerFile(t.index, int64(items+1)*indexEntrySize); err != nil { 342 return err 343 } 344 // Calculate the new expected size of the data file and truncate it 345 buffer := make([]byte, indexEntrySize) 346 if _, err := t.index.ReadAt(buffer, int64(items*indexEntrySize)); err != nil { 347 return err 348 } 349 var expected indexEntry 350 expected.unmarshalBinary(buffer) 351 352 // We might need to truncate back to older files 353 if expected.filenum != t.headId { 354 // If already open for reading, force-reopen for writing 355 t.releaseFile(expected.filenum) 356 newHead, err := t.openFile(expected.filenum, openFreezerFileForAppend) 357 if err != nil { 358 return err 359 } 360 // Release any files _after the current head -- both the previous head 361 // and any files which may have been opened for reading 362 t.releaseFilesAfter(expected.filenum, true) 363 // Set back the historic head 364 t.head = newHead 365 atomic.StoreUint32(&t.headId, expected.filenum) 366 } 367 if err := truncateFreezerFile(t.head, int64(expected.offset)); err != nil { 368 return err 369 } 370 // All data files truncated, set internal counters and return 371 atomic.StoreUint64(&t.items, items) 372 atomic.StoreUint32(&t.headBytes, expected.offset) 373 374 // Retrieve the new size and update the total size counter 375 newSize, err := t.sizeNolock() 376 if err != nil { 377 return err 378 } 379 t.sizeCounter.Dec(int64(oldSize - newSize)) 380 381 return nil 382 } 383 384 // Close closes all opened files. 385 func (t *freezerTable) Close() error { 386 t.lock.Lock() 387 defer t.lock.Unlock() 388 389 var errs []error 390 if err := t.index.Close(); err != nil { 391 errs = append(errs, err) 392 } 393 t.index = nil 394 395 for _, f := range t.files { 396 if err := f.Close(); err != nil { 397 errs = append(errs, err) 398 } 399 } 400 t.head = nil 401 402 if errs != nil { 403 return fmt.Errorf("%v", errs) 404 } 405 return nil 406 } 407 408 // openFile assumes that the write-lock is held by the caller 409 func (t *freezerTable) openFile(num uint32, opener func(string) (*os.File, error)) (f *os.File, err error) { 410 var exist bool 411 if f, exist = t.files[num]; !exist { 412 var name string 413 if t.noCompression { 414 name = fmt.Sprintf("%s.%04d.rdat", t.name, num) 415 } else { 416 name = fmt.Sprintf("%s.%04d.cdat", t.name, num) 417 } 418 f, err = opener(filepath.Join(t.path, name)) 419 if err != nil { 420 return nil, err 421 } 422 t.files[num] = f 423 } 424 return f, err 425 } 426 427 // releaseFile closes a file, and removes it from the open file cache. 428 // Assumes that the caller holds the write lock 429 func (t *freezerTable) releaseFile(num uint32) { 430 if f, exist := t.files[num]; exist { 431 delete(t.files, num) 432 f.Close() 433 } 434 } 435 436 // releaseFilesAfter closes all open files with a higher number, and optionally also deletes the files 437 func (t *freezerTable) releaseFilesAfter(num uint32, remove bool) { 438 for fnum, f := range t.files { 439 if fnum > num { 440 delete(t.files, fnum) 441 f.Close() 442 if remove { 443 os.Remove(f.Name()) 444 } 445 } 446 } 447 } 448 449 // Append injects a binary blob at the end of the freezer table. The item number 450 // is a precautionary parameter to ensure data correctness, but the table will 451 // reject already existing data. 452 // 453 // Note, this method will *not* flush any data to disk so be sure to explicitly 454 // fsync before irreversibly deleting data from the database. 455 func (t *freezerTable) Append(item uint64, blob []byte) error { 456 // Read lock prevents competition with truncate 457 t.lock.RLock() 458 // Ensure the table is still accessible 459 if t.index == nil || t.head == nil { 460 t.lock.RUnlock() 461 return errClosed 462 } 463 // Ensure only the next item can be written, nothing else 464 if atomic.LoadUint64(&t.items) != item { 465 t.lock.RUnlock() 466 return fmt.Errorf("appending unexpected item: want %d, have %d", t.items, item) 467 } 468 // Encode the blob and write it into the data file 469 if !t.noCompression { 470 blob = snappy.Encode(nil, blob) 471 } 472 bLen := uint32(len(blob)) 473 if t.headBytes+bLen < bLen || 474 t.headBytes+bLen > t.maxFileSize { 475 // we need a new file, writing would overflow 476 t.lock.RUnlock() 477 t.lock.Lock() 478 nextID := atomic.LoadUint32(&t.headId) + 1 479 // We open the next file in truncated mode -- if this file already 480 // exists, we need to start over from scratch on it 481 newHead, err := t.openFile(nextID, openFreezerFileTruncated) 482 if err != nil { 483 t.lock.Unlock() 484 return err 485 } 486 // Close old file, and reopen in RDONLY mode 487 t.releaseFile(t.headId) 488 t.openFile(t.headId, openFreezerFileForReadOnly) 489 490 // Swap out the current head 491 t.head = newHead 492 atomic.StoreUint32(&t.headBytes, 0) 493 atomic.StoreUint32(&t.headId, nextID) 494 t.lock.Unlock() 495 t.lock.RLock() 496 } 497 498 defer t.lock.RUnlock() 499 if _, err := t.head.Write(blob); err != nil { 500 return err 501 } 502 newOffset := atomic.AddUint32(&t.headBytes, bLen) 503 idx := indexEntry{ 504 filenum: atomic.LoadUint32(&t.headId), 505 offset: newOffset, 506 } 507 // Write indexEntry 508 t.index.Write(idx.marshallBinary()) 509 510 t.writeMeter.Mark(int64(bLen + indexEntrySize)) 511 t.sizeCounter.Inc(int64(bLen + indexEntrySize)) 512 513 atomic.AddUint64(&t.items, 1) 514 return nil 515 } 516 517 // getBounds returns the indexes for the item 518 // returns start, end, filenumber and error 519 func (t *freezerTable) getBounds(item uint64) (uint32, uint32, uint32, error) { 520 var startIdx, endIdx indexEntry 521 buffer := make([]byte, indexEntrySize) 522 if _, err := t.index.ReadAt(buffer, int64(item*indexEntrySize)); err != nil { 523 return 0, 0, 0, err 524 } 525 startIdx.unmarshalBinary(buffer) 526 if _, err := t.index.ReadAt(buffer, int64((item+1)*indexEntrySize)); err != nil { 527 return 0, 0, 0, err 528 } 529 endIdx.unmarshalBinary(buffer) 530 if startIdx.filenum != endIdx.filenum { 531 // If a piece of data 'crosses' a data-file, 532 // it's actually in one piece on the second data-file. 533 // We return a zero-indexEntry for the second file as start 534 return 0, endIdx.offset, endIdx.filenum, nil 535 } 536 return startIdx.offset, endIdx.offset, endIdx.filenum, nil 537 } 538 539 // Retrieve looks up the data offset of an item with the given number and retrieves 540 // the raw binary blob from the data file. 541 func (t *freezerTable) Retrieve(item uint64) ([]byte, error) { 542 // Ensure the table and the item is accessible 543 if t.index == nil || t.head == nil { 544 return nil, errClosed 545 } 546 if atomic.LoadUint64(&t.items) <= item { 547 return nil, errOutOfBounds 548 } 549 // Ensure the item was not deleted from the tail either 550 offset := atomic.LoadUint32(&t.itemOffset) 551 if uint64(offset) > item { 552 return nil, errOutOfBounds 553 } 554 t.lock.RLock() 555 startOffset, endOffset, filenum, err := t.getBounds(item - uint64(offset)) 556 if err != nil { 557 t.lock.RUnlock() 558 return nil, err 559 } 560 dataFile, exist := t.files[filenum] 561 if !exist { 562 t.lock.RUnlock() 563 return nil, fmt.Errorf("missing data file %d", filenum) 564 } 565 // Retrieve the data itself, decompress and return 566 blob := make([]byte, endOffset-startOffset) 567 if _, err := dataFile.ReadAt(blob, int64(startOffset)); err != nil { 568 t.lock.RUnlock() 569 return nil, err 570 } 571 t.lock.RUnlock() 572 t.readMeter.Mark(int64(len(blob) + 2*indexEntrySize)) 573 574 if t.noCompression { 575 return blob, nil 576 } 577 return snappy.Decode(nil, blob) 578 } 579 580 // has returns an indicator whether the specified number data 581 // exists in the freezer table. 582 func (t *freezerTable) has(number uint64) bool { 583 return atomic.LoadUint64(&t.items) > number 584 } 585 586 // size returns the total data size in the freezer table. 587 func (t *freezerTable) size() (uint64, error) { 588 t.lock.RLock() 589 defer t.lock.RUnlock() 590 591 return t.sizeNolock() 592 } 593 594 // sizeNolock returns the total data size in the freezer table without obtaining 595 // the mutex first. 596 func (t *freezerTable) sizeNolock() (uint64, error) { 597 stat, err := t.index.Stat() 598 if err != nil { 599 return 0, err 600 } 601 total := uint64(t.maxFileSize)*uint64(t.headId-t.tailId) + uint64(t.headBytes) + uint64(stat.Size()) 602 return total, nil 603 } 604 605 // Sync pushes any pending data from memory out to disk. This is an expensive 606 // operation, so use it with care. 607 func (t *freezerTable) Sync() error { 608 if err := t.index.Sync(); err != nil { 609 return err 610 } 611 return t.head.Sync() 612 } 613 614 // printIndex is a debug print utility function for testing 615 func (t *freezerTable) printIndex() { 616 buf := make([]byte, indexEntrySize) 617 618 fmt.Printf("|-----------------|\n") 619 fmt.Printf("| fileno | offset |\n") 620 fmt.Printf("|--------+--------|\n") 621 622 for i := uint64(0); ; i++ { 623 if _, err := t.index.ReadAt(buf, int64(i*indexEntrySize)); err != nil { 624 break 625 } 626 var entry indexEntry 627 entry.unmarshalBinary(buf) 628 fmt.Printf("| %03d | %03d | \n", entry.filenum, entry.offset) 629 if i > 100 { 630 fmt.Printf(" ... \n") 631 break 632 } 633 } 634 fmt.Printf("|-----------------|\n") 635 }