Skip to content
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

Fix #7783: Deadlock during index creation when parallel workers are used #8163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/jrd/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,8 @@ Statement::Statement(thread_db* tdbb, MemoryPool* p, CompilerScratch* csb)

case Resource::rsc_index:
{
jrd_rel* relation = resource->rsc_rel;
IndexLock* index = CMP_get_index_lock(tdbb, relation, resource->rsc_id);
if (index)
{
++index->idl_count;
if (index->idl_count == 1) {
LCK_lock(tdbb, index->idl_lock, LCK_SR, LCK_WAIT);
}
}
// No need to lock the index here because it is already
// locked by Optimizer::compileRelation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is good, but assert will not harm, imho

break;
}

Expand Down
54 changes: 53 additions & 1 deletion src/jrd/optimizer/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,38 @@ void Optimizer::compileRelation(StreamType stream)
}
}

{ // scope
ThreadStatusGuard tempStatus(tdbb);

// Check if some indices are being deleted and don't use them in this case
for (auto idx = idxList.begin(); idx < idxList.end();)
{
IndexLock* idx_lock = CMP_get_index_lock(tdbb, relation, idx->idx_id);

if (idx_lock)
{
if (idx_lock->idl_count == 1 && idx_lock->idl_lock->lck_logical == LCK_EX)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that idl_count > 1 ?
I.e. perhaps it should check for idl_count > 0 here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like currently LCK_EX is possible only with idl_count == 1. Should I correct it to idl_count > 0 to make it more safe (maybe for future changes)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, do it, please. It makes no harm but protect us from "impossible" cases ;)
Also, consider to add assert, if you sure it is really impossible.

{
idxList.remove(idx);
continue;
}
else
{
if (idx_lock->idl_count == 0 && !LCK_lock(tdbb, idx_lock->idl_lock, LCK_SR, LCK_NO_WAIT))
{
idxList.remove(idx);
continue;
}

fb_assert(idx_lock->idl_lock->lck_logical == LCK_SR);
idx_lock->idl_count++;
}
}

idx++;
}
}

if (idxList.hasData())
tail->csb_idx = FB_NEW_POOL(getPool()) IndexDescList(getPool(), idxList);

Expand Down Expand Up @@ -1656,6 +1688,27 @@ void Optimizer::checkIndices()
for (const auto compileStream : compileStreams)
{
const auto tail = &csb->csb_rpt[compileStream];
const auto relation = tail->csb_relation;

if (tail->csb_idx && relation)
{
// Release locks of unused indices
for (const auto& idx : *tail->csb_idx)
{
if (!(idx.idx_runtime_flags & idx_used))
{
IndexLock* index = CMP_get_index_lock(tdbb, relation, idx.idx_id);

if (index && index->idl_count)
{
--index->idl_count;

if (!index->idl_count)
LCK_release(tdbb, index->idl_lock);
}
}
}
}

const auto plan = tail->csb_plan;
if (!plan)
Expand All @@ -1664,7 +1717,6 @@ void Optimizer::checkIndices()
if (plan->type != PlanNode::TYPE_RETRIEVE)
continue;

const auto relation = tail->csb_relation;
if (!relation)
return;

Expand Down
Loading