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.

235 lines
6.8 KiB

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