Skip to content

Commit

Permalink
Merge pull request etcd-io#18 from etcd-team/pb_cmd
Browse files Browse the repository at this point in the history
etcd: make cmd a protobuf type
  • Loading branch information
xiang90 committed Aug 4, 2014
2 parents 49c08e2 + df0af00 commit 9f3cba1
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 45 deletions.
54 changes: 35 additions & 19 deletions etcd/v2_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package etcd

import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/store"
Expand All @@ -30,29 +30,29 @@ func (p *participant) v2apply(index int64, ent raft.Entry) {
var e *store.Event
var err error

cmd := new(cmd)
if err := json.Unmarshal(ent.Data, cmd); err != nil {
var cmd Cmd
if err := cmd.Unmarshal(ent.Data); err != nil {
log.Printf("id=%x participant.store.apply decodeErr=\"%v\"\n", p.id, err)
return
}

switch cmd.Type {
case "set":
e, err = p.Store.Set(cmd.Key, cmd.Dir, cmd.Value, cmd.Time)
case "update":
e, err = p.Store.Update(cmd.Key, cmd.Value, cmd.Time)
case "create", "unique":
e, err = p.Store.Create(cmd.Key, cmd.Dir, cmd.Value, cmd.Unique, cmd.Time)
case "delete":
e, err = p.Store.Delete(cmd.Key, cmd.Dir, cmd.Recursive)
case "cad":
e, err = p.Store.CompareAndDelete(cmd.Key, cmd.PrevValue, cmd.PrevIndex)
case "cas":
e, err = p.Store.CompareAndSwap(cmd.Key, cmd.PrevValue, cmd.PrevIndex, cmd.Value, cmd.Time)
case "quorumGet":
e, err = p.Store.Get(cmd.Key, cmd.Recursive, cmd.Sorted)
case "sync":
p.Store.DeleteExpiredKeys(cmd.Time)
case stset:
e, err = p.Store.Set(cmd.Key, *cmd.Dir, *cmd.Value, mustUnmarshalTime(cmd.Time))
case stupdate:
e, err = p.Store.Update(cmd.Key, *cmd.Value, mustUnmarshalTime(cmd.Time))
case stcreate:
e, err = p.Store.Create(cmd.Key, *cmd.Dir, *cmd.Value, *cmd.Unique, mustUnmarshalTime(cmd.Time))
case stdelete:
e, err = p.Store.Delete(cmd.Key, *cmd.Dir, *cmd.Recursive)
case stcad:
e, err = p.Store.CompareAndDelete(cmd.Key, *cmd.PrevValue, *cmd.PrevIndex)
case stcas:
e, err = p.Store.CompareAndSwap(cmd.Key, *cmd.PrevValue, *cmd.PrevIndex, *cmd.Value, mustUnmarshalTime(cmd.Time))
case stqget:
e, err = p.Store.Get(cmd.Key, *cmd.Recursive, *cmd.Sorted)
case stsync:
p.Store.DeleteExpiredKeys(mustUnmarshalTime(cmd.Time))
return
default:
log.Printf("id=%x participant.store.apply err=\"unexpected command type %s\"\n", p.id, cmd.Type)
Expand Down Expand Up @@ -81,3 +81,19 @@ func (p *participant) v2apply(index int64, ent raft.Entry) {
p.node.result[w] <- ret
delete(p.node.result, w)
}

func mustMarshalTime(t *time.Time) []byte {
b, err := t.MarshalBinary()
if err != nil {
panic(err)
}
return b
}

func mustUnmarshalTime(b []byte) time.Time {
var time time.Time
if err := time.UnmarshalBinary(b); err != nil {
panic(err)
}
return time
}
6 changes: 3 additions & 3 deletions etcd/v2_raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package etcd

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -54,8 +53,9 @@ func (r *v2Raft) Sync() {
if !r.Node.IsLeader() {
return
}
sync := &cmd{Type: "sync", Time: time.Now()}
data, err := json.Marshal(sync)
t := time.Now()
sync := &Cmd{Type: stsync, Time: mustMarshalTime(&t)}
data, err := sync.Marshal()
if err != nil {
panic(err)
}
Expand Down
42 changes: 19 additions & 23 deletions etcd/v2_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,59 @@ limitations under the License.
package etcd

import (
"encoding/json"
"fmt"
"time"

"github.com/coreos/etcd/store"
)

type cmd struct {
Type string
Key string
Value string
PrevValue string
PrevIndex uint64
Dir bool
Recursive bool
Unique bool
Sorted bool
Time time.Time
}
const (
stset = iota
stcreate
stupdate
stcas
stdelete
stcad
stqget
stsync
)

func (p *participant) Set(key string, dir bool, value string, expireTime time.Time) (*store.Event, error) {
set := &cmd{Type: "set", Key: key, Dir: dir, Value: value, Time: expireTime}
set := &Cmd{Type: stset, Key: key, Dir: &dir, Value: &value, Time: mustMarshalTime(&expireTime)}
return p.do(set)
}

func (p *participant) Create(key string, dir bool, value string, expireTime time.Time, unique bool) (*store.Event, error) {
create := &cmd{Type: "create", Key: key, Dir: dir, Value: value, Time: expireTime, Unique: unique}
create := &Cmd{Type: stcreate, Key: key, Dir: &dir, Value: &value, Time: mustMarshalTime(&expireTime), Unique: &unique}
return p.do(create)
}

func (p *participant) Update(key string, value string, expireTime time.Time) (*store.Event, error) {
update := &cmd{Type: "update", Key: key, Value: value, Time: expireTime}
update := &Cmd{Type: stupdate, Key: key, Value: &value, Time: mustMarshalTime(&expireTime)}
return p.do(update)
}

func (p *participant) CAS(key, value, prevValue string, prevIndex uint64, expireTime time.Time) (*store.Event, error) {
cas := &cmd{Type: "cas", Key: key, Value: value, PrevValue: prevValue, PrevIndex: prevIndex, Time: expireTime}
cas := &Cmd{Type: stcas, Key: key, Value: &value, PrevValue: &prevValue, PrevIndex: &prevIndex, Time: mustMarshalTime(&expireTime)}
return p.do(cas)
}

func (p *participant) Delete(key string, dir, recursive bool) (*store.Event, error) {
d := &cmd{Type: "delete", Key: key, Dir: dir, Recursive: recursive}
d := &Cmd{Type: stdelete, Key: key, Dir: &dir, Recursive: &recursive}
return p.do(d)
}

func (p *participant) CAD(key string, prevValue string, prevIndex uint64) (*store.Event, error) {
cad := &cmd{Type: "cad", Key: key, PrevValue: prevValue, PrevIndex: prevIndex}
cad := &Cmd{Type: stcad, Key: key, PrevValue: &prevValue, PrevIndex: &prevIndex}
return p.do(cad)
}

func (p *participant) QuorumGet(key string, recursive, sorted bool) (*store.Event, error) {
get := &cmd{Type: "quorumGet", Key: key, Recursive: recursive, Sorted: sorted}
get := &Cmd{Type: stqget, Key: key, Recursive: &recursive, Sorted: &sorted}
return p.do(get)
}

func (p *participant) do(c *cmd) (*store.Event, error) {
data, err := json.Marshal(c)
func (p *participant) do(c *Cmd) (*store.Event, error) {
data, err := c.Marshal()
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit 9f3cba1

Please sign in to comment.