-
Notifications
You must be signed in to change notification settings - Fork 119
Issue 7462 - Fix SERIAL LOCK and entry cache lock ordering on BDB dea… #7463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vashirov
wants to merge
1
commit into
389ds:main
Choose a base branch
from
vashirov:i7462
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,8 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| Connection *pb_conn; | ||
| int32_t parent_op = 0; | ||
| int32_t betxn_callback_fails = 0; /* if a BETXN fails we need to revert entry cache */ | ||
| int32_t e_locked = 0; /* non-zero when entry 'e' cache lock is held */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should better use bool and true/false |
||
| int32_t parent_locked = 0; /* non-zero when parent entry cache lock is held */ | ||
| struct timespec parent_time; | ||
|
|
||
| if (slapi_pblock_get(pb, SLAPI_CONN_ID, &conn_id) < 0) { | ||
|
|
@@ -198,8 +200,23 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) { /* retry_count > 0 */ | ||
| Slapi_Entry *ent = NULL; | ||
|
|
||
| /* Don't release SERIAL LOCK */ | ||
| dblayer_txn_abort_ext(li, &txn, PR_FALSE); | ||
| /* | ||
| * Unlock entry cache locks before releasing SERIAL LOCK | ||
| * to maintain lock ordering (SERIAL LOCK -> entry locks) | ||
| * and prevent AB-BA deadlock on retry. | ||
| * Entries stay refcounted in cache, so pointers remain valid. | ||
| */ | ||
| if (e_locked) { | ||
| cache_unlock_entry(&inst->inst_cache, e); | ||
| e_locked = 0; | ||
| } | ||
| if (parent_locked && parent_modify_c.old_entry) { | ||
| cache_unlock_entry(&inst->inst_cache, parent_modify_c.old_entry); | ||
| parent_locked = 0; | ||
| } | ||
|
|
||
| /* Release SERIAL LOCK */ | ||
| dblayer_txn_abort(be, &txn); | ||
| slapi_pblock_set(pb, SLAPI_TXN, parent_txn); | ||
|
|
||
| /* reset original entry */ | ||
|
|
@@ -257,13 +274,7 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| } | ||
| #endif | ||
| } | ||
| if (0 == retry_count) { | ||
| /* First time, hold SERIAL LOCK */ | ||
| retval = dblayer_txn_begin(be, parent_txn, &txn); | ||
| } else { | ||
| /* Otherwise, no SERIAL LOCK */ | ||
| retval = dblayer_txn_begin_ext(li, parent_txn, &txn, PR_FALSE); | ||
| } | ||
| retval = dblayer_txn_begin(be, parent_txn, &txn); | ||
| if (0 != retval) { | ||
| if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1; | ||
| ldap_result_code= LDAP_OPERATIONS_ERROR; | ||
|
|
@@ -273,6 +284,39 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| /* stash the transaction */ | ||
| slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn); | ||
|
|
||
| /* Re-lock entries after SERIAL LOCK to maintain lock ordering */ | ||
| if (retry_count > 0) { | ||
| if (e) { | ||
| int lock_rc = cache_lock_entry(&inst->inst_cache, e); | ||
| if (lock_rc != 0) { | ||
| slapi_log_err(SLAPI_LOG_ERR, "ldbm_back_delete", | ||
| "conn=%" PRIu64 " op=%d Failed to re-lock entry " | ||
| "after deadlock retry (rc=%d)\n", | ||
| conn_id, op_id, lock_rc); | ||
| CACHE_RETURN(&inst->inst_cache, &e); | ||
| e = NULL; | ||
| ldap_result_code = LDAP_OPERATIONS_ERROR; | ||
| retval = -1; | ||
| goto error_return; | ||
| } | ||
| e_locked = 1; | ||
| } | ||
| if (parent_found && parent_modify_c.old_entry) { | ||
| int lock_rc = cache_lock_entry(&inst->inst_cache, | ||
| parent_modify_c.old_entry); | ||
| if (lock_rc != 0) { | ||
| slapi_log_err(SLAPI_LOG_ERR, "ldbm_back_delete", | ||
| "conn=%" PRIu64 " op=%d Failed to re-lock parent " | ||
| "entry after deadlock retry (rc=%d)\n", | ||
| conn_id, op_id, lock_rc); | ||
| ldap_result_code = LDAP_OPERATIONS_ERROR; | ||
| retval = -1; | ||
| goto error_return; | ||
| } | ||
| parent_locked = 1; | ||
| } | ||
| } | ||
|
|
||
| if (0 == retry_count) { /* just once */ | ||
| /* find and lock the entry we are about to modify */ | ||
| /* | ||
|
|
@@ -291,6 +335,7 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_delete", "Deleting entry is already deleted.\n"); | ||
| goto error_return; /* error result sent by find_entry2modify() */ | ||
| } | ||
| e_locked = 1; | ||
| ep_id = e->ep_id; | ||
|
|
||
| /* JCMACL - Shouldn't the access check be before the has children check... | ||
|
|
@@ -356,6 +401,7 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| CACHE_REMOVE(&inst->inst_cache, e); | ||
| } | ||
| cache_unlock_entry(&inst->inst_cache, e); | ||
| e_locked = 0; | ||
| CACHE_RETURN(&inst->inst_cache, &e); | ||
| /* | ||
| * e is unlocked and no longer in cache. | ||
|
|
@@ -540,17 +586,49 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| * Its possible that the parent entry retrieved from the cache in id2entry | ||
| * could be removed before we lock it, because tombstone purging updated/replaced | ||
| * the parent. If we fail to lock the entry, just try again. | ||
| * | ||
| * Must not sleep while holding SERIAL LOCK -- abort txn | ||
| * first, sleep, then re-begin (same as deadlock retry). | ||
| */ | ||
| while (1) { | ||
| parent = id2entry(be, pid, &txn, &retval); | ||
| if (parent && (cache_retry = cache_lock_entry(&inst->inst_cache, parent))) { | ||
| /* Failed to obtain parent entry's entry lock */ | ||
| if (cache_retry == RETRY_CACHE_LOCK && | ||
| cache_retry_count < LDBM_CACHE_RETRY_COUNT) { | ||
| /* try again */ | ||
| CACHE_RETURN(&(inst->inst_cache), &parent); | ||
| /* Unlock entry, release SERIAL LOCK, sleep, re-acquire */ | ||
| cache_unlock_entry(&inst->inst_cache, e); | ||
| e_locked = 0; | ||
| dblayer_txn_abort(be, &txn); | ||
| slapi_pblock_set(pb, SLAPI_TXN, parent_txn); | ||
| DS_Sleep(PR_MillisecondsToInterval(100)); | ||
| cache_retry_count++; | ||
| txn.back_txn_txn = NULL; | ||
| retval = dblayer_txn_begin(be, parent_txn, &txn); | ||
| if (0 != retval) { | ||
| if (LDBM_OS_ERR_IS_DISKFULL(retval)) | ||
| disk_full = 1; | ||
| ldap_result_code = LDAP_OPERATIONS_ERROR; | ||
| goto error_return; | ||
| } | ||
| slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn); | ||
| /* Re-lock target entry */ | ||
| { | ||
| int lock_rc = cache_lock_entry(&inst->inst_cache, e); | ||
| if (lock_rc != 0) { | ||
| slapi_log_err(SLAPI_LOG_ERR, "ldbm_back_delete", | ||
| "conn=%" PRIu64 " op=%d Failed to re-lock " | ||
| "entry during parent cache lock retry (rc=%d)\n", | ||
| conn_id, op_id, lock_rc); | ||
| CACHE_RETURN(&inst->inst_cache, &e); | ||
| e = NULL; | ||
| ldap_result_code = LDAP_OPERATIONS_ERROR; | ||
| retval = -1; | ||
| goto error_return; | ||
| } | ||
| e_locked = 1; | ||
| } | ||
| continue; | ||
| } | ||
| retval = -1; | ||
|
|
@@ -576,6 +654,7 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| int isglue; | ||
| size_t haschildren = 0; | ||
| int op = PARENTUPDATE_DEL; | ||
| parent_locked = 1; | ||
|
|
||
| /* Unfortunately findentry doesn't tell us whether it just | ||
| * didn't find the entry, or if there was an error, so we | ||
|
|
@@ -1507,7 +1586,10 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| CACHE_REMOVE(&inst->inst_cache, e); | ||
| } | ||
| } | ||
| cache_unlock_entry(&inst->inst_cache, e); | ||
| if (e_locked) { | ||
| cache_unlock_entry(&inst->inst_cache, e); | ||
| e_locked = 0; | ||
| } | ||
| CACHE_RETURN(&inst->inst_cache, &e); | ||
| /* | ||
| * e is unlocked and no longer in cache. | ||
|
|
@@ -1549,6 +1631,12 @@ int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from | |
| "conn=%" PRIu64 " op=%d modify_term: old_entry=0x%p, new_entry=0x%p, in_cache=%d\n", | ||
| conn_id, op_id, parent_modify_c.old_entry, parent_modify_c.new_entry, | ||
| inst ? cache_is_in_cache(&inst->inst_cache, parent_modify_c.new_entry):-1); | ||
| /* Parent unlocked during retry but not re-locked (error path). | ||
| * Return to cache and NULL out to prevent double-unlock in modify_term. */ | ||
| if (!parent_locked && parent_modify_c.old_entry) { | ||
| CACHE_RETURN(&(inst->inst_cache), &(parent_modify_c.old_entry)); | ||
| parent_modify_c.old_entry = NULL; | ||
| } | ||
| myrc = modify_term(&parent_modify_c, be); | ||
| if (free_delete_existing_entry) { | ||
| done_with_pblock_entry(pb, SLAPI_DELETE_EXISTING_ENTRY); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there is not a confusion between backend lock and db_env lock.
Initially the backend lock was held during all lmdb_add and db_env release during retry/sleep.
With the move of plugins in betxn (#351) it was changed from dblayer_txn_abort (release the db_env) to dblayer_txn_abort_ext(...FALSE) (to not release the db_env) but the comment was about 'Serial Lock' that in my mind means backend lock.
Of course we do not want to release the backend lock for a retry but it looks fine to me to release db_env.
What is not clear to me is the impact of releasing it. IIUC it allows others txn but on different backend. Is it the expected benefit ?