Skip to content

Commit

Permalink
Use a single leveldb for state maintainance
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1543

Change-Id: Ib11d1a2fc14cf81dd5380eadf93e73e1c56113e3
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Jan 8, 2017
1 parent 3e534de commit f0c43f7
Show file tree
Hide file tree
Showing 29 changed files with 885 additions and 197 deletions.
3 changes: 3 additions & 0 deletions core/chaincode/configer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -98,6 +99,8 @@ func TestConfigerInvokeJoinChainWrongParams(t *testing.T) {
func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
//t.Skip("Test CI build")
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
defer os.RemoveAll("/var/hyperledger/test/")

e := new(PeerConfiger)
Expand Down
3 changes: 2 additions & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/peer/msp"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -114,7 +115,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
}
closeListenerAndSleep(lis)
}

ledgermgmt.CleanupTestEnv()
ledgerPath := viper.GetString("peer.fileSystemPath")
os.RemoveAll(ledgerPath)
os.RemoveAll(filepath.Join(os.TempDir(), "hyperledger"))
Expand Down
14 changes: 7 additions & 7 deletions core/committer/committer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ limitations under the License.
package committer

import (
"os"
"testing"

"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
pb "github.com/hyperledger/fabric/protos/peer"
)

func TestKVLedgerBlockStorage(t *testing.T) {
conf := kvledger.NewConf("/tmp/tests/ledger/", 0)
defer os.RemoveAll("/tmp/tests/ledger/")

ledger, _ := kvledger.NewKVLedger(conf)
viper.Set("peer.fileSystemPath", "/tmp/fabric/committertest")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
ledger, err := ledgermgmt.CreateLedger("TestLedger")
assert.NoError(t, err, "Error while creating ledger: %s", err)
defer ledger.Close()

committer := NewLedgerCommitter(ledger)

height, err := committer.LedgerHeight()
assert.Equal(t, uint64(0), height)
assert.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions core/committer/txvalidator/txvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ limitations under the License.
package txvalidator

import (
"os"
"testing"

"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand All @@ -36,10 +36,10 @@ func (v *mockVsccValidator) VSCCValidateTx(payload *common.Payload, envBytes []b
}

func TestKVLedgerBlockStorage(t *testing.T) {
conf := kvledger.NewConf("/tmp/tests/ledger/", 0)
defer os.RemoveAll("/tmp/tests/ledger/")

ledger, _ := kvledger.NewKVLedger(conf)
viper.Set("peer.fileSystemPath", "/tmp/fabric/txvalidatortest")
ledgermgmt.InitializeTestEnv()
defer ledgermgmt.CleanupTestEnv()
ledger, _ := ledgermgmt.CreateLedger("TestLedger")
defer ledger.Close()

validator := &txValidator{ledger, &mockVsccValidator{}}
Expand Down
18 changes: 12 additions & 6 deletions core/ledger/kvledger/example/main/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import (
"os"

"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/ledger/kvledger/example"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
)

const (
ledgerPath = "/tmp/test/ledger/kvledger/example"
ledgerID = "Default"
)

var finalLedger ledger.ValidatedLedger
Expand All @@ -47,10 +48,10 @@ func init() {
// Initialization will get a handle to the ledger at the specified path
// Note, if subledgers are supported in the future,
// the various ledgers could be created/managed at this level
os.RemoveAll(ledgerPath)
ledgerConf := kvledger.NewConf(ledgerPath, 0)
cleanup()
ledgermgmt.Initialize()
var err error
finalLedger, err = kvledger.NewKVLedger(ledgerConf)
finalLedger, err = ledgermgmt.CreateLedger(ledgerID)
if err != nil {
panic(fmt.Errorf("Error in NewKVLedger(): %s", err))
}
Expand All @@ -60,7 +61,7 @@ func init() {
}

func main() {
defer finalLedger.Close()
defer ledgermgmt.Close()

// Each of the functions here will emulate endorser, orderer,
// and committer by calling ledger APIs to similate the proposal,
Expand Down Expand Up @@ -164,3 +165,8 @@ func handleError(err error, quit bool) {
}
}
}

func cleanup() {
ledgerRootPath := ledgerconfig.GetRootPath()
os.RemoveAll(ledgerRootPath)
}
38 changes: 10 additions & 28 deletions core/ledger/kvledger/kv_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package kvledger
import (
"errors"
"fmt"
"strings"

"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/blkstorage"
"github.com/hyperledger/fabric/core/ledger/blkstorage/fsblkstorage"
"github.com/hyperledger/fabric/core/ledger/history"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/stateleveldb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
Expand All @@ -39,45 +38,28 @@ import (

var logger = logging.MustGetLogger("kvledger")

// Conf captures `KVLedger` configurations
type Conf struct {
blockStorageDir string
maxBlockfileSize int
txMgrDBPath string
}

// NewConf constructs new `Conf`.
// filesystemPath is the top level directory under which `KVLedger` manages its data
func NewConf(filesystemPath string, maxBlockfileSize int) *Conf {
if !strings.HasSuffix(filesystemPath, "/") {
filesystemPath = filesystemPath + "/"
}
blocksStorageDir := filesystemPath + "blocks"
txMgrDBPath := filesystemPath + "txMgmgt/db"
return &Conf{blocksStorageDir, maxBlockfileSize, txMgrDBPath}
}

// KVLedger provides an implementation of `ledger.ValidatedLedger`.
// This implementation provides a key-value based data model
type KVLedger struct {
ledgerID string
blockStore blkstorage.BlockStore
txtmgmt txmgr.TxMgr
historymgmt history.HistMgr
}

// NewKVLedger constructs new `KVLedger`
func NewKVLedger(conf *Conf) (*KVLedger, error) {

logger.Debugf("Creating KVLedger using config: ", conf)

func NewKVLedger(versionedDBProvider statedb.VersionedDBProvider, ledgerID string) (*KVLedger, error) {
logger.Debugf("Creating KVLedger ledgerID=%s: ", ledgerID)
attrsToIndex := []blkstorage.IndexableAttr{
blkstorage.IndexableAttrBlockHash,
blkstorage.IndexableAttrBlockNum,
blkstorage.IndexableAttrTxID,
blkstorage.IndexableAttrBlockNumTranNum,
}
indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
blockStorageConf := fsblkstorage.NewConf(conf.blockStorageDir, conf.maxBlockfileSize)

blockStorageDir := ledgerconfig.GetBlockStoragePath(ledgerID)
blockStorageConf := fsblkstorage.NewConf(blockStorageDir, ledgerconfig.GetMaxBlockfileSize())
blockStore := fsblkstorage.NewFsBlockStore(blockStorageConf, indexConfig)

//State and History database managers
Expand All @@ -91,14 +73,14 @@ func NewKVLedger(conf *Conf) (*KVLedger, error) {
couchDBDef := ledgerconfig.GetCouchDBDefinition()

//create new transaction manager based on couchDB
txmgmt = couchdbtxmgmt.NewCouchDBTxMgr(&couchdbtxmgmt.Conf{DBPath: conf.txMgrDBPath},
txmgmt = couchdbtxmgmt.NewCouchDBTxMgr(&couchdbtxmgmt.Conf{DBPath: ""},
couchDBDef.URL, //couchDB connection URL
"system", //couchDB db name matches ledger name, TODO for now use system ledger, eventually allow passing in subledger name
couchDBDef.Username, //enter couchDB id here
couchDBDef.Password) //enter couchDB pw here
} else {
// Fall back to using goleveldb lockbased transaction manager
db := stateleveldb.NewVersionedDBProvider(&stateleveldb.Conf{DBPath: conf.txMgrDBPath}).GetDBHandle("Default")
db := versionedDBProvider.GetDBHandle(ledgerID)
txmgmt = lockbasedtxmgr.NewLockBasedTxMgr(db)
}

Expand All @@ -114,7 +96,7 @@ func NewKVLedger(conf *Conf) (*KVLedger, error) {
couchDBDef.Password) //enter couchDB pw here
}

l := &KVLedger{blockStore, txmgmt, historymgmt}
l := &KVLedger{ledgerID, blockStore, txmgmt, historymgmt}

if err := recoverStateDB(l); err != nil {
panic(fmt.Errorf(`Error during state DB recovery:%s`, err))
Expand Down

0 comments on commit f0c43f7

Please sign in to comment.