2016-05-11 08:35:27 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-05-11 08:35:27 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2016-05-12 23:11:26 +00:00
|
|
|
// txnKVS handles all KV-related operations.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) txnKVS(tx *memdb.Txn, 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
|
|
|
|
err = s.kvsSetTxn(tx, idx, entry, false)
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDelete:
|
2016-05-11 08:35:27 +00:00
|
|
|
err = s.kvsDeleteTxn(tx, idx, op.DirEnt.Key)
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVDeleteCAS:
|
2016-05-11 08:35:27 +00:00
|
|
|
var ok bool
|
|
|
|
ok, err = s.kvsDeleteCASTxn(tx, idx, op.DirEnt.ModifyIndex, op.DirEnt.Key)
|
|
|
|
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:
|
2016-05-11 08:35:27 +00:00
|
|
|
err = s.kvsDeleteTreeTxn(tx, idx, op.DirEnt.Key)
|
|
|
|
|
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
|
|
|
|
ok, err = s.kvsSetCASTxn(tx, idx, entry)
|
|
|
|
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
|
|
|
|
ok, err = s.kvsLockTxn(tx, idx, entry)
|
|
|
|
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
|
|
|
|
ok, err = s.kvsUnlockTxn(tx, idx, entry)
|
|
|
|
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:
|
2017-01-24 19:20:51 +00:00
|
|
|
_, entry, err = s.kvsGetTxn(tx, nil, op.DirEnt.Key)
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, entries, err = s.kvsListTxn(tx, nil, op.DirEnt.Key)
|
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:
|
2016-05-11 08:35:27 +00:00
|
|
|
entry, err = s.kvsCheckSessionTxn(tx, op.DirEnt.Key, op.DirEnt.Session)
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
case api.KVCheckIndex:
|
2016-05-11 08:35:27 +00:00
|
|
|
entry, err = s.kvsCheckIndexTxn(tx, op.DirEnt.Key, op.DirEnt.ModifyIndex)
|
|
|
|
|
2017-04-21 00:50:52 +00:00
|
|
|
case api.KVCheckNotExists:
|
|
|
|
_, entry, err = s.kvsGetTxn(tx, nil, op.DirEnt.Key)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:38:25 +00:00
|
|
|
// txnDispatch runs the given operations inside the state store transaction.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) txnDispatch(tx *memdb.Txn, 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.
|
2016-05-11 17:58:27 +00:00
|
|
|
if op.KV != nil {
|
2016-05-13 08:47:55 +00:00
|
|
|
ret, err = s.txnKVS(tx, idx, op.KV)
|
2016-05-11 08:35:27 +00:00
|
|
|
} else {
|
|
|
|
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) {
|
2016-05-13 00:38:25 +00:00
|
|
|
tx := s.db.Txn(true)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
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
|
|
|
|
}
|