2016-07-03 06:50:58 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/backend"
|
2019-05-22 05:41:20 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/idx"
|
2019-04-19 04:43:36 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2016-07-03 06:50:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2019-04-19 08:56:38 +00:00
|
|
|
func CheckVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, e error) {
|
2016-07-03 06:50:58 +00:00
|
|
|
var indexSize int64
|
|
|
|
if indexSize, e = verifyIndexFileIntegrity(indexFile); e != nil {
|
2019-04-19 08:56:38 +00:00
|
|
|
return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), e)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
if indexSize == 0 {
|
2019-04-30 03:22:19 +00:00
|
|
|
return 0, nil
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
var lastIdxEntry []byte
|
2019-04-19 07:39:34 +00:00
|
|
|
if lastIdxEntry, e = readIndexEntryAtOffset(indexFile, indexSize-NeedleMapEntrySize); e != nil {
|
2019-04-19 08:56:38 +00:00
|
|
|
return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), e)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
2019-05-22 05:41:20 +00:00
|
|
|
key, offset, size := idx.IdxFileEntry(lastIdxEntry)
|
2019-04-19 08:56:38 +00:00
|
|
|
if offset.IsZero() {
|
2019-04-30 03:22:19 +00:00
|
|
|
return 0, nil
|
2016-07-25 06:54:40 +00:00
|
|
|
}
|
2019-04-19 08:56:38 +00:00
|
|
|
if size == TombstoneFileSize {
|
|
|
|
size = 0
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
2019-10-29 07:35:16 +00:00
|
|
|
if lastAppendAtNs, e = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToAcutalOffset(), key, size); e != nil {
|
2019-04-19 08:56:38 +00:00
|
|
|
return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), e)
|
|
|
|
}
|
|
|
|
return
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIndexFileIntegrity(indexFile *os.File) (indexSize int64, err error) {
|
|
|
|
if indexSize, err = util.GetFileSize(indexFile); err == nil {
|
2019-04-19 07:39:34 +00:00
|
|
|
if indexSize%NeedleMapEntrySize != 0 {
|
2016-07-03 06:50:58 +00:00
|
|
|
err = fmt.Errorf("index file's size is %d bytes, maybe corrupted", indexSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func readIndexEntryAtOffset(indexFile *os.File, offset int64) (bytes []byte, err error) {
|
|
|
|
if offset < 0 {
|
|
|
|
err = fmt.Errorf("offset %d for index file is invalid", offset)
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 07:39:34 +00:00
|
|
|
bytes = make([]byte, NeedleMapEntrySize)
|
2016-07-03 06:50:58 +00:00
|
|
|
_, err = indexFile.ReadAt(bytes, offset)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func verifyNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, offset int64, key NeedleId, size Size) (lastAppendAtNs uint64, err error) {
|
2019-04-19 04:43:36 +00:00
|
|
|
n := new(needle.Needle)
|
2019-04-19 08:56:38 +00:00
|
|
|
if err = n.ReadData(datFile, offset, size, v); err != nil {
|
2020-03-17 16:43:57 +00:00
|
|
|
return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", offset, offset+int64(size), err)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
|
|
|
if n.Id != key {
|
2019-04-19 08:56:38 +00:00
|
|
|
return n.AppendAtNs, fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|
2019-04-19 08:56:38 +00:00
|
|
|
return n.AppendAtNs, err
|
2016-07-03 06:50:58 +00:00
|
|
|
}
|