volume_checking.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 CheckVolumeDataIntegrity(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. return 0, fmt.Errorf("CheckVolumeDataIntegrity %s failed: index size %d differs from healthy size %d", indexFile.Name(), indexSize, healthyIndexSize)
  36. }
  37. return
  38. }
  39. func doCheckAndFixVolumeData(v *Volume, indexFile *os.File, indexOffset int64) (lastAppendAtNs uint64, err error) {
  40. var lastIdxEntry []byte
  41. if lastIdxEntry, err = readIndexEntryAtOffset(indexFile, indexOffset); err != nil {
  42. return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), err)
  43. }
  44. key, offset, size := idx.IdxFileEntry(lastIdxEntry)
  45. if offset.IsZero() {
  46. return 0, nil
  47. }
  48. if size < 0 {
  49. // read the deletion entry
  50. if lastAppendAtNs, err = verifyDeletedNeedleIntegrity(v.DataBackend, v.Version(), key); err != nil {
  51. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), err)
  52. }
  53. } else {
  54. if lastAppendAtNs, err = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToActualOffset(), key, size); err != nil {
  55. if err == ErrorSizeMismatch {
  56. return verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToActualOffset()+int64(MaxPossibleVolumeSize), key, size)
  57. }
  58. return lastAppendAtNs, err
  59. }
  60. }
  61. return lastAppendAtNs, nil
  62. }
  63. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  64. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  65. if indexSize%NeedleMapEntrySize != 0 {
  66. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  67. }
  68. }
  69. return
  70. }
  71. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  72. if offset < 0 {
  73. err = fmt.Errorf("offset %d for index file is invalid", offset)
  74. return
  75. }
  76. bytes = make([]byte, NeedleMapEntrySize)
  77. _, err = indexFile.ReadAt(bytes, offset)
  78. return
  79. }
  80. func verifyNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, offset int64, key NeedleId, size Size) (lastAppendAtNs uint64, err error) {
  81. n, _, _, err := needle.ReadNeedleHeader(datFile, v, offset)
  82. if err == io.EOF {
  83. return 0, err
  84. }
  85. if err != nil {
  86. return 0, fmt.Errorf("read %s at %d", datFile.Name(), offset)
  87. }
  88. if n.Size != size {
  89. return 0, ErrorSizeMismatch
  90. }
  91. if v == needle.Version3 {
  92. bytes := make([]byte, TimestampSize)
  93. _, err = datFile.ReadAt(bytes, offset+NeedleHeaderSize+int64(size)+needle.NeedleChecksumSize)
  94. if err == io.EOF {
  95. return 0, err
  96. }
  97. if err != nil {
  98. return 0, fmt.Errorf("verifyNeedleIntegrity check %s entry offset %d size %d: %v", datFile.Name(), offset, size, err)
  99. }
  100. n.AppendAtNs = util.BytesToUint64(bytes)
  101. fileTailOffset := offset + needle.GetActualSize(size, v)
  102. fileSize, _, err := datFile.GetStat()
  103. if err != nil {
  104. return 0, fmt.Errorf("stat file %s: %v", datFile.Name(), err)
  105. }
  106. if fileSize == fileTailOffset {
  107. return n.AppendAtNs, nil
  108. }
  109. if fileSize > fileTailOffset {
  110. glog.Warningf("data file %s actual %d bytes expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  111. return n.AppendAtNs, fmt.Errorf("data file %s actual %d bytes expected %d bytes", datFile.Name(), fileSize, fileTailOffset)
  112. }
  113. glog.Warningf("data file %s has %d bytes, less than expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  114. }
  115. if err = n.ReadData(datFile, offset, size, v); err != nil {
  116. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", offset, offset+int64(size), err)
  117. }
  118. if n.Id != key {
  119. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  120. }
  121. return n.AppendAtNs, err
  122. }
  123. func verifyDeletedNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, key NeedleId) (lastAppendAtNs uint64, err error) {
  124. n := new(needle.Needle)
  125. size := n.DiskSize(v)
  126. var fileSize int64
  127. fileSize, _, err = datFile.GetStat()
  128. if err != nil {
  129. return 0, fmt.Errorf("GetStat: %v", err)
  130. }
  131. if err = n.ReadData(datFile, fileSize-size, Size(0), v); err != nil {
  132. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", fileSize-size, size, err)
  133. }
  134. if n.Id != key {
  135. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  136. }
  137. return n.AppendAtNs, err
  138. }
  139. func (v *Volume) checkIdxFile() error {
  140. datFileSize, _, err := v.DataBackend.GetStat()
  141. if err != nil {
  142. return fmt.Errorf("get stat %s: %v", v.FileName(".dat"), err)
  143. }
  144. if datFileSize <= super_block.SuperBlockSize {
  145. return nil
  146. }
  147. indexFileName := v.FileName(".idx")
  148. if util.FileExists(indexFileName) {
  149. return nil
  150. }
  151. return fmt.Errorf("idx file %s does not exists", indexFileName)
  152. }