Skip to content

Commit

Permalink
[FABG-737] Default Channel: Channel Policies
Browse files Browse the repository at this point in the history
Change-Id: I7cdb4f661c14a14750251b1d18c12d68e6a86d85
Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
  • Loading branch information
sandrask committed Aug 24, 2018
1 parent 3e8f41f commit 31c2fa4
Show file tree
Hide file tree
Showing 18 changed files with 632 additions and 126 deletions.
2 changes: 1 addition & 1 deletion pkg/client/common/discovery/dynamicdiscovery/chservice.go
Expand Up @@ -87,7 +87,7 @@ func (s *ChannelService) getTargets(ctx contextAPI.Client) ([]fab.PeerConfig, er
}

//pick number of peers given in channel policy
return random.PickRandomNPeerConfigs(chPeers, chConfig.Policies.QueryChannelConfig.QueryDiscovery), nil
return random.PickRandomNPeerConfigs(chPeers, chConfig.Policies.Discovery.MaxTargets), nil
}

// evaluate validates the responses and returns the peers
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/common/random/random_test.go
Expand Up @@ -134,3 +134,16 @@ func verifyDuplicates(t *testing.T, chPeers []pfab.PeerConfig) {
seen[v.URL] = true
}
}

func TestPickMorePeersThanChannelPeers(t *testing.T) {

// create 2 peers
allChPeers := createNChannelPeers(2)

// Ask for 4 peers
result := PickRandomNPeerConfigs(allChPeers, 4)
assert.NotNil(t, result)
assert.NotEmpty(t, result)
assert.Equal(t, 2, len(result))
verifyDuplicates(t, result)
}
Expand Up @@ -283,7 +283,7 @@ func (s *Service) getTargets(ctx contextAPI.Client) ([]fab.PeerConfig, error) {
}

//pick number of peers based on channel policy
return random.PickRandomNPeerConfigs(chpeers, chConfig.Policies.QueryChannelConfig.QueryDiscovery), nil
return random.PickRandomNPeerConfigs(chpeers, chConfig.Policies.Discovery.MaxTargets), nil
}

func asChaincodeInterests(chaincodes []*fab.ChaincodeCall) *discovery.ChaincodeInterest {
Expand Down
17 changes: 12 additions & 5 deletions pkg/common/providers/fab/network.go
Expand Up @@ -35,14 +35,21 @@ type ChannelEndpointConfig struct {
type ChannelPolicies struct {
//Policy for querying channel block
QueryChannelConfig QueryChannelConfigPolicy
Discovery DiscoveryPolicy
}

//QueryChannelConfigPolicy defines opts for channelConfigBlock
//QueryChannelConfigPolicy defines policy for channelConfigBlock
type QueryChannelConfigPolicy struct {
MinResponses int
MaxTargets int
QueryDiscovery int
RetryOpts retry.Opts
MinResponses int
MaxTargets int
RetryOpts retry.Opts
}

//DiscoveryPolicy defines policy for discovery
type DiscoveryPolicy struct {
MinResponses int
MaxTargets int
RetryOpts retry.Opts
}

// PeerChannelConfig defines the peer capabilities
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/config/config.go
Expand Up @@ -60,7 +60,7 @@ func FromFile(name string, opts ...Option) core.ConfigProvider {
// If a config file is found, read it in.
err = backend.configViper.MergeInConfig()
if err != nil {
return nil, errors.Wrap(err, "loading config file failed")
return nil, errors.Wrapf(err, "loading config file failed: %s", name)
}

setLogLevel(backend)
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/config/defbackend.go
Expand Up @@ -39,7 +39,7 @@ func (c *defConfigBackend) loadTemplateConfig() error {
c.configViper.AddConfigPath(pathvar.Subst(templatePath))
err := c.configViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return errors.Wrap(err, "loading config file failed")
return errors.Wrapf(err, "loading config from template failed: %s", templatePath)
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/core/config/lookup/lookup_test.go
Expand Up @@ -211,7 +211,7 @@ func TestUnmarshal(t *testing.T) {
networkConfig := networkConfig{}
testLookup.UnmarshalKey("channels", &networkConfig.Channels)

assert.Equal(t, len(networkConfig.Channels), 5)
assert.Equal(t, len(networkConfig.Channels), 6)
assert.Equal(t, len(networkConfig.Channels["mychannel"].Peers), 1)
assert.Equal(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MinResponses, 1)
assert.Equal(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MaxTargets, 1)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestUnmarshalWithMultipleBackend(t *testing.T) {
assert.True(t, networkConfig.Client.Organization == "org1")

//Channel
assert.Equal(t, len(networkConfig.Channels), 5)
assert.Equal(t, len(networkConfig.Channels), 6)
assert.Equal(t, len(networkConfig.Channels["mychannel"].Peers), 1)
assert.Equal(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MinResponses, 1)
assert.Equal(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MaxTargets, 1)
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestUnmarshalWithHookFunc(t *testing.T) {
testLookup.UnmarshalKey("channels", &networkConfig.Channels, WithUnmarshalHookFunction(setTrueDefaultForPeerChannelConfig()))

//Test if mandatory hook func is working as expected
assert.True(t, len(networkConfig.Channels) == 5)
assert.True(t, len(networkConfig.Channels) == 6)
assert.True(t, len(networkConfig.Channels["mychannel"].Peers) == 1)
assert.True(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MinResponses == 1)
assert.True(t, networkConfig.Channels["mychannel"].Policies.QueryChannelConfig.MaxTargets == 1)
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/config/testdata/config_test.yaml
Expand Up @@ -116,9 +116,6 @@ channels:
minResponses: 1
#[Optional] channel config will be retrieved for these number of random targets
maxTargets: 1
#[Optional] number of peers to be queried for discovery = Min(queryDiscovery, len(channel.peers in config)), by default queryDiscovery=2
queryDiscovery: 1
#[Optional] retry options for query config block
retryOpts:
#[Optional] number of retry attempts
attempts: 5
Expand Down
82 changes: 75 additions & 7 deletions pkg/core/config/testdata/config_test_embedded_pems.yaml
Expand Up @@ -64,6 +64,68 @@ client:
# section.
#
channels:

# Default channel is used if channel configuration is missing or if channel configuration is missing some info
_default:

# [Optional]. List of bootstrap peers from participating orgs.
peers:
peer0.org2.example.com:
# [Optional]. will this peer be sent transaction proposals for endorsement? The peer must
# have the chaincode installed. The app can also use this property to decide which peers
# to send the chaincode install request. Default: true
endorsingPeer: true

# [Optional]. will this peer be sent query proposals? The peer must have the chaincode
# installed. The app can also use this property to decide which peers to send the
# chaincode install request. Default: true
chaincodeQuery: true

# [Optional]. will this peer be sent query proposals that do not require chaincodes, like
# queryBlock(), queryTransaction(), etc. Default: true
ledgerQuery: true

# [Optional]. will this peer be the target of the SDK's listener registration? All peers can
# produce events but the app typically only needs to connect to one to listen to events.
# Default: true
eventSource: true

# [Optional]. Orderer used by this channel
orderers:
- orderer.example.com

# [Optional]. The application will use these option when using services such as discovery etc.
policies:
discovery:
#[Optional] Maximum number of random targets
maxTargets: 3
#[Optional] retry options
retryOpts:
#[Optional] number of retry attempts
attempts: 2
#[Optional] the back off interval for the first retry attempt
initialBackoff: 2s
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 7s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 2.0
#[Optional] options for retrieving channel configuration blocks
queryChannelConfig:
#[Optional] min number of success responses (from targets/peers)
minResponses: 1
#[Optional] Maximum number of random targets
maxTargets: 3
#[Optional] retry options
retryOpts:
#[Optional] number of retry attempts
attempts: 3
#[Optional] the back off interval for the first retry attempt
initialBackoff: 1s
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 9s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 3.0

# name of the channel
mychannel:
# Required. list of orderers designated by the application to use for transactions on this
Expand Down Expand Up @@ -107,19 +169,25 @@ channels:
#[Optional] options for retrieving channel configuration blocks
queryChannelConfig:
#[Optional] min number of success responses (from targets/peers)
minResponses: 1
minResponses: 8
#[Optional] channel config will be retrieved for these number of random targets
maxTargets: 1
#[Optional] retry options for query config block
maxTargets: 8
retryOpts:
#[Optional] number of retry attempts
#[Required] number of retry attempts
# If retryOpts section is defined then attempts must be specified
attempts: 5
#[Optional] the back off interval for the first retry attempt
initialBackoff: 500ms
initialBackoff: 5s
discovery:
#[Optional] retry options
retryOpts:
#[Required] number of retry attempts
# If retryOpts section is defined then attempts must be specified
attempts: 4
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 5s
maxBackoff: 8s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 2.0
backoffFactor: 8.0

# multi-org test channel
orgchannel:
Expand Down
96 changes: 94 additions & 2 deletions pkg/core/config/testdata/config_test_entity_matchers.yaml
Expand Up @@ -76,6 +76,68 @@ client:
# section.
#
channels:

# Default channel is used if channel configuration is missing or if defined channel configuration is missing info
_default:

# [Optional]. List of bootstrap peers from participating orgs.
peers:
peer0.org2.example.com:
# [Optional]. will this peer be sent transaction proposals for endorsement? The peer must
# have the chaincode installed. The app can also use this property to decide which peers
# to send the chaincode install request. Default: true
endorsingPeer: true

# [Optional]. will this peer be sent query proposals? The peer must have the chaincode
# installed. The app can also use this property to decide which peers to send the
# chaincode install request. Default: true
chaincodeQuery: true

# [Optional]. will this peer be sent query proposals that do not require chaincodes, like
# queryBlock(), queryTransaction(), etc. Default: true
ledgerQuery: true

# [Optional]. will this peer be the target of the SDK's listener registration? All peers can
# produce events but the app typically only needs to connect to one to listen to events.
# Default: true
eventSource: true

# [Optional]. Orderer used by this channel
orderers:
- orderer.example.com

# [Optional]. The application will use these option when using services such as discovery etc.
policies:
discovery:
#[Optional] Maximum number of random targets
maxTargets: 3
#[Optional] retry options
retryOpts:
#[Optional] number of retry attempts
attempts: 2
#[Optional] the back off interval for the first retry attempt
initialBackoff: 2s
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 7s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 2.0
#[Optional] options for retrieving channel configuration blocks
queryChannelConfig:
#[Optional] min number of success responses (from targets/peers)
minResponses: 1
#[Optional] Maximum number of random targets
maxTargets: 3
#[Optional] retry options
retryOpts:
#[Optional] number of retry attempts
attempts: 3
#[Optional] the back off interval for the first retry attempt
initialBackoff: 1s
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 9s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 3.0

# name of the channel
mychannel:
# Required. list of orderers designated by the application to use for transactions on this
Expand Down Expand Up @@ -116,8 +178,20 @@ channels:
minResponses: 1
#[Optional] channel config will be retrieved for these number of random targets
maxTargets: 1
#[Optional] number of peers to be queried for discovery = Min(queryDiscovery, len(channel.peers in config)), by default queryDiscovery=2
queryDiscovery: 1
#[Optional] retry options for query config block
retryOpts:
#[Optional] number of retry attempts
attempts: 5
#[Optional] the back off interval for the first retry attempt
initialBackoff: 500ms
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 5s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 2.0
#[Optional] options for discovery service
discovery:
#[Optional] channel config will be retrieved for these number of random targets
maxTargets: 1
#[Optional] retry options for query config block
retryOpts:
#[Optional] number of retry attempts
Expand Down Expand Up @@ -161,6 +235,24 @@ channels:
ledgerQuery: true
eventSource: true

# [Optional]. The application can use these options to perform channel operations like retrieving channel
# config etc.
policies:
#[Optional] options for discovery service
discovery:
#[Optional] channel config will be retrieved for these number of random targets
maxTargets: 4
#[Optional] retry options for query config block
retryOpts:
#[Optional] number of retry attempts
attempts: 5
#[Optional] the back off interval for the first retry attempt
initialBackoff: 500ms
#[Optional] the maximum back off interval for any retry attempt
maxBackoff: 5s
#[Optional] he factor by which the initial back off period is exponentially incremented
backoffFactor: 2.0

ch2:

orderers:
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/config/testdata/template/config.yaml
Expand Up @@ -180,8 +180,6 @@ channels:
# minResponses: 1
# #[Optional] channel config will be retrieved for these number of random targets
# maxTargets: 1
# #[Optional] number of peers to be queried for discovery = Min(queryDiscovery, len(channel.peers in config)), by default queryDiscovery=2
# queryDiscovery: 1
# #[Optional] retry options for query config block
# retryOpts:
# #[Optional] number of retry attempts
Expand Down
16 changes: 12 additions & 4 deletions pkg/fab/api.go
Expand Up @@ -61,14 +61,22 @@ type ChannelEndpointConfig struct {
type ChannelPolicies struct {
//Policy for querying channel block
QueryChannelConfig QueryChannelConfigPolicy
//Policy for querying discovery
Discovery DiscoveryPolicy
}

//QueryChannelConfigPolicy defines opts for channelConfigBlock
type QueryChannelConfigPolicy struct {
MinResponses int
MaxTargets int
QueryDiscovery int
RetryOpts retry.Opts
MinResponses int
MaxTargets int
RetryOpts retry.Opts
}

//DiscoveryPolicy defines policy for discovery
type DiscoveryPolicy struct {
MinResponses int
MaxTargets int
RetryOpts retry.Opts
}

// PeerChannelConfig defines the peer capabilities
Expand Down

0 comments on commit 31c2fa4

Please sign in to comment.