Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove a concurrent map access #874

Merged
merged 1 commit into from
Mar 3, 2016
Merged
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
25 changes: 16 additions & 9 deletions client/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ type ConsulService struct {
shutdownCh chan struct{}
node *structs.Node

trackedTasks map[string]*trackedTask
serviceStates map[string]string
allocToService map[string][]string
trackedTskLock sync.Mutex
trackedTasks map[string]*trackedTask
serviceStates map[string]string
allocToService map[string][]string
trackedTaskLock sync.Mutex
}

type consulServiceConfig struct {
Expand Down Expand Up @@ -147,7 +147,7 @@ func NewConsulService(config *consulServiceConfig) (*ConsulService, error) {
// adds/removes services and checks associated with it.
func (c *ConsulService) Register(task *structs.Task, alloc *structs.Allocation) error {
var mErr multierror.Error
c.trackedTskLock.Lock()
c.trackedTaskLock.Lock()
tt := &trackedTask{task: task, alloc: alloc}
c.trackedTasks[fmt.Sprintf("%s-%s", alloc.ID, task.Name)] = tt

Expand All @@ -156,7 +156,7 @@ func (c *ConsulService) Register(task *structs.Task, alloc *structs.Allocation)
for _, service := range c.allocToService[alloc.ID] {
delete(c.serviceStates, service)
}
c.trackedTskLock.Unlock()
c.trackedTaskLock.Unlock()

for _, service := range task.Services {
// Track the services this alloc is registering.
Expand All @@ -175,10 +175,10 @@ func (c *ConsulService) Register(task *structs.Task, alloc *structs.Allocation)
// removes all the services and checks associated with the Task
func (c *ConsulService) Deregister(task *structs.Task, alloc *structs.Allocation) error {
var mErr multierror.Error
c.trackedTskLock.Lock()
c.trackedTaskLock.Lock()
delete(c.trackedTasks, fmt.Sprintf("%s-%s", alloc.ID, task.Name))
delete(c.allocToService, alloc.ID)
c.trackedTskLock.Unlock()
c.trackedTaskLock.Unlock()
for _, service := range task.Services {
serviceID := alloc.Services[service.Name]
if serviceID == "" {
Expand Down Expand Up @@ -234,8 +234,15 @@ func (c *ConsulService) performSync() {
knownChecks := make(map[string]struct{})
knownServices := make(map[string]struct{})

// Add services and checks which Consul doesn't know about
c.trackedTaskLock.Lock()
tasks := make([]*trackedTask, 0, len(c.trackedTasks))
for _, trackedTask := range c.trackedTasks {
tasks = append(tasks, trackedTask)
}
c.trackedTaskLock.Unlock()

// Add services and checks which Consul doesn't know about
for _, trackedTask := range tasks {
for _, service := range trackedTask.task.Services {
serviceID := trackedTask.alloc.Services[service.Name]

Expand Down