2015-03-17 18:15:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2015-03-17 18:15:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fixVolumePath = flag.String("dir", "/tmp", "data directory to store files")
|
|
|
|
fixVolumeCollection = flag.String("collection", "", "the volume collection name")
|
|
|
|
fixVolumeId = flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is to resolve an one-time issue that caused inconsistency with .dat and .idx files.
|
2022-05-16 14:11:33 +00:00
|
|
|
In this case, the .dat file contains all data, but some deletion caused incorrect offset.
|
2015-03-17 21:22:56 +00:00
|
|
|
The .idx has all correct offsets.
|
|
|
|
|
2015-03-17 18:15:37 +00:00
|
|
|
1. fix the .dat file, a new .dat_fixed file will be generated.
|
|
|
|
go run fix_dat.go -volumeId=9 -dir=/Users/chrislu/Downloads
|
|
|
|
2. move the original .dat and .idx files to some backup folder, and rename .dat_fixed to .dat file
|
|
|
|
mv 9.dat_fixed 9.dat
|
|
|
|
3. fix the .idx file with the "weed fix"
|
|
|
|
weed fix -volumeId=9 -dir=/Users/chrislu/Downloads
|
|
|
|
*/
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
fileName := strconv.Itoa(*fixVolumeId)
|
|
|
|
if *fixVolumeCollection != "" {
|
|
|
|
fileName = *fixVolumeCollection + "_" + fileName
|
|
|
|
}
|
|
|
|
indexFile, err := os.OpenFile(path.Join(*fixVolumePath, fileName+".idx"), os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
2018-06-24 18:37:08 +00:00
|
|
|
glog.Fatalf("Read Volume Index %v", err)
|
2015-03-17 18:15:37 +00:00
|
|
|
}
|
|
|
|
defer indexFile.Close()
|
2019-10-29 07:35:16 +00:00
|
|
|
datFileName := path.Join(*fixVolumePath, fileName+".dat")
|
|
|
|
datFile, err := os.OpenFile(datFileName, os.O_RDONLY, 0644)
|
2015-03-17 18:15:37 +00:00
|
|
|
if err != nil {
|
2018-06-24 18:37:08 +00:00
|
|
|
glog.Fatalf("Read Volume Data %v", err)
|
2015-03-17 18:15:37 +00:00
|
|
|
}
|
2019-10-30 05:37:36 +00:00
|
|
|
datBackend := backend.NewDiskFile(datFile)
|
2019-10-29 07:35:16 +00:00
|
|
|
defer datBackend.Close()
|
2015-03-17 18:15:37 +00:00
|
|
|
|
|
|
|
newDatFile, err := os.Create(path.Join(*fixVolumePath, fileName+".dat_fixed"))
|
|
|
|
if err != nil {
|
2018-06-24 18:37:08 +00:00
|
|
|
glog.Fatalf("Write New Volume Data %v", err)
|
2015-03-17 18:15:37 +00:00
|
|
|
}
|
|
|
|
defer newDatFile.Close()
|
|
|
|
|
2019-12-23 20:48:20 +00:00
|
|
|
superBlock, err := super_block.ReadSuperBlock(datBackend)
|
2018-06-24 18:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Read Volume Data superblock %v", err)
|
|
|
|
}
|
|
|
|
newDatFile.Write(superBlock.Bytes())
|
2015-03-17 18:15:37 +00:00
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
iterateEntries(datBackend, indexFile, func(n *needle.Needle, offset int64) {
|
2018-08-01 06:39:56 +00:00
|
|
|
fmt.Printf("needle id=%v name=%s size=%d dataSize=%d\n", n.Id, string(n.Name), n.Size, n.DataSize)
|
2019-12-28 19:50:42 +00:00
|
|
|
_, s, _, e := n.Append(datBackend, superBlock.Version)
|
2015-03-17 18:15:37 +00:00
|
|
|
fmt.Printf("size %d error %v\n", s, e)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-29 02:33:18 +00:00
|
|
|
func iterateEntries(datBackend backend.BackendStorageFile, idxFile *os.File, visitNeedle func(n *needle.Needle, offset int64)) {
|
2015-03-17 18:15:37 +00:00
|
|
|
// start to read index file
|
|
|
|
var readerOffset int64
|
|
|
|
bytes := make([]byte, 16)
|
|
|
|
count, _ := idxFile.ReadAt(bytes, readerOffset)
|
|
|
|
readerOffset += int64(count)
|
|
|
|
|
|
|
|
// start to read dat file
|
2019-12-23 20:48:20 +00:00
|
|
|
superBlock, err := super_block.ReadSuperBlock(datBackend)
|
2018-06-24 18:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cannot read dat file super block: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-01-17 01:17:19 +00:00
|
|
|
offset := int64(superBlock.BlockSize())
|
2019-12-28 19:50:42 +00:00
|
|
|
version := superBlock.Version
|
2019-10-29 07:35:16 +00:00
|
|
|
n, _, rest, err := needle.ReadNeedleHeader(datBackend, version, offset)
|
2015-03-17 18:15:37 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cannot read needle header: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Needle %+v, rest %d\n", n, rest)
|
|
|
|
for n != nil && count > 0 {
|
|
|
|
// parse index file entry
|
|
|
|
key := util.BytesToUint64(bytes[0:8])
|
|
|
|
offsetFromIndex := util.BytesToUint32(bytes[8:12])
|
2020-08-19 00:23:01 +00:00
|
|
|
sizeFromIndex := types.BytesToSize(bytes[12:16])
|
2015-03-17 18:15:37 +00:00
|
|
|
count, _ = idxFile.ReadAt(bytes, readerOffset)
|
|
|
|
readerOffset += int64(count)
|
|
|
|
|
|
|
|
if offsetFromIndex != 0 && offset != int64(offsetFromIndex)*8 {
|
|
|
|
//t := offset
|
|
|
|
offset = int64(offsetFromIndex) * 8
|
|
|
|
//fmt.Printf("Offset change %d => %d\n", t, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("key: %d offsetFromIndex %d n.Size %d sizeFromIndex:%d\n", key, offsetFromIndex, n.Size, sizeFromIndex)
|
|
|
|
|
2019-04-19 04:43:36 +00:00
|
|
|
rest = needle.NeedleBodyLength(sizeFromIndex, version)
|
2015-03-17 18:15:37 +00:00
|
|
|
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("Recovered in f", r)
|
|
|
|
}
|
|
|
|
}()
|
2019-10-29 07:35:16 +00:00
|
|
|
if _, err = n.ReadNeedleBody(datBackend, version, offset+int64(types.NeedleHeaderSize), rest); err != nil {
|
2015-03-17 18:15:37 +00:00
|
|
|
fmt.Printf("cannot read needle body: offset %d body %d %v\n", offset, rest, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-08-19 00:23:01 +00:00
|
|
|
if n.Size <= types.Size(n.DataSize) {
|
2015-03-17 18:15:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
visitNeedle(n, offset)
|
|
|
|
|
2019-04-19 07:39:34 +00:00
|
|
|
offset += types.NeedleHeaderSize + rest
|
2015-03-17 18:15:37 +00:00
|
|
|
//fmt.Printf("==> new entry offset %d\n", offset)
|
2019-10-29 07:35:16 +00:00
|
|
|
if n, _, rest, err = needle.ReadNeedleHeader(datBackend, version, offset); err != nil {
|
2015-03-17 18:15:37 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("cannot read needle header: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//fmt.Printf("new entry needle size:%d rest:%d\n", n.Size, rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|