seaweedfs/weed/messaging/broker/broker_grpc_server_publish.go

113 lines
2.4 KiB
Go
Raw Normal View History

2020-04-18 08:12:01 +00:00
package broker
2020-04-18 05:39:21 +00:00
import (
"crypto/md5"
"fmt"
2020-04-18 05:39:21 +00:00
"io"
"github.com/golang/protobuf/proto"
2020-09-01 07:21:19 +00:00
"github.com/chrislusf/seaweedfs/weed/filer"
2020-04-18 05:39:21 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-04-18 05:39:21 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
)
func (broker *MessageBroker) Publish(stream messaging_pb.SeaweedMessaging_PublishServer) error {
// process initial request
in, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
// TODO look it up
topicConfig := &messaging_pb.TopicConfiguration{
2020-04-30 10:05:34 +00:00
// IsTransient: true,
2020-04-18 05:39:21 +00:00
}
2020-04-19 07:18:32 +00:00
// send init response
initResponse := &messaging_pb.PublishResponse{
Config: nil,
Redirect: nil,
}
err = stream.Send(initResponse)
if err != nil {
return err
}
if initResponse.Redirect != nil {
return nil
}
2020-04-18 05:39:21 +00:00
2020-04-18 08:12:01 +00:00
// get lock
tp := TopicPartition{
Namespace: in.Init.Namespace,
Topic: in.Init.Topic,
Partition: in.Init.Partition,
}
2020-09-01 07:21:19 +00:00
tpDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, tp.Namespace, tp.Topic)
md5File := fmt.Sprintf("p%02d.md5", tp.Partition)
// println("chan data stored under", tpDir, "as", md5File)
if exists, err := filer_pb.Exists(broker, tpDir, md5File, false); err == nil && exists {
return fmt.Errorf("channel is already closed")
}
2020-05-12 15:48:00 +00:00
tl := broker.topicManager.RequestLock(tp, topicConfig, true)
defer broker.topicManager.ReleaseLock(tp, true)
2020-04-18 08:12:01 +00:00
md5hash := md5.New()
2020-04-18 08:12:01 +00:00
// process each message
2020-04-18 05:39:21 +00:00
for {
2020-05-08 09:47:22 +00:00
// println("recv")
2020-04-18 05:39:21 +00:00
in, err := stream.Recv()
2020-05-08 09:47:22 +00:00
// glog.V(0).Infof("recieved %v err: %v", in, err)
2020-04-18 05:39:21 +00:00
if err == io.EOF {
return nil
}
if err != nil {
return err
}
2020-04-18 08:12:01 +00:00
if in.Data == nil {
continue
}
// fmt.Printf("received: %d : %s\n", len(in.Data.Value), string(in.Data.Value))
data, err := proto.Marshal(in.Data)
2020-04-18 05:39:21 +00:00
if err != nil {
glog.Errorf("marshall error: %v\n", err)
continue
}
tl.logBuffer.AddToBuffer(in.Data.Key, data, in.Data.EventTimeNs)
2020-04-18 05:39:21 +00:00
2020-05-08 09:47:22 +00:00
if in.Data.IsClose {
// println("server received closing")
break
}
md5hash.Write(in.Data.Value)
}
if err := broker.appendToFile(tpDir+"/"+md5File, topicConfig, md5hash.Sum(nil)); err != nil {
glog.V(0).Infof("err writing %s: %v", md5File, err)
2020-04-18 05:39:21 +00:00
}
2020-05-08 09:47:22 +00:00
// fmt.Printf("received md5 %X\n", md5hash.Sum(nil))
2020-05-08 09:47:22 +00:00
// send the close ack
// println("server send ack closing")
if err := stream.Send(&messaging_pb.PublishResponse{IsClosed: true}); err != nil {
glog.V(0).Infof("err sending close response: %v", err)
}
return nil
2020-04-18 05:39:21 +00:00
}