Skip to content

Commit bc88a59

Browse files
committed
mv DB's loc. constructor code to a single place
Currently, we construct the path of various DBs and stores in many places within the ledger component. This is risky as we need to ensure that the same postfix is used during a path construction at all places. Hence, in this CR, we move this logic to a common place. FAB-16126 #done Change-Id: I04b24f85d64b5867200316add3cd6423cc3e8da3 Signed-off-by: senthil <cendhu@gmail.com>
1 parent 68545d7 commit bc88a59

File tree

10 files changed

+93
-46
lines changed

10 files changed

+93
-46
lines changed

core/ledger/kvledger/drop_dbs.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kvledger
88

99
import (
1010
"os"
11-
"path/filepath"
1211

1312
"github.com/pkg/errors"
1413
)
@@ -39,27 +38,27 @@ func dropDBs(rootFSPath string) error {
3938
}
4039

4140
func dropStateLevelDB(rootFSPath string) error {
42-
stateLeveldbPath := filepath.Join(rootFSPath, "stateLeveldb")
41+
stateLeveldbPath := StateDBPath(rootFSPath)
4342
logger.Infof("Dropping StateLevelDB at location [%s] ...if present", stateLeveldbPath)
4443
return os.RemoveAll(stateLeveldbPath)
4544
}
4645

4746
func dropConfigHistoryDB(rootFSPath string) error {
48-
configHistoryDBPath := filepath.Join(rootFSPath, "configHistory")
47+
configHistoryDBPath := ConfigHistoryDBPath(rootFSPath)
4948
logger.Infof("Dropping ConfigHistoryDB at location [%s]", configHistoryDBPath)
5049
err := os.RemoveAll(configHistoryDBPath)
5150
return errors.Wrapf(err, "error removing the ConfigHistoryDB located at %s", configHistoryDBPath)
5251
}
5352

5453
func dropBookkeeperDB(rootFSPath string) error {
55-
bookkeeperDBPath := filepath.Join(rootFSPath, "bookkeeper")
54+
bookkeeperDBPath := BookkeeperDBPath(rootFSPath)
5655
logger.Infof("Dropping BookkeeperDB at location [%s]", bookkeeperDBPath)
5756
err := os.RemoveAll(bookkeeperDBPath)
5857
return errors.Wrapf(err, "error removing the BookkeeperDB located at %s", bookkeeperDBPath)
5958
}
6059

6160
func dropHistoryDB(rootFSPath string) error {
62-
historyDBPath := filepath.Join(rootFSPath, "historyLeveldb")
61+
historyDBPath := HistoryDBPath(rootFSPath)
6362
logger.Infof("Dropping HistoryDB at location [%s] ...if present", historyDBPath)
6463
return os.RemoveAll(historyDBPath)
6564
}

core/ledger/kvledger/kv_ledger_provider.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package kvledger
99
import (
1010
"bytes"
1111
"fmt"
12-
"path/filepath"
1312

1413
"github.com/golang/protobuf/proto"
1514
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
@@ -56,7 +55,7 @@ type Provider struct {
5655
// NewProvider instantiates a new Provider.
5756
// This is not thread-safe and assumed to be synchronized by the caller
5857
func NewProvider(initializer *ledger.Initializer) (*Provider, error) {
59-
fileLockPath := filepath.Join(initializer.Config.RootFSPath, "fileLock")
58+
fileLockPath := fileLockPath(initializer.Config.RootFSPath)
6059
fileLock := leveldbhelper.NewFileLock(fileLockPath)
6160
if err := fileLock.Lock(); err != nil {
6261
return nil, errors.Wrap(err, "as another peer node command is executing,"+
@@ -67,29 +66,29 @@ func NewProvider(initializer *ledger.Initializer) (*Provider, error) {
6766
p.initializer = initializer
6867
p.fileLock = fileLock
6968
// initialize the ID store (inventory of chainIds/ledgerIds)
70-
idStore := openIDStore(filepath.Join(p.initializer.Config.RootFSPath, "ledgerProvider"))
69+
idStore := openIDStore(ledgerProviderPath(p.initializer.Config.RootFSPath))
7170
p.idStore = idStore
7271
// initialize ledger storage
7372
privateData := &pvtdatastorage.PrivateDataConfig{
7473
PrivateDataConfig: initializer.Config.PrivateDataConfig,
75-
StorePath: filepath.Join(initializer.Config.RootFSPath, "pvtdataStore"),
74+
StorePath: PvtDataStorePath(p.initializer.Config.RootFSPath),
7675
}
7776
ledgerStoreProvider := ledgerstorage.NewProvider(
78-
p.initializer.Config.RootFSPath,
77+
BlockStorePath(p.initializer.Config.RootFSPath),
7978
privateData,
8079
p.initializer.MetricsProvider,
8180
)
8281
p.ledgerStoreProvider = ledgerStoreProvider
8382
if initializer.Config.HistoryDBConfig.Enabled {
8483
// Initialize the history database (index for history of values by key)
8584
historydbProvider := history.NewDBProvider(
86-
filepath.Join(p.initializer.Config.RootFSPath, "historyLeveldb"),
85+
HistoryDBPath(p.initializer.Config.RootFSPath),
8786
)
8887
p.historydbProvider = historydbProvider
8988
}
9089
// initialize config history for chaincode
9190
configHistoryMgr := confighistory.NewMgr(
92-
filepath.Join(p.initializer.Config.RootFSPath, "configHistory"),
91+
ConfigHistoryDBPath(p.initializer.Config.RootFSPath),
9392
initializer.DeployedChaincodeInfoProvider,
9493
)
9594
p.configHistoryMgr = configHistoryMgr
@@ -108,12 +107,12 @@ func NewProvider(initializer *ledger.Initializer) (*Provider, error) {
108107
p.stateListeners = stateListeners
109108

110109
p.bookkeepingProvider = bookkeeping.NewProvider(
111-
filepath.Join(p.initializer.Config.RootFSPath, "bookkeeper"),
110+
BookkeeperDBPath(p.initializer.Config.RootFSPath),
112111
)
113112
var err error
114113
stateDB := &privacyenabledstate.StateDBConfig{
115114
StateDBConfig: initializer.Config.StateDBConfig,
116-
LevelDBPath: filepath.Join(initializer.Config.RootFSPath, "stateLeveldb"),
115+
LevelDBPath: StateDBPath(p.initializer.Config.RootFSPath),
117116
}
118117
p.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(
119118
p.bookkeepingProvider,

core/ledger/kvledger/kv_ledger_provider_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ func TestLedgerBackup(t *testing.T) {
331331

332332
// remove the statedb, historydb, and block indexes (they are supposed to be auto created during opening of an existing ledger)
333333
// and rename the originalPath to restorePath
334-
assert.NoError(t, os.RemoveAll(filepath.Join(originalPath, "stateLeveldb")))
335-
assert.NoError(t, os.RemoveAll(filepath.Join(originalPath, "historyLeveldb")))
336-
assert.NoError(t, os.RemoveAll(filepath.Join(originalPath, "chains", fsblkstorage.IndexDir)))
334+
assert.NoError(t, os.RemoveAll(StateDBPath(originalPath)))
335+
assert.NoError(t, os.RemoveAll(HistoryDBPath(originalPath)))
336+
assert.NoError(t, os.RemoveAll(filepath.Join(BlockStorePath(originalPath), fsblkstorage.IndexDir)))
337337
assert.NoError(t, os.Rename(originalPath, restorePath))
338338

339339
// Instantiate the ledger from restore environment and this should behave exactly as it would have in the original environment
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package kvledger
7+
8+
import (
9+
"path/filepath"
10+
)
11+
12+
func fileLockPath(rootFSPath string) string {
13+
return filepath.Join(rootFSPath, "fileLock")
14+
}
15+
16+
func ledgerProviderPath(rootFSPath string) string {
17+
return filepath.Join(rootFSPath, "ledgerProvider")
18+
}
19+
20+
// BlockStorePath returns the absolute path of block storage
21+
func BlockStorePath(rootFSPath string) string {
22+
return filepath.Join(rootFSPath, "chains")
23+
}
24+
25+
// PvtDataStorePath returns the absolute path of pvtdata storage
26+
func PvtDataStorePath(rootFSPath string) string {
27+
return filepath.Join(rootFSPath, "pvtdataStore")
28+
}
29+
30+
// StateDBPath returns the absolute path of state level DB
31+
func StateDBPath(rootFSPath string) string {
32+
return filepath.Join(rootFSPath, "stateLeveldb")
33+
}
34+
35+
// HistoryDBPath returns the absolute path of history DB
36+
func HistoryDBPath(rootFSPath string) string {
37+
return filepath.Join(rootFSPath, "historyLeveldb")
38+
}
39+
40+
// ConfigHistoryDBPath returns the absolute path of configHistory DB
41+
func ConfigHistoryDBPath(rootFSPath string) string {
42+
return filepath.Join(rootFSPath, "configHistory")
43+
}
44+
45+
// BookkeeperDBPath return the absolute path of bookkeeper DB
46+
func BookkeeperDBPath(rootFSPath string) string {
47+
return filepath.Join(rootFSPath, "bookkeeper")
48+
}

core/ledger/kvledger/reset.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ SPDX-License-Identifier: Apache-2.0
66
package kvledger
77

88
import (
9-
"path/filepath"
10-
119
"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
1210
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
1311
"github.com/pkg/errors"
1412
)
1513

1614
// ResetAllKVLedgers resets all ledger to the genesis block.
1715
func ResetAllKVLedgers(rootFSPath string) error {
18-
fileLockPath := filepath.Join(rootFSPath, "fileLock")
16+
fileLockPath := fileLockPath(rootFSPath)
1917
fileLock := leveldbhelper.NewFileLock(fileLockPath)
2018
if err := fileLock.Lock(); err != nil {
2119
return errors.Wrap(err, "as another peer node command is executing,"+
@@ -37,20 +35,20 @@ func ResetAllKVLedgers(rootFSPath string) error {
3735

3836
// LoadPreResetHeight returns the prereset height of all ledgers.
3937
func LoadPreResetHeight(rootFSPath string) (map[string]uint64, error) {
40-
blockstorePath := filepath.Join(rootFSPath, "chains")
38+
blockstorePath := BlockStorePath(rootFSPath)
4139
logger.Infof("Loading prereset height from path [%s]", blockstorePath)
4240
return fsblkstorage.LoadPreResetHeight(blockstorePath)
4341
}
4442

4543
// ClearPreResetHeight removes the prereset height recorded in the file system.
4644
func ClearPreResetHeight(rootFSPath string) error {
47-
blockstorePath := filepath.Join(rootFSPath, "chains")
45+
blockstorePath := BlockStorePath(rootFSPath)
4846
logger.Infof("Clearing off prereset height files from path [%s]", blockstorePath)
4947
return fsblkstorage.ClearPreResetHeight(blockstorePath)
5048
}
5149

5250
func resetBlockStorage(rootFSPath string) error {
53-
blockstorePath := filepath.Join(rootFSPath, "chains")
51+
blockstorePath := BlockStorePath(rootFSPath)
5452
logger.Infof("Resetting BlockStore to genesis block at location [%s]", blockstorePath)
5553
return fsblkstorage.ResetBlockStore(blockstorePath)
5654
}

core/ledger/kvledger/rollback.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ SPDX-License-Identifier: Apache-2.0
77
package kvledger
88

99
import (
10-
"path/filepath"
11-
1210
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
1311
"github.com/hyperledger/fabric/core/ledger/ledgerstorage"
1412
"github.com/pkg/errors"
1513
)
1614

1715
// RollbackKVLedger rollbacks a ledger to a specified block number
1816
func RollbackKVLedger(rootFSPath, ledgerID string, blockNum uint64) error {
19-
fileLockPath := filepath.Join(rootFSPath, "fileLock")
17+
fileLockPath := fileLockPath(rootFSPath)
2018
fileLock := leveldbhelper.NewFileLock(fileLockPath)
2119
if err := fileLock.Lock(); err != nil {
2220
return errors.Wrap(err, "as another peer node command is executing,"+
2321
" wait for that command to complete its execution or terminate it before retrying")
2422
}
2523
defer fileLock.Unlock()
2624

27-
blockstorePath := filepath.Join(rootFSPath, "chains")
25+
blockstorePath := BlockStorePath(rootFSPath)
2826
if err := ledgerstorage.ValidateRollbackParams(blockstorePath, ledgerID, blockNum); err != nil {
2927
return err
3028
}

core/ledger/kvledger/tests/env.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/hyperledger/fabric/common/metrics/disabled"
1818
"github.com/hyperledger/fabric/core/common/privdata"
1919
"github.com/hyperledger/fabric/core/ledger"
20+
"github.com/hyperledger/fabric/core/ledger/kvledger"
2021
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
2122
"github.com/hyperledger/fabric/core/scc/lscc"
2223
"github.com/hyperledger/fabric/msp"
@@ -166,23 +167,23 @@ func (e *env) getLedgerRootPath() string {
166167
}
167168

168169
func (e *env) getLevelstateDBPath() string {
169-
return filepath.Join(e.initializer.Config.RootFSPath, "stateLeveldb")
170+
return kvledger.StateDBPath(e.initializer.Config.RootFSPath)
170171
}
171172

172173
func (e *env) getBlockIndexDBPath() string {
173-
return filepath.Join(e.initializer.Config.RootFSPath, "chains", fsblkstorage.IndexDir)
174+
return filepath.Join(kvledger.BlockStorePath(e.initializer.Config.RootFSPath), fsblkstorage.IndexDir)
174175
}
175176

176177
func (e *env) getConfigHistoryDBPath() string {
177-
return filepath.Join(e.initializer.Config.RootFSPath, "configHistory")
178+
return kvledger.ConfigHistoryDBPath(e.initializer.Config.RootFSPath)
178179
}
179180

180181
func (e *env) getHistoryDBPath() string {
181-
return filepath.Join(e.initializer.Config.RootFSPath, "historyLeveldb")
182+
return kvledger.HistoryDBPath(e.initializer.Config.RootFSPath)
182183
}
183184

184185
func (e *env) getBookkeeperDBPath() string {
185-
return filepath.Join(e.initializer.Config.RootFSPath, "bookkeeper")
186+
return kvledger.BookkeeperDBPath(e.initializer.Config.RootFSPath)
186187
}
187188

188189
func populateMissingsWithTestDefaults(t *testing.T, initializer *ledgermgmt.Initializer) {

core/ledger/kvledger/tests/reset_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package tests
88

99
import (
1010
"fmt"
11-
"path/filepath"
1211
"testing"
1312

1413
"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
@@ -172,7 +171,7 @@ func TestResetLedgerWithoutDroppingDBs(t *testing.T) {
172171
env.closeLedgerMgmt()
173172

174173
// Reset All kv ledgers
175-
blockstorePath := filepath.Join(env.initializer.Config.RootFSPath, "chains")
174+
blockstorePath := kvledger.BlockStorePath(env.initializer.Config.RootFSPath)
176175
err := fsblkstorage.ResetBlockStore(blockstorePath)
177176
assert.NoError(t, err)
178177
rebuildable := rebuildableStatedb | rebuildableBookkeeper | rebuildableConfigHistory | rebuildableHistoryDB

core/ledger/ledgerstorage/store.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package ledgerstorage
88

99
import (
10-
"path/filepath"
1110
"sync"
1211
"sync/atomic"
1312

@@ -50,12 +49,12 @@ var attrsToIndex = []blkstorage.IndexableAttr{
5049
}
5150

5251
// NewProvider returns the handle to the provider
53-
func NewProvider(storeDir string, conf *pvtdatastorage.PrivateDataConfig, metricsProvider metrics.Provider) *Provider {
52+
func NewProvider(blockStoreDir string, conf *pvtdatastorage.PrivateDataConfig, metricsProvider metrics.Provider) *Provider {
5453
// Initialize the block storage
5554
indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
5655
blockStoreProvider := fsblkstorage.NewProvider(
5756
fsblkstorage.NewConf(
58-
filepath.Join(storeDir, "chains"),
57+
blockStoreDir,
5958
maxBlockFileSize,
6059
),
6160
indexConfig,

0 commit comments

Comments
 (0)