dbTalk Databases Forums  

berkeley db causes itself to deadlock

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss berkeley db causes itself to deadlock in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
moshe
 
Posts: n/a

Default berkeley db causes itself to deadlock - 02-16-2006 , 10:56 AM






hello all,

my application is using secondary databases.
it seems that berkeley db can cause itself to deadlock when trying to
retrieve data
using 'pget' in one thread and at the same time trying to insert data,
using 'put' in another thread.

after a glance at the DB source code it seems like those functions are
working in the following manner:

put:
-----
1. acquire exclusive lock on the page in the primary database and
putting the data.
2. acquire exclusive lock on the page in the secondary database and
putting the data.

pget:
-------
1. acquire shared lock on the secondary database.
2. acquire shared lock on the primary database.

the two functions are blocking each other.
in order to avoid deadlock, the two functions need to access the data
in the same order.
is this a bug?
can you see the problem?

waiting for your reply,
moshe.


Reply With Quote
  #2  
Old   
Michael Cahill
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-16-2006 , 04:53 PM






Hi Moshe,

Deadlock is certainly possible in that situation, so your application
will need to deal with it (by running deadlock detection and handling
deadlock errors).

Berkeley DB does try to minimize deadlock in this case: nothing can be
done about secondary gets, since they have to read the secondary to
find the primary key. So primary put operations try hard to access the
secondaries before the primary. That is possible in many cases, but
not if doing a DB->c_put with the DB_CURRENT flag, or for puts with the
DB_DBT_PARTIAL flag set on the data.

Can you explain what flags you're using for the put, including the DBT
flags?

Regards,
Michael.


Reply With Quote
  #3  
Old   
Herman.Ron@gmail.com
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-19-2006 , 04:03 AM



Hi,
We use the C++ API wrapping of the Berkeley DB Interface, after
examining our code we discovered we get the deadlock when using the
DB_NOOVERWITE flag (with the C++ interface). We've managed to bypass
this deadlock by avoiding the use of this flag (which is very limiting,
and narrows down our application's capabilities), but we've encountered
another deadlock now, when deleting a record from the primary DB
(similar to writing without overwriting...)
It is not clear to us, why the Berkeley DB doesn't provide atomicity
for these actions, we expect the infrastructure to be deadlock free
when performing single instructions.
It would be of great help if you could come up with a solution for this
problem

As for now, the only solution we have in mind, is to maintain a locking
mechanism of our own...

Thanks in advance,
Ron & Moshe.


Reply With Quote
  #4  
Old   
Patrick Schaaf
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-19-2006 , 04:25 AM



Herman.Ron (AT) gmail (DOT) com writes:

Quote:
we expect the infrastructure to be deadlock free
when performing single instructions.
Where does this expectation come from?

Quote:
It would be of great help if you could come up with a solution for this
problem
Handle deadlocks.

best regards
Patrick


Reply With Quote
  #5  
Old   
Herman.Ron@gmail.com
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-20-2006 , 04:18 AM



Hi Patrrick
I was very upset after reading your reply... if you have nothing to
say, then don't say anything
I think that it is a very reasonable assumption to think the Berkeley
DB actions should be atomic as to one another... and on top of that,
the documentation doesn't mention anything about this limitation, I
personaly thought, that the same locking order in implemented on every
instruction the infrastructure supplies (in order to avoid such
dealocks!!!)

P.S :
A. handling dealock is not an option for us in our
application, due to pre-defined/pre-designed interface, which we're
basing on...
B. we tried using the supplied locking machanism, but the
documentation isn't clear regarding the " Dbt *object " pointer
supplied to these methods

Regards,
Ron & Moshe


Reply With Quote
  #6  
Old   
Patrick Schaaf
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-20-2006 , 05:17 AM



Herman.Ron (AT) gmail (DOT) com writes:

Quote:
I was very upset after reading your reply...
I am sorry to hear that.

Quote:
if you have nothing to say, then don't say anything
That is very good advise. I had to say two things. I said them.

all the best
Patrick


Reply With Quote
  #7  
Old   
Michael Cahill
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-20-2006 , 07:23 PM



Hi Ron and Moshe,

Quote:
We use the C++ API wrapping of the Berkeley DB Interface, after
examining our code we discovered we get the deadlock when using the
DB_NOOVERWITE flag (with the C++ interface).
That makes sense -- DB_NOOVERWRITE has to check whether the item
exists, which means it will lock the primary first.

Quote:
We've managed to bypass
this deadlock by avoiding the use of this flag (which is very limiting,
and narrows down our application's capabilities), but we've encountered
another deadlock now, when deleting a record from the primary DB
(similar to writing without overwriting...)
Again, that makes sense -- DB can't generate the secondary key(s) until
it has the record from the primary that is being deleted.

Quote:
It is not clear to us, why the Berkeley DB doesn't provide atomicity
for these actions, we expect the infrastructure to be deadlock free
when performing single instructions.
If you don't want to handle deadlock errors in your application, you
should consider the Concurrent Data Store product (open your
environment with DB_INIT_CDB instead of DB_INIT_LOCK | DB_INIT_LOG |
DB_INIT_TXN).

It implements a locking scheme similar to what you are describing
(coarse-grained read/write locking so that these operation would not
interleave). It is deadlock free. When used with secondaries, you
should also set the DB_CDB_ALLDB flag on the environment.

The major disadvantage of CDS is that it does not provide logging and
recovery, so you need to verify database integrity after application or
process failures, and potentially restore data from backups.

Regards,
Michael.



Reply With Quote
  #8  
Old   
Florian Weimer
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-21-2006 , 02:02 PM



* Patrick Schaaf:

Quote:
Herman.Ron (AT) gmail (DOT) com writes:

we expect the infrastructure to be deadlock free
when performing single instructions.

Where does this expectation come from?
This expectation is quite reasonable if you've got a certain SQL
background. Typical SQL databases, when used in auto-commit mode,
automatically recover from deadlocks.


Reply With Quote
  #9  
Old   
Patrick Schaaf
 
Posts: n/a

Default Re: berkeley db causes itself to deadlock - 02-22-2006 , 12:03 AM



Florian Weimer <fw (AT) deneb (DOT) enyo.de> writes:

Quote:
we expect the infrastructure to be deadlock free
when performing single instructions.

Where does this expectation come from?

This expectation is quite reasonable if you've got a certain SQL
background. Typical SQL databases, when used in auto-commit mode,
automatically recover from deadlocks.
Aha. Thank you. That would explain that. I have no SQL background
at all, so how could I know...

best regards
Patrick


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.