github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/upgrade_dbs.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package kvledger 8 9 import ( 10 "github.com/hechain20/hechain/common/ledger/blkstorage" 11 "github.com/hechain20/hechain/common/ledger/util/leveldbhelper" 12 "github.com/hechain20/hechain/core/ledger" 13 "github.com/hechain20/hechain/core/ledger/kvledger/txmgmt/statedb/statecouchdb" 14 "github.com/pkg/errors" 15 ) 16 17 // UpgradeDBs upgrades existing ledger databases to the latest formats. 18 // It checks the format of idStore and does not drop any databases 19 // if the format is already the latest version. Otherwise, it drops 20 // ledger databases and upgrades the idStore format. 21 func UpgradeDBs(config *ledger.Config) error { 22 rootFSPath := config.RootFSPath 23 fileLockPath := fileLockPath(rootFSPath) 24 fileLock := leveldbhelper.NewFileLock(fileLockPath) 25 if err := fileLock.Lock(); err != nil { 26 return errors.Wrap(err, "as another peer node command is executing,"+ 27 " wait for that command to complete its execution or terminate it before retrying") 28 } 29 defer fileLock.Unlock() 30 31 logger.Infof("Ledger data folder from config = [%s]", rootFSPath) 32 33 dbPath := LedgerProviderPath(rootFSPath) 34 db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath}) 35 db.Open() 36 defer db.Close() 37 idStore := &idStore{db, dbPath} 38 39 // Check upfront whether we should upgrade the data format before dropping databases. 40 // If someone mistakenly executes the upgrade command in a peer that has some channels that 41 // are bootstrapped from a snapshot, the peer will not be able to start as the data for those channels 42 // cannot be recovered 43 isEligible, err := idStore.checkUpgradeEligibility() 44 if err != nil { 45 return errors.WithMessage(err, "error while checking whether upgrade is required") 46 } 47 if !isEligible { 48 return errors.New("the data format is already up to date. No upgrade is required") 49 } 50 51 if config.StateDBConfig.StateDatabase == ledger.CouchDB { 52 if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil { 53 return err 54 } 55 } 56 if err := dropDBs(rootFSPath); err != nil { 57 return err 58 } 59 if err := blkstorage.DeleteBlockStoreIndex(BlockStorePath(rootFSPath)); err != nil { 60 return err 61 } 62 63 return idStore.upgradeFormat() 64 }