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
|
|
|
import (
|
2013-06-21 02:59:23 +04:00
|
|
|
"encoding/json"
|
2013-07-31 09:39:45 +04:00
|
|
|
"fmt"
|
2013-07-08 20:41:04 +04:00
|
|
|
"github.com/coreos/etcd/store"
|
|
|
|
"github.com/coreos/go-raft"
|
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
|
|
|
)
|
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-29 02:37:29 +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
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// The name of the set command in the log
|
2013-06-09 21:42:34 +04:00
|
|
|
func (c *SetCommand) CommandName() string {
|
2013-07-26 23:15:42 +04:00
|
|
|
return "etcd:set"
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// Set the key-value pair
|
2013-06-29 02:37:29 +04:00
|
|
|
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-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
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 "testAndSet"
|
|
|
|
}
|
|
|
|
|
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())
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2013-06-09 21:42:34 +04:00
|
|
|
func (c *GetCommand) CommandName() string {
|
2013-07-26 23:15:42 +04:00
|
|
|
return "etcd:get"
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// Get the value of key
|
2013-06-29 02:37:29 +04:00
|
|
|
func (c *GetCommand) Apply(server *raft.Server) (interface{}, error) {
|
2013-07-12 19:41:28 +04:00
|
|
|
return etcdStore.Get(c.Key)
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2013-06-09 21:42:34 +04:00
|
|
|
func (c *DeleteCommand) CommandName() string {
|
2013-07-26 23:15:42 +04:00
|
|
|
return "etcd:delete"
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-06-21 02:59:23 +04:00
|
|
|
// Delete the key
|
2013-06-29 02:37:29 +04:00
|
|
|
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
|
2013-07-10 00:14:12 +04:00
|
|
|
return etcdStore.Delete(c.Key, server.CommitIndex())
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 01:05:49 +04:00
|
|
|
// Watch command
|
|
|
|
type WatchCommand struct {
|
2013-07-01 03:30:41 +04:00
|
|
|
Key string `json:"key"`
|
|
|
|
SinceIndex uint64 `json:"sinceIndex"`
|
2013-06-11 01:05:49 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// The name of the watch command in the log
|
2013-06-11 01:05:49 +04:00
|
|
|
func (c *WatchCommand) CommandName() string {
|
2013-07-26 23:15:42 +04:00
|
|
|
return "etcd:watch"
|
2013-06-11 01:05:49 +04:00
|
|
|
}
|
|
|
|
|
2013-06-29 02:37:29 +04:00
|
|
|
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-11 01:05:49 +04:00
|
|
|
|
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-11 01:05:49 +04:00
|
|
|
|
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
|
2013-06-11 01:05:49 +04:00
|
|
|
|
2013-08-04 10:29:05 +04:00
|
|
|
if res == nil {
|
|
|
|
return nil, fmt.Errorf("watcher is cleared")
|
|
|
|
}
|
|
|
|
|
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-07-14 22:45:35 +04:00
|
|
|
Name string `json:"name"`
|
|
|
|
Hostname string `json:"hostName"`
|
|
|
|
RaftPort int `json:"raftPort"`
|
|
|
|
ClientPort int `json:"clientPort"`
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// The name of the join command in the log
|
2013-06-10 08:46:30 +04:00
|
|
|
func (c *JoinCommand) CommandName() string {
|
2013-07-26 23:15:42 +04:00
|
|
|
return "etcd:join"
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 01:02:59 +04:00
|
|
|
// Join a server to the cluster
|
2013-07-14 22:45:35 +04:00
|
|
|
func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
|
2013-07-31 22:05:12 +04:00
|
|
|
|
2013-08-04 09:33:56 +04:00
|
|
|
// 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))
|
|
|
|
|
|
|
|
if response != nil {
|
|
|
|
return []byte("join success"), nil
|
|
|
|
}
|
|
|
|
|
2013-07-31 22:05:12 +04:00
|
|
|
// check machine number in the cluster
|
|
|
|
num := machineNum()
|
|
|
|
if num == maxClusterSize {
|
|
|
|
return []byte("join fail"), fmt.Errorf(errors[103])
|
|
|
|
}
|
|
|
|
|
|
|
|
// add peer in raft
|
2013-07-14 22:45:35 +04:00
|
|
|
err := raftServer.AddPeer(c.Name)
|
2013-07-31 22:05:12 +04:00
|
|
|
|
|
|
|
// add machine in etcd storage
|
2013-08-03 07:21:11 +04:00
|
|
|
key := path.Join("_etcd/machines", c.Name)
|
2013-07-31 09:39:45 +04:00
|
|
|
value := fmt.Sprintf("%s,%d,%d", c.Hostname, c.RaftPort, c.ClientPort)
|
|
|
|
etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
|
2013-07-31 22:05:12 +04:00
|
|
|
|
2013-07-14 06:13:07 +04:00
|
|
|
return []byte("join success"), err
|
2013-06-09 21:42:34 +04:00
|
|
|
}
|
2013-07-26 23:15:42 +04:00
|
|
|
|
|
|
|
func (c *JoinCommand) NodeName() string {
|
|
|
|
return c.Name
|
|
|
|
}
|