2014-08-26 05:39:02 +04:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
2014-09-27 01:18:59 +04:00
|
|
|
"encoding/json"
|
2014-08-27 02:42:10 +04:00
|
|
|
"errors"
|
2014-09-16 04:35:02 +04:00
|
|
|
"log"
|
2014-09-18 01:38:10 +04:00
|
|
|
"math/rand"
|
2014-09-25 05:05:33 +04:00
|
|
|
"sync/atomic"
|
2014-08-27 03:17:02 +04:00
|
|
|
"time"
|
2014-08-27 02:42:10 +04:00
|
|
|
|
2014-09-03 08:36:14 +04:00
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
2014-08-26 05:39:02 +04:00
|
|
|
"github.com/coreos/etcd/raft"
|
2014-08-28 05:53:18 +04:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
2014-08-27 02:28:21 +04:00
|
|
|
"github.com/coreos/etcd/store"
|
2014-09-04 07:06:16 +04:00
|
|
|
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
|
2014-08-26 05:39:02 +04:00
|
|
|
"github.com/coreos/etcd/wait"
|
|
|
|
)
|
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
const (
|
|
|
|
defaultSyncTimeout = time.Second
|
|
|
|
DefaultSnapCount = 10000
|
|
|
|
)
|
2014-09-16 04:35:02 +04:00
|
|
|
|
2014-08-29 03:41:42 +04:00
|
|
|
var (
|
|
|
|
ErrUnknownMethod = errors.New("etcdserver: unknown method")
|
|
|
|
ErrStopped = errors.New("etcdserver: server stopped")
|
|
|
|
)
|
2014-08-27 02:42:10 +04:00
|
|
|
|
2014-09-18 01:38:10 +04:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2014-08-28 05:53:18 +04:00
|
|
|
type SendFunc func(m []raftpb.Message)
|
2014-09-16 04:35:02 +04:00
|
|
|
type SaveFunc func(st raftpb.HardState, ents []raftpb.Entry)
|
2014-08-28 00:37:22 +04:00
|
|
|
|
2014-08-26 05:39:02 +04:00
|
|
|
type Response struct {
|
2014-08-31 01:19:03 +04:00
|
|
|
Event *store.Event
|
2014-09-09 03:56:10 +04:00
|
|
|
Watcher store.Watcher
|
2014-08-31 02:27:43 +04:00
|
|
|
err error
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
type Storage interface {
|
|
|
|
// Save function saves ents and state to the underlying stable storage.
|
|
|
|
// Save MUST block until st and ents are on stable storage.
|
|
|
|
Save(st raftpb.HardState, ents []raftpb.Entry)
|
|
|
|
// SaveSnap function saves snapshot to the underlying stable storage.
|
|
|
|
SaveSnap(snap raftpb.Snapshot)
|
|
|
|
|
|
|
|
// TODO: WAL should be able to control cut itself. After implement self-controled cut,
|
|
|
|
// remove it in this interface.
|
|
|
|
// Cut cuts out a new wal file for saving new state and entries.
|
|
|
|
Cut() error
|
|
|
|
}
|
|
|
|
|
2014-09-12 04:14:41 +04:00
|
|
|
type Server interface {
|
|
|
|
// Start performs any initialization of the Server necessary for it to
|
|
|
|
// begin serving requests. It must be called before Do or Process.
|
|
|
|
// Start must be non-blocking; any long-running server functionality
|
|
|
|
// should be implemented in goroutines.
|
|
|
|
Start()
|
|
|
|
// Stop terminates the Server and performs any necessary finalization.
|
|
|
|
// Do and Process cannot be called after Stop has been invoked.
|
|
|
|
Stop()
|
|
|
|
// Do takes a request and attempts to fulfil it, returning a Response.
|
|
|
|
Do(ctx context.Context, r pb.Request) (Response, error)
|
|
|
|
// Process takes a raft message and applies it to the server's raft state
|
|
|
|
// machine, respecting any timeout of the given context.
|
|
|
|
Process(ctx context.Context, m raftpb.Message) error
|
|
|
|
}
|
|
|
|
|
2014-09-25 05:05:33 +04:00
|
|
|
type RaftTimer interface {
|
|
|
|
Index() int64
|
|
|
|
Term() int64
|
|
|
|
}
|
|
|
|
|
2014-09-12 04:14:41 +04:00
|
|
|
// EtcdServer is the production implementation of the Server interface
|
|
|
|
type EtcdServer struct {
|
2014-09-11 04:12:58 +04:00
|
|
|
w wait.Wait
|
2014-08-29 03:41:42 +04:00
|
|
|
done chan struct{}
|
2014-08-27 00:39:33 +04:00
|
|
|
|
2014-08-28 00:37:22 +04:00
|
|
|
Node raft.Node
|
|
|
|
Store store.Store
|
2014-08-27 02:28:21 +04:00
|
|
|
|
2014-09-25 06:51:27 +04:00
|
|
|
// Send specifies the send function for sending msgs to members. Send
|
2014-08-27 00:39:33 +04:00
|
|
|
// MUST NOT block. It is okay to drop messages, since clients should
|
2014-09-12 04:14:41 +04:00
|
|
|
// timeout and reissue their messages. If Send is nil, server will
|
2014-08-27 00:39:33 +04:00
|
|
|
// panic.
|
2014-08-28 00:37:22 +04:00
|
|
|
Send SendFunc
|
2014-08-27 00:39:33 +04:00
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
Storage Storage
|
2014-09-03 03:59:29 +04:00
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
Ticker <-chan time.Time
|
|
|
|
SyncTicker <-chan time.Time
|
2014-09-17 05:18:45 +04:00
|
|
|
|
|
|
|
SnapCount int64 // number of entries to trigger a snapshot
|
2014-09-25 05:05:33 +04:00
|
|
|
|
|
|
|
// Cache of the latest raft index and raft term the server has seen
|
2014-09-25 06:51:27 +04:00
|
|
|
raftIndex int64
|
|
|
|
raftTerm int64
|
|
|
|
ClusterStore ClusterStore
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-01 05:48:18 +04:00
|
|
|
// Start prepares and starts server in a new goroutine. It is no longer safe to
|
2014-09-12 04:14:41 +04:00
|
|
|
// modify a server's fields after it has been sent to Start.
|
|
|
|
func (s *EtcdServer) Start() {
|
2014-09-17 05:18:45 +04:00
|
|
|
if s.SnapCount == 0 {
|
|
|
|
log.Printf("etcdserver: set snapshot count to default %d", DefaultSnapCount)
|
|
|
|
s.SnapCount = DefaultSnapCount
|
|
|
|
}
|
2014-08-29 03:41:42 +04:00
|
|
|
s.w = wait.New()
|
|
|
|
s.done = make(chan struct{})
|
2014-09-25 06:51:27 +04:00
|
|
|
// TODO: if this is an empty log, writes all peer infos
|
|
|
|
// into the first entry
|
2014-08-29 03:41:42 +04:00
|
|
|
go s.run()
|
|
|
|
}
|
2014-08-28 00:37:22 +04:00
|
|
|
|
2014-09-12 04:14:41 +04:00
|
|
|
func (s *EtcdServer) Process(ctx context.Context, m raftpb.Message) error {
|
|
|
|
return s.Node.Step(ctx, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) run() {
|
2014-09-16 04:35:02 +04:00
|
|
|
var syncC <-chan time.Time
|
2014-09-17 05:18:45 +04:00
|
|
|
// snapi indicates the index of the last submitted snapshot request
|
|
|
|
var snapi, appliedi int64
|
2014-08-26 05:39:02 +04:00
|
|
|
for {
|
2014-08-27 02:28:21 +04:00
|
|
|
select {
|
2014-09-03 03:59:29 +04:00
|
|
|
case <-s.Ticker:
|
|
|
|
s.Node.Tick()
|
2014-08-28 00:37:22 +04:00
|
|
|
case rd := <-s.Node.Ready():
|
2014-09-17 05:18:45 +04:00
|
|
|
s.Storage.Save(rd.HardState, rd.Entries)
|
|
|
|
s.Storage.SaveSnap(rd.Snapshot)
|
2014-08-27 02:28:21 +04:00
|
|
|
s.Send(rd.Messages)
|
2014-08-28 02:34:33 +04:00
|
|
|
|
|
|
|
// TODO(bmizerany): do this in the background, but take
|
|
|
|
// care to apply entries in a single goroutine, and not
|
|
|
|
// race them.
|
2014-09-25 06:51:27 +04:00
|
|
|
// TODO: apply configuration change into ClusterStore.
|
2014-08-28 02:34:33 +04:00
|
|
|
for _, e := range rd.CommittedEntries {
|
2014-09-19 23:36:19 +04:00
|
|
|
switch e.Type {
|
2014-09-21 09:48:17 +04:00
|
|
|
case raftpb.EntryNormal:
|
2014-09-19 23:36:19 +04:00
|
|
|
var r pb.Request
|
|
|
|
if err := r.Unmarshal(e.Data); err != nil {
|
|
|
|
panic("TODO: this is bad, what do we do about it?")
|
|
|
|
}
|
2014-09-23 21:17:53 +04:00
|
|
|
s.w.Trigger(r.ID, s.apply(r))
|
2014-09-23 23:02:44 +04:00
|
|
|
case raftpb.EntryConfChange:
|
|
|
|
var cc raftpb.ConfChange
|
2014-09-23 08:59:13 +04:00
|
|
|
if err := cc.Unmarshal(e.Data); err != nil {
|
2014-09-19 23:36:19 +04:00
|
|
|
panic("TODO: this is bad, what do we do about it?")
|
|
|
|
}
|
2014-09-23 23:02:44 +04:00
|
|
|
s.Node.ApplyConfChange(cc)
|
2014-09-23 08:59:13 +04:00
|
|
|
s.w.Trigger(cc.ID, nil)
|
2014-09-19 23:36:19 +04:00
|
|
|
default:
|
2014-09-23 08:59:13 +04:00
|
|
|
panic("unexpected entry type")
|
2014-08-29 03:41:42 +04:00
|
|
|
}
|
2014-09-25 05:05:33 +04:00
|
|
|
atomic.StoreInt64(&s.raftIndex, e.Index)
|
|
|
|
atomic.StoreInt64(&s.raftTerm, e.Term)
|
2014-09-17 05:18:45 +04:00
|
|
|
appliedi = e.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
if rd.Snapshot.Index > snapi {
|
|
|
|
snapi = rd.Snapshot.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
// recover from snapshot if it is more updated than current applied
|
|
|
|
if rd.Snapshot.Index > appliedi {
|
|
|
|
if err := s.Store.Recovery(rd.Snapshot.Data); err != nil {
|
|
|
|
panic("TODO: this is bad, what do we do about it?")
|
|
|
|
}
|
|
|
|
appliedi = rd.Snapshot.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
if appliedi-snapi > s.SnapCount {
|
|
|
|
s.snapshot()
|
|
|
|
snapi = appliedi
|
2014-08-28 02:34:33 +04:00
|
|
|
}
|
2014-09-16 04:35:02 +04:00
|
|
|
|
|
|
|
if rd.SoftState != nil {
|
|
|
|
if rd.RaftState == raft.StateLeader {
|
|
|
|
syncC = s.SyncTicker
|
|
|
|
} else {
|
|
|
|
syncC = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-syncC:
|
|
|
|
s.sync(defaultSyncTimeout)
|
2014-08-29 03:41:42 +04:00
|
|
|
case <-s.done:
|
2014-08-26 22:37:07 +04:00
|
|
|
return
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 04:14:41 +04:00
|
|
|
// Stop stops the server, and shuts down the running goroutine. Stop should be
|
|
|
|
// called after a Start(s), otherwise it will block forever.
|
|
|
|
func (s *EtcdServer) Stop() {
|
2014-09-11 04:12:58 +04:00
|
|
|
s.Node.Stop()
|
|
|
|
close(s.done)
|
2014-08-30 02:32:41 +04:00
|
|
|
}
|
2014-08-29 03:41:42 +04:00
|
|
|
|
2014-08-30 03:32:31 +04:00
|
|
|
// Do interprets r and performs an operation on s.Store according to r.Method
|
2014-09-16 04:35:02 +04:00
|
|
|
// and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET" with
|
2014-09-01 05:48:18 +04:00
|
|
|
// Quorum == true, r will be sent through consensus before performing its
|
|
|
|
// respective operation. Do will block until an action is performed or there is
|
|
|
|
// an error.
|
2014-09-12 04:14:41 +04:00
|
|
|
func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
|
2014-09-23 21:17:53 +04:00
|
|
|
if r.ID == 0 {
|
2014-08-26 05:39:02 +04:00
|
|
|
panic("r.Id cannot be 0")
|
|
|
|
}
|
2014-08-31 01:35:27 +04:00
|
|
|
if r.Method == "GET" && r.Quorum {
|
|
|
|
r.Method = "QGET"
|
|
|
|
}
|
2014-08-27 02:28:21 +04:00
|
|
|
switch r.Method {
|
2014-08-31 01:35:27 +04:00
|
|
|
case "POST", "PUT", "DELETE", "QGET":
|
2014-08-27 02:28:21 +04:00
|
|
|
data, err := r.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return Response{}, err
|
|
|
|
}
|
2014-09-23 21:17:53 +04:00
|
|
|
ch := s.w.Register(r.ID)
|
2014-08-29 03:41:42 +04:00
|
|
|
s.Node.Propose(ctx, data)
|
2014-08-27 02:28:21 +04:00
|
|
|
select {
|
|
|
|
case x := <-ch:
|
|
|
|
resp := x.(Response)
|
|
|
|
return resp, resp.err
|
|
|
|
case <-ctx.Done():
|
2014-09-23 21:17:53 +04:00
|
|
|
s.w.Trigger(r.ID, nil) // GC wait
|
2014-08-27 02:28:21 +04:00
|
|
|
return Response{}, ctx.Err()
|
2014-08-29 03:41:42 +04:00
|
|
|
case <-s.done:
|
|
|
|
return Response{}, ErrStopped
|
2014-08-27 02:28:21 +04:00
|
|
|
}
|
|
|
|
case "GET":
|
|
|
|
switch {
|
|
|
|
case r.Wait:
|
2014-09-24 01:08:18 +04:00
|
|
|
wc, err := s.Store.Watch(r.Path, r.Recursive, r.Stream, r.Since)
|
2014-08-27 02:28:21 +04:00
|
|
|
if err != nil {
|
|
|
|
return Response{}, err
|
|
|
|
}
|
|
|
|
return Response{Watcher: wc}, nil
|
|
|
|
default:
|
2014-08-28 00:37:22 +04:00
|
|
|
ev, err := s.Store.Get(r.Path, r.Recursive, r.Sorted)
|
2014-08-27 02:28:21 +04:00
|
|
|
if err != nil {
|
|
|
|
return Response{}, err
|
|
|
|
}
|
|
|
|
return Response{Event: ev}, nil
|
|
|
|
}
|
2014-08-27 02:42:10 +04:00
|
|
|
default:
|
|
|
|
return Response{}, ErrUnknownMethod
|
2014-08-26 05:39:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-26 22:37:07 +04:00
|
|
|
|
2014-09-19 23:36:19 +04:00
|
|
|
func (s *EtcdServer) AddNode(ctx context.Context, id int64, context []byte) error {
|
2014-09-23 23:02:44 +04:00
|
|
|
cc := raftpb.ConfChange{
|
2014-09-21 05:56:40 +04:00
|
|
|
ID: GenID(),
|
2014-09-23 23:02:44 +04:00
|
|
|
Type: raftpb.ConfChangeAddNode,
|
2014-09-19 23:36:19 +04:00
|
|
|
NodeID: id,
|
|
|
|
Context: context,
|
|
|
|
}
|
2014-09-23 08:59:13 +04:00
|
|
|
return s.configure(ctx, cc)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) RemoveNode(ctx context.Context, id int64) error {
|
2014-09-23 23:02:44 +04:00
|
|
|
cc := raftpb.ConfChange{
|
2014-09-21 05:56:40 +04:00
|
|
|
ID: GenID(),
|
2014-09-23 23:02:44 +04:00
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
2014-09-19 23:36:19 +04:00
|
|
|
NodeID: id,
|
|
|
|
}
|
2014-09-23 08:59:13 +04:00
|
|
|
return s.configure(ctx, cc)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2014-09-25 05:05:33 +04:00
|
|
|
// Implement the RaftTimer interface
|
|
|
|
func (s *EtcdServer) Index() int64 {
|
|
|
|
return atomic.LoadInt64(&s.raftIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtcdServer) Term() int64 {
|
|
|
|
return atomic.LoadInt64(&s.raftTerm)
|
|
|
|
}
|
|
|
|
|
2014-09-19 23:36:19 +04:00
|
|
|
// configure sends configuration change through consensus then performs it.
|
|
|
|
// It will block until the change is performed or there is an error.
|
2014-09-23 23:02:44 +04:00
|
|
|
func (s *EtcdServer) configure(ctx context.Context, cc raftpb.ConfChange) error {
|
2014-09-23 08:59:13 +04:00
|
|
|
ch := s.w.Register(cc.ID)
|
2014-09-23 23:02:44 +04:00
|
|
|
if err := s.Node.ProposeConfChange(ctx, cc); err != nil {
|
2014-09-23 01:38:21 +04:00
|
|
|
log.Printf("configure error: %v", err)
|
2014-09-23 08:59:13 +04:00
|
|
|
s.w.Trigger(cc.ID, nil)
|
2014-09-19 23:36:19 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
2014-09-23 08:59:13 +04:00
|
|
|
s.w.Trigger(cc.ID, nil) // GC wait
|
2014-09-19 23:36:19 +04:00
|
|
|
return ctx.Err()
|
|
|
|
case <-s.done:
|
|
|
|
return ErrStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
// sync proposes a SYNC request and is non-blocking.
|
|
|
|
// This makes no guarantee that the request will be proposed or performed.
|
|
|
|
// The request will be cancelled after the given timeout.
|
|
|
|
func (s *EtcdServer) sync(timeout time.Duration) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
req := pb.Request{
|
|
|
|
Method: "SYNC",
|
2014-09-23 21:17:53 +04:00
|
|
|
ID: GenID(),
|
2014-09-16 04:35:02 +04:00
|
|
|
Time: time.Now().UnixNano(),
|
|
|
|
}
|
|
|
|
data, err := req.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("marshal request %#v error: %v", req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// There is no promise that node has leader when do SYNC request,
|
|
|
|
// so it uses goroutine to propose.
|
|
|
|
go func() {
|
|
|
|
s.Node.Propose(ctx, data)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:18:59 +04:00
|
|
|
// publish registers server information into the cluster. The information
|
|
|
|
// is the json format of the given member.
|
|
|
|
// The function keeps attempting to register until it succeeds,
|
|
|
|
// or its server is stopped.
|
|
|
|
func (s *EtcdServer) publish(m Member, retryInterval time.Duration) {
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("etcdserver: json marshal error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req := pb.Request{
|
2014-09-30 01:14:45 +04:00
|
|
|
ID: GenID(),
|
2014-09-27 01:18:59 +04:00
|
|
|
Method: "PUT",
|
|
|
|
Path: m.storeKey(),
|
|
|
|
Val: string(b),
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), retryInterval)
|
|
|
|
_, err := s.Do(ctx, req)
|
|
|
|
cancel()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
log.Printf("etcdserver: published %+v to the cluster", m)
|
|
|
|
return
|
|
|
|
case ErrStopped:
|
|
|
|
log.Printf("etcdserver: aborting publish because server is stopped")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Printf("etcdserver: publish error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 01:16:12 +04:00
|
|
|
func getExpirationTime(r *pb.Request) time.Time {
|
|
|
|
var t time.Time
|
|
|
|
if r.Expiration != 0 {
|
|
|
|
t = time.Unix(0, r.Expiration)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-09-24 21:46:22 +04:00
|
|
|
// apply interprets r as a call to store.X and returns a Response interpreted
|
|
|
|
// from store.Event
|
2014-09-23 08:59:13 +04:00
|
|
|
func (s *EtcdServer) apply(r pb.Request) Response {
|
2014-08-31 02:27:43 +04:00
|
|
|
f := func(ev *store.Event, err error) Response {
|
|
|
|
return Response{Event: ev, err: err}
|
|
|
|
}
|
2014-09-23 01:16:12 +04:00
|
|
|
expr := getExpirationTime(&r)
|
2014-08-27 03:17:02 +04:00
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.Create(r.Path, r.Dir, r.Val, true, expr))
|
2014-08-27 03:17:02 +04:00
|
|
|
case "PUT":
|
2014-09-19 08:22:10 +04:00
|
|
|
exists, existsSet := getBool(r.PrevExist)
|
2014-08-27 03:39:26 +04:00
|
|
|
switch {
|
2014-08-27 23:04:00 +04:00
|
|
|
case existsSet:
|
2014-08-27 22:54:29 +04:00
|
|
|
if exists {
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.Update(r.Path, r.Val, expr))
|
2014-08-27 22:54:29 +04:00
|
|
|
}
|
2014-09-24 21:46:22 +04:00
|
|
|
return f(s.Store.Create(r.Path, r.Dir, r.Val, false, expr))
|
2014-08-27 22:38:25 +04:00
|
|
|
case r.PrevIndex > 0 || r.PrevValue != "":
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr))
|
2014-08-27 22:29:33 +04:00
|
|
|
default:
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.Set(r.Path, r.Dir, r.Val, expr))
|
2014-08-27 03:39:26 +04:00
|
|
|
}
|
2014-08-27 03:17:02 +04:00
|
|
|
case "DELETE":
|
2014-08-27 22:34:42 +04:00
|
|
|
switch {
|
|
|
|
case r.PrevIndex > 0 || r.PrevValue != "":
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.CompareAndDelete(r.Path, r.PrevValue, r.PrevIndex))
|
2014-08-27 22:34:42 +04:00
|
|
|
default:
|
2014-09-23 01:40:19 +04:00
|
|
|
return f(s.Store.Delete(r.Path, r.Dir, r.Recursive))
|
2014-08-27 22:34:42 +04:00
|
|
|
}
|
2014-08-31 01:35:27 +04:00
|
|
|
case "QGET":
|
2014-08-31 02:27:43 +04:00
|
|
|
return f(s.Store.Get(r.Path, r.Recursive, r.Sorted))
|
2014-09-16 04:35:02 +04:00
|
|
|
case "SYNC":
|
|
|
|
s.Store.DeleteExpiredKeys(time.Unix(0, r.Time))
|
|
|
|
return Response{}
|
2014-08-27 03:17:02 +04:00
|
|
|
default:
|
2014-08-28 00:37:22 +04:00
|
|
|
// This should never be reached, but just in case:
|
2014-08-31 02:27:43 +04:00
|
|
|
return Response{err: ErrUnknownMethod}
|
2014-08-27 03:17:02 +04:00
|
|
|
}
|
2014-08-26 22:37:07 +04:00
|
|
|
}
|
2014-08-27 22:54:29 +04:00
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
// TODO: non-blocking snapshot
|
|
|
|
func (s *EtcdServer) snapshot() {
|
|
|
|
d, err := s.Store.Save()
|
|
|
|
// TODO: current store will never fail to do a snapshot
|
|
|
|
// what should we do if the store might fail?
|
|
|
|
if err != nil {
|
|
|
|
panic("TODO: this is bad, what do we do about it?")
|
|
|
|
}
|
|
|
|
s.Node.Compact(d)
|
|
|
|
s.Storage.Cut()
|
|
|
|
}
|
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
// TODO: move the function to /id pkg maybe?
|
|
|
|
// GenID generates a random id that is not equal to 0.
|
2014-09-18 01:38:10 +04:00
|
|
|
func GenID() (n int64) {
|
|
|
|
for n == 0 {
|
|
|
|
n = rand.Int63()
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2014-09-18 01:38:10 +04:00
|
|
|
return
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-27 22:54:29 +04:00
|
|
|
func getBool(v *bool) (vv bool, set bool) {
|
|
|
|
if v == nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
return *v, true
|
|
|
|
}
|