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.

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