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.

236 lines
6.7 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. )
  11. // isFileUnchanged checks whether this needle to write is same as last one.
  12. // It requires serialized access in the same volume.
  13. func (v *Volume) isFileUnchanged(n *Needle) bool {
  14. if v.Ttl.String() != "" {
  15. return false
  16. }
  17. nv, ok := v.nm.Get(n.Id)
  18. if ok && nv.Offset > 0 {
  19. oldNeedle := new(Needle)
  20. err := oldNeedle.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  21. if err != nil {
  22. glog.V(0).Infof("Failed to check updated file %v", err)
  23. return false
  24. }
  25. defer oldNeedle.ReleaseMemory()
  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. if size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
  97. if e := v.dataFile.Truncate(offset); e != nil {
  98. err = fmt.Errorf("%s\ncannot truncate %s: %v", err, v.dataFile.Name(), e)
  99. }
  100. return
  101. }
  102. nv, ok := v.nm.Get(n.Id)
  103. if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
  104. if err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size); err != nil {
  105. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  106. }
  107. }
  108. if v.lastModifiedTime < n.LastModified {
  109. v.lastModifiedTime = n.LastModified
  110. }
  111. return
  112. }
  113. func (v *Volume) deleteNeedle(n *Needle) (uint32, error) {
  114. glog.V(4).Infof("delete needle %s", NewFileIdFromNeedle(v.Id, n).String())
  115. if v.readOnly {
  116. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  117. }
  118. v.dataFileAccessLock.Lock()
  119. defer v.dataFileAccessLock.Unlock()
  120. nv, ok := v.nm.Get(n.Id)
  121. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  122. if ok && nv.Size != TombstoneFileSize {
  123. size := nv.Size
  124. offset, err := v.dataFile.Seek(0, 2)
  125. if err != nil {
  126. return size, err
  127. }
  128. if err := v.nm.Delete(n.Id, uint32(offset/NeedlePaddingSize)); err != nil {
  129. return size, err
  130. }
  131. n.Data = nil
  132. _, _, err = n.Append(v.dataFile, v.Version())
  133. return size, err
  134. }
  135. return 0, nil
  136. }
  137. // read fills in Needle content by looking up n.Id from NeedleMapper
  138. func (v *Volume) readNeedle(n *Needle) (int, error) {
  139. nv, ok := v.nm.Get(n.Id)
  140. if !ok || nv.Offset == 0 {
  141. return -1, errors.New("Not Found")
  142. }
  143. if nv.Size == TombstoneFileSize {
  144. return -1, errors.New("Already Deleted")
  145. }
  146. err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  147. if err != nil {
  148. return 0, err
  149. }
  150. bytesRead := len(n.Data)
  151. if !n.HasTtl() {
  152. return bytesRead, nil
  153. }
  154. ttlMinutes := n.Ttl.Minutes()
  155. if ttlMinutes == 0 {
  156. return bytesRead, nil
  157. }
  158. if !n.HasLastModifiedDate() {
  159. return bytesRead, nil
  160. }
  161. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  162. return bytesRead, nil
  163. }
  164. n.ReleaseMemory()
  165. return -1, errors.New("Not Found")
  166. }
  167. func ScanVolumeFile(dirname string, collection string, id VolumeId,
  168. needleMapKind NeedleMapType,
  169. visitSuperBlock func(SuperBlock) error,
  170. readNeedleBody bool,
  171. visitNeedle func(n *Needle, offset int64) error) (err error) {
  172. var v *Volume
  173. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  174. return fmt.Errorf("Failed to load volume %d: %v", id, err)
  175. }
  176. if err = visitSuperBlock(v.SuperBlock); err != nil {
  177. return fmt.Errorf("Failed to process volume %d super block: %v", id, err)
  178. }
  179. version := v.Version()
  180. offset := int64(SuperBlockSize)
  181. n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
  182. if e != nil {
  183. err = fmt.Errorf("cannot read needle header: %v", e)
  184. return
  185. }
  186. for n != nil {
  187. if readNeedleBody {
  188. if err = n.ReadNeedleBody(v.dataFile, version, offset+int64(NeedleHeaderSize), rest); err != nil {
  189. glog.V(0).Infof("cannot read needle body: %v", err)
  190. //err = fmt.Errorf("cannot read needle body: %v", err)
  191. //return
  192. }
  193. if n.DataSize >= n.Size {
  194. // this should come from a bug reported on #87 and #93
  195. // fixed in v0.69
  196. // remove this whole "if" clause later, long after 0.69
  197. oldRest, oldSize := rest, n.Size
  198. padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
  199. n.Size = 0
  200. rest = n.Size + NeedleChecksumSize + padding
  201. if rest%NeedlePaddingSize != 0 {
  202. rest += (NeedlePaddingSize - rest%NeedlePaddingSize)
  203. }
  204. glog.V(4).Infof("Adjusting n.Size %d=>0 rest:%d=>%d %+v", oldSize, oldRest, rest, n)
  205. }
  206. }
  207. if err = visitNeedle(n, offset); err != nil {
  208. glog.V(0).Infof("visit needle error: %v", err)
  209. }
  210. offset += int64(NeedleHeaderSize) + int64(rest)
  211. glog.V(4).Infof("==> new entry offset %d", offset)
  212. if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
  213. if err == io.EOF {
  214. return nil
  215. }
  216. return fmt.Errorf("cannot read needle header: %v", err)
  217. }
  218. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  219. }
  220. return
  221. }