volume_checking.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package storage
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  8. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func CheckAndFixVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, err error) {
  14. var indexSize int64
  15. if indexSize, err = verifyIndexFileIntegrity(indexFile); err != nil {
  16. return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), err)
  17. }
  18. if indexSize == 0 {
  19. return 0, nil
  20. }
  21. healthyIndexSize := indexSize
  22. for i := 1; i <= 10 && indexSize >= int64(i)*NeedleMapEntrySize; i++ {
  23. // check and fix last 10 entries
  24. lastAppendAtNs, err = doCheckAndFixVolumeData(v, indexFile, indexSize-int64(i)*NeedleMapEntrySize)
  25. if err == io.EOF {
  26. healthyIndexSize = indexSize - int64(i)*NeedleMapEntrySize
  27. continue
  28. }
  29. if err != ErrorSizeMismatch {
  30. break
  31. }
  32. }
  33. if healthyIndexSize < indexSize {
  34. glog.Warningf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d", indexFile.Name(), indexSize, healthyIndexSize)
  35. err = indexFile.Truncate(healthyIndexSize)
  36. if err != nil {
  37. glog.Warningf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d: %v", indexFile.Name(), indexSize, healthyIndexSize, err)
  38. }
  39. }
  40. return
  41. }
  42. func doCheckAndFixVolumeData(v *Volume, indexFile *os.File, indexOffset int64) (lastAppendAtNs uint64, err error) {
  43. var lastIdxEntry []byte
  44. if lastIdxEntry, err = readIndexEntryAtOffset(indexFile, indexOffset); err != nil {
  45. return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), err)
  46. }
  47. key, offset, size := idx.IdxFileEntry(lastIdxEntry)
  48. if offset.IsZero() {
  49. return 0, nil
  50. }
  51. if size < 0 {
  52. // read the deletion entry
  53. if lastAppendAtNs, err = verifyDeletedNeedleIntegrity(v.DataBackend, v.Version(), key); err != nil {
  54. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), err)
  55. }
  56. } else {
  57. if lastAppendAtNs, err = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToAcutalOffset(), key, size); err != nil {
  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("Truncate %s from %d bytes to %d bytes!", datFile.Name(), fileSize, fileTailOffset)
  111. err = datFile.Truncate(fileTailOffset)
  112. if err == nil {
  113. return n.AppendAtNs, nil
  114. }
  115. return n.AppendAtNs, fmt.Errorf("truncate file %s: %v", datFile.Name(), err)
  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 %#x does not match needle's Id %#x", 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 %#x does not match needle's Id %#x", key, n.Id)
  140. }
  141. return n.AppendAtNs, err
  142. }