volume_tailer.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "flag"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "log"
  6. "time"
  7. "context"
  8. "github.com/seaweedfs/seaweedfs/weed/operation"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  11. util2 "github.com/seaweedfs/seaweedfs/weed/util"
  12. "golang.org/x/tools/godoc/util"
  13. )
  14. var (
  15. master = flag.String("master", "localhost:9333", "master server host and port")
  16. volumeId = flag.Int("volumeId", -1, "a volume id")
  17. rewindDuration = flag.Duration("rewind", -1, "rewind back in time. -1 means from the first entry. 0 means from now.")
  18. timeoutSeconds = flag.Int("timeoutSeconds", 0, "disconnect if no activity after these seconds")
  19. showTextFile = flag.Bool("showTextFile", false, "display textual file content")
  20. )
  21. func main() {
  22. flag.Parse()
  23. util2.LoadConfiguration("security", false)
  24. grpcDialOption := security.LoadClientTLS(util2.GetViper(), "grpc.client")
  25. vid := needle.VolumeId(*volumeId)
  26. var sinceTimeNs int64
  27. if *rewindDuration == 0 {
  28. sinceTimeNs = time.Now().UnixNano()
  29. } else if *rewindDuration == -1 {
  30. sinceTimeNs = 0
  31. } else if *rewindDuration > 0 {
  32. sinceTimeNs = time.Now().Add(-*rewindDuration).UnixNano()
  33. }
  34. err := operation.TailVolume(func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*master) }, grpcDialOption, vid, uint64(sinceTimeNs), *timeoutSeconds, func(n *needle.Needle) (err error) {
  35. if n.Size == 0 {
  36. println("-", n.String())
  37. return nil
  38. } else {
  39. println("+", n.String())
  40. }
  41. if *showTextFile {
  42. data := n.Data
  43. if n.IsCompressed() {
  44. if data, err = util2.DecompressData(data); err != nil {
  45. return err
  46. }
  47. }
  48. if util.IsText(data) {
  49. println(string(data))
  50. }
  51. println("-", n.String(), "compressed", n.IsCompressed(), "original size", len(data))
  52. }
  53. return nil
  54. })
  55. if err != nil {
  56. log.Printf("Error VolumeTailSender volume %d: %v", vid, err)
  57. }
  58. }