decoder.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 wal
  15. import (
  16. "bufio"
  17. "encoding/binary"
  18. "hash"
  19. "io"
  20. "sync"
  21. "github.com/coreos/etcd/pkg/crc"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/wal/walpb"
  25. )
  26. type decoder struct {
  27. mu sync.Mutex
  28. brs []*bufio.Reader
  29. // lastValidOff file offset following the last valid decoded record
  30. lastValidOff int64
  31. crc hash.Hash32
  32. }
  33. func newDecoder(r ...io.Reader) *decoder {
  34. readers := make([]*bufio.Reader, len(r))
  35. for i := range r {
  36. readers[i] = bufio.NewReader(r[i])
  37. }
  38. return &decoder{
  39. brs: readers,
  40. crc: crc.New(0, crcTable),
  41. }
  42. }
  43. func (d *decoder) decode(rec *walpb.Record) error {
  44. rec.Reset()
  45. d.mu.Lock()
  46. defer d.mu.Unlock()
  47. return d.decodeRecord(rec)
  48. }
  49. func (d *decoder) decodeRecord(rec *walpb.Record) error {
  50. if len(d.brs) == 0 {
  51. return io.EOF
  52. }
  53. l, err := readInt64(d.brs[0])
  54. if err == io.EOF {
  55. d.brs = d.brs[1:]
  56. d.lastValidOff = 0
  57. return d.decodeRecord(rec)
  58. }
  59. if err != nil {
  60. return err
  61. }
  62. if l == 0 {
  63. // hit preallocated space
  64. d.brs = d.brs[1:]
  65. if len(d.brs) == 0 {
  66. return io.EOF
  67. }
  68. d.lastValidOff = 0
  69. return d.decodeRecord(rec)
  70. }
  71. data := make([]byte, l)
  72. if _, err = io.ReadFull(d.brs[0], data); err != nil {
  73. // ReadFull returns io.EOF only if no bytes were read
  74. // the decoder should treat this as an ErrUnexpectedEOF instead.
  75. if err == io.EOF {
  76. err = io.ErrUnexpectedEOF
  77. }
  78. return err
  79. }
  80. if err := rec.Unmarshal(data); err != nil {
  81. return err
  82. }
  83. // skip crc checking if the record type is crcType
  84. if rec.Type != crcType {
  85. d.crc.Write(rec.Data)
  86. if err := rec.Validate(d.crc.Sum32()); err != nil {
  87. return err
  88. }
  89. }
  90. // record decoded as valid; point last valid offset to end of record
  91. d.lastValidOff += l + 8
  92. return nil
  93. }
  94. func (d *decoder) updateCRC(prevCrc uint32) {
  95. d.crc = crc.New(prevCrc, crcTable)
  96. }
  97. func (d *decoder) lastCRC() uint32 {
  98. return d.crc.Sum32()
  99. }
  100. func (d *decoder) lastOffset() int64 { return d.lastValidOff }
  101. func mustUnmarshalEntry(d []byte) raftpb.Entry {
  102. var e raftpb.Entry
  103. pbutil.MustUnmarshal(&e, d)
  104. return e
  105. }
  106. func mustUnmarshalState(d []byte) raftpb.HardState {
  107. var s raftpb.HardState
  108. pbutil.MustUnmarshal(&s, d)
  109. return s
  110. }
  111. func readInt64(r io.Reader) (int64, error) {
  112. var n int64
  113. err := binary.Read(r, binary.LittleEndian, &n)
  114. return n, err
  115. }