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.

169 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 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. var readCount int
  78. readCount, err = indexFile.ReadAt(bytes, offset)
  79. if err == io.EOF && readCount == NeedleMapEntrySize {
  80. err = nil
  81. }
  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. var readCount int
  98. readCount, err = datFile.ReadAt(bytes, offset+NeedleHeaderSize+int64(size)+needle.NeedleChecksumSize)
  99. if err == io.EOF && readCount == TimestampSize {
  100. err = nil
  101. }
  102. if err == io.EOF {
  103. return 0, err
  104. }
  105. if err != nil {
  106. return 0, fmt.Errorf("verifyNeedleIntegrity check %s entry offset %d size %d: %v", datFile.Name(), offset, size, err)
  107. }
  108. n.AppendAtNs = util.BytesToUint64(bytes)
  109. fileTailOffset := offset + needle.GetActualSize(size, v)
  110. fileSize, _, err := datFile.GetStat()
  111. if err != nil {
  112. return 0, fmt.Errorf("stat file %s: %v", datFile.Name(), err)
  113. }
  114. if fileSize == fileTailOffset {
  115. return n.AppendAtNs, nil
  116. }
  117. if fileSize > fileTailOffset {
  118. glog.Warningf("data file %s actual %d bytes expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  119. return n.AppendAtNs, fmt.Errorf("data file %s actual %d bytes expected %d bytes", datFile.Name(), fileSize, fileTailOffset)
  120. }
  121. glog.Warningf("data file %s has %d bytes, less than expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  122. }
  123. if err = n.ReadData(datFile, offset, size, v); err != nil {
  124. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", offset, offset+int64(size), err)
  125. }
  126. if n.Id != key {
  127. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  128. }
  129. return n.AppendAtNs, err
  130. }
  131. func verifyDeletedNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, key NeedleId) (lastAppendAtNs uint64, err error) {
  132. n := new(needle.Needle)
  133. size := n.DiskSize(v)
  134. var fileSize int64
  135. fileSize, _, err = datFile.GetStat()
  136. if err != nil {
  137. return 0, fmt.Errorf("GetStat: %v", err)
  138. }
  139. if err = n.ReadData(datFile, fileSize-size, Size(0), v); err != nil {
  140. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", fileSize-size, size, err)
  141. }
  142. if n.Id != key {
  143. return n.AppendAtNs, fmt.Errorf("index key %v does not match needle's Id %v", key, n.Id)
  144. }
  145. return n.AppendAtNs, err
  146. }
  147. func (v *Volume) checkIdxFile() error {
  148. datFileSize, _, err := v.DataBackend.GetStat()
  149. if err != nil {
  150. return fmt.Errorf("get stat %s: %v", v.FileName(".dat"), err)
  151. }
  152. if datFileSize <= super_block.SuperBlockSize {
  153. return nil
  154. }
  155. indexFileName := v.FileName(".idx")
  156. if util.FileExists(indexFileName) {
  157. return nil
  158. }
  159. return fmt.Errorf("idx file %s does not exists", indexFileName)
  160. }