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.

228 lines
6.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 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/backend"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  13. )
  14. var ErrorNotFound = errors.New("not found")
  15. // isFileUnchanged checks whether this needle to write is same as last one.
  16. // It requires serialized access in the same volume.
  17. func (v *Volume) isFileUnchanged(n *needle.Needle) bool {
  18. if v.Ttl.String() != "" {
  19. return false
  20. }
  21. nv, ok := v.nm.Get(n.Id)
  22. if ok && !nv.Offset.IsZero() && nv.Size != TombstoneFileSize {
  23. oldNeedle := new(needle.Needle)
  24. err := oldNeedle.ReadData(v.DataBackend, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  25. if err != nil {
  26. glog.V(0).Infof("Failed to check updated file at offset %d size %d: %v", nv.Offset.ToAcutalOffset(), nv.Size, err)
  27. return false
  28. }
  29. if oldNeedle.Cookie == n.Cookie && oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
  30. n.DataSize = oldNeedle.DataSize
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. // Destroy removes everything related to this volume
  37. func (v *Volume) Destroy() (err error) {
  38. if v.isCompacting {
  39. err = fmt.Errorf("volume %d is compacting", v.Id)
  40. return
  41. }
  42. v.Close()
  43. os.Remove(v.FileName() + ".dat")
  44. os.Remove(v.FileName() + ".idx")
  45. os.Remove(v.FileName() + ".tier")
  46. os.Remove(v.FileName() + ".sdb")
  47. os.Remove(v.FileName() + ".cpd")
  48. os.Remove(v.FileName() + ".cpx")
  49. os.RemoveAll(v.FileName() + ".ldb")
  50. return
  51. }
  52. func (v *Volume) writeNeedle(n *needle.Needle) (offset uint64, size uint32, isUnchanged bool, err error) {
  53. glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  54. v.dataFileAccessLock.Lock()
  55. defer v.dataFileAccessLock.Unlock()
  56. if v.isFileUnchanged(n) {
  57. size = n.DataSize
  58. isUnchanged = true
  59. return
  60. }
  61. if n.Ttl == needle.EMPTY_TTL && v.Ttl != needle.EMPTY_TTL {
  62. n.SetHasTtl()
  63. n.Ttl = v.Ttl
  64. }
  65. // check whether existing needle cookie matches
  66. nv, ok := v.nm.Get(n.Id)
  67. if ok {
  68. existingNeedle, _, _, existingNeedleReadErr := needle.ReadNeedleHeader(v.DataBackend, v.Version(), nv.Offset.ToAcutalOffset())
  69. if existingNeedleReadErr != nil {
  70. err = fmt.Errorf("reading existing needle: %v", existingNeedleReadErr)
  71. return
  72. }
  73. if existingNeedle.Cookie != n.Cookie {
  74. glog.V(0).Infof("write cookie mismatch: existing %x, new %x", existingNeedle.Cookie, n.Cookie)
  75. err = fmt.Errorf("mismatching cookie %x", n.Cookie)
  76. return
  77. }
  78. }
  79. // append to dat file
  80. n.AppendAtNs = uint64(time.Now().UnixNano())
  81. if offset, size, _, err = n.Append(v.DataBackend, v.Version()); err != nil {
  82. return
  83. }
  84. v.lastAppendAtNs = n.AppendAtNs
  85. // add to needle map
  86. if !ok || uint64(nv.Offset.ToAcutalOffset()) < offset {
  87. if err = v.nm.Put(n.Id, ToOffset(int64(offset)), n.Size); err != nil {
  88. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  89. }
  90. }
  91. if v.lastModifiedTsSeconds < n.LastModified {
  92. v.lastModifiedTsSeconds = n.LastModified
  93. }
  94. return
  95. }
  96. func (v *Volume) deleteNeedle(n *needle.Needle) (uint32, error) {
  97. glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  98. v.dataFileAccessLock.Lock()
  99. defer v.dataFileAccessLock.Unlock()
  100. nv, ok := v.nm.Get(n.Id)
  101. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  102. if ok && nv.Size != TombstoneFileSize {
  103. size := nv.Size
  104. n.Data = nil
  105. n.AppendAtNs = uint64(time.Now().UnixNano())
  106. offset, _, _, err := n.Append(v.DataBackend, v.Version())
  107. if err != nil {
  108. return size, err
  109. }
  110. v.lastAppendAtNs = n.AppendAtNs
  111. if err = v.nm.Delete(n.Id, ToOffset(int64(offset))); err != nil {
  112. return size, err
  113. }
  114. return size, err
  115. }
  116. return 0, nil
  117. }
  118. // read fills in Needle content by looking up n.Id from NeedleMapper
  119. func (v *Volume) readNeedle(n *needle.Needle) (int, error) {
  120. v.dataFileAccessLock.RLock()
  121. defer v.dataFileAccessLock.RUnlock()
  122. nv, ok := v.nm.Get(n.Id)
  123. if !ok || nv.Offset.IsZero() {
  124. return -1, ErrorNotFound
  125. }
  126. if nv.Size == TombstoneFileSize {
  127. return -1, errors.New("already deleted")
  128. }
  129. if nv.Size == 0 {
  130. return 0, nil
  131. }
  132. err := n.ReadData(v.DataBackend, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  133. if err != nil {
  134. return 0, err
  135. }
  136. bytesRead := len(n.Data)
  137. if !n.HasTtl() {
  138. return bytesRead, nil
  139. }
  140. ttlMinutes := n.Ttl.Minutes()
  141. if ttlMinutes == 0 {
  142. return bytesRead, nil
  143. }
  144. if !n.HasLastModifiedDate() {
  145. return bytesRead, nil
  146. }
  147. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  148. return bytesRead, nil
  149. }
  150. return -1, ErrorNotFound
  151. }
  152. type VolumeFileScanner interface {
  153. VisitSuperBlock(SuperBlock) error
  154. ReadNeedleBody() bool
  155. VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error
  156. }
  157. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  158. needleMapKind NeedleMapType,
  159. volumeFileScanner VolumeFileScanner) (err error) {
  160. var v *Volume
  161. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  162. return fmt.Errorf("failed to load volume %d: %v", id, err)
  163. }
  164. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  165. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  166. }
  167. defer v.Close()
  168. version := v.Version()
  169. offset := int64(v.SuperBlock.BlockSize())
  170. return ScanVolumeFileFrom(version, v.DataBackend, offset, volumeFileScanner)
  171. }
  172. func ScanVolumeFileFrom(version needle.Version, datBackend backend.BackendStorageFile, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  173. n, nh, rest, e := needle.ReadNeedleHeader(datBackend, version, offset)
  174. if e != nil {
  175. if e == io.EOF {
  176. return nil
  177. }
  178. return fmt.Errorf("cannot read %s at offset %d: %v", datBackend.Name(), offset, e)
  179. }
  180. for n != nil {
  181. var needleBody []byte
  182. if volumeFileScanner.ReadNeedleBody() {
  183. if needleBody, err = n.ReadNeedleBody(datBackend, version, offset+NeedleHeaderSize, rest); err != nil {
  184. glog.V(0).Infof("cannot read needle body: %v", err)
  185. //err = fmt.Errorf("cannot read needle body: %v", err)
  186. //return
  187. }
  188. }
  189. err := volumeFileScanner.VisitNeedle(n, offset, nh, needleBody)
  190. if err == io.EOF {
  191. return nil
  192. }
  193. if err != nil {
  194. glog.V(0).Infof("visit needle error: %v", err)
  195. return fmt.Errorf("visit needle error: %v", err)
  196. }
  197. offset += NeedleHeaderSize + rest
  198. glog.V(4).Infof("==> new entry offset %d", offset)
  199. if n, nh, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
  200. if err == io.EOF {
  201. return nil
  202. }
  203. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  204. }
  205. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  206. }
  207. return nil
  208. }