-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC, FSM, state store for Node.EmitEvent
add node event when registering a node for the first time
- Loading branch information
1 parent
c9bcf56
commit cb27331
Showing
8 changed files
with
252 additions
and
0 deletions.
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
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
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,47 @@ | ||
package state | ||
|
||
import ( | ||
"fmt" | ||
|
||
memdb "github.com/hashicorp/go-memdb" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
// addNodeEvent is a function which wraps upsertNodeEvent | ||
func (s *StateStore) AddNodeEvent(index uint64, nodeID string, event *structs.NodeEvent) error { | ||
txn := s.db.Txn(true) | ||
defer txn.Abort() | ||
|
||
return s.upsertNodeEvent(index, nodeID, event, txn) | ||
} | ||
|
||
// upsertNodeEvent upserts a node event for a respective node. It also maintains | ||
// that only 10 node events are ever stored simultaneously, deleting older | ||
// events once this bound has been reached. | ||
func (s *StateStore) upsertNodeEvent(index uint64, nodeID string, event *structs.NodeEvent, txn *memdb.Txn) error { | ||
|
||
ws := memdb.NewWatchSet() | ||
node, err := s.NodeByID(ws, nodeID) | ||
|
||
if err != nil { | ||
return fmt.Errorf("unable to look up nodes by id %+v", err) | ||
} | ||
|
||
if node == nil { | ||
return fmt.Errorf("unable to look up nodes by id %s", nodeID) | ||
} | ||
|
||
event.CreateIndex = index | ||
|
||
nodeEvents := node.NodeEvents | ||
|
||
if len(nodeEvents) >= 10 { | ||
delta := len(nodeEvents) - 10 | ||
nodeEvents = nodeEvents[delta+1:] | ||
} | ||
nodeEvents = append(nodeEvents, event) | ||
node.NodeEvents = nodeEvents | ||
|
||
txn.Commit() | ||
return 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,69 @@ | ||
package state | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
memdb "github.com/hashicorp/go-memdb" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStateStore_AddSingleNodeEvent(t *testing.T) { | ||
require := require.New(t) | ||
state := testStateStore(t) | ||
|
||
node := mock.Node() | ||
|
||
err := state.UpsertNode(1000, node) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
nodeEvent := &structs.NodeEvent{ | ||
Message: "failed", | ||
Subsystem: "Driver", | ||
Timestamp: time.Now().Unix(), | ||
} | ||
err = state.AddNodeEvent(1001, node.ID, nodeEvent) | ||
require.Nil(err) | ||
|
||
ws := memdb.NewWatchSet() | ||
actualNode, err := state.NodeByID(ws, node.ID) | ||
require.Nil(err) | ||
require.Equal(1, len(actualNode.NodeEvents)) | ||
require.Equal(nodeEvent, actualNode.NodeEvents[0]) | ||
} | ||
|
||
// To prevent stale node events from accumulating, we limit the number of | ||
// stored node events to 10. | ||
func TestStateStore_NodeEvents_RetentionWindow(t *testing.T) { | ||
require := require.New(t) | ||
state := testStateStore(t) | ||
|
||
node := mock.Node() | ||
|
||
err := state.UpsertNode(1000, node) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
for i := 1; i <= 20; i++ { | ||
nodeEvent := &structs.NodeEvent{ | ||
Message: "failed", | ||
Subsystem: "Driver", | ||
Timestamp: time.Now().Unix(), | ||
} | ||
err := state.AddNodeEvent(uint64(i), node.ID, nodeEvent) | ||
require.Nil(err) | ||
} | ||
|
||
ws := memdb.NewWatchSet() | ||
actualNode, err := state.NodeByID(ws, node.ID) | ||
require.Nil(err) | ||
|
||
require.Equal(10, len(actualNode.NodeEvents)) | ||
require.Equal(uint64(11), actualNode.NodeEvents[0].CreateIndex) | ||
require.Equal(uint64(20), actualNode.NodeEvents[len(actualNode.NodeEvents)-1].CreateIndex) | ||
} |
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