2016-07-03 06:50:58 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-30 05:25:09 +00:00
|
|
|
"io"
|
2016-07-03 06:50:58 +00:00
|
|
|
"os"
|
|
|
|
|
2020-11-17 06:26:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/log"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-10-28 08:14:39 +00:00
|
|
|
func CheckAndFixVolumeDataIntegrity(v *Volume, indexFile *os.File) (lastAppendAtNs uint64, err error) {
|
2016-07-03 06:50:58 +00:00
|
|
|
var indexSize int64
|
2020-10-28 08:14:39 +00:00
|
|
|
if indexSize, err = verifyIndexFileIntegrity(indexFile); err != nil {
|
|
|
|
return 0, fmt.Errorf("verifyIndexFileIntegrity %s failed: %v", indexFile.Name(), err)
|
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
|
|
|
}
|
2020-10-30 05:25:09 +00:00
|
|
|
healthyIndexSize := indexSize
|
2020-10-31 22:50:01 +00:00
|
|
|
for i := 1; i <= 10 && indexSize >= int64(i)*NeedleMapEntrySize; i++ {
|
2020-10-28 08:14:39 +00:00
|
|
|
// check and fix last 10 entries
|
|
|
|
lastAppendAtNs, err = doCheckAndFixVolumeData(v, indexFile, indexSize-int64(i)*NeedleMapEntrySize)
|
2020-10-30 05:25:09 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
healthyIndexSize = indexSize - int64(i)*NeedleMapEntrySize
|
|
|
|
continue
|
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
if err != ErrorSizeMismatch {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-30 05:25:09 +00:00
|
|
|
if healthyIndexSize < indexSize {
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Warnf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d", indexFile.Name(), indexSize, healthyIndexSize)
|
2020-10-30 05:25:09 +00:00
|
|
|
err = indexFile.Truncate(healthyIndexSize)
|
|
|
|
if err != nil {
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Warnf("CheckAndFixVolumeDataIntegrity truncate idx file %s from %d to %d: %v", indexFile.Name(), indexSize, healthyIndexSize, err)
|
2020-10-30 05:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func doCheckAndFixVolumeData(v *Volume, indexFile *os.File, indexOffset int64) (lastAppendAtNs uint64, err error) {
|
2016-07-03 06:50:58 +00:00
|
|
|
var lastIdxEntry []byte
|
2020-10-28 08:14:39 +00:00
|
|
|
if lastIdxEntry, err = readIndexEntryAtOffset(indexFile, indexOffset); err != nil {
|
|
|
|
return 0, fmt.Errorf("readLastIndexEntry %s failed: %v", indexFile.Name(), err)
|
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
|
|
|
}
|
2020-09-11 18:34:10 +00:00
|
|
|
if size < 0 {
|
2020-09-12 11:07:04 +00:00
|
|
|
// read the deletion entry
|
2020-10-28 08:14:39 +00:00
|
|
|
if lastAppendAtNs, err = verifyDeletedNeedleIntegrity(v.DataBackend, v.Version(), key); err != nil {
|
|
|
|
return lastAppendAtNs, fmt.Errorf("verifyNeedleIntegrity %s failed: %v", indexFile.Name(), err)
|
2020-09-12 11:07:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-28 08:14:39 +00:00
|
|
|
if lastAppendAtNs, err = verifyNeedleIntegrity(v.DataBackend, v.Version(), offset.ToAcutalOffset(), key, size); err != nil {
|
|
|
|
return lastAppendAtNs, err
|
2020-09-12 11:07:04 +00:00
|
|
|
}
|
2019-04-19 08:56:38 +00:00
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
return lastAppendAtNs, nil
|
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) {
|
2020-10-28 08:14:39 +00:00
|
|
|
n, _, _, err := needle.ReadNeedleHeader(datFile, v, offset)
|
2020-10-30 05:25:09 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("read %s at %d", datFile.Name(), offset)
|
|
|
|
}
|
|
|
|
if n.Size != size {
|
|
|
|
return 0, ErrorSizeMismatch
|
|
|
|
}
|
|
|
|
if v == needle.Version3 {
|
|
|
|
bytes := make([]byte, TimestampSize)
|
|
|
|
_, err = datFile.ReadAt(bytes, offset+NeedleHeaderSize+int64(size)+needle.NeedleChecksumSize)
|
2020-10-30 05:25:09 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
if err != nil {
|
2020-10-30 05:25:09 +00:00
|
|
|
return 0, fmt.Errorf("verifyNeedleIntegrity check %s entry offset %d size %d: %v", datFile.Name(), offset, size, err)
|
2020-10-28 08:14:39 +00:00
|
|
|
}
|
|
|
|
n.AppendAtNs = util.BytesToUint64(bytes)
|
|
|
|
fileTailOffset := offset + needle.GetActualSize(size, v)
|
|
|
|
fileSize, _, err := datFile.GetStat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("stat file %s: %v", datFile.Name(), err)
|
|
|
|
}
|
|
|
|
if fileSize == fileTailOffset {
|
|
|
|
return n.AppendAtNs, nil
|
|
|
|
}
|
|
|
|
if fileSize > fileTailOffset {
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Warnf("Truncate %s from %d bytes to %d bytes!", datFile.Name(), fileSize, fileTailOffset)
|
2020-10-28 08:14:39 +00:00
|
|
|
err = datFile.Truncate(fileTailOffset)
|
|
|
|
if err == nil {
|
|
|
|
return n.AppendAtNs, nil
|
2020-10-28 04:03:06 +00:00
|
|
|
}
|
2020-10-28 08:14:39 +00:00
|
|
|
return n.AppendAtNs, fmt.Errorf("truncate file %s: %v", datFile.Name(), err)
|
2020-10-28 04:03:06 +00:00
|
|
|
}
|
2020-11-17 06:26:58 +00:00
|
|
|
log.Warnf("data file %s has %d bytes, less than expected %d bytes!", datFile.Name(), fileSize, fileTailOffset)
|
2020-10-28 04:03:06 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-09-12 11:07:04 +00:00
|
|
|
|
|
|
|
func verifyDeletedNeedleIntegrity(datFile backend.BackendStorageFile, v needle.Version, key NeedleId) (lastAppendAtNs uint64, err error) {
|
|
|
|
n := new(needle.Needle)
|
|
|
|
size := n.DiskSize(v)
|
|
|
|
var fileSize int64
|
|
|
|
fileSize, _, err = datFile.GetStat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("GetStat: %v", err)
|
|
|
|
}
|
|
|
|
if err = n.ReadData(datFile, fileSize-size, Size(0), v); err != nil {
|
|
|
|
return n.AppendAtNs, fmt.Errorf("read data [%d,%d) : %v", fileSize-size, size, err)
|
|
|
|
}
|
|
|
|
if n.Id != key {
|
|
|
|
return n.AppendAtNs, fmt.Errorf("index key %#x does not match needle's Id %#x", key, n.Id)
|
|
|
|
}
|
|
|
|
return n.AppendAtNs, err
|
|
|
|
}
|