volume_tailer.go 1.8 KB

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