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.

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