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.

141 lines
4.3 KiB

6 years ago
10 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path"
  8. "strconv"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/types"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. var (
  16. fixVolumePath = flag.String("dir", "/tmp", "data directory to store files")
  17. fixVolumeCollection = flag.String("collection", "", "the volume collection name")
  18. fixVolumeId = flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  19. )
  20. /*
  21. This is to resolve an one-time issue that caused inconsistency with .dat and .idx files.
  22. In this case, the .dat file contains all data, but some of deletion caused incorrect offset.
  23. The .idx has all correct offsets.
  24. 1. fix the .dat file, a new .dat_fixed file will be generated.
  25. go run fix_dat.go -volumeId=9 -dir=/Users/chrislu/Downloads
  26. 2. move the original .dat and .idx files to some backup folder, and rename .dat_fixed to .dat file
  27. mv 9.dat_fixed 9.dat
  28. 3. fix the .idx file with the "weed fix"
  29. weed fix -volumeId=9 -dir=/Users/chrislu/Downloads
  30. */
  31. func main() {
  32. flag.Parse()
  33. fileName := strconv.Itoa(*fixVolumeId)
  34. if *fixVolumeCollection != "" {
  35. fileName = *fixVolumeCollection + "_" + fileName
  36. }
  37. indexFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".idx"), os.O_RDONLY, 0644)
  38. if err != nil {
  39. glog.Fatalf("Read Volume Index %v", err)
  40. }
  41. defer indexFile.Close()
  42. datFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".dat"), os.O_RDONLY, 0644)
  43. if err != nil {
  44. glog.Fatalf("Read Volume Data %v", err)
  45. }
  46. defer datFile.Close()
  47. newDatFile, err := os.Create(path.Join(*fixVolumePath, fileName+".dat_fixed"))
  48. if err != nil {
  49. glog.Fatalf("Write New Volume Data %v", err)
  50. }
  51. defer newDatFile.Close()
  52. superBlock, err := storage.ReadSuperBlock(datFile)
  53. if err != nil {
  54. glog.Fatalf("Read Volume Data superblock %v", err)
  55. }
  56. newDatFile.Write(superBlock.Bytes())
  57. iterateEntries(datFile, indexFile, func(n *needle.Needle, offset int64) {
  58. fmt.Printf("needle id=%v name=%s size=%d dataSize=%d\n", n.Id, string(n.Name), n.Size, n.DataSize)
  59. _, s, _, e := n.Append(newDatFile, superBlock.Version())
  60. fmt.Printf("size %d error %v\n", s, e)
  61. })
  62. }
  63. func iterateEntries(datFile, idxFile *os.File, visitNeedle func(n *needle.Needle, offset int64)) {
  64. // start to read index file
  65. var readerOffset int64
  66. bytes := make([]byte, 16)
  67. count, _ := idxFile.ReadAt(bytes, readerOffset)
  68. readerOffset += int64(count)
  69. // start to read dat file
  70. superBlock, err := storage.ReadSuperBlock(datFile)
  71. if err != nil {
  72. fmt.Printf("cannot read dat file super block: %v", err)
  73. return
  74. }
  75. offset := int64(superBlock.BlockSize())
  76. version := superBlock.Version()
  77. n, _, rest, err := needle.ReadNeedleHeader(datFile, version, offset)
  78. if err != nil {
  79. fmt.Printf("cannot read needle header: %v", err)
  80. return
  81. }
  82. fmt.Printf("Needle %+v, rest %d\n", n, rest)
  83. for n != nil && count > 0 {
  84. // parse index file entry
  85. key := util.BytesToUint64(bytes[0:8])
  86. offsetFromIndex := util.BytesToUint32(bytes[8:12])
  87. sizeFromIndex := util.BytesToUint32(bytes[12:16])
  88. count, _ = idxFile.ReadAt(bytes, readerOffset)
  89. readerOffset += int64(count)
  90. if offsetFromIndex != 0 && offset != int64(offsetFromIndex)*8 {
  91. //t := offset
  92. offset = int64(offsetFromIndex) * 8
  93. //fmt.Printf("Offset change %d => %d\n", t, offset)
  94. }
  95. fmt.Printf("key: %d offsetFromIndex %d n.Size %d sizeFromIndex:%d\n", key, offsetFromIndex, n.Size, sizeFromIndex)
  96. rest = needle.NeedleBodyLength(sizeFromIndex, version)
  97. func() {
  98. defer func() {
  99. if r := recover(); r != nil {
  100. fmt.Println("Recovered in f", r)
  101. }
  102. }()
  103. if _, err = n.ReadNeedleBody(datFile, version, offset+int64(types.NeedleHeaderSize), rest); err != nil {
  104. fmt.Printf("cannot read needle body: offset %d body %d %v\n", offset, rest, err)
  105. }
  106. }()
  107. if n.Size <= n.DataSize {
  108. continue
  109. }
  110. visitNeedle(n, offset)
  111. offset += types.NeedleHeaderSize + rest
  112. //fmt.Printf("==> new entry offset %d\n", offset)
  113. if n, _, rest, err = needle.ReadNeedleHeader(datFile, version, offset); err != nil {
  114. if err == io.EOF {
  115. return
  116. }
  117. fmt.Printf("cannot read needle header: %v\n", err)
  118. return
  119. }
  120. //fmt.Printf("new entry needle size:%d rest:%d\n", n.Size, rest)
  121. }
  122. }