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.

88 lines
3.0 KiB

9 years ago
6 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
6 years ago
9 years ago
9 years ago
9 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  6. "github.com/chrislusf/seaweedfs/weed/storage/idx"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, e error) {
  12. var indexSize int64
  13. if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
  14. return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
  15. }
  16. if indexSize == 0 {
  17. return 0, nil
  18. }
  19. var lastIdxEntry []byte
  20. if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleMapEntrySize); e != nil {
  21. return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
  22. }
  23. key, offset, size := idx.IdxFileEntry(lastIdxEntry)
  24. if offset.IsZero() {
  25. return 0, nil
  26. }
  27. if size < 0 {
  28. // read the deletion entry
  29. if lastAppendAtNs, e = verifyDeletedNeedleIntegrity(v.DataBackend, v.Version(), key); e != nil {
  30. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
  31. }
  32. } else {
  33. if lastAppendAtNs, e = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToAcutalOffset(), key, size); e != nil {
  34. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
  35. }
  36. }
  37. return
  38. }
  39. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  40. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  41. if indexSize%NeedleMapEntrySize != 0 {
  42. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  43. }
  44. }
  45. return
  46. }
  47. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  48. if offset < 0 {
  49. err = fmt.Errorf("offset %d for index file is invalid", offset)
  50. return
  51. }
  52. bytes = make([]byte, NeedleMapEntrySize)
  53. _, err = indexFile.ReadAt(bytes, offset)
  54. return
  55. }
  56. func verifyNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, offset int64, key NeedleId, size Size) (lastAppendAtNs uint64, err error) {
  57. n := new(needle.Needle)
  58. if err = n.ReadData(datFile, offset, size, v); err != nil {
  59. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", offset, offset+int64(size), err)
  60. }
  61. if n.Id != key {
  62. return n.AppendAtNs, fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
  63. }
  64. return n.AppendAtNs, err
  65. }
  66. func verifyDeletedNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, key NeedleId) (lastAppendAtNs uint64, err error) {
  67. n := new(needle.Needle)
  68. size := n.DiskSize(v)
  69. var fileSize int64
  70. fileSize, _, err = datFile.GetStat()
  71. if err != nil {
  72. return 0, fmt.Errorf("GetStat: %v", err)
  73. }
  74. if err = n.ReadData(datFile, fileSize-size, Size(0), v); err != nil {
  75. return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", fileSize-size, size, err)
  76. }
  77. if n.Id != key {
  78. return n.AppendAtNs, fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
  79. }
  80. return n.AppendAtNs, err
  81. }