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.

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