2013-06-09 21:42:34 +04:00
|
|
|
package main
|
2013-06-21 02:59:23 +04:00
|
|
|
|
2013-06-09 21:42:34 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Commands
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import (
|
2013-06-21 02:59:23 +04:00
|
|
|
"encoding/json"
|
2013-06-28 20:58:41 +04:00
|
|
|
"github.com/xiangli-cmu/go-raft"
|
2013-06-21 02:26:31 +04:00
|
|
|
"github.com/xiangli-cmu/raft-etcd/store"
|
2013-06-17 01:02:07 +04:00
|
|
|
"time"
|
2013-06-21 02:59:23 +04:00
|
|
|
)
|
2013-06-09 21:42:34 +04:00
|
|
|
|
|
|
|
// A command represents an action to be taken on the replicated state machine.
|
|
|
|
type Command interface {
|
|
|
|
CommandName() string
|
2013-06-28 20:58:41 +04:00
|
|
|
Apply(server *raft.Server) (interface {}, error)
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set command
|
|
|
|
type SetCommand struct {
|
2013-06-21 02:59:23 +04:00
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
2013-06-17 02:07:45 +04:00
|
|
|
ExpireTime time.Time `json:"expireTime"`
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The name of the command in the log
|
|
|
|
func (c *SetCommand) CommandName() string {
|
|
|
|
return "set"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value of key to value
|
2013-06-28 20:58:41 +04:00
|
|
|
func (c *SetCommand) Apply(server *raft.Server) (interface {}, error) {
|
2013-06-19 02:04:30 +04:00
|
|
|
return store.Set(c.Key, c.Value, c.ExpireTime)
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-06-13 22:01:06 +04:00
|
|
|
// Get the path for http request
|
2013-06-11 01:05:49 +04:00
|
|
|
func (c *SetCommand) GeneratePath() string {
|
2013-06-12 03:01:12 +04:00
|
|
|
return "set/" + c.Key
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get command
|
|
|
|
type GetCommand struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name of the command in the log
|
|
|
|
func (c *GetCommand) CommandName() string {
|
|
|
|
return "get"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value of key to value
|
2013-06-28 20:58:41 +04:00
|
|
|
func (c *GetCommand) Apply(server *raft.Server) (interface {}, error) {
|
2013-06-18 22:13:24 +04:00
|
|
|
res := store.Get(c.Key)
|
2013-06-09 21:42:34 +04:00
|
|
|
return json.Marshal(res)
|
|
|
|
}
|
|
|
|
|
2013-06-21 02:59:23 +04:00
|
|
|
func (c *GetCommand) GeneratePath() string {
|
2013-06-12 03:01:12 +04:00
|
|
|
return "get/" + c.Key
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete command
|
|
|
|
type DeleteCommand struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name of the command in the log
|
|
|
|
func (c *DeleteCommand) CommandName() string {
|
|
|
|
return "delete"
|
|
|
|
}
|
|
|
|
|
2013-06-21 02:59:23 +04:00
|
|
|
// Delete the key
|
2013-06-28 20:58:41 +04:00
|
|
|
func (c *DeleteCommand) Apply(server *raft.Server) (interface {}, error) {
|
2013-06-19 02:04:30 +04:00
|
|
|
return store.Delete(c.Key)
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 01:05:49 +04:00
|
|
|
// Watch command
|
|
|
|
type WatchCommand struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//The name of the command in the log
|
|
|
|
func (c *WatchCommand) CommandName() string {
|
|
|
|
return "watch"
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:58:41 +04:00
|
|
|
func (c *WatchCommand) Apply(server *raft.Server) (interface {}, error) {
|
2013-06-18 22:13:24 +04:00
|
|
|
ch := make(chan store.Response)
|
2013-06-11 01:05:49 +04:00
|
|
|
|
2013-06-13 22:01:06 +04:00
|
|
|
// add to the watchers list
|
2013-06-21 08:34:27 +04:00
|
|
|
store.AddWatcher(c.Key, ch, 0)
|
2013-06-11 01:05:49 +04:00
|
|
|
|
2013-06-13 22:01:06 +04:00
|
|
|
// wait for the notification for any changing
|
2013-06-21 02:59:23 +04:00
|
|
|
res := <-ch
|
2013-06-11 01:05:49 +04:00
|
|
|
|
|
|
|
return json.Marshal(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinCommand
|
2013-06-10 08:46:30 +04:00
|
|
|
type JoinCommand struct {
|
2013-06-09 21:42:34 +04:00
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2013-06-10 08:46:30 +04:00
|
|
|
func (c *JoinCommand) CommandName() string {
|
2013-06-09 21:42:34 +04:00
|
|
|
return "join"
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:58:41 +04:00
|
|
|
func (c *JoinCommand) Apply(server *raft.Server) (interface {}, error) {
|
2013-06-09 21:42:34 +04:00
|
|
|
err := server.AddPeer(c.Name)
|
2013-06-13 22:01:06 +04:00
|
|
|
// no result will be returned
|
2013-06-09 21:42:34 +04:00
|
|
|
return nil, err
|
|
|
|
}
|