chrislu
1 year ago
6 changed files with 77 additions and 164 deletions
-
4weed/mq/client/cmd/weed_sub/subscriber.go
-
74weed/mq/client/sub_client/connect_to_sub_coordinator.go
-
34weed/mq/client/sub_client/lookup.go
-
85weed/mq/client/sub_client/process.go
-
42weed/mq/client/sub_client/subscribe.go
-
2weed/mq/client/sub_client/subscriber.go
@ -1,34 +0,0 @@ |
|||||
package sub_client |
|
||||
|
|
||||
import ( |
|
||||
"context" |
|
||||
"fmt" |
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb" |
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" |
|
||||
) |
|
||||
|
|
||||
func (sub *TopicSubscriber) doLookup(brokerAddress string) error { |
|
||||
err := pb.WithBrokerGrpcClient(true, |
|
||||
brokerAddress, |
|
||||
sub.SubscriberConfig.GrpcDialOption, |
|
||||
func(client mq_pb.SeaweedMessagingClient) error { |
|
||||
lookupResp, err := client.LookupTopicBrokers(context.Background(), |
|
||||
&mq_pb.LookupTopicBrokersRequest{ |
|
||||
Topic: &mq_pb.Topic{ |
|
||||
Namespace: sub.ContentConfig.Namespace, |
|
||||
Name: sub.ContentConfig.Topic, |
|
||||
}, |
|
||||
IsForPublish: false, |
|
||||
}) |
|
||||
if err != nil { |
|
||||
return err |
|
||||
} |
|
||||
sub.brokerPartitionAssignments = lookupResp.BrokerPartitionAssignments |
|
||||
return nil |
|
||||
}) |
|
||||
|
|
||||
if err != nil { |
|
||||
return fmt.Errorf("lookup topic %s/%s: %v", sub.ContentConfig.Namespace, sub.ContentConfig.Topic, err) |
|
||||
} |
|
||||
return nil |
|
||||
} |
|
@ -1,85 +0,0 @@ |
|||||
package sub_client |
|
||||
|
|
||||
import ( |
|
||||
"context" |
|
||||
"fmt" |
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb" |
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" |
|
||||
"sync" |
|
||||
) |
|
||||
|
|
||||
func (sub *TopicSubscriber) doProcess() error { |
|
||||
var wg sync.WaitGroup |
|
||||
for _, brokerPartitionAssignment := range sub.brokerPartitionAssignments { |
|
||||
brokerAddress := brokerPartitionAssignment.LeaderBroker |
|
||||
grpcConnection, err := pb.GrpcDial(context.Background(), brokerAddress, true, sub.SubscriberConfig.GrpcDialOption) |
|
||||
if err != nil { |
|
||||
return fmt.Errorf("dial broker %s: %v", brokerAddress, err) |
|
||||
} |
|
||||
brokerClient := mq_pb.NewSeaweedMessagingClient(grpcConnection) |
|
||||
subscribeClient, err := brokerClient.Subscribe(context.Background(), &mq_pb.SubscribeRequest{ |
|
||||
Message: &mq_pb.SubscribeRequest_Init{ |
|
||||
Init: &mq_pb.SubscribeRequest_InitMessage{ |
|
||||
ConsumerGroup: sub.SubscriberConfig.ConsumerGroup, |
|
||||
ConsumerId: sub.SubscriberConfig.ConsumerGroupInstanceId, |
|
||||
Topic: &mq_pb.Topic{ |
|
||||
Namespace: sub.ContentConfig.Namespace, |
|
||||
Name: sub.ContentConfig.Topic, |
|
||||
}, |
|
||||
Partition: &mq_pb.Partition{ |
|
||||
RingSize: brokerPartitionAssignment.Partition.RingSize, |
|
||||
RangeStart: brokerPartitionAssignment.Partition.RangeStart, |
|
||||
RangeStop: brokerPartitionAssignment.Partition.RangeStop, |
|
||||
}, |
|
||||
Filter: sub.ContentConfig.Filter, |
|
||||
Offset: &mq_pb.SubscribeRequest_InitMessage_StartTimestampNs{ |
|
||||
StartTimestampNs: sub.alreadyProcessedTsNs, |
|
||||
}, |
|
||||
}, |
|
||||
}, |
|
||||
}) |
|
||||
if err != nil { |
|
||||
return fmt.Errorf("create subscribe client: %v", err) |
|
||||
} |
|
||||
wg.Add(1) |
|
||||
go func() { |
|
||||
defer wg.Done() |
|
||||
if sub.OnCompletionFunc != nil { |
|
||||
defer sub.OnCompletionFunc() |
|
||||
} |
|
||||
defer func() { |
|
||||
subscribeClient.SendMsg(&mq_pb.SubscribeRequest{ |
|
||||
Message: &mq_pb.SubscribeRequest_Ack{ |
|
||||
Ack: &mq_pb.SubscribeRequest_AckMessage{ |
|
||||
Sequence: 0, |
|
||||
}, |
|
||||
}, |
|
||||
}) |
|
||||
subscribeClient.CloseSend() |
|
||||
}() |
|
||||
for { |
|
||||
resp, err := subscribeClient.Recv() |
|
||||
if err != nil { |
|
||||
fmt.Printf("subscribe error: %v\n", err) |
|
||||
return |
|
||||
} |
|
||||
if resp.Message == nil { |
|
||||
continue |
|
||||
} |
|
||||
switch m := resp.Message.(type) { |
|
||||
case *mq_pb.SubscribeResponse_Data: |
|
||||
if !sub.OnEachMessageFunc(m.Data.Key, m.Data.Value) { |
|
||||
return |
|
||||
} |
|
||||
sub.alreadyProcessedTsNs = m.Data.TsNs |
|
||||
case *mq_pb.SubscribeResponse_Ctrl: |
|
||||
if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic { |
|
||||
return |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
}() |
|
||||
} |
|
||||
wg.Wait() |
|
||||
return nil |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue