Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  Mnesia deadlock detector

luke at not-a-domain.com
Posted: Sun Aug 08, 1999 7:35 pm Reply with quote
Guest
Hi all, me again,

I've been poking around with mnesia this time, and I've found some
strange behaviour apparently relating to deadlock detection. I have a
function which I'm using for transactions:

fun() -> mnesia:write(#test_rec{}) end

With: -record(test_rec, {id=1, date=now()}).

Running that function in mnesia:transaction/1 seems to take about
0.7ms on my machine. However, when I run that function several times
concurrently the performance drops hugely. It looks like Mnesia is
thinking there are deadlocks, because it cancels the transactions that
miss out on the write lock, puts them to sleep for a random length of
time (I've experienced over 900ms), and then wakes them again (at
which point they seem to sleep/restart again if the lock isn't free).

Thought I'd report it incase it's a bug. It sounds like
deadlock-avoidance behaviour, but all that's actually occuring here is
contention for the write lock as far as I can tell. Or maybe I'm
doing something silly. :-)

Code and output attached. I'm using Mnesia 3.6.

Cheers,
Luke



Post generated using Mail2Forum (http://m2f.sourceforge.net)
dgud at erix.ericsson.se
Posted: Mon Aug 09, 1999 12:14 pm Reply with quote
Guest
Hi Luke

You're right it is the deadlock avoidance algo.
The lock will usally be queued but if that's not allowed
(due to wrong order in transaction identifier)
the transaction will sleep for awhile and then be restarted.

The sleeping time has been decreased in mnesia 3.7

/Dan

Luke Gorrie writes:
> Hi all, me again,
>
> I've been poking around with mnesia this time, and I've found some
> strange behaviour apparently relating to deadlock detection. I have a
> function which I'm using for transactions:
>
> fun() -> mnesia:write(#test_rec{}) end
>
> With: -record(test_rec, {id=1, date=now()}).
>
> Running that function in mnesia:transaction/1 seems to take about
> 0.7ms on my machine. However, when I run that function several times
> concurrently the performance drops hugely. It looks like Mnesia is
> thinking there are deadlocks, because it cancels the transactions that
> miss out on the write lock, puts them to sleep for a random length of
> time (I've experienced over 900ms), and then wakes them again (at
> which point they seem to sleep/restart again if the lock isn't free).
>
> Thought I'd report it incase it's a bug. It sounds like
> deadlock-avoidance behaviour, but all that's actually occuring here is
> contention for the write lock as far as I can tell. Or maybe I'm
> doing something silly. Smile
>
> Code and output attached. I'm using Mnesia 3.6.
>
> Cheers,
> Luke
>
> %%%----------------------------------------------------------------------
> %%% File : mnesia_deadlock.erl
> %%% Author : Luke Gorrie <luke_at_not-a-domain.com>
> %%% Purpose : Try to induce a lot of deadlocks into Mnesia to see if it'd
> %%% be useful to add "upgrade" locks.
> %%% Created : 9 Aug 1999 by Luke Gorrie <luke_at_not-a-domain.com>
> %%%----------------------------------------------------------------------
>
> -module(mnesia_deadlock).
> -author('luke_at_not-a-domain.com').
>
> -record(test_rec, {id=1, date=now()}).
>
> -export([init/0,run_test/1,cleanup/0, transaction/2]).
>
> init() ->
> mnesia:create_table(test_rec,
> [{attributes, record_info(fields, test_rec)}]),
> mnesia:transaction(fun() -> mnesia:write(#test_rec{}) end).
>
> cleanup() ->
> mnesia:delete_table(test_rec).
>
> run_test(N) ->
> Deadlocker =
> fun() ->
> mnesia:write(#test_rec{})
> end,
> spawn_worker(N, Deadlocker),
> wait_responses(N).
>
> spawn_worker(0, _F) ->
> ok;
> spawn_worker(N, F) ->
> spawn_link(?MODULE, transaction, [self(), F]),
> %transaction(self(), F),
> spawn_worker(N-1, F).
>
> wait_responses(0) ->
> ok;
> wait_responses(N) ->
> receive
> finished ->
> wait_responses(N-1)
> end.
>
> transaction(Pid, F) ->
> {atomic, _} = mnesia:transaction(F),
> Pid ! finished.
>
>
> ------------------------------------------------------------
>
> Erlang (JAM) emulator version 47.4.1
>
> Eshell V47.4.1 (abort with ^G)
> (emacs_at_baked.vegetable.org)1> c("/home/luke/devel/erlang/mnesia_deadlock", [{outdir, "/home/luke/devel/erlang/"}]).
> {ok,mnesia_deadlock}
> (emacs_at_baked.vegetable.org)2> mnesia:start().
> ok
> (emacs_at_baked.vegetable.org)3> mnesia_deadlock:init().
> {atomic,ok}
> (emacs_at_baked.vegetable.org)4> mnesia_lib:set(debug, trace).
> true
> (emacs_at_baked.vegetable.org)5> mnesia_deadlock:run_test(3).
> Mnesia('emacs_at_baked.vegetable.org'): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.92.0>,
> normal}
> Mnesia('emacs_at_baked.vegetable.org'): Restarting transaction {tid,16,<0.93.0>}: in 358ms {cyclic,'emacs_at_baked.vegetable.org',{test_rec,1},write,write,{tid,15,<0.92.0>}}
> Mnesia('emacs_at_baked.vegetable.org'): Restarting transaction {tid,17,<0.94.0>}: in 359ms {cyclic,'emacs_at_baked.vegetable.org',{test_rec,1},write,write,{tid,15,<0.92.0>}}
> Mnesia('emacs_at_baked.vegetable.org'): Restarting transaction {tid,17,<0.94.0>}: in 263ms {cyclic,'emacs_at_baked.vegetable.org',{test_rec,1},write,write,{tid,16,<0.93.0>}}
> Mnesia('emacs_at_baked.vegetable.org'): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.93.0>,
> normal}
> ok
> Mnesia('emacs_at_baked.vegetable.org'): ** ERROR ** mnesia_tm got local EXIT from unknown process: {<0.94.0>,
> normal}
> (emacs_at_baked.vegetable.org)6>

--
Dan Gudmundsson Project: Mnesia, Erlang/OTP
Ericsson Utvecklings AB Phone: +46 8 727 5762
UAB/F/P Mobile: +46 70 519 9469
S-125 25 Stockholm Visit addr: Armborstv 1



Post generated using Mail2Forum (http://m2f.sourceforge.net)

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum