Skip to content

Commit

Permalink
Refactor GenericEventsFromChanges (#9279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Hicks committed Nov 5, 2020
1 parent 4daf5a4 commit 3926317
Showing 1 changed file with 77 additions and 119 deletions.
196 changes: 77 additions & 119 deletions nomad/state/events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package state

import (
"fmt"

memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand Down Expand Up @@ -91,128 +90,87 @@ func GenericEventsFromChanges(tx ReadTxn, changes Changes) (*structs.Events, err

var events []structs.Event
for _, change := range changes.Changes {
switch change.Table {
case "evals":
if change.Deleted() {
return nil, nil
}
after, ok := change.After.(*structs.Evaluation)
if !ok {
return nil, fmt.Errorf("transaction change was not an Evaluation")
}

event := structs.Event{
Topic: structs.TopicEval,
Type: eventType,
Index: changes.Index,
Key: after.ID,
FilterKeys: []string{
after.JobID,
after.DeploymentID,
},
Namespace: after.Namespace,
Payload: &EvalEvent{
Eval: after,
},
}
if change.Deleted() {
return nil, nil
}

if event, ok := eventFromChange(change); ok {
event.Type = eventType
event.Index = changes.Index
events = append(events, event)
}
}

case "allocs":
if change.Deleted() {
return nil, nil
}
after, ok := change.After.(*structs.Allocation)
if !ok {
return nil, fmt.Errorf("transaction change was not an Allocation")
}

alloc := after.Copy()

filterKeys := []string{
alloc.JobID,
alloc.DeploymentID,
}

// remove job info to help keep size of alloc event down
alloc.Job = nil

event := structs.Event{
Topic: structs.TopicAlloc,
Type: eventType,
Index: changes.Index,
Key: after.ID,
FilterKeys: filterKeys,
Namespace: after.Namespace,
Payload: &AllocEvent{
Alloc: alloc,
},
}

events = append(events, event)
case "jobs":
if change.Deleted() {
return nil, nil
}
after, ok := change.After.(*structs.Job)
if !ok {
return nil, fmt.Errorf("transaction change was not an Allocation")
}

event := structs.Event{
Topic: structs.TopicJob,
Type: eventType,
Index: changes.Index,
Key: after.ID,
Namespace: after.Namespace,
Payload: &JobEvent{
Job: after,
},
}
return &structs.Events{Index: changes.Index, Events: events}, nil
}

events = append(events, event)
case "nodes":
if change.Deleted() {
return nil, nil
}
after, ok := change.After.(*structs.Node)
if !ok {
return nil, fmt.Errorf("transaction change was not a Node")
}

event := structs.Event{
Topic: structs.TopicNode,
Type: eventType,
Index: changes.Index,
Key: after.ID,
Payload: &NodeEvent{
Node: after,
},
}
events = append(events, event)
case "deployment":
if change.Deleted() {
return nil, nil
}
after, ok := change.After.(*structs.Deployment)
if !ok {
return nil, fmt.Errorf("transaction change was not a Node")
}

event := structs.Event{
Topic: structs.TopicDeployment,
Type: eventType,
Index: changes.Index,
Key: after.ID,
Namespace: after.Namespace,
FilterKeys: []string{after.JobID},
Payload: &DeploymentEvent{
Deployment: after,
},
}
events = append(events, event)
func eventFromChange(change memdb.Change) (structs.Event, bool) {
switch after := change.After.(type) {
case *structs.Evaluation:
return structs.Event{
Topic: structs.TopicEval,
Key: after.ID,
FilterKeys: []string{
after.JobID,
after.DeploymentID,
},
Namespace: after.Namespace,
Payload: &EvalEvent{
Eval: after,
},
}, true

case *structs.Allocation:
alloc := after.Copy()

filterKeys := []string{
alloc.JobID,
alloc.DeploymentID,
}

// remove job info to help keep size of alloc event down
alloc.Job = nil

return structs.Event{
Topic: structs.TopicAlloc,
Key: after.ID,
FilterKeys: filterKeys,
Namespace: after.Namespace,
Payload: &AllocEvent{
Alloc: alloc,
},
}, true

case *structs.Job:
return structs.Event{
Topic: structs.TopicJob,
Key: after.ID,
Namespace: after.Namespace,
Payload: &JobEvent{
Job: after,
},
}, true

case *structs.Node:
return structs.Event{
Topic: structs.TopicNode,
Key: after.ID,
Payload: &NodeEvent{
Node: after,
},
}, true

case *structs.Deployment:
return structs.Event{
Topic: structs.TopicDeployment,
Key: after.ID,
Namespace: after.Namespace,
FilterKeys: []string{after.JobID},
Payload: &DeploymentEvent{
Deployment: after,
},
}, true
}

return &structs.Events{Index: changes.Index, Events: events}, nil
return structs.Event{}, false
}

0 comments on commit 3926317

Please sign in to comment.