influxdb/snapshot_recovery_request.go

96 lines
2.3 KiB
Go
Raw Normal View History

2013-07-17 00:40:19 +00:00
package raft
2013-07-18 20:39:07 +00:00
import (
"code.google.com/p/goprotobuf/proto"
"github.com/goraft/raft/protobuf"
2013-07-18 20:39:07 +00:00
"io"
"io/ioutil"
)
2013-07-17 00:40:19 +00:00
// The request sent to a server to start from the snapshot.
type SnapshotRecoveryRequest struct {
2013-07-18 20:39:07 +00:00
LeaderName string
LastIndex uint64
LastTerm uint64
2013-08-15 23:35:01 +00:00
Peers []*Peer
2013-07-18 20:39:07 +00:00
State []byte
2013-07-17 00:40:19 +00:00
}
//------------------------------------------------------------------------------
//
// Constructors
//
//------------------------------------------------------------------------------
// Creates a new Snapshot request.
func newSnapshotRecoveryRequest(leaderName string, snapshot *Snapshot) *SnapshotRecoveryRequest {
return &SnapshotRecoveryRequest{
LeaderName: leaderName,
LastIndex: snapshot.LastIndex,
LastTerm: snapshot.LastTerm,
Peers: snapshot.Peers,
State: snapshot.State,
}
}
2013-07-18 20:39:07 +00:00
// Encodes the SnapshotRecoveryRequest to a buffer. Returns the number of bytes
// written and any error that may have occurred.
2013-09-22 05:15:54 +00:00
func (req *SnapshotRecoveryRequest) Encode(w io.Writer) (int, error) {
2013-08-15 23:35:01 +00:00
protoPeers := make([]*protobuf.ProtoSnapshotRecoveryRequest_ProtoPeer, len(req.Peers))
for i, peer := range req.Peers {
protoPeers[i] = &protobuf.ProtoSnapshotRecoveryRequest_ProtoPeer{
Name: proto.String(peer.Name),
ConnectionString: proto.String(peer.ConnectionString),
}
}
2013-07-18 20:39:07 +00:00
pb := &protobuf.ProtoSnapshotRecoveryRequest{
LeaderName: proto.String(req.LeaderName),
LastIndex: proto.Uint64(req.LastIndex),
LastTerm: proto.Uint64(req.LastTerm),
2013-08-15 23:35:01 +00:00
Peers: protoPeers,
2013-07-18 20:39:07 +00:00
State: req.State,
}
p, err := proto.Marshal(pb)
2013-07-18 20:39:07 +00:00
if err != nil {
return -1, err
}
return w.Write(p)
2013-07-18 20:39:07 +00:00
}
// Decodes the SnapshotRecoveryRequest from a buffer. Returns the number of bytes read and
// any error that occurs.
2013-09-22 05:15:54 +00:00
func (req *SnapshotRecoveryRequest) Decode(r io.Reader) (int, error) {
2013-07-18 20:39:07 +00:00
data, err := ioutil.ReadAll(r)
if err != nil {
return 0, err
2013-07-17 00:40:19 +00:00
}
2013-07-18 20:39:07 +00:00
totalBytes := len(data)
2013-08-15 23:35:01 +00:00
pb := &protobuf.ProtoSnapshotRecoveryRequest{}
if err = proto.Unmarshal(data, pb); err != nil {
2013-07-18 20:39:07 +00:00
return -1, err
}
req.LeaderName = pb.GetLeaderName()
req.LastIndex = pb.GetLastIndex()
req.LastTerm = pb.GetLastTerm()
req.State = pb.GetState()
2013-07-18 20:39:07 +00:00
2013-08-15 23:35:01 +00:00
req.Peers = make([]*Peer, len(pb.Peers))
for i, peer := range pb.Peers {
req.Peers[i] = &Peer{
Name: peer.GetName(),
ConnectionString: peer.GetConnectionString(),
}
}
2013-07-18 20:39:07 +00:00
return totalBytes, nil
2013-07-17 00:40:19 +00:00
}