2021-03-22 07:03:16 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-10-09 16:57:26 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
2023-10-09 16:57:26 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/server/constants"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
|
2022-04-18 02:35:43 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-10-16 03:38:46 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-03-22 07:03:16 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
2022-10-31 03:32:46 +00:00
|
|
|
"net/http"
|
2023-10-09 16:57:26 +00:00
|
|
|
"sync"
|
2022-10-28 19:12:20 +00:00
|
|
|
"time"
|
2021-03-22 07:03:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandVolumeCheckDisk{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeCheckDisk struct {
|
2023-10-09 16:57:26 +00:00
|
|
|
env *CommandEnv
|
|
|
|
writer io.Writer
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeCheckDisk) Name() string {
|
|
|
|
return "volume.check.disk"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeCheckDisk) Help() string {
|
2022-01-12 23:04:48 +00:00
|
|
|
return `check all replicated volumes to find and fix inconsistencies. It is optional and resource intensive.
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
How it works:
|
|
|
|
|
|
|
|
find all volumes that are replicated
|
|
|
|
for each volume id, if there are more than 2 replicas, find one pair with the largest 2 in file count.
|
|
|
|
for the pair volume A and B
|
|
|
|
append entries in A and not in B to B
|
|
|
|
append entries in B and not in A to A
|
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:57:26 +00:00
|
|
|
func (c *commandVolumeCheckDisk) getVolumeStatusFileCount(vid uint32, dn *master_pb.DataNodeInfo) (totalFileCount, deletedFileCount uint64) {
|
|
|
|
err := operation.WithVolumeServerClient(false, pb.NewServerAddressWithGrpcPort(dn.Id, int(dn.GrpcPort)), c.env.option.GrpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
|
|
|
resp, reqErr := volumeServerClient.VolumeStatus(context.Background(), &volume_server_pb.VolumeStatusRequest{
|
|
|
|
VolumeId: uint32(vid),
|
|
|
|
})
|
|
|
|
if resp != nil {
|
|
|
|
totalFileCount = resp.FileCount
|
|
|
|
deletedFileCount = resp.FileDeletedCount
|
|
|
|
}
|
|
|
|
return reqErr
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(c.writer, "getting number of files for volume id %d from volumes status: %+v\n", vid, err)
|
|
|
|
}
|
|
|
|
return totalFileCount, deletedFileCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeCheckDisk) eqVolumeFileCount(a, b *VolumeReplica) (bool, bool) {
|
|
|
|
var waitGroup sync.WaitGroup
|
|
|
|
var fileCountA, fileCountB, fileDeletedCountA, fileDeletedCountB uint64
|
|
|
|
waitGroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer waitGroup.Done()
|
|
|
|
fileCountA, fileDeletedCountA = c.getVolumeStatusFileCount(a.info.Id, a.location.dataNode)
|
|
|
|
}()
|
|
|
|
waitGroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer waitGroup.Done()
|
|
|
|
fileCountB, fileDeletedCountB = c.getVolumeStatusFileCount(b.info.Id, b.location.dataNode)
|
|
|
|
}()
|
|
|
|
// Trying to synchronize a remote call to two nodes
|
|
|
|
waitGroup.Wait()
|
|
|
|
return fileCountA == fileCountB, fileDeletedCountA == fileDeletedCountB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeCheckDisk) shouldSkipVolume(a, b *VolumeReplica, pulseTimeAtSecond int64, syncDeletions, verbose bool) bool {
|
|
|
|
doSyncDeletedCount := false
|
|
|
|
if syncDeletions && a.info.DeleteCount != b.info.DeleteCount {
|
|
|
|
doSyncDeletedCount = true
|
|
|
|
}
|
|
|
|
if (a.info.FileCount != b.info.FileCount) || doSyncDeletedCount {
|
|
|
|
// Do synchronization of volumes, if the modification time was before the last pulsation time
|
|
|
|
if a.info.ModifiedAtSecond < pulseTimeAtSecond || b.info.ModifiedAtSecond < pulseTimeAtSecond {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if eqFileCount, eqDeletedFileCount := c.eqVolumeFileCount(a, b); eqFileCount {
|
|
|
|
if doSyncDeletedCount && !eqDeletedFileCount {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if verbose {
|
|
|
|
fmt.Fprintf(c.writer, "skipping active volumes %d with the same file counts on %s and %s\n",
|
|
|
|
a.info.Id, a.location.dataNode.Id, b.location.dataNode.Id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-22 07:03:16 +00:00
|
|
|
func (c *commandVolumeCheckDisk) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
|
|
|
fsckCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2021-03-23 04:01:43 +00:00
|
|
|
slowMode := fsckCommand.Bool("slow", false, "slow mode checks all replicas even file counts are the same")
|
2021-03-22 07:03:16 +00:00
|
|
|
verbose := fsckCommand.Bool("v", false, "verbose mode")
|
2022-04-25 13:27:14 +00:00
|
|
|
volumeId := fsckCommand.Uint("volumeId", 0, "the volume id")
|
2021-03-22 07:03:16 +00:00
|
|
|
applyChanges := fsckCommand.Bool("force", false, "apply the fix")
|
2023-09-28 06:12:10 +00:00
|
|
|
syncDeletions := fsckCommand.Bool("syncDeleted", false, "sync of deletions the fix")
|
2021-03-24 17:07:13 +00:00
|
|
|
nonRepairThreshold := fsckCommand.Float64("nonRepairThreshold", 0.3, "repair when missing keys is not more than this limit")
|
2021-03-22 07:03:16 +00:00
|
|
|
if err = fsckCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-31 21:48:46 +00:00
|
|
|
infoAboutSimulationMode(writer, *applyChanges, "-force")
|
2021-03-22 07:03:16 +00:00
|
|
|
|
2021-12-10 21:24:38 +00:00
|
|
|
if err = commandEnv.confirmIsLocked(args); err != nil {
|
2021-09-14 05:13:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-22 07:03:16 +00:00
|
|
|
c.env = commandEnv
|
2023-10-09 16:57:26 +00:00
|
|
|
c.writer = writer
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
// collect topology information
|
2023-10-09 16:57:26 +00:00
|
|
|
pulseTimeAtSecond := time.Now().Unix() - constants.VolumePulseSeconds*2
|
2022-02-08 08:53:55 +00:00
|
|
|
topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
|
2021-03-22 07:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
volumeReplicas, _ := collectVolumeReplicaLocations(topologyInfo)
|
|
|
|
|
|
|
|
// pick 1 pairs of volume replica
|
|
|
|
for _, replicas := range volumeReplicas {
|
2022-04-25 13:27:14 +00:00
|
|
|
if *volumeId > 0 && replicas[0].info.Id != uint32(*volumeId) {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-25 16:35:16 +00:00
|
|
|
slices.SortFunc(replicas, func(a, b *VolumeReplica) int {
|
2023-10-09 16:57:26 +00:00
|
|
|
return int(b.info.FileCount - a.info.FileCount)
|
2021-03-22 07:03:16 +00:00
|
|
|
})
|
|
|
|
for len(replicas) >= 2 {
|
|
|
|
a, b := replicas[0], replicas[1]
|
2023-10-09 16:57:26 +00:00
|
|
|
replicas = replicas[1:]
|
2021-03-22 07:03:16 +00:00
|
|
|
if a.info.ReadOnly || b.info.ReadOnly {
|
2023-10-09 16:57:26 +00:00
|
|
|
fmt.Fprintf(writer, "skipping readonly volume %d on %s and %s\n",
|
|
|
|
a.info.Id, a.location.dataNode.Id, b.location.dataNode.Id)
|
2021-03-22 07:03:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-10-09 16:57:26 +00:00
|
|
|
if !*slowMode && c.shouldSkipVolume(a, b, pulseTimeAtSecond, *syncDeletions, *verbose) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.syncTwoReplicas(a, b, *applyChanges, *syncDeletions, *nonRepairThreshold, *verbose); err != nil {
|
2021-07-31 13:23:52 +00:00
|
|
|
fmt.Fprintf(writer, "sync volume %d on %s and %s: %v\n", a.info.Id, a.location.dataNode.Id, b.location.dataNode.Id, err)
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:57:26 +00:00
|
|
|
func (c *commandVolumeCheckDisk) syncTwoReplicas(a *VolumeReplica, b *VolumeReplica, applyChanges bool, doSyncDeletions bool, nonRepairThreshold float64, verbose bool) (err error) {
|
2021-07-16 09:03:32 +00:00
|
|
|
aHasChanges, bHasChanges := true, true
|
|
|
|
for aHasChanges || bHasChanges {
|
2023-10-09 16:57:26 +00:00
|
|
|
if aHasChanges, bHasChanges, err = c.checkBoth(a, b, applyChanges, doSyncDeletions, nonRepairThreshold, verbose); err != nil {
|
2022-01-12 22:59:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:57:26 +00:00
|
|
|
func (c *commandVolumeCheckDisk) checkBoth(a *VolumeReplica, b *VolumeReplica, applyChanges bool, doSyncDeletions bool, nonRepairThreshold float64, verbose bool) (aHasChanges bool, bHasChanges bool, err error) {
|
2022-01-12 22:59:29 +00:00
|
|
|
aDB, bDB := needle_map.NewMemDb(), needle_map.NewMemDb()
|
|
|
|
defer func() {
|
2021-07-16 09:03:32 +00:00
|
|
|
aDB.Close()
|
|
|
|
bDB.Close()
|
2022-01-12 22:59:29 +00:00
|
|
|
}()
|
2021-07-16 09:03:32 +00:00
|
|
|
|
2022-01-12 22:59:29 +00:00
|
|
|
// read index db
|
2022-10-28 19:12:20 +00:00
|
|
|
readIndexDbCutoffFrom := uint64(time.Now().UnixNano())
|
2023-10-09 16:57:26 +00:00
|
|
|
if err = readIndexDatabase(aDB, a.info.Collection, a.info.Id, pb.NewServerAddressFromDataNode(a.location.dataNode), verbose, c.writer, c.env.option.GrpcDialOption); err != nil {
|
2022-01-12 22:59:29 +00:00
|
|
|
return true, true, fmt.Errorf("readIndexDatabase %s volume %d: %v", a.location.dataNode, a.info.Id, err)
|
|
|
|
}
|
2023-10-09 16:57:26 +00:00
|
|
|
if err := readIndexDatabase(bDB, b.info.Collection, b.info.Id, pb.NewServerAddressFromDataNode(b.location.dataNode), verbose, c.writer, c.env.option.GrpcDialOption); err != nil {
|
2022-01-12 22:59:29 +00:00
|
|
|
return true, true, fmt.Errorf("readIndexDatabase %s volume %d: %v", b.location.dataNode, b.info.Id, err)
|
|
|
|
}
|
2021-07-16 09:03:32 +00:00
|
|
|
|
2022-01-12 22:59:29 +00:00
|
|
|
// find and make up the differences
|
2023-10-09 16:57:26 +00:00
|
|
|
if aHasChanges, err = doVolumeCheckDisk(bDB, aDB, b, a, verbose, c.writer, applyChanges, doSyncDeletions, nonRepairThreshold, readIndexDbCutoffFrom, c.env.option.GrpcDialOption); err != nil {
|
2022-09-05 14:49:26 +00:00
|
|
|
return true, true, fmt.Errorf("doVolumeCheckDisk source:%s target:%s volume %d: %v", b.location.dataNode.Id, a.location.dataNode.Id, b.info.Id, err)
|
2021-07-16 09:03:32 +00:00
|
|
|
}
|
2023-10-09 16:57:26 +00:00
|
|
|
if bHasChanges, err = doVolumeCheckDisk(aDB, bDB, a, b, verbose, c.writer, applyChanges, doSyncDeletions, nonRepairThreshold, readIndexDbCutoffFrom, c.env.option.GrpcDialOption); err != nil {
|
2022-09-05 14:49:26 +00:00
|
|
|
return true, true, fmt.Errorf("doVolumeCheckDisk source:%s target:%s volume %d: %v", a.location.dataNode.Id, b.location.dataNode.Id, a.info.Id, err)
|
2022-01-12 22:59:29 +00:00
|
|
|
}
|
|
|
|
return
|
2021-07-16 09:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
func doVolumeCheckDisk(minuend, subtrahend *needle_map.MemDb, source, target *VolumeReplica, verbose bool, writer io.Writer, applyChanges bool, doSyncDeletions bool, nonRepairThreshold float64, cutoffFromAtNs uint64, grpcDialOption grpc.DialOption) (hasChanges bool, err error) {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
// find missing keys
|
|
|
|
// hash join, can be more efficient
|
|
|
|
var missingNeedles []needle_map.NeedleValue
|
2022-10-31 03:32:46 +00:00
|
|
|
var partiallyDeletedNeedles []needle_map.NeedleValue
|
2021-03-22 07:03:16 +00:00
|
|
|
var counter int
|
2022-10-28 19:12:20 +00:00
|
|
|
doCutoffOfLastNeedle := true
|
2022-10-31 03:32:46 +00:00
|
|
|
minuend.DescendingVisit(func(minuendValue needle_map.NeedleValue) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
counter++
|
2022-10-31 03:32:46 +00:00
|
|
|
if subtrahendValue, found := subtrahend.Get(minuendValue.Key); !found {
|
|
|
|
if minuendValue.Size.IsDeleted() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-28 19:12:20 +00:00
|
|
|
if doCutoffOfLastNeedle {
|
2023-09-28 06:12:10 +00:00
|
|
|
if needleMeta, err := readNeedleMeta(grpcDialOption, pb.NewServerAddressFromDataNode(source.location.dataNode), source.info.Id, minuendValue); err == nil {
|
2022-10-28 19:12:20 +00:00
|
|
|
// needles older than the cutoff time are not missing yet
|
|
|
|
if needleMeta.AppendAtNs > cutoffFromAtNs {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
doCutoffOfLastNeedle = false
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 03:32:46 +00:00
|
|
|
missingNeedles = append(missingNeedles, minuendValue)
|
|
|
|
} else {
|
|
|
|
if minuendValue.Size.IsDeleted() && !subtrahendValue.Size.IsDeleted() {
|
|
|
|
partiallyDeletedNeedles = append(partiallyDeletedNeedles, minuendValue)
|
|
|
|
}
|
|
|
|
if doCutoffOfLastNeedle {
|
|
|
|
doCutoffOfLastNeedle = false
|
|
|
|
}
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-10-31 03:32:46 +00:00
|
|
|
fmt.Fprintf(writer, "volume %d %s has %d entries, %s missed %d and partially deleted %d entries\n",
|
|
|
|
source.info.Id, source.location.dataNode.Id, counter, target.location.dataNode.Id, len(missingNeedles), len(partiallyDeletedNeedles))
|
2021-03-24 17:51:22 +00:00
|
|
|
|
2022-10-31 03:32:46 +00:00
|
|
|
if counter == 0 || (len(missingNeedles) == 0 && len(partiallyDeletedNeedles) == 0) {
|
2021-07-13 18:19:56 +00:00
|
|
|
return false, nil
|
2021-03-24 17:51:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:42:35 +00:00
|
|
|
missingNeedlesFraction := float64(len(missingNeedles)) / float64(counter)
|
2021-03-24 17:07:13 +00:00
|
|
|
if missingNeedlesFraction > nonRepairThreshold {
|
2021-07-13 18:19:56 +00:00
|
|
|
return false, fmt.Errorf(
|
2021-03-23 10:04:07 +00:00
|
|
|
"failed to start repair volume %d, percentage of missing keys is greater than the threshold: %.2f > %.2f",
|
2021-03-24 17:07:13 +00:00
|
|
|
source.info.Id, missingNeedlesFraction, nonRepairThreshold)
|
2021-03-23 10:04:07 +00:00
|
|
|
}
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
for _, needleValue := range missingNeedles {
|
2023-09-28 06:12:10 +00:00
|
|
|
needleBlob, err := readSourceNeedleBlob(grpcDialOption, pb.NewServerAddressFromDataNode(source.location.dataNode), source.info.Id, needleValue)
|
2021-03-22 07:03:16 +00:00
|
|
|
if err != nil {
|
2021-07-13 18:40:21 +00:00
|
|
|
return hasChanges, err
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !applyChanges {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
2022-10-31 03:32:46 +00:00
|
|
|
fmt.Fprintf(writer, "read %s %s => %s\n", needleValue.Key.FileId(source.info.Id), source.location.dataNode.Id, target.location.dataNode.Id)
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:19:56 +00:00
|
|
|
hasChanges = true
|
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
if err = writeNeedleBlobToTarget(grpcDialOption, pb.NewServerAddressFromDataNode(target.location.dataNode), source.info.Id, needleValue, needleBlob); err != nil {
|
2021-07-13 18:40:21 +00:00
|
|
|
return hasChanges, err
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:15:20 +00:00
|
|
|
if doSyncDeletions && applyChanges && len(partiallyDeletedNeedles) > 0 {
|
2022-10-31 03:32:46 +00:00
|
|
|
var fidList []string
|
|
|
|
for _, needleValue := range partiallyDeletedNeedles {
|
|
|
|
fidList = append(fidList, needleValue.Key.FileId(source.info.Id))
|
|
|
|
if verbose {
|
|
|
|
fmt.Fprintf(writer, "delete %s %s => %s\n", needleValue.Key.FileId(source.info.Id), source.location.dataNode.Id, target.location.dataNode.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteResults, deleteErr := operation.DeleteFilesAtOneVolumeServer(
|
|
|
|
pb.NewServerAddressFromDataNode(target.location.dataNode),
|
2023-09-28 06:12:10 +00:00
|
|
|
grpcDialOption, fidList, false)
|
2022-10-31 03:32:46 +00:00
|
|
|
if deleteErr != nil {
|
|
|
|
return hasChanges, deleteErr
|
|
|
|
}
|
|
|
|
for _, deleteResult := range deleteResults {
|
2023-10-12 15:15:20 +00:00
|
|
|
if deleteResult.Status == http.StatusAccepted && deleteResult.Size > 0 {
|
2022-10-31 03:32:46 +00:00
|
|
|
hasChanges = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 18:19:56 +00:00
|
|
|
return
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 03:38:46 +00:00
|
|
|
func readSourceNeedleBlob(grpcDialOption grpc.DialOption, sourceVolumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (needleBlob []byte, err error) {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
2022-10-16 03:38:46 +00:00
|
|
|
err = operation.WithVolumeServerClient(false, sourceVolumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
resp, err := client.ReadNeedleBlob(context.Background(), &volume_server_pb.ReadNeedleBlobRequest{
|
|
|
|
VolumeId: volumeId,
|
2021-03-23 10:04:07 +00:00
|
|
|
Offset: needleValue.Offset.ToActualOffset(),
|
|
|
|
Size: int32(needleValue.Size),
|
2021-03-22 07:03:16 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
needleBlob = resp.NeedleBlob
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
func writeNeedleBlobToTarget(grpcDialOption grpc.DialOption, targetVolumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue, needleBlob []byte) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
return operation.WithVolumeServerClient(false, targetVolumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
_, err := client.WriteNeedleBlob(context.Background(), &volume_server_pb.WriteNeedleBlobRequest{
|
2021-03-23 10:04:07 +00:00
|
|
|
VolumeId: volumeId,
|
|
|
|
NeedleId: uint64(needleValue.Key),
|
|
|
|
Size: int32(needleValue.Size),
|
2021-03-22 07:03:16 +00:00
|
|
|
NeedleBlob: needleBlob,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
func readIndexDatabase(db *needle_map.MemDb, collection string, volumeId uint32, volumeServer pb.ServerAddress, verbose bool, writer io.Writer, grpcDialOption grpc.DialOption) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2023-09-28 06:12:10 +00:00
|
|
|
if err := copyVolumeIndexFile(collection, volumeId, volumeServer, &buf, verbose, writer, grpcDialOption); err != nil {
|
2021-03-22 07:03:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
fmt.Fprintf(writer, "load collection %s volume %d index size %d from %s ...\n", collection, volumeId, buf.Len(), volumeServer)
|
|
|
|
}
|
2022-10-31 03:32:46 +00:00
|
|
|
return db.LoadFilterFromReaderAt(bytes.NewReader(buf.Bytes()), true, false)
|
2021-03-22 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
func copyVolumeIndexFile(collection string, volumeId uint32, volumeServer pb.ServerAddress, buf *bytes.Buffer, verbose bool, writer io.Writer, grpcDialOption grpc.DialOption) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
2023-09-28 06:12:10 +00:00
|
|
|
return operation.WithVolumeServerClient(true, volumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
2021-03-22 07:03:16 +00:00
|
|
|
|
|
|
|
ext := ".idx"
|
|
|
|
|
|
|
|
copyFileClient, err := volumeServerClient.CopyFile(context.Background(), &volume_server_pb.CopyFileRequest{
|
|
|
|
VolumeId: volumeId,
|
|
|
|
Ext: ".idx",
|
|
|
|
CompactionRevision: math.MaxUint32,
|
|
|
|
StopOffset: math.MaxInt64,
|
|
|
|
Collection: collection,
|
|
|
|
IsEcVolume: false,
|
|
|
|
IgnoreSourceFileNotFound: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start copying volume %d%s: %v", volumeId, ext, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeToBuffer(copyFileClient, buf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to copy %d%s from %s: %v", volumeId, ext, volumeServer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeToBuffer(client volume_server_pb.VolumeServer_CopyFileClient, buf *bytes.Buffer) error {
|
|
|
|
for {
|
|
|
|
resp, receiveErr := client.Recv()
|
|
|
|
if receiveErr == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if receiveErr != nil {
|
|
|
|
return fmt.Errorf("receiving: %v", receiveErr)
|
|
|
|
}
|
|
|
|
buf.Write(resp.FileContent)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|