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

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