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.

276 lines
8.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package storage
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/joeslay/seaweedfs/weed/glog"
  10. "github.com/joeslay/seaweedfs/weed/storage/memory_map"
  11. "github.com/joeslay/seaweedfs/weed/storage/needle"
  12. . "github.com/joeslay/seaweedfs/weed/storage/types"
  13. )
  14. var ErrorNotFound = errors.New("not found")
  15. // isFileUnchanged checks whether this needle to write is same as last one.
  16. // It requires serialized access in the same volume.
  17. func (v *Volume) isFileUnchanged(n *needle.Needle) bool {
  18. if v.Ttl.String() != "" {
  19. return false
  20. }
  21. nv, ok := v.nm.Get(n.Id)
  22. if ok && !nv.Offset.IsZero() && nv.Size != TombstoneFileSize {
  23. oldNeedle := new(needle.Needle)
  24. err := oldNeedle.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  25. if err != nil {
  26. glog.V(0).Infof("Failed to check updated file at offset %d size %d: %v", nv.Offset.ToAcutalOffset(), nv.Size, err)
  27. return false
  28. }
  29. if oldNeedle.Cookie == n.Cookie && oldNeedle.Checksum == n.Checksum && bytes.Equal(oldNeedle.Data, n.Data) {
  30. n.DataSize = oldNeedle.DataSize
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. // Destroy removes everything related to this volume
  37. func (v *Volume) Destroy() (err error) {
  38. if v.readOnly {
  39. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  40. return
  41. }
  42. if v.isCompacting {
  43. err = fmt.Errorf("volume %d is compacting", v.Id)
  44. return
  45. }
  46. mem_map, exists := memory_map.FileMemoryMap[v.dataFile.Name()]
  47. if exists {
  48. mem_map.DeleteFileAndMemoryMap()
  49. delete(memory_map.FileMemoryMap, v.dataFile.Name())
  50. }
  51. v.Close()
  52. os.Remove(v.FileName() + ".dat")
  53. os.Remove(v.FileName() + ".idx")
  54. os.Remove(v.FileName() + ".cpd")
  55. os.Remove(v.FileName() + ".cpx")
  56. os.Remove(v.FileName() + ".ldb")
  57. os.Remove(v.FileName() + ".bdb")
  58. return
  59. }
  60. func (v *Volume) writeNeedle(n *needle.Needle) (offset uint64, size uint32, isUnchanged bool, err error) {
  61. glog.V(4).Infof("writing needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  62. if v.readOnly {
  63. err = fmt.Errorf("%s is read-only", v.dataFile.Name())
  64. return
  65. }
  66. v.dataFileAccessLock.Lock()
  67. defer v.dataFileAccessLock.Unlock()
  68. if v.isFileUnchanged(n) {
  69. size = n.DataSize
  70. isUnchanged = true
  71. return
  72. }
  73. if n.Ttl == needle.EMPTY_TTL && v.Ttl != needle.EMPTY_TTL {
  74. n.SetHasTtl()
  75. n.Ttl = v.Ttl
  76. }
  77. // check whether existing needle cookie matches
  78. nv, ok := v.nm.Get(n.Id)
  79. if ok {
  80. existingNeedle, _, _, existingNeedleReadErr := needle.ReadNeedleHeader(v.dataFile, v.Version(), nv.Offset.ToAcutalOffset())
  81. if existingNeedleReadErr != nil {
  82. err = fmt.Errorf("reading existing needle: %v", existingNeedleReadErr)
  83. return
  84. }
  85. if existingNeedle.Cookie != n.Cookie {
  86. glog.V(0).Infof("write cookie mismatch: existing %x, new %x", existingNeedle.Cookie, n.Cookie)
  87. err = fmt.Errorf("mismatching cookie %x", n.Cookie)
  88. return
  89. }
  90. }
  91. // append to dat file
  92. n.AppendAtNs = uint64(time.Now().UnixNano())
  93. if offset, size, _, err = n.Append(v.dataFile, v.Version()); err != nil {
  94. return
  95. }
  96. v.lastAppendAtNs = n.AppendAtNs
  97. // add to needle map
  98. if !ok || uint64(nv.Offset.ToAcutalOffset()) < offset {
  99. if err = v.nm.Put(n.Id, ToOffset(int64(offset)), n.Size); err != nil {
  100. glog.V(4).Infof("failed to save in needle map %d: %v", n.Id, err)
  101. }
  102. }
  103. if v.lastModifiedTsSeconds < n.LastModified {
  104. v.lastModifiedTsSeconds = n.LastModified
  105. }
  106. return
  107. }
  108. func (v *Volume) deleteNeedle(n *needle.Needle) (uint32, error) {
  109. glog.V(4).Infof("delete needle %s", needle.NewFileIdFromNeedle(v.Id, n).String())
  110. if v.readOnly {
  111. return 0, fmt.Errorf("%s is read-only", v.dataFile.Name())
  112. }
  113. v.dataFileAccessLock.Lock()
  114. defer v.dataFileAccessLock.Unlock()
  115. nv, ok := v.nm.Get(n.Id)
  116. //fmt.Println("key", n.Id, "volume offset", nv.Offset, "data_size", n.Size, "cached size", nv.Size)
  117. if ok && nv.Size != TombstoneFileSize {
  118. size := nv.Size
  119. n.Data = nil
  120. n.AppendAtNs = uint64(time.Now().UnixNano())
  121. offset, _, _, err := n.Append(v.dataFile, v.Version())
  122. if err != nil {
  123. return size, err
  124. }
  125. v.lastAppendAtNs = n.AppendAtNs
  126. if err = v.nm.Delete(n.Id, ToOffset(int64(offset))); err != nil {
  127. return size, err
  128. }
  129. return size, err
  130. }
  131. return 0, nil
  132. }
  133. // read fills in Needle content by looking up n.Id from NeedleMapper
  134. func (v *Volume) readNeedle(n *needle.Needle) (int, error) {
  135. v.dataFileAccessLock.Lock()
  136. defer v.dataFileAccessLock.Unlock()
  137. nv, ok := v.nm.Get(n.Id)
  138. if !ok || nv.Offset.IsZero() {
  139. return -1, ErrorNotFound
  140. }
  141. if nv.Size == TombstoneFileSize {
  142. return -1, errors.New("already deleted")
  143. }
  144. if nv.Size == 0 {
  145. return 0, nil
  146. }
  147. err := n.ReadData(v.dataFile, nv.Offset.ToAcutalOffset(), nv.Size, v.Version())
  148. if err != nil {
  149. return 0, err
  150. }
  151. bytesRead := len(n.Data)
  152. if !n.HasTtl() {
  153. return bytesRead, nil
  154. }
  155. ttlMinutes := n.Ttl.Minutes()
  156. if ttlMinutes == 0 {
  157. return bytesRead, nil
  158. }
  159. if !n.HasLastModifiedDate() {
  160. return bytesRead, nil
  161. }
  162. if uint64(time.Now().Unix()) < n.LastModified+uint64(ttlMinutes*60) {
  163. return bytesRead, nil
  164. }
  165. return -1, ErrorNotFound
  166. }
  167. type VolumeFileScanner interface {
  168. VisitSuperBlock(SuperBlock) error
  169. ReadNeedleBody() bool
  170. VisitNeedle(n *needle.Needle, offset int64) error
  171. }
  172. func ScanVolumeFile(dirname string, collection string, id needle.VolumeId,
  173. needleMapKind NeedleMapType,
  174. volumeFileScanner VolumeFileScanner) (err error) {
  175. var v *Volume
  176. if v, err = loadVolumeWithoutIndex(dirname, collection, id, needleMapKind); err != nil {
  177. return fmt.Errorf("failed to load volume %d: %v", id, err)
  178. }
  179. if err = volumeFileScanner.VisitSuperBlock(v.SuperBlock); err != nil {
  180. return fmt.Errorf("failed to process volume %d super block: %v", id, err)
  181. }
  182. defer v.Close()
  183. version := v.Version()
  184. offset := int64(v.SuperBlock.BlockSize())
  185. return ScanVolumeFileFrom(version, v.dataFile, offset, volumeFileScanner)
  186. }
  187. func ScanVolumeFileFrom(version needle.Version, dataFile *os.File, offset int64, volumeFileScanner VolumeFileScanner) (err error) {
  188. n, _, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  189. if e != nil {
  190. if e == io.EOF {
  191. return nil
  192. }
  193. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  194. }
  195. for n != nil {
  196. if volumeFileScanner.ReadNeedleBody() {
  197. if _, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  198. glog.V(0).Infof("cannot read needle body: %v", err)
  199. //err = fmt.Errorf("cannot read needle body: %v", err)
  200. //return
  201. }
  202. }
  203. err := volumeFileScanner.VisitNeedle(n, offset)
  204. if err == io.EOF {
  205. return nil
  206. }
  207. if err != nil {
  208. glog.V(0).Infof("visit needle error: %v", err)
  209. return fmt.Errorf("visit needle error: %v", err)
  210. }
  211. offset += NeedleHeaderSize + rest
  212. glog.V(4).Infof("==> new entry offset %d", offset)
  213. if n, _, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  214. if err == io.EOF {
  215. return nil
  216. }
  217. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  218. }
  219. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  220. }
  221. return nil
  222. }
  223. func ScanVolumeFileNeedleFrom(version needle.Version, dataFile *os.File, offset int64, fn func(needleHeader, needleBody []byte, needleAppendAtNs uint64) error) (err error) {
  224. n, nh, rest, e := needle.ReadNeedleHeader(dataFile, version, offset)
  225. if e != nil {
  226. if e == io.EOF {
  227. return nil
  228. }
  229. return fmt.Errorf("cannot read %s at offset %d: %v", dataFile.Name(), offset, e)
  230. }
  231. for n != nil {
  232. var needleBody []byte
  233. if needleBody, err = n.ReadNeedleBody(dataFile, version, offset+NeedleHeaderSize, rest); err != nil {
  234. glog.V(0).Infof("cannot read needle body: %v", err)
  235. //err = fmt.Errorf("cannot read needle body: %v", err)
  236. //return
  237. }
  238. err = fn(nh, needleBody, n.AppendAtNs)
  239. if err != nil {
  240. glog.V(0).Infof("visit needle error: %v", err)
  241. return
  242. }
  243. offset += NeedleHeaderSize + rest
  244. glog.V(4).Infof("==> new entry offset %d", offset)
  245. if n, nh, rest, err = needle.ReadNeedleHeader(dataFile, version, offset); err != nil {
  246. if err == io.EOF {
  247. return nil
  248. }
  249. return fmt.Errorf("cannot read needle header at offset %d: %v", offset, err)
  250. }
  251. glog.V(4).Infof("new entry needle size:%d rest:%d", n.Size, rest)
  252. }
  253. return nil
  254. }