Commit dd8961c9 authored by Gari Singh's avatar Gari Singh
Browse files

FAB-15182 Use idiomatic self-reference



Use short idiomatic reference for
provider. Really an aesthetic change.

FAB-15182 #done

Change-Id: Id64e6f0ee88b44b3f722b937349aac7cb63c722f
Signed-off-by: default avatarGari Singh <gari.r.singh@gmail.com>
parent d468a176
......@@ -62,13 +62,13 @@ func NewProvider() (ledger.PeerLedgerProvider, error) {
// Initialize the history database (index for history of values by key)
historydbProvider := historyleveldb.NewHistoryDBProvider()
logger.Info("ledger provider Initialized")
provider := &Provider{idStore, ledgerStoreProvider,
p := &Provider{idStore, ledgerStoreProvider,
nil, historydbProvider, nil, nil, nil, nil, nil, nil}
return provider, nil
return p, nil
}
// Initialize implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
func (p *Provider) Initialize(initializer *ledger.Initializer) error {
var err error
configHistoryMgr := confighistory.NewMgr(initializer.DeployedChaincodeInfoProvider)
collElgNotifier := &collElgNotifier{
......@@ -80,13 +80,13 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
stateListeners = append(stateListeners, collElgNotifier)
stateListeners = append(stateListeners, configHistoryMgr)
provider.initializer = initializer
provider.configHistoryMgr = configHistoryMgr
provider.stateListeners = stateListeners
provider.collElgNotifier = collElgNotifier
provider.bookkeepingProvider = bookkeeping.NewProvider()
provider.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(
provider.bookkeepingProvider,
p.initializer = initializer
p.configHistoryMgr = configHistoryMgr
p.stateListeners = stateListeners
p.collElgNotifier = collElgNotifier
p.bookkeepingProvider = bookkeeping.NewProvider()
p.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(
p.bookkeepingProvider,
initializer.MetricsProvider,
initializer.HealthCheckRegistry,
initializer.Config.StateDB,
......@@ -94,8 +94,8 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
if err != nil {
return err
}
provider.stats = newStats(initializer.MetricsProvider)
provider.recoverUnderConstructionLedger()
p.stats = newStats(initializer.MetricsProvider)
p.recoverUnderConstructionLedger()
return nil
}
......@@ -104,26 +104,26 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
// upon a successful ledger creation with the committed genesis block, removes the flag and add entry into
// created ledgers list (atomically). If a crash happens in between, the 'recoverUnderConstructionLedger'
// function is invoked before declaring the provider to be usable
func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, error) {
func (p *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, error) {
ledgerID, err := protoutil.GetChainIDFromBlock(genesisBlock)
if err != nil {
return nil, err
}
exists, err := provider.idStore.ledgerIDExists(ledgerID)
exists, err := p.idStore.ledgerIDExists(ledgerID)
if err != nil {
return nil, err
}
if exists {
return nil, ErrLedgerIDExists
}
if err = provider.idStore.setUnderConstructionFlag(ledgerID); err != nil {
if err = p.idStore.setUnderConstructionFlag(ledgerID); err != nil {
return nil, err
}
lgr, err := provider.openInternal(ledgerID)
lgr, err := p.openInternal(ledgerID)
if err != nil {
logger.Errorf("Error opening a new empty ledger. Unsetting under construction flag. Error: %+v", err)
panicOnErr(provider.runCleanup(ledgerID), "Error running cleanup for ledger id [%s]", ledgerID)
panicOnErr(provider.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
panicOnErr(p.runCleanup(ledgerID), "Error running cleanup for ledger id [%s]", ledgerID)
panicOnErr(p.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
return nil, err
}
if err := lgr.CommitWithPvtData(&ledger.BlockAndPvtData{
......@@ -132,40 +132,40 @@ func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger,
lgr.Close()
return nil, err
}
panicOnErr(provider.idStore.createLedgerID(ledgerID, genesisBlock), "Error while marking ledger as created")
panicOnErr(p.idStore.createLedgerID(ledgerID, genesisBlock), "Error while marking ledger as created")
return lgr, nil
}
// Open implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Open(ledgerID string) (ledger.PeerLedger, error) {
func (p *Provider) Open(ledgerID string) (ledger.PeerLedger, error) {
logger.Debugf("Open() opening kvledger: %s", ledgerID)
// Check the ID store to ensure that the chainId/ledgerId exists
exists, err := provider.idStore.ledgerIDExists(ledgerID)
exists, err := p.idStore.ledgerIDExists(ledgerID)
if err != nil {
return nil, err
}
if !exists {
return nil, ErrNonExistingLedgerID
}
return provider.openInternal(ledgerID)
return p.openInternal(ledgerID)
}
func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, error) {
func (p *Provider) openInternal(ledgerID string) (ledger.PeerLedger, error) {
// Get the block store for a chain/ledger
blockStore, err := provider.ledgerStoreProvider.Open(ledgerID)
blockStore, err := p.ledgerStoreProvider.Open(ledgerID)
if err != nil {
return nil, err
}
provider.collElgNotifier.registerListener(ledgerID, blockStore)
p.collElgNotifier.registerListener(ledgerID, blockStore)
// Get the versioned database (state database) for a chain/ledger
vDB, err := provider.vdbProvider.GetDBHandle(ledgerID)
vDB, err := p.vdbProvider.GetDBHandle(ledgerID)
if err != nil {
return nil, err
}
// Get the history database (index for history of values by key) for a chain/ledger
historyDB, err := provider.historydbProvider.GetDBHandle(ledgerID)
historyDB, err := p.historydbProvider.GetDBHandle(ledgerID)
if err != nil {
return nil, err
}
......@@ -173,10 +173,10 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro
// Create a kvLedger for this chain/ledger, which encasulates the underlying data stores
// (id store, blockstore, state database, history database)
l, err := newKVLedger(
ledgerID, blockStore, vDB, historyDB, provider.configHistoryMgr,
provider.stateListeners, provider.bookkeepingProvider,
provider.initializer.DeployedChaincodeInfoProvider,
provider.stats.ledgerStats(ledgerID),
ledgerID, blockStore, vDB, historyDB, p.configHistoryMgr,
p.stateListeners, p.bookkeepingProvider,
p.initializer.DeployedChaincodeInfoProvider,
p.stats.ledgerStats(ledgerID),
)
if err != nil {
return nil, err
......@@ -185,39 +185,39 @@ func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, erro
}
// Exists implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Exists(ledgerID string) (bool, error) {
return provider.idStore.ledgerIDExists(ledgerID)
func (p *Provider) Exists(ledgerID string) (bool, error) {
return p.idStore.ledgerIDExists(ledgerID)
}
// List implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) List() ([]string, error) {
return provider.idStore.getAllLedgerIds()
func (p *Provider) List() ([]string, error) {
return p.idStore.getAllLedgerIds()
}
// Close implements the corresponding method from interface ledger.PeerLedgerProvider
func (provider *Provider) Close() {
provider.idStore.close()
provider.ledgerStoreProvider.Close()
provider.vdbProvider.Close()
provider.historydbProvider.Close()
provider.bookkeepingProvider.Close()
provider.configHistoryMgr.Close()
func (p *Provider) Close() {
p.idStore.close()
p.ledgerStoreProvider.Close()
p.vdbProvider.Close()
p.historydbProvider.Close()
p.bookkeepingProvider.Close()
p.configHistoryMgr.Close()
}
// recoverUnderConstructionLedger checks whether the under construction flag is set - this would be the case
// if a crash had happened during creation of ledger and the ledger creation could have been left in intermediate
// state. Recovery checks if the ledger was created and the genesis block was committed successfully then it completes
// the last step of adding the ledger id to the list of created ledgers. Else, it clears the under construction flag
func (provider *Provider) recoverUnderConstructionLedger() {
func (p *Provider) recoverUnderConstructionLedger() {
logger.Debugf("Recovering under construction ledger")
ledgerID, err := provider.idStore.getUnderConstructionFlag()
ledgerID, err := p.idStore.getUnderConstructionFlag()
panicOnErr(err, "Error while checking whether the under construction flag is set")
if ledgerID == "" {
logger.Debugf("No under construction ledger found. Quitting recovery")
return
}
logger.Infof("ledger [%s] found as under construction", ledgerID)
ledger, err := provider.openInternal(ledgerID)
ledger, err := p.openInternal(ledgerID)
panicOnErr(err, "Error while opening under construction ledger [%s]", ledgerID)
bcInfo, err := ledger.GetBlockchainInfo()
panicOnErr(err, "Error while getting blockchain info for the under construction ledger [%s]", ledgerID)
......@@ -226,13 +226,13 @@ func (provider *Provider) recoverUnderConstructionLedger() {
switch bcInfo.Height {
case 0:
logger.Infof("Genesis block was not committed. Hence, the peer ledger not created. unsetting the under construction flag")
panicOnErr(provider.runCleanup(ledgerID), "Error while running cleanup for ledger id [%s]", ledgerID)
panicOnErr(provider.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
panicOnErr(p.runCleanup(ledgerID), "Error while running cleanup for ledger id [%s]", ledgerID)
panicOnErr(p.idStore.unsetUnderConstructionFlag(), "Error while unsetting under construction flag")
case 1:
logger.Infof("Genesis block was committed. Hence, marking the peer ledger as created")
genesisBlock, err := ledger.GetBlockByNumber(0)
panicOnErr(err, "Error while retrieving genesis block from blockchain for ledger [%s]", ledgerID)
panicOnErr(provider.idStore.createLedgerID(ledgerID, genesisBlock), "Error while adding ledgerID [%s] to created list", ledgerID)
panicOnErr(p.idStore.createLedgerID(ledgerID, genesisBlock), "Error while adding ledgerID [%s] to created list", ledgerID)
default:
panic(errors.Errorf(
"data inconsistency: under construction flag is set for ledger [%s] while the height of the blockchain is [%d]",
......@@ -243,7 +243,7 @@ func (provider *Provider) recoverUnderConstructionLedger() {
// runCleanup cleans up blockstorage, statedb, and historydb for what
// may have got created during in-complete ledger creation
func (provider *Provider) runCleanup(ledgerID string) error {
func (p *Provider) runCleanup(ledgerID string) error {
// TODO - though, not having this is harmless for kv ledger.
// If we want, following could be done:
// - blockstorage could remove empty folders
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment