| 1 |
// Copyright 2015 The etcd Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package rafthttp |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"io" |
| 21 |
"io/ioutil" |
| 22 |
"net/http" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/coreos/etcd/pkg/httputil" |
| 26 |
pioutil "github.com/coreos/etcd/pkg/ioutil" |
| 27 |
"github.com/coreos/etcd/pkg/types" |
| 28 |
"github.com/coreos/etcd/raft" |
| 29 |
"github.com/coreos/etcd/snap" |
| 30 |
) |
| 31 |
|
| 32 |
var ( |
| 33 |
// timeout for reading snapshot response body |
| 34 |
snapResponseReadTimeout = 5 * time.Second |
| 35 |
) |
| 36 |
|
| 37 |
type snapshotSender struct { |
| 38 |
from, to types.ID |
| 39 |
cid types.ID |
| 40 |
|
| 41 |
tr *Transport |
| 42 |
picker *urlPicker |
| 43 |
status *peerStatus |
| 44 |
r Raft |
| 45 |
errorc chan error |
| 46 |
|
| 47 |
stopc chan struct{} |
| 48 |
} |
| 49 |
|
| 50 |
func newSnapshotSender(tr *Transport, picker *urlPicker, to types.ID, status *peerStatus) *snapshotSender { |
| 51 |
return &snapshotSender{ |
| 52 |
from: tr.ID, |
| 53 |
to: to, |
| 54 |
cid: tr.ClusterID, |
| 55 |
tr: tr, |
| 56 |
picker: picker, |
| 57 |
status: status, |
| 58 |
r: tr.Raft, |
| 59 |
errorc: tr.ErrorC, |
| 60 |
stopc: make(chan struct{}), |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func (s *snapshotSender) stop() { close(s.stopc) } |
| 65 |
|
| 66 |
func (s *snapshotSender) send(merged snap.Message) { |
| 67 |
m := merged.Message |
| 68 |
|
| 69 |
body := createSnapBody(merged) |
| 70 |
defer body.Close() |
| 71 |
|
| 72 |
u := s.picker.pick() |
| 73 |
req := createPostRequest(u, RaftSnapshotPrefix, body, "application/octet-stream", s.tr.URLs, s.from, s.cid) |
| 74 |
|
| 75 |
plog.Infof("start to send database snapshot [index: %d, to %s]...", m.Snapshot.Metadata.Index, types.ID(m.To)) |
| 76 |
|
| 77 |
err := s.post(req) |
| 78 |
defer merged.CloseWithError(err) |
| 79 |
if err != nil { |
| 80 |
plog.Warningf("database snapshot [index: %d, to: %s] failed to be sent out (%v)", m.Snapshot.Metadata.Index, types.ID(m.To), err) |
| 81 |
|
| 82 |
// errMemberRemoved is a critical error since a removed member should |
| 83 |
// always be stopped. So we use reportCriticalError to report it to errorc. |
| 84 |
if err == errMemberRemoved { |
| 85 |
reportCriticalError(err, s.errorc) |
| 86 |
} |
| 87 |
|
| 88 |
s.picker.unreachable(u) |
| 89 |
s.status.deactivate(failureType{source: sendSnap, action: "post"}, err.Error()) |
| 90 |
s.r.ReportUnreachable(m.To) |
| 91 |
// report SnapshotFailure to raft state machine. After raft state |
| 92 |
// machine knows about it, it would pause a while and retry sending |
| 93 |
// new snapshot message. |
| 94 |
s.r.ReportSnapshot(m.To, raft.SnapshotFailure) |
| 95 |
sentFailures.WithLabelValues(types.ID(m.To).String()).Inc() |
| 96 |
return |
| 97 |
} |
| 98 |
s.status.activate() |
| 99 |
s.r.ReportSnapshot(m.To, raft.SnapshotFinish) |
| 100 |
plog.Infof("database snapshot [index: %d, to: %s] sent out successfully", m.Snapshot.Metadata.Index, types.ID(m.To)) |
| 101 |
|
| 102 |
sentBytes.WithLabelValues(types.ID(m.To).String()).Add(float64(merged.TotalSize)) |
| 103 |
} |
| 104 |
|
| 105 |
// post posts the given request. |
| 106 |
// It returns nil when request is sent out and processed successfully. |
| 107 |
func (s *snapshotSender) post(req *http.Request) (err error) { |
| 108 |
ctx, cancel := context.WithCancel(context.Background()) |
| 109 |
req = req.WithContext(ctx) |
| 110 |
defer cancel() |
| 111 |
|
| 112 |
type responseAndError struct { |
| 113 |
resp *http.Response |
| 114 |
body []byte |
| 115 |
err error |
| 116 |
} |
| 117 |
result := make(chan responseAndError, 1) |
| 118 |
|
| 119 |
go func() { |
| 120 |
resp, err := s.tr.pipelineRt.RoundTrip(req) |
| 121 |
if err != nil { |
| 122 |
result <- responseAndError{resp, nil, err} |
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
// close the response body when timeouts. |
| 127 |
// prevents from reading the body forever when the other side dies right after |
| 128 |
// successfully receives the request body. |
| 129 |
time.AfterFunc(snapResponseReadTimeout, func() { httputil.GracefulClose(resp) }) |
| 130 |
body, err := ioutil.ReadAll(resp.Body) |
| 131 |
result <- responseAndError{resp, body, err} |
| 132 |
}() |
| 133 |
|
| 134 |
select { |
| 135 |
case <-s.stopc: |
| 136 |
return errStopped |
| 137 |
case r := <-result: |
| 138 |
if r.err != nil { |
| 139 |
return r.err |
| 140 |
} |
| 141 |
return checkPostResponse(r.resp, r.body, req, s.to) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func createSnapBody(merged snap.Message) io.ReadCloser { |
| 146 |
buf := new(bytes.Buffer) |
| 147 |
enc := &messageEncoder{w: buf} |
| 148 |
// encode raft message |
| 149 |
if err := enc.encode(&merged.Message); err != nil { |
| 150 |
plog.Panicf("encode message error (%v)", err) |
| 151 |
} |
| 152 |
|
| 153 |
return &pioutil.ReaderAndCloser{ |
| 154 |
Reader: io.MultiReader(buf, merged.ReadCloser), |
| 155 |
Closer: merged.ReadCloser, |
| 156 |
} |
| 157 |
} |