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.

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