summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Radestock <matthias@rabbitmq.com>2014-04-21 12:49:43 +0100
committerMatthias Radestock <matthias@rabbitmq.com>2014-04-21 12:49:43 +0100
commit89356db4a339ada3764dc36024a3ba20ca50e102 (patch)
treebcd7e9c18cffe1c47e1e8dd0fa3703e25285144a
parente090a4f0c39b0e6c2fa94ad3185279039e6f5e8d (diff)
downloadrabbitmq-server-89356db4a339ada3764dc36024a3ba20ca50e102.tar.gz
enforce gm GROUP_TABLE abstraction
...by using mnesia:write/3 instead of /1 and generally abstract mnesia reading/writing and make it dirty reads obvious.
-rw-r--r--src/gm.erl38
1 files changed, 17 insertions, 21 deletions
diff --git a/src/gm.erl b/src/gm.erl
index 0b0ada21..1c7a8894 100644
--- a/src/gm.erl
+++ b/src/gm.erl
@@ -636,7 +636,7 @@ handle_cast({?TAG, ReqVer, Msg},
callback_args = Args }) ->
{Result, State1} =
case needs_view_update(ReqVer, View) of
- true -> View1 = group_to_view(read_group(GroupName)),
+ true -> View1 = group_to_view(dirty_read_group(GroupName)),
MemberState1 = remove_erased_members(MembersState, View1),
{callback_view_changed(Args, Module, View, View1),
check_neighbours(
@@ -1037,7 +1037,7 @@ ensure_alive_suffix1(MembersQ) ->
%% ---------------------------------------------------------------------------
join_group(Self, GroupName, TxnFun) ->
- join_group(Self, GroupName, read_group(GroupName), TxnFun).
+ join_group(Self, GroupName, dirty_read_group(GroupName), TxnFun).
join_group(Self, GroupName, {error, not_found}, TxnFun) ->
join_group(Self, GroupName,
@@ -1080,27 +1080,29 @@ join_group(Self, GroupName, #gm_group { members = Members } = Group, TxnFun) ->
end
end.
-read_group(GroupName) ->
+dirty_read_group(GroupName) ->
case mnesia:dirty_read(?GROUP_TABLE, GroupName) of
[] -> {error, not_found};
[Group] -> Group
end.
+read_group(GroupName) -> mnesia:read({?GROUP_TABLE, GroupName}).
+
+write_group(Group) -> mnesia:write(?GROUP_TABLE, Group, write), Group.
+
prune_or_create_group(Self, GroupName, TxnFun) ->
Group = TxnFun(
fun () ->
GroupNew = #gm_group { name = GroupName,
members = [Self],
version = get_version(Self) },
- case mnesia:read({?GROUP_TABLE, GroupName}) of
+ case read_group(GroupName) of
[] ->
- mnesia:write(GroupNew),
- GroupNew;
+ write_group(GroupNew);
[Group1 = #gm_group { members = Members }] ->
case lists:any(fun is_member_alive/1, Members) of
true -> Group1;
- false -> mnesia:write(GroupNew),
- GroupNew
+ false -> write_group(GroupNew)
end
end
end),
@@ -1117,10 +1119,8 @@ record_dead_member_in_group(Member, GroupName, TxnFun) ->
Group1;
{Members1, [Member | Members2]} ->
Members3 = Members1 ++ [{dead, Member} | Members2],
- Group2 = Group1 #gm_group { members = Members3,
- version = Ver + 1 },
- mnesia:write(Group2),
- Group2
+ write_group(Group1 #gm_group { members = Members3,
+ version = Ver + 1 })
end
end),
Group.
@@ -1136,9 +1136,7 @@ record_new_member_in_group(GroupName, Left, NewMember, Fun, TxnFun) ->
Members1 = Prefix ++ [Left, NewMember | Suffix],
Group2 = Group1 #gm_group { members = Members1,
version = Ver + 1 },
- Result = Fun(Group2),
- mnesia:write(Group2),
- {Result, Group2}
+ {Fun(Group2), write_group(Group2)}
end),
{Result, Group}.
@@ -1152,11 +1150,9 @@ erase_members_in_group(Members, GroupName, TxnFun) ->
mnesia:read({?GROUP_TABLE, GroupName}),
case Members1 -- DeadMembers of
Members1 -> Group1;
- Members2 -> Group2 =
- Group1 #gm_group { members = Members2,
- version = Ver + 1 },
- mnesia:write(Group2),
- Group2
+ Members2 -> write_group(
+ Group1 #gm_group { members = Members2,
+ version = Ver + 1 })
end
end),
Group.
@@ -1321,7 +1317,7 @@ prepare_members_state(MembersState) -> ?DICT:to_list(MembersState).
build_members_state(MembersStateList) -> ?DICT:from_list(MembersStateList).
make_member(GroupName) ->
- {case read_group(GroupName) of
+ {case dirty_read_group(GroupName) of
#gm_group { version = Version } -> Version;
{error, not_found} -> ?VERSION_START
end, self()}.