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.

265 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.isCompacting {
  38. err = fmt.Errorf("volume %d is compacting", v.Id)
  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. v.dataFileAccessLock.Lock()
  126. defer v.dataFileAccessLock.Unlock()
  127. nv, ok := v.nm.Get(n.Id)
  128. if !ok || nv.Offset.IsZero() {
  129. return -1, ErrorNotFound
  130. }
  131. if nv.Size == TombstoneFileSize {
  132. return -1, errors.New("already deleted")
  133. }
  134. if nv.Size == 0 {
  135. return 0, nil
  136. }
  137. err := n.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  138. if err != nil {
  139. return 0, err
  140. }
  141. bytesRead := len(n.Data)
  142. if !n.HasTtl() {
  143. return bytesRead, nil
  144. }
  145. ttlMinutes := n.Ttl.Minutes()
  146. if ttlMinutes == 0 {
  147. return bytesRead, nil
  148. }
  149. if !n.HasLastModifiedDate() {
  150. return bytesRead, nil
  151. }
  152. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  153. return bytesRead, nil
  154. }
  155. return -1, ErrorNotFound
  156. }
  157. type VolumeFileScanner interface {
  158. VisitSuperBlock(SuperBlock) error
  159. ReadNeedleBody() bool
  160. VisitNeedle(n *needle.Needle, offset int64) error
  161. }
  162. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  163. needleMapKind NeedleMapType,
  164. volumeFileScanner VolumeFileScanner) (err error) {
  165. var v *Volume
  166. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  167. return fmt.Errorf("failed to load volume %d: %v", id, err)
  168. }
  169. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  170. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  171. }
  172. defer v.Close()
  173. version := v.Version()
  174. offset := int64(v.SuperBlock.BlockSize())
  175. return ScanVolumeFileFrom(version, v.dataFile, offset, volumeFileScanner)
  176. }
  177. func ScanVolumeFileFrom(version needle.Version, dataFile *os.File, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  178. n, _, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  179. if e != nil {
  180. if e == io.EOF {
  181. return nil
  182. }
  183. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  184. }
  185. for n != nil {
  186. if volumeFileScanner.ReadNeedleBody() {
  187. if _, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  188. glog.V(0).Infof("cannot read needle body: %v", err)
  189. //err = fmt.Errorf("cannot read needle body: %v", err)
  190. //return
  191. }
  192. }
  193. err := volumeFileScanner.VisitNeedle(n, offset)
  194. if err == io.EOF {
  195. return nil
  196. }
  197. if err != nil {
  198. glog.V(0).Infof("visit needle error: %v", err)
  199. return fmt.Errorf("visit needle error: %v", err)
  200. }
  201. offset += NeedleHeaderSize + rest
  202. glog.V(4).Infof("==> new entry offset %d", offset)
  203. if n, _, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  204. if err == io.EOF {
  205. return nil
  206. }
  207. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  208. }
  209. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  210. }
  211. return nil
  212. }
  213. func ScanVolumeFileNeedleFrom(version needle.Version, dataFile *os.File, offset int64, fn func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error) (err error) {
  214. n, nh, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  215. if e != nil {
  216. if e == io.EOF {
  217. return nil
  218. }
  219. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  220. }
  221. for n != nil {
  222. var needleBody []byte
  223. if needleBody, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  224. glog.V(0).Infof("cannot read needle body: %v", err)
  225. //err = fmt.Errorf("cannot read needle body: %v", err)
  226. //return
  227. }
  228. err = fn(nh, needleBody, n.AppendAtNs)
  229. if err != nil {
  230. glog.V(0).Infof("visit needle error: %v", err)
  231. return
  232. }
  233. offset += NeedleHeaderSize + rest
  234. glog.V(4).Infof("==> new entry offset %d", offset)
  235. if n, nh, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  236. if err == io.EOF {
  237. return nil
  238. }
  239. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  240. }
  241. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  242. }
  243. return nil
  244. }