You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
9.3 KiB

6 years ago
12 years ago
12 years ago
12 years ago
5 years ago
9 years ago
12 years ago
12 years ago
7 years ago
12 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
5 years ago
5 years ago
9 years ago
9 years ago
6 years ago
6 years ago
  1. package needle
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/stats"
  7. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  8. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "io"
  11. )
  12. const (
  13. FlagIsCompressed = 0x01
  14. FlagHasName = 0x02
  15. FlagHasMime = 0x04
  16. FlagHasLastModifiedDate = 0x08
  17. FlagHasTtl = 0x10
  18. FlagHasPairs = 0x20
  19. FlagIsChunkManifest = 0x80
  20. LastModifiedBytesLength = 5
  21. TtlBytesLength = 2
  22. )
  23. var ErrorSizeMismatch = errors.New("size mismatch")
  24. func (n *Needle) DiskSize(version Version) int64 {
  25. return GetActualSize(n.Size, version)
  26. }
  27. func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size Size, version Version) (dataSlice []byte, err error) {
  28. dataSize := GetActualSize(size, version)
  29. dataSlice = make([]byte, int(dataSize))
  30. var n int
  31. n, err = r.ReadAt(dataSlice, offset)
  32. if err != nil && int64(n) == dataSize {
  33. err = nil
  34. }
  35. if err != nil {
  36. fileSize, _, _ := r.GetStat()
  37. glog.Errorf("%s read %d dataSize %d offset %d fileSize %d: %v", r.Name(), n, dataSize, offset, fileSize, err)
  38. }
  39. return dataSlice, err
  40. }
  41. // ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
  42. func (n *Needle) ReadBytes(bytes []byte, offset int64, size Size, version Version) (err error) {
  43. n.ParseNeedleHeader(bytes)
  44. if n.Size != size {
  45. // cookie is not always passed in for this API. Use size to do preliminary checking.
  46. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  47. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatchOffsetSize).Inc()
  48. glog.Errorf("entry not found1: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  49. return ErrorSizeMismatch
  50. }
  51. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatch).Inc()
  52. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  53. }
  54. switch version {
  55. case Version1:
  56. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  57. case Version2, Version3:
  58. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  59. }
  60. if err != nil && err != io.EOF {
  61. return err
  62. }
  63. if size > 0 {
  64. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  65. newChecksum := NewCRC(n.Data)
  66. if checksum != newChecksum.Value() {
  67. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorCRC).Inc()
  68. return errors.New("CRC error! Data On Disk Corrupted")
  69. }
  70. n.Checksum = newChecksum
  71. }
  72. if version == Version3 {
  73. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  74. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  75. }
  76. return nil
  77. }
  78. // ReadData hydrates the needle from the file, with only n.Id is set.
  79. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  80. bytes, err := ReadNeedleBlob(r, offset, size, version)
  81. if err != nil {
  82. return err
  83. }
  84. return n.ReadBytes(bytes, offset, size, version)
  85. }
  86. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  87. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  88. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  89. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  90. }
  91. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  92. index, lenBytes := 0, len(bytes)
  93. if index < lenBytes {
  94. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  95. index = index + 4
  96. if int(n.DataSize)+index > lenBytes {
  97. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  98. return fmt.Errorf("index out of range %d", 1)
  99. }
  100. n.Data = bytes[index : index+int(n.DataSize)]
  101. index = index + int(n.DataSize)
  102. }
  103. _, err = n.readNeedleDataVersion2NonData(bytes[index:])
  104. return
  105. }
  106. func (n *Needle) readNeedleDataVersion2NonData(bytes []byte) (index int, err error) {
  107. lenBytes := len(bytes)
  108. if index < lenBytes {
  109. n.Flags = bytes[index]
  110. index = index + 1
  111. }
  112. if index < lenBytes && n.HasName() {
  113. n.NameSize = uint8(bytes[index])
  114. index = index + 1
  115. if int(n.NameSize)+index > lenBytes {
  116. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  117. return index, fmt.Errorf("index out of range %d", 2)
  118. }
  119. n.Name = bytes[index : index+int(n.NameSize)]
  120. index = index + int(n.NameSize)
  121. }
  122. if index < lenBytes && n.HasMime() {
  123. n.MimeSize = uint8(bytes[index])
  124. index = index + 1
  125. if int(n.MimeSize)+index > lenBytes {
  126. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  127. return index, fmt.Errorf("index out of range %d", 3)
  128. }
  129. n.Mime = bytes[index : index+int(n.MimeSize)]
  130. index = index + int(n.MimeSize)
  131. }
  132. if index < lenBytes && n.HasLastModifiedDate() {
  133. if LastModifiedBytesLength+index > lenBytes {
  134. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  135. return index, fmt.Errorf("index out of range %d", 4)
  136. }
  137. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  138. index = index + LastModifiedBytesLength
  139. }
  140. if index < lenBytes && n.HasTtl() {
  141. if TtlBytesLength+index > lenBytes {
  142. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  143. return index, fmt.Errorf("index out of range %d", 5)
  144. }
  145. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  146. index = index + TtlBytesLength
  147. }
  148. if index < lenBytes && n.HasPairs() {
  149. if 2+index > lenBytes {
  150. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  151. return index, fmt.Errorf("index out of range %d", 6)
  152. }
  153. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  154. index += 2
  155. if int(n.PairsSize)+index > lenBytes {
  156. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  157. return index, fmt.Errorf("index out of range %d", 7)
  158. }
  159. end := index + int(n.PairsSize)
  160. n.Pairs = bytes[index:end]
  161. index = end
  162. }
  163. return index, nil
  164. }
  165. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  166. n = new(Needle)
  167. if version == Version1 || version == Version2 || version == Version3 {
  168. bytes = make([]byte, NeedleHeaderSize)
  169. var count int
  170. count, err = r.ReadAt(bytes, offset)
  171. if count <= 0 || err != nil {
  172. return nil, bytes, 0, err
  173. }
  174. n.ParseNeedleHeader(bytes)
  175. bodyLength = NeedleBodyLength(n.Size, version)
  176. }
  177. return
  178. }
  179. func PaddingLength(needleSize Size, version Version) Size {
  180. if version == Version3 {
  181. // this is same value as version2, but just listed here for clarity
  182. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  183. }
  184. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  185. }
  186. func NeedleBodyLength(needleSize Size, version Version) int64 {
  187. if version == Version3 {
  188. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  189. }
  190. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  191. }
  192. //n should be a needle already read the header
  193. //the input stream will read until next file entry
  194. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  195. if bodyLength <= 0 {
  196. return nil, nil
  197. }
  198. bytes = make([]byte, bodyLength)
  199. if _, err = r.ReadAt(bytes, offset); err != nil {
  200. return
  201. }
  202. err = n.ReadNeedleBodyBytes(bytes, version)
  203. return
  204. }
  205. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  206. if len(needleBody) <= 0 {
  207. return nil
  208. }
  209. switch version {
  210. case Version1:
  211. n.Data = needleBody[:n.Size]
  212. n.Checksum = NewCRC(n.Data)
  213. case Version2, Version3:
  214. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  215. n.Checksum = NewCRC(n.Data)
  216. if version == Version3 {
  217. tsOffset := n.Size + NeedleChecksumSize
  218. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  219. }
  220. default:
  221. err = fmt.Errorf("unsupported version %d!", version)
  222. }
  223. return
  224. }
  225. func (n *Needle) IsCompressed() bool {
  226. return n.Flags&FlagIsCompressed > 0
  227. }
  228. func (n *Needle) SetIsCompressed() {
  229. n.Flags = n.Flags | FlagIsCompressed
  230. }
  231. func (n *Needle) HasName() bool {
  232. return n.Flags&FlagHasName > 0
  233. }
  234. func (n *Needle) SetHasName() {
  235. n.Flags = n.Flags | FlagHasName
  236. }
  237. func (n *Needle) HasMime() bool {
  238. return n.Flags&FlagHasMime > 0
  239. }
  240. func (n *Needle) SetHasMime() {
  241. n.Flags = n.Flags | FlagHasMime
  242. }
  243. func (n *Needle) HasLastModifiedDate() bool {
  244. return n.Flags&FlagHasLastModifiedDate > 0
  245. }
  246. func (n *Needle) SetHasLastModifiedDate() {
  247. n.Flags = n.Flags | FlagHasLastModifiedDate
  248. }
  249. func (n *Needle) HasTtl() bool {
  250. return n.Flags&FlagHasTtl > 0
  251. }
  252. func (n *Needle) SetHasTtl() {
  253. n.Flags = n.Flags | FlagHasTtl
  254. }
  255. func (n *Needle) IsChunkedManifest() bool {
  256. return n.Flags&FlagIsChunkManifest > 0
  257. }
  258. func (n *Needle) SetIsChunkManifest() {
  259. n.Flags = n.Flags | FlagIsChunkManifest
  260. }
  261. func (n *Needle) HasPairs() bool {
  262. return n.Flags&FlagHasPairs != 0
  263. }
  264. func (n *Needle) SetHasPairs() {
  265. n.Flags = n.Flags | FlagHasPairs
  266. }
  267. func GetActualSize(size Size, version Version) int64 {
  268. return NeedleHeaderSize + NeedleBodyLength(size, version)
  269. }