From f401199b678dcb4e2a5a504d9b53f6281af2b56e Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 31 May 2021 15:09:30 +0100 Subject: [PATCH 01/21] core/rawdb, eth, les: implemented updating the unclean shutdown marker every 5 minutes. Fixes #22580 --- core/rawdb/accessors_metadata.go | 41 ++++++++++++++++++++++++++++++-- eth/backend.go | 12 +++++++++- les/client.go | 12 +++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 079e335fa6fb..76df39863da7 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -121,8 +121,11 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) return previous, discarded, nil } -// PopUncleanShutdownMarker removes the last unclean shutdown marker -func PopUncleanShutdownMarker(db ethdb.KeyValueStore) { +// PopUncleanShutdownMarker removes the last unclean shutdown marker and stops +// the function updating the marker (UpdateUncleanShutdownMarker) +func PopUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpdateCh chan bool) { + // stop the unclean shutdown marker update first + stopUncleanShutdownUpdateCh <- true var uncleanShutdowns crashList // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { @@ -138,3 +141,37 @@ func PopUncleanShutdownMarker(db ethdb.KeyValueStore) { log.Warn("Failed to clear unclean-shutdown marker", "err", err) } } + +// UpdateUncleanShutdownMarker updates the current unclean shutdown marker +// every 5 minutes +func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpdateCh chan bool) { + var uncleanShutdowns crashList + // Read old data + if previous, err := db.Get(uncleanShutdownKey); err != nil { + log.Warn("Error reading unclean shutdown markers", "error", err) + } else if err := rlp.DecodeBytes(previous, &uncleanShutdowns); err != nil { + log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen + } + l := len(uncleanShutdowns.Recent) + // uncleanShutdowns.Recent should not be empty in this context, + // unless PushUncleanShutdownMarker fails. + if l == 0 { + log.Warn("recent unclean shutdown markers is empty. Stopping marker update") + return + } + // update marker every five minutes + ticker := time.NewTicker(300 * time.Second) + defer func() { ticker.Stop() }() + for { + select { + case <-ticker.C: + uncleanShutdowns.Recent[l] = uint64(time.Now().Unix()) + data, _ := rlp.EncodeToBytes(uncleanShutdowns) + if err := db.Put(uncleanShutdownKey, data); err != nil { + log.Warn("Failed to update unclean-shutdown marker", "err", err) + } + case <-stopUncleanShutdownUpdateCh: + return + } + } +} diff --git a/eth/backend.go b/eth/backend.go index 3e770fe83d92..d26258b64c5a 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -97,6 +97,12 @@ type Ethereum struct { lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) } +// I know this shouldn't be a global, but I'm +// not sure where the appropriate place would be to put +// the chan, maybe in handler in the Ethereum struct, but +// that doesn't seem appropriate to me? +var stopUncleanShutdownUpdateCh chan bool = make(chan bool) + // New creates a new Ethereum object (including the // initialisation of the common Ethereum object) func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { @@ -271,6 +277,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { "age", common.PrettyAge(t)) } } + // starts a goroutine to update the unclean shutdown marker + // every five minutes, so the user can get an approximate time of + // the previous crash upon startup + go rawdb.UpdateUncleanShutdownMarker(chainDb, stopUncleanShutdownUpdateCh) return eth, nil } @@ -559,7 +569,7 @@ func (s *Ethereum) Stop() error { s.miner.Stop() s.blockchain.Stop() s.engine.Close() - rawdb.PopUncleanShutdownMarker(s.chainDb) + rawdb.PopUncleanShutdownMarker(s.chainDb, stopUncleanShutdownUpdateCh) s.chainDb.Close() s.eventMux.Stop() diff --git a/les/client.go b/les/client.go index 1d8a2c6f9a07..5cf5653707b2 100644 --- a/les/client.go +++ b/les/client.go @@ -78,6 +78,12 @@ type LightEthereum struct { udpEnabled bool } +// I know this shouldn't be a global, but I'm +// not sure where the appropriate place would be to put +// the chan, maybe in handler in the Ethereum struct, but +// that doesn't seem appropriate to me? +var stopUncleanShutdownUpdateCh chan bool = make(chan bool) + // New creates an instance of the light client. func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) { chainDb, err := stack.OpenDatabase("lightchaindata", config.DatabaseCache, config.DatabaseHandles, "eth/db/chaindata/", false) @@ -195,6 +201,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) { "age", common.PrettyAge(t)) } } + // starts a goroutine to update the unclean shutdown marker + // every five minutes, so the user can get an approximate time of + // the previous crash upon startup + go rawdb.UpdateUncleanShutdownMarker(chainDb, stopUncleanShutdownUpdateCh) return leth, nil } @@ -383,7 +393,7 @@ func (s *LightEthereum) Stop() error { s.engine.Close() s.pruner.close() s.eventMux.Stop() - rawdb.PopUncleanShutdownMarker(s.chainDb) + rawdb.PopUncleanShutdownMarker(s.chainDb, stopUncleanShutdownUpdateCh) s.chainDb.Close() s.lesDb.Close() s.wg.Wait() From f1652f332f9ba4110cc697d99d08d26c15f167a6 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 31 May 2021 15:28:05 +0100 Subject: [PATCH 02/21] core/rawdb: removed return from error check because it will block the shutdown --- core/rawdb/accessors_metadata.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 76df39863da7..8de2bd9d9b8c 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -157,7 +157,6 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda // unless PushUncleanShutdownMarker fails. if l == 0 { log.Warn("recent unclean shutdown markers is empty. Stopping marker update") - return } // update marker every five minutes ticker := time.NewTicker(300 * time.Second) From c97c7d4978ace1d3eb37056a0f285e08c4c9ac1c Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 1 Jun 2021 12:29:20 +0100 Subject: [PATCH 03/21] core/rawdb: removed old code --- core/rawdb/accessors_metadata.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 8de2bd9d9b8c..d4f6d4d894c9 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -153,18 +153,13 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen } l := len(uncleanShutdowns.Recent) - // uncleanShutdowns.Recent should not be empty in this context, - // unless PushUncleanShutdownMarker fails. - if l == 0 { - log.Warn("recent unclean shutdown markers is empty. Stopping marker update") - } // update marker every five minutes ticker := time.NewTicker(300 * time.Second) defer func() { ticker.Stop() }() for { select { case <-ticker.C: - uncleanShutdowns.Recent[l] = uint64(time.Now().Unix()) + uncleanShutdowns.Recent[l-1] = uint64(time.Now().Unix()) data, _ := rlp.EncodeToBytes(uncleanShutdowns) if err := db.Put(uncleanShutdownKey, data); err != nil { log.Warn("Failed to update unclean-shutdown marker", "err", err) From eca0d46d1944d12f3613ada82398bcd462792ac4 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:15:31 +0100 Subject: [PATCH 04/21] core/rawdb, eth,les: moved shutdown marker update to rawdb/database.go --- core/rawdb/accessors_metadata.go | 9 +++++---- core/rawdb/database.go | 34 ++++++++++++++++++++++++++++---- eth/backend.go | 24 ---------------------- les/client.go | 25 ----------------------- 4 files changed, 35 insertions(+), 57 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index d4f6d4d894c9..9e4cc6246f8c 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -98,7 +98,10 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) var uncleanShutdowns crashList // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { - log.Warn("Error reading unclean shutdown markers", "error", err) + // don't want to warn if there were no unclean shutdowns + if err.Error() != "not found" { + log.Warn("Error reading unclean shutdown markers", "error", err) + } } else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil { return nil, 0, err } @@ -123,9 +126,7 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) // PopUncleanShutdownMarker removes the last unclean shutdown marker and stops // the function updating the marker (UpdateUncleanShutdownMarker) -func PopUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpdateCh chan bool) { - // stop the unclean shutdown marker update first - stopUncleanShutdownUpdateCh <- true +func PopUncleanShutdownMarker(db ethdb.KeyValueStore) { var uncleanShutdowns crashList // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { diff --git a/core/rawdb/database.go b/core/rawdb/database.go index c8bfdbace14e..35aaa8fa892e 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -33,15 +33,20 @@ import ( ) // freezerdb is a database wrapper that enabled freezer data retrievals. +// stopUncleanMarkerUpdateCh is used to stop the unclean shutdown marker +// that updates every 5 minutes. type freezerdb struct { ethdb.KeyValueStore ethdb.AncientStore + stopUncleanMarkerUpdateCh chan bool } // Close implements io.Closer, closing both the fast key-value store as well as // the slow ancient tables. func (frdb *freezerdb) Close() error { var errs []error + frdb.stopUncleanMarkerUpdateCh <- true + PopUncleanShutdownMarker(frdb.KeyValueStore) if err := frdb.AncientStore.Close(); err != nil { errs = append(errs, err) } @@ -75,8 +80,11 @@ func (frdb *freezerdb) Freeze(threshold uint64) error { } // nofreezedb is a database wrapper that disables freezer data retrievals. +// stopUncleanMarkerUpdateCh is used to stop the unclean shutdown marker +// that updates every 5 minutes. type nofreezedb struct { ethdb.KeyValueStore + stopUncleanMarkerUpdateCh chan bool } // HasAncient returns an error as we don't have a backing chain freezer. @@ -117,9 +125,25 @@ func (db *nofreezedb) Sync() error { // NewDatabase creates a high level database on top of a given key-value data // store without a freezer moving immutable chain segments into cold storage. func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { - return &nofreezedb{ - KeyValueStore: db, + frdb := &nofreezedb{ + KeyValueStore: db, + stopUncleanMarkerUpdateCh: make(chan bool), } + // Check for unclean shutdown + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } + go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) + return frdb } // NewDatabaseWithFreezer creates a high level database on top of a given key- @@ -204,8 +228,9 @@ func NewDatabaseWithFreezer(db ethdb.KeyValueStore, freezer string, namespace st }() } return &freezerdb{ - KeyValueStore: db, - AncientStore: frdb, + KeyValueStore: db, + AncientStore: frdb, + stopUncleanMarkerUpdateCh: make(chan bool), }, nil } @@ -244,6 +269,7 @@ func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer kvdb.Close() return nil, err } + go UpdateUncleanShutdownMarker(frdb, frdb.(*freezerdb).stopUncleanMarkerUpdateCh) return frdb, nil } diff --git a/eth/backend.go b/eth/backend.go index d26258b64c5a..6d41d246a8bc 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -97,12 +97,6 @@ type Ethereum struct { lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase) } -// I know this shouldn't be a global, but I'm -// not sure where the appropriate place would be to put -// the chan, maybe in handler in the Ethereum struct, but -// that doesn't seem appropriate to me? -var stopUncleanShutdownUpdateCh chan bool = make(chan bool) - // New creates a new Ethereum object (including the // initialisation of the common Ethereum object) func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { @@ -264,23 +258,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { stack.RegisterAPIs(eth.APIs()) stack.RegisterProtocols(eth.Protocols()) stack.RegisterLifecycle(eth) - // Check for unclean shutdown - if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(chainDb); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - // starts a goroutine to update the unclean shutdown marker - // every five minutes, so the user can get an approximate time of - // the previous crash upon startup - go rawdb.UpdateUncleanShutdownMarker(chainDb, stopUncleanShutdownUpdateCh) return eth, nil } @@ -569,7 +546,6 @@ func (s *Ethereum) Stop() error { s.miner.Stop() s.blockchain.Stop() s.engine.Close() - rawdb.PopUncleanShutdownMarker(s.chainDb, stopUncleanShutdownUpdateCh) s.chainDb.Close() s.eventMux.Stop() diff --git a/les/client.go b/les/client.go index 5cf5653707b2..519a6b0fb16b 100644 --- a/les/client.go +++ b/les/client.go @@ -78,12 +78,6 @@ type LightEthereum struct { udpEnabled bool } -// I know this shouldn't be a global, but I'm -// not sure where the appropriate place would be to put -// the chan, maybe in handler in the Ethereum struct, but -// that doesn't seem appropriate to me? -var stopUncleanShutdownUpdateCh chan bool = make(chan bool) - // New creates an instance of the light client. func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) { chainDb, err := stack.OpenDatabase("lightchaindata", config.DatabaseCache, config.DatabaseHandles, "eth/db/chaindata/", false) @@ -187,24 +181,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) { stack.RegisterAPIs(leth.APIs()) stack.RegisterProtocols(leth.Protocols()) stack.RegisterLifecycle(leth) - - // Check for unclean shutdown - if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(chainDb); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - // starts a goroutine to update the unclean shutdown marker - // every five minutes, so the user can get an approximate time of - // the previous crash upon startup - go rawdb.UpdateUncleanShutdownMarker(chainDb, stopUncleanShutdownUpdateCh) return leth, nil } @@ -393,7 +369,6 @@ func (s *LightEthereum) Stop() error { s.engine.Close() s.pruner.close() s.eventMux.Stop() - rawdb.PopUncleanShutdownMarker(s.chainDb, stopUncleanShutdownUpdateCh) s.chainDb.Close() s.lesDb.Close() s.wg.Wait() From 2374f518f4ca7036442b52631a9a720381b65dd1 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:25:15 +0100 Subject: [PATCH 05/21] core/rawdb: added a lenght check --- core/rawdb/accessors_metadata.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 9e4cc6246f8c..e0de5710b5da 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -154,6 +154,9 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen } l := len(uncleanShutdowns.Recent) + if l == 0 { + l++ + } // update marker every five minutes ticker := time.NewTicker(300 * time.Second) defer func() { ticker.Stop() }() From a3a163ddc23dd5956217b3755d28d969ea29e851 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:33:03 +0100 Subject: [PATCH 06/21] test: --- core/rawdb/accessors_metadata.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index e0de5710b5da..d858e5104720 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -18,6 +18,7 @@ package rawdb import ( "encoding/json" + "fmt" "time" "github.com/ethereum/go-ethereum/common" @@ -117,6 +118,7 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) } // And save it again data, _ := rlp.EncodeToBytes(uncleanShutdowns) + fmt.Println(data) if err := db.Put(uncleanShutdownKey, data); err != nil { log.Warn("Failed to write unclean-shutdown marker", "err", err) return nil, 0, err From 32f3c09b09f1b6f0c8efa0e24fa6b54126152126 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:33:37 +0100 Subject: [PATCH 07/21] tests --- core/rawdb/accessors_metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index d858e5104720..25b958b4b3d7 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -160,7 +160,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda l++ } // update marker every five minutes - ticker := time.NewTicker(300 * time.Second) + ticker := time.NewTicker(3 * time.Second) defer func() { ticker.Stop() }() for { select { From 13c16bb977de3632ea3f674bfbc2b121016dd43d Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:35:09 +0100 Subject: [PATCH 08/21] tests --- core/rawdb/accessors_metadata.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 25b958b4b3d7..7f3c315664bc 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -123,6 +123,7 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) log.Warn("Failed to write unclean-shutdown marker", "err", err) return nil, 0, err } + fmt.Println(db.Get(uncleanShutdownKey)) return previous, discarded, nil } From d321679b8eb56abf095a5ae0ccace3c5f2bac597 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:37:30 +0100 Subject: [PATCH 09/21] da --- core/rawdb/accessors_metadata.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 7f3c315664bc..d5cb8a02b012 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -156,6 +156,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda } else if err := rlp.DecodeBytes(previous, &uncleanShutdowns); err != nil { log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen } + fmt.Println(uncleanShutdowns) l := len(uncleanShutdowns.Recent) if l == 0 { l++ From 22258ff7cb0d68fa88ef88f78de4834b2e427d81 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:39:06 +0100 Subject: [PATCH 10/21] da --- core/rawdb/database.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 35aaa8fa892e..ab334ceaac7f 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -142,6 +142,7 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { "age", common.PrettyAge(t)) } } + fmt.Println(">>>>????>>>>") go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) return frdb } From ae1a6730ce472c7254dabae34926e97cc8522a80 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:41:38 +0100 Subject: [PATCH 11/21] da --- core/rawdb/database.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index ab334ceaac7f..c9b402e8e8c2 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "fmt" + log2 "log" "os" "sync/atomic" "time" @@ -143,6 +144,7 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { } } fmt.Println(">>>>????>>>>") + log2.Panic("WF") go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) return frdb } From 8559c6377ccdd16b2847d3ea1f55cb93d57996a2 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:45:14 +0100 Subject: [PATCH 12/21] da --- core/rawdb/database.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index c9b402e8e8c2..0c7d04d9b6ea 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -20,7 +20,6 @@ import ( "bytes" "errors" "fmt" - log2 "log" "os" "sync/atomic" "time" @@ -130,22 +129,6 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { KeyValueStore: db, stopUncleanMarkerUpdateCh: make(chan bool), } - // Check for unclean shutdown - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - fmt.Println(">>>>????>>>>") - log2.Panic("WF") - go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) return frdb } From 32a0bc034fa720eb6ed44da967941bf5e0b301ce Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:49:49 +0100 Subject: [PATCH 13/21] da --- core/rawdb/database.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 0c7d04d9b6ea..b0829a6ddf79 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -255,6 +255,18 @@ func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer kvdb.Close() return nil, err } + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } go UpdateUncleanShutdownMarker(frdb, frdb.(*freezerdb).stopUncleanMarkerUpdateCh) return frdb, nil } From 452a5c0f2a897c4b3912dbeaf479f9b4417dee10 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:53:03 +0100 Subject: [PATCH 14/21] da --- core/rawdb/accessors_metadata.go | 4 ---- core/rawdb/database.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index d5cb8a02b012..1d6a877b45af 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -18,7 +18,6 @@ package rawdb import ( "encoding/json" - "fmt" "time" "github.com/ethereum/go-ethereum/common" @@ -118,12 +117,10 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) } // And save it again data, _ := rlp.EncodeToBytes(uncleanShutdowns) - fmt.Println(data) if err := db.Put(uncleanShutdownKey, data); err != nil { log.Warn("Failed to write unclean-shutdown marker", "err", err) return nil, 0, err } - fmt.Println(db.Get(uncleanShutdownKey)) return previous, discarded, nil } @@ -156,7 +153,6 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda } else if err := rlp.DecodeBytes(previous, &uncleanShutdowns); err != nil { log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen } - fmt.Println(uncleanShutdowns) l := len(uncleanShutdowns.Recent) if l == 0 { l++ diff --git a/core/rawdb/database.go b/core/rawdb/database.go index b0829a6ddf79..1766c66992ad 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -129,6 +129,19 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { KeyValueStore: db, stopUncleanMarkerUpdateCh: make(chan bool), } + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } + go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) return frdb } From c2b1ed41331033223d9970e19542f8de912e8f08 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 20:59:05 +0100 Subject: [PATCH 15/21] updates --- core/rawdb/accessors_metadata.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 1d6a877b45af..09435e128392 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -124,8 +124,7 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) return previous, discarded, nil } -// PopUncleanShutdownMarker removes the last unclean shutdown marker and stops -// the function updating the marker (UpdateUncleanShutdownMarker) +// PopUncleanShutdownMarker removes the last unclean shutdown marker func PopUncleanShutdownMarker(db ethdb.KeyValueStore) { var uncleanShutdowns crashList // Read old data From c73fcddaa1c8699807a3e740a0b34f05924f5860 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 21:29:53 +0100 Subject: [PATCH 16/21] asdf --- core/rawdb/accessors_metadata.go | 11 +++++++++-- core/rawdb/database.go | 26 -------------------------- node/node.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 09435e128392..cc601497331c 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -90,11 +90,13 @@ type crashList struct { const crashesToKeep = 10 +func StartUncleanMarkerMonitor() + // PushUncleanShutdownMarker appends a new unclean shutdown marker and returns // the previous data // - a list of timestamps // - a count of how many old unclean-shutdowns have been discarded -func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) { +func PushUncleanShutdownMarker(isFreezer bool, db ethdb.KeyValueStore) ([]uint64, uint64, error) { var uncleanShutdowns crashList // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { @@ -121,6 +123,11 @@ func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) log.Warn("Failed to write unclean-shutdown marker", "err", err) return nil, 0, err } + if isFreezer { + go UpdateUncleanShutdownMarker(db, db.(*freezerdb).stopUncleanMarkerUpdateCh) + } else { + go UpdateUncleanShutdownMarker(db, db.(*nofreezedb).stopUncleanMarkerUpdateCh) + } return previous, discarded, nil } @@ -157,7 +164,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda l++ } // update marker every five minutes - ticker := time.NewTicker(3 * time.Second) + ticker := time.NewTicker(1 * time.Second) defer func() { ticker.Stop() }() for { select { diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 1766c66992ad..26afc6c34d36 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -129,19 +129,6 @@ func NewDatabase(db ethdb.KeyValueStore) ethdb.Database { KeyValueStore: db, stopUncleanMarkerUpdateCh: make(chan bool), } - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - go UpdateUncleanShutdownMarker(frdb, frdb.stopUncleanMarkerUpdateCh) return frdb } @@ -268,19 +255,6 @@ func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer kvdb.Close() return nil, err } - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(frdb); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - go UpdateUncleanShutdownMarker(frdb, frdb.(*freezerdb).stopUncleanMarkerUpdateCh) return frdb, nil } diff --git a/node/node.go b/node/node.go index 1e65fff1c271..199de36a6d0b 100644 --- a/node/node.go +++ b/node/node.go @@ -25,8 +25,10 @@ import ( "reflect" "strings" "sync" + "time" "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" @@ -561,6 +563,20 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r } else { db, err = rawdb.NewLevelDBDatabase(n.ResolvePath(name), cache, handles, namespace, readonly) } + if name == "lightchaindata" { + if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(false, db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } + } if err == nil { db = n.wrapDatabase(db) @@ -594,6 +610,18 @@ func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, freezer, } db, err = rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace, readonly) } + if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(true, db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } if err == nil { db = n.wrapDatabase(db) From 6aecb892c3203abbf7eaea32cb4ffdfea4c8b1a4 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 21:30:47 +0100 Subject: [PATCH 17/21] afsd --- core/rawdb/accessors_metadata.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index cc601497331c..1a1db7afc3d0 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -18,6 +18,7 @@ package rawdb import ( "encoding/json" + "fmt" "time" "github.com/ethereum/go-ethereum/common" @@ -90,8 +91,6 @@ type crashList struct { const crashesToKeep = 10 -func StartUncleanMarkerMonitor() - // PushUncleanShutdownMarker appends a new unclean shutdown marker and returns // the previous data // - a list of timestamps @@ -175,6 +174,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda log.Warn("Failed to update unclean-shutdown marker", "err", err) } case <-stopUncleanShutdownUpdateCh: + fmt.Println("GOT HERE") return } } From 55dbf709a628ccfb2ae74b03ee9c974b1de742e1 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 21:34:26 +0100 Subject: [PATCH 18/21] core/rawdb: simplified code --- core/rawdb/database.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 26afc6c34d36..4d89198457ec 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -87,6 +87,15 @@ type nofreezedb struct { stopUncleanMarkerUpdateCh chan bool } +func (db *nofreezedb) Close() error { + db.stopUncleanMarkerUpdateCh <- true + PopUncleanShutdownMarker(db.KeyValueStore) + if err := db.KeyValueStore.Close(); err != nil { + return err + } + return nil +} + // HasAncient returns an error as we don't have a backing chain freezer. func (db *nofreezedb) HasAncient(kind string, number uint64) (bool, error) { return false, errNotSupported From bfc1dcd7f04064078b88230c875c62ee2dae2c51 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 23:08:17 +0100 Subject: [PATCH 19/21] core/rawdb, node: Improved code organization --- core/rawdb/accessors_metadata.go | 60 +++++++++++++++++++++++++++++--- core/rawdb/database.go | 18 +++++++--- node/node.go | 34 +++--------------- 3 files changed, 72 insertions(+), 40 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 1a1db7afc3d0..ded917b60d99 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -18,7 +18,6 @@ package rawdb import ( "encoding/json" - "fmt" "time" "github.com/ethereum/go-ethereum/common" @@ -91,8 +90,60 @@ type crashList struct { const crashesToKeep = 10 -// PushUncleanShutdownMarker appends a new unclean shutdown marker and returns -// the previous data +func StartUncleanShutdownMarker(name string, db ethdb.KeyValueStore) { + if name == "lightchaindata" { + db.(*nofreezedb).isChainDb = true + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(false, db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + } + return + } else if name == "les.client" { + db.(*nofreezedb).isChainDb = false + return + } else if name == "chaindata" { + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(true, db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + return + } + } else if name == "" { + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(false, db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) + } + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, + "age", common.PrettyAge(t)) + } + return + } + } + return +} + +// PushUncleanShutdownMarker appends a new unclean shutdown marker and starts a goroutine +// to update the unclean shutdown marker every five minutes. It returns +// the previous unclean shutdown // - a list of timestamps // - a count of how many old unclean-shutdowns have been discarded func PushUncleanShutdownMarker(isFreezer bool, db ethdb.KeyValueStore) ([]uint64, uint64, error) { @@ -100,7 +151,7 @@ func PushUncleanShutdownMarker(isFreezer bool, db ethdb.KeyValueStore) ([]uint64 // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { // don't want to warn if there were no unclean shutdowns - if err.Error() != "not found" { + if err.Error() != "leveldb: not found" { log.Warn("Error reading unclean shutdown markers", "error", err) } } else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil { @@ -174,7 +225,6 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda log.Warn("Failed to update unclean-shutdown marker", "err", err) } case <-stopUncleanShutdownUpdateCh: - fmt.Println("GOT HERE") return } } diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 4d89198457ec..453b29188db5 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -85,15 +85,23 @@ func (frdb *freezerdb) Freeze(threshold uint64) error { type nofreezedb struct { ethdb.KeyValueStore stopUncleanMarkerUpdateCh chan bool + isChainDb bool } func (db *nofreezedb) Close() error { - db.stopUncleanMarkerUpdateCh <- true - PopUncleanShutdownMarker(db.KeyValueStore) - if err := db.KeyValueStore.Close(); err != nil { - return err + if db.isChainDb { + db.stopUncleanMarkerUpdateCh <- true + PopUncleanShutdownMarker(db.KeyValueStore) + if err := db.KeyValueStore.Close(); err != nil { + return err + } + return nil + } else { + if err := db.KeyValueStore.Close(); err != nil { + return err + } + return nil } - return nil } // HasAncient returns an error as we don't have a backing chain freezer. diff --git a/node/node.go b/node/node.go index 199de36a6d0b..ec81d97718eb 100644 --- a/node/node.go +++ b/node/node.go @@ -25,10 +25,8 @@ import ( "reflect" "strings" "sync" - "time" "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" @@ -558,26 +556,13 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r var db ethdb.Database var err error + fmt.Println(n.config.DataDir) if n.config.DataDir == "" { db = rawdb.NewMemoryDatabase() } else { db, err = rawdb.NewLevelDBDatabase(n.ResolvePath(name), cache, handles, namespace, readonly) } - if name == "lightchaindata" { - if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(false, db); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - } - + rawdb.StartUncleanShutdownMarker(name, db) if err == nil { db = n.wrapDatabase(db) } @@ -600,6 +585,7 @@ func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, freezer, var err error if n.config.DataDir == "" { db = rawdb.NewMemoryDatabase() + rawdb.StartUncleanShutdownMarker("", db) } else { root := n.ResolvePath(name) switch { @@ -609,20 +595,8 @@ func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, freezer, freezer = n.ResolvePath(freezer) } db, err = rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace, readonly) + rawdb.StartUncleanShutdownMarker(name, db) } - if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(true, db); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - if err == nil { db = n.wrapDatabase(db) } From a5e56a488000df9405efd64018ad7d729951bad8 Mon Sep 17 00:00:00 2001 From: BitBaseBit Date: Wed, 9 Jun 2021 23:10:29 +0100 Subject: [PATCH 20/21] core/rawdb: changed update time back to five minutes --- core/rawdb/accessors_metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index ded917b60d99..e0e7cec4ae30 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -214,7 +214,7 @@ func UpdateUncleanShutdownMarker(db ethdb.KeyValueStore, stopUncleanShutdownUpda l++ } // update marker every five minutes - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(300 * time.Second) defer func() { ticker.Stop() }() for { select { From 0e0ccef9eeb9aace417435403e536180835b3b03 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 28 Jun 2021 14:46:24 +0200 Subject: [PATCH 21/21] core/rawdb: cleanup --- core/rawdb/accessors_metadata.go | 71 +++++++++++--------------------- core/rawdb/database.go | 14 +++---- 2 files changed, 29 insertions(+), 56 deletions(-) diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index e0e7cec4ae30..cce30f526a9f 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -91,51 +91,28 @@ type crashList struct { const crashesToKeep = 10 func StartUncleanShutdownMarker(name string, db ethdb.KeyValueStore) { - if name == "lightchaindata" { - db.(*nofreezedb).isChainDb = true - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(false, db); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - } - return - } else if name == "les.client" { + switch name { + case "les.client": db.(*nofreezedb).isChainDb = false return - } else if name == "chaindata" { - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(true, db); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - return + case "lightchaindata": + db.(*nofreezedb).isChainDb = true + case "chaindata": + break + case "": + break + default: + return + } + if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(db); err != nil { + log.Error("Could not update unclean-shutdown-marker list", "error", err) + } else { + if discards > 0 { + log.Warn("Old unclean shutdowns found", "count", discards) } - } else if name == "" { - if uncleanShutdowns, discards, err := PushUncleanShutdownMarker(false, db); err != nil { - log.Error("Could not update unclean-shutdown-marker list", "error", err) - } else { - if discards > 0 { - log.Warn("Old unclean shutdowns found", "count", discards) - } - for _, tstamp := range uncleanShutdowns { - t := time.Unix(int64(tstamp), 0) - log.Warn("Unclean shutdown detected", "booted", t, - "age", common.PrettyAge(t)) - } - return + for _, tstamp := range uncleanShutdowns { + t := time.Unix(int64(tstamp), 0) + log.Warn("Unclean shutdown detected", "booted", t, "age", common.PrettyAge(t)) } } return @@ -146,7 +123,7 @@ func StartUncleanShutdownMarker(name string, db ethdb.KeyValueStore) { // the previous unclean shutdown // - a list of timestamps // - a count of how many old unclean-shutdowns have been discarded -func PushUncleanShutdownMarker(isFreezer bool, db ethdb.KeyValueStore) ([]uint64, uint64, error) { +func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) { var uncleanShutdowns crashList // Read old data if data, err := db.Get(uncleanShutdownKey); err != nil { @@ -173,10 +150,10 @@ func PushUncleanShutdownMarker(isFreezer bool, db ethdb.KeyValueStore) ([]uint64 log.Warn("Failed to write unclean-shutdown marker", "err", err) return nil, 0, err } - if isFreezer { - go UpdateUncleanShutdownMarker(db, db.(*freezerdb).stopUncleanMarkerUpdateCh) - } else { - go UpdateUncleanShutdownMarker(db, db.(*nofreezedb).stopUncleanMarkerUpdateCh) + if frdb, ok := db.(*freezerdb); ok { + go UpdateUncleanShutdownMarker(db, frdb.stopUncleanMarkerUpdateCh) + } else if nfrdb, ok := db.(*nofreezedb); ok { + go UpdateUncleanShutdownMarker(db, nfrdb.stopUncleanMarkerUpdateCh) } return previous, discarded, nil } diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 453b29188db5..f4d86117a646 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -92,16 +92,12 @@ func (db *nofreezedb) Close() error { if db.isChainDb { db.stopUncleanMarkerUpdateCh <- true PopUncleanShutdownMarker(db.KeyValueStore) - if err := db.KeyValueStore.Close(); err != nil { - return err - } - return nil - } else { - if err := db.KeyValueStore.Close(); err != nil { - return err - } - return nil } + if err := db.KeyValueStore.Close(); err != nil { + return err + } + return nil + } // HasAncient returns an error as we don't have a backing chain freezer.