-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_dbs.go
47 lines (41 loc) · 1.57 KB
/
rebuild_dbs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Copyright hechain. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package kvledger
import (
"github.com/hechain20/hechain/common/ledger/blkstorage"
"github.com/hechain20/hechain/common/ledger/util/leveldbhelper"
"github.com/hechain20/hechain/core/ledger"
"github.com/hechain20/hechain/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/pkg/errors"
)
// RebuildDBs drops existing ledger databases.
// Dropped database will be rebuilt upon server restart
func RebuildDBs(config *ledger.Config) error {
rootFSPath := config.RootFSPath
fileLockPath := fileLockPath(rootFSPath)
fileLock := leveldbhelper.NewFileLock(fileLockPath)
if err := fileLock.Lock(); err != nil {
return errors.Wrap(err, "as another peer node command is executing,"+
" wait for that command to complete its execution or terminate it before retrying")
}
defer fileLock.Unlock()
blockstorePath := BlockStorePath(rootFSPath)
ledgerIDs, err := blkstorage.GetLedgersBootstrappedFromSnapshot(blockstorePath)
if err != nil {
return errors.WithMessage(err, "error while checking if any ledger has been bootstrapped from snapshot")
}
if len(ledgerIDs) > 0 {
return errors.Errorf("cannot rebuild databases because the peer contains channel(s) %s that were bootstrapped from snapshot", ledgerIDs)
}
if config.StateDBConfig.StateDatabase == ledger.CouchDB {
if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
return err
}
}
if err := dropDBs(rootFSPath); err != nil {
return err
}
return blkstorage.DeleteBlockStoreIndex(blockstorePath)
}