msgapp.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 CoreOS, Inc.
  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. package rafthttp
  15. import (
  16. "encoding/binary"
  17. "io"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/stats"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. )
  23. // msgAppEncoder is a optimized encoder for append messages. It assumes
  24. // that the decoder has enough information to recover the fields except
  25. // Entries, and it writes only Entries into the Writer.
  26. // It MUST be used with a paired msgAppDecoder.
  27. type msgAppEncoder struct {
  28. w io.Writer
  29. // TODO: move the fs stats and use new metrics
  30. fs *stats.FollowerStats
  31. }
  32. func (enc *msgAppEncoder) encode(m raftpb.Message) error {
  33. if isLinkHeartbeatMessage(m) {
  34. return binary.Write(enc.w, binary.BigEndian, uint64(0))
  35. }
  36. start := time.Now()
  37. ents := m.Entries
  38. l := len(ents)
  39. // There is no need to send empty ents, and it avoids confusion with
  40. // heartbeat.
  41. if l == 0 {
  42. return nil
  43. }
  44. if err := binary.Write(enc.w, binary.BigEndian, uint64(l)); err != nil {
  45. return err
  46. }
  47. for i := 0; i < l; i++ {
  48. ent := &ents[i]
  49. if err := writeEntryTo(enc.w, ent); err != nil {
  50. return err
  51. }
  52. }
  53. enc.fs.Succ(time.Since(start))
  54. return nil
  55. }
  56. // msgAppDecoder is a optimized decoder for append messages. It reads data
  57. // from the Reader and parses it into Entries, then builds messages.
  58. type msgAppDecoder struct {
  59. r io.Reader
  60. local, remote types.ID
  61. term uint64
  62. }
  63. func (dec *msgAppDecoder) decode() (raftpb.Message, error) {
  64. var m raftpb.Message
  65. var l uint64
  66. if err := binary.Read(dec.r, binary.BigEndian, &l); err != nil {
  67. return m, err
  68. }
  69. if l == 0 {
  70. return linkHeartbeatMessage, nil
  71. }
  72. ents := make([]raftpb.Entry, int(l))
  73. for i := 0; i < int(l); i++ {
  74. ent := &ents[i]
  75. if err := readEntryFrom(dec.r, ent); err != nil {
  76. return m, err
  77. }
  78. }
  79. m = raftpb.Message{
  80. Type: raftpb.MsgApp,
  81. From: uint64(dec.remote),
  82. To: uint64(dec.local),
  83. Term: dec.term,
  84. LogTerm: dec.term,
  85. Index: ents[0].Index - 1,
  86. Entries: ents,
  87. }
  88. return m, nil
  89. }