You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

  1. package remote_storage
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. "google.golang.org/grpc"
  9. )
  10. const (
  11. SyncKeyPrefix = "remote.sync."
  12. )
  13. func GetSyncOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, dir string) (lastOffsetTsNs int64, readErr error) {
  14. dirHash := uint32(util.HashStringToLong(dir))
  15. readErr = pb.WithFilerClient(false, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  16. syncKey := []byte(SyncKeyPrefix + "____")
  17. util.Uint32toBytes(syncKey[len(SyncKeyPrefix):len(SyncKeyPrefix)+4], dirHash)
  18. resp, err := client.KvGet(context.Background(), &filer_pb.KvGetRequest{Key: syncKey})
  19. if err != nil {
  20. return err
  21. }
  22. if len(resp.Error) != 0 {
  23. return errors.New(resp.Error)
  24. }
  25. if len(resp.Value) < 8 {
  26. return nil
  27. }
  28. lastOffsetTsNs = int64(util.BytesToUint64(resp.Value))
  29. return nil
  30. })
  31. return
  32. }
  33. func SetSyncOffset(grpcDialOption grpc.DialOption, filer pb.ServerAddress, dir string, offsetTsNs int64) error {
  34. dirHash := uint32(util.HashStringToLong(dir))
  35. return pb.WithFilerClient(false, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  36. syncKey := []byte(SyncKeyPrefix + "____")
  37. util.Uint32toBytes(syncKey[len(SyncKeyPrefix):len(SyncKeyPrefix)+4], dirHash)
  38. valueBuf := make([]byte, 8)
  39. util.Uint64toBytes(valueBuf, uint64(offsetTsNs))
  40. resp, err := client.KvPut(context.Background(), &filer_pb.KvPutRequest{
  41. Key: syncKey,
  42. Value: valueBuf,
  43. })
  44. if err != nil {
  45. return err
  46. }
  47. if len(resp.Error) != 0 {
  48. return errors.New(resp.Error)
  49. }
  50. return nil
  51. })
  52. }