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.

140 lines
4.4 KiB

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