39
39
handle_connection_down /2 ,
40
40
consumer_groups /3 ,
41
41
group_consumers /5 ,
42
- overview /1 ]).
42
+ overview /1 ,
43
+ import_state /2 ]).
43
44
44
45
-import (rabbit_stream_coordinator , [ra_local_query /1 ]).
45
46
47
+ -define (ACTIVE , active ).
48
+ -define (WAITING , waiting ).
49
+ -define (DEACTIVATING , deactivating ).
50
+
46
51
% % Single Active Consumer API
47
52
-spec register_consumer (binary (),
48
53
binary (),
@@ -231,7 +236,7 @@ apply(#command_unregister_consumer{vhost = VirtualHost,
231
236
G1 = remove_from_group (Consumer , Group0 ),
232
237
handle_consumer_removal (
233
238
G1 , Stream , ConsumerName ,
234
- is_active (Consumer # consumer .active ));
239
+ is_active (Consumer # consumer .status ));
235
240
false ->
236
241
{Group0 , []}
237
242
end ,
@@ -255,10 +260,10 @@ apply(#command_activate_consumer{vhost = VirtualHost,
255
260
[{VirtualHost , Stream , ConsumerName }]),
256
261
{undefined , []};
257
262
Group0 ->
258
- Group1 = update_consumers (Group0 , waiting ),
263
+ Group1 = update_consumers (Group0 , ? WAITING ),
259
264
# consumer {pid = Pid , subscription_id = SubId } =
260
265
evaluate_active_consumer (Group1 ),
261
- Group2 = update_consumer_state_in_group (Group1 , Pid , SubId , active ),
266
+ Group2 = update_consumer_state_in_group (Group1 , Pid , SubId , ? ACTIVE ),
262
267
{Group2 , [notify_consumer_effect (Pid , SubId , Stream , ConsumerName , true )]}
263
268
end ,
264
269
StreamGroups1 =
@@ -314,7 +319,7 @@ group_consumers(VirtualHost,
314
319
#{GroupId := # group {consumers = Consumers }} ->
315
320
Cs = lists :foldr (fun (# consumer {subscription_id = SubId ,
316
321
owner = Owner ,
317
- active = Active },
322
+ status = Status },
318
323
Acc ) ->
319
324
Record =
320
325
lists :foldr (fun (subscription_id , RecAcc ) ->
@@ -326,7 +331,7 @@ group_consumers(VirtualHost,
326
331
Owner }
327
332
| RecAcc ];
328
333
(state , RecAcc ) ->
329
- [{state , Active }
334
+ [{state , cli_consumer_status_label ( Status ) }
330
335
| RecAcc ];
331
336
(Unknown , RecAcc ) ->
332
337
[{Unknown ,
@@ -342,6 +347,11 @@ group_consumers(VirtualHost,
342
347
{error , not_found }
343
348
end .
344
349
350
+ cli_consumer_status_label (? ACTIVE ) ->
351
+ active ;
352
+ cli_consumer_status_label (_ ) ->
353
+ inactive .
354
+
345
355
-spec ensure_monitors (command (),
346
356
state (),
347
357
map (),
@@ -432,7 +442,7 @@ handle_group_after_connection_down(Pid,
432
442
% % keep flags to know what happened
433
443
{Consumers1 , ActiveRemoved , AnyRemoved } =
434
444
lists :foldl (
435
- fun (# consumer {pid = P , active = S }, {L , ActiveFlag , _ })
445
+ fun (# consumer {pid = P , status = S }, {L , ActiveFlag , _ })
436
446
when P == Pid ->
437
447
{L , is_active (S ) or ActiveFlag , true };
438
448
(C , {L , ActiveFlag , AnyFlag }) ->
@@ -454,6 +464,42 @@ handle_group_after_connection_down(Pid,
454
464
end
455
465
end .
456
466
467
+ -spec import_state (ra_machine :version (), map ()) -> state ().
468
+ import_state (4 , #{<<" groups" >> := Groups , <<" pids_groups" >> := PidsGroups }) ->
469
+ #? MODULE {groups = map_to_groups (Groups ),
470
+ pids_groups = map_to_pids_groups (PidsGroups )}.
471
+
472
+ map_to_groups (Groups ) when is_map (Groups ) ->
473
+ maps :fold (fun (K , V , Acc ) ->
474
+ Acc #{K => map_to_group (V )}
475
+ end , #{}, Groups );
476
+ map_to_groups (_ ) ->
477
+ #{}.
478
+
479
+ map_to_pids_groups (PidsGroups ) when is_map (PidsGroups ) ->
480
+ PidsGroups ;
481
+ map_to_pids_groups (_ ) ->
482
+ #{}.
483
+
484
+ map_to_group (#{<<" consumers" >> := Consumers , <<" partition_index" >> := Index }) ->
485
+ C = lists :foldl (fun (V , Acc ) ->
486
+ Acc ++ [map_to_consumer (V )]
487
+ end , [], Consumers ),
488
+ # group {consumers = C ,
489
+ partition_index = Index }.
490
+
491
+ map_to_consumer (#{<<" pid" >> := Pid , <<" subscription_id" >> := SubId ,
492
+ <<" owner" >> := Owner , <<" active" >> := Active }) ->
493
+ # consumer {pid = Pid ,
494
+ subscription_id = SubId ,
495
+ owner = Owner ,
496
+ status = active_to_status (Active )}.
497
+
498
+ active_to_status (true ) ->
499
+ ? ACTIVE ;
500
+ active_to_status (false ) ->
501
+ ? WAITING .
502
+
457
503
is_active (waiting ) ->
458
504
false ;
459
505
is_active (_ ) ->
@@ -476,12 +522,12 @@ do_register_consumer(VirtualHost,
476
522
# consumer {pid = ConnectionPid ,
477
523
owner = Owner ,
478
524
subscription_id = SubscriptionId ,
479
- active = waiting };
525
+ status = ? WAITING };
480
526
false ->
481
527
# consumer {pid = ConnectionPid ,
482
528
subscription_id = SubscriptionId ,
483
529
owner = Owner ,
484
- active = active }
530
+ status = ? ACTIVE }
485
531
end ,
486
532
Group1 = add_to_group (Consumer , Group0 ),
487
533
StreamGroups1 =
@@ -491,17 +537,17 @@ do_register_consumer(VirtualHost,
491
537
Group1 ,
492
538
StreamGroups0 ),
493
539
494
- # consumer {active = Active } = Consumer ,
540
+ # consumer {status = Status } = Consumer ,
495
541
Effects =
496
- case Active of
497
- active ->
542
+ case Status of
543
+ ? ACTIVE ->
498
544
[notify_consumer_effect (ConnectionPid , SubscriptionId ,
499
- Stream , ConsumerName , is_active (Active ))];
545
+ Stream , ConsumerName , is_active (Status ))];
500
546
_ ->
501
547
[]
502
548
end ,
503
549
504
- {State #? MODULE {groups = StreamGroups1 }, {ok , is_active (Active )}, Effects };
550
+ {State #? MODULE {groups = StreamGroups1 }, {ok , is_active (Status )}, Effects };
505
551
do_register_consumer (VirtualHost ,
506
552
Stream ,
507
553
_PartitionIndex ,
@@ -521,7 +567,7 @@ do_register_consumer(VirtualHost,
521
567
# consumer {pid = ConnectionPid ,
522
568
owner = Owner ,
523
569
subscription_id = SubscriptionId ,
524
- active = active },
570
+ status = ? ACTIVE },
525
571
G1 = add_to_group (Consumer0 , Group0 ),
526
572
{G1 ,
527
573
[notify_consumer_effect (ConnectionPid , SubscriptionId ,
@@ -532,7 +578,7 @@ do_register_consumer(VirtualHost,
532
578
# consumer {pid = ConnectionPid ,
533
579
owner = Owner ,
534
580
subscription_id = SubscriptionId ,
535
- active = waiting },
581
+ status = ? WAITING },
536
582
G1 = add_to_group (Consumer0 , Group0 ),
537
583
538
584
case lookup_active_consumer (G1 ) of
@@ -548,7 +594,7 @@ do_register_consumer(VirtualHost,
548
594
{update_consumer_state_in_group (G1 ,
549
595
ActPid ,
550
596
ActSubId ,
551
- deactivating ),
597
+ ? DEACTIVATING ),
552
598
[notify_consumer_effect (ActPid ,
553
599
ActSubId ,
554
600
Stream ,
@@ -568,9 +614,9 @@ do_register_consumer(VirtualHost,
568
614
ConsumerName ,
569
615
Group1 ,
570
616
StreamGroups0 ),
571
- {value , # consumer {active = Active }} =
617
+ {value , # consumer {status = Status }} =
572
618
lookup_consumer (ConnectionPid , SubscriptionId , Group1 ),
573
- {State #? MODULE {groups = StreamGroups1 }, {ok , is_active (Active )}, Effects }.
619
+ {State #? MODULE {groups = StreamGroups1 }, {ok , is_active (Status )}, Effects }.
574
620
575
621
handle_consumer_removal (# group {consumers = []} = G , _ , _ , _ ) ->
576
622
{G , []};
@@ -606,7 +652,7 @@ handle_consumer_removal(Group0, Stream, ConsumerName, ActiveRemoved) ->
606
652
{update_consumer_state_in_group (Group0 ,
607
653
ActPid ,
608
654
ActSubId ,
609
- deactivating ),
655
+ ? DEACTIVATING ),
610
656
[notify_consumer_effect (ActPid , ActSubId ,
611
657
Stream , ConsumerName , false , true )]}
612
658
end ;
@@ -616,7 +662,7 @@ handle_consumer_removal(Group0, Stream, ConsumerName, ActiveRemoved) ->
616
662
% % the active one is going away, picking a new one
617
663
# consumer {pid = P , subscription_id = SID } =
618
664
evaluate_active_consumer (Group0 ),
619
- {update_consumer_state_in_group (Group0 , P , SID , active ),
665
+ {update_consumer_state_in_group (Group0 , P , SID , ? ACTIVE ),
620
666
[notify_consumer_effect (P , SID ,
621
667
Stream , ConsumerName , true )]};
622
668
false ->
@@ -683,13 +729,13 @@ compute_active_consumer(#group{consumers = Crs,
683
729
compute_active_consumer (# group {partition_index = - 1 ,
684
730
consumers = [Consumer0 ]} =
685
731
Group0 ) ->
686
- Consumer1 = Consumer0 # consumer {active = active },
732
+ Consumer1 = Consumer0 # consumer {status = ? ACTIVE },
687
733
Group0 # group {consumers = [Consumer1 ]};
688
734
compute_active_consumer (# group {partition_index = - 1 ,
689
735
consumers = [Consumer0 | T ]} =
690
736
Group0 ) ->
691
- Consumer1 = Consumer0 # consumer {active = active },
692
- Consumers = lists :map (fun (C ) -> C # consumer {active = waiting } end , T ),
737
+ Consumer1 = Consumer0 # consumer {status = ? ACTIVE },
738
+ Consumers = lists :map (fun (C ) -> C # consumer {status = ? WAITING } end , T ),
693
739
Group0 # group {consumers = [Consumer1 ] ++ Consumers }.
694
740
695
741
evaluate_active_consumer (# group {partition_index = PartitionIndex ,
@@ -706,7 +752,7 @@ lookup_consumer(ConnectionPid, SubscriptionId,
706
752
Consumers ).
707
753
708
754
lookup_active_consumer (# group {consumers = Consumers }) ->
709
- lists :search (fun (# consumer {active = Active }) -> is_active (Active ) end ,
755
+ lists :search (fun (# consumer {status = Status }) -> is_active (Status ) end ,
710
756
Consumers ).
711
757
712
758
update_groups (_VirtualHost ,
@@ -732,20 +778,20 @@ update_groups(VirtualHost,
732
778
update_consumer_state_in_group (# group {consumers = Consumers0 } = G ,
733
779
Pid ,
734
780
SubId ,
735
- NewState ) ->
781
+ NewStatus ) ->
736
782
CS1 = lists :map (fun (C0 ) ->
737
783
case C0 of
738
784
# consumer {pid = Pid , subscription_id = SubId } ->
739
- C0 # consumer {active = NewState };
785
+ C0 # consumer {status = NewStatus };
740
786
C -> C
741
787
end
742
788
end ,
743
789
Consumers0 ),
744
790
G # group {consumers = CS1 }.
745
791
746
- update_consumers (# group {consumers = Consumers0 } = G , NewState ) ->
792
+ update_consumers (# group {consumers = Consumers0 } = G , NewStatus ) ->
747
793
Consumers1 = lists :map (fun (C ) ->
748
- C # consumer {active = NewState }
794
+ C # consumer {status = NewStatus }
749
795
end , Consumers0 ),
750
796
G # group {consumers = Consumers1 }.
751
797
0 commit comments