mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
feat add set command
This commit is contained in:
@@ -254,11 +254,10 @@ func (s *Server) SpeedTestHandler(w http.ResponseWriter, req *http.Request) erro
|
|||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
for j := 0; j < 10; j++ {
|
for j := 0; j < 10; j++ {
|
||||||
c := &store.CreateCommand{
|
c := &store.SetCommand{
|
||||||
Key: "foo",
|
Key: "foo",
|
||||||
Value: "bar",
|
Value: "bar",
|
||||||
ExpireTime: time.Unix(0, 0),
|
ExpireTime: time.Unix(0, 0),
|
||||||
Force: true,
|
|
||||||
}
|
}
|
||||||
s.peerServer.RaftServer().Do(c)
|
s.peerServer.RaftServer().Do(c)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,11 +39,10 @@ func SetKeyHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
c = &store.CreateCommand{
|
c = &store.SetCommand{
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: value,
|
Value: value,
|
||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
Force: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,11 +35,10 @@ func PutHandler(w http.ResponseWriter, req *http.Request, s Server) error {
|
|||||||
|
|
||||||
// Set command: create a new node or replace the old one.
|
// Set command: create a new node or replace the old one.
|
||||||
if !valueOk && !indexOk && !existOk {
|
if !valueOk && !indexOk && !existOk {
|
||||||
c = &store.CreateCommand{
|
c = &store.SetCommand{
|
||||||
Key: key,
|
Key: key,
|
||||||
Value: value,
|
Value: value,
|
||||||
ExpireTime: expireTime,
|
ExpireTime: expireTime,
|
||||||
Force: true,
|
|
||||||
}
|
}
|
||||||
return s.Dispatch(c, w, req)
|
return s.Dispatch(c, w, req)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ type CreateCommand struct {
|
|||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
ExpireTime time.Time `json:"expireTime"`
|
ExpireTime time.Time `json:"expireTime"`
|
||||||
IncrementalSuffix bool `json:"incrementalSuffix"`
|
IncrementalSuffix bool `json:"incrementalSuffix"`
|
||||||
Force bool `json:"force"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The name of the create command in the log
|
// The name of the create command in the log
|
||||||
@@ -28,7 +27,7 @@ func (c *CreateCommand) CommandName() string {
|
|||||||
func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
|
func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
|
||||||
s, _ := server.StateMachine().(Store)
|
s, _ := server.StateMachine().(Store)
|
||||||
|
|
||||||
e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, c.Force, c.ExpireTime, server.CommitIndex(), server.Term())
|
e, err := s.Create(c.Key, c.Value, c.IncrementalSuffix, false, c.ExpireTime, server.CommitIndex(), server.Term())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
|
|||||||
38
store/set_command.go
Normal file
38
store/set_command.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/coreos/etcd/log"
|
||||||
|
"github.com/coreos/go-raft"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
raft.RegisterCommand(&CreateCommand{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create command
|
||||||
|
type SetCommand struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
ExpireTime time.Time `json:"expireTime"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// The name of the create command in the log
|
||||||
|
func (c *SetCommand) CommandName() string {
|
||||||
|
return "etcd:set"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create node
|
||||||
|
func (c *SetCommand) Apply(server raft.Server) (interface{}, error) {
|
||||||
|
s, _ := server.StateMachine().(Store)
|
||||||
|
|
||||||
|
// create a new node or replace the old node.
|
||||||
|
e, err := s.Create(c.Key, c.Value, false, true, c.ExpireTime, server.CommitIndex(), server.Term())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debug(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user