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.

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