decoder.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 || (err == nil && l == 0) {
  55. // hit end of file or preallocated space
  56. d.brs = d.brs[1:]
  57. if len(d.brs) == 0 {
  58. return io.EOF
  59. }
  60. d.lastValidOff = 0
  61. return d.decodeRecord(rec)
  62. }
  63. if err != nil {
  64. return err
  65. }
  66. data := make([]byte, l)
  67. if _, err = io.ReadFull(d.brs[0], data); err != nil {
  68. // ReadFull returns io.EOF only if no bytes were read
  69. // the decoder should treat this as an ErrUnexpectedEOF instead.
  70. if err == io.EOF {
  71. err = io.ErrUnexpectedEOF
  72. }
  73. return err
  74. }
  75. if err := rec.Unmarshal(data); err != nil {
  76. return err
  77. }
  78. // skip crc checking if the record type is crcType
  79. if rec.Type != crcType {
  80. d.crc.Write(rec.Data)
  81. if err := rec.Validate(d.crc.Sum32()); err != nil {
  82. return err
  83. }
  84. }
  85. // record decoded as valid; point last valid offset to end of record
  86. d.lastValidOff += l + 8
  87. return nil
  88. }
  89. func (d *decoder) updateCRC(prevCrc uint32) {
  90. d.crc = crc.New(prevCrc, crcTable)
  91. }
  92. func (d *decoder) lastCRC() uint32 {
  93. return d.crc.Sum32()
  94. }
  95. func (d *decoder) lastOffset() int64 { return d.lastValidOff }
  96. func mustUnmarshalEntry(d []byte) raftpb.Entry {
  97. var e raftpb.Entry
  98. pbutil.MustUnmarshal(&e, d)
  99. return e
  100. }
  101. func mustUnmarshalState(d []byte) raftpb.HardState {
  102. var s raftpb.HardState
  103. pbutil.MustUnmarshal(&s, d)
  104. return s
  105. }
  106. func readInt64(r io.Reader) (int64, error) {
  107. var n int64
  108. err := binary.Read(r, binary.LittleEndian, &n)
  109. return n, err
  110. }