needle_write.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package needle
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  7. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. "math"
  10. "sync"
  11. )
  12. var bufPool = sync.Pool{
  13. New: func() interface{} {
  14. return new(bytes.Buffer)
  15. },
  16. }
  17. func (n *Needle) prepareWriteBuffer(version Version, writeBytes *bytes.Buffer) (Size, int64, error) {
  18. writeBytes.Reset()
  19. switch version {
  20. case Version1:
  21. header := make([]byte, NeedleHeaderSize)
  22. CookieToBytes(header[0:CookieSize], n.Cookie)
  23. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  24. n.Size = Size(len(n.Data))
  25. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  26. size := n.Size
  27. actualSize := NeedleHeaderSize + int64(n.Size)
  28. writeBytes.Write(header)
  29. writeBytes.Write(n.Data)
  30. padding := PaddingLength(n.Size, version)
  31. util.Uint32toBytes(header[0:NeedleChecksumSize], uint32(n.Checksum))
  32. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  33. return size, actualSize, nil
  34. case Version2, Version3:
  35. header := make([]byte, NeedleHeaderSize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
  36. CookieToBytes(header[0:CookieSize], n.Cookie)
  37. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  38. if len(n.Name) >= math.MaxUint8 {
  39. n.NameSize = math.MaxUint8
  40. } else {
  41. n.NameSize = uint8(len(n.Name))
  42. }
  43. n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
  44. if n.DataSize > 0 {
  45. n.Size = 4 + Size(n.DataSize) + 1
  46. if n.HasName() {
  47. n.Size = n.Size + 1 + Size(n.NameSize)
  48. }
  49. if n.HasMime() {
  50. n.Size = n.Size + 1 + Size(n.MimeSize)
  51. }
  52. if n.HasLastModifiedDate() {
  53. n.Size = n.Size + LastModifiedBytesLength
  54. }
  55. if n.HasTtl() {
  56. n.Size = n.Size + TtlBytesLength
  57. }
  58. if n.HasPairs() {
  59. n.Size += 2 + Size(n.PairsSize)
  60. }
  61. } else {
  62. n.Size = 0
  63. }
  64. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  65. writeBytes.Write(header[0:NeedleHeaderSize])
  66. if n.DataSize > 0 {
  67. util.Uint32toBytes(header[0:4], n.DataSize)
  68. writeBytes.Write(header[0:4])
  69. writeBytes.Write(n.Data)
  70. util.Uint8toBytes(header[0:1], n.Flags)
  71. writeBytes.Write(header[0:1])
  72. if n.HasName() {
  73. util.Uint8toBytes(header[0:1], n.NameSize)
  74. writeBytes.Write(header[0:1])
  75. writeBytes.Write(n.Name[:n.NameSize])
  76. }
  77. if n.HasMime() {
  78. util.Uint8toBytes(header[0:1], n.MimeSize)
  79. writeBytes.Write(header[0:1])
  80. writeBytes.Write(n.Mime)
  81. }
  82. if n.HasLastModifiedDate() {
  83. util.Uint64toBytes(header[0:8], n.LastModified)
  84. writeBytes.Write(header[8-LastModifiedBytesLength : 8])
  85. }
  86. if n.HasTtl() && n.Ttl != nil {
  87. n.Ttl.ToBytes(header[0:TtlBytesLength])
  88. writeBytes.Write(header[0:TtlBytesLength])
  89. }
  90. if n.HasPairs() {
  91. util.Uint16toBytes(header[0:2], n.PairsSize)
  92. writeBytes.Write(header[0:2])
  93. writeBytes.Write(n.Pairs)
  94. }
  95. }
  96. padding := PaddingLength(n.Size, version)
  97. util.Uint32toBytes(header[0:NeedleChecksumSize], uint32(n.Checksum))
  98. if version == Version2 {
  99. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  100. } else {
  101. // version3
  102. util.Uint64toBytes(header[NeedleChecksumSize:NeedleChecksumSize+TimestampSize], n.AppendAtNs)
  103. writeBytes.Write(header[0 : NeedleChecksumSize+TimestampSize+padding])
  104. }
  105. return Size(n.DataSize), GetActualSize(n.Size, version), nil
  106. }
  107. return 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
  108. }
  109. func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size Size, actualSize int64, err error) {
  110. if end, _, e := w.GetStat(); e == nil {
  111. defer func(w backend.BackendStorageFile, off int64) {
  112. if err != nil {
  113. if te := w.Truncate(end); te != nil {
  114. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  115. }
  116. }
  117. }(w, end)
  118. offset = uint64(end)
  119. } else {
  120. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  121. return
  122. }
  123. if offset >= MaxPossibleVolumeSize && n.Size.IsValid() {
  124. err = fmt.Errorf("Volume Size %d Exceeded %d", offset, MaxPossibleVolumeSize)
  125. return
  126. }
  127. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  128. defer bufPool.Put(bytesBuffer)
  129. size, actualSize, err = n.prepareWriteBuffer(version, bytesBuffer)
  130. if err == nil {
  131. _, err = w.WriteAt(bytesBuffer.Bytes(), int64(offset))
  132. }
  133. return offset, size, actualSize, err
  134. }
  135. func WriteNeedleBlob(w backend.BackendStorageFile, dataSlice []byte, size Size, appendAtNs uint64, version Version) (offset uint64, err error) {
  136. if end, _, e := w.GetStat(); e == nil {
  137. defer func(w backend.BackendStorageFile, off int64) {
  138. if err != nil {
  139. if te := w.Truncate(end); te != nil {
  140. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  141. }
  142. }
  143. }(w, end)
  144. offset = uint64(end)
  145. } else {
  146. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  147. return
  148. }
  149. if version == Version3 {
  150. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  151. util.Uint64toBytes(dataSlice[tsOffset:tsOffset+TimestampSize], appendAtNs)
  152. }
  153. if err == nil {
  154. _, err = w.WriteAt(dataSlice, int64(offset))
  155. }
  156. return
  157. }