33
33
% % exported for testing only
34
34
-export ([start_msg_store /2 , stop_msg_store /0 , init /6 ]).
35
35
36
+ -export ([move_messages_to_vhost_store /0 ]).
37
+ -include_lib (" stdlib/include/qlc.hrl" ).
38
+
36
39
% %----------------------------------------------------------------------------
37
40
% % Messages, and their position in the queue, can be in memory or on
38
41
% % disk, or both. Persistent messages will have both message and
334
337
}).
335
338
336
339
-define (HEADER_GUESS_SIZE , 100 ). % % see determine_persist_to/2
340
+ -define (PERSISTENT_MSG_STORE_SUP , msg_store_persistent_vhost ).
341
+ -define (TRANSIENT_MSG_STORE_SUP , msg_store_transient_vhost ).
337
342
-define (PERSISTENT_MSG_STORE , msg_store_persistent ).
338
343
-define (TRANSIENT_MSG_STORE , msg_store_transient ).
344
+
339
345
-define (QUEUE , lqueue ).
340
346
341
347
-include (" rabbit.hrl" ).
344
350
% %----------------------------------------------------------------------------
345
351
346
352
-rabbit_upgrade ({multiple_routing_keys , local , []}).
347
- -rabbit_upgrade ({move_messages_to_vhost_store , local , []}).
353
+ % -rabbit_upgrade({move_messages_to_vhost_store, local, []}). requires mnesia, requires rabbit_sup, requires worker_pool, requires fhc
354
+ -compile (export_all ).
348
355
349
356
-type seq_id () :: non_neg_integer ().
350
357
452
459
% % Public API
453
460
% %----------------------------------------------------------------------------
454
461
462
+
463
+
455
464
start (DurableQueues ) ->
456
465
{AllTerms , StartFunState } = rabbit_queue_index :start (DurableQueues ),
457
466
start_msg_store (
@@ -470,23 +479,23 @@ stop() ->
470
479
471
480
start_msg_store (Refs , StartFunState ) ->
472
481
VHosts = rabbit_vhost :list (),
473
- ok = rabbit_sup :start_child (? TRANSIENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
474
- [? TRANSIENT_MSG_STORE , rabbit_mnesia :dir (),
482
+ ok = rabbit_sup :start_child (? TRANSIENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
483
+ [? TRANSIENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
475
484
undefined , {fun (ok ) -> finished end , ok }]),
476
- ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
477
- [? PERSISTENT_MSG_STORE , rabbit_mnesia :dir (),
485
+ ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
486
+ [? PERSISTENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
478
487
Refs , StartFunState ]),
479
488
lists :foreach (
480
489
fun (VHost ) ->
481
- rabbit_msg_store_vhost_sup :add_vhost (? TRANSIENT_MSG_STORE , VHost ),
482
- rabbit_msg_store_vhost_sup :add_vhost (? PERSISTENT_MSG_STORE , VHost )
490
+ rabbit_msg_store_vhost_sup :add_vhost (? TRANSIENT_MSG_STORE_SUP , VHost ),
491
+ rabbit_msg_store_vhost_sup :add_vhost (? PERSISTENT_MSG_STORE_SUP , VHost )
483
492
end ,
484
493
VHosts ),
485
494
ok .
486
495
487
496
stop_msg_store () ->
488
- ok = rabbit_sup :stop_child (? PERSISTENT_MSG_STORE ),
489
- ok = rabbit_sup :stop_child (? TRANSIENT_MSG_STORE ).
497
+ ok = rabbit_sup :stop_child (? PERSISTENT_MSG_STORE_SUP ),
498
+ ok = rabbit_sup :stop_child (? TRANSIENT_MSG_STORE_SUP ).
490
499
491
500
init (Queue , Recover , Callback ) ->
492
501
init (
@@ -504,11 +513,11 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, new,
504
513
VHost = QueueName # resource .virtual_host ,
505
514
init (IsDurable , IndexState , 0 , 0 , [],
506
515
case IsDurable of
507
- true -> msg_store_client_init (? PERSISTENT_MSG_STORE ,
516
+ true -> msg_store_client_init (? PERSISTENT_MSG_STORE_SUP ,
508
517
MsgOnDiskFun , AsyncCallback , VHost );
509
518
false -> undefined
510
519
end ,
511
- msg_store_client_init (? TRANSIENT_MSG_STORE , undefined ,
520
+ msg_store_client_init (? TRANSIENT_MSG_STORE_SUP , undefined ,
512
521
AsyncCallback , VHost ));
513
522
514
523
% % We can be recovering a transient queue if it crashed
@@ -518,7 +527,7 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, Terms,
518
527
VHost = QueueName # resource .virtual_host ,
519
528
{PersistentClient , ContainsCheckFun } =
520
529
case IsDurable of
521
- true -> C = msg_store_client_init (? PERSISTENT_MSG_STORE , PRef ,
530
+ true -> C = msg_store_client_init (? PERSISTENT_MSG_STORE_SUP , PRef ,
522
531
MsgOnDiskFun , AsyncCallback ,
523
532
VHost ),
524
533
{C , fun (MsgId ) when is_binary (MsgId ) ->
@@ -528,14 +537,14 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, Terms,
528
537
end };
529
538
false -> {undefined , fun (_MsgId ) -> false end }
530
539
end ,
531
- TransientClient = msg_store_client_init (? TRANSIENT_MSG_STORE ,
540
+ TransientClient = msg_store_client_init (? TRANSIENT_MSG_STORE_SUP ,
532
541
undefined , AsyncCallback ,
533
542
VHost ),
534
543
{DeltaCount , DeltaBytes , IndexState } =
535
544
rabbit_queue_index :recover (
536
545
QueueName , RecoveryTerms ,
537
546
rabbit_msg_store_vhost_sup :successfully_recovered_state (
538
- ? PERSISTENT_MSG_STORE , VHost ),
547
+ ? PERSISTENT_MSG_STORE_SUP , VHost ),
539
548
ContainsCheckFun , MsgIdxOnDiskFun , MsgAndIdxOnDiskFun ),
540
549
init (IsDurable , IndexState , DeltaCount , DeltaBytes , RecoveryTerms ,
541
550
PersistentClient , TransientClient ).
@@ -1208,7 +1217,7 @@ msg_store_client_init(MsgStore, MsgOnDiskFun, Callback, VHost) ->
1208
1217
Callback , VHost ).
1209
1218
1210
1219
msg_store_client_init (MsgStore , Ref , MsgOnDiskFun , Callback , VHost ) ->
1211
- CloseFDsFun = msg_store_close_fds_fun (MsgStore =:= ? PERSISTENT_MSG_STORE ),
1220
+ CloseFDsFun = msg_store_close_fds_fun (MsgStore =:= ? PERSISTENT_MSG_STORE_SUP ),
1212
1221
rabbit_msg_store_vhost_sup :client_init (MsgStore , Ref , MsgOnDiskFun ,
1213
1222
fun () ->
1214
1223
Callback (? MODULE , CloseFDsFun )
@@ -2666,23 +2675,26 @@ multiple_routing_keys() ->
2666
2675
2667
2676
% % Assumes message store is not running
2668
2677
transform_storage (TransformFun ) ->
2669
- transform_store (? PERSISTENT_MSG_STORE , TransformFun ),
2670
- transform_store (? TRANSIENT_MSG_STORE , TransformFun ).
2678
+ transform_store (? PERSISTENT_MSG_STORE_SUP , TransformFun ),
2679
+ transform_store (? TRANSIENT_MSG_STORE_SUP , TransformFun ).
2671
2680
2672
2681
transform_store (Store , TransformFun ) ->
2673
2682
rabbit_msg_store :force_recovery (rabbit_mnesia :dir (), Store ),
2674
2683
rabbit_msg_store :transform_dir (rabbit_mnesia :dir (), Store , TransformFun ).
2675
2684
2676
2685
move_messages_to_vhost_store () ->
2677
- Queues = list_persistent_queues (),
2686
+ Queues = rabbit_variable_queue : list_persistent_queues (),
2678
2687
% Maybe recover old store.
2679
- {RecoveryTerms , StartFunState } = start_recovery_terms (Queues ),
2680
- OldStore = run_old_persistent_store (RecoveryTerms , StartFunState ),
2681
- NewStoreSup = start_new_store_sup (),
2682
- Migrations = spawn_for_each (fun (Queue ) ->
2688
+ {RecoveryTerms , StartFunState } = rabbit_variable_queue : start_recovery_terms (Queues ),
2689
+ OldStore = rabbit_variable_queue : run_old_persistent_store (RecoveryTerms , StartFunState ),
2690
+ NewStoreSup = rabbit_variable_queue : start_new_store_sup (),
2691
+ lists : map (fun (Queue ) ->
2683
2692
migrate_queue (Queue , OldStore , NewStoreSup )
2684
2693
end , Queues ),
2685
- wait (Migrations ),
2694
+ % Migrations = spawn_for_each(fun(Queue) ->
2695
+ % migrate_queue(Queue, OldStore, NewStoreSup)
2696
+ % end, Queues),
2697
+ % wait(Migrations),
2686
2698
delete_old_store (OldStore ).
2687
2699
2688
2700
migrate_queue (Queue , OldStore , NewStoreSup ) ->
@@ -2697,8 +2709,19 @@ migrate_queue(Queue, OldStore, NewStoreSup) ->
2697
2709
_ -> OldC
2698
2710
end
2699
2711
end ,
2712
+ OldStoreClient ,
2700
2713
Queue ).
2701
2714
2715
+ walk_queue_index (Fun , Client , # amqqueue {name = QueueName }) ->
2716
+ % WARNING: State is being recovered and terminated. This can cause side effects!
2717
+ rabbit_queue_index :scan_queue_segments (
2718
+ fun (_SeqId , MsgId , _MsgProps , true , _IsDelivered , _IsAcked , ClientState )
2719
+ when is_binary (MsgId ) ->
2720
+ Fun (MsgId , ClientState );
2721
+ (_SeqId , _MsgId , _MsgProps , _IsPersistent , _IsDelivered , _IsAcked , ClientState ) ->
2722
+ ClientState
2723
+ end , Client , QueueName ).
2724
+
2702
2725
spawn_for_each (Fun , List ) ->
2703
2726
Ref = erlang :make_ref (),
2704
2727
Self = self (),
@@ -2752,7 +2775,8 @@ list_persistent_queues() ->
2752
2775
end ).
2753
2776
2754
2777
start_recovery_terms (Queues ) ->
2755
- {AllTerms , StartFunState } = rabbit_queue_index :start (Queues ),
2778
+ QueueNames = [Name || # amqqueue {name = Name } <- Queues ],
2779
+ {AllTerms , StartFunState } = rabbit_queue_index :start (QueueNames ),
2756
2780
Refs = [Ref || Terms <- AllTerms ,
2757
2781
Terms /= non_clean_shutdown ,
2758
2782
begin
@@ -2762,30 +2786,30 @@ start_recovery_terms(Queues) ->
2762
2786
{Refs , StartFunState }.
2763
2787
2764
2788
run_old_persistent_store (Refs , StartFunState ) ->
2765
- OldStoreName = old_persistent_msg_store ,
2789
+ OldStoreName = ? PERSISTENT_MSG_STORE ,
2766
2790
ok = rabbit_sup :start_child (OldStoreName , rabbit_msg_store ,
2767
2791
[OldStoreName , rabbit_mnesia :dir (),
2768
2792
Refs , StartFunState ]),
2769
2793
OldStoreName .
2770
2794
2771
- run_persistent_store (Vhost ) ->
2772
-
2773
-
2774
- ? PERSISTENT_MSG_STORE .
2775
-
2776
2795
start_new_store_sup () ->
2777
2796
% Start persistent store sup without recovery.
2778
- ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
2779
- [? PERSISTENT_MSG_STORE , rabbit_mnesia :dir (),
2797
+ ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
2798
+ [? PERSISTENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
2780
2799
undefined , {fun (ok ) -> finished end , ok }]),
2781
- ? PERSISTENT_MSG_STORE .
2800
+ ? PERSISTENT_MSG_STORE_SUP .
2782
2801
2783
2802
delete_old_store (OldStore ) ->
2784
2803
gen_server :stop (OldStore ),
2785
- rabbit_file :recursive_delete (
2786
- filename :join ([rabbit_mnesia :dir (), ? PERSISTENT_MSG_STORE ])).
2787
-
2804
+ rabbit_file :recursive_delete ([filename :join ([rabbit_mnesia :dir (), ? PERSISTENT_MSG_STORE ])]).
2788
2805
2789
2806
2790
2807
2808
+ setup () ->
2809
+ application :load (rabbit ),
2810
+ mnesia :start (),
2811
+ rabbit_sup :start_link (),
2812
+ rabbit :start_fhc (),
2813
+ rabbit_sup :start_restartable_child (rabbit_guid ),
2814
+ rabbit_sup :start_supervisor_child (worker_pool_sup ).
2791
2815
0 commit comments