2022-07-17 01:49:34 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
2023-08-21 13:53:05 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
2022-08-18 03:42:03 +08:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2022-07-17 01:49:34 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-08-21 13:53:05 +08:00
|
|
|
func (broker *MessageQueueBroker) checkSegmentOnFiler(segment *topic.Segment) (brokers []pb.ServerAddress, err error) {
|
|
|
|
info, found, err := broker.readSegmentInfoOnFiler(segment)
|
2022-07-17 01:49:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, b := range info.Brokers {
|
|
|
|
brokers = append(brokers, pb.ServerAddress(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:53:05 +08:00
|
|
|
func (broker *MessageQueueBroker) saveSegmentBrokersOnFiler(segment *topic.Segment, brokers []pb.ServerAddress) (err error) {
|
2022-07-17 01:49:34 +08:00
|
|
|
var nodes []string
|
|
|
|
for _, b := range brokers {
|
|
|
|
nodes = append(nodes, string(b))
|
|
|
|
}
|
2023-08-21 13:53:05 +08:00
|
|
|
broker.saveSegmentInfoToFiler(segment, &mq_pb.SegmentInfo{
|
2022-07-17 01:49:34 +08:00
|
|
|
Segment: segment.ToPbSegment(),
|
|
|
|
StartTsNs: time.Now().UnixNano(),
|
|
|
|
Brokers: nodes,
|
|
|
|
StopTsNs: 0,
|
|
|
|
PreviousSegments: nil,
|
|
|
|
NextSegments: nil,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:53:05 +08:00
|
|
|
func (broker *MessageQueueBroker) readSegmentInfoOnFiler(segment *topic.Segment) (info *mq_pb.SegmentInfo, found bool, err error) {
|
2022-07-17 01:49:34 +08:00
|
|
|
dir, name := segment.DirAndName()
|
|
|
|
|
|
|
|
found, err = filer_pb.Exists(broker, dir, name, false)
|
|
|
|
if !found || err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-20 17:48:12 +08:00
|
|
|
err = pb.WithFilerClient(false, 0, broker.GetFiler(), broker.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2022-07-17 01:49:34 +08:00
|
|
|
// read filer conf first
|
|
|
|
data, err := filer.ReadInsideFiler(client, dir, name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ReadEntry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse into filer conf object
|
|
|
|
info = &mq_pb.SegmentInfo{}
|
2022-08-18 03:42:03 +08:00
|
|
|
if err = jsonpb.Unmarshal(data, info); err != nil {
|
2022-07-17 01:49:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:53:05 +08:00
|
|
|
func (broker *MessageQueueBroker) saveSegmentInfoToFiler(segment *topic.Segment, info *mq_pb.SegmentInfo) (err error) {
|
2022-07-17 01:49:34 +08:00
|
|
|
dir, name := segment.DirAndName()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
filer.ProtoToText(&buf, info)
|
|
|
|
|
2023-01-20 17:48:12 +08:00
|
|
|
err = pb.WithFilerClient(false, 0, broker.GetFiler(), broker.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2022-07-17 01:49:34 +08:00
|
|
|
// read filer conf first
|
|
|
|
err := filer.SaveInsideFiler(client, dir, name, buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save segment info: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|