Skip to content

Commit

Permalink
*: fix various data races detected by race detector
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Oct 27, 2015
1 parent 336d177 commit 9858d60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion etcdserver/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"sort"
"strings"
"sync"
"time"

"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
Expand Down Expand Up @@ -93,7 +94,9 @@ type store struct {
server doer
timeout time.Duration
ensuredOnce bool
enabled *bool

mu sync.Mutex // protect enabled
enabled *bool
}

type User struct {
Expand Down Expand Up @@ -377,13 +380,20 @@ func (s *store) UpdateRole(role Role) (Role, error) {
}

func (s *store) AuthEnabled() bool {
s.mu.Lock()
defer s.mu.Unlock()

return s.detectAuth()
}

func (s *store) EnableAuth() error {
if s.AuthEnabled() {
return authErr(http.StatusConflict, "already enabled")
}

s.mu.Lock()
defer s.mu.Unlock()

_, err := s.GetUser("root")
if err != nil {
return authErr(http.StatusConflict, "No root user available, please create one")
Expand Down Expand Up @@ -412,6 +422,10 @@ func (s *store) DisableAuth() error {
if !s.AuthEnabled() {
return authErr(http.StatusConflict, "already disabled")
}

s.mu.Lock()
defer s.mu.Unlock()

err := s.disableAuth()
if err == nil {
b := false
Expand Down
3 changes: 3 additions & 0 deletions etcdserver/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) Recover() {
c.Lock()
defer c.Unlock()

c.members, c.removed = membersFromStore(c.store)
c.version = clusterVersionFromStore(c.store)
MustDetectDowngrade(c.version)
Expand Down
9 changes: 9 additions & 0 deletions etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const (
)

var (
// protect raftStatus
// raftStatus might be accessed concurrently
// by integration tests since multiple etcdservers
// share the same raftStatus
raftStatusMu sync.Mutex
// indirection for expvar func interface
// expvar panics when publishing duplicate name
// expvar does not support remove a registered name
Expand Down Expand Up @@ -274,7 +279,9 @@ func startNode(cfg *ServerConfig, cl *cluster, ids []types.ID) (id types.ID, n r
MaxInflightMsgs: maxInflightMsgs,
}
n = raft.StartNode(c, peers)
raftStatusMu.Lock()
raftStatus = n.Status
raftStatusMu.Unlock()
advanceTicksForElection(n, c.ElectionTick)
return
}
Expand Down Expand Up @@ -304,7 +311,9 @@ func restartNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *clust
MaxInflightMsgs: maxInflightMsgs,
}
n := raft.RestartNode(c)
raftStatusMu.Lock()
raftStatus = n.Status
raftStatusMu.Unlock()
advanceTicksForElection(n, c.ElectionTick)
return id, cl, n, s, w
}
Expand Down
3 changes: 3 additions & 0 deletions rafthttp/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ func (t *Transport) Send(msgs []raftpb.Message) {
}
to := types.ID(m.To)

t.mu.RLock()
p, ok := t.peers[to]
t.mu.RUnlock()

if ok {
if m.Type == raftpb.MsgApp {
t.ServerStats.SendAppendReq(m.Size())
Expand Down

0 comments on commit 9858d60

Please sign in to comment.