Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang Mnesia - mnesia:transaction(F) vs. mnesia:activity(transaction, F)

I've read and re-read the docs re: mnesia:activity/3, mnesia:activity/4, and mnesia/transaction/2, but they still read like an obscure foreign language to me.

In my limited experiments they seem to return the same result.

Can some kind soul help me understand when and why I would use one vs. the other?

Many thanks,

LRP

like image 558
Lloyd R. Prentice Avatar asked Dec 19 '22 22:12

Lloyd R. Prentice


2 Answers

There are two differences.

First, mnesia:activity lets you specify the access module. The only use for this I know of is to use fragmented tables. That is, if you have a table with many records, you may want to split the data among the nodes in your cluster. The mnesia_frag access module lets you do that, and still access the data transparently - as long as you use mnesia:activity and specify mnesia_frag as the access module. See the section on Table Fragmentation in the Mnesia documentation for more information.

Second, mnesia:transaction returns {atomic, Result} on success and {aborted, Reason} on error. On the other hand, mnesia:activity simply returns the Result on success, and signals an error if there is a problem. Which style you prefer is mostly a matter of taste - but note that mnesia:transaction can fail "silently" if you don't check every return value.

Apart from those differences in functionality, with mnesia:activity you can easily change the access context in your code as needed. If you start out using transactions but later want to change to dirty operations (or vice versa), you can simply change the first argument to your mnesia:activity calls, while if you have been using mnesia:transaction and direct calls to mnesia:dirty_*, changing from one to another is a bit more involved. (You could mitigate the latter somewhat by using mnesia:sync_dirty and/or mnesia:async_dirty).

like image 56
legoscia Avatar answered Apr 13 '23 01:04

legoscia


First of all, think "Transaction" as a concept, which is nothing but a guarantee that a sequence of operations will be done atomically (consistent, isolated and durable as well making the ACID property).

If any of the operation fails, the actions performed by previous operations in that transaction will be rolled back.

Now in Erlang, you have different types of transactions supported [ Check here ]:

  1. transaction (regular one)
  2. sync_transaction (waits until all active replicas has committed the transaction (to disc) )
  3. async_dirty (No locking and updates are done asynchronously)
  4. sync_dirty (same as async_dirty, just that the call will wait until all nodes are updates)
  5. ets (For RAM copies which doesnt scale out to multiple nodes)

Now, back to the question, What is Activity then ?

How I see it is, activity provides me with a generic interface using which I can hide the details of like which kind of transaction I am actually doing. In your application, you may have different consistency and performance requirement in different parts or functionalities.

So, you can just pass the type of transaction you want to do as the first parameter of the activity call [ Check Here ].

Besides that, the biggest difference is, activity lets you customize the access mode of records in Mnesia (Explained in the second link). Which I must say, must be done by a very experienced erlang programmer only. For most of the cases the default one provided must suffice.

Excepts from the documentation:

This function (mnesia:activity/4) differs in an important aspect from the mnesia:transaction, mnesia:sync_transaction, mnesia:async_dirty, mnesia:sync_dirty and mnesia:ets functions. The AccessMod argument is the name of a callback module which implements the mnesia_access behavior

like image 34
Arunmu Avatar answered Apr 13 '23 01:04

Arunmu