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.

165 lines
5.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  5. "io"
  6. "os"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/idx"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  11. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. func CheckAndFixVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, err error) {
  15. var indexSize int64
  16. if indexSize, err = verifyIndexFileIntegrity(indexFile); err != nil {
  17. return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), err)
  18. }
  19. if indexSize == 0 {
  20. return 0, nil
  21. }
  22. healthyIndexSize := indexSize
  23. for i := 1; i <= 10 && indexSize >= int64(i)*NeedleMapEntrySize; i++ {
  24. // check and fix last 10 entries
  25. lastAppendAtNs, err = doCheckAndFixVolumeData(v, indexFile, indexSize-int64(i)*NeedleMapEntrySize)
  26. if err == io.EOF {
  27. healthyIndexSize = indexSize - int64(i)*NeedleMapEntrySize
  28. continue
  29. }
  30. if err != ErrorSizeMismatch {
  31. break
  32. }
  33. }
  34. if healthyIndexSize < indexSize {
  35. glog.Warningf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d", indexFile.Name(), indexSize, healthyIndexSize)
  36. err = indexFile.Truncate(healthyIndexSize)
  37. if err != nil {
  38. glog.Warningf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d: %v", indexFile.Name(), indexSize, healthyIndexSize, err)
  39. }
  40. }
  41. return
  42. }
  43. func doCheckAndFixVolumeData(v *Volume, indexFile *os.File, indexOffset int64) (lastAppendAtNs uint64, err error) {
  44. var lastIdxEntry []byte
  45. if lastIdxEntry, err = readIndexEntryAtOffset(indexFile, indexOffset); err != nil {
  46. return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), err)
  47. }
  48. key, offset, size := idx.IdxFileEntry(lastIdxEntry)
  49. if offset.IsZero() {
  50. return 0, nil
  51. }
  52. if size < 0 {
  53. // read the deletion entry
  54. if lastAppendAtNs, err = verifyDeletedNeedleIntegrity(v.DataBackend, v.Version(), key); err != nil {
  55. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), err)
  56. }
  57. } else {
  58. if lastAppendAtNs, err = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToActualOffset(), key, size); err != nil {
  59. if err == ErrorSizeMismatch {
  60. return verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToActualOffset()+int64(MaxPossibleVolumeSize), key, size)
  61. }
  62. return lastAppendAtNs, err
  63. }
  64. }
  65. return lastAppendAtNs, nil
  66. }
  67. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  68. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  69. if indexSize%NeedleMapEntrySize != 0 {
  70. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  71. }
  72. }
  73. return
  74. }
  75. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  76. if offset < 0 {
  77. err = fmt.Errorf("offset %d for index file is invalid", offset)
  78. return
  79. }
  80. bytes = make([]byte, NeedleMapEntrySize)
  81. _, err = indexFile.ReadAt(bytes, offset)
  82. return
  83. }
  84. func verifyNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, offset int64, key NeedleId, size Size) (lastAppendAtNs uint64, err error) {
  85. n, _, _, err := needle.ReadNeedleHeader(datFile, v, offset)
  86. if err == io.EOF {
  87. return 0, err
  88. }
  89. if err != nil {
  90. return 0, fmt.Errorf("read %s at %d", datFile.Name(), offset)
  91. }
  92. if n.Size != size {
  93. return 0, ErrorSizeMismatch
  94. }
  95. if v == needle.Version3 {
  96. bytes := make([]byte, TimestampSize)
  97. _, err = datFile.ReadAt(bytes, offset+NeedleHeaderSize+int64(size)+needle.NeedleChecksumSize)
  98. if err == io.EOF {
  99. return 0, err
  100. }
  101. if err != nil {
  102. return 0, fmt.Errorf("verifyNeedleIntegrity check %s entry offset %d size %d: %v", datFile.Name(), offset, size, err)
  103. }
  104. n.AppendAtNs = util.BytesToUint64(bytes)
  105. fileTailOffset := offset + needle.GetActualSize(size, v)
  106. fileSize, _, err := datFile.GetStat()
  107. if err != nil {
  108. return 0, fmt.Errorf("stat file %s: %v", datFile.Name(), err)
  109. }
  110. if fileSize == fileTailOffset {
  111. return n.AppendAtNs, nil
  112. }
  113. if fileSize > fileTailOffset {
  114. glog.Warningf("data file %s actual %d bytes expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  115. return n.AppendAtNs, fmt.Errorf("data file %s actual %d bytes expected %d bytes", datFile.Name(), fileSize, fileTailOffset)
  116. }
  117. glog.Warningf("data file %s has %d bytes, less than expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  118. }
  119. if err = n.ReadData(datFile, offset, size, v); err != nil {
  120. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", offset, offset+int64(size), err)
  121. }
  122. if n.Id != key {
  123. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  124. }
  125. return n.AppendAtNs, err
  126. }
  127. func verifyDeletedNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, key NeedleId) (lastAppendAtNs uint64, err error) {
  128. n := new(needle.Needle)
  129. size := n.DiskSize(v)
  130. var fileSize int64
  131. fileSize, _, err = datFile.GetStat()
  132. if err != nil {
  133. return 0, fmt.Errorf("GetStat: %v", err)
  134. }
  135. if err = n.ReadData(datFile, fileSize-size, Size(0), v); err != nil {
  136. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", fileSize-size, size, err)
  137. }
  138. if n.Id != key {
  139. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  140. }
  141. return n.AppendAtNs, err
  142. }
  143. func (v *Volume) checkIdxFile() error {
  144. datFileSize, _, err := v.DataBackend.GetStat()
  145. if err != nil {
  146. return fmt.Errorf("get stat %s: %v", v.FileName(".dat"), err)
  147. }
  148. if datFileSize <= super_block.SuperBlockSize {
  149. return nil
  150. }
  151. indexFileName := v.FileName(".idx")
  152. if util.FileExists(indexFileName) {
  153. return nil
  154. }
  155. return fmt.Errorf("idx file %s does not exists", indexFileName)
  156. }