encoder.go 2.8 KB

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