encoder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. package wal
  15. import (
  16. "encoding/binary"
  17. "hash"
  18. "io"
  19. "sync"
  20. "github.com/coreos/etcd/pkg/crc"
  21. "github.com/coreos/etcd/pkg/ioutil"
  22. "github.com/coreos/etcd/wal/walpb"
  23. )
  24. // walPageBytes is the alignment for flushing records to the backing Writer.
  25. // It should be a multiple of the minimum sector size so that WAL repair can
  26. // safely between torn writes and ordinary data corruption.
  27. const walPageBytes = 8 * minSectorSize
  28. type encoder struct {
  29. mu sync.Mutex
  30. bw *ioutil.PageWriter
  31. crc hash.Hash32
  32. buf []byte
  33. uint64buf []byte
  34. }
  35. func newEncoder(w io.Writer, prevCrc uint32) *encoder {
  36. return &encoder{
  37. bw: ioutil.NewPageWriter(w, walPageBytes),
  38. crc: crc.New(prevCrc, crcTable),
  39. // 1MB buffer
  40. buf: make([]byte, 1024*1024),
  41. uint64buf: make([]byte, 8),
  42. }
  43. }
  44. func (e *encoder) encode(rec *walpb.Record) error {
  45. e.mu.Lock()
  46. defer e.mu.Unlock()
  47. e.crc.Write(rec.Data)
  48. rec.Crc = e.crc.Sum32()
  49. var (
  50. data []byte
  51. err error
  52. n int
  53. )
  54. if rec.Size() > len(e.buf) {
  55. data, err = rec.Marshal()
  56. if err != nil {
  57. return err
  58. }
  59. } else {
  60. n, err = rec.MarshalTo(e.buf)
  61. if err != nil {
  62. return err
  63. }
  64. data = e.buf[:n]
  65. }
  66. lenField, padBytes := encodeFrameSize(len(data))
  67. if err = writeUint64(e.bw, lenField, e.uint64buf); err != nil {
  68. return err
  69. }
  70. if padBytes != 0 {
  71. data = append(data, make([]byte, padBytes)...)
  72. }
  73. _, err = e.bw.Write(data)
  74. return err
  75. }
  76. func encodeFrameSize(dataBytes int) (lenField uint64, padBytes int) {
  77. lenField = uint64(dataBytes)
  78. // force 8 byte alignment so length never gets a torn write
  79. padBytes = (8 - (dataBytes % 8)) % 8
  80. if padBytes != 0 {
  81. lenField |= uint64(0x80|padBytes) << 56
  82. }
  83. return
  84. }
  85. func (e *encoder) flush() error {
  86. e.mu.Lock()
  87. defer e.mu.Unlock()
  88. return e.bw.Flush()
  89. }
  90. func writeUint64(w io.Writer, n uint64, buf []byte) error {
  91. // http://golang.org/src/encoding/binary/binary.go
  92. binary.LittleEndian.PutUint64(buf, n)
  93. _, err := w.Write(buf)
  94. return err
  95. }