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.

247 lines
7.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. } else if offset != int64(v.dataFileSize) {
  59. glog.V(0).Infof("dataFileSize %d != actual data file size: %d", v.dataFileSize, offset)
  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. } else if offset != int64(v.dataFileSize) {
  68. glog.V(0).Infof("dataFileSize %d != actual data file size: %d", v.dataFileSize, offset)
  69. }
  70. }
  71. _, err = v.dataFile.Write(b)
  72. v.dataFileSize += int64(len(b))
  73. return
  74. }
  75. func (v *Volume) writeNeedle(n *Needle) (size uint32, err error) {
  76. glog.V(4).Infof("writing needle %s", NewFileIdFromNeedle(v.Id, n).String())
  77. if v.readOnly {
  78. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  79. return
  80. }
  81. v.dataFileAccessLock.Lock()
  82. defer v.dataFileAccessLock.Unlock()
  83. if v.isFileUnchanged(n) {
  84. size = n.DataSize
  85. glog.V(4).Infof("needle is unchanged!")
  86. return
  87. }
  88. var offset, actualSize int64
  89. if offset, err = v.dataFile.Seek(0, 2); err != nil {
  90. glog.V(0).Infof("failed to seek the end of file: %v", err)
  91. return
  92. } else if offset != int64(v.dataFileSize) {
  93. glog.V(0).Infof("dataFileSize %d != actual data file size: %d", v.dataFileSize, offset)
  94. }
  95. //ensure file writing starting from aligned positions
  96. if offset%NeedlePaddingSize != 0 {
  97. offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
  98. if offset, err = v.dataFile.Seek(offset, 0); err != nil {
  99. glog.V(0).Infof("failed to align in datafile %s: %v", v.dataFile.Name(), err)
  100. return
  101. }
  102. }
  103. if size, actualSize, err = n.Append(v.dataFile, v.Version()); err != nil {
  104. if e := v.dataFile.Truncate(offset); e != nil {
  105. err = fmt.Errorf("%s\ncannot truncate %s: %v", err, v.dataFile.Name(), e)
  106. }
  107. return
  108. }
  109. v.dataFileSize += actualSize
  110. nv, ok := v.nm.Get(n.Id)
  111. if !ok || int64(nv.Offset)*NeedlePaddingSize < offset {
  112. if err = v.nm.Put(n.Id, uint32(offset/NeedlePaddingSize), n.Size); err != nil {
  113. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  114. }
  115. }
  116. if v.lastModifiedTime < n.LastModified {
  117. v.lastModifiedTime = n.LastModified
  118. }
  119. return
  120. }
  121. func (v *Volume) deleteNeedle(n *Needle) (uint32, error) {
  122. glog.V(4).Infof("delete needle %s", NewFileIdFromNeedle(v.Id, n).String())
  123. if v.readOnly {
  124. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  125. }
  126. v.dataFileAccessLock.Lock()
  127. defer v.dataFileAccessLock.Unlock()
  128. nv, ok := v.nm.Get(n.Id)
  129. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  130. if ok && nv.Size != TombstoneFileSize {
  131. size := nv.Size
  132. // println("adding tombstone", n.Id, "at offset", v.dataFileSize)
  133. if err := v.nm.Delete(n.Id, uint32(v.dataFileSize/NeedlePaddingSize)); err != nil {
  134. return size, err
  135. }
  136. if offset, err := v.dataFile.Seek(0, 2); err != nil {
  137. return size, err
  138. } else if offset != int64(v.dataFileSize) {
  139. glog.V(0).Infof("dataFileSize %d != actual data file size: %d, deleteMarker: %d", v.dataFileSize, offset, getActualSize(0))
  140. }
  141. n.Data = nil
  142. _, actualSize, err := n.Append(v.dataFile, v.Version())
  143. v.dataFileSize += actualSize
  144. return size, err
  145. }
  146. return 0, nil
  147. }
  148. // read fills in Needle content by looking up n.Id from NeedleMapper
  149. func (v *Volume) readNeedle(n *Needle) (int, error) {
  150. nv, ok := v.nm.Get(n.Id)
  151. if !ok || nv.Offset == 0 {
  152. return -1, errors.New("Not Found")
  153. }
  154. if nv.Size == TombstoneFileSize {
  155. return -1, errors.New("Already Deleted")
  156. }
  157. err := n.ReadData(v.dataFile, int64(nv.Offset)*NeedlePaddingSize, nv.Size, v.Version())
  158. if err != nil {
  159. return 0, err
  160. }
  161. bytesRead := len(n.Data)
  162. if !n.HasTtl() {
  163. return bytesRead, nil
  164. }
  165. ttlMinutes := n.Ttl.Minutes()
  166. if ttlMinutes == 0 {
  167. return bytesRead, nil
  168. }
  169. if !n.HasLastModifiedDate() {
  170. return bytesRead, nil
  171. }
  172. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  173. return bytesRead, nil
  174. }
  175. n.ReleaseMemory()
  176. return -1, errors.New("Not Found")
  177. }
  178. func ScanVolumeFile(dirname string, collection string, id VolumeId,
  179. needleMapKind NeedleMapType,
  180. visitSuperBlock func(SuperBlock) error,
  181. readNeedleBody bool,
  182. visitNeedle func(n *Needle, offset int64) error) (err error) {
  183. var v *Volume
  184. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  185. return fmt.Errorf("Failed to load volume %d: %v", id, err)
  186. }
  187. if err = visitSuperBlock(v.SuperBlock); err != nil {
  188. return fmt.Errorf("Failed to process volume %d super block: %v", id, err)
  189. }
  190. version := v.Version()
  191. offset := int64(SuperBlockSize)
  192. n, rest, e := ReadNeedleHeader(v.dataFile, version, offset)
  193. if e != nil {
  194. err = fmt.Errorf("cannot read needle header: %v", e)
  195. return
  196. }
  197. for n != nil {
  198. if readNeedleBody {
  199. if err = n.ReadNeedleBody(v.dataFile, version, offset+int64(NeedleHeaderSize), rest); err != nil {
  200. glog.V(0).Infof("cannot read needle body: %v", err)
  201. //err = fmt.Errorf("cannot read needle body: %v", err)
  202. //return
  203. }
  204. if n.DataSize >= n.Size {
  205. // this should come from a bug reported on #87 and #93
  206. // fixed in v0.69
  207. // remove this whole "if" clause later, long after 0.69
  208. oldRest, oldSize := rest, n.Size
  209. padding := NeedlePaddingSize - ((n.Size + NeedleHeaderSize + NeedleChecksumSize) % NeedlePaddingSize)
  210. n.Size = 0
  211. rest = n.Size + NeedleChecksumSize + padding
  212. if rest%NeedlePaddingSize != 0 {
  213. rest += (NeedlePaddingSize - rest%NeedlePaddingSize)
  214. }
  215. glog.V(4).Infof("Adjusting n.Size %d=>0 rest:%d=>%d %+v", oldSize, oldRest, rest, n)
  216. }
  217. }
  218. if err = visitNeedle(n, offset); err != nil {
  219. glog.V(0).Infof("visit needle error: %v", err)
  220. }
  221. offset += int64(NeedleHeaderSize) + int64(rest)
  222. glog.V(4).Infof("==> new entry offset %d", offset)
  223. if n, rest, err = ReadNeedleHeader(v.dataFile, version, offset); err != nil {
  224. if err == io.EOF {
  225. return nil
  226. }
  227. return fmt.Errorf("cannot read needle header: %v", err)
  228. }
  229. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  230. }
  231. return
  232. }