2016-05-11 08:35:27 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-05-11 08:35:27 +00:00
|
|
|
)
|
|
|
|
|
2016-05-12 23:11:26 +00:00
|
|
|
// txnKVS handles all KV-related operations.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) txnKVS(tx WriteTxn, idx uint64, op *structs.TxnKVOp) (structs.TxnResults, error) {
|
2016-05-11 08:35:27 +00:00
|
|
|
var entry *structs.DirEntry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch op.Verb {
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVSet:
|
2016-05-11 08:35:27 +00:00
|
|
|
entry = &op.DirEnt
|
2020-07-10 00:56:43 +00:00
|
|
|
err = kvsSetTxn(tx, idx, entry, false)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDelete:
|
2019-11-25 17:57:35 +00:00
|
|
|
err = s.kvsDeleteTxn(tx, idx, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteCAS:
|
2016-05-11 08:35:27 +00:00
|
|
|
var ok bool
|
2019-11-25 17:57:35 +00:00
|
|
|
ok, err = s.kvsDeleteCASTxn(tx, idx, op.DirEnt.ModifyIndex, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 08:35:27 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to delete key %q, index is stale", op.DirEnt.Key)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteTree:
|
2019-11-25 17:57:35 +00:00
|
|
|
err = s.kvsDeleteTreeTxn(tx, idx, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCAS:
|
2016-05-11 08:35:27 +00:00
|
|
|
var ok bool
|
|
|
|
entry = &op.DirEnt
|
2020-07-10 00:56:43 +00:00
|
|
|
ok, err = kvsSetCASTxn(tx, idx, entry)
|
2016-05-11 08:35:27 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to set key %q, index is stale", op.DirEnt.Key)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVLock:
|
2016-05-11 08:35:27 +00:00
|
|
|
var ok bool
|
|
|
|
entry = &op.DirEnt
|
2020-07-10 00:56:43 +00:00
|
|
|
ok, err = kvsLockTxn(tx, idx, entry)
|
2016-05-11 08:35:27 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to lock key %q, lock is already held", op.DirEnt.Key)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVUnlock:
|
2016-05-11 08:35:27 +00:00
|
|
|
var ok bool
|
|
|
|
entry = &op.DirEnt
|
2020-07-10 00:56:43 +00:00
|
|
|
ok, err = kvsUnlockTxn(tx, idx, entry)
|
2016-05-11 08:35:27 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to unlock key %q, lock isn't held, or is held by another session", op.DirEnt.Key)
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVGet:
|
2020-07-10 00:56:43 +00:00
|
|
|
_, entry, err = kvsGetTxn(tx, nil, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 21:18:31 +00:00
|
|
|
if entry == nil && err == nil {
|
|
|
|
err = fmt.Errorf("key %q doesn't exist", op.DirEnt.Key)
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVGetTree:
|
2016-05-13 23:57:39 +00:00
|
|
|
var entries structs.DirEntries
|
2019-11-25 17:57:35 +00:00
|
|
|
_, entries, err = s.kvsListTxn(tx, nil, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2016-05-13 23:57:39 +00:00
|
|
|
if err == nil {
|
|
|
|
results := make(structs.TxnResults, 0, len(entries))
|
|
|
|
for _, e := range entries {
|
|
|
|
result := structs.TxnResult{KV: e}
|
|
|
|
results = append(results, &result)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCheckSession:
|
2020-07-10 00:56:43 +00:00
|
|
|
entry, err = kvsCheckSessionTxn(tx, op.DirEnt.Key, op.DirEnt.Session, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCheckIndex:
|
2020-07-10 00:56:43 +00:00
|
|
|
entry, err = kvsCheckIndexTxn(tx, op.DirEnt.Key, op.DirEnt.ModifyIndex, &op.DirEnt.EnterpriseMeta)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
2017-04-21 00:50:52 +00:00
|
|
|
case api.KVCheckNotExists:
|
2020-07-10 00:56:43 +00:00
|
|
|
_, entry, err = kvsGetTxn(tx, nil, op.DirEnt.Key, &op.DirEnt.EnterpriseMeta)
|
2017-04-21 00:50:52 +00:00
|
|
|
if entry != nil && err == nil {
|
|
|
|
err = fmt.Errorf("key %q exists", op.DirEnt.Key)
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
default:
|
2016-05-11 17:58:27 +00:00
|
|
|
err = fmt.Errorf("unknown KV verb %q", op.Verb)
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a GET we keep the value, otherwise we clone and blank out the
|
|
|
|
// value (we have to clone so we don't modify the entry being used by
|
|
|
|
// the state store).
|
|
|
|
if entry != nil {
|
2017-04-19 23:00:11 +00:00
|
|
|
if op.Verb == api.KVGet {
|
2016-05-13 08:47:55 +00:00
|
|
|
result := structs.TxnResult{KV: entry}
|
|
|
|
return structs.TxnResults{&result}, nil
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clone := entry.Clone()
|
|
|
|
clone.Value = nil
|
2016-05-13 08:47:55 +00:00
|
|
|
result := structs.TxnResult{KV: clone}
|
|
|
|
return structs.TxnResults{&result}, nil
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:57:35 +00:00
|
|
|
// txnSession handles all Session-related operations.
|
2020-09-03 23:38:03 +00:00
|
|
|
func txnSession(tx WriteTxn, idx uint64, op *structs.TxnSessionOp) error {
|
2019-11-25 17:57:35 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch op.Verb {
|
|
|
|
case api.SessionDelete:
|
2020-07-10 00:56:43 +00:00
|
|
|
err = sessionDeleteWithSession(tx, &op.Session, idx)
|
2019-11-25 17:57:35 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown Session verb %q", op.Verb)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete session: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// txnLegacyIntention handles all Intention-related operations.
|
|
|
|
//
|
|
|
|
// Deprecated: see TxnOp.Intention description
|
2020-09-03 23:38:03 +00:00
|
|
|
func txnLegacyIntention(tx WriteTxn, idx uint64, op *structs.TxnIntentionOp) error {
|
2018-10-18 12:45:37 +00:00
|
|
|
switch op.Op {
|
|
|
|
case structs.IntentionOpCreate, structs.IntentionOpUpdate:
|
2020-10-06 18:24:05 +00:00
|
|
|
return legacyIntentionSetTxn(tx, idx, op.Intention)
|
2018-10-18 12:45:37 +00:00
|
|
|
case structs.IntentionOpDelete:
|
2020-10-06 18:24:05 +00:00
|
|
|
return legacyIntentionDeleteTxn(tx, idx, op.Intention.ID)
|
|
|
|
case structs.IntentionOpDeleteAll:
|
|
|
|
fallthrough // deliberately not available via this api
|
|
|
|
case structs.IntentionOpUpsert:
|
|
|
|
fallthrough // deliberately not available via this api
|
2018-10-18 12:45:37 +00:00
|
|
|
default:
|
2018-10-29 18:41:42 +00:00
|
|
|
return fmt.Errorf("unknown Intention op %q", op.Op)
|
2018-10-18 12:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 07:51:18 +00:00
|
|
|
// txnNode handles all Node-related operations.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) txnNode(tx WriteTxn, idx uint64, op *structs.TxnNodeOp) (structs.TxnResults, error) {
|
2018-12-03 07:51:18 +00:00
|
|
|
var entry *structs.Node
|
|
|
|
var err error
|
|
|
|
|
2018-12-12 20:46:33 +00:00
|
|
|
getNode := func() (*structs.Node, error) {
|
2018-12-12 17:14:02 +00:00
|
|
|
if op.Node.ID != "" {
|
2018-12-12 20:46:33 +00:00
|
|
|
return getNodeIDTxn(tx, op.Node.ID)
|
2018-12-12 17:14:02 +00:00
|
|
|
} else {
|
2018-12-12 20:46:33 +00:00
|
|
|
return getNodeTxn(tx, op.Node.Node)
|
2018-12-12 17:14:02 +00:00
|
|
|
}
|
2018-12-12 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch op.Verb {
|
|
|
|
case api.NodeGet:
|
|
|
|
entry, err = getNode()
|
2018-12-12 17:14:02 +00:00
|
|
|
if entry == nil && err == nil {
|
|
|
|
err = fmt.Errorf("node %q doesn't exist", op.Node.Node)
|
|
|
|
}
|
2018-12-03 07:51:18 +00:00
|
|
|
|
|
|
|
case api.NodeSet:
|
2020-08-11 05:52:36 +00:00
|
|
|
err = s.ensureNodeTxn(tx, idx, false, &op.Node)
|
2018-12-12 13:22:25 +00:00
|
|
|
if err == nil {
|
2018-12-12 20:46:33 +00:00
|
|
|
entry, err = getNode()
|
2018-12-12 13:22:25 +00:00
|
|
|
}
|
2018-12-03 07:51:18 +00:00
|
|
|
|
|
|
|
case api.NodeCAS:
|
|
|
|
var ok bool
|
|
|
|
ok, err = s.ensureNodeCASTxn(tx, idx, &op.Node)
|
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to set node %q, index is stale", op.Node.Node)
|
2018-12-12 13:22:25 +00:00
|
|
|
break
|
2018-12-03 07:51:18 +00:00
|
|
|
}
|
2018-12-12 20:46:33 +00:00
|
|
|
entry, err = getNode()
|
2018-12-03 07:51:18 +00:00
|
|
|
|
|
|
|
case api.NodeDelete:
|
|
|
|
err = s.deleteNodeTxn(tx, idx, op.Node.Node)
|
|
|
|
|
|
|
|
case api.NodeDeleteCAS:
|
|
|
|
var ok bool
|
|
|
|
ok, err = s.deleteNodeCASTxn(tx, idx, op.Node.ModifyIndex, op.Node.Node)
|
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to delete node %q, index is stale", op.Node.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown Node verb %q", op.Verb)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a GET we keep the value, otherwise we clone and blank out the
|
|
|
|
// value (we have to clone so we don't modify the entry being used by
|
|
|
|
// the state store).
|
|
|
|
if entry != nil {
|
|
|
|
if op.Verb == api.NodeGet {
|
|
|
|
result := structs.TxnResult{Node: entry}
|
|
|
|
return structs.TxnResults{&result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clone := *entry
|
|
|
|
result := structs.TxnResult{Node: &clone}
|
|
|
|
return structs.TxnResults{&result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-12-03 08:41:24 +00:00
|
|
|
// txnService handles all Service-related operations.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) txnService(tx WriteTxn, idx uint64, op *structs.TxnServiceOp) (structs.TxnResults, error) {
|
2018-12-03 08:41:24 +00:00
|
|
|
switch op.Verb {
|
|
|
|
case api.ServiceGet:
|
2020-07-10 00:56:43 +00:00
|
|
|
entry, err := getNodeServiceTxn(tx, op.Node, op.Service.ID, &op.Service.EnterpriseMeta)
|
2020-05-19 20:48:17 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
case entry == nil:
|
2020-05-14 18:41:20 +00:00
|
|
|
return nil, fmt.Errorf("service %q on node %q doesn't exist", op.Service.ID, op.Node)
|
2020-05-19 20:48:17 +00:00
|
|
|
default:
|
|
|
|
return structs.TxnResults{&structs.TxnResult{Service: entry}}, nil
|
2018-12-12 17:14:02 +00:00
|
|
|
}
|
2018-12-03 08:41:24 +00:00
|
|
|
|
|
|
|
case api.ServiceSet:
|
2020-08-11 05:52:36 +00:00
|
|
|
if err := ensureServiceTxn(tx, idx, op.Node, false, &op.Service); err != nil {
|
2020-05-14 18:41:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-10 00:56:43 +00:00
|
|
|
entry, err := getNodeServiceTxn(tx, op.Node, op.Service.ID, &op.Service.EnterpriseMeta)
|
2020-05-19 20:48:17 +00:00
|
|
|
return newTxnResultFromNodeServiceEntry(entry), err
|
2018-12-03 08:41:24 +00:00
|
|
|
|
|
|
|
case api.ServiceCAS:
|
2020-07-10 00:56:43 +00:00
|
|
|
err := ensureServiceCASTxn(tx, idx, op.Node, &op.Service)
|
2020-05-20 16:43:33 +00:00
|
|
|
switch {
|
|
|
|
case err == errCASCompareFailed:
|
2020-05-19 20:48:17 +00:00
|
|
|
err := fmt.Errorf("failed to set service %q on node %q, index is stale", op.Service.ID, op.Node)
|
|
|
|
return nil, err
|
2020-05-20 16:43:33 +00:00
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
2018-12-03 08:41:24 +00:00
|
|
|
}
|
2020-05-19 20:48:17 +00:00
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
entry, err := getNodeServiceTxn(tx, op.Node, op.Service.ID, &op.Service.EnterpriseMeta)
|
2020-05-19 20:48:17 +00:00
|
|
|
return newTxnResultFromNodeServiceEntry(entry), err
|
2018-12-03 08:41:24 +00:00
|
|
|
|
|
|
|
case api.ServiceDelete:
|
2020-05-19 20:48:17 +00:00
|
|
|
err := s.deleteServiceTxn(tx, idx, op.Node, op.Service.ID, &op.Service.EnterpriseMeta)
|
|
|
|
return nil, err
|
2018-12-03 08:41:24 +00:00
|
|
|
|
|
|
|
case api.ServiceDeleteCAS:
|
2020-05-19 20:48:17 +00:00
|
|
|
ok, err := s.deleteServiceCASTxn(tx, idx, op.Service.ModifyIndex, op.Node, op.Service.ID, &op.Service.EnterpriseMeta)
|
2018-12-03 08:41:24 +00:00
|
|
|
if !ok && err == nil {
|
2020-05-14 18:41:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to delete service %q on node %q, index is stale", op.Service.ID, op.Node)
|
2018-12-03 08:41:24 +00:00
|
|
|
}
|
2020-05-19 20:48:17 +00:00
|
|
|
return nil, err
|
2018-12-03 08:41:24 +00:00
|
|
|
|
|
|
|
default:
|
2020-05-14 18:41:20 +00:00
|
|
|
return nil, fmt.Errorf("unknown Service verb %q", op.Verb)
|
2018-12-03 08:41:24 +00:00
|
|
|
}
|
2020-05-19 20:48:17 +00:00
|
|
|
}
|
2018-12-03 08:41:24 +00:00
|
|
|
|
2020-05-19 20:48:17 +00:00
|
|
|
// newTxnResultFromNodeServiceEntry returns a TxnResults with a single result,
|
|
|
|
// a copy of entry. The entry is copied to prevent modification of the state
|
|
|
|
// store.
|
|
|
|
func newTxnResultFromNodeServiceEntry(entry *structs.NodeService) structs.TxnResults {
|
|
|
|
if entry == nil {
|
|
|
|
return nil
|
2018-12-03 08:41:24 +00:00
|
|
|
}
|
2020-05-19 20:48:17 +00:00
|
|
|
clone := *entry
|
|
|
|
result := structs.TxnResult{Service: &clone}
|
|
|
|
return structs.TxnResults{&result}
|
2018-12-03 08:41:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:41:42 +00:00
|
|
|
// txnCheck handles all Check-related operations.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) txnCheck(tx WriteTxn, idx uint64, op *structs.TxnCheckOp) (structs.TxnResults, error) {
|
2018-10-29 18:41:42 +00:00
|
|
|
var entry *structs.HealthCheck
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch op.Verb {
|
|
|
|
case api.CheckGet:
|
2020-07-10 00:56:43 +00:00
|
|
|
_, entry, err = getNodeCheckTxn(tx, op.Check.Node, op.Check.CheckID, &op.Check.EnterpriseMeta)
|
2018-10-29 18:41:42 +00:00
|
|
|
if entry == nil && err == nil {
|
|
|
|
err = fmt.Errorf("check %q on node %q doesn't exist", op.Check.CheckID, op.Check.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
case api.CheckSet:
|
2020-08-11 05:52:36 +00:00
|
|
|
err = s.ensureCheckTxn(tx, idx, false, &op.Check)
|
2018-12-12 13:22:25 +00:00
|
|
|
if err == nil {
|
2020-07-10 00:56:43 +00:00
|
|
|
_, entry, err = getNodeCheckTxn(tx, op.Check.Node, op.Check.CheckID, &op.Check.EnterpriseMeta)
|
2018-12-12 13:22:25 +00:00
|
|
|
}
|
2018-10-29 18:41:42 +00:00
|
|
|
|
|
|
|
case api.CheckCAS:
|
|
|
|
var ok bool
|
|
|
|
entry = &op.Check
|
|
|
|
ok, err = s.ensureCheckCASTxn(tx, idx, entry)
|
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to set check %q on node %q, index is stale", entry.CheckID, entry.Node)
|
2018-12-12 13:22:25 +00:00
|
|
|
break
|
2018-10-29 18:41:42 +00:00
|
|
|
}
|
2020-07-10 00:56:43 +00:00
|
|
|
_, entry, err = getNodeCheckTxn(tx, op.Check.Node, op.Check.CheckID, &op.Check.EnterpriseMeta)
|
2018-10-29 18:41:42 +00:00
|
|
|
|
|
|
|
case api.CheckDelete:
|
2019-12-10 02:26:41 +00:00
|
|
|
err = s.deleteCheckTxn(tx, idx, op.Check.Node, op.Check.CheckID, &op.Check.EnterpriseMeta)
|
2018-10-29 18:41:42 +00:00
|
|
|
|
|
|
|
case api.CheckDeleteCAS:
|
|
|
|
var ok bool
|
2019-12-10 02:26:41 +00:00
|
|
|
ok, err = s.deleteCheckCASTxn(tx, idx, op.Check.ModifyIndex, op.Check.Node, op.Check.CheckID, &op.Check.EnterpriseMeta)
|
2018-10-29 18:41:42 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
err = fmt.Errorf("failed to delete check %q on node %q, index is stale", op.Check.CheckID, op.Check.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown Check verb %q", op.Verb)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a GET we keep the value, otherwise we clone and blank out the
|
|
|
|
// value (we have to clone so we don't modify the entry being used by
|
|
|
|
// the state store).
|
|
|
|
if entry != nil {
|
|
|
|
if op.Verb == api.CheckGet {
|
|
|
|
result := structs.TxnResult{Check: entry}
|
|
|
|
return structs.TxnResults{&result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
clone := entry.Clone()
|
|
|
|
result := structs.TxnResult{Check: clone}
|
|
|
|
return structs.TxnResults{&result}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// txnDispatch runs the given operations inside the state store transaction.
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) txnDispatch(tx WriteTxn, idx uint64, ops structs.TxnOps) (structs.TxnResults, structs.TxnErrors) {
|
2016-05-11 08:35:27 +00:00
|
|
|
results := make(structs.TxnResults, 0, len(ops))
|
|
|
|
errors := make(structs.TxnErrors, 0, len(ops))
|
|
|
|
for i, op := range ops {
|
2016-05-13 08:47:55 +00:00
|
|
|
var ret structs.TxnResults
|
2016-05-11 08:35:27 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// Dispatch based on the type of operation.
|
2018-10-18 12:45:37 +00:00
|
|
|
switch {
|
|
|
|
case op.KV != nil:
|
2016-05-13 08:47:55 +00:00
|
|
|
ret, err = s.txnKVS(tx, idx, op.KV)
|
2018-12-03 08:41:24 +00:00
|
|
|
case op.Node != nil:
|
|
|
|
ret, err = s.txnNode(tx, idx, op.Node)
|
|
|
|
case op.Service != nil:
|
|
|
|
ret, err = s.txnService(tx, idx, op.Service)
|
|
|
|
case op.Check != nil:
|
|
|
|
ret, err = s.txnCheck(tx, idx, op.Check)
|
2019-11-25 17:57:35 +00:00
|
|
|
case op.Session != nil:
|
2020-08-11 20:31:23 +00:00
|
|
|
err = txnSession(tx, idx, op.Session)
|
2020-10-06 18:24:05 +00:00
|
|
|
case op.Intention != nil:
|
|
|
|
// NOTE: this branch is deprecated and exists for backwards
|
|
|
|
// compatibility with pre-1.9.0 raft logs and during upgrades.
|
|
|
|
err = txnLegacyIntention(tx, idx, op.Intention)
|
2018-10-18 12:45:37 +00:00
|
|
|
default:
|
2016-05-11 08:35:27 +00:00
|
|
|
err = fmt.Errorf("no operation specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate the results.
|
2016-05-13 08:47:55 +00:00
|
|
|
results = append(results, ret...)
|
2016-05-11 08:35:27 +00:00
|
|
|
|
|
|
|
// Capture any error along with the index of the operation that
|
|
|
|
// failed.
|
|
|
|
if err != nil {
|
2017-03-23 23:05:35 +00:00
|
|
|
errors = append(errors, &structs.TxnError{
|
|
|
|
OpIndex: i,
|
|
|
|
What: err.Error(),
|
|
|
|
})
|
2016-05-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return nil, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxnRW tries to run the given operations all inside a single transaction. If
|
|
|
|
// any of the operations fail, the entire transaction will be rolled back. This
|
|
|
|
// is done in a full write transaction on the state store, so reads and writes
|
|
|
|
// are possible
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) TxnRW(idx uint64, ops structs.TxnOps) (structs.TxnResults, structs.TxnErrors) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2016-05-13 00:38:25 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
results, errors := s.txnDispatch(tx, idx, ops)
|
2016-05-11 08:35:27 +00:00
|
|
|
if len(errors) > 0 {
|
|
|
|
return nil, errors
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return nil, structs.TxnErrors{
|
|
|
|
{What: err.Error(), OpIndex: 0},
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 08:35:27 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
2016-05-13 00:38:25 +00:00
|
|
|
|
|
|
|
// TxnRO runs the given operations inside a single read transaction in the state
|
|
|
|
// store. You must verify outside this function that no write operations are
|
|
|
|
// present, otherwise you'll get an error from the state store.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) TxnRO(ops structs.TxnOps) (structs.TxnResults, structs.TxnErrors) {
|
2016-05-13 00:38:25 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
results, errors := s.txnDispatch(tx, 0, ops)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return nil, errors
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|