@@ -31,11 +31,11 @@ import (
3131 "github.com/apache/rocketmq-clients/golang/v5/pkg/ticker"
3232 "github.com/apache/rocketmq-clients/golang/v5/pkg/utils"
3333 v2 "github.com/apache/rocketmq-clients/golang/v5/protocol/v2"
34- "github.com/golang/protobuf/proto"
3534 "github.com/google/uuid"
3635 "go.uber.org/atomic"
3736 "go.uber.org/zap"
3837 "google.golang.org/grpc/metadata"
38+ "google.golang.org/protobuf/proto"
3939)
4040
4141type Client interface {
@@ -117,7 +117,7 @@ func (cs *defaultClientSession) startUp() {
117117 if err != nil {
118118 // we are recovering
119119 if ! cs .recovering {
120- cs .cli .log .Info ("Encountered error while receiving TelemetryCommand, trying to recover" )
120+ cs .cli .log .Infof ("Encountered error while receiving TelemetryCommand, trying to recover, err=%v" , err )
121121 // we wait five seconds to give time for the transmission error to be resolved externally before we attempt to read the message again.
122122 time .Sleep (cs .recoveryWaitTime )
123123 cs .recovering = true
@@ -176,13 +176,19 @@ func (cs *defaultClientSession) release() {
176176}
177177func (cs * defaultClientSession ) publish (ctx context.Context , common * v2.TelemetryCommand ) error {
178178 var err error
179- cs .observerLock .RLock ()
180- if cs .observer != nil {
181- err = cs .observer .Send (common )
182- cs .observerLock .RUnlock ()
179+
180+ f0 := func () (bool , error ) {
181+ cs .observerLock .RLock ()
182+ defer cs .observerLock .RUnlock ()
183+ if cs .observer != nil {
184+ return true , cs .observer .Send (common )
185+ }
186+ return false , nil
187+ }
188+ over , err := f0 ()
189+ if over {
183190 return err
184191 }
185- cs .observerLock .RUnlock ()
186192
187193 cs .observerLock .Lock ()
188194 defer cs .observerLock .Unlock ()
@@ -218,8 +224,8 @@ type defaultClient struct {
218224 endpointsTelemetryClientTable map [string ]* defaultClientSession
219225 endpointsTelemetryClientsLock sync.RWMutex
220226 on atomic.Bool
221-
222- clientImpl isClient
227+ inited atomic. Bool
228+ clientImpl isClient
223229}
224230
225231var NewClient = func (config * Config , opts ... ClientOption ) (Client , error ) {
@@ -235,6 +241,7 @@ var NewClient = func(config *Config, opts ...ClientOption) (Client, error) {
235241 messageInterceptors : make ([]MessageInterceptor , 0 ),
236242 endpointsTelemetryClientTable : make (map [string ]* defaultClientSession ),
237243 on : * atomic .NewBool (true ),
244+ inited : * atomic .NewBool (false ),
238245 }
239246 cli .log = sugarBaseLogger .With ("client_id" , cli .clientID )
240247 for _ , opt := range opts {
@@ -299,7 +306,7 @@ func (cli *defaultClient) registerMessageInterceptor(messageInterceptor MessageI
299306 cli .messageInterceptors = append (cli .messageInterceptors , messageInterceptor )
300307}
301308
302- func (cli * defaultClient ) doBefore (hookPoint MessageHookPoints , messageCommons []* MessageCommon ) {
309+ func (cli * defaultClient ) doBefore (hookPoint MessageHookPoints , messageCommons []* MessageCommon ) error {
303310 cli .messageInterceptorsLock .RLocker ().Lock ()
304311 defer cli .messageInterceptorsLock .RLocker ().Unlock ()
305312
@@ -309,9 +316,10 @@ func (cli *defaultClient) doBefore(hookPoint MessageHookPoints, messageCommons [
309316 cli .log .Errorf ("exception raised while intercepting message, hookPoint=%v, err=%v" , hookPoint , err )
310317 }
311318 }
319+ return nil
312320}
313321
314- func (cli * defaultClient ) doAfter (hookPoint MessageHookPoints , messageCommons []* MessageCommon , duration time.Duration , status MessageHookPointsStatus ) {
322+ func (cli * defaultClient ) doAfter (hookPoint MessageHookPoints , messageCommons []* MessageCommon , duration time.Duration , status MessageHookPointsStatus ) error {
315323 cli .messageInterceptorsLock .RLocker ().Lock ()
316324 defer cli .messageInterceptorsLock .RLocker ().Unlock ()
317325
@@ -321,6 +329,7 @@ func (cli *defaultClient) doAfter(hookPoint MessageHookPoints, messageCommons []
321329 cli .log .Errorf ("exception raised while intercepting message, hookPoint=%v, err=%v" , hookPoint , err )
322330 }
323331 }
332+ return nil
324333}
325334
326335func (cli * defaultClient ) getMessageQueues (ctx context.Context , topic string ) ([]* v2.MessageQueue , error ) {
@@ -417,6 +426,36 @@ func (cli *defaultClient) getSettingsCommand() *v2.TelemetryCommand {
417426 }
418427}
419428
429+ func (cli * defaultClient ) queryAssignments (ctx context.Context , topic string , group string , duration time.Duration ) (* []* v2.Assignment , error ) {
430+ ctx = cli .Sign (ctx )
431+ response , err := cli .clientManager .QueryAssignments (ctx , cli .accessPoint , cli .getQueryAssignmentRequest (topic , group ), duration )
432+ if err != nil {
433+ return nil , err
434+ }
435+ if response .GetStatus ().GetCode () != v2 .Code_OK {
436+ return nil , & ErrRpcStatus {
437+ Code : int32 (response .Status .GetCode ()),
438+ Message : response .GetStatus ().GetMessage (),
439+ }
440+ }
441+ ret := response .GetAssignments ()
442+ return & ret , nil
443+ }
444+
445+ func (cli * defaultClient ) getQueryAssignmentRequest (topic string , group string ) * v2.QueryAssignmentRequest {
446+ return & v2.QueryAssignmentRequest {
447+ Topic : & v2.Resource {
448+ Name : topic ,
449+ ResourceNamespace : cli .config .NameSpace ,
450+ },
451+ Group : & v2.Resource {
452+ Name : group ,
453+ ResourceNamespace : cli .config .NameSpace ,
454+ },
455+ Endpoints : cli .accessPoint ,
456+ }
457+ }
458+
420459func (cli * defaultClient ) doHeartbeat (target string , request * v2.HeartbeatRequest ) error {
421460 ctx := cli .Sign (context .Background ())
422461 endpoints , err := utils .ParseTarget (target )
@@ -428,9 +467,6 @@ func (cli *defaultClient) doHeartbeat(target string, request *v2.HeartbeatReques
428467 return fmt .Errorf ("failed to send heartbeat, endpoints=%v, err=%v, requestId=%s" , endpoints , err , utils .GetRequestID (ctx ))
429468 }
430469 if resp .Status .GetCode () != v2 .Code_OK {
431- if resp .Status .GetCode () == v2 .Code_UNRECOGNIZED_CLIENT_TYPE {
432- go cli .trySyncSettings ()
433- }
434470 cli .log .Errorf ("failed to send heartbeat, code=%v, status message=[%s], endpoints=%v, requestId=%s" , resp .Status .GetCode (), resp .Status .GetMessage (), endpoints , utils .GetRequestID (ctx ))
435471 return & ErrRpcStatus {
436472 Code : int32 (resp .Status .GetCode ()),
@@ -496,6 +532,7 @@ func (cli *defaultClient) startUp() error {
496532 cm .startUp ()
497533 cm .RegisterClient (cli )
498534 cli .clientManager = cm
535+
499536 for _ , topic := range cli .initTopics {
500537 _ , err := cli .getMessageQueues (context .Background (), topic )
501538 if err != nil {
@@ -546,6 +583,13 @@ func (cli *defaultClient) startUp() error {
546583 })
547584 }
548585 ticker .Tick (f , time .Second * 30 , cli .done )
586+
587+ // wait syncSettings finish
588+ for ! cli .inited .Load () {
589+ sugarBaseLogger .Infoln ("wait for sync settings finish" )
590+ time .Sleep (time .Second )
591+ }
592+ sugarBaseLogger .Infoln ("sync settings finished" )
549593 return nil
550594}
551595
@@ -587,6 +631,10 @@ func (cli *defaultClient) GracefulStop() error {
587631 return nil
588632}
589633
634+ func (cli * defaultClient ) isRunning () bool {
635+ return cli .on .Load ()
636+ }
637+
590638func (cli * defaultClient ) Sign (ctx context.Context ) context.Context {
591639 now := time .Now ().Format ("20060102T150405Z" )
592640 return metadata .AppendToOutgoingContext (ctx ,
@@ -629,7 +677,9 @@ func (cli *defaultClient) onSettingsCommand(endpoints *v2.Endpoints, settings *v
629677 if metric != nil {
630678 cli .clientMeterProvider .Reset (metric )
631679 }
632- return cli .settings .applySettingsCommand (settings )
680+ err := cli .settings .applySettingsCommand (settings )
681+ cli .inited .Store (true )
682+ return err
633683}
634684
635685func (cli * defaultClient ) onRecoverOrphanedTransactionCommand (endpoints * v2.Endpoints , command * v2.RecoverOrphanedTransactionCommand ) {
0 commit comments