filer_client_accessor.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package filer_client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  8. "github.com/seaweedfs/seaweedfs/weed/pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  11. "google.golang.org/grpc"
  12. jsonpb "google.golang.org/protobuf/encoding/protojson"
  13. )
  14. type FilerClientAccessor struct {
  15. GetFiler func() pb.ServerAddress
  16. GetGrpcDialOption func() grpc.DialOption
  17. }
  18. func (fca *FilerClientAccessor) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  19. return pb.WithFilerClient(streamingMode, 0, fca.GetFiler(), fca.GetGrpcDialOption(), fn)
  20. }
  21. func (fca *FilerClientAccessor) SaveTopicConfToFiler(t *mq_pb.Topic, conf *mq_pb.ConfigureTopicResponse) error {
  22. glog.V(0).Infof("save conf for topic %v to filer", t)
  23. // save the topic configuration on filer
  24. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  25. if err := fca.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  26. var buf bytes.Buffer
  27. filer.ProtoToText(&buf, conf)
  28. return filer.SaveInsideFiler(client, topicDir, "topic.conf", buf.Bytes())
  29. }); err != nil {
  30. return fmt.Errorf("save topic to %s: %v", topicDir, err)
  31. }
  32. return nil
  33. }
  34. func (fca *FilerClientAccessor) ReadTopicConfFromFiler(t topic.Topic) (conf *mq_pb.ConfigureTopicResponse, err error) {
  35. glog.V(0).Infof("load conf for topic %v from filer", t)
  36. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  37. if err = fca.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  38. data, err := filer.ReadInsideFiler(client, topicDir, "topic.conf")
  39. if err == filer_pb.ErrNotFound {
  40. return err
  41. }
  42. if err != nil {
  43. return fmt.Errorf("read topic.conf of %v: %v", t, err)
  44. }
  45. // parse into filer conf object
  46. conf = &mq_pb.ConfigureTopicResponse{}
  47. if err = jsonpb.Unmarshal(data, conf); err != nil {
  48. return fmt.Errorf("unmarshal topic %v conf: %v", t, err)
  49. }
  50. return nil
  51. }); err != nil {
  52. return nil, err
  53. }
  54. return conf, nil
  55. }