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.

228 lines
6.3 KiB

  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/types"
  11. )
  12. // isFileUnchanged checks whether this needle to write is same as last one.
  13. // It requires serialized access in the same volume.
  14. func (v *Volume) isFileUnchanged(n *Needle) bool {
  15. if v.Ttl.String() != "" {
  16. return false
  17. }
  18. nv, ok := v.nm.Get(n.Id)
  19. if ok && nv.Offset > 0 {
  20. oldNeedle := new(Needle)
  21. err := oldNeedle.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  22. if err != nil {
  23. glog.V(0).Infof("Failed to check updated file %v", err)
  24. return false
  25. }
  26. if oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
  27. n.DataSize = oldNeedle.DataSize
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. // Destroy removes everything related to this volume
  34. func (v *Volume) Destroy() (err error) {
  35. if v.readOnly {
  36. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  37. return
  38. }
  39. v.Close()
  40. err = os.Remove(v.dataFile.Name())
  41. if err != nil {
  42. return
  43. }
  44. err = v.nm.Destroy()
  45. return
  46. }
  47. // AppendBlob append a blob to end of the data file, used in replication
  48. func (v *Volume) AppendBlob(b []byte) (offset int64, err error) {
  49. if v.readOnly {
  50. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  51. return
  52. }
  53. v.dataFileAccessLock.Lock()
  54. defer v.dataFileAccessLock.Unlock()
  55. if offset, err = v.dataFile.Seek(0, 2); err != nil {
  56. glog.V(0).Infof("failed to seek the end of file: %v", err)
  57. return
  58. }
  59. //ensure file writing starting from aligned positions
  60. if offset%NeedlePaddingSize != 0 {
  61. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  62. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  63. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  64. return
  65. }
  66. }
  67. _, err = v.dataFile.Write(b)
  68. return
  69. }
  70. func (v *Volume) writeNeedle(n *Needle) (size uint32, err error) {
  71. glog.V(4).Infof("writing needle %s", NewFileIdFromNeedle(v.Id, n).String())
  72. if v.readOnly {
  73. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  74. return
  75. }
  76. v.dataFileAccessLock.Lock()
  77. defer v.dataFileAccessLock.Unlock()
  78. if v.isFileUnchanged(n) {
  79. size = n.DataSize
  80. glog.V(4).Infof("needle is unchanged!")
  81. return
  82. }
  83. var offset int64
  84. if offset, err = v.dataFile.Seek(0, 2); err != nil {
  85. glog.V(0).Infof("failed to seek the end of file: %v", err)
  86. return
  87. }
  88. //ensure file writing starting from aligned positions
  89. if offset%NeedlePaddingSize != 0 {
  90. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  91. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  92. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  93. return
  94. }
  95. }
  96. n.AppendAtNs = uint64(time.Now().UnixNano())
  97. if size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
  98. if e := v.dataFile.Truncate(offset); e != nil {
  99. err = fmt.Errorf("%s\ncannot truncate %s: %v", err, v.dataFile.Name(), e)
  100. }
  101. return
  102. }
  103. nv, ok := v.nm.Get(n.Id)
  104. if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
  105. if err = v.nm.Put(n.Id, Offset(offset/NeedlePaddingSize), n.Size); err != nil {
  106. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  107. }
  108. }
  109. if v.lastModifiedTime < n.LastModified {
  110. v.lastModifiedTime = n.LastModified
  111. }
  112. return
  113. }
  114. func (v *Volume) deleteNeedle(n *Needle) (uint32, error) {
  115. glog.V(4).Infof("delete needle %s", NewFileIdFromNeedle(v.Id, n).String())
  116. if v.readOnly {
  117. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  118. }
  119. v.dataFileAccessLock.Lock()
  120. defer v.dataFileAccessLock.Unlock()
  121. nv, ok := v.nm.Get(n.Id)
  122. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  123. if ok && nv.Size != TombstoneFileSize {
  124. size := nv.Size
  125. offset, err := v.dataFile.Seek(0, 2)
  126. if err != nil {
  127. return size, err
  128. }
  129. if err := v.nm.Delete(n.Id, Offset(offset/NeedlePaddingSize)); err != nil {
  130. return size, err
  131. }
  132. n.Data = nil
  133. n.AppendAtNs = uint64(time.Now().UnixNano())
  134. _, _, err = n.Append(v.dataFile, v.Version())
  135. return size, err
  136. }
  137. return 0, nil
  138. }
  139. // read fills in Needle content by looking up n.Id from NeedleMapper
  140. func (v *Volume) readNeedle(n *Needle) (int, error) {
  141. nv, ok := v.nm.Get(n.Id)
  142. if !ok || nv.Offset == 0 {
  143. return -1, errors.New("Not Found")
  144. }
  145. if nv.Size == TombstoneFileSize {
  146. return -1, errors.New("Already Deleted")
  147. }
  148. err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  149. if err != nil {
  150. return 0, err
  151. }
  152. bytesRead := len(n.Data)
  153. if !n.HasTtl() {
  154. return bytesRead, nil
  155. }
  156. ttlMinutes := n.Ttl.Minutes()
  157. if ttlMinutes == 0 {
  158. return bytesRead, nil
  159. }
  160. if !n.HasLastModifiedDate() {
  161. return bytesRead, nil
  162. }
  163. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  164. return bytesRead, nil
  165. }
  166. return -1, errors.New("Not Found")
  167. }
  168. func ScanVolumeFile(dirname string, collection string, id VolumeId,
  169. needleMapKind NeedleMapType,
  170. visitSuperBlock func(SuperBlock) error,
  171. readNeedleBody bool,
  172. visitNeedle func(n *Needle, offset int64) error) (err error) {
  173. var v *Volume
  174. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  175. return fmt.Errorf("Failed to load volume %d: %v", id, err)
  176. }
  177. if err = visitSuperBlock(v.SuperBlock); err != nil {
  178. return fmt.Errorf("Failed to process volume %d super block: %v", id, err)
  179. }
  180. version := v.Version()
  181. offset := int64(v.SuperBlock.BlockSize())
  182. n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
  183. if e != nil {
  184. err = fmt.Errorf("cannot read needle header: %v", e)
  185. return
  186. }
  187. for n != nil {
  188. if readNeedleBody {
  189. if err = n.ReadNeedleBody(v.dataFile, version, offset+NeedleEntrySize, rest); err != nil {
  190. glog.V(0).Infof("cannot read needle body: %v", err)
  191. //err = fmt.Errorf("cannot read needle body: %v", err)
  192. //return
  193. }
  194. }
  195. err = visitNeedle(n, offset)
  196. if err == io.EOF {
  197. return nil
  198. }
  199. if err != nil {
  200. glog.V(0).Infof("visit needle error: %v", err)
  201. }
  202. offset += NeedleEntrySize + rest
  203. glog.V(4).Infof("==> new entry offset %d", offset)
  204. if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
  205. if err == io.EOF {
  206. return nil
  207. }
  208. return fmt.Errorf("cannot read needle header: %v", err)
  209. }
  210. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  211. }
  212. return
  213. }