consul/state: basic session destroy works

This commit is contained in:
Ryan Uber 2015-09-06 16:44:17 -07:00 committed by James Phillips
parent 747527fef5
commit e30c3cdea8
2 changed files with 87 additions and 0 deletions

View file

@ -1138,3 +1138,44 @@ func (s *StateStore) NodeSessions(nodeID string) (uint64, []*structs.Session, er
}
return lindex, result, nil
}
// SessionDestroy is used to remove an active session. This will
// implicitly invalidate the session and invoke the specified
// session destroy behavior.
func (s *StateStore) SessionDestroy(idx uint64, sessionID string) error {
tx := s.db.Txn(true)
defer tx.Abort()
// Call the session deletion
if err := s.sessionDestroyTxn(idx, sessionID, tx); err != nil {
return err
}
tx.Commit()
return nil
}
// sessionDestroyTxn is the inner method, which is used to do the actual
// session deletion and handle session invalidation, watch triggers, etc.
func (s *StateStore) sessionDestroyTxn(idx uint64, sessionID string, tx *memdb.Txn) error {
// Look up the session
sess, err := tx.First("sessions", "id", sessionID)
if err != nil {
return fmt.Errorf("failed session lookup: %s", err)
}
if sess == nil {
return nil
}
// Delete the session and write the new index
if err := tx.Delete("sessions", sess); err != nil {
return fmt.Errorf("failed deleting session: %s", err)
}
if err := tx.Insert("index", &IndexEntry{"sessions", idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err)
}
// TODO: invalidate session
return nil
}

View file

@ -1431,3 +1431,49 @@ func TestStateStore_NodeSessions(t *testing.T) {
t.Fatalf("bad: %#v", res)
}
}
func TestStateStore_SessionDestroy(t *testing.T) {
s := testStateStore(t)
// Session destroy is idempotent and returns no error
// if the session doesn't exist.
if err := s.SessionDestroy(1, "nope"); err != nil {
t.Fatalf("err: %s", err)
}
// Ensure the index was not updated if nothing was destroyed
if idx := s.maxIndex("sessions"); idx != 0 {
t.Fatalf("bad index: %d", idx)
}
// Register a node
testRegisterNode(t, s, 1, "node1")
// Register a new session
sess := &structs.Session{
ID: "session1",
Node: "node1",
}
if err := s.SessionCreate(2, sess); err != nil {
t.Fatalf("err: %s", err)
}
// Destroy the session
if err := s.SessionDestroy(3, "session1"); err != nil {
t.Fatalf("err: %s", err)
}
tx := s.db.Txn(false)
defer tx.Abort()
// Make sure the session is really gone
sessions, err := tx.Get("sessions", "id")
if err != nil || sessions.Next() != nil {
t.Fatalf("session should not exist")
}
// Check that the index was updated
if idx := s.maxIndex("sessions"); idx != 3 {
t.Fatalf("bad index: %d", idx)
}
}