2014-03-31 21:15:49 +00:00
|
|
|
package consul
|
|
|
|
|
2014-03-31 23:00:23 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"time"
|
|
|
|
)
|
2014-03-31 21:15:49 +00:00
|
|
|
|
|
|
|
// KVS endpoint is used to manipulate the Key-Value store
|
|
|
|
type KVS struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
|
|
|
|
// Apply is used to apply a KVS request to the data store. This should
|
|
|
|
// only be used for operations that modify the data
|
2014-03-31 23:10:49 +00:00
|
|
|
func (k *KVS) Apply(args *structs.KVSRequest, reply *bool) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := k.srv.forward("KVS.Apply", args, args, reply); done {
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"consul", "kvs", "apply"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the args
|
2014-04-01 03:00:01 +00:00
|
|
|
if args.DirEnt.Key == "" && args.Op != structs.KVSDeleteTree {
|
2014-03-31 23:00:23 +00:00
|
|
|
return fmt.Errorf("Must provide key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the update
|
2014-03-31 23:10:49 +00:00
|
|
|
resp, err := k.srv.raftApply(structs.KVSRequestType, args)
|
2014-03-31 23:00:23 +00:00
|
|
|
if err != nil {
|
2014-03-31 23:10:49 +00:00
|
|
|
k.srv.logger.Printf("[ERR] consul.kvs: Apply failed: %v", err)
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the return type is a bool
|
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to lookup a single key
|
|
|
|
func (k *KVS) Get(args *structs.KeyRequest, reply *structs.IndexedDirEntries) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := k.srv.forward("KVS.Get", args, args, reply); done {
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the local state
|
|
|
|
state := k.srv.fsm.State()
|
2014-04-21 18:31:15 +00:00
|
|
|
return k.srv.blockingRPC(&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2014-03-31 23:00:23 +00:00
|
|
|
state.QueryTables("KVSGet"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2014-03-31 23:00:23 +00:00
|
|
|
index, ent, err := state.KVSGet(args.Key)
|
|
|
|
if err != nil {
|
2014-04-21 18:18:27 +00:00
|
|
|
return err
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
if ent == nil {
|
2014-04-01 03:00:46 +00:00
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
|
|
|
} else {
|
|
|
|
reply.Index = index
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
reply.Entries = nil
|
|
|
|
} else {
|
|
|
|
reply.Index = ent.ModifyIndex
|
|
|
|
reply.Entries = structs.DirEntries{ent}
|
|
|
|
}
|
2014-04-21 18:18:27 +00:00
|
|
|
return nil
|
2014-03-31 23:00:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to list all keys with a given prefix
|
|
|
|
func (k *KVS) List(args *structs.KeyRequest, reply *structs.IndexedDirEntries) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := k.srv.forward("KVS.List", args, args, reply); done {
|
2014-03-31 23:00:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the local state
|
|
|
|
state := k.srv.fsm.State()
|
2014-04-21 18:31:15 +00:00
|
|
|
return k.srv.blockingRPC(&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2014-03-31 23:00:23 +00:00
|
|
|
state.QueryTables("KVSList"),
|
2014-04-21 18:18:27 +00:00
|
|
|
func() error {
|
2014-03-31 23:00:23 +00:00
|
|
|
index, ent, err := state.KVSList(args.Key)
|
|
|
|
if err != nil {
|
2014-04-21 18:18:27 +00:00
|
|
|
return err
|
2014-03-31 23:00:23 +00:00
|
|
|
}
|
|
|
|
if len(ent) == 0 {
|
2014-04-01 03:00:46 +00:00
|
|
|
// Must provide non-zero index to prevent blocking
|
|
|
|
// Index 1 is impossible anyways (due to Raft internals)
|
|
|
|
if index == 0 {
|
|
|
|
reply.Index = 1
|
|
|
|
} else {
|
|
|
|
reply.Index = index
|
|
|
|
}
|
2014-03-31 23:00:23 +00:00
|
|
|
reply.Entries = nil
|
|
|
|
} else {
|
|
|
|
// Determine the maximum affected index
|
|
|
|
var maxIndex uint64
|
|
|
|
for _, e := range ent {
|
|
|
|
if e.ModifyIndex > maxIndex {
|
|
|
|
maxIndex = e.ModifyIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index = maxIndex
|
|
|
|
reply.Entries = ent
|
|
|
|
}
|
2014-04-21 18:18:27 +00:00
|
|
|
return nil
|
2014-03-31 23:00:23 +00:00
|
|
|
})
|
|
|
|
}
|