Skip to content

Commit

Permalink
Shutdown blockstore (if open) before dropping ledger (#1992)
Browse files Browse the repository at this point in the history
FAB-18261

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored Oct 9, 2020
1 parent f79abaa commit 114f030
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 11 deletions.
11 changes: 9 additions & 2 deletions common/ledger/blockledger/fileledger/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type blockStoreProvider interface {

type fileLedgerFactory struct {
blkstorageProvider blockStoreProvider
ledgers map[string]blockledger.ReadWriter
ledgers map[string]*FileLedger
mutex sync.Mutex
}

Expand Down Expand Up @@ -55,6 +55,13 @@ func (f *fileLedgerFactory) Remove(channelID string) error {
f.mutex.Lock()
defer f.mutex.Unlock()

// check cache for open blockstore and, if one exists,
// shut it down in order to avoid resource contention
ledger, ok := f.ledgers[channelID]
if ok {
ledger.blockStore.Shutdown()
}

err := f.blkstorageProvider.Drop(channelID)
if err != nil {
return err
Expand Down Expand Up @@ -92,6 +99,6 @@ func New(directory string, metricsProvider metrics.Provider) (blockledger.Factor
}
return &fileLedgerFactory{
blkstorageProvider: p,
ledgers: make(map[string]blockledger.ReadWriter),
ledgers: map[string]*FileLedger{},
}, nil
}
36 changes: 27 additions & 9 deletions common/ledger/blockledger/fileledger/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ import (
"os"
"testing"

"github.com/hyperledger/fabric/common/ledger/blockledger"
"github.com/hyperledger/fabric/common/ledger/blockledger/fileledger/mock"
"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/stretchr/testify/require"
)

//go:generate counterfeiter -o mock/file_ledger_block_store.go --fake-name FileLedgerBlockStore . fileLedgerBlockStore

type fileLedgerBlockStore interface {
FileLedgerBlockStore
}

func TestBlockStoreProviderErrors(t *testing.T) {
mockBlockStore := &mock.BlockStoreProvider{}
mockBlockStoreProvider := &mock.BlockStoreProvider{}
f := &fileLedgerFactory{
blkstorageProvider: mockBlockStore,
ledgers: map[string]blockledger.ReadWriter{},
blkstorageProvider: mockBlockStoreProvider,
ledgers: map[string]*FileLedger{},
}

t.Run("list", func(t *testing.T) {
mockBlockStore.ListReturns(nil, errors.New("boogie"))
mockBlockStoreProvider.ListReturns(nil, errors.New("boogie"))
require.PanicsWithValue(
t,
"boogie",
Expand All @@ -36,16 +41,29 @@ func TestBlockStoreProviderErrors(t *testing.T) {
})

t.Run("open", func(t *testing.T) {
mockBlockStore.OpenReturns(nil, errors.New("woogie"))
mockBlockStoreProvider.OpenReturns(nil, errors.New("woogie"))
_, err := f.GetOrCreate("foo")
require.EqualError(t, err, "woogie")
require.Empty(t, f.ledgers, "Expected no new ledger is created")
})

t.Run("remove", func(t *testing.T) {
mockBlockStore.DropReturns(errors.New("oogie"))
err := f.Remove("foo")
require.EqualError(t, err, "oogie")
t.Run("ledger doesn't exist", func(t *testing.T) {
err := f.Remove("foo")
require.NoError(t, err)
require.Equal(t, 1, mockBlockStoreProvider.DropCallCount())
})

t.Run("dropping the blockstore fails", func(t *testing.T) {
mockBlockStore := &mock.FileLedgerBlockStore{}
f.ledgers["foo"] = &FileLedger{blockStore: mockBlockStore}
mockBlockStoreProvider.DropReturns(errors.New("oogie"))

err := f.Remove("foo")
require.EqualError(t, err, "oogie")
require.Equal(t, 1, mockBlockStore.ShutdownCallCount())
require.Equal(t, 2, mockBlockStoreProvider.DropCallCount())
})
})
}

Expand Down
1 change: 1 addition & 0 deletions common/ledger/blockledger/fileledger/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FileLedgerBlockStore interface {
AddBlock(block *cb.Block) error
GetBlockchainInfo() (*cb.BlockchainInfo, error)
RetrieveBlocks(startBlockNumber uint64) (ledger.ResultsIterator, error)
Shutdown()
}

// NewFileLedger creates a new FileLedger for interaction with the ledger
Expand Down
285 changes: 285 additions & 0 deletions common/ledger/blockledger/fileledger/mock/file_ledger_block_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func (flbs fileLedgerBlockStore) RetrieveBlocks(startBlockNumber uint64) (common
return flbs.GetBlocksIterator(startBlockNumber)
}

func (flbs fileLedgerBlockStore) Shutdown() {}

// NewConfigSupport returns
func NewConfigSupport(peer *Peer) cc.Manager {
return &configSupport{
Expand Down

0 comments on commit 114f030

Please sign in to comment.