Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #566 from bstasyszyn/fixdeadlock
Browse files Browse the repository at this point in the history
feat: Register a query executor provider along with state DB
  • Loading branch information
fqutishat authored Feb 10, 2021
2 parents 70ca2fd + a204e96 commit c5ded8c
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
google.golang.org/grpc v1.29.1
)

replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4
replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e

replace github.com/hyperledger/fabric/extensions => ./mod/peer

Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8L
github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y=
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab h1:9pygWVFqbY9lPxM0peffumuVDyMuIMzNLyO9uFjJuQo=
github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw=
github.com/Microsoft/hcsshim v0.8.10 h1:k5wTrpnVU2/xv8ZuzGkbXVd3js5zJ8RnumPo5RxiIxU=
github.com/Microsoft/hcsshim v0.8.10/go.mod h1:g5uw8EV2mAlzqe94tfNBNdr89fnbD/n3HV0OhsddkmM=
github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331 h1:3YnB7Hpmh1lPecPE8doMOtYCrMdrpedZOvxfuNES/Vk=
github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/hcsshim v0.8.14 h1:lbPVK25c1cu5xTLITwpUcxoA9vKrKErASPYygvouJns=
github.com/Microsoft/hcsshim v0.8.14/go.mod h1:NtVKoYxQuTLx6gEq0L96c9Ju4JbRJ4nY2ow3VK6a9Lg=
github.com/Shopify/sarama v1.20.1 h1:Bb0h3I++r4eX333Y0uZV2vwUXepJbt6ig05TUU1qt9I=
github.com/Shopify/sarama v1.20.1/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
Expand Down Expand Up @@ -625,10 +625,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285/go.mod h1:9OrXJ
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc h1:LUUe4cdABGrIJAhl1P1ZpWY76AwukVszFdwkVFVLwIk=
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4 h1:37E/T+2AGNrhN5TtANrQfHCxJeKGooG8+itNiG0oyF0=
github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4/go.mod h1:XaPNFwRTdf4lOytspbjkw1W0nsbmzC7ni0L6DQN1yDM=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20210107224011-c611644b3514 h1:DT6FIhK2he85RJQZOL5HNNrtwyjl6EdZK0AEa/vp0CE=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20210107224011-c611644b3514/go.mod h1:hil7DJWkbNoLezsVWTkGMiXc0KRMpETmU3U3P5G5zDc=
github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e h1:UkC+RK4HR0CLpAF965s5d66AdRMCjo7EBtoLsLfzrcE=
github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e/go.mod h1:XaPNFwRTdf4lOytspbjkw1W0nsbmzC7ni0L6DQN1yDM=
github.com/trustbloc/fabric-protos-go-ext v0.1.5 h1:dJ/Bt/nj98SRhUlZQc2DV5wRSE/oDcs3gJQRimwNQ3o=
github.com/trustbloc/fabric-protos-go-ext v0.1.5/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
Expand Down Expand Up @@ -757,6 +755,8 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 h1:kzM6+9dur93BcC2kVlYl34cHU+TYZLanmpSJHVMmL64=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion mod/peer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module github.com/trustbloc/fabric-peer-ext/mod/peer

replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4
replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e

replace github.com/hyperledger/fabric/extensions => ./

Expand Down
14 changes: 8 additions & 6 deletions mod/peer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8L
github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y=
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab h1:9pygWVFqbY9lPxM0peffumuVDyMuIMzNLyO9uFjJuQo=
github.com/Microsoft/go-winio v0.4.15-0.20200908182639-5b44b70ab3ab/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw=
github.com/Microsoft/hcsshim v0.8.10 h1:k5wTrpnVU2/xv8ZuzGkbXVd3js5zJ8RnumPo5RxiIxU=
github.com/Microsoft/hcsshim v0.8.10/go.mod h1:g5uw8EV2mAlzqe94tfNBNdr89fnbD/n3HV0OhsddkmM=
github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331 h1:3YnB7Hpmh1lPecPE8doMOtYCrMdrpedZOvxfuNES/Vk=
github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/Microsoft/hcsshim v0.8.14 h1:lbPVK25c1cu5xTLITwpUcxoA9vKrKErASPYygvouJns=
github.com/Microsoft/hcsshim v0.8.14/go.mod h1:NtVKoYxQuTLx6gEq0L96c9Ju4JbRJ4nY2ow3VK6a9Lg=
github.com/Shopify/sarama v1.20.1 h1:Bb0h3I++r4eX333Y0uZV2vwUXepJbt6ig05TUU1qt9I=
github.com/Shopify/sarama v1.20.1/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
Expand Down Expand Up @@ -607,8 +607,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285/go.mod h1:9OrXJ
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc h1:LUUe4cdABGrIJAhl1P1ZpWY76AwukVszFdwkVFVLwIk=
github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4 h1:37E/T+2AGNrhN5TtANrQfHCxJeKGooG8+itNiG0oyF0=
github.com/trustbloc/fabric-mod v0.1.6-0.20210106200132-8b87450ce0f4/go.mod h1:XaPNFwRTdf4lOytspbjkw1W0nsbmzC7ni0L6DQN1yDM=
github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e h1:UkC+RK4HR0CLpAF965s5d66AdRMCjo7EBtoLsLfzrcE=
github.com/trustbloc/fabric-mod v0.1.6-0.20210209215355-966ca0cc520e/go.mod h1:XaPNFwRTdf4lOytspbjkw1W0nsbmzC7ni0L6DQN1yDM=
github.com/trustbloc/fabric-protos-go-ext v0.1.5 h1:dJ/Bt/nj98SRhUlZQc2DV5wRSE/oDcs3gJQRimwNQ3o=
github.com/trustbloc/fabric-protos-go-ext v0.1.5/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/ugorji/go v1.1.1/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
Expand Down Expand Up @@ -737,6 +737,8 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479 h1:LhLiKguPgZL+Tglay4GhVtfF0kb8cvOJ0dHTCBO8YNI=
golang.org/x/sys v0.0.0-20200120151820-655fe14d7479/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3 h1:kzM6+9dur93BcC2kVlYl34cHU+TYZLanmpSJHVMmL64=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
16 changes: 14 additions & 2 deletions mod/peer/storage/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package statedb

import (
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
gossipapi "github.com/hyperledger/fabric/extensions/gossip/api"
"github.com/hyperledger/fabric/extensions/gossip/blockpublisher"
Expand All @@ -15,6 +16,17 @@ import (
extstatedb "github.com/trustbloc/fabric-peer-ext/pkg/statedb"
)

// QueryExecutorProvider provides a query executor with and without a commit lock
type QueryExecutorProvider interface {
// NewQueryExecutor returns a query executor that first acquires a commit read lock.
// Done() must be called when finished using it.
NewQueryExecutor() (ledger.QueryExecutor, error)

// NewQueryExecutorNoLock returns a query executor that does not acquire a commit read lock.
// Done() must NOT be called.
NewQueryExecutorNoLock() (ledger.QueryExecutor, error)
}

//AddCCUpgradeHandler adds chaincode upgrade handler to blockpublisher
func AddCCUpgradeHandler(chainName string, handler gossipapi.ChaincodeUpgradeHandler) {
if !roles.IsCommitter() {
Expand All @@ -23,8 +35,8 @@ func AddCCUpgradeHandler(chainName string, handler gossipapi.ChaincodeUpgradeHan
}

// Register registers a state database for a given channel
func Register(channelID string, db statedb.VersionedDB) {
extstatedb.GetProvider().Register(channelID, &stateDB{db: db})
func Register(channelID string, db statedb.VersionedDB, qep QueryExecutorProvider) {
extstatedb.GetProvider().Register(channelID, &stateDB{db: db}, qep)
}

type stateDB struct {
Expand Down
2 changes: 1 addition & 1 deletion mod/peer/storage/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ func TestAddCCUpgradeHandlerAsCommitter(t *testing.T) {
func TestRegister(t *testing.T) {
// NOTE: statedb.VersionedDB cannot be mocked since this interface references an internal package: github.com/hyperledger/fabric/core/ledger/internal/version.
// Therefore, stateDB cannot be fully tested
require.NotPanics(t, func() { Register("channel1", nil) })
require.NotPanics(t, func() { Register("channel1", nil, nil) })
}
5 changes: 5 additions & 0 deletions pkg/mocks/mockledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (m *Ledger) NewQueryExecutor() (ledger2.QueryExecutor, error) {
return m.QueryExecutor, m.Error
}

// NewQueryExecutorNoLock returns the query executor
func (m *Ledger) NewQueryExecutorNoLock() (ledger2.QueryExecutor, error) {
return m.QueryExecutor, m.Error
}

// NewHistoryQueryExecutor returns the history query executor
func (m *Ledger) NewHistoryQueryExecutor() (ledger2.HistoryQueryExecutor, error) {
panic("not implemented")
Expand Down
147 changes: 147 additions & 0 deletions pkg/statedb/mocks/queryexecutorprovider.gen.go

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

38 changes: 33 additions & 5 deletions pkg/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package statedb
import (
"sync"

"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
)

Expand Down Expand Up @@ -44,9 +45,25 @@ type StateDB interface {
UpdateCache(blockNum uint64, updates []byte) error
}

// QueryExecutorProvider provides a query executor with and without a commit lock
type QueryExecutorProvider interface {
// NewQueryExecutor returns a query executor that first acquires a commit read lock.
// Done() must be called when finished using it.
NewQueryExecutor() (ledger.QueryExecutor, error)

// NewQueryExecutorNoLock returns a query executor that does not acquire a commit read lock.
// Done() must NOT be called.
NewQueryExecutorNoLock() (ledger.QueryExecutor, error)
}

type channelDB struct {
stateDB StateDB
qeProvider QueryExecutorProvider
}

// Provider is a state database Provider
type Provider struct {
channelDBs map[string]StateDB
channelDBs map[string]*channelDB
mutex sync.RWMutex
}

Expand All @@ -57,7 +74,7 @@ func GetProvider() *Provider {

func newProvider() *Provider {
return &Provider{
channelDBs: make(map[string]StateDB),
channelDBs: make(map[string]*channelDB),
}
}

Expand All @@ -66,13 +83,24 @@ func (p *Provider) StateDBForChannel(channelID string) StateDB {
p.mutex.RLock()
defer p.mutex.RUnlock()

return p.channelDBs[channelID]
return p.channelDBs[channelID].stateDB
}

// QueryExecutorProviderForChannel returns the query executor provider for the given channel
func (p *Provider) QueryExecutorProviderForChannel(channelID string) QueryExecutorProvider {
p.mutex.RLock()
defer p.mutex.RUnlock()

return p.channelDBs[channelID].qeProvider
}

// Register registers a state database for the given channel
func (p *Provider) Register(channelID string, db StateDB) {
func (p *Provider) Register(channelID string, db StateDB, qep QueryExecutorProvider) {
p.mutex.Lock()
defer p.mutex.Unlock()

p.channelDBs[channelID] = db
p.channelDBs[channelID] = &channelDB{
stateDB: db,
qeProvider: qep,
}
}
5 changes: 4 additions & 1 deletion pkg/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import (

"github.com/stretchr/testify/require"
"github.com/trustbloc/fabric-peer-ext/pkg/mocks"
statedbmocks "github.com/trustbloc/fabric-peer-ext/pkg/statedb/mocks"
)

//go:generate counterfeiter -o ../mocks/statedb.gen.go --fake-name StateDB . StateDB
//go:generate counterfeiter -o ./mocks/queryexecutorprovider.gen.go --fake-name QueryExecutorProvider . QueryExecutorProvider

func TestProvider(t *testing.T) {
db := &mocks.StateDB{}
qep := &statedbmocks.QueryExecutorProvider{}

require.NotPanics(t, func() { GetProvider().Register("channel1", db) })
require.NotPanics(t, func() { GetProvider().Register("channel1", db, qep) })

require.True(t, GetProvider().StateDBForChannel("channel1") == db)
}
4 changes: 2 additions & 2 deletions scripts/pull_fabric.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ git clone https://github.com/trustbloc/fabric-mod.git $GOPATH/src/github.com/hyp
cp -r . $GOPATH/src/github.com/hyperledger/fabric/fabric-peer-ext
cd $GOPATH/src/github.com/hyperledger/fabric
git config advice.detachedHead false
# fabric-mod (Jan 8, 2021)
git checkout 8b87450ce0f44bea81debed1d0caad029d11d66d
# fabric-mod (Feb 9, 2021)
git checkout 966ca0cc520e95e448847861e16a484fb087bd20

declare envOS
envOS=$(uname -s)
Expand Down
Loading

0 comments on commit c5ded8c

Please sign in to comment.