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.

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