Skip to content

Commit

Permalink
[FAB-7610] Print orderer parameters during boot time.
Browse files Browse the repository at this point in the history
Change-Id: I11e66a55ebd526a36e310036891ec9f3204a8073
Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
  • Loading branch information
guoger committed Jan 10, 2018
1 parent 91340fe commit 130dfef
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
57 changes: 57 additions & 0 deletions common/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"math/big"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -193,3 +194,59 @@ func ConcatenateBytes(data ...[]byte) []byte {
}
return result
}

// `flatten` recursively retrieves every leaf node in a struct in depth-first fashion
// and aggregate the results into given string slice with format: "path.to.leaf = value"
// in the order of definition. Root name is ignored in the path. This helper function is
// useful to pretty-print a struct, such as configs.
// for example, given data structure:
// A{
// B{
// C: "foo",
// D: 42,
// },
// E: nil,
// }
// it should yield a slice of string containing following items:
// [
// "B.C = \"foo\"",
// "B.D = 42",
// "E =",
// ]
func Flatten(i interface{}) []string {
var res []string
flatten("", &res, reflect.ValueOf(i))
return res
}

const DELIMITER = "."

func flatten(k string, m *[]string, v reflect.Value) {
delimiter := DELIMITER
if k == "" {
delimiter = ""
}

switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
*m = append(*m, fmt.Sprintf("%s =", k))
return
}
flatten(k, m, v.Elem())
case reflect.Struct:
if x, ok := v.Interface().(fmt.Stringer); ok {
*m = append(*m, fmt.Sprintf("%s = %v", k, x))
return
}

for i := 0; i < v.NumField(); i++ {
flatten(k+delimiter+v.Type().Field(i).Name, m, v.Field(i))
}
case reflect.String:
// It is useful to quote string values
*m = append(*m, fmt.Sprintf("%s = \"%s\"", k, v))
default:
*m = append(*m, fmt.Sprintf("%s = %v", k, v))
}
}
45 changes: 45 additions & 0 deletions common/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package util

import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
"time"

"github.com/op/go-logging"
"github.com/stretchr/testify/assert"
)

func TestComputeSHA256(t *testing.T) {
Expand Down Expand Up @@ -191,3 +194,45 @@ func TestMetadataSignatureBytesNil(t *testing.T) {
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
}
}

type A struct {
s string
}

type B struct {
A A
i int
X string
}

type C struct{}

type D struct {
B B
c *C
}

func (a A) String() string {
return fmt.Sprintf("I'm '%s'", a.s)
}

func TestFlattenStruct(t *testing.T) {
d := &D{
B: B{
A: A{
s: "foo",
},
i: 42,
X: "bar ",
},
c: nil,
}

var x []string
flatten("", &x, reflect.ValueOf(d))
assert.Equal(t, 4, len(x), "expect 3 items")
assert.Equal(t, x[0], "B.A = I'm 'foo'")
assert.Equal(t, x[1], "B.i = 42")
assert.Equal(t, x[2], "B.X = \"bar \"")
assert.Equal(t, x[3], "c =")
}
13 changes: 13 additions & 0 deletions orderer/common/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package server

import (
"bytes"
"fmt"
"io/ioutil"
"net"
Expand Down Expand Up @@ -35,6 +36,7 @@ import (
"github.com/hyperledger/fabric/protos/utils"

"github.com/hyperledger/fabric/common/localmsp"
"github.com/hyperledger/fabric/common/util"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/orderer/common/performance"
"github.com/op/go-logging"
Expand Down Expand Up @@ -72,6 +74,7 @@ func Main() {
initializeLoggingLevel(conf)
initializeLocalMsp(conf)

prettyPrintStruct(conf)
Start(fullCmd, conf)
}

Expand Down Expand Up @@ -341,3 +344,13 @@ func updateTrustedRoots(srv comm.GRPCServer, rootCASupport *comm.CASupport,
}
}
}

func prettyPrintStruct(i interface{}) {
params := util.Flatten(i)
var buffer bytes.Buffer
for i := range params {
buffer.WriteString("\n\t")
buffer.WriteString(params[i])
}
logger.Infof("Orderer config values:%s\n", buffer.String())
}

0 comments on commit 130dfef

Please sign in to comment.