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.

66 lines
2.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
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/idx"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, e error) {
  11. var indexSize int64
  12. if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
  13. return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
  14. }
  15. if indexSize == 0 {
  16. return 0, nil
  17. }
  18. var lastIdxEntry []byte
  19. if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleMapEntrySize); e != nil {
  20. return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
  21. }
  22. key, offset, size := idx.IdxFileEntry(lastIdxEntry)
  23. if offset.IsZero() {
  24. return 0, nil
  25. }
  26. if size == TombstoneFileSize {
  27. size = 0
  28. }
  29. if lastAppendAtNs, e = verifyNeedleIntegrity(v.dataFile, v.Version(), offset.ToAcutalOffset(), key, size); e != nil {
  30. return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
  31. }
  32. return
  33. }
  34. func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
  35. if indexSize, err = util.GetFileSize(indexFile); err == nil {
  36. if indexSize%NeedleMapEntrySize != 0 {
  37. err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
  38. }
  39. }
  40. return
  41. }
  42. func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
  43. if offset < 0 {
  44. err = fmt.Errorf("offset %d for index file is invalid", offset)
  45. return
  46. }
  47. bytes = make([]byte, NeedleMapEntrySize)
  48. _, err = indexFile.ReadAt(bytes, offset)
  49. return
  50. }
  51. func verifyNeedleIntegrity(datFile *os.File, v needle.Version, offset int64, key NeedleId, size uint32) (lastAppendAtNs uint64, err error) {
  52. n := new(needle.Needle)
  53. if err = n.ReadData(datFile, offset, size, v); err != nil {
  54. return n.AppendAtNs, err
  55. }
  56. if n.Id != key {
  57. return n.AppendAtNs, fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
  58. }
  59. return n.AppendAtNs, err
  60. }