Merge pull request #3758 from xiang90/race

*: fix various data races detected by race detector
This commit is contained in:
Xiang Li 2015-10-26 20:57:31 -07:00
commit 70f9407d2d
4 changed files with 32 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import (
"reflect" "reflect"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
@ -93,7 +94,9 @@ type store struct {
server doer server doer
timeout time.Duration timeout time.Duration
ensuredOnce bool ensuredOnce bool
enabled *bool
mu sync.Mutex // protect enabled
enabled *bool
} }
type User struct { type User struct {
@ -377,6 +380,9 @@ func (s *store) UpdateRole(role Role) (Role, error) {
} }
func (s *store) AuthEnabled() bool { func (s *store) AuthEnabled() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.detectAuth() return s.detectAuth()
} }
@ -384,6 +390,10 @@ func (s *store) EnableAuth() error {
if s.AuthEnabled() { if s.AuthEnabled() {
return authErr(http.StatusConflict, "already enabled") return authErr(http.StatusConflict, "already enabled")
} }
s.mu.Lock()
defer s.mu.Unlock()
_, err := s.GetUser("root") _, err := s.GetUser("root")
if err != nil { if err != nil {
return authErr(http.StatusConflict, "No root user available, please create one") return authErr(http.StatusConflict, "No root user available, please create one")
@ -412,6 +422,10 @@ func (s *store) DisableAuth() error {
if !s.AuthEnabled() { if !s.AuthEnabled() {
return authErr(http.StatusConflict, "already disabled") return authErr(http.StatusConflict, "already disabled")
} }
s.mu.Lock()
defer s.mu.Unlock()
err := s.disableAuth() err := s.disableAuth()
if err == nil { if err == nil {
b := false b := false

View File

@ -220,6 +220,9 @@ func (c *cluster) SetID(id types.ID) { c.id = id }
func (c *cluster) SetStore(st store.Store) { c.store = st } func (c *cluster) SetStore(st store.Store) { c.store = st }
func (c *cluster) Recover() { func (c *cluster) Recover() {
c.Lock()
defer c.Unlock()
c.members, c.removed = membersFromStore(c.store) c.members, c.removed = membersFromStore(c.store)
c.version = clusterVersionFromStore(c.store) c.version = clusterVersionFromStore(c.store)
MustDetectDowngrade(c.version) MustDetectDowngrade(c.version)

View File

@ -52,6 +52,8 @@ const (
) )
var ( var (
// protects raftStatus
raftStatusMu sync.Mutex
// indirection for expvar func interface // indirection for expvar func interface
// expvar panics when publishing duplicate name // expvar panics when publishing duplicate name
// expvar does not support remove a registered name // expvar does not support remove a registered name
@ -62,7 +64,11 @@ var (
func init() { func init() {
raft.SetLogger(capnslog.NewPackageLogger("github.com/coreos/etcd", "raft")) raft.SetLogger(capnslog.NewPackageLogger("github.com/coreos/etcd", "raft"))
expvar.Publish("raft.status", expvar.Func(func() interface{} { return raftStatus() })) expvar.Publish("raft.status", expvar.Func(func() interface{} {
raftStatusMu.Lock()
defer raftStatusMu.Unlock()
return raftStatus()
}))
} }
type RaftTimer interface { type RaftTimer interface {
@ -274,7 +280,9 @@ func startNode(cfg *ServerConfig, cl *cluster, ids []types.ID) (id types.ID, n r
MaxInflightMsgs: maxInflightMsgs, MaxInflightMsgs: maxInflightMsgs,
} }
n = raft.StartNode(c, peers) n = raft.StartNode(c, peers)
raftStatusMu.Lock()
raftStatus = n.Status raftStatus = n.Status
raftStatusMu.Unlock()
advanceTicksForElection(n, c.ElectionTick) advanceTicksForElection(n, c.ElectionTick)
return return
} }
@ -304,7 +312,9 @@ func restartNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *clust
MaxInflightMsgs: maxInflightMsgs, MaxInflightMsgs: maxInflightMsgs,
} }
n := raft.RestartNode(c) n := raft.RestartNode(c)
raftStatusMu.Lock()
raftStatus = n.Status raftStatus = n.Status
raftStatusMu.Unlock()
advanceTicksForElection(n, c.ElectionTick) advanceTicksForElection(n, c.ElectionTick)
return id, cl, n, s, w return id, cl, n, s, w
} }

View File

@ -180,7 +180,10 @@ func (t *Transport) Send(msgs []raftpb.Message) {
} }
to := types.ID(m.To) to := types.ID(m.To)
t.mu.RLock()
p, ok := t.peers[to] p, ok := t.peers[to]
t.mu.RUnlock()
if ok { if ok {
if m.Type == raftpb.MsgApp { if m.Type == raftpb.MsgApp {
t.ServerStats.SendAppendReq(m.Size()) t.ServerStats.SendAppendReq(m.Size())