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.

234 lines
6.8 KiB

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