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.

272 lines
7.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  12. )
  13. var ErrorNotFound = errors.New("not found")
  14. // isFileUnchanged checks whether this needle to write is same as last one.
  15. // It requires serialized access in the same volume.
  16. func (v *Volume) isFileUnchanged(n *needle.Needle) bool {
  17. if v.Ttl.String() != "" {
  18. return false
  19. }
  20. nv, ok := v.nm.Get(n.Id)
  21. if ok && !nv.Offset.IsZero() && nv.Size != TombstoneFileSize {
  22. oldNeedle := new(needle.Needle)
  23. err := oldNeedle.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  24. if err != nil {
  25. glog.V(0).Infof("Failed to check updated file at offset %d size %d: %v", nv.Offset.ToAcutalOffset(), nv.Size, err)
  26. return false
  27. }
  28. if oldNeedle.Cookie == n.Cookie && oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
  29. n.DataSize = oldNeedle.DataSize
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. // Destroy removes everything related to this volume
  36. func (v *Volume) Destroy() (err error) {
  37. if v.readOnly {
  38. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  39. return
  40. }
  41. v.Close()
  42. os.Remove(v.FileName() + ".dat")
  43. os.Remove(v.FileName() + ".idx")
  44. os.Remove(v.FileName() + ".cpd")
  45. os.Remove(v.FileName() + ".cpx")
  46. os.Remove(v.FileName() + ".ldb")
  47. os.Remove(v.FileName() + ".bdb")
  48. return
  49. }
  50. // AppendBlob append a blob to end of the data file, used in replication
  51. func (v *Volume) AppendBlob(b []byte) (offset int64, err error) {
  52. if v.readOnly {
  53. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  54. return
  55. }
  56. v.dataFileAccessLock.Lock()
  57. defer v.dataFileAccessLock.Unlock()
  58. if offset, err = v.dataFile.Seek(0, 2); err != nil {
  59. glog.V(0).Infof("failed to seek the end of file: %v", err)
  60. return
  61. }
  62. //ensure file writing starting from aligned positions
  63. if offset%NeedlePaddingSize != 0 {
  64. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  65. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  66. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  67. return
  68. }
  69. }
  70. _, err = v.dataFile.Write(b)
  71. return
  72. }
  73. func (v *Volume) writeNeedle(n *needle.Needle) (offset uint64, size uint32, isUnchanged bool, err error) {
  74. glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  75. if v.readOnly {
  76. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  77. return
  78. }
  79. v.dataFileAccessLock.Lock()
  80. defer v.dataFileAccessLock.Unlock()
  81. if v.isFileUnchanged(n) {
  82. size = n.DataSize
  83. isUnchanged = true
  84. return
  85. }
  86. if n.Ttl == needle.EMPTY_TTL && v.Ttl != needle.EMPTY_TTL {
  87. n.SetHasTtl()
  88. n.Ttl = v.Ttl
  89. }
  90. n.AppendAtNs = uint64(time.Now().UnixNano())
  91. if offset, size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
  92. return
  93. }
  94. v.lastAppendAtNs = n.AppendAtNs
  95. nv, ok := v.nm.Get(n.Id)
  96. if !ok || uint64(nv.Offset.ToAcutalOffset()) < offset {
  97. if err = v.nm.Put(n.Id, ToOffset(int64(offset)), n.Size); err != nil {
  98. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  99. }
  100. }
  101. if v.lastModifiedTsSeconds < n.LastModified {
  102. v.lastModifiedTsSeconds = n.LastModified
  103. }
  104. return
  105. }
  106. func (v *Volume) deleteNeedle(n *needle.Needle) (uint32, error) {
  107. glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  108. if v.readOnly {
  109. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  110. }
  111. v.dataFileAccessLock.Lock()
  112. defer v.dataFileAccessLock.Unlock()
  113. nv, ok := v.nm.Get(n.Id)
  114. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  115. if ok && nv.Size != TombstoneFileSize {
  116. size := nv.Size
  117. n.Data = nil
  118. n.AppendAtNs = uint64(time.Now().UnixNano())
  119. offset, _, _, err := n.Append(v.dataFile, v.Version())
  120. if err != nil {
  121. return size, err
  122. }
  123. v.lastAppendAtNs = n.AppendAtNs
  124. if err = v.nm.Delete(n.Id, ToOffset(int64(offset))); err != nil {
  125. return size, err
  126. }
  127. return size, err
  128. }
  129. return 0, nil
  130. }
  131. // read fills in Needle content by looking up n.Id from NeedleMapper
  132. func (v *Volume) readNeedle(n *needle.Needle) (int, error) {
  133. nv, ok := v.nm.Get(n.Id)
  134. if !ok || nv.Offset.IsZero() {
  135. v.compactingWg.Wait()
  136. nv, ok = v.nm.Get(n.Id)
  137. if !ok || nv.Offset.IsZero() {
  138. return -1, ErrorNotFound
  139. }
  140. }
  141. if nv.Size == TombstoneFileSize {
  142. return -1, errors.New("already deleted")
  143. }
  144. if nv.Size == 0 {
  145. return 0, nil
  146. }
  147. err := n.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  148. if err != nil {
  149. return 0, err
  150. }
  151. bytesRead := len(n.Data)
  152. if !n.HasTtl() {
  153. return bytesRead, nil
  154. }
  155. ttlMinutes := n.Ttl.Minutes()
  156. if ttlMinutes == 0 {
  157. return bytesRead, nil
  158. }
  159. if !n.HasLastModifiedDate() {
  160. return bytesRead, nil
  161. }
  162. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  163. return bytesRead, nil
  164. }
  165. return -1, ErrorNotFound
  166. }
  167. type VolumeFileScanner interface {
  168. VisitSuperBlock(SuperBlock) error
  169. ReadNeedleBody() bool
  170. VisitNeedle(n *needle.Needle, offset int64) error
  171. }
  172. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  173. needleMapKind NeedleMapType,
  174. volumeFileScanner VolumeFileScanner) (err error) {
  175. var v *Volume
  176. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  177. return fmt.Errorf("failed to load volume %d: %v", id, err)
  178. }
  179. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  180. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  181. }
  182. defer v.Close()
  183. version := v.Version()
  184. offset := int64(v.SuperBlock.BlockSize())
  185. return ScanVolumeFileFrom(version, v.dataFile, offset, volumeFileScanner)
  186. }
  187. func ScanVolumeFileFrom(version needle.Version, dataFile *os.File, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  188. n, _, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  189. if e != nil {
  190. if e == io.EOF {
  191. return nil
  192. }
  193. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  194. }
  195. for n != nil {
  196. if volumeFileScanner.ReadNeedleBody() {
  197. if _, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  198. glog.V(0).Infof("cannot read needle body: %v", err)
  199. //err = fmt.Errorf("cannot read needle body: %v", err)
  200. //return
  201. }
  202. }
  203. err := volumeFileScanner.VisitNeedle(n, offset)
  204. if err == io.EOF {
  205. return nil
  206. }
  207. if err != nil {
  208. glog.V(0).Infof("visit needle error: %v", err)
  209. }
  210. offset += NeedleHeaderSize + rest
  211. glog.V(4).Infof("==> new entry offset %d", offset)
  212. if n, _, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  213. if err == io.EOF {
  214. return nil
  215. }
  216. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  217. }
  218. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  219. }
  220. return nil
  221. }
  222. func ScanVolumeFileNeedleFrom(version needle.Version, dataFile *os.File, offset int64, fn func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error) (err error) {
  223. n, nh, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  224. if e != nil {
  225. if e == io.EOF {
  226. return nil
  227. }
  228. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  229. }
  230. for n != nil {
  231. var needleBody []byte
  232. if needleBody, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  233. glog.V(0).Infof("cannot read needle body: %v", err)
  234. //err = fmt.Errorf("cannot read needle body: %v", err)
  235. //return
  236. }
  237. err = fn(nh, needleBody, n.AppendAtNs)
  238. if err != nil {
  239. glog.V(0).Infof("visit needle error: %v", err)
  240. return
  241. }
  242. offset += NeedleHeaderSize + rest
  243. glog.V(4).Infof("==> new entry offset %d", offset)
  244. if n, nh, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  245. if err == io.EOF {
  246. return nil
  247. }
  248. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  249. }
  250. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  251. }
  252. return nil
  253. }