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.

267 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. 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. // AppendBlob append a blob to end of the data file, used in replication
  51. func (v *Volume) AppendBlob(b []byte) (offset int64, err error) {
  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 offset, err = v.dataFile.Seek(0, 2); err != nil {
  59. glog.V(0).Infof("failed to seek the end of file: %v", err)
  60. return
  61. }
  62. //ensure file writing starting from aligned positions
  63. if offset%NeedlePaddingSize != 0 {
  64. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  65. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  66. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  67. return
  68. }
  69. }
  70. _, err = v.dataFile.Write(b)
  71. return
  72. }
  73. func (v *Volume) writeNeedle(n *needle.Needle) (offset uint64, size uint32, isUnchanged bool, err error) {
  74. glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  75. if v.readOnly {
  76. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  77. return
  78. }
  79. v.dataFileAccessLock.Lock()
  80. defer v.dataFileAccessLock.Unlock()
  81. if v.isFileUnchanged(n) {
  82. size = n.DataSize
  83. isUnchanged = true
  84. return
  85. }
  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. nv, ok := v.nm.Get(n.Id)
  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. nv, ok := v.nm.Get(n.Id)
  130. if !ok || nv.Offset.IsZero() {
  131. v.compactingWg.Wait()
  132. nv, ok = v.nm.Get(n.Id)
  133. if !ok || nv.Offset.IsZero() {
  134. return -1, ErrorNotFound
  135. }
  136. }
  137. if nv.Size == TombstoneFileSize {
  138. return -1, errors.New("already deleted")
  139. }
  140. if nv.Size == 0 {
  141. return 0, nil
  142. }
  143. err := n.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  144. if err != nil {
  145. return 0, err
  146. }
  147. bytesRead := len(n.Data)
  148. if !n.HasTtl() {
  149. return bytesRead, nil
  150. }
  151. ttlMinutes := n.Ttl.Minutes()
  152. if ttlMinutes == 0 {
  153. return bytesRead, nil
  154. }
  155. if !n.HasLastModifiedDate() {
  156. return bytesRead, nil
  157. }
  158. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  159. return bytesRead, nil
  160. }
  161. return -1, ErrorNotFound
  162. }
  163. type VolumeFileScanner interface {
  164. VisitSuperBlock(SuperBlock) error
  165. ReadNeedleBody() bool
  166. VisitNeedle(n *needle.Needle, offset int64) error
  167. }
  168. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  169. needleMapKind NeedleMapType,
  170. volumeFileScanner VolumeFileScanner) (err error) {
  171. var v *Volume
  172. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  173. return fmt.Errorf("failed to load volume %d: %v", id, err)
  174. }
  175. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  176. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  177. }
  178. defer v.Close()
  179. version := v.Version()
  180. offset := int64(v.SuperBlock.BlockSize())
  181. return ScanVolumeFileFrom(version, v.dataFile, offset, volumeFileScanner)
  182. }
  183. func ScanVolumeFileFrom(version needle.Version, dataFile *os.File, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  184. n, _, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  185. if e != nil {
  186. if e == io.EOF {
  187. return nil
  188. }
  189. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  190. }
  191. for n != nil {
  192. if volumeFileScanner.ReadNeedleBody() {
  193. if _, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  194. glog.V(0).Infof("cannot read needle body: %v", err)
  195. //err = fmt.Errorf("cannot read needle body: %v", err)
  196. //return
  197. }
  198. }
  199. err := volumeFileScanner.VisitNeedle(n, offset)
  200. if err == io.EOF {
  201. return nil
  202. }
  203. if err != nil {
  204. glog.V(0).Infof("visit needle error: %v", err)
  205. }
  206. offset += NeedleHeaderSize + rest
  207. glog.V(4).Infof("==> new entry offset %d", offset)
  208. if n, _, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  209. if err == io.EOF {
  210. return nil
  211. }
  212. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  213. }
  214. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  215. }
  216. return nil
  217. }
  218. func ScanVolumeFileNeedleFrom(version needle.Version, dataFile *os.File, offset int64, fn func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error) (err error) {
  219. n, nh, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  220. if e != nil {
  221. if e == io.EOF {
  222. return nil
  223. }
  224. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  225. }
  226. for n != nil {
  227. var needleBody []byte
  228. if needleBody, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  229. glog.V(0).Infof("cannot read needle body: %v", err)
  230. //err = fmt.Errorf("cannot read needle body: %v", err)
  231. //return
  232. }
  233. err = fn(nh, needleBody, n.AppendAtNs)
  234. if err != nil {
  235. glog.V(0).Infof("visit needle error: %v", err)
  236. return
  237. }
  238. offset += NeedleHeaderSize + rest
  239. glog.V(4).Infof("==> new entry offset %d", offset)
  240. if n, nh, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  241. if err == io.EOF {
  242. return nil
  243. }
  244. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  245. }
  246. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  247. }
  248. return nil
  249. }