needle_read_write.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package needle
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  9. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "io"
  12. "math"
  13. "sync"
  14. )
  15. const (
  16. FlagIsCompressed = 0x01
  17. FlagHasName = 0x02
  18. FlagHasMime = 0x04
  19. FlagHasLastModifiedDate = 0x08
  20. FlagHasTtl = 0x10
  21. FlagHasPairs = 0x20
  22. FlagIsChunkManifest = 0x80
  23. LastModifiedBytesLength = 5
  24. TtlBytesLength = 2
  25. )
  26. var ErrorSizeMismatch = errors.New("size mismatch")
  27. func (n *Needle) DiskSize(version Version) int64 {
  28. return GetActualSize(n.Size, version)
  29. }
  30. var bufPool = sync.Pool{
  31. New: func() interface{} {
  32. return new(bytes.Buffer)
  33. },
  34. }
  35. func (n *Needle) prepareWriteBuffer(version Version, writeBytes *bytes.Buffer) (Size, int64, error) {
  36. writeBytes.Reset()
  37. switch version {
  38. case Version1:
  39. header := make([]byte, NeedleHeaderSize)
  40. CookieToBytes(header[0:CookieSize], n.Cookie)
  41. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  42. n.Size = Size(len(n.Data))
  43. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  44. size := n.Size
  45. actualSize := NeedleHeaderSize + int64(n.Size)
  46. writeBytes.Write(header)
  47. writeBytes.Write(n.Data)
  48. padding := PaddingLength(n.Size, version)
  49. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  50. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  51. return size, actualSize, nil
  52. case Version2, Version3:
  53. header := make([]byte, NeedleHeaderSize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
  54. CookieToBytes(header[0:CookieSize], n.Cookie)
  55. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  56. if len(n.Name) >= math.MaxUint8 {
  57. n.NameSize = math.MaxUint8
  58. } else {
  59. n.NameSize = uint8(len(n.Name))
  60. }
  61. n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
  62. if n.DataSize > 0 {
  63. n.Size = 4 + Size(n.DataSize) + 1
  64. if n.HasName() {
  65. n.Size = n.Size + 1 + Size(n.NameSize)
  66. }
  67. if n.HasMime() {
  68. n.Size = n.Size + 1 + Size(n.MimeSize)
  69. }
  70. if n.HasLastModifiedDate() {
  71. n.Size = n.Size + LastModifiedBytesLength
  72. }
  73. if n.HasTtl() {
  74. n.Size = n.Size + TtlBytesLength
  75. }
  76. if n.HasPairs() {
  77. n.Size += 2 + Size(n.PairsSize)
  78. }
  79. } else {
  80. n.Size = 0
  81. }
  82. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  83. writeBytes.Write(header[0:NeedleHeaderSize])
  84. if n.DataSize > 0 {
  85. util.Uint32toBytes(header[0:4], n.DataSize)
  86. writeBytes.Write(header[0:4])
  87. writeBytes.Write(n.Data)
  88. util.Uint8toBytes(header[0:1], n.Flags)
  89. writeBytes.Write(header[0:1])
  90. if n.HasName() {
  91. util.Uint8toBytes(header[0:1], n.NameSize)
  92. writeBytes.Write(header[0:1])
  93. writeBytes.Write(n.Name[:n.NameSize])
  94. }
  95. if n.HasMime() {
  96. util.Uint8toBytes(header[0:1], n.MimeSize)
  97. writeBytes.Write(header[0:1])
  98. writeBytes.Write(n.Mime)
  99. }
  100. if n.HasLastModifiedDate() {
  101. util.Uint64toBytes(header[0:8], n.LastModified)
  102. writeBytes.Write(header[8-LastModifiedBytesLength : 8])
  103. }
  104. if n.HasTtl() && n.Ttl != nil {
  105. n.Ttl.ToBytes(header[0:TtlBytesLength])
  106. writeBytes.Write(header[0:TtlBytesLength])
  107. }
  108. if n.HasPairs() {
  109. util.Uint16toBytes(header[0:2], n.PairsSize)
  110. writeBytes.Write(header[0:2])
  111. writeBytes.Write(n.Pairs)
  112. }
  113. }
  114. padding := PaddingLength(n.Size, version)
  115. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  116. if version == Version2 {
  117. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  118. } else {
  119. // version3
  120. util.Uint64toBytes(header[NeedleChecksumSize:NeedleChecksumSize+TimestampSize], n.AppendAtNs)
  121. writeBytes.Write(header[0 : NeedleChecksumSize+TimestampSize+padding])
  122. }
  123. return Size(n.DataSize), GetActualSize(n.Size, version), nil
  124. }
  125. return 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
  126. }
  127. func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size Size, actualSize int64, err error) {
  128. if end, _, e := w.GetStat(); e == nil {
  129. defer func(w backend.BackendStorageFile, off int64) {
  130. if err != nil {
  131. if te := w.Truncate(end); te != nil {
  132. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  133. }
  134. }
  135. }(w, end)
  136. offset = uint64(end)
  137. } else {
  138. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  139. return
  140. }
  141. if offset >= MaxPossibleVolumeSize && n.Size.IsValid() {
  142. err = fmt.Errorf("Volume Size %d Exeededs %d", offset, MaxPossibleVolumeSize)
  143. return
  144. }
  145. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  146. defer bufPool.Put(bytesBuffer)
  147. size, actualSize, err = n.prepareWriteBuffer(version, bytesBuffer)
  148. if err == nil {
  149. _, err = w.WriteAt(bytesBuffer.Bytes(), int64(offset))
  150. }
  151. return offset, size, actualSize, err
  152. }
  153. func WriteNeedleBlob(w backend.BackendStorageFile, dataSlice []byte, size Size, appendAtNs uint64, version Version) (offset uint64, err error) {
  154. if end, _, e := w.GetStat(); e == nil {
  155. defer func(w backend.BackendStorageFile, off int64) {
  156. if err != nil {
  157. if te := w.Truncate(end); te != nil {
  158. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  159. }
  160. }
  161. }(w, end)
  162. offset = uint64(end)
  163. } else {
  164. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  165. return
  166. }
  167. if version == Version3 {
  168. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  169. util.Uint64toBytes(dataSlice[tsOffset:tsOffset+TimestampSize], appendAtNs)
  170. }
  171. if err == nil {
  172. _, err = w.WriteAt(dataSlice, int64(offset))
  173. }
  174. return
  175. }
  176. func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size Size, version Version) (dataSlice []byte, err error) {
  177. dataSize := GetActualSize(size, version)
  178. dataSlice = make([]byte, int(dataSize))
  179. var n int
  180. n, err = r.ReadAt(dataSlice, offset)
  181. if err != nil && int64(n) == dataSize {
  182. err = nil
  183. }
  184. if err != nil {
  185. fileSize, _, _ := r.GetStat()
  186. glog.Errorf("%s read %d dataSize %d offset %d fileSize %d: %v", r.Name(), n, dataSize, offset, fileSize, err)
  187. }
  188. return dataSlice, err
  189. }
  190. // ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
  191. func (n *Needle) ReadBytes(bytes []byte, offset int64, size Size, version Version) (err error) {
  192. n.ParseNeedleHeader(bytes)
  193. if n.Size != size {
  194. // cookie is not always passed in for this API. Use size to do preliminary checking.
  195. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  196. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatchOffsetSize).Inc()
  197. glog.Errorf("entry not found1: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  198. return ErrorSizeMismatch
  199. }
  200. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorSizeMismatch).Inc()
  201. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  202. }
  203. switch version {
  204. case Version1:
  205. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  206. case Version2, Version3:
  207. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  208. }
  209. if err != nil && err != io.EOF {
  210. return err
  211. }
  212. if size > 0 {
  213. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  214. newChecksum := NewCRC(n.Data)
  215. if checksum != newChecksum.Value() {
  216. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorCRC).Inc()
  217. return errors.New("CRC error! Data On Disk Corrupted")
  218. }
  219. n.Checksum = newChecksum
  220. }
  221. if version == Version3 {
  222. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  223. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  224. }
  225. return nil
  226. }
  227. // ReadData hydrates the needle from the file, with only n.Id is set.
  228. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  229. bytes, err := ReadNeedleBlob(r, offset, size, version)
  230. if err != nil {
  231. return err
  232. }
  233. return n.ReadBytes(bytes, offset, size, version)
  234. }
  235. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  236. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  237. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  238. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  239. }
  240. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  241. index, lenBytes := 0, len(bytes)
  242. if index < lenBytes {
  243. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  244. index = index + 4
  245. if int(n.DataSize)+index > lenBytes {
  246. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  247. return fmt.Errorf("index out of range %d", 1)
  248. }
  249. n.Data = bytes[index : index+int(n.DataSize)]
  250. index = index + int(n.DataSize)
  251. n.Flags = bytes[index]
  252. index = index + 1
  253. }
  254. if index < lenBytes && n.HasName() {
  255. n.NameSize = uint8(bytes[index])
  256. index = index + 1
  257. if int(n.NameSize)+index > lenBytes {
  258. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  259. return fmt.Errorf("index out of range %d", 2)
  260. }
  261. n.Name = bytes[index : index+int(n.NameSize)]
  262. index = index + int(n.NameSize)
  263. }
  264. if index < lenBytes && n.HasMime() {
  265. n.MimeSize = uint8(bytes[index])
  266. index = index + 1
  267. if int(n.MimeSize)+index > lenBytes {
  268. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  269. return fmt.Errorf("index out of range %d", 3)
  270. }
  271. n.Mime = bytes[index : index+int(n.MimeSize)]
  272. index = index + int(n.MimeSize)
  273. }
  274. if index < lenBytes && n.HasLastModifiedDate() {
  275. if LastModifiedBytesLength+index > lenBytes {
  276. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  277. return fmt.Errorf("index out of range %d", 4)
  278. }
  279. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  280. index = index + LastModifiedBytesLength
  281. }
  282. if index < lenBytes && n.HasTtl() {
  283. if TtlBytesLength+index > lenBytes {
  284. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  285. return fmt.Errorf("index out of range %d", 5)
  286. }
  287. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  288. index = index + TtlBytesLength
  289. }
  290. if index < lenBytes && n.HasPairs() {
  291. if 2+index > lenBytes {
  292. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  293. return fmt.Errorf("index out of range %d", 6)
  294. }
  295. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  296. index += 2
  297. if int(n.PairsSize)+index > lenBytes {
  298. stats.VolumeServerRequestCounter.WithLabelValues(stats.ErrorIndexOutOfRange).Inc()
  299. return fmt.Errorf("index out of range %d", 7)
  300. }
  301. end := index + int(n.PairsSize)
  302. n.Pairs = bytes[index:end]
  303. index = end
  304. }
  305. return nil
  306. }
  307. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  308. n = new(Needle)
  309. if version == Version1 || version == Version2 || version == Version3 {
  310. bytes = make([]byte, NeedleHeaderSize)
  311. var count int
  312. count, err = r.ReadAt(bytes, offset)
  313. if count <= 0 || err != nil {
  314. return nil, bytes, 0, err
  315. }
  316. n.ParseNeedleHeader(bytes)
  317. bodyLength = NeedleBodyLength(n.Size, version)
  318. }
  319. return
  320. }
  321. func PaddingLength(needleSize Size, version Version) Size {
  322. if version == Version3 {
  323. // this is same value as version2, but just listed here for clarity
  324. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  325. }
  326. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  327. }
  328. func NeedleBodyLength(needleSize Size, version Version) int64 {
  329. if version == Version3 {
  330. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  331. }
  332. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  333. }
  334. //n should be a needle already read the header
  335. //the input stream will read until next file entry
  336. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  337. if bodyLength <= 0 {
  338. return nil, nil
  339. }
  340. bytes = make([]byte, bodyLength)
  341. if _, err = r.ReadAt(bytes, offset); err != nil {
  342. return
  343. }
  344. err = n.ReadNeedleBodyBytes(bytes, version)
  345. return
  346. }
  347. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  348. if len(needleBody) <= 0 {
  349. return nil
  350. }
  351. switch version {
  352. case Version1:
  353. n.Data = needleBody[:n.Size]
  354. n.Checksum = NewCRC(n.Data)
  355. case Version2, Version3:
  356. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  357. n.Checksum = NewCRC(n.Data)
  358. if version == Version3 {
  359. tsOffset := n.Size + NeedleChecksumSize
  360. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  361. }
  362. default:
  363. err = fmt.Errorf("unsupported version %d!", version)
  364. }
  365. return
  366. }
  367. func (n *Needle) IsCompressed() bool {
  368. return n.Flags&FlagIsCompressed > 0
  369. }
  370. func (n *Needle) SetIsCompressed() {
  371. n.Flags = n.Flags | FlagIsCompressed
  372. }
  373. func (n *Needle) HasName() bool {
  374. return n.Flags&FlagHasName > 0
  375. }
  376. func (n *Needle) SetHasName() {
  377. n.Flags = n.Flags | FlagHasName
  378. }
  379. func (n *Needle) HasMime() bool {
  380. return n.Flags&FlagHasMime > 0
  381. }
  382. func (n *Needle) SetHasMime() {
  383. n.Flags = n.Flags | FlagHasMime
  384. }
  385. func (n *Needle) HasLastModifiedDate() bool {
  386. return n.Flags&FlagHasLastModifiedDate > 0
  387. }
  388. func (n *Needle) SetHasLastModifiedDate() {
  389. n.Flags = n.Flags | FlagHasLastModifiedDate
  390. }
  391. func (n *Needle) HasTtl() bool {
  392. return n.Flags&FlagHasTtl > 0
  393. }
  394. func (n *Needle) SetHasTtl() {
  395. n.Flags = n.Flags | FlagHasTtl
  396. }
  397. func (n *Needle) IsChunkedManifest() bool {
  398. return n.Flags&FlagIsChunkManifest > 0
  399. }
  400. func (n *Needle) SetIsChunkManifest() {
  401. n.Flags = n.Flags | FlagIsChunkManifest
  402. }
  403. func (n *Needle) HasPairs() bool {
  404. return n.Flags&FlagHasPairs != 0
  405. }
  406. func (n *Needle) SetHasPairs() {
  407. n.Flags = n.Flags | FlagHasPairs
  408. }
  409. func GetActualSize(size Size, version Version) int64 {
  410. return NeedleHeaderSize + NeedleBodyLength(size, version)
  411. }