Skip to content

Commit

Permalink
Create secondary indices on rabbit_tracked_connection.vhost and username
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Feb 10, 2016
1 parent b468c0f commit 723e6e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/rabbit_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 10 additions & 1 deletion src/rabbit_table.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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").

Expand Down Expand Up @@ -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
Expand All @@ -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)]).
Expand Down
15 changes: 13 additions & 2 deletions src/rabbit_upgrade_functions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]}).

%% -------------------------------------------------------------------

Expand Down Expand Up @@ -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}) ->
Expand Down Expand Up @@ -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
Expand All @@ -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.

0 comments on commit 723e6e4

Please sign in to comment.