2021-09-01 08:29:22 +00:00
|
|
|
package remote_storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2021-09-01 08:29:22 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SyncKeyPrefix = "remote.sync."
|
|
|
|
)
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func GetSyncOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, dir string) (lastOffsetTsNs int64, readErr error) {
|
2021-09-01 08:29:22 +00:00
|
|
|
|
|
|
|
dirHash := uint32(util.HashStringToLong(dir))
|
|
|
|
|
2023-01-20 09:48:12 +00:00
|
|
|
readErr = pb.WithFilerClient(false, 0, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-01 08:29:22 +00:00
|
|
|
syncKey := []byte(SyncKeyPrefix + "____")
|
|
|
|
util.Uint32toBytes(syncKey[len(SyncKeyPrefix):len(SyncKeyPrefix)+4], dirHash)
|
|
|
|
|
|
|
|
resp, err := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: syncKey})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Error) != 0 {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
if len(resp.Value) < 8 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lastOffsetTsNs = int64(util.BytesToUint64(resp.Value))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func SetSyncOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, dir string, offsetTsNs int64) error {
|
2021-09-01 08:29:22 +00:00
|
|
|
|
|
|
|
dirHash := uint32(util.HashStringToLong(dir))
|
|
|
|
|
2023-01-20 09:48:12 +00:00
|
|
|
return pb.WithFilerClient(false, 0, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-01 08:29:22 +00:00
|
|
|
|
|
|
|
syncKey := []byte(SyncKeyPrefix + "____")
|
|
|
|
util.Uint32toBytes(syncKey[len(SyncKeyPrefix):len(SyncKeyPrefix)+4], dirHash)
|
|
|
|
|
|
|
|
valueBuf := make([]byte, 8)
|
|
|
|
util.Uint64toBytes(valueBuf, uint64(offsetTsNs))
|
|
|
|
|
|
|
|
resp, err := client.KvPut(context.Background(), &filer_pb.KvPutRequest{
|
|
|
|
Key: syncKey,
|
|
|
|
Value: valueBuf,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Error) != 0 {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|