[erlang-questions] time-limited mnesia operations

Hakan Mattsson <>
Tue Jan 20 14:02:35 CET 2009


On Mon, 19 Jan 2009, Dmitriy Kargapolov wrote:

> Hi,
> We have to limit in time heavy set of mnesia updates. If update or load was
> not completed in scheduled period of time, it just does not make sense to
> continue database operation, wasting resources. We'd like to break process
> in that case and make sure database will not be broken (but it may be
> inconsistent from application point of view), and will be ready for future
> cleanup and load.
> I guess this could be easy to implement in transactional context, but I need
> this to be done in dirty context as well, including dirty_sync.
> I could come up with something like this:
> Pid = spawn_link( fun() ->
>     mnesia:dirty_write(Record),
>     ...
>     mnesia:dirty_write(Record)
> end ),
> timer:exit_after(Time, Pid, timeout).
> But I'm not sure how much atomic is mnesia:dirty_write call, wouldn't be
> mnesia broken on some lower level if process Pid get signal in the middle of
> write operation.
> More complex case would be use of mnesia:sync_dirty() call and at least two
> nodes.
> Does anybody has experience of safe breaking "hanging" updates?
> Thanks.

Dirty updates should be avoided as far as possible as they may
leave the database in an inconsistent state. Use transactions
if you can afford them. They are safe.

If you persist in using dirty updates, you may improve your code
example a little by not killing the process in the middle of the
dirty update:

Ref = make_ref(),
Pid = spawn_link( fun() ->
    mnesia:dirty_write(Record),
    ...
    receive {Ref, safe_exit, Reason} -> exit(Reason) end,
    ...
    mnesia:dirty_write(Record),
    ...
    receive {Ref, safe_exit, Reason} -> exit(Reason) end,
    ...
    mnesia:dirty_write(Record)
end ),
erlang:send_after:exit_after(Time, Pid, {Ref, safe_exit, timeout}).

/Håkan
---
Håkan Mattsson (uabhams)
Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list