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.

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