etcd/command.go

229 lines
5.3 KiB
Go
Raw Normal View History

package main
2013-06-21 02:59:23 +04:00
import (
2013-08-19 23:10:11 +04:00
"encoding/binary"
2013-06-21 02:59:23 +04:00
"encoding/json"
2013-07-31 09:39:45 +04:00
"fmt"
2013-08-18 07:41:15 +04:00
etcdErr "github.com/coreos/etcd/error"
"github.com/coreos/etcd/store"
"github.com/coreos/go-raft"
2013-08-19 23:10:11 +04:00
"os"
2013-07-31 09:39:45 +04:00
"path"
2013-06-17 01:02:07 +04:00
"time"
2013-06-21 02:59:23 +04:00
)
const commandPrefix = "etcd:"
func commandName(name string) string {
return commandPrefix + name
}
// A command represents an action to be taken on the replicated state machine.
type Command interface {
CommandName() string
Apply(server *raft.Server) (interface{}, error)
}
// Set command
type SetCommand struct {
2013-06-21 02:59:23 +04:00
Key string `json:"key"`
Value string `json:"value"`
ExpireTime time.Time `json:"expireTime"`
}
2013-07-10 01:02:59 +04:00
// The name of the set command in the log
func (c *SetCommand) CommandName() string {
return commandName("set")
}
2013-07-10 01:02:59 +04:00
// Set the key-value pair
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
2013-07-10 00:14:12 +04:00
return etcdStore.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
}
2013-07-08 22:00:10 +04:00
// TestAndSet command
type TestAndSetCommand struct {
Key string `json:"key"`
Value string `json:"value"`
PrevValue string `json: prevValue`
ExpireTime time.Time `json:"expireTime"`
}
2013-07-10 01:02:59 +04:00
// The name of the testAndSet command in the log
2013-07-08 22:00:10 +04:00
func (c *TestAndSetCommand) CommandName() string {
return commandName("testAndSet")
2013-07-08 22:00:10 +04:00
}
2013-07-10 01:02:59 +04:00
// Set the key-value pair if the current value of the key equals to the given prevValue
2013-07-08 22:00:10 +04:00
func (c *TestAndSetCommand) Apply(server *raft.Server) (interface{}, error) {
2013-07-10 00:14:12 +04:00
return etcdStore.TestAndSet(c.Key, c.PrevValue, c.Value, c.ExpireTime, server.CommitIndex())
}
// Get command
type GetCommand struct {
Key string `json:"key"`
}
2013-07-10 01:02:59 +04:00
// The name of the get command in the log
func (c *GetCommand) CommandName() string {
return commandName("get")
}
2013-07-10 01:02:59 +04:00
// Get the value of key
func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
2013-07-12 19:41:28 +04:00
return etcdStore.Get(c.Key)
}
// Delete command
type DeleteCommand struct {
Key string `json:"key"`
}
2013-07-10 01:02:59 +04:00
// The name of the delete command in the log
func (c *DeleteCommand) CommandName() string {
return commandName("delete")
}
2013-06-21 02:59:23 +04:00
// Delete the key
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
2013-07-10 00:14:12 +04:00
return etcdStore.Delete(c.Key, server.CommitIndex())
}
// Watch command
type WatchCommand struct {
2013-07-01 03:30:41 +04:00
Key string `json:"key"`
SinceIndex uint64 `json:"sinceIndex"`
}
2013-07-10 01:02:59 +04:00
// The name of the watch command in the log
func (c *WatchCommand) CommandName() string {
return commandName("watch")
}
func (c *WatchCommand) Apply(server *raft.Server) (interface{}, error) {
2013-07-10 01:02:59 +04:00
// create a new watcher
2013-08-05 04:17:40 +04:00
watcher := store.NewWatcher()
2013-06-13 22:01:06 +04:00
// add to the watchers list
2013-07-10 00:14:12 +04:00
etcdStore.AddWatcher(c.Key, watcher, c.SinceIndex)
2013-06-13 22:01:06 +04:00
// wait for the notification for any changing
2013-07-10 00:14:12 +04:00
res := <-watcher.C
if res == nil {
2013-08-06 04:03:15 +04:00
return nil, fmt.Errorf("Clearing watch")
}
return json.Marshal(res)
}
// JoinCommand
type JoinCommand struct {
RaftVersion string `json:"raftVersion"`
2013-08-19 21:46:16 +04:00
Name string `json:"name"`
RaftURL string `json:"raftURL"`
EtcdURL string `json:"etcdURL"`
}
2013-08-15 02:20:25 +04:00
func newJoinCommand() *JoinCommand {
return &JoinCommand{
RaftVersion: r.version,
2013-08-19 21:46:16 +04:00
Name: r.name,
RaftURL: r.url,
EtcdURL: e.url,
2013-08-15 02:20:25 +04:00
}
}
2013-07-10 01:02:59 +04:00
// The name of the join command in the log
func (c *JoinCommand) CommandName() string {
return commandName("join")
}
2013-07-10 01:02:59 +04:00
// Join a server to the cluster
func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
// check if the join command is from a previous machine, who lost all its previous log.
response, _ := etcdStore.RawGet(path.Join("_etcd/machines", c.Name))
2013-08-19 23:10:11 +04:00
b := make([]byte, 8)
binary.PutUvarint(b, raftServer.CommitIndex())
if response != nil {
2013-08-19 23:10:11 +04:00
return b, nil
}
// check machine number in the cluster
num := machineNum()
if num == maxClusterSize {
2013-08-18 07:41:15 +04:00
debug("Reject join request from ", c.Name)
2013-08-19 23:10:11 +04:00
return []byte{0}, etcdErr.NewError(103, "")
}
addNameToURL(c.Name, c.RaftVersion, c.RaftURL, c.EtcdURL)
// add peer in raft
2013-08-19 08:12:36 +04:00
err := raftServer.AddPeer(c.Name, "")
// add machine in etcd storage
key := path.Join("_etcd/machines", c.Name)
value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
2013-07-31 09:39:45 +04:00
etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
2013-08-19 23:10:11 +04:00
return b, err
}
func (c *JoinCommand) NodeName() string {
return c.Name
}
2013-08-19 23:10:11 +04:00
// RemoveCommand
type RemoveCommand struct {
Name string `json:"name"`
}
// The name of the remove command in the log
func (c *RemoveCommand) CommandName() string {
2013-08-20 00:43:12 +04:00
return commandName("remove")
2013-08-19 23:10:11 +04:00
}
// Remove a server from the cluster
func (c *RemoveCommand) Apply(raftServer *raft.Server) (interface{}, error) {
// remove machine in etcd storage
key := path.Join("_etcd/machines", c.Name)
_, err := etcdStore.Delete(key, raftServer.CommitIndex())
if err != nil {
return []byte{0}, err
}
// remove peer in raft
err = raftServer.RemovePeer(c.Name)
if err != nil {
return []byte{0}, err
}
if c.Name == raftServer.Name() {
// the removed node is this node
2013-08-20 00:42:00 +04:00
// if the node is not replaying the previous logs
2013-08-19 23:10:11 +04:00
// and the node has sent out a join request in this
// start. It is sure that this node received a new remove
// command and need to be removed
if raftServer.CommitIndex() > r.joinIndex && r.joinIndex != 0 {
debugf("server [%s] is removed", raftServer.Name())
os.Exit(0)
} else {
// else ignore remove
debugf("ignore previous remove command.")
2013-08-19 23:10:11 +04:00
}
}
b := make([]byte, 8)
binary.PutUvarint(b, raftServer.CommitIndex())
return b, err
}