bump 3rd party

This commit is contained in:
Xiang Li
2013-09-21 23:09:20 -04:00
parent 940294d1cd
commit d3fbf6d997
32 changed files with 288 additions and 97 deletions

View File

@ -1,3 +1,2 @@
name = example
name = logger3
sync = 0

View File

@ -22,14 +22,14 @@ import (
)
func main() {
logger1, _ := logging.SimpleLogger("main")
logger1, _ := logging.SimpleLogger("logger1")
logger1.SetLevel(logging.NOTSET)
logger1.Error("this is a test from error")
logger1.Debug("this is a test from debug")
logger1.Notset("orz", time.Now().UnixNano())
logger1.Destroy()
logger2, _ := logging.RichLogger("main")
logger2, _ := logging.RichLogger("logger2")
logger2.SetLevel(logging.DEBUG)
logger2.Error("this is a test from error")
logger2.Debug("this is a test from debug")

View File

@ -16,7 +16,7 @@
package logging
// Logln receives log request from the client. The request includes a set of
// Log receives log request from the client. The request includes a set of
// variables.
func (logger *Logger) Log(level Level, v ...interface{}) {
// Don't delete this calling. The calling is used to keep the same

View File

@ -97,7 +97,7 @@ func RichLogger(name string) (*Logger, error) {
// FileLogger creates a new logger with file output.
func FileLogger(name string, level Level, format string, timeFormat string, file string, sync bool) (*Logger, error) {
out, err := os.Create(file)
out, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModeAppend|0666)
if err != nil {
return nil, err
}

View File

@ -2,4 +2,49 @@
golang client library for etcd
This etcd client library is under heavy development. Check back soon for more docs. In the meantime, check out [etcd](https://github.com/coreos/etcd) for details on the client protocol.
This etcd client library is under heavy development. Check back soon for more
docs. In the meantime, check out [etcd](https://github.com/coreos/etcd) for
details on the client protocol.
For usage see example below or look at godoc: [go-etcd/etcd](http://godoc.org/github.com/coreos/go-etcd/etcd)
## Install
```bash
go get github.com/coreos/go-etcd/etcd
```
## Examples
Returning error values are not showed for the sake of simplicity, but you
should check them.
```go
package main
import (
"fmt"
"github.com/coreos/go-etcd/etcd"
)
func main() {
c := etcd.NewClient() // default binds to http://0.0.0.0:4001
// SET the value "bar" to the key "foo" with zero TTL
// returns a: *store.Response
res, _ := c.Set("foo", "bar", 0)
fmt.Printf("set response: %+v\n", res)
// GET the value that is stored for the key "foo"
// return a slice: []*store.Response
values, _ := c.Get("foo")
for i, res := range values { // .. and print them out
fmt.Printf("[%d] get response: %+v\n", i, res)
}
// DELETE the key "foo"
// returns a: *store.Response
res, _ = c.Delete("foo")
fmt.Printf("delete response: %+v\n", res)
}
```

View File

@ -117,7 +117,7 @@ func (c *Client) SyncCluster() bool {
// sync cluster information by providing machine list
func (c *Client) internalSyncCluster(machines []string) bool {
for _, machine := range machines {
httpPath := c.createHttpPath(machine, "v1/machines")
httpPath := c.createHttpPath(machine, version+"/machines")
resp, err := c.httpClient.Get(httpPath)
if err != nil {
// try another machine in the cluster
@ -236,11 +236,12 @@ func (c *Client) sendRequest(method string, _path string, body string) (*http.Re
// try to connect the leader
continue
} else if resp.StatusCode == http.StatusInternalServerError {
resp.Body.Close()
retry++
if retry > 2*len(c.cluster.Machines) {
return nil, errors.New("Cannot reach servers")
}
resp.Body.Close()
continue
} else {
logger.Debug("send.return.response ", httpPath)

View File

@ -1,3 +1,3 @@
package etcd
var version = "v1"
const version = "v1"

View File

@ -31,7 +31,7 @@ func newAppendEntriesRequest(term uint64, prevLogIndex uint64, prevLogTerm uint6
// Encodes the AppendEntriesRequest to a buffer. Returns the number of bytes
// written and any error that may have occurred.
func (req *AppendEntriesRequest) encode(w io.Writer) (int, error) {
func (req *AppendEntriesRequest) Encode(w io.Writer) (int, error) {
protoEntries := make([]*protobuf.ProtoAppendEntriesRequest_ProtoLogEntry, len(req.Entries))
@ -63,7 +63,7 @@ func (req *AppendEntriesRequest) encode(w io.Writer) (int, error) {
// Decodes the AppendEntriesRequest from a buffer. Returns the number of bytes read and
// any error that occurs.
func (req *AppendEntriesRequest) decode(r io.Reader) (int, error) {
func (req *AppendEntriesRequest) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {

View File

@ -10,7 +10,7 @@ func BenchmarkAppendEntriesRequestEncoding(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
req.encode(&buf)
req.Encode(&buf)
}
b.SetBytes(int64(len(tmp)))
}
@ -19,7 +19,7 @@ func BenchmarkAppendEntriesRequestDecoding(b *testing.B) {
req, buf := createTestAppendEntriesRequest(2000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
req.decode(bytes.NewReader(buf))
req.Decode(bytes.NewReader(buf))
}
b.SetBytes(int64(len(buf)))
}
@ -34,7 +34,7 @@ func createTestAppendEntriesRequest(entryCount int) (*AppendEntriesRequest, []by
req := newAppendEntriesRequest(1, 1, 1, 1, "leader", entries)
var buf bytes.Buffer
req.encode(&buf)
req.Encode(&buf)
return req, buf.Bytes()
}

View File

@ -30,7 +30,7 @@ func newAppendEntriesResponse(term uint64, success bool, index uint64, commitInd
// Encodes the AppendEntriesResponse to a buffer. Returns the number of bytes
// written and any error that may have occurred.
func (resp *AppendEntriesResponse) encode(w io.Writer) (int, error) {
func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoAppendEntriesResponse{
Term: proto.Uint64(resp.Term),
Index: proto.Uint64(resp.Index),
@ -47,7 +47,7 @@ func (resp *AppendEntriesResponse) encode(w io.Writer) (int, error) {
// Decodes the AppendEntriesResponse from a buffer. Returns the number of bytes read and
// any error that occurs.
func (resp *AppendEntriesResponse) decode(r io.Reader) (int, error) {
func (resp *AppendEntriesResponse) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {

View File

@ -10,7 +10,7 @@ func BenchmarkAppendEntriesResponseEncoding(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf bytes.Buffer
req.encode(&buf)
req.Encode(&buf)
}
b.SetBytes(int64(len(tmp)))
}
@ -19,7 +19,7 @@ func BenchmarkAppendEntriesResponseDecoding(b *testing.B) {
req, buf := createTestAppendEntriesResponse(2000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
req.decode(bytes.NewReader(buf))
req.Decode(bytes.NewReader(buf))
}
b.SetBytes(int64(len(buf)))
}
@ -28,7 +28,7 @@ func createTestAppendEntriesResponse(entryCount int) (*AppendEntriesResponse, []
resp := newAppendEntriesResponse(1, true, 1, 1)
var buf bytes.Buffer
resp.encode(&buf)
resp.Encode(&buf)
return resp, buf.Bytes()
}

View File

@ -89,7 +89,7 @@ func (t *HTTPTransporter) Install(server *Server, mux HTTPMuxer) {
// Sends an AppendEntries RPC to a peer.
func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse {
var b bytes.Buffer
if _, err := req.encode(&b); err != nil {
if _, err := req.Encode(&b); err != nil {
traceln("transporter.ae.encoding.error:", err)
return nil
}
@ -106,7 +106,7 @@ func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, r
defer httpResp.Body.Close()
resp := &AppendEntriesResponse{}
if _, err = resp.decode(httpResp.Body); err != nil && err != io.EOF {
if _, err = resp.Decode(httpResp.Body); err != nil && err != io.EOF {
traceln("transporter.ae.decoding.error:", err)
return nil
}
@ -117,7 +117,7 @@ func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, r
// Sends a RequestVote RPC to a peer.
func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse {
var b bytes.Buffer
if _, err := req.encode(&b); err != nil {
if _, err := req.Encode(&b); err != nil {
traceln("transporter.rv.encoding.error:", err)
return nil
}
@ -134,7 +134,7 @@ func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *Reque
defer httpResp.Body.Close()
resp := &RequestVoteResponse{}
if _, err = resp.decode(httpResp.Body); err != nil && err != io.EOF {
if _, err = resp.Decode(httpResp.Body); err != nil && err != io.EOF {
traceln("transporter.rv.decoding.error:", err)
return nil
}
@ -162,13 +162,13 @@ func (t *HTTPTransporter) appendEntriesHandler(server *Server) http.HandlerFunc
traceln(server.Name(), "RECV /appendEntries")
req := &AppendEntriesRequest{}
if _, err := req.decode(r.Body); err != nil {
if _, err := req.Decode(r.Body); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
resp := server.AppendEntries(req)
if _, err := resp.encode(w); err != nil {
if _, err := resp.Encode(w); err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}
@ -181,13 +181,13 @@ func (t *HTTPTransporter) requestVoteHandler(server *Server) http.HandlerFunc {
traceln(server.Name(), "RECV /requestVote")
req := &RequestVoteRequest{}
if _, err := req.decode(r.Body); err != nil {
if _, err := req.Decode(r.Body); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
resp := server.RequestVote(req)
if _, err := resp.encode(w); err != nil {
if _, err := resp.Encode(w); err != nil {
http.Error(w, "", http.StatusInternalServerError)
return
}

View File

@ -180,26 +180,23 @@ func (l *Log) open(path string) error {
}
break
}
// Append entry.
l.entries = append(l.entries, entry)
if entry.Index <= l.commitIndex {
command, err := newCommand(entry.CommandName, entry.Command)
if err != nil {
continue
if entry.Index > l.startIndex {
// Append entry.
l.entries = append(l.entries, entry)
if entry.Index <= l.commitIndex {
command, err := newCommand(entry.CommandName, entry.Command)
if err != nil {
continue
}
l.ApplyFunc(command)
}
l.ApplyFunc(command)
debugln("open.log.append log index ", entry.Index)
}
debugln("open.log.append log index ", entry.Index)
readBytes += int64(n)
}
l.results = make([]*logResult, len(l.entries))
l.compact(l.startIndex, l.startTerm)
debugln("open.log.recovery number of log ", len(l.entries))
return nil
}
@ -273,6 +270,8 @@ func (l *Log) getEntriesAfter(index uint64, maxLogEntriesPerRequest uint64) ([]*
entries := l.entries[index-l.startIndex:]
length := len(entries)
traceln("log.entriesAfter: startIndex:", l.startIndex, " lenght", len(l.entries))
if uint64(length) < maxLogEntriesPerRequest {
// Determine the term at the given entry and return a subslice.
return entries, l.entries[index-1-l.startIndex].Term
@ -353,7 +352,10 @@ func (l *Log) lastInfo() (index uint64, term uint64) {
func (l *Log) updateCommitIndex(index uint64) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.commitIndex = index
if index > l.commitIndex {
l.commitIndex = index
}
debugln("update.commit.index ", index)
}
// Updates the commit index and writes entries after that index to the stable storage.

View File

@ -255,6 +255,12 @@ func (p *Peer) sendSnapshotRecoveryRequest() {
req := newSnapshotRecoveryRequest(p.server.name, p.server.lastSnapshot)
debugln("peer.snap.recovery.send: ", p.Name)
resp := p.server.Transporter().SendSnapshotRecoveryRequest(p.server, p, req)
if resp == nil {
debugln("peer.snap.recovery.timeout: ", p.Name)
return
}
if resp.Success {
p.prevLogIndex = req.LastIndex
} else {

View File

@ -28,7 +28,7 @@ func newRequestVoteRequest(term uint64, candidateName string, lastLogIndex uint6
// Encodes the RequestVoteRequest to a buffer. Returns the number of bytes
// written and any error that may have occurred.
func (req *RequestVoteRequest) encode(w io.Writer) (int, error) {
func (req *RequestVoteRequest) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoRequestVoteRequest{
Term: proto.Uint64(req.Term),
LastLogIndex: proto.Uint64(req.LastLogIndex),
@ -45,7 +45,7 @@ func (req *RequestVoteRequest) encode(w io.Writer) (int, error) {
// Decodes the RequestVoteRequest from a buffer. Returns the number of bytes read and
// any error that occurs.
func (req *RequestVoteRequest) decode(r io.Reader) (int, error) {
func (req *RequestVoteRequest) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {

View File

@ -24,7 +24,7 @@ func newRequestVoteResponse(term uint64, voteGranted bool) *RequestVoteResponse
// Encodes the RequestVoteResponse to a buffer. Returns the number of bytes
// written and any error that may have occurred.
func (resp *RequestVoteResponse) encode(w io.Writer) (int, error) {
func (resp *RequestVoteResponse) Encode(w io.Writer) (int, error) {
pb := &protobuf.ProtoRequestVoteResponse{
Term: proto.Uint64(resp.Term),
VoteGranted: proto.Bool(resp.VoteGranted),
@ -40,7 +40,7 @@ func (resp *RequestVoteResponse) encode(w io.Writer) (int, error) {
// Decodes the RequestVoteResponse from a buffer. Returns the number of bytes read and
// any error that occurs.
func (resp *RequestVoteResponse) decode(r io.Reader) (int, error) {
func (resp *RequestVoteResponse) Decode(r io.Reader) (int, error) {
data, err := ioutil.ReadAll(r)
if err != nil {

View File

@ -80,6 +80,8 @@ type Server struct {
lastSnapshot *Snapshot
stateMachine StateMachine
maxLogEntriesPerRequest uint64
connectionString string
}
// An event to be processed by the server's event loop.
@ -96,7 +98,7 @@ type event struct {
//------------------------------------------------------------------------------
// Creates a new server with a log at the given path.
func NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, context interface{}) (*Server, error) {
func NewServer(name string, path string, transporter Transporter, stateMachine StateMachine, context interface{}, connectiongString string) (*Server, error) {
if name == "" {
return nil, errors.New("raft.Server: Name cannot be blank")
}
@ -117,6 +119,7 @@ func NewServer(name string, path string, transporter Transporter, stateMachine S
electionTimeout: DefaultElectionTimeout,
heartbeatTimeout: DefaultHeartbeatTimeout,
maxLogEntriesPerRequest: MaxLogEntriesPerRequest,
connectionString: connectiongString,
}
// Setup apply function.
@ -1009,10 +1012,16 @@ func (s *Server) TakeSnapshot() error {
state = []byte{0}
}
var peers []*Peer
peers := make([]*Peer, len(s.peers)+1)
i := 0
for _, peer := range s.peers {
peers = append(peers, peer.clone())
peers[i] = peer.clone()
}
peers[i] = &Peer{
Name: s.Name(),
ConnectionString: s.connectionString,
}
s.currentSnapshot = &Snapshot{lastIndex, lastTerm, peers, state, path}
@ -1253,7 +1262,7 @@ func (s *Server) readConf() error {
return err
}
s.log.commitIndex = conf.CommitIndex
s.log.updateCommitIndex(conf.CommitIndex)
return nil
}

View File

@ -428,7 +428,7 @@ func TestServerRecoverFromPreviousLogAndConf(t *testing.T) {
for _, name := range names {
server := servers[name]
if server.CommitIndex() != 17 {
t.Fatalf("%s commitIndex is invalid [%d/%d]", name, server.CommitIndex(), 16)
t.Fatalf("%s commitIndex is invalid [%d/%d]", name, server.CommitIndex(), 17)
}
server.Stop()
}

View File

@ -65,12 +65,12 @@ func newTestServer(name string, transporter Transporter) *Server {
if err := os.MkdirAll(p, 0644); err != nil {
panic(err.Error())
}
server, _ := NewServer(name, p, transporter, nil, nil)
server, _ := NewServer(name, p, transporter, nil, nil, "")
return server
}
func newTestServerWithPath(name string, transporter Transporter, p string) *Server {
server, _ := NewServer(name, p, transporter, nil, nil)
server, _ := NewServer(name, p, transporter, nil, nil, "")
return server
}