mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
change imports to coreos/etcd coreos/go-raft; change API to work with new go-raft
This commit is contained in:
parent
0f22918e2b
commit
e2f0420862
@ -8,8 +8,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/xiangli-cmu/go-raft"
|
"github.com/coreos/etcd/store"
|
||||||
"github.com/xiangli-cmu/raft-etcd/store"
|
"github.com/coreos/go-raft"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ func (c *SetCommand) CommandName() string {
|
|||||||
|
|
||||||
// Set the value of key to value
|
// Set the value of key to value
|
||||||
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
|
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
|
||||||
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommittedIndex())
|
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommitIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the path for http request
|
// Get the path for http request
|
||||||
@ -88,7 +88,7 @@ func (c *DeleteCommand) CommandName() string {
|
|||||||
|
|
||||||
// Delete the key
|
// Delete the key
|
||||||
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
|
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
|
||||||
return store.Delete(c.Key, server.CommittedIndex())
|
return store.Delete(c.Key, server.CommitIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch command
|
// Watch command
|
||||||
|
8
etcd.go
8
etcd.go
@ -8,9 +8,9 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/xiangli-cmu/go-raft"
|
"github.com/coreos/etcd/store"
|
||||||
"github.com/xiangli-cmu/raft-etcd/store"
|
"github.com/coreos/etcd/web"
|
||||||
"github.com/xiangli-cmu/raft-etcd/web"
|
"github.com/coreos/go-raft"
|
||||||
//"io"
|
//"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -187,7 +187,7 @@ func main() {
|
|||||||
// start as a leader in a new cluster
|
// start as a leader in a new cluster
|
||||||
if cluster == "" {
|
if cluster == "" {
|
||||||
server.StartLeader()
|
server.StartLeader()
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 20)
|
time.Sleep(time.Millisecond * 20)
|
||||||
|
|
||||||
// join self as a peer
|
// join self as a peer
|
||||||
|
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/xiangli-cmu/go-raft"
|
"github.com/coreos/go-raft"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -25,7 +25,7 @@ func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
err := decodeJsonRequest(req, rvreq)
|
err := decodeJsonRequest(req, rvreq)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
debug("[recv] POST http://%v/vote [%s]", server.Name(), rvreq.CandidateName)
|
debug("[recv] POST http://%v/vote [%s]", server.Name(), rvreq.CandidateName)
|
||||||
if resp, _ := server.RequestVote(rvreq); resp != nil {
|
if resp := server.RequestVote(rvreq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
return
|
return
|
||||||
@ -40,7 +40,7 @@ func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
err := decodeJsonRequest(req, aereq)
|
err := decodeJsonRequest(req, aereq)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
debug("[recv] POST http://%s/log/append [%d]", server.Name(), len(aereq.Entries))
|
debug("[recv] POST http://%s/log/append [%d]", server.Name(), len(aereq.Entries))
|
||||||
if resp, _ := server.AppendEntries(aereq); resp != nil {
|
if resp := server.AppendEntries(aereq); resp != nil {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(resp)
|
json.NewEncoder(w).Encode(resp)
|
||||||
if !resp.Success {
|
if !resp.Success {
|
||||||
@ -187,7 +187,7 @@ func excute(c Command, w *http.ResponseWriter, req *http.Request) {
|
|||||||
url := scheme + leaderClient() + path
|
url := scheme + leaderClient() + path
|
||||||
|
|
||||||
debug("redirect to %s", url)
|
debug("redirect to %s", url)
|
||||||
|
|
||||||
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
|
http.Redirect(*w, req, url, http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/xiangli-cmu/go-raft"
|
"github.com/coreos/go-raft"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -15,49 +15,49 @@ type transHandler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sends AppendEntries RPCs to a peer when the server is the leader.
|
// Sends AppendEntries RPCs to a peer when the server is the leader.
|
||||||
func (t transHandler) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) (*raft.AppendEntriesResponse, error) {
|
func (t transHandler) SendAppendEntriesRequest(server *raft.Server, peer *raft.Peer, req *raft.AppendEntriesRequest) *raft.AppendEntriesResponse {
|
||||||
var aersp *raft.AppendEntriesResponse
|
var aersp *raft.AppendEntriesResponse
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
json.NewEncoder(&b).Encode(req)
|
json.NewEncoder(&b).Encode(req)
|
||||||
|
|
||||||
debug("Send LogEntries to %s ", peer.Name())
|
debug("Send LogEntries to %s ", peer.Name())
|
||||||
|
|
||||||
resp, err := Post(&t, fmt.Sprintf("%s/log/append", peer.Name()), &b)
|
resp, _ := Post(&t, fmt.Sprintf("%s/log/append", peer.Name()), &b)
|
||||||
|
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
aersp = &raft.AppendEntriesResponse{}
|
aersp = &raft.AppendEntriesResponse{}
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
|
if err := json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
|
||||||
return aersp, nil
|
return aersp
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return aersp, fmt.Errorf("raftd: Unable to append entries: %v", err)
|
return aersp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends RequestVote RPCs to a peer when the server is the candidate.
|
// Sends RequestVote RPCs to a peer when the server is the candidate.
|
||||||
func (t transHandler) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) (*raft.RequestVoteResponse, error) {
|
func (t transHandler) SendVoteRequest(server *raft.Server, peer *raft.Peer, req *raft.RequestVoteRequest) *raft.RequestVoteResponse {
|
||||||
var rvrsp *raft.RequestVoteResponse
|
var rvrsp *raft.RequestVoteResponse
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
json.NewEncoder(&b).Encode(req)
|
json.NewEncoder(&b).Encode(req)
|
||||||
|
|
||||||
debug("Send Vote to %s", peer.Name())
|
debug("Send Vote to %s", peer.Name())
|
||||||
|
|
||||||
resp, err := Post(&t, fmt.Sprintf("%s/vote", peer.Name()), &b)
|
resp, _ := Post(&t, fmt.Sprintf("%s/vote", peer.Name()), &b)
|
||||||
|
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
rvrsp := &raft.RequestVoteResponse{}
|
rvrsp := &raft.RequestVoteResponse{}
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
|
if err := json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
|
||||||
return rvrsp, nil
|
return rvrsp
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return rvrsp, fmt.Errorf("Unable to request vote: %v", err)
|
return rvrsp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends SnapshotRequest RPCs to a peer when the server is the candidate.
|
// Sends SnapshotRequest RPCs to a peer when the server is the candidate.
|
||||||
func (t transHandler) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) (*raft.SnapshotResponse, error) {
|
func (t transHandler) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) *raft.SnapshotResponse {
|
||||||
var aersp *raft.SnapshotResponse
|
var aersp *raft.SnapshotResponse
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
json.NewEncoder(&b).Encode(req)
|
json.NewEncoder(&b).Encode(req)
|
||||||
@ -72,8 +72,8 @@ func (t transHandler) SendSnapshotRequest(server *raft.Server, peer *raft.Peer,
|
|||||||
aersp = &raft.SnapshotResponse{}
|
aersp = &raft.SnapshotResponse{}
|
||||||
if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
|
if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
|
||||||
|
|
||||||
return aersp, nil
|
return aersp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return aersp, fmt.Errorf("Unable to send snapshot: %v", err)
|
return aersp
|
||||||
}
|
}
|
||||||
|
2
util.go
2
util.go
@ -3,7 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/xiangli-cmu/raft-etcd/web"
|
"github.com/coreos/etcd/web"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -3,7 +3,7 @@ package web
|
|||||||
import (
|
import (
|
||||||
"code.google.com/p/go.net/websocket"
|
"code.google.com/p/go.net/websocket"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/xiangli-cmu/go-raft"
|
"github.com/coreos/go-raft"
|
||||||
//"github.com/xiangli-cmu/raft-etcd/store"
|
//"github.com/xiangli-cmu/raft-etcd/store"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user