github.com/ubiq/go-ubiq/v6@v6.0.0/core/rawdb/freezer.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 "errors" 21 "fmt" 22 "math" 23 "os" 24 "path/filepath" 25 "sync" 26 "sync/atomic" 27 "time" 28 29 "github.com/prometheus/tsdb/fileutil" 30 "github.com/ubiq/go-ubiq/v6/common" 31 "github.com/ubiq/go-ubiq/v6/ethdb" 32 "github.com/ubiq/go-ubiq/v6/log" 33 "github.com/ubiq/go-ubiq/v6/metrics" 34 "github.com/ubiq/go-ubiq/v6/params" 35 ) 36 37 var ( 38 // errReadOnly is returned if the freezer is opened in read only mode. All the 39 // mutations are disallowed. 40 errReadOnly = errors.New("read only") 41 42 // errUnknownTable is returned if the user attempts to read from a table that is 43 // not tracked by the freezer. 44 errUnknownTable = errors.New("unknown table") 45 46 // errOutOrderInsertion is returned if the user attempts to inject out-of-order 47 // binary blobs into the freezer. 48 errOutOrderInsertion = errors.New("the append operation is out-order") 49 50 // errSymlinkDatadir is returned if the ancient directory specified by user 51 // is a symbolic link. 52 errSymlinkDatadir = errors.New("symbolic link datadir is not supported") 53 ) 54 55 const ( 56 // freezerRecheckInterval is the frequency to check the key-value database for 57 // chain progression that might permit new blocks to be frozen into immutable 58 // storage. 59 freezerRecheckInterval = time.Minute * 8 60 61 // freezerBatchLimit is the maximum number of blocks to freeze in one batch 62 // before doing an fsync and deleting it from the key-value store. 63 freezerBatchLimit = 30000 64 65 // freezerTableSize defines the maximum size of freezer data files. 66 freezerTableSize = 2 * 1000 * 1000 * 1000 67 ) 68 69 // freezer is an memory mapped append-only database to store immutable chain data 70 // into flat files: 71 // 72 // - The append only nature ensures that disk writes are minimized. 73 // - The memory mapping ensures we can max out system memory for caching without 74 // reserving it for go-ethereum. This would also reduce the memory requirements 75 // of Geth, and thus also GC overhead. 76 type freezer struct { 77 // WARNING: The `frozen` 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 frozen uint64 // Number of blocks already frozen 81 threshold uint64 // Number of recent blocks not to freeze (params.FullImmutabilityThreshold apart from tests) 82 83 // This lock synchronizes writers and the truncate operation, as well as 84 // the "atomic" (batched) read operations. 85 writeLock sync.RWMutex 86 writeBatch *freezerBatch 87 88 readonly bool 89 tables map[string]*freezerTable // Data tables for storing everything 90 instanceLock fileutil.Releaser // File-system lock to prevent double opens 91 92 trigger chan chan struct{} // Manual blocking freeze trigger, test determinism 93 94 quit chan struct{} 95 wg sync.WaitGroup 96 closeOnce sync.Once 97 } 98 99 // newFreezer creates a chain freezer that moves ancient chain data into 100 // append-only flat file containers. 101 // 102 // The 'tables' argument defines the data tables. If the value of a map 103 // entry is true, snappy compression is disabled for the table. 104 func newFreezer(datadir string, namespace string, readonly bool, maxTableSize uint32, tables map[string]bool) (*freezer, error) { 105 // Create the initial freezer object 106 var ( 107 readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil) 108 writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil) 109 sizeGauge = metrics.NewRegisteredGauge(namespace+"ancient/size", nil) 110 ) 111 // Ensure the datadir is not a symbolic link if it exists. 112 if info, err := os.Lstat(datadir); !os.IsNotExist(err) { 113 if info.Mode()&os.ModeSymlink != 0 { 114 log.Warn("Symbolic link ancient database is not supported", "path", datadir) 115 return nil, errSymlinkDatadir 116 } 117 } 118 // Leveldb uses LOCK as the filelock filename. To prevent the 119 // name collision, we use FLOCK as the lock name. 120 lock, _, err := fileutil.Flock(filepath.Join(datadir, "FLOCK")) 121 if err != nil { 122 return nil, err 123 } 124 // Open all the supported data tables 125 freezer := &freezer{ 126 readonly: readonly, 127 threshold: params.FullImmutabilityThreshold, 128 tables: make(map[string]*freezerTable), 129 instanceLock: lock, 130 trigger: make(chan chan struct{}), 131 quit: make(chan struct{}), 132 } 133 134 // Create the tables. 135 for name, disableSnappy := range tables { 136 table, err := newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy) 137 if err != nil { 138 for _, table := range freezer.tables { 139 table.Close() 140 } 141 lock.Release() 142 return nil, err 143 } 144 freezer.tables[name] = table 145 } 146 147 // Truncate all tables to common length. 148 if err := freezer.repair(); err != nil { 149 for _, table := range freezer.tables { 150 table.Close() 151 } 152 lock.Release() 153 return nil, err 154 } 155 156 // Create the write batch. 157 freezer.writeBatch = newFreezerBatch(freezer) 158 159 log.Info("Opened ancient database", "database", datadir, "readonly", readonly) 160 return freezer, nil 161 } 162 163 // Close terminates the chain freezer, unmapping all the data files. 164 func (f *freezer) Close() error { 165 f.writeLock.Lock() 166 defer f.writeLock.Unlock() 167 168 var errs []error 169 f.closeOnce.Do(func() { 170 close(f.quit) 171 // Wait for any background freezing to stop 172 f.wg.Wait() 173 for _, table := range f.tables { 174 if err := table.Close(); err != nil { 175 errs = append(errs, err) 176 } 177 } 178 if err := f.instanceLock.Release(); err != nil { 179 errs = append(errs, err) 180 } 181 }) 182 if errs != nil { 183 return fmt.Errorf("%v", errs) 184 } 185 return nil 186 } 187 188 // HasAncient returns an indicator whether the specified ancient data exists 189 // in the freezer. 190 func (f *freezer) HasAncient(kind string, number uint64) (bool, error) { 191 if table := f.tables[kind]; table != nil { 192 return table.has(number), nil 193 } 194 return false, nil 195 } 196 197 // Ancient retrieves an ancient binary blob from the append-only immutable files. 198 func (f *freezer) Ancient(kind string, number uint64) ([]byte, error) { 199 if table := f.tables[kind]; table != nil { 200 return table.Retrieve(number) 201 } 202 return nil, errUnknownTable 203 } 204 205 // AncientRange retrieves multiple items in sequence, starting from the index 'start'. 206 // It will return 207 // - at most 'max' items, 208 // - at least 1 item (even if exceeding the maxByteSize), but will otherwise 209 // return as many items as fit into maxByteSize. 210 func (f *freezer) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) { 211 if table := f.tables[kind]; table != nil { 212 return table.RetrieveItems(start, count, maxBytes) 213 } 214 return nil, errUnknownTable 215 } 216 217 // Ancients returns the length of the frozen items. 218 func (f *freezer) Ancients() (uint64, error) { 219 return atomic.LoadUint64(&f.frozen), nil 220 } 221 222 // AncientSize returns the ancient size of the specified category. 223 func (f *freezer) AncientSize(kind string) (uint64, error) { 224 // This needs the write lock to avoid data races on table fields. 225 // Speed doesn't matter here, AncientSize is for debugging. 226 f.writeLock.RLock() 227 defer f.writeLock.RUnlock() 228 229 if table := f.tables[kind]; table != nil { 230 return table.size() 231 } 232 return 0, errUnknownTable 233 } 234 235 // ReadAncients runs the given read operation while ensuring that no writes take place 236 // on the underlying freezer. 237 func (f *freezer) ReadAncients(fn func(ethdb.AncientReader) error) (err error) { 238 f.writeLock.RLock() 239 defer f.writeLock.RUnlock() 240 return fn(f) 241 } 242 243 // ModifyAncients runs the given write operation. 244 func (f *freezer) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (writeSize int64, err error) { 245 if f.readonly { 246 return 0, errReadOnly 247 } 248 f.writeLock.Lock() 249 defer f.writeLock.Unlock() 250 251 // Roll back all tables to the starting position in case of error. 252 prevItem := f.frozen 253 defer func() { 254 if err != nil { 255 // The write operation has failed. Go back to the previous item position. 256 for name, table := range f.tables { 257 err := table.truncate(prevItem) 258 if err != nil { 259 log.Error("Freezer table roll-back failed", "table", name, "index", prevItem, "err", err) 260 } 261 } 262 } 263 }() 264 265 f.writeBatch.reset() 266 if err := fn(f.writeBatch); err != nil { 267 return 0, err 268 } 269 item, writeSize, err := f.writeBatch.commit() 270 if err != nil { 271 return 0, err 272 } 273 atomic.StoreUint64(&f.frozen, item) 274 return writeSize, nil 275 } 276 277 // TruncateAncients discards any recent data above the provided threshold number. 278 func (f *freezer) TruncateAncients(items uint64) error { 279 if f.readonly { 280 return errReadOnly 281 } 282 f.writeLock.Lock() 283 defer f.writeLock.Unlock() 284 285 if atomic.LoadUint64(&f.frozen) <= items { 286 return nil 287 } 288 for _, table := range f.tables { 289 if err := table.truncate(items); err != nil { 290 return err 291 } 292 } 293 atomic.StoreUint64(&f.frozen, items) 294 return nil 295 } 296 297 // Sync flushes all data tables to disk. 298 func (f *freezer) Sync() error { 299 var errs []error 300 for _, table := range f.tables { 301 if err := table.Sync(); err != nil { 302 errs = append(errs, err) 303 } 304 } 305 if errs != nil { 306 return fmt.Errorf("%v", errs) 307 } 308 return nil 309 } 310 311 // repair truncates all data tables to the same length. 312 func (f *freezer) repair() error { 313 min := uint64(math.MaxUint64) 314 for _, table := range f.tables { 315 items := atomic.LoadUint64(&table.items) 316 if min > items { 317 min = items 318 } 319 } 320 for _, table := range f.tables { 321 if err := table.truncate(min); err != nil { 322 return err 323 } 324 } 325 atomic.StoreUint64(&f.frozen, min) 326 return nil 327 } 328 329 // freeze is a background thread that periodically checks the blockchain for any 330 // import progress and moves ancient data from the fast database into the freezer. 331 // 332 // This functionality is deliberately broken off from block importing to avoid 333 // incurring additional data shuffling delays on block propagation. 334 func (f *freezer) freeze(db ethdb.KeyValueStore) { 335 nfdb := &nofreezedb{KeyValueStore: db} 336 337 var ( 338 backoff bool 339 triggered chan struct{} // Used in tests 340 ) 341 for { 342 select { 343 case <-f.quit: 344 log.Info("Freezer shutting down") 345 return 346 default: 347 } 348 if backoff { 349 // If we were doing a manual trigger, notify it 350 if triggered != nil { 351 triggered <- struct{}{} 352 triggered = nil 353 } 354 select { 355 case <-time.NewTimer(freezerRecheckInterval).C: 356 backoff = false 357 case triggered = <-f.trigger: 358 backoff = false 359 case <-f.quit: 360 return 361 } 362 } 363 // Retrieve the freezing threshold. 364 hash := ReadHeadBlockHash(nfdb) 365 if hash == (common.Hash{}) { 366 log.Debug("Current full block hash unavailable") // new chain, empty database 367 backoff = true 368 continue 369 } 370 number := ReadHeaderNumber(nfdb, hash) 371 threshold := atomic.LoadUint64(&f.threshold) 372 373 switch { 374 case number == nil: 375 log.Error("Current full block number unavailable", "hash", hash) 376 backoff = true 377 continue 378 379 case *number < threshold: 380 log.Debug("Current full block not old enough", "number", *number, "hash", hash, "delay", threshold) 381 backoff = true 382 continue 383 384 case *number-threshold <= f.frozen: 385 log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", f.frozen) 386 backoff = true 387 continue 388 } 389 head := ReadHeader(nfdb, hash, *number) 390 if head == nil { 391 log.Error("Current full block unavailable", "number", *number, "hash", hash) 392 backoff = true 393 continue 394 } 395 396 // Seems we have data ready to be frozen, process in usable batches 397 var ( 398 start = time.Now() 399 first, _ = f.Ancients() 400 limit = *number - threshold 401 ) 402 if limit-first > freezerBatchLimit { 403 limit = first + freezerBatchLimit 404 } 405 ancients, err := f.freezeRange(nfdb, first, limit) 406 if err != nil { 407 log.Error("Error in block freeze operation", "err", err) 408 backoff = true 409 continue 410 } 411 412 // Batch of blocks have been frozen, flush them before wiping from leveldb 413 if err := f.Sync(); err != nil { 414 log.Crit("Failed to flush frozen tables", "err", err) 415 } 416 417 // Wipe out all data from the active database 418 batch := db.NewBatch() 419 for i := 0; i < len(ancients); i++ { 420 // Always keep the genesis block in active database 421 if first+uint64(i) != 0 { 422 DeleteBlockWithoutNumber(batch, ancients[i], first+uint64(i)) 423 DeleteCanonicalHash(batch, first+uint64(i)) 424 } 425 } 426 if err := batch.Write(); err != nil { 427 log.Crit("Failed to delete frozen canonical blocks", "err", err) 428 } 429 batch.Reset() 430 431 // Wipe out side chains also and track dangling side chains 432 var dangling []common.Hash 433 for number := first; number < f.frozen; number++ { 434 // Always keep the genesis block in active database 435 if number != 0 { 436 dangling = ReadAllHashes(db, number) 437 for _, hash := range dangling { 438 log.Trace("Deleting side chain", "number", number, "hash", hash) 439 DeleteBlock(batch, hash, number) 440 } 441 } 442 } 443 if err := batch.Write(); err != nil { 444 log.Crit("Failed to delete frozen side blocks", "err", err) 445 } 446 batch.Reset() 447 448 // Step into the future and delete and dangling side chains 449 if f.frozen > 0 { 450 tip := f.frozen 451 for len(dangling) > 0 { 452 drop := make(map[common.Hash]struct{}) 453 for _, hash := range dangling { 454 log.Debug("Dangling parent from freezer", "number", tip-1, "hash", hash) 455 drop[hash] = struct{}{} 456 } 457 children := ReadAllHashes(db, tip) 458 for i := 0; i < len(children); i++ { 459 // Dig up the child and ensure it's dangling 460 child := ReadHeader(nfdb, children[i], tip) 461 if child == nil { 462 log.Error("Missing dangling header", "number", tip, "hash", children[i]) 463 continue 464 } 465 if _, ok := drop[child.ParentHash]; !ok { 466 children = append(children[:i], children[i+1:]...) 467 i-- 468 continue 469 } 470 // Delete all block data associated with the child 471 log.Debug("Deleting dangling block", "number", tip, "hash", children[i], "parent", child.ParentHash) 472 DeleteBlock(batch, children[i], tip) 473 } 474 dangling = children 475 tip++ 476 } 477 if err := batch.Write(); err != nil { 478 log.Crit("Failed to delete dangling side blocks", "err", err) 479 } 480 } 481 482 // Log something friendly for the user 483 context := []interface{}{ 484 "blocks", f.frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", f.frozen - 1, 485 } 486 if n := len(ancients); n > 0 { 487 context = append(context, []interface{}{"hash", ancients[n-1]}...) 488 } 489 log.Info("Deep froze chain segment", context...) 490 491 // Avoid database thrashing with tiny writes 492 if f.frozen-first < freezerBatchLimit { 493 backoff = true 494 } 495 } 496 } 497 498 func (f *freezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hashes []common.Hash, err error) { 499 hashes = make([]common.Hash, 0, limit-number) 500 501 _, err = f.ModifyAncients(func(op ethdb.AncientWriteOp) error { 502 for ; number <= limit; number++ { 503 // Retrieve all the components of the canonical block. 504 hash := ReadCanonicalHash(nfdb, number) 505 if hash == (common.Hash{}) { 506 return fmt.Errorf("canonical hash missing, can't freeze block %d", number) 507 } 508 header := ReadHeaderRLP(nfdb, hash, number) 509 if len(header) == 0 { 510 return fmt.Errorf("block header missing, can't freeze block %d", number) 511 } 512 body := ReadBodyRLP(nfdb, hash, number) 513 if len(body) == 0 { 514 return fmt.Errorf("block body missing, can't freeze block %d", number) 515 } 516 receipts := ReadReceiptsRLP(nfdb, hash, number) 517 if len(receipts) == 0 { 518 return fmt.Errorf("block receipts missing, can't freeze block %d", number) 519 } 520 td := ReadTdRLP(nfdb, hash, number) 521 if len(td) == 0 { 522 return fmt.Errorf("total difficulty missing, can't freeze block %d", number) 523 } 524 525 // Write to the batch. 526 if err := op.AppendRaw(freezerHashTable, number, hash[:]); err != nil { 527 return fmt.Errorf("can't write hash to freezer: %v", err) 528 } 529 if err := op.AppendRaw(freezerHeaderTable, number, header); err != nil { 530 return fmt.Errorf("can't write header to freezer: %v", err) 531 } 532 if err := op.AppendRaw(freezerBodiesTable, number, body); err != nil { 533 return fmt.Errorf("can't write body to freezer: %v", err) 534 } 535 if err := op.AppendRaw(freezerReceiptTable, number, receipts); err != nil { 536 return fmt.Errorf("can't write receipts to freezer: %v", err) 537 } 538 if err := op.AppendRaw(freezerDifficultyTable, number, td); err != nil { 539 return fmt.Errorf("can't write td to freezer: %v", err) 540 } 541 542 hashes = append(hashes, hash) 543 } 544 return nil 545 }) 546 547 return hashes, err 548 }