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.

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