Commit e6d904ba authored by Chongxin Luo's avatar Chongxin Luo Committed by Matthew Sykes
Browse files

[FAB-15078] Remove old configuration cache code



Remove peer configuration cache code (cache configuration
being replaced with coreConfig struct).
Add error handling for GlobalConfig() loading function.
Modified unit tests.

Change-Id: Ib80735109b4892a41b319973f0aa65e40677cd5b
Signed-off-by: default avatarChongxin Luo <Chongxin.Luo@ibm.com>
parent 3ff6312a
......@@ -65,16 +65,18 @@ type Config struct {
ChaincodePull bool
}
func GlobalConfig() *Config {
func GlobalConfig() (*Config, error) {
c := &Config{}
c.load()
return c
if err := c.load(); err != nil {
return nil, err
}
return c, nil
}
func (c *Config) load() {
func (c *Config) load() error {
preeAddress, err := getLocalAddress()
if err != nil {
panic(err)
return err
}
c.PeerAddress = preeAddress
c.PeerID = viper.GetString("peer.id")
......@@ -106,28 +108,7 @@ func (c *Config) load() {
c.VMDockerAttachStdout = viper.GetBool("vm.docker.attachStdout")
c.ChaincodePull = viper.GetBool("chaincode.pull")
}
// Is the configuration cached?
var configurationCached = false
// Cached values and error values of the computed getValidatorStreamAddress()
// and getPeerEndpoint()
var peerEndpoint *pb.PeerEndpoint
var peerEndpointError error
// Cached values of commonly used configuration constants.
// CacheConfiguration computes and caches commonly-used constants and
// computed constants as package variables. Routines which were previously
// global have been embedded here to preserve the original abstraction.
func CacheConfiguration() error {
peerEndpoint, peerEndpointError = getPeerEndpoint()
if peerEndpointError != nil {
return peerEndpointError
}
configurationCached = true
return nil
}
......@@ -165,33 +146,6 @@ func getLocalAddress() (string, error) {
}
// getPeerEndpoint returns the PeerEndpoint for this Peer instance. Affected by env:peer.addressAutoDetect
func getPeerEndpoint() (*pb.PeerEndpoint, error) {
var peerAddress string
peerAddress, err := getLocalAddress()
if err != nil {
return nil, err
}
return &pb.PeerEndpoint{Id: &pb.PeerID{Name: viper.GetString("peer.id")}, Address: peerAddress}, nil
}
// cacheConfiguration logs an error if error checks have failed.
func cacheConfiguration() {
if err := CacheConfiguration(); err != nil {
peerLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err)
}
}
//Functional forms
// GetPeerEndpoint returns peerEndpoint from cached configuration
func GetPeerEndpoint() (*pb.PeerEndpoint, error) {
if !configurationCached {
cacheConfiguration()
}
return peerEndpoint, peerEndpointError
}
// GetServerConfig returns the gRPC server configuration for the peer
func GetServerConfig() (comm.ServerConfig, error) {
secureOptions := &comm.SecureOptions{
......
......@@ -22,9 +22,18 @@ func TestCacheConfigurationNegative(t *testing.T) {
// set a bad peer.address
viper.Set("peer.addressAutoDetect", true)
viper.Set("peer.address", "testing.com")
cacheConfiguration()
err := CacheConfiguration()
_, err := GlobalConfig()
assert.Error(t, err, "Expected error for bad configuration")
viper.Set("peer.addressAutoDetect", false)
viper.Set("peer.address", "")
_, err = GlobalConfig()
assert.Error(t, err, "Expected error for bad configuration")
viper.Set("peer.address", "wrongAddress")
_, err = GlobalConfig()
assert.Error(t, err, "Expected error for bad configuration")
}
func TestConfiguration(t *testing.T) {
......@@ -95,23 +104,11 @@ func TestConfiguration(t *testing.T) {
for k, v := range test.settings {
viper.Set(k, v)
}
// reset the cache
configurationCached = false
// GetPeerEndpoint
pe, err := GetPeerEndpoint()
assert.NoError(t, err, "GetPeerEndpoint returned unexpected error")
assert.Equal(t, test.settings["peer.id"], pe.Id.Name, "GetPeerEndpoint returned the wrong peer ID")
assert.Equal(t, test.expectedPeerAddress, pe.Address, "GetPeerEndpoint returned the wrong peer address")
// now check with cached configuration
err = CacheConfiguration()
assert.NoError(t, err, "CacheConfiguration should not have returned an err")
// check functions again
// GetPeerEndpoint
pe, err = GetPeerEndpoint()
// load Config file
coreConfig, err := GlobalConfig()
assert.NoError(t, err, "GetPeerEndpoint returned unexpected error")
assert.Equal(t, test.settings["peer.id"], pe.Id.Name, "GetPeerEndpoint returned the wrong peer ID")
assert.Equal(t, test.expectedPeerAddress, pe.Address, "GetPeerEndpoint returned the wrong peer address")
assert.Equal(t, test.settings["peer.id"], coreConfig.PeerEndpoint.Id.Name, "GetPeerEndpoint returned the wrong peer ID")
assert.Equal(t, test.expectedPeerAddress, coreConfig.PeerEndpoint.Address, "GetPeerEndpoint returned the wrong peer address")
})
}
}
......@@ -233,7 +230,7 @@ func TestGetClientCertificate(t *testing.T) {
func TestGlobalConfig(t *testing.T) {
//Capture the configuration from viper
viper.Set("peer.addressAutoDetect", "false")
viper.Set("peer.addressAutoDetect", false)
viper.Set("peer.address", "localhost:8080")
viper.Set("peer.id", "testPeerID")
viper.Set("peer.localMspId", "SampleOrg")
......@@ -259,7 +256,8 @@ func TestGlobalConfig(t *testing.T) {
viper.Set("chaincode.pull", false)
coreConfig := GlobalConfig()
coreConfig, err := GlobalConfig()
assert.NoError(t, err)
assert.Equal(t, coreConfig.LocalMspID, "SampleOrg")
assert.Equal(t, coreConfig.ListenAddress, "0.0.0.0:7051")
......@@ -289,9 +287,4 @@ func TestGlobalConfig(t *testing.T) {
assert.Equal(t, coreConfig.PeerEndpoint.Id.Name, "testPeerID")
assert.Equal(t, coreConfig.PeerEndpoint.Address, "localhost:8080")
//bad peer address
viper.Set("peer.address", "")
assert.Panics(t, func() { GlobalConfig() }, "The code did not panic")
viper.Set("peer.address", "wrongAddress")
assert.Panics(t, func() { GlobalConfig() }, "The code did not panic")
}
......@@ -147,7 +147,10 @@ func serve(args []string) error {
)
//obtain coreConfiguration
coreConfig := peer.GlobalConfig()
coreConfig, err := peer.GlobalConfig()
if err != nil {
return err
}
platformRegistry := platforms.NewRegistry(platforms.SupportedPlatforms...)
......@@ -156,7 +159,7 @@ func serve(args []string) error {
}
opsSystem := newOperationsSystem()
err := opsSystem.Start()
err = opsSystem.Start()
if err != nil {
return errors.WithMessage(err, "failed to initialize operations subystems")
}
......@@ -234,15 +237,7 @@ func serve(args []string) error {
viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode)
}
if err := peer.CacheConfiguration(); err != nil {
return err
}
peerEndpoint, err := peer.GetPeerEndpoint()
if err != nil {
err = fmt.Errorf("Failed to get Peer Endpoint: %s", err)
return err
}
peerEndpoint := coreConfig.PeerEndpoint
peerHost, _, err := net.SplitHostPort(peerEndpoint.Address)
if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment