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.

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