Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/b3fs/wfs/pkg/logutil"
"github.com/b3fs/wfs/server"
"go.uber.org/zap"
)

func main() {
Expand All @@ -21,6 +22,25 @@ func main() {
os.Exit(0)
}

defer logutil.LogPanic()

switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
exit(0)
default:
log.Fatal("parse cmd flags error", zap.Error(err))
}
// New zap logger
err = cfg.SetupLogger()
if err == nil {
log.ReplaceGlobals(cfg.GetZapLogger(), cfg.GetZapLogProperties())
} else {
log.Fatal("initialize logger error", zap.Error(err))
}
// Flushing any buffered log entries
defer log.Sync()

err = logutil.InitLogger(&cfg.Log)
if err != nil {
log.Fatalf("initialize logger error: %s\n", fmt.Sprintf("%+v", err))
Expand Down
34 changes: 18 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/b3fs/wfs

require (
github.com/BurntSushi/toml v0.3.1
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/coreos/bbolt v1.3.0 // indirect
github.com/coreos/etcd v3.3.9+incompatible
github.com/coreos/go-semver v0.2.0
Expand All @@ -11,30 +11,32 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/gogo/protobuf v1.1.1
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/golang/protobuf v1.2.0
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c
github.com/gorilla/websocket v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.5.1 // indirect
github.com/hawkingrei/wfs v0.0.0-20181005145148-b215acd11928 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/juju/errors v0.0.0-20180806074554-22422dad46e1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/juju/ratelimit v1.0.1
github.com/montanaflynn/stats v0.5.0 // indirect
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8
github.com/pingcap/kvproto v0.0.0-20180930052200-fae11119f066
github.com/pingcap/pd v2.0.5+incompatible
github.com/pkg/errors v0.8.0
github.com/prometheus/client_golang v0.8.0 // indirect
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/pd v2.1.8+incompatible
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2 // indirect
github.com/sirupsen/logrus v1.1.0
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/syndtr/goleveldb v1.0.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6 // indirect
github.com/ugorji/go/codec v0.0.0-20180927125128-99ea80c8b19a // indirect
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
github.com/ugorji/go v1.1.4 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/etcd v3.3.12+incompatible
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/zap v1.10.0
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.15.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.2.1 // indirect
)
141 changes: 125 additions & 16 deletions go.sum

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions pkg/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package logutil

import (
Expand All @@ -19,14 +18,16 @@ import (
"os"
"path"
"runtime"
"runtime/debug"
"strings"
"sync"

"github.com/coreos/etcd/raft"
"github.com/coreos/pkg/capnslog"
zaplog "github.com/pingcap/log"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"go.etcd.io/etcd/raft"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
Expand Down Expand Up @@ -144,6 +145,23 @@ func StringToLogLevel(level string) log.Level {
return defaultLogLevel
}

// StringToZapLogLevel translates log level string to log level.
func StringToZapLogLevel(level string) zapcore.Level {
switch strings.ToLower(level) {
case "fatal":
return zapcore.FatalLevel
case "error":
return zapcore.ErrorLevel
case "warn", "warning":
return zapcore.WarnLevel
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
}
return zapcore.InfoLevel
}

// textFormatter is for compatibility with ngaut/log
type textFormatter struct {
DisableTimestamp bool
Expand Down Expand Up @@ -173,7 +191,8 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) {
return b.Bytes(), nil
}

func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
// StringToLogFormatter uses the different log formatter according to a given format name.
func StringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
switch strings.ToLower(format) {
case "text":
return &textFormatter{
Expand All @@ -196,7 +215,7 @@ func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
}

// InitFileLog initializes file based logging options.
func InitFileLog(cfg *FileLogConfig) error {
func InitFileLog(cfg *zaplog.FileLogConfig) error {
if st, err := os.Stat(cfg.Filename); err == nil {
if st.IsDir() {
return errors.New("can't use directory as log file name")
Expand Down Expand Up @@ -238,7 +257,7 @@ func (lg *wrapLogrus) V(l int) bool {
var once sync.Once

// InitLogger initializes PD's logger.
func InitLogger(cfg *LogConfig) error {
func InitLogger(cfg *zaplog.Config) error {
var err error

once.Do(func() {
Expand All @@ -248,7 +267,7 @@ func InitLogger(cfg *LogConfig) error {
if cfg.Format == "" {
cfg.Format = defaultLogFormat
}
log.SetFormatter(stringToLogFormatter(cfg.Format, cfg.DisableTimestamp))
log.SetFormatter(StringToLogFormatter(cfg.Format, cfg.DisableTimestamp))

// etcd log
capnslog.SetFormatter(&redirectFormatter{})
Expand All @@ -271,6 +290,6 @@ func InitLogger(cfg *LogConfig) error {
// Commonly used with a `defer`.
func LogPanic() {
if e := recover(); e != nil {
log.Fatalf("panic: %v, stack: %s", e, string(debug.Stack()))
zaplog.Fatal("panic", zap.Reflect("recover", e))
}
}
35 changes: 34 additions & 1 deletion pkg/logutil/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (

"github.com/coreos/pkg/capnslog"
. "github.com/pingcap/check"
zaplog "github.com/pingcap/log"
log "github.com/sirupsen/logrus"
"go.uber.org/zap/zapcore"
)

const (
Expand Down Expand Up @@ -51,9 +53,35 @@ func (s *testLogSuite) TestStringToLogLevel(c *C) {
c.Assert(StringToLogLevel("whatever"), Equals, log.InfoLevel)
}

func (s *testLogSuite) TestStringToZapLogLevel(c *C) {
c.Assert(StringToZapLogLevel("fatal"), Equals, zapcore.FatalLevel)
c.Assert(StringToZapLogLevel("ERROR"), Equals, zapcore.ErrorLevel)
c.Assert(StringToZapLogLevel("warn"), Equals, zapcore.WarnLevel)
c.Assert(StringToZapLogLevel("warning"), Equals, zapcore.WarnLevel)
c.Assert(StringToZapLogLevel("debug"), Equals, zapcore.DebugLevel)
c.Assert(StringToZapLogLevel("info"), Equals, zapcore.InfoLevel)
c.Assert(StringToZapLogLevel("whatever"), Equals, zapcore.InfoLevel)
}

func (s *testLogSuite) TestStringToLogFormatter(c *C) {
c.Assert(StringToLogFormatter("text", true), DeepEquals, &textFormatter{
DisableTimestamp: true,
})
c.Assert(StringToLogFormatter("json", true), DeepEquals, &log.JSONFormatter{
DisableTimestamp: true,
TimestampFormat: defaultLogTimeFormat,
})
c.Assert(StringToLogFormatter("console", true), DeepEquals, &log.TextFormatter{
DisableTimestamp: true,
FullTimestamp: true,
TimestampFormat: defaultLogTimeFormat,
})
c.Assert(StringToLogFormatter("", true), DeepEquals, &textFormatter{})
}

// TestLogging assure log format and log redirection works.
func (s *testLogSuite) TestLogging(c *C) {
conf := &LogConfig{Level: "warn", File: FileLogConfig{}}
conf := &zaplog.Config{Level: "warn", File: zaplog.FileLogConfig{}}
c.Assert(InitLogger(conf), IsNil)

log.SetOutput(s.buf)
Expand All @@ -76,3 +104,8 @@ func (s *testLogSuite) TestLogging(c *C) {
c.Assert(entry, Matches, logPattern)
c.Assert(strings.Contains(entry, "log_test.go"), IsTrue)
}

func (s *testLogSuite) TestFileLog(c *C) {
c.Assert(InitFileLog(&zaplog.FileLogConfig{Filename: "/tmp"}), NotNil)
c.Assert(InitFileLog(&zaplog.FileLogConfig{Filename: "/tmp/test_file_log", MaxSize: 0}), IsNil)
}
50 changes: 50 additions & 0 deletions server/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"sync"
"time"

syncer "github.com/b3fs/wfs/server/region_syncer"
)

var backgroundJobInterval = time.Minute

// RaftCluster is used for cluster config management.
// Raft cluster key format:
// cluster 1 -> /1/raft, value is metapb.Cluster
// cluster 2 -> /2/raft
// For cluster 1
// store 1 -> /1/raft/s/1, value is metapb.Store
// region 1 -> /1/raft/r/1, value is metapb.Region
type RaftCluster struct {
sync.RWMutex

s *Server

running bool

clusterID uint64
clusterRoot string

// cached cluster info
cachedCluster *clusterInfo

coordinator *coordinator

wg sync.WaitGroup
quit chan struct{}
regionSyncer *syncer.RegionSyncer
}
Loading