2019-11-25 17:07:04 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-11-25 17:07:04 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sessionIndexer() *memdb.UUIDFieldIndex {
|
|
|
|
return &memdb.UUIDFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
func nodeSessionsIndexer() *memdb.StringFieldIndex {
|
|
|
|
return &memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
2019-11-25 17:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func nodeChecksIndexer() *memdb.CompoundIndex {
|
|
|
|
return &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
&CheckIDIndex{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) sessionDeleteWithSession(tx *txnWrapper, session *structs.Session, idx uint64) error {
|
2019-11-25 17:07:04 +00:00
|
|
|
if err := tx.Delete("sessions", session); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting session: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the indexes
|
|
|
|
err := tx.Insert("index", &IndexEntry{"sessions", idx})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed updating sessions index: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) insertSessionTxn(tx *txnWrapper, session *structs.Session, idx uint64, updateMax bool) error {
|
2019-11-25 17:07:04 +00:00
|
|
|
if err := tx.Insert("sessions", session); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the check mappings
|
2019-12-10 02:26:41 +00:00
|
|
|
for _, checkID := range session.CheckIDs() {
|
2019-11-25 17:07:04 +00:00
|
|
|
mapping := &sessionCheck{
|
|
|
|
Node: session.Node,
|
2019-12-10 02:26:41 +00:00
|
|
|
CheckID: structs.CheckID{ID: checkID},
|
2019-11-25 17:07:04 +00:00
|
|
|
Session: session.ID,
|
|
|
|
}
|
|
|
|
if err := tx.Insert("session_checks", mapping); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting session check mapping: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the index
|
|
|
|
if updateMax {
|
|
|
|
if err := indexUpdateMaxTxn(tx, idx, "sessions"); err != nil {
|
|
|
|
return fmt.Errorf("failed updating sessions index: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := tx.Insert("index", &IndexEntry{"sessions", idx})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed updating sessions index: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) allNodeSessionsTxn(tx *txnWrapper, node string) (structs.Sessions, error) {
|
2019-11-25 17:57:35 +00:00
|
|
|
return s.nodeSessionsTxn(tx, nil, node, nil)
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) nodeSessionsTxn(tx *txnWrapper,
|
2019-11-25 17:57:35 +00:00
|
|
|
ws memdb.WatchSet, node string, entMeta *structs.EnterpriseMeta) (structs.Sessions, error) {
|
|
|
|
|
|
|
|
sessions, err := tx.Get("sessions", "node", node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed session lookup: %s", err)
|
|
|
|
}
|
|
|
|
ws.Add(sessions.WatchCh())
|
|
|
|
|
|
|
|
var result structs.Sessions
|
|
|
|
for session := sessions.Next(); session != nil; session = sessions.Next() {
|
|
|
|
result = append(result, session.(*structs.Session))
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) sessionMaxIndex(tx *txnWrapper, entMeta *structs.EnterpriseMeta) uint64 {
|
2019-11-25 17:07:04 +00:00
|
|
|
return maxIndexTxn(tx, "sessions")
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) validateSessionChecksTxn(tx *txnWrapper, session *structs.Session) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// Go over the session checks and ensure they exist.
|
|
|
|
for _, checkID := range session.CheckIDs() {
|
|
|
|
check, err := tx.First("checks", "id", session.Node, string(checkID))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed check lookup: %s", err)
|
|
|
|
}
|
|
|
|
if check == nil {
|
|
|
|
return fmt.Errorf("Missing check '%s' registration", checkID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the check is not in critical state
|
|
|
|
status := check.(*structs.HealthCheck).Status
|
|
|
|
if status == api.HealthCritical {
|
|
|
|
return fmt.Errorf("Check '%s' is in %s state", checkID, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|