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
330
333
}).
331
334
332
335
-define (HEADER_GUESS_SIZE , 100 ). % % see determine_persist_to/2
336
+ -define (PERSISTENT_MSG_STORE_SUP , msg_store_persistent_vhost ).
337
+ -define (TRANSIENT_MSG_STORE_SUP , msg_store_transient_vhost ).
333
338
-define (PERSISTENT_MSG_STORE , msg_store_persistent ).
334
339
-define (TRANSIENT_MSG_STORE , msg_store_transient ).
340
+
335
341
-define (QUEUE , lqueue ).
336
342
337
343
-include (" rabbit.hrl" ).
340
346
% %----------------------------------------------------------------------------
341
347
342
348
-rabbit_upgrade ({multiple_routing_keys , local , []}).
343
- -rabbit_upgrade ({move_messages_to_vhost_store , local , []}).
349
+ % -rabbit_upgrade({move_messages_to_vhost_store, local, []}). requires mnesia, requires rabbit_sup, requires worker_pool, requires fhc
350
+ -compile (export_all ).
344
351
345
352
-ifdef (use_specs ).
346
353
436
443
% % Public API
437
444
% %----------------------------------------------------------------------------
438
445
446
+
447
+
439
448
start (DurableQueues ) ->
440
449
{AllTerms , StartFunState } = rabbit_queue_index :start (DurableQueues ),
441
450
start_msg_store (
@@ -454,23 +463,23 @@ stop() ->
454
463
455
464
start_msg_store (Refs , StartFunState ) ->
456
465
VHosts = rabbit_vhost :list (),
457
- ok = rabbit_sup :start_child (? TRANSIENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
458
- [? TRANSIENT_MSG_STORE , rabbit_mnesia :dir (),
466
+ ok = rabbit_sup :start_child (? TRANSIENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
467
+ [? TRANSIENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
459
468
undefined , {fun (ok ) -> finished end , ok }]),
460
- ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
461
- [? PERSISTENT_MSG_STORE , rabbit_mnesia :dir (),
469
+ ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
470
+ [? PERSISTENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
462
471
Refs , StartFunState ]),
463
472
lists :foreach (
464
473
fun (VHost ) ->
465
- rabbit_msg_store_vhost_sup :add_vhost (? TRANSIENT_MSG_STORE , VHost ),
466
- rabbit_msg_store_vhost_sup :add_vhost (? PERSISTENT_MSG_STORE , VHost )
474
+ rabbit_msg_store_vhost_sup :add_vhost (? TRANSIENT_MSG_STORE_SUP , VHost ),
475
+ rabbit_msg_store_vhost_sup :add_vhost (? PERSISTENT_MSG_STORE_SUP , VHost )
467
476
end ,
468
477
VHosts ),
469
478
ok .
470
479
471
480
stop_msg_store () ->
472
- ok = rabbit_sup :stop_child (? PERSISTENT_MSG_STORE ),
473
- ok = rabbit_sup :stop_child (? TRANSIENT_MSG_STORE ).
481
+ ok = rabbit_sup :stop_child (? PERSISTENT_MSG_STORE_SUP ),
482
+ ok = rabbit_sup :stop_child (? TRANSIENT_MSG_STORE_SUP ).
474
483
475
484
init (Queue , Recover , Callback ) ->
476
485
init (
@@ -488,11 +497,11 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, new,
488
497
VHost = QueueName # resource .virtual_host ,
489
498
init (IsDurable , IndexState , 0 , 0 , [],
490
499
case IsDurable of
491
- true -> msg_store_client_init (? PERSISTENT_MSG_STORE ,
500
+ true -> msg_store_client_init (? PERSISTENT_MSG_STORE_SUP ,
492
501
MsgOnDiskFun , AsyncCallback , VHost );
493
502
false -> undefined
494
503
end ,
495
- msg_store_client_init (? TRANSIENT_MSG_STORE , undefined ,
504
+ msg_store_client_init (? TRANSIENT_MSG_STORE_SUP , undefined ,
496
505
AsyncCallback , VHost ));
497
506
498
507
% % We can be recovering a transient queue if it crashed
@@ -502,7 +511,7 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, Terms,
502
511
VHost = QueueName # resource .virtual_host ,
503
512
{PersistentClient , ContainsCheckFun } =
504
513
case IsDurable of
505
- true -> C = msg_store_client_init (? PERSISTENT_MSG_STORE , PRef ,
514
+ true -> C = msg_store_client_init (? PERSISTENT_MSG_STORE_SUP , PRef ,
506
515
MsgOnDiskFun , AsyncCallback ,
507
516
VHost ),
508
517
{C , fun (MsgId ) when is_binary (MsgId ) ->
@@ -512,14 +521,14 @@ init(#amqqueue { name = QueueName, durable = IsDurable }, Terms,
512
521
end };
513
522
false -> {undefined , fun (_MsgId ) -> false end }
514
523
end ,
515
- TransientClient = msg_store_client_init (? TRANSIENT_MSG_STORE ,
524
+ TransientClient = msg_store_client_init (? TRANSIENT_MSG_STORE_SUP ,
516
525
undefined , AsyncCallback ,
517
526
VHost ),
518
527
{DeltaCount , DeltaBytes , IndexState } =
519
528
rabbit_queue_index :recover (
520
529
QueueName , RecoveryTerms ,
521
530
rabbit_msg_store_vhost_sup :successfully_recovered_state (
522
- ? PERSISTENT_MSG_STORE , VHost ),
531
+ ? PERSISTENT_MSG_STORE_SUP , VHost ),
523
532
ContainsCheckFun , MsgIdxOnDiskFun , MsgAndIdxOnDiskFun ),
524
533
init (IsDurable , IndexState , DeltaCount , DeltaBytes , RecoveryTerms ,
525
534
PersistentClient , TransientClient ).
@@ -1192,7 +1201,7 @@ msg_store_client_init(MsgStore, MsgOnDiskFun, Callback, VHost) ->
1192
1201
Callback , VHost ).
1193
1202
1194
1203
msg_store_client_init (MsgStore , Ref , MsgOnDiskFun , Callback , VHost ) ->
1195
- CloseFDsFun = msg_store_close_fds_fun (MsgStore =:= ? PERSISTENT_MSG_STORE ),
1204
+ CloseFDsFun = msg_store_close_fds_fun (MsgStore =:= ? PERSISTENT_MSG_STORE_SUP ),
1196
1205
rabbit_msg_store_vhost_sup :client_init (MsgStore , Ref , MsgOnDiskFun ,
1197
1206
fun () ->
1198
1207
Callback (? MODULE , CloseFDsFun )
@@ -2639,23 +2648,26 @@ multiple_routing_keys() ->
2639
2648
2640
2649
% % Assumes message store is not running
2641
2650
transform_storage (TransformFun ) ->
2642
- transform_store (? PERSISTENT_MSG_STORE , TransformFun ),
2643
- transform_store (? TRANSIENT_MSG_STORE , TransformFun ).
2651
+ transform_store (? PERSISTENT_MSG_STORE_SUP , TransformFun ),
2652
+ transform_store (? TRANSIENT_MSG_STORE_SUP , TransformFun ).
2644
2653
2645
2654
transform_store (Store , TransformFun ) ->
2646
2655
rabbit_msg_store :force_recovery (rabbit_mnesia :dir (), Store ),
2647
2656
rabbit_msg_store :transform_dir (rabbit_mnesia :dir (), Store , TransformFun ).
2648
2657
2649
2658
move_messages_to_vhost_store () ->
2650
- Queues = list_persistent_queues (),
2659
+ Queues = rabbit_variable_queue : list_persistent_queues (),
2651
2660
% Maybe recover old store.
2652
- {RecoveryTerms , StartFunState } = start_recovery_terms (Queues ),
2653
- OldStore = run_old_persistent_store (RecoveryTerms , StartFunState ),
2654
- NewStoreSup = start_new_store_sup (),
2655
- Migrations = spawn_for_each (fun (Queue ) ->
2661
+ {RecoveryTerms , StartFunState } = rabbit_variable_queue : start_recovery_terms (Queues ),
2662
+ OldStore = rabbit_variable_queue : run_old_persistent_store (RecoveryTerms , StartFunState ),
2663
+ NewStoreSup = rabbit_variable_queue : start_new_store_sup (),
2664
+ lists : map (fun (Queue ) ->
2656
2665
migrate_queue (Queue , OldStore , NewStoreSup )
2657
2666
end , Queues ),
2658
- wait (Migrations ),
2667
+ % Migrations = spawn_for_each(fun(Queue) ->
2668
+ % migrate_queue(Queue, OldStore, NewStoreSup)
2669
+ % end, Queues),
2670
+ % wait(Migrations),
2659
2671
delete_old_store (OldStore ).
2660
2672
2661
2673
migrate_queue (Queue , OldStore , NewStoreSup ) ->
@@ -2670,8 +2682,19 @@ migrate_queue(Queue, OldStore, NewStoreSup) ->
2670
2682
_ -> OldC
2671
2683
end
2672
2684
end ,
2685
+ OldStoreClient ,
2673
2686
Queue ).
2674
2687
2688
+ walk_queue_index (Fun , Client , # amqqueue {name = QueueName }) ->
2689
+ % WARNING: State is being recovered and terminated. This can cause side effects!
2690
+ rabbit_queue_index :scan_queue_segments (
2691
+ fun (_SeqId , MsgId , _MsgProps , true , _IsDelivered , _IsAcked , ClientState )
2692
+ when is_binary (MsgId ) ->
2693
+ Fun (MsgId , ClientState );
2694
+ (_SeqId , _MsgId , _MsgProps , _IsPersistent , _IsDelivered , _IsAcked , ClientState ) ->
2695
+ ClientState
2696
+ end , Client , QueueName ).
2697
+
2675
2698
spawn_for_each (Fun , List ) ->
2676
2699
Ref = erlang :make_ref (),
2677
2700
Self = self (),
@@ -2725,7 +2748,8 @@ list_persistent_queues() ->
2725
2748
end ).
2726
2749
2727
2750
start_recovery_terms (Queues ) ->
2728
- {AllTerms , StartFunState } = rabbit_queue_index :start (Queues ),
2751
+ QueueNames = [Name || # amqqueue {name = Name } <- Queues ],
2752
+ {AllTerms , StartFunState } = rabbit_queue_index :start (QueueNames ),
2729
2753
Refs = [Ref || Terms <- AllTerms ,
2730
2754
Terms /= non_clean_shutdown ,
2731
2755
begin
@@ -2735,30 +2759,30 @@ start_recovery_terms(Queues) ->
2735
2759
{Refs , StartFunState }.
2736
2760
2737
2761
run_old_persistent_store (Refs , StartFunState ) ->
2738
- OldStoreName = old_persistent_msg_store ,
2762
+ OldStoreName = ? PERSISTENT_MSG_STORE ,
2739
2763
ok = rabbit_sup :start_child (OldStoreName , rabbit_msg_store ,
2740
2764
[OldStoreName , rabbit_mnesia :dir (),
2741
2765
Refs , StartFunState ]),
2742
2766
OldStoreName .
2743
2767
2744
- run_persistent_store (Vhost ) ->
2745
-
2746
-
2747
- ? PERSISTENT_MSG_STORE .
2748
-
2749
2768
start_new_store_sup () ->
2750
2769
% Start persistent store sup without recovery.
2751
- ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE , rabbit_msg_store_vhost_sup ,
2752
- [? PERSISTENT_MSG_STORE , rabbit_mnesia :dir (),
2770
+ ok = rabbit_sup :start_child (? PERSISTENT_MSG_STORE_SUP , rabbit_msg_store_vhost_sup ,
2771
+ [? PERSISTENT_MSG_STORE_SUP , rabbit_mnesia :dir (),
2753
2772
undefined , {fun (ok ) -> finished end , ok }]),
2754
- ? PERSISTENT_MSG_STORE .
2773
+ ? PERSISTENT_MSG_STORE_SUP .
2755
2774
2756
2775
delete_old_store (OldStore ) ->
2757
2776
gen_server :stop (OldStore ),
2758
- rabbit_file :recursive_delete (
2759
- filename :join ([rabbit_mnesia :dir (), ? PERSISTENT_MSG_STORE ])).
2760
-
2777
+ rabbit_file :recursive_delete ([filename :join ([rabbit_mnesia :dir (), ? PERSISTENT_MSG_STORE ])]).
2761
2778
2762
2779
2763
2780
2781
+ setup () ->
2782
+ application :load (rabbit ),
2783
+ mnesia :start (),
2784
+ rabbit_sup :start_link (),
2785
+ rabbit :start_fhc (),
2786
+ rabbit_sup :start_restartable_child (rabbit_guid ),
2787
+ rabbit_sup :start_supervisor_child (worker_pool_sup ).
2764
2788
0 commit comments