Skip to content

Commit

Permalink
[FAB-2649] Concurrent access to viper
Browse files Browse the repository at this point in the history
Running gossip discovery tests in parallel rarelly results
in concurrent access to viper and concurrent map read and map write
error. This CS fix this by protecting access by lock

Change-Id: I02db2cc1c5565008de316250850492e2f98af55e
Signed-off-by: Gennady Laventman <[email protected]>
  • Loading branch information
gennadylaventman committed Jun 6, 2017
1 parent 1e9a087 commit 384a4a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions gossip/discovery/discovery_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
"github.com/spf13/viper"
)

const defaultHelloInterval = time.Duration(5) * time.Second
Expand All @@ -42,12 +41,12 @@ var maxConnectionAttempts = 120

// SetAliveTimeInterval sets the alive time interval
func SetAliveTimeInterval(interval time.Duration) {
viper.Set("peer.gossip.aliveTimeInterval", interval)
util.SetDuration("peer.gossip.aliveTimeInterval", interval)
}

// SetAliveExpirationTimeout sets the expiration timeout
func SetAliveExpirationTimeout(timeout time.Duration) {
viper.Set("peer.gossip.aliveExpirationTimeout", timeout)
util.SetDuration("peer.gossip.aliveExpirationTimeout", timeout)
aliveExpirationCheckInterval = time.Duration(timeout / 10)
}

Expand All @@ -58,7 +57,7 @@ func SetAliveExpirationCheckInterval(interval time.Duration) {

// SetReconnectInterval sets the reconnect interval
func SetReconnectInterval(interval time.Duration) {
viper.Set("peer.gossip.reconnectInterval", interval)
util.SetDuration("peer.gossip.reconnectInterval", interval)
}

// SetMaxConnAttempts sets the maximum number of connection
Expand Down
15 changes: 15 additions & 0 deletions gossip/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
// Equals returns whether a and b are the same
type Equals func(a interface{}, b interface{}) bool

var viperLock sync.RWMutex

// IndexInSlice returns the index of given object o in array
func IndexInSlice(array interface{}, o interface{}, equals Equals) int {
arr := reflect.ValueOf(array)
Expand Down Expand Up @@ -145,6 +147,9 @@ func PrintStackTrace() {

// GetIntOrDefault returns the int value from config if present otherwise default value
func GetIntOrDefault(key string, defVal int) int {
viperLock.RLock()
defer viperLock.RUnlock()

if val := viper.GetInt(key); val != 0 {
return val
}
Expand All @@ -154,13 +159,23 @@ func GetIntOrDefault(key string, defVal int) int {

// GetDurationOrDefault returns the Duration value from config if present otherwise default value
func GetDurationOrDefault(key string, defVal time.Duration) time.Duration {
viperLock.RLock()
defer viperLock.RUnlock()

if val := viper.GetDuration(key); val != 0 {
return val
}

return defVal
}

// SetDuration stores duration key value to viper
func SetDuration(key string, val time.Duration) {
viperLock.Lock()
defer viperLock.Unlock()
viper.Set(key, val)
}

// RandomInt returns, as an int, a non-negative pseudo-random integer in [0,n)
// It panics if n <= 0
func RandomInt(n int) int {
Expand Down

0 comments on commit 384a4a6

Please sign in to comment.