Skip to content

Commit

Permalink
Merge "[FAB-9551] New config object for ChaincodeSupport"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Yellick authored and Gerrit Code Review committed Apr 25, 2018
2 parents 01afac3 + 1f3ec39 commit 8062fa9
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 104 deletions.
92 changes: 17 additions & 75 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ package chaincode

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/container"
"github.com/hyperledger/fabric/core/container/ccintf"
pb "github.com/hyperledger/fabric/protos/peer"
logging "github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/net/context"
Expand Down Expand Up @@ -70,99 +66,49 @@ func (cs *ChaincodeSupport) preLaunchSetup(chaincode string, notify chan bool) {

// NewChaincodeSupport creates a new ChaincodeSupport instance
func NewChaincodeSupport(
config *Config,
peerAddress string,
userrunsCC bool,
ccstartuptimeout time.Duration,
caCert []byte,
certGenerator CertGenerator,
) *ChaincodeSupport {
ccprovider.SetChaincodesPath(ccprovider.GetCCsPath())
pnid := viper.GetString("peer.networkId")
pid := viper.GetString("peer.id")

theChaincodeSupport := &ChaincodeSupport{
caCert: caCert,
peerNetworkID: pnid,
peerID: pid,
cs := &ChaincodeSupport{
caCert: caCert,
peerNetworkID: config.PeerNetworkID,
peerID: config.PeerID,
userRunsCC: userrunsCC,
ccStartupTimeout: ccstartuptimeout,
keepalive: config.Keepalive,
executetimeout: config.ExecuteTimeout,
runningChaincodes: &runningChaincodes{
chaincodeMap: make(map[string]*chaincodeRTEnv),
launchStarted: make(map[string]bool),
},
}

theChaincodeSupport.userRunsCC = userrunsCC
theChaincodeSupport.ccStartupTimeout = ccstartuptimeout
theChaincodeSupport.peerTLS = viper.GetBool("peer.tls.enabled")

kadef := 0
if ka := viper.GetString("chaincode.keepalive"); ka == "" {
theChaincodeSupport.keepalive = time.Duration(kadef) * time.Second
} else {
t, terr := strconv.Atoi(ka)
if terr != nil {
chaincodeLogger.Errorf("Invalid keepalive value %s (%s) defaulting to %d", ka, terr, kadef)
t = kadef
} else if t <= 0 {
chaincodeLogger.Debugf("Turn off keepalive(value %s)", ka)
t = kadef
}
theChaincodeSupport.keepalive = time.Duration(t) * time.Second
}

//default chaincode execute timeout is 30 secs
execto := time.Duration(30) * time.Second
if eto := viper.GetDuration("chaincode.executetimeout"); eto <= time.Duration(1)*time.Second {
chaincodeLogger.Errorf("Invalid execute timeout value %s (should be at least 1s); defaulting to %s", eto, execto)
} else {
chaincodeLogger.Debugf("Setting execute timeout value to %s", eto)
execto = eto
}

theChaincodeSupport.executetimeout = execto

viper.SetEnvPrefix("CORE")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)

theChaincodeSupport.chaincodeLogLevel = getLogLevelFromViper("level")
theChaincodeSupport.shimLogLevel = getLogLevelFromViper("shim")
theChaincodeSupport.logFormat = viper.GetString("chaincode.logging.format")

// Keep TestQueries working
if !theChaincodeSupport.peerTLS {
if !config.TLSEnabled {
certGenerator = nil
}

theChaincodeSupport.ContainerRuntime = &ContainerRuntime{
cs.ContainerRuntime = &ContainerRuntime{
CertGenerator: certGenerator,
Processor: ProcessFunc(container.VMCProcess),
CACert: caCert,
PeerAddress: peerAddress,
PeerID: pid,
PeerNetworkID: pnid,
PeerID: config.PeerID,
PeerNetworkID: config.PeerNetworkID,
CommonEnv: []string{
"CORE_CHAINCODE_LOGGING_LEVEL=" + theChaincodeSupport.chaincodeLogLevel,
"CORE_CHAINCODE_LOGGING_SHIM=" + theChaincodeSupport.shimLogLevel,
"CORE_CHAINCODE_LOGGING_FORMAT=" + theChaincodeSupport.logFormat,
"CORE_CHAINCODE_LOGGING_LEVEL=" + config.LogLevel,
"CORE_CHAINCODE_LOGGING_SHIM=" + config.ShimLogLevel,
"CORE_CHAINCODE_LOGGING_FORMAT=" + config.LogFormat,
},
}

return theChaincodeSupport
}

// getLogLevelFromViper gets the chaincode container log levels from viper
func getLogLevelFromViper(module string) string {
levelString := viper.GetString("chaincode.logging." + module)
_, err := logging.LogLevel(levelString)

if err == nil {
chaincodeLogger.Debugf("CORE_CHAINCODE_%s set to level %s", strings.ToUpper(module), levelString)
} else {
chaincodeLogger.Warningf("CORE_CHAINCODE_%s has invalid log level %s. defaulting to %s", strings.ToUpper(module), levelString, flogging.DefaultLevel())
levelString = flogging.DefaultLevel()
}
return levelString
return cs
}

// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
Expand All @@ -174,12 +120,8 @@ type ChaincodeSupport struct {
peerNetworkID string
peerID string
keepalive time.Duration
chaincodeLogLevel string
shimLogLevel string
logFormat string
executetimeout time.Duration
userRunsCC bool
peerTLS bool
ContainerRuntime Runtime
}

Expand Down
38 changes: 13 additions & 25 deletions core/chaincode/chaincode_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func initMockPeer(chainIDs ...string) error {
ccStartupTimeout := time.Duration(10) * time.Second
ca, _ := accesscontrol.NewCA()
certGenerator := accesscontrol.NewAuthenticator(ca)
theChaincodeSupport = NewChaincodeSupport("0.0.0.0:7052", false, ccStartupTimeout, ca.CertBytes(), certGenerator)
theChaincodeSupport = NewChaincodeSupport(GlobalConfig(), "0.0.0.0:7052", false, ccStartupTimeout, ca.CertBytes(), certGenerator)
SideEffectInitialize(theChaincodeSupport)
theChaincodeSupport.executetimeout = time.Duration(1) * time.Second

Expand Down Expand Up @@ -893,12 +893,9 @@ func TestLaunchAndWaitSuccess(t *testing.T) {
}

newCCSupport := &ChaincodeSupport{
peerTLS: false,
chaincodeLogLevel: "debug",
shimLogLevel: "info",
ccStartupTimeout: time.Duration(10) * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
ccStartupTimeout: time.Duration(10) * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
runningChaincodes: &runningChaincodes{
chaincodeMap: make(map[string]*chaincodeRTEnv),
launchStarted: make(map[string]bool),
Expand Down Expand Up @@ -928,12 +925,9 @@ func TestLaunchAndWaitTimeout(t *testing.T) {
}

newCCSupport := &ChaincodeSupport{
peerTLS: false,
chaincodeLogLevel: "debug",
shimLogLevel: "info",
ccStartupTimeout: 500 * time.Millisecond,
peerNetworkID: "networkID",
peerID: "peerID",
ccStartupTimeout: 500 * time.Millisecond,
peerNetworkID: "networkID",
peerID: "peerID",
runningChaincodes: &runningChaincodes{
chaincodeMap: make(map[string]*chaincodeRTEnv),
launchStarted: make(map[string]bool),
Expand Down Expand Up @@ -962,12 +956,9 @@ func TestLaunchAndWaitNotificationError(t *testing.T) {
}

newCCSupport := &ChaincodeSupport{
peerTLS: false,
chaincodeLogLevel: "debug",
shimLogLevel: "info",
ccStartupTimeout: 10 * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
ccStartupTimeout: 10 * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
runningChaincodes: &runningChaincodes{
chaincodeMap: make(map[string]*chaincodeRTEnv),
launchStarted: make(map[string]bool),
Expand Down Expand Up @@ -995,12 +986,9 @@ func TestLaunchAndWaitLaunchError(t *testing.T) {
}

newCCSupport := &ChaincodeSupport{
peerTLS: false,
chaincodeLogLevel: "debug",
shimLogLevel: "info",
ccStartupTimeout: time.Duration(10) * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
ccStartupTimeout: time.Duration(10) * time.Second,
peerNetworkID: "networkID",
peerID: "peerID",
runningChaincodes: &runningChaincodes{
chaincodeMap: make(map[string]*chaincodeRTEnv),
launchStarted: make(map[string]bool),
Expand Down
86 changes: 86 additions & 0 deletions core/chaincode/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package chaincode

import (
"strconv"
"strings"
"time"

"github.com/hyperledger/fabric/common/flogging"
logging "github.com/op/go-logging"
"github.com/spf13/viper"
)

const (
defaultExecutionTimeout = 30 * time.Second
minimumStartupTimeout = 5 * time.Second
)

type Config struct {
PeerNetworkID string
PeerID string
TLSEnabled bool
Keepalive time.Duration
ExecuteTimeout time.Duration
StartupTimeout time.Duration
LogFormat string
LogLevel string
ShimLogLevel string
}

func GlobalConfig() *Config {
c := &Config{}
c.load()
return c
}

func (c *Config) load() {
viper.SetEnvPrefix("CORE")
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)

c.PeerNetworkID = viper.GetString("peer.networkId")
c.PeerID = viper.GetString("peer.id")
c.TLSEnabled = viper.GetBool("peer.tls.enabled")

c.Keepalive = toSeconds(viper.GetString("chaincode.keepalive"), 0)
c.ExecuteTimeout = viper.GetDuration("chaincode.executetimeout")
if c.ExecuteTimeout < time.Second {
c.ExecuteTimeout = defaultExecutionTimeout
}
c.StartupTimeout = viper.GetDuration("chaincode.startuptimeout")
if c.StartupTimeout < minimumStartupTimeout {
c.StartupTimeout = 5 * time.Second
}

c.LogFormat = viper.GetString("chaincode.logging.format")
c.LogLevel = getLogLevelFromViper("chaincode.logging.level")
c.ShimLogLevel = getLogLevelFromViper("chaincode.logging.shim")
}

func toSeconds(s string, def int) time.Duration {
seconds, err := strconv.Atoi(s)
if err != nil {
return time.Duration(def) * time.Second
}

return time.Duration(seconds) * time.Second
}

// getLogLevelFromViper gets the chaincode container log levels from viper
func getLogLevelFromViper(key string) string {
levelString := viper.GetString(key)
_, err := logging.LogLevel(levelString)
if err != nil {
chaincodeLogger.Warningf("%s has invalid log level %s. defaulting to %s", key, levelString, flogging.DefaultLevel())
levelString = flogging.DefaultLevel()
}

return levelString
}
Loading

0 comments on commit 8062fa9

Please sign in to comment.