-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Events/event source node #8918
Merged
Merged
Events/event source node #8918
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/nomad/nomad/stream" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
const ( | ||
TopicNodeRegistration = "NodeRegistration" | ||
TopicNodeDeregistration = "NodeDeregistration" | ||
) | ||
|
||
type NodeRegistrationEvent struct { | ||
Event *structs.NodeEvent | ||
NodeStatus string | ||
} | ||
|
||
type NodeDeregistrationEvent struct { | ||
NodeID string | ||
} | ||
|
||
// NodeRegisterEventFromChanges generates a NodeRegistrationEvent from a set | ||
// of transaction changes. | ||
func NodeRegisterEventFromChanges(tx ReadTxn, changes Changes) ([]stream.Event, error) { | ||
var events []stream.Event | ||
for _, change := range changes.Changes { | ||
switch change.Table { | ||
case "nodes": | ||
after, ok := change.After.(*structs.Node) | ||
if !ok { | ||
return nil, fmt.Errorf("transaction change was not a Node") | ||
} | ||
|
||
event := stream.Event{ | ||
Topic: TopicNodeRegistration, | ||
Index: changes.Index, | ||
Key: after.ID, | ||
Payload: &NodeRegistrationEvent{ | ||
Event: after.Events[len(after.Events)-1], | ||
NodeStatus: after.Status, | ||
}, | ||
} | ||
events = append(events, event) | ||
} | ||
} | ||
return events, nil | ||
} | ||
|
||
// NodeDeregisterEventFromChanges generates a NodeDeregistrationEvent from a set | ||
// of transaction changes. | ||
func NodeDeregisterEventFromChanges(tx ReadTxn, changes Changes) ([]stream.Event, error) { | ||
var events []stream.Event | ||
for _, change := range changes.Changes { | ||
switch change.Table { | ||
case "nodes": | ||
before, ok := change.Before.(*structs.Node) | ||
if !ok { | ||
return nil, fmt.Errorf("transaction change was not a Node") | ||
} | ||
|
||
event := stream.Event{ | ||
Topic: TopicNodeDeregistration, | ||
Index: changes.Index, | ||
Key: before.ID, | ||
Payload: &NodeDeregistrationEvent{ | ||
NodeID: before.ID, | ||
}, | ||
} | ||
events = append(events, event) | ||
} | ||
} | ||
return events, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/stream" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNodeRegisterEventFromChanges(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
MsgType structs.MessageType | ||
Setup func(s *StateStore, tx *txn) error | ||
Mutate func(s *StateStore, tx *txn) error | ||
WantEvents []stream.Event | ||
WantErr bool | ||
WantTopic string | ||
}{ | ||
{ | ||
MsgType: structs.NodeRegisterRequestType, | ||
WantTopic: TopicNodeRegistration, | ||
Name: "node registered", | ||
Mutate: func(s *StateStore, tx *txn) error { | ||
return upsertNodeTxn(tx, tx.Index, testNode()) | ||
}, | ||
WantEvents: []stream.Event{{ | ||
Topic: TopicNodeRegistration, | ||
Key: testNodeID(), | ||
Index: 100, | ||
Payload: &NodeRegistrationEvent{ | ||
Event: &structs.NodeEvent{ | ||
Message: "Node registered", | ||
Subsystem: "Cluster", | ||
}, | ||
NodeStatus: structs.NodeStatusReady, | ||
}, | ||
}}, | ||
WantErr: false, | ||
}, | ||
{ | ||
MsgType: structs.NodeRegisterRequestType, | ||
WantTopic: TopicNodeRegistration, | ||
Name: "node registered initializing", | ||
Mutate: func(s *StateStore, tx *txn) error { | ||
return upsertNodeTxn(tx, tx.Index, testNode(nodeNotReady)) | ||
}, | ||
WantEvents: []stream.Event{{ | ||
Topic: TopicNodeRegistration, | ||
Key: testNodeID(), | ||
Index: 100, | ||
Payload: &NodeRegistrationEvent{ | ||
Event: &structs.NodeEvent{ | ||
Message: "Node registered", | ||
Subsystem: "Cluster", | ||
}, | ||
NodeStatus: structs.NodeStatusInit, | ||
}, | ||
}}, | ||
WantErr: false, | ||
}, | ||
{ | ||
MsgType: structs.NodeDeregisterRequestType, | ||
WantTopic: TopicNodeDeregistration, | ||
Name: "node deregistered", | ||
Setup: func(s *StateStore, tx *txn) error { | ||
return upsertNodeTxn(tx, tx.Index, testNode()) | ||
}, | ||
Mutate: func(s *StateStore, tx *txn) error { | ||
return deleteNodeTxn(tx, tx.Index, []string{testNodeID()}) | ||
}, | ||
WantEvents: []stream.Event{{ | ||
Topic: TopicNodeDeregistration, | ||
Key: testNodeID(), | ||
Index: 100, | ||
Payload: &NodeDeregistrationEvent{ | ||
NodeID: testNodeID(), | ||
}, | ||
}}, | ||
WantErr: false, | ||
}, | ||
{ | ||
MsgType: structs.NodeDeregisterRequestType, | ||
WantTopic: TopicNodeDeregistration, | ||
Name: "batch node deregistered", | ||
Setup: func(s *StateStore, tx *txn) error { | ||
require.NoError(t, upsertNodeTxn(tx, tx.Index, testNode())) | ||
return upsertNodeTxn(tx, tx.Index, testNode(nodeIDTwo)) | ||
}, | ||
Mutate: func(s *StateStore, tx *txn) error { | ||
return deleteNodeTxn(tx, tx.Index, []string{testNodeID(), testNodeIDTwo()}) | ||
}, | ||
WantEvents: []stream.Event{ | ||
{ | ||
Topic: TopicNodeDeregistration, | ||
Key: testNodeID(), | ||
Index: 100, | ||
Payload: &NodeDeregistrationEvent{ | ||
NodeID: testNodeID(), | ||
}, | ||
}, | ||
{ | ||
Topic: TopicNodeDeregistration, | ||
Key: testNodeIDTwo(), | ||
Index: 100, | ||
Payload: &NodeDeregistrationEvent{ | ||
NodeID: testNodeIDTwo(), | ||
}, | ||
}, | ||
}, | ||
WantErr: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
s := TestStateStoreCfg(t, TestStateStorePublisher(t)) | ||
defer s.StopEventPublisher() | ||
|
||
if tc.Setup != nil { | ||
// Bypass publish mechanism for setup | ||
setupTx := s.db.WriteTxn(10) | ||
require.NoError(t, tc.Setup(s, setupTx)) | ||
setupTx.Txn.Commit() | ||
} | ||
|
||
tx := s.db.WriteTxn(100) | ||
require.NoError(t, tc.Mutate(s, tx)) | ||
|
||
changes := Changes{Changes: tx.Changes(), Index: 100, MsgType: tc.MsgType} | ||
got, err := processDBChanges(tx, changes) | ||
|
||
if tc.WantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, len(tc.WantEvents), len(got)) | ||
for idx, g := range got { | ||
switch tc.MsgType { | ||
case structs.NodeRegisterRequestType: | ||
requireNodeRegistrationEventEqual(t, tc.WantEvents[idx], g) | ||
case structs.NodeDeregisterRequestType: | ||
requireNodeDeregistrationEventEqual(t, tc.WantEvents[idx], g) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func requireNodeRegistrationEventEqual(t *testing.T, want, got stream.Event) { | ||
t.Helper() | ||
|
||
require.Equal(t, want.Index, got.Index) | ||
require.Equal(t, want.Key, got.Key) | ||
require.Equal(t, want.Topic, got.Topic) | ||
|
||
wantPayload := want.Payload.(*NodeRegistrationEvent) | ||
gotPayload := got.Payload.(*NodeRegistrationEvent) | ||
|
||
// Check payload equality for the fields that we can easily control | ||
require.Equal(t, wantPayload.NodeStatus, gotPayload.NodeStatus) | ||
require.Equal(t, wantPayload.Event.Message, gotPayload.Event.Message) | ||
require.Equal(t, wantPayload.Event.Subsystem, gotPayload.Event.Subsystem) | ||
} | ||
|
||
func requireNodeDeregistrationEventEqual(t *testing.T, want, got stream.Event) { | ||
t.Helper() | ||
|
||
require.Equal(t, want.Index, got.Index) | ||
require.Equal(t, want.Key, got.Key) | ||
require.Equal(t, want.Topic, got.Topic) | ||
|
||
wantPayload := want.Payload.(*NodeDeregistrationEvent) | ||
gotPayload := got.Payload.(*NodeDeregistrationEvent) | ||
|
||
require.Equal(t, wantPayload, gotPayload) | ||
} | ||
|
||
type nodeOpts func(n *structs.Node) | ||
|
||
func nodeNotReady(n *structs.Node) { | ||
n.Status = structs.NodeStatusInit | ||
} | ||
|
||
func nodeIDTwo(n *structs.Node) { | ||
n.ID = testNodeIDTwo() | ||
} | ||
|
||
func testNode(opts ...nodeOpts) *structs.Node { | ||
n := mock.Node() | ||
n.ID = testNodeID() | ||
|
||
n.SecretID = "ab9812d3-6a21-40d3-973d-d9d2174a23ee" | ||
|
||
for _, opt := range opts { | ||
opt(n) | ||
} | ||
return n | ||
} | ||
|
||
func testNodeID() string { | ||
return "9d5741c1-3899-498a-98dd-eb3c05665863" | ||
} | ||
|
||
func testNodeIDTwo() string { | ||
return "694ff31d-8c59-4030-ac83-e15692560c8d" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell there's a 1:1 mapping between message types and
n.apply*
functions. So we're creating a context and threading the message type from raft -> fsm -> state store. There's only ever 1 parameter and it's the message type, which the apply function already needs to know. What do we get by creating acontext
rather than tacking on the msgType as a new parameter lower down the stack?