Skip to content

Improvement #8042 : Improve conflict resolution on replica when table have both Primary and Unique keys #8043

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

Merged
merged 1 commit into from
Mar 16, 2024
Merged
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
37 changes: 32 additions & 5 deletions src/jrd/replication/Applier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ namespace
}
};

class LocalThreadContext
class LocalThreadContext : Firebird::ContextPoolHolder
{
public:
LocalThreadContext(thread_db* tdbb, jrd_tra* tra, jrd_req* req = NULL)
: m_tdbb(tdbb)
: Firebird::ContextPoolHolder(req ? req->req_pool : tdbb->getDefaultPool()),
m_tdbb(tdbb)
{
tdbb->setTransaction(tra);
tdbb->setRequest(req);
Expand Down Expand Up @@ -560,6 +561,8 @@ void Applier::insertRecord(thread_db* tdbb, TraNumber traNum,
rpb.rpb_length = length;
record->copyDataFrom(data);

FbLocalStatus error;

try
{
doInsert(tdbb, &rpb, transaction);
Expand All @@ -574,6 +577,7 @@ void Applier::insertRecord(thread_db* tdbb, TraNumber traNum,
throw;
}

ex.stuffException(&error);
fb_utils::init_status(tdbb->tdbb_status_vector);

// The subsequent backout will delete the blobs we have stored before,
Expand Down Expand Up @@ -614,8 +618,14 @@ void Applier::insertRecord(thread_db* tdbb, TraNumber traNum,
bool found = false;

#ifdef RESOLVE_CONFLICTS
fb_assert(error[1] == isc_unique_key_violation || error[1] == isc_no_dup);
fb_assert(error[2] == isc_arg_string);
fb_assert(error[3] != 0);

const char* idxName = reinterpret_cast<const char*>(error[3]);

index_desc idx;
const auto indexed = lookupRecord(tdbb, relation, record, m_bitmap, idx);
const auto indexed = lookupRecord(tdbb, relation, record, m_bitmap, idx, idxName);

AutoPtr<Record> cleanup;

Expand Down Expand Up @@ -1060,7 +1070,7 @@ bool Applier::compareKey(thread_db* tdbb, jrd_rel* relation, const index_desc& i
bool Applier::lookupRecord(thread_db* tdbb,
jrd_rel* relation, Record* record,
RecordBitmap* bitmap,
index_desc& idx)
index_desc& idx, const char* idxName)
{
RecordBitmap::reset(bitmap);

Expand All @@ -1071,7 +1081,24 @@ bool Applier::lookupRecord(thread_db* tdbb,
return false;
}

if (lookupKey(tdbb, relation, idx))
bool haveIdx = false;
if (idxName)
{
SLONG foundRelId;
IndexStatus idxStatus;
SLONG idx_id = MET_lookup_index_name(tdbb, idxName, &foundRelId, &idxStatus);

fb_assert(idxStatus == MET_object_active);
fb_assert(foundRelId == relation->rel_id);

haveIdx = (idxStatus == MET_object_active) && (foundRelId == relation->rel_id) &&
BTR_lookup(tdbb, relation, idx_id, &idx, relation->getPages(tdbb));
}

if (!haveIdx)
haveIdx = lookupKey(tdbb, relation, idx);

if (haveIdx)
{
temporary_key key;
const auto result = BTR_key(tdbb, relation, record, &idx, &key,
Expand Down
2 changes: 1 addition & 1 deletion src/jrd/replication/Applier.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ namespace Jrd
Record* record1, Record* record2);
bool lookupRecord(thread_db* tdbb, jrd_rel* relation,
Record* record, RecordBitmap* bitmap,
index_desc& idx);
index_desc& idx, const char* idxName = nullptr);

const Format* findFormat(thread_db* tdbb, jrd_rel* relation, ULONG length);

Expand Down