seaweedfs/unmaintained/volume_tailer/volume_tailer.go

133 lines
3 KiB
Go
Raw Normal View History

2019-04-18 18:05:02 +00:00
package main
import (
"context"
"flag"
2019-04-19 03:04:44 +00:00
"fmt"
2019-04-18 18:05:02 +00:00
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
"github.com/chrislusf/seaweedfs/weed/security"
weed_server "github.com/chrislusf/seaweedfs/weed/server"
"github.com/chrislusf/seaweedfs/weed/storage"
"github.com/spf13/viper"
2019-04-19 02:22:13 +00:00
"golang.org/x/tools/godoc/util"
2019-04-19 03:04:44 +00:00
"google.golang.org/grpc"
2019-04-18 18:05:02 +00:00
"io"
"log"
)
var (
master = flag.String("master", "localhost:9333", "master server host and port")
volumeId = flag.Int("volumeId", -1, "a volume id")
timeoutSeconds = flag.Int("timeoutSeconds", 0, "disconnect if no activity after these seconds")
2019-04-19 02:22:13 +00:00
showTextFile = flag.Bool("showTextFile", false, "display textual file content")
2019-04-18 18:05:02 +00:00
)
func main() {
flag.Parse()
weed_server.LoadConfiguration("security", false)
grpcDialOption := security.LoadClientTLS(viper.Sub("grpc"), "client")
vid := storage.VolumeId(*volumeId)
2019-04-19 03:04:44 +00:00
err := TailVolume(*master, grpcDialOption, vid, func(n *storage.Needle) (err error) {
if n.Size == 0 {
println("-", n.String())
return nil
} else {
println("+", n.String())
}
if *showTextFile {
data := n.Data
if n.IsGzipped() {
if data, err = operation.UnGzipData(data); err != nil {
return err
}
}
if util.IsText(data) {
println(string(data))
}
println("-", n.String(), "compressed", n.IsGzipped(), "original size", len(data))
}
return nil
})
if err != nil {
log.Printf("Error VolumeTail volume %d: %v", vid, err)
}
}
func TailVolume(master string, grpcDialOption grpc.DialOption, vid storage.VolumeId, fn func(n *storage.Needle) error) error {
2019-04-18 18:05:02 +00:00
// find volume location, replication, ttl info
2019-04-19 03:04:44 +00:00
lookup, err := operation.Lookup(master, vid.String())
2019-04-18 18:05:02 +00:00
if err != nil {
2019-04-19 03:04:44 +00:00
return fmt.Errorf("Error looking up volume %d: %v", vid, err)
2019-04-18 18:05:02 +00:00
}
2019-04-19 03:04:44 +00:00
if len(lookup.Locations) == 0 {
return fmt.Errorf("unable to locate volume %d", vid)
}
2019-04-18 18:05:02 +00:00
volumeServer := lookup.Locations[0].Url
2019-04-19 03:04:44 +00:00
return operation.WithVolumeServerClient(volumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
2019-04-18 18:05:02 +00:00
stream, err := client.VolumeTail(context.Background(), &volume_server_pb.VolumeTailRequest{
VolumeId: uint32(vid),
SinceNs: 0,
DrainingSeconds: uint32(*timeoutSeconds),
})
if err != nil {
return err
}
for {
resp, recvErr := stream.Recv()
if recvErr != nil {
if recvErr == io.EOF {
break
} else {
return recvErr
}
}
2019-04-19 02:22:13 +00:00
needleHeader := resp.NeedleHeader
needleBody := resp.NeedleBody
if len(needleHeader) == 0 {
continue
}
for !resp.IsLastChunk {
resp, recvErr = stream.Recv()
if recvErr != nil {
if recvErr == io.EOF {
break
} else {
return recvErr
}
}
needleBody = append(needleBody, resp.NeedleBody...)
}
n := new(storage.Needle)
n.ParseNeedleHeader(needleHeader)
n.ReadNeedleBodyBytes(needleBody, storage.CurrentVersion)
2019-04-19 03:04:44 +00:00
err = fn(n)
2019-04-19 02:22:13 +00:00
2019-04-19 03:04:44 +00:00
if err != nil {
return err
2019-04-19 02:22:13 +00:00
}
2019-04-18 18:05:02 +00:00
}
return nil
})
}