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.

212 lines
5.8 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) (offset Offset, 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. n.AppendAtNs = uint64(time.Now().UnixNano())
  85. if offset, size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
  86. return
  87. }
  88. nv, ok := v.nm.Get(n.Id)
  89. if !ok || Offset(nv.Offset)*NeedlePaddingSize < offset {
  90. if err = v.nm.Put(n.Id, offset/NeedlePaddingSize, n.Size); err != nil {
  91. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  92. }
  93. }
  94. if v.lastModifiedTime < n.LastModified {
  95. v.lastModifiedTime = n.LastModified
  96. }
  97. return
  98. }
  99. func (v *Volume) deleteNeedle(n *Needle) (uint32, error) {
  100. glog.V(4).Infof("delete needle %s", NewFileIdFromNeedle(v.Id, n).String())
  101. if v.readOnly {
  102. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  103. }
  104. v.dataFileAccessLock.Lock()
  105. defer v.dataFileAccessLock.Unlock()
  106. nv, ok := v.nm.Get(n.Id)
  107. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  108. if ok && nv.Size != TombstoneFileSize {
  109. size := nv.Size
  110. n.Data = nil
  111. n.AppendAtNs = uint64(time.Now().UnixNano())
  112. offset, _, _, err := n.Append(v.dataFile, v.Version())
  113. if err != nil {
  114. return size, err
  115. }
  116. if err = v.nm.Delete(n.Id, offset/NeedlePaddingSize); err != nil {
  117. return size, err
  118. }
  119. return size, err
  120. }
  121. return 0, nil
  122. }
  123. // read fills in Needle content by looking up n.Id from NeedleMapper
  124. func (v *Volume) readNeedle(n *Needle) (int, error) {
  125. nv, ok := v.nm.Get(n.Id)
  126. if !ok || nv.Offset == 0 {
  127. return -1, errors.New("Not Found")
  128. }
  129. if nv.Size == TombstoneFileSize {
  130. return -1, errors.New("Already Deleted")
  131. }
  132. err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  133. if err != nil {
  134. return 0, err
  135. }
  136. bytesRead := len(n.Data)
  137. if !n.HasTtl() {
  138. return bytesRead, nil
  139. }
  140. ttlMinutes := n.Ttl.Minutes()
  141. if ttlMinutes == 0 {
  142. return bytesRead, nil
  143. }
  144. if !n.HasLastModifiedDate() {
  145. return bytesRead, nil
  146. }
  147. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  148. return bytesRead, nil
  149. }
  150. return -1, errors.New("Not Found")
  151. }
  152. func ScanVolumeFile(dirname string, collection string, id VolumeId,
  153. needleMapKind NeedleMapType,
  154. visitSuperBlock func(SuperBlock) error,
  155. readNeedleBody bool,
  156. visitNeedle func(n *Needle, offset int64) error) (err error) {
  157. var v *Volume
  158. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  159. return fmt.Errorf("Failed to load volume %d: %v", id, err)
  160. }
  161. if err = visitSuperBlock(v.SuperBlock); err != nil {
  162. return fmt.Errorf("Failed to process volume %d super block: %v", id, err)
  163. }
  164. defer v.Close()
  165. version := v.Version()
  166. offset := int64(v.SuperBlock.BlockSize())
  167. n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
  168. if e != nil {
  169. err = fmt.Errorf("cannot read needle header: %v", e)
  170. return
  171. }
  172. for n != nil {
  173. if readNeedleBody {
  174. if err = n.ReadNeedleBody(v.dataFile, version, offset+NeedleEntrySize, rest); err != nil {
  175. glog.V(0).Infof("cannot read needle body: %v", err)
  176. //err = fmt.Errorf("cannot read needle body: %v", err)
  177. //return
  178. }
  179. }
  180. err = visitNeedle(n, offset)
  181. if err == io.EOF {
  182. return nil
  183. }
  184. if err != nil {
  185. glog.V(0).Infof("visit needle error: %v", err)
  186. }
  187. offset += NeedleEntrySize + rest
  188. glog.V(4).Infof("==> new entry offset %d", offset)
  189. if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
  190. if err == io.EOF {
  191. return nil
  192. }
  193. return fmt.Errorf("cannot read needle header: %v", err)
  194. }
  195. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  196. }
  197. return
  198. }