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.

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