From 723e6e4e412b81e47f3a9db5451a1342debcf79e Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Wed, 10 Feb 2016 16:31:34 +0300 Subject: [PATCH] Create secondary indices on rabbit_tracked_connection.vhost and username --- src/rabbit_mnesia.erl | 4 +++- src/rabbit_table.erl | 11 ++++++++++- src/rabbit_upgrade_functions.erl | 15 +++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/rabbit_mnesia.erl b/src/rabbit_mnesia.erl index afd0508aac2d..2a68df06b3c6 100644 --- a/src/rabbit_mnesia.erl +++ b/src/rabbit_mnesia.erl @@ -460,12 +460,14 @@ init_db(ClusterNodes, NodeType, CheckOtherNodes) -> {[], true, disc} -> %% First disc node up maybe_force_load(), + ok = rabbit_table:ensure_secondary_indecies(), ok; {[_ | _], _, _} -> %% Subsequent node in cluster, catch up maybe_force_load(), ok = rabbit_table:wait_for_replicated(), - ok = rabbit_table:create_local_copy(NodeType) + ok = rabbit_table:create_local_copy(NodeType), + ok = rabbit_table:ensure_secondary_indecies() end, ensure_schema_integrity(), rabbit_node_monitor:update_cluster_status(), diff --git a/src/rabbit_table.erl b/src/rabbit_table.erl index cc30e28bef84..db95a768083d 100644 --- a/src/rabbit_table.erl +++ b/src/rabbit_table.erl @@ -18,7 +18,8 @@ -export([create/0, create_local_copy/1, wait_for_replicated/0, wait/1, force_load/0, is_present/0, is_empty/0, needs_default_data/0, - check_schema_integrity/0, clear_ram_only_tables/0, wait_timeout/0]). + check_schema_integrity/0, clear_ram_only_tables/0, wait_timeout/0, + ensure_secondary_indecies/0, ensure_secondary_indecies/2]). -include("rabbit.hrl"). @@ -54,6 +55,7 @@ create() -> Tab, TabDef1, Reason}}) end end, definitions()), + ok = rabbit_table:ensure_secondary_indecies(), ok. %% The sequence in which we delete the schema and then the other @@ -67,6 +69,13 @@ create_local_copy(ram) -> create_local_copies(ram), create_local_copy(schema, ram_copies). +ensure_secondary_indecies() -> + ensure_secondary_indecies(rabbit_tracked_connection, [vhost, username]), + ok. + +ensure_secondary_indecies(Tab, Fields) -> + [mnesia:add_table_index(Tab, Field) || Field <- Fields]. + wait_for_replicated() -> wait([Tab || {Tab, TabDef} <- definitions(), not lists:member({local_content, true}, TabDef)]). diff --git a/src/rabbit_upgrade_functions.erl b/src/rabbit_upgrade_functions.erl index 2056eed0dbd0..22e6ab296cd4 100644 --- a/src/rabbit_upgrade_functions.erl +++ b/src/rabbit_upgrade_functions.erl @@ -53,7 +53,7 @@ -rabbit_upgrade({recoverable_slaves, mnesia, [queue_state]}). -rabbit_upgrade({user_password_hashing, mnesia, [hash_passwords]}). -rabbit_upgrade({vhost_limits, mnesia, []}). --rabbit_upgrade({tracked_connection, mnesia, []}). +-rabbit_upgrade({tracked_connection, mnesia, [vhost_limits]}). %% ------------------------------------------------------------------- @@ -100,12 +100,14 @@ tracked_connection() -> {attributes, [id, node, vhost, name, pid, protocol, peer_host, peer_port, - username, connected_at]}]). + username, connected_at]}], + [vhost, username]). %% replaces vhost.dummy (used to avoid having a single-field record %% which Mnesia doesn't like) with vhost.limits (which is actually %% used) vhost_limits() -> + io:format("vhost_limits vhost_limits vhost_limits~n"), transform( rabbit_vhost, fun ({vhost, VHost, _Dummy}) -> @@ -488,6 +490,11 @@ create(Tab, TabDef) -> {atomic, ok} = mnesia:create_table(Tab, TabDef), ok. +create(Tab, TabDef, SecondaryIndices) -> + {atomic, ok} = mnesia:create_table(Tab, TabDef), + [mnesia:add_table_index(Tab, Idx) || Idx <- SecondaryIndices], + ok. + %% Dumb replacement for rabbit_exchange:declare that does not require %% the exchange type registry or worker pool to be running by dint of %% not validating anything and assuming the exchange type does not @@ -496,3 +503,7 @@ create(Tab, TabDef) -> declare_exchange(XName, Type) -> X = {exchange, XName, Type, true, false, false, []}, ok = mnesia:dirty_write(rabbit_durable_exchange, X). + +add_indices(Tab, FieldList) -> + [mnesia:add_table_index(Tab, Field) || Field <- FieldList], + ok.