Skip to content

Commit

Permalink
Merge pull request #4686 from hashicorp/f-logger-deps
Browse files Browse the repository at this point in the history
Use StandardLogger for Raft/Serf/Memberlist/Yamux
  • Loading branch information
dadgar committed Sep 17, 2018
2 parents 07bfc85 + 58c889a commit 3a26035
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 27 deletions.
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

metrics "github.com/armon/go-metrics"
consulapi "github.com/hashicorp/consul/api"
hclog "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
consulApi "github.com/hashicorp/nomad/client/consul"
cstructs "github.com/hashicorp/nomad/client/structs"
Expand All @@ -26,7 +27,6 @@ import (

"github.com/boltdb/bolt"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/allocrunner"
"github.com/hashicorp/nomad/client/config"
Expand Down Expand Up @@ -212,16 +212,19 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic
}
}

// Create the logger
logger := cfg.Logger.ResetNamed("")

// Create the client
c := &Client{
config: cfg,
consulCatalog: consulCatalog,
consulService: consulService,
start: time.Now(),
connPool: pool.NewPool(cfg.LogOutput, clientRPCCache, clientMaxStreams, tlsWrap),
connPool: pool.NewPool(logger, clientRPCCache, clientMaxStreams, tlsWrap),
tlsWrap: tlsWrap,
streamingRpcs: structs.NewStreamingRpcRegistry(),
logger: cfg.Logger.ResetNamed("").StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
allocs: make(map[string]*allocrunner.AllocRunner),
allocUpdates: make(chan *structs.Allocation, 64),
shutdownCh: make(chan struct{}),
Expand Down
13 changes: 8 additions & 5 deletions helper/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"container/list"
"fmt"
"io"
"log"
"net"
"net/rpc"
"sync"
"sync/atomic"
"time"

hclog "github.com/hashicorp/go-hclog"
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/nomad/structs"
Expand Down Expand Up @@ -121,8 +123,8 @@ func (c *Conn) returnClient(client *StreamClient) {
type ConnPool struct {
sync.Mutex

// LogOutput is used to control logging
logOutput io.Writer
// logger is the logger to be used
logger *log.Logger

// The maximum time to keep a connection open
maxTime time.Duration
Expand Down Expand Up @@ -156,9 +158,9 @@ type ConnPool struct {
// Set maxTime to 0 to disable reaping. maxStreams is used to control
// the number of idle streams allowed.
// If TLS settings are provided outgoing connections use TLS.
func NewPool(logOutput io.Writer, maxTime time.Duration, maxStreams int, tlsWrap tlsutil.RegionWrapper) *ConnPool {
func NewPool(logger hclog.Logger, maxTime time.Duration, maxStreams int, tlsWrap tlsutil.RegionWrapper) *ConnPool {
pool := &ConnPool{
logOutput: logOutput,
logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
maxTime: maxTime,
maxStreams: maxStreams,
pool: make(map[string]*Conn),
Expand Down Expand Up @@ -335,7 +337,8 @@ func (p *ConnPool) getNewConn(region string, addr net.Addr, version int) (*Conn,

// Setup the logger
conf := yamux.DefaultConfig()
conf.LogOutput = p.logOutput
conf.LogOutput = nil
conf.Logger = p.logger

// Create a multiplexed session
session, err := yamux.Client(conn, conf)
Expand Down
4 changes: 2 additions & 2 deletions helper/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

func newTestPool(t *testing.T) *ConnPool {
w := testlog.NewWriter(t)
p := NewPool(w, 1*time.Minute, 10, nil)
l := testlog.HCLogger(t)
p := NewPool(l, 1*time.Minute, 10, nil)
return p
}

Expand Down
17 changes: 12 additions & 5 deletions nomad/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"time"

golog "log"

metrics "github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
Expand Down Expand Up @@ -47,13 +49,16 @@ const (

type rpcHandler struct {
*Server
logger log.Logger
logger log.Logger
gologger *golog.Logger
}

func newRpcHandler(s *Server) *rpcHandler {
logger := s.logger.Named("rpc")
return &rpcHandler{
Server: s,
logger: s.logger.Named("rpc"),
Server: s,
logger: logger,
gologger: logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true}),
}
}

Expand Down Expand Up @@ -207,7 +212,8 @@ func (r *rpcHandler) handleMultiplex(ctx context.Context, conn net.Conn, rpcCtx
}()

conf := yamux.DefaultConfig()
conf.LogOutput = r.config.LogOutput
conf.LogOutput = nil
conf.Logger = r.gologger
server, err := yamux.Server(conn, conf)
if err != nil {
r.logger.Error("multiplex failed to create yamux server", "error", err)
Expand Down Expand Up @@ -315,7 +321,8 @@ func (r *rpcHandler) handleMultiplexV2(ctx context.Context, conn net.Conn, rpcCt
}()

conf := yamux.DefaultConfig()
conf.LogOutput = r.config.LogOutput
conf.LogOutput = nil
conf.Logger = r.gologger
server, err := yamux.Server(conn, conf)
if err != nil {
r.logger.Error("multiplex_v2 failed to create yamux server", "error", err)
Expand Down
2 changes: 1 addition & 1 deletion nomad/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestRPC_handleMultiplexV2(t *testing.T) {

// Make two streams
conf := yamux.DefaultConfig()
conf.LogOutput = testlog.NewWriter(t)
conf.Logger = testlog.Logger(t)
session, err := yamux.Client(p1, conf)
require.Nil(err)

Expand Down
20 changes: 14 additions & 6 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,15 @@ func NewServer(config *Config, consulCatalog consul.CatalogAPI) (*Server, error)
return nil, err
}

// Create the logger
logger := config.Logger.ResetNamed("nomad")

// Create the server
s := &Server{
config: config,
consulCatalog: consulCatalog,
connPool: pool.NewPool(config.LogOutput, serverRPCCache, serverMaxStreams, tlsWrap),
logger: config.Logger.ResetNamed("nomad"),
connPool: pool.NewPool(logger, serverRPCCache, serverMaxStreams, tlsWrap),
logger: logger,
tlsWrap: tlsWrap,
rpcServer: rpc.NewServer(),
streamingRpcs: structs.NewStreamingRpcRegistry(),
Expand Down Expand Up @@ -1089,8 +1092,10 @@ func (s *Server) setupRaft() error {
s.config.LogOutput)
s.raftTransport = trans

// Make sure we set the LogOutput.
s.config.RaftConfig.LogOutput = s.config.LogOutput
// Make sure we set the Logger.
logger := s.logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true})
s.config.RaftConfig.Logger = logger
s.config.RaftConfig.LogOutput = nil

// Our version of Raft protocol requires the LocalID to match the network
// address of the transport.
Expand Down Expand Up @@ -1253,8 +1258,11 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) (
if s.config.UpgradeVersion != "" {
conf.Tags[AutopilotVersionTag] = s.config.UpgradeVersion
}
conf.MemberlistConfig.LogOutput = s.config.LogOutput
conf.LogOutput = s.config.LogOutput
logger := s.logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true})
conf.MemberlistConfig.Logger = logger
conf.Logger = logger
conf.MemberlistConfig.LogOutput = nil
conf.LogOutput = nil
conf.EventCh = ch
if !s.config.DevMode {
conf.SnapshotPath = filepath.Join(s.config.DataDir, path)
Expand Down
13 changes: 12 additions & 1 deletion vendor/github.com/hashicorp/yamux/mux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions vendor/github.com/hashicorp/yamux/session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
{"path":"github.com/hashicorp/vault/helper/jsonutil","checksumSHA1":"POgkM3GrjRFw6H3sw95YNEs552A=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"},
{"path":"github.com/hashicorp/vault/helper/parseutil","checksumSHA1":"HA2MV/2XI0HcoThSRxQCaBZR2ps=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"},
{"path":"github.com/hashicorp/vault/helper/strutil","checksumSHA1":"HdVuYhZ5TuxeIFqi0jy2GHW7a4o=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"},
{"path":"github.com/hashicorp/yamux","checksumSHA1":"NnWv17i1tpvBNJtpdRRWpE6j4LY=","revision":"2658be15c5f05e76244154714161f17e3e77de2e","revisionTime":"2018-03-14T20:07:45Z"},
{"path":"github.com/hashicorp/yamux","checksumSHA1":"m9OKUPd/iliwKxs+LCSmAGpDJOs=","revision":"7221087c3d281fda5f794e28c2ea4c6e4d5c4558","revisionTime":"2018-09-17T20:50:41Z"},
{"path":"github.com/hpcloud/tail/util","checksumSHA1":"0xM336Lb25URO/1W1/CtGoRygVU=","revision":"37f4271387456dd1bf82ab1ad9229f060cc45386","revisionTime":"2017-08-14T16:06:53Z"},
{"path":"github.com/hpcloud/tail/watch","checksumSHA1":"TP4OAv5JMtzj2TB6OQBKqauaKDc=","revision":"37f4271387456dd1bf82ab1ad9229f060cc45386","revisionTime":"2017-08-14T16:06:53Z"},
{"path":"github.com/jmespath/go-jmespath","comment":"0.2.2-2-gc01cf91","revision":"c01cf91b011868172fdcd9f41838e80c9d716264"},
Expand Down

0 comments on commit 3a26035

Please sign in to comment.