feat(get): get from quorum

This commit is contained in:
Xiang Li 2014-06-22 21:33:38 -07:00
parent fb32a999a6
commit 973bde9a07
4 changed files with 52 additions and 2 deletions

View File

@ -17,6 +17,14 @@ func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
vars := mux.Vars(req) vars := mux.Vars(req)
key := "/" + vars["key"] key := "/" + vars["key"]
recursive := (req.FormValue("recursive") == "true")
sort := (req.FormValue("sorted") == "true")
if req.FormValue("quorum") == "true" {
c := s.Store().CommandFactory().CreateGetCommand(key, recursive, sort)
return s.Dispatch(c, w, req)
}
// Help client to redirect the request to the current leader // Help client to redirect the request to the current leader
if req.FormValue("consistent") == "true" && s.State() != raft.Leader { if req.FormValue("consistent") == "true" && s.State() != raft.Leader {
leader := s.Leader() leader := s.Leader()
@ -35,8 +43,6 @@ func GetHandler(w http.ResponseWriter, req *http.Request, s Server) error {
return nil return nil
} }
recursive := (req.FormValue("recursive") == "true")
sort := (req.FormValue("sorted") == "true")
waitIndex := req.FormValue("waitIndex") waitIndex := req.FormValue("waitIndex")
stream := (req.FormValue("stream") == "true") stream := (req.FormValue("stream") == "true")

View File

@ -24,6 +24,7 @@ type CommandFactory interface {
prevIndex uint64, expireTime time.Time) raft.Command prevIndex uint64, expireTime time.Time) raft.Command
CreateCompareAndDeleteCommand(key string, prevValue string, prevIndex uint64) raft.Command CreateCompareAndDeleteCommand(key string, prevValue string, prevIndex uint64) raft.Command
CreateSyncCommand(now time.Time) raft.Command CreateSyncCommand(now time.Time) raft.Command
CreateGetCommand(key string, recursive, sorted bool) raft.Command
} }
// RegisterCommandFactory adds a command factory to the global registry. // RegisterCommandFactory adds a command factory to the global registry.

View File

@ -89,3 +89,11 @@ func (f *CommandFactory) CreateSyncCommand(now time.Time) raft.Command {
Time: time.Now(), Time: time.Now(),
} }
} }
func (f *CommandFactory) CreateGetCommand(key string, recursive, sorted bool) raft.Command {
return &GetCommand{
Key: key,
Recursive: recursive,
Sorted: sorted,
}
}

35
store/v2/get_command.go Normal file
View File

@ -0,0 +1,35 @@
package v2
import (
"github.com/coreos/etcd/log"
"github.com/coreos/etcd/store"
"github.com/coreos/etcd/third_party/github.com/goraft/raft"
)
func init() {
raft.RegisterCommand(&GetCommand{})
}
// The GetCommand gets a key from the Store.
type GetCommand struct {
Key string `json:"key"`
Recursive bool `json:"recursive"`
Sorted bool `json:sorted`
}
// The name of the get command in the log
func (c *GetCommand) CommandName() string {
return "etcd:get"
}
// Get the key
func (c *GetCommand) Apply(context raft.Context) (interface{}, error) {
s, _ := context.Server().StateMachine().(store.Store)
e, err := s.Get(c.Key, c.Recursive, c.Sorted)
if err != nil {
log.Debug(err)
return nil, err
}
return e, nil
}