Skip to content

Commit

Permalink
[FABG-782] Fix default event service policy
Browse files Browse the repository at this point in the history
If the eventService policy is not defined for a channel
then the one defined under the _default channel is used.

This causes a problem with parameters where 0 is a valid
value, i.e. blockHeightLagThreshold and
reconnectBlockHeightLagThreshold. If 0 is specified
then the SDK will assume that the value was not provided
and then use the default value. For this reason, -2 is now
used for blockHeightLagThreshold to indicate that only
up-to-date peers are to be used and -1 is used for
reconnectBlockHeightLagThreshold to indicate that the
disconnect feature should be disabled.

Change-Id: I03d667548a2649bb719ae756bec59bf533548c57
Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
  • Loading branch information
bstasyszyn committed Oct 19, 2018
1 parent 0ff6dad commit ca9ef66
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 71 deletions.
51 changes: 49 additions & 2 deletions pkg/common/providers/fab/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,46 @@ type CertKeyPair struct {
Key []byte
}

// ResolverStrategy is the peer resolver type
type ResolverStrategy string

const (
// BalancedStrategy is a peer resolver strategy that chooses peers based on a configured load balancer
BalancedStrategy ResolverStrategy = "Balanced"

// MinBlockHeightStrategy is a peer resolver strategy that chooses the best peer according to a block height lag threshold.
// The maximum block height of all peers is determined and the peers whose block heights are under the maximum height but above
// a provided "lag" threshold are load balanced. The other peers are not considered.
MinBlockHeightStrategy ResolverStrategy = "MinBlockHeight"

// PreferOrgStrategy is a peer resolver strategy that determines which peers are suitable based on block height lag threshold,
// although will prefer the peers in the current org (as long as their block height is above a configured threshold).
// If none of the peers from the current org are suitable then a peer from another org is chosen.
PreferOrgStrategy ResolverStrategy = "PreferOrg"
)

// MinBlockHeightResolverMode specifies the behaviour of the MinBlockHeight resolver strategy.
type MinBlockHeightResolverMode string

const (
// ResolveByThreshold resolves to peers based on block height lag threshold.
ResolveByThreshold MinBlockHeightResolverMode = "ResolveByThreshold"

// ResolveLatest resolves to peers with the most up-to-date block height
ResolveLatest MinBlockHeightResolverMode = "ResolveLatest"
)

// EnabledDisabled specifies whether or not a feature is enabled
type EnabledDisabled string

const (
// Enabled indicates that the feature is enabled.
Enabled EnabledDisabled = "Enabled"

// Disabled indicates that the feature is disabled.
Disabled EnabledDisabled = "Disabled"
)

// EventServicePolicy specifies the policy for the event service
type EventServicePolicy struct {
// ResolverStrategy returns the peer resolver strategy to use when connecting to a peer
Expand All @@ -152,13 +192,20 @@ type EventServicePolicy struct {
// Balancer is the balancer to use when choosing a peer to connect to
Balancer BalancerType

// MinBlockHeightResolverMode specifies the behaviour of the MinBlockHeight resolver. Note that this
// parameter is used when ResolverStrategy is either MinBlockHeightStrategy or PreferOrgStrategy.
// ResolveByThreshold (default): resolves to peers based on block height lag threshold, as specified by BlockHeightLagThreshold.
// MinBlockHeightResolverMode: then only the peers with the latest block heights are chosen.
MinBlockHeightResolverMode MinBlockHeightResolverMode

// BlockHeightLagThreshold returns the block height lag threshold. This value is used for choosing a peer
// to connect to. If a peer is lagging behind the most up-to-date peer by more than the given number of
// blocks then it will be excluded from selection.
// If set to 0 then only the most up-to-date peers are considered.
// If set to -1 then all peers (regardless of block height) are considered for selection.
BlockHeightLagThreshold int

// PeerMonitor indicates whether or not to enable the peer monitor.
PeerMonitor EnabledDisabled

// ReconnectBlockHeightLagThreshold - if >0 then the event client will disconnect from the peer if the peer's
// block height falls behind the specified number of blocks and will reconnect to a better performing peer.
// If set to 0 (default) then the peer will not disconnect based on block height.
Expand Down
18 changes: 0 additions & 18 deletions pkg/common/providers/fab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,6 @@ type EndpointConfig interface {
CryptoConfigPath() string
}

// ResolverStrategy is the peer resolver type
type ResolverStrategy string

const (
// BalancedStrategy is a peer resolver strategy that chooses peers based on a configured load balancer
BalancedStrategy ResolverStrategy = "Balanced"

// MinBlockHeightStrategy is a peer resolver strategy that chooses the best peer according to a block height lag threshold.
// The maximum block height of all peers is determined and the peers whose block heights are under the maximum height but above
// a provided "lag" threshold are load balanced. The other peers are not considered.
MinBlockHeightStrategy ResolverStrategy = "MinBlockHeight"

// PreferOrgStrategy is a peer resolver strategy that determines which peers are suitable based on block height lag threshold,
// although will prefer the peers in the current org (as long as their block height is above a configured threshold).
// If none of the peers from the current org are suitable then a peer from another org is chosen.
PreferOrgStrategy ResolverStrategy = "PreferOrg"
)

// TimeoutType enumerates the different types of outgoing connections
type TimeoutType int

Expand Down
16 changes: 10 additions & 6 deletions pkg/core/config/testdata/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ channels:
#to a lower priority list of peers which will be sorted according to block height.
#Note: This property only applies to BlockHeightPriority sorter.
BlockHeightLagThreshold: 5

#[Optional] options for event service
eventService:
# [Optional] resolverStrategy specifies the peer resolver strategy to use when connecting to a peer
Expand All @@ -153,28 +154,31 @@ channels:
# Balanced:
# Chooses peers using the configured balancer.
resolverStrategy: MinBlockHeight

# [Optional] balancer is the balancer to use when choosing a peer to connect to
# Possible values: [Random (default), RoundRobin]

balancer: RoundRobin

# [Optional] blockHeightLagThreshold sets the block height lag threshold. This value is used for choosing a peer
# to connect to. If a peer is lagging behind the most up-to-date peer by more than the given number of
# blocks then it will be excluded from selection.
# If set to 0 then only the most up-to-date peers are considered.
# If set to -1 then all peers (regardless of block height) are considered for selection.
# Note that this parameter is applicable only when minBlockHeightResolverMode is set to ResolveByThreshold.
# Default: 5
blockHeightLagThreshold: 4
# [Optional] reconnectBlockHeightLagThreshold - if >0 then the event client will disconnect from the peer if the peer's

# [Optional] reconnectBlockHeightLagThreshold - the event client will disconnect from the peer if the peer's
# block height falls behind the specified number of blocks and will reconnect to a better performing peer.
# If set to 0 then this feature is disabled.
# Note that this parameter is only applicable if peerMonitor is set to Enabled (default).
# Default: 10
# NOTES:
# - peerMonitorPeriod must be >0 to enable this feature
# - Setting this value too low may cause the event client to disconnect/reconnect too frequently, thereby
# affecting performance.
reconnectBlockHeightLagThreshold: 8

# [Optional] peerMonitorPeriod is the period in which the connected peer is monitored to see if
# the event client should disconnect from it and reconnect to another peer.
# Default: 0 (disabled)
# Default: 5s
peerMonitorPeriod: 6s

# multi-org test channel
Expand Down
24 changes: 21 additions & 3 deletions pkg/core/config/testdata/config_test_embedded_pems.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ channels:
# [Optional] blockHeightLagThreshold sets the block height lag threshold. This value is used for choosing a peer
# to connect to. If a peer is lagging behind the most up-to-date peer by more than the given number of
# blocks then it will be excluded from selection.
# If set to 0 then only the most up-to-date peers are considered.
# If set to -2 then only the most up-to-date peers are considered.
# If set to -1 then all peers (regardless of block height) are considered for selection.
# Default: 5
blockHeightLagThreshold: 3
# [Optional] reconnectBlockHeightLagThreshold - if >0 then the event client will disconnect from the peer if the peer's
# block height falls behind the specified number of blocks and will reconnect to a better performing peer.
# If set to 0 then this feature is disabled.
# If set to -1 then this feature is disabled.
# Default: 10
# NOTES:
# - peerMonitorPeriod must be >0 to enable this feature
Expand Down Expand Up @@ -225,9 +225,27 @@ channels:
attempts: 4
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 8s
#[Optional] he factor by which the initial back off period is exponentially incremented
#[Optional] the factor by which the initial back off period is exponentially incremented
backoffFactor: 8.0

eventService:
# [Optional] minBlockHeightResolverMode specifies the behaviour of the MinBlockHeight resolver strategy.
# Note that this parameter is applicable only when resolverStrategy is set to MinBlockHeight or PreferOrg.
# Possible values: [ResolveByThreshold (default), ResolveLatest]
#
# ResolveByThreshold:
# Chooses peers based on block height lag threshold.
# ResolveLatest:
# Chooses only the peers with the most up-to-date block height.
minBlockHeightResolverMode: ResolveLatest

# [Optional] peerMonitor indicates whether or not a peer monitor should be enabled in order to monitor
# the block height of the connected peer. In the case of MinBlockHeight and PreferOrg strategy, the event client
# will disconnect from the peer if its block height falls below the specified threshold.
# Possible values: [Enabled, Disabled]
# Default: Enabled for MinBlockHeight and PreferOrg strategy; Disabled for Balanced strategy
peerMonitor: Disabled

# multi-org test channel
orgchannel:

Expand Down
30 changes: 25 additions & 5 deletions pkg/core/config/testdata/template/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ channels:
# #to a lower priority list of peers which will be sorted according to block height.
# #Note: This property only applies to BlockHeightPriority sorter.
# BlockHeightLagThreshold: 5

# #[Optional] options for event service
# eventService:
# # [Optional] resolverStrategy specifies the peer resolver strategy to use when connecting to a peer
Expand All @@ -195,28 +196,47 @@ channels:
# # Balanced:
# # Chooses peers using the configured balancer.
# resolverStrategy: PreferOrg

# # [Optional] minBlockHeightResolverMode specifies the behaviour of the MinBlockHeight resolver strategy.
# # Note that this parameter is applicable only when resolverStrategy is set to MinBlockHeight or PreferOrg.
# # Possible values: [ResolveByThreshold (default), ResolveLatest]
# #
# # ResolveByThreshold:
# # Chooses peers based on block height lag threshold.
# # ResolveLatest:
# # Chooses only the peers with the most up-to-date block height.
# minBlockHeightResolverMode: ResolveByThreshold
#
# # [Optional] balancer is the balancer to use when choosing a peer to connect to
# # Possible values: [Random (default), RoundRobin]
# balancer: Random
# # [Optional] blockHeightLagThreshold sets the block height lag threshold. This value is used for choosing a peer
# # to connect to. If a peer is lagging behind the most up-to-date peer by more than the given number of
# # blocks then it will be excluded from selection.
# # If set to 0 then only the most up-to-date peers are considered.
# # If set to -1 then all peers (regardless of block height) are considered for selection.
# # Note that this parameter is applicable only when minBlockHeightResolverMode is set to ResolveByThreshold.
# # Default: 5
# blockHeightLagThreshold: 5
# # [Optional] reconnectBlockHeightLagThreshold - if >0 then the event client will disconnect from the peer if the peer's

# # [Optional] reconnectBlockHeightLagThreshold - the event client will disconnect from the peer if the peer's
# # block height falls behind the specified number of blocks and will reconnect to a better performing peer.
# # If set to 0 then this feature is disabled.
# # Note that this parameter is only applicable if peerMonitor is set to Enabled (default).
# # Default: 10
# # NOTES:
# # - peerMonitorPeriod must be >0 to enable this feature
# # - Setting this value too low may cause the event client to disconnect/reconnect too frequently, thereby
# # affecting performance.
# reconnectBlockHeightLagThreshold: 10

# # [Optional] peerMonitor indicates whether or not a peer monitor should be enabled in order to monitor
# # the block height of the connected peer. In the case of MinBlockHeight and PreferOrg strategy, the event client
# # will disconnect from the peer if its block height falls below the specified threshold.
# # Possible values: [Enabled, Disabled]
# # Default: Enabled for MinBlockHeight and PreferOrg strategy; Disabled for Balanced strategy
# peerMonitor: Enabled

# # [Optional] peerMonitorPeriod is the period in which the connected peer is monitored to see if
# # the event client should disconnect from it and reconnect to another peer.
# # Default: 0 (disabled)
# # Default: 5s
# peerMonitorPeriod: 5s

# sample channel with channel matcher (sample*channel will return ch1 config where * can be any word or '')
Expand Down
2 changes: 2 additions & 0 deletions pkg/fab/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ type SelectionPolicy struct {
// EventServicePolicy specifies the policy for the event service
type EventServicePolicy struct {
ResolverStrategy string
MinBlockHeightResolverMode string
Balancer BalancerType
BlockHeightLagThreshold int
PeerMonitor string
ReconnectBlockHeightLagThreshold int
PeerMonitorPeriod time.Duration
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/fab/default_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestDefaultChannelWithNoDefaultChannelConfiguredAndWithMatchers(t *testing.

}

func TestMissingDiscoveryPolicesInfo(t *testing.T) {
func TestMissingPolicesInfo(t *testing.T) {

// Default channel and no channel matchers test
defaultChannelBackend, err := config.FromFile(configEmbeddedUsersTestFilePath)()
Expand Down Expand Up @@ -166,6 +166,24 @@ func TestMissingDiscoveryPolicesInfo(t *testing.T) {
assert.Equal(t, defChConfig.Policies.QueryChannelConfig.RetryOpts.InitialBackoff, chConfig.Policies.QueryChannelConfig.RetryOpts.InitialBackoff)
assert.Equal(t, defChConfig.Policies.QueryChannelConfig.RetryOpts.BackoffFactor, chConfig.Policies.QueryChannelConfig.RetryOpts.BackoffFactor)
assert.Equal(t, defChConfig.Policies.QueryChannelConfig.RetryOpts.MaxBackoff, chConfig.Policies.QueryChannelConfig.RetryOpts.MaxBackoff)

assert.Equal(t, defChConfig.Policies.EventService.Balancer, chConfig.Policies.EventService.Balancer)
assert.Equal(t, defChConfig.Policies.EventService.MinBlockHeightResolverMode, chConfig.Policies.EventService.MinBlockHeightResolverMode)
assert.Equal(t, defChConfig.Policies.EventService.BlockHeightLagThreshold, chConfig.Policies.EventService.BlockHeightLagThreshold)
assert.Equal(t, defChConfig.Policies.EventService.PeerMonitorPeriod, chConfig.Policies.EventService.PeerMonitorPeriod)
assert.Equal(t, defChConfig.Policies.EventService.PeerMonitor, chConfig.Policies.EventService.PeerMonitor)
assert.Equal(t, defChConfig.Policies.EventService.ReconnectBlockHeightLagThreshold, chConfig.Policies.EventService.ReconnectBlockHeightLagThreshold)
assert.Equal(t, defChConfig.Policies.EventService.ResolverStrategy, chConfig.Policies.EventService.ResolverStrategy)

chConfig = endpointConfig.ChannelConfig("mychannel")
assert.NotNil(t, chConfig)

assert.Equal(t, fab.ResolveLatest, chConfig.Policies.EventService.MinBlockHeightResolverMode)
assert.Equal(t, fab.Disabled, chConfig.Policies.EventService.PeerMonitor)
assert.Equal(t, defChConfig.Policies.EventService.Balancer, chConfig.Policies.EventService.Balancer)
assert.Equal(t, defChConfig.Policies.EventService.PeerMonitorPeriod, chConfig.Policies.EventService.PeerMonitorPeriod)
assert.Equal(t, defChConfig.Policies.EventService.ReconnectBlockHeightLagThreshold, chConfig.Policies.EventService.ReconnectBlockHeightLagThreshold)
assert.Equal(t, defChConfig.Policies.EventService.ResolverStrategy, chConfig.Policies.EventService.ResolverStrategy)
}

func TestMissingPartialChannelPoliciesInfo(t *testing.T) {
Expand Down
Loading

0 comments on commit ca9ef66

Please sign in to comment.