etcd/rafthttp/transport.go

288 lines
8.2 KiB
Go
Raw Normal View History

// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rafthttp
import (
"net/http"
2014-12-29 04:44:26 +03:00
"sync"
"time"
2015-06-03 01:33:58 +03:00
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
2015-08-03 04:07:16 +03:00
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/xiang90/probing"
2015-01-30 02:59:58 +03:00
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/etcdserver/stats"
"github.com/coreos/etcd/pkg/types"
2015-03-01 19:17:14 +03:00
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
)
2015-06-11 01:30:43 +03:00
var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "rafthttp")
2015-06-03 01:33:58 +03:00
2014-12-29 04:44:26 +03:00
type Raft interface {
Process(ctx context.Context, m raftpb.Message) error
IsIDRemoved(id uint64) bool
ReportUnreachable(id uint64)
2015-03-01 19:17:14 +03:00
ReportSnapshot(id uint64, status raft.SnapshotStatus)
}
type Transporter interface {
// Start starts the given Transporter.
// Start MUST be called before calling other functions in the interface.
Start()
// Handler returns the HTTP handler of the transporter.
// A transporter HTTP handler handles the HTTP requests
// from remote peers.
// The handler MUST be used to handle RaftPrefix(/raft)
// endpoint.
Handler() http.Handler
// Send sends out the given messages to the remote peers.
// Each message has a To field, which is an id that maps
// to an existing peer in the transport.
// If the id cannot be found in the transport, the message
// will be ignored.
Send(m []raftpb.Message)
// AddRemote adds a remote with given peer urls into the transport.
// A remote helps newly joined member to catch up the progress of cluster,
// and will not be used after that.
2015-08-07 20:57:11 +03:00
// It is the caller's responsibility to ensure the urls are all valid,
// or it panics.
AddRemote(id types.ID, urls []string)
// AddPeer adds a peer with given peer urls into the transport.
2015-08-07 20:57:11 +03:00
// It is the caller's responsibility to ensure the urls are all valid,
// or it panics.
// Peer urls are used to connect to the remote peer.
AddPeer(id types.ID, urls []string)
// RemovePeer removes the peer with given id.
RemovePeer(id types.ID)
// RemoveAllPeers removes all the existing peers in the transport.
RemoveAllPeers()
// UpdatePeer updates the peer urls of the peer with the given id.
2015-08-07 20:57:11 +03:00
// It is the caller's responsibility to ensure the urls are all valid,
// or it panics.
UpdatePeer(id types.ID, urls []string)
// ActiveSince returns the time that the connection with the peer
// of the given id becomes active.
// If the connection is active since peer was added, it returns the adding time.
// If the connection is currently inactive, it returns zero time.
ActiveSince(id types.ID) time.Time
// Stop closes the connections and stops the transporter.
Stop()
}
// Transport implements Transporter interface. It provides the functionality
// to send raft messages to peers, and receive raft messages from peers.
// User should call Handler method to get a handler to serve requests
// received from peerURLs.
// User needs to call Start before calling other functions, and call
// Stop when the Transport is no longer used.
type Transport struct {
RoundTripper http.RoundTripper // roundTripper to send requests
ID types.ID // local member ID
ClusterID types.ID // raft cluster ID for request validation
Raft Raft // raft state machine, to which the Transport forwards received messages and reports status
ServerStats *stats.ServerStats // used to record general transportation statistics
// used to record transportation statistics with followers when
// performing as leader in raft protocol
LeaderStats *stats.LeaderStats
// error channel used to report detected critical error, e.g.,
// the member has been permanently removed from the cluster
// When an error is received from ErrorC, user should stop raft state
// machine and thus stop the Transport.
ErrorC chan error
mu sync.RWMutex // protect the term, remote and peer map
term uint64 // the latest term that has been observed
remotes map[types.ID]*remote // remotes map that helps newly joined member to catch up
peers map[types.ID]Peer // peers map
2015-08-03 04:07:16 +03:00
prober probing.Prober
}
func (t *Transport) Start() {
t.remotes = make(map[types.ID]*remote)
t.peers = make(map[types.ID]Peer)
t.prober = probing.NewProber(t.RoundTripper)
2014-12-29 04:44:26 +03:00
}
func (t *Transport) Handler() http.Handler {
pipelineHandler := NewHandler(t.Raft, t.ClusterID)
streamHandler := newStreamHandler(t, t.Raft, t.ID, t.ClusterID)
mux := http.NewServeMux()
mux.Handle(RaftPrefix, pipelineHandler)
mux.Handle(RaftStreamPrefix+"/", streamHandler)
2015-08-03 04:07:16 +03:00
mux.Handle(ProbingPrefix, probing.NewHandler())
2014-12-29 04:44:26 +03:00
return mux
}
func (t *Transport) Get(id types.ID) Peer {
2014-12-29 04:44:26 +03:00
t.mu.RLock()
defer t.mu.RUnlock()
return t.peers[id]
}
func (t *Transport) maybeUpdatePeersTerm(term uint64) {
t.mu.Lock()
defer t.mu.Unlock()
if t.term >= term {
return
}
t.term = term
for _, p := range t.peers {
p.setTerm(term)
}
}
func (t *Transport) Send(msgs []raftpb.Message) {
2014-12-29 04:44:26 +03:00
for _, m := range msgs {
// intentionally dropped message
if m.To == 0 {
continue
}
to := types.ID(m.To)
if m.Type != raftpb.MsgProp { // proposal message does not have a valid term
t.maybeUpdatePeersTerm(m.Term)
}
2014-12-29 04:44:26 +03:00
p, ok := t.peers[to]
if ok {
if m.Type == raftpb.MsgApp {
t.ServerStats.SendAppendReq(m.Size())
}
p.Send(m)
2014-12-29 04:44:26 +03:00
continue
}
g, ok := t.remotes[to]
if ok {
g.Send(m)
continue
2014-12-29 04:44:26 +03:00
}
2015-06-03 01:33:58 +03:00
plog.Debugf("ignored message %s (sent to unknown peer %s)", m.Type, to)
2014-12-29 04:44:26 +03:00
}
}
func (t *Transport) Stop() {
for _, r := range t.remotes {
r.Stop()
}
2014-12-29 04:44:26 +03:00
for _, p := range t.peers {
p.Stop()
}
t.prober.RemoveAll()
if tr, ok := t.RoundTripper.(*http.Transport); ok {
2014-12-29 04:44:26 +03:00
tr.CloseIdleConnections()
}
}
func (t *Transport) AddRemote(id types.ID, us []string) {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.remotes[id]; ok {
return
}
urls, err := types.NewURLs(us)
if err != nil {
2015-06-03 01:33:58 +03:00
plog.Panicf("newURLs %+v should never fail: %+v", us, err)
}
t.remotes[id] = startRemote(t.RoundTripper, urls, t.ID, id, t.ClusterID, t.Raft, t.ErrorC)
}
func (t *Transport) AddPeer(id types.ID, us []string) {
2014-12-29 04:44:26 +03:00
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.peers[id]; ok {
return
}
2015-02-27 18:54:06 +03:00
urls, err := types.NewURLs(us)
2014-12-29 04:44:26 +03:00
if err != nil {
2015-06-03 01:33:58 +03:00
plog.Panicf("newURLs %+v should never fail: %+v", us, err)
2014-12-29 04:44:26 +03:00
}
fs := t.LeaderStats.Follower(id.String())
t.peers[id] = startPeer(t.RoundTripper, urls, t.ID, id, t.ClusterID, t.Raft, fs, t.ErrorC, t.term)
2015-08-03 04:07:16 +03:00
addPeerToProber(t.prober, id.String(), us)
2014-12-29 04:44:26 +03:00
}
func (t *Transport) RemovePeer(id types.ID) {
2014-12-29 04:44:26 +03:00
t.mu.Lock()
defer t.mu.Unlock()
t.removePeer(id)
}
func (t *Transport) RemoveAllPeers() {
t.mu.Lock()
defer t.mu.Unlock()
for id := range t.peers {
t.removePeer(id)
}
}
// the caller of this function must have the peers mutex.
func (t *Transport) removePeer(id types.ID) {
if peer, ok := t.peers[id]; ok {
peer.Stop()
} else {
2015-06-03 01:33:58 +03:00
plog.Panicf("unexpected removal of unknown peer '%d'", id)
}
2014-12-29 04:44:26 +03:00
delete(t.peers, id)
delete(t.LeaderStats.Followers, id.String())
2015-08-03 04:07:16 +03:00
t.prober.Remove(id.String())
2014-12-29 04:44:26 +03:00
}
func (t *Transport) UpdatePeer(id types.ID, us []string) {
2014-12-29 04:44:26 +03:00
t.mu.Lock()
defer t.mu.Unlock()
// TODO: return error or just panic?
if _, ok := t.peers[id]; !ok {
return
}
2015-02-27 18:54:06 +03:00
urls, err := types.NewURLs(us)
2014-12-29 04:44:26 +03:00
if err != nil {
2015-06-03 01:33:58 +03:00
plog.Panicf("newURLs %+v should never fail: %+v", us, err)
2014-12-29 04:44:26 +03:00
}
2015-02-27 18:54:06 +03:00
t.peers[id].Update(urls)
2015-08-03 04:07:16 +03:00
t.prober.Remove(id.String())
addPeerToProber(t.prober, id.String(), us)
2014-12-29 04:44:26 +03:00
}
func (t *Transport) ActiveSince(id types.ID) time.Time {
t.mu.Lock()
defer t.mu.Unlock()
if p, ok := t.peers[id]; ok {
return p.activeSince()
}
return time.Time{}
}
2014-12-29 23:20:52 +03:00
type Pausable interface {
Pause()
Resume()
}
2014-12-29 04:44:26 +03:00
// for testing
func (t *Transport) Pause() {
2014-12-29 04:44:26 +03:00
for _, p := range t.peers {
2015-02-26 11:13:35 +03:00
p.(Pausable).Pause()
2014-12-29 04:44:26 +03:00
}
}
func (t *Transport) Resume() {
2014-12-29 04:44:26 +03:00
for _, p := range t.peers {
2015-02-26 11:13:35 +03:00
p.(Pausable).Resume()
2014-12-29 04:44:26 +03:00
}
}