Skip to content

Add replication journal cleanup when drop database #8626

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
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
21 changes: 11 additions & 10 deletions src/common/os/guid.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ static_assert(sizeof(UUID) == 16, "Guid size mismatch");

namespace Firebird {

const int GUID_BUFF_SIZE = 39;
const int GUID_BODY_SIZE = 36;
constexpr int GUID_BUFF_SIZE = 39;

void GenerateRandomBytes(void* buffer, FB_SIZE_T size);

Expand All @@ -65,8 +64,10 @@ void GenerateGuid(UUID* guid);
class Guid
{
// Some versions of MSVC cannot recognize hh specifier but MSVC 2015 has it
static constexpr const char* GUID_FORMAT =
"{%08X-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}";
#define GUID_FORMAT_BASE "%08X-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX"
static constexpr const char* GUID_FORMAT = "{" GUID_FORMAT_BASE "}";
static constexpr const char* GUID_FORMAT_WITHOUT_BRACKETS = GUID_FORMAT_BASE;
#undef GUID_FORMAT_BASE
static constexpr int GUID_FORMAT_ARGS = 11;

Guid()
Expand Down Expand Up @@ -121,25 +122,25 @@ class Guid
memcpy(&m_data, buffer, SIZE);
}

void toString(char* buffer) const
void toString(char* buffer, bool withBrackets = true) const
{
sprintf(buffer, GUID_FORMAT,
sprintf(buffer, withBrackets ? GUID_FORMAT : GUID_FORMAT_WITHOUT_BRACKETS,
m_data.Data1, m_data.Data2, m_data.Data3,
m_data.Data4[0], m_data.Data4[1], m_data.Data4[2], m_data.Data4[3],
m_data.Data4[4], m_data.Data4[5], m_data.Data4[6], m_data.Data4[7]);
}

Firebird::string toString() const
Firebird::string toString(bool withBrackets = true) const
{
Firebird::string result;
toString(result.getBuffer(GUID_BUFF_SIZE - 1));
toString(result.getBuffer(GUID_BUFF_SIZE - (withBrackets ? 1 : 3)), withBrackets);
return result;
}

Firebird::PathName toPathName() const
Firebird::PathName toPathName(bool withBrackets = true) const
{
Firebird::PathName result;
toString(result.getBuffer(GUID_BUFF_SIZE - 1));
toString(result.getBuffer(GUID_BUFF_SIZE - (withBrackets ? 1 : 3)), withBrackets);
return result;
}

Expand Down
5 changes: 5 additions & 0 deletions src/jrd/jrd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3116,6 +3116,8 @@ JAttachment* JProvider::createDatabase(CheckStatusWrapper* user_status, const ch
if (attachment2)
{
allow_overwrite = attachment2->getHandle()->locksmith(tdbb, DROP_DATABASE);
if (allow_overwrite)
REPL_journal_cleanup(attachment2->getHandle()->att_database);
attachment2->detach(user_status);
}
else
Expand Down Expand Up @@ -3604,6 +3606,9 @@ void JAttachment::internalDropDatabase(CheckStatusWrapper* user_status)
throw;
}

// Unlink active replication segments
REPL_journal_cleanup(dbb);

// Unlink attachment from database
release_attachment(tdbb, attachment, &threadGuard);
att = NULL;
Expand Down
26 changes: 23 additions & 3 deletions src/jrd/replication/ChangeLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace

const unsigned COPY_BLOCK_SIZE = 64 * 1024; // 64 KB

const char* FILENAME_PATTERN = "%s.journal-%09" UQUADFORMAT;
const char* FILENAME_PATTERN = "%s_%s.journal-%09" UQUADFORMAT;

const char* FILENAME_WILDCARD = "$(filename)";
const char* PATHNAME_WILDCARD = "$(pathname)";
Expand Down Expand Up @@ -885,6 +885,26 @@ void ChangeLog::bgArchiver()
}
}

void ChangeLog::cleanup()
{
LockGuard guard(this);

while (m_segments.hasData())
{
const auto segment = m_segments.pop();

if (segment->getState() == SEGMENT_STATE_USED && segment->hasData())
segment->setState(SEGMENT_STATE_FULL);

if (segment->getState() == SEGMENT_STATE_FULL)
archiveSegment(segment);

const PathName filename = segment->getPathName();
segment->release();
unlink(filename.c_str());
}
}

void ChangeLog::initSegments()
{
clearSegments();
Expand Down Expand Up @@ -939,7 +959,7 @@ ChangeLog::Segment* ChangeLog::createSegment()
const auto sequence = state->sequence + 1;

PathName filename;
filename.printf(FILENAME_PATTERN, m_config->filePrefix.c_str(), sequence);
filename.printf(FILENAME_PATTERN, m_config->filePrefix.c_str(), m_guid.toString(false).c_str(), sequence);
filename = m_config->journalDirectory + filename;

const auto fd = os_utils::openCreateSharedFile(filename.c_str(), O_EXCL | O_BINARY);
Expand Down Expand Up @@ -989,7 +1009,7 @@ ChangeLog::Segment* ChangeLog::reuseSegment(ChangeLog::Segment* segment)
// Attempt to rename the backing file

PathName newname;
newname.printf(FILENAME_PATTERN, m_config->filePrefix.c_str(), sequence);
newname.printf(FILENAME_PATTERN, m_config->filePrefix.c_str(), m_guid.toString(false).c_str(), sequence);
newname = m_config->journalDirectory + newname;

// If renaming fails, then we just create a new file.
Expand Down
1 change: 1 addition & 0 deletions src/jrd/replication/ChangeLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ namespace Replication
FB_UINT64 write(ULONG length, const UCHAR* data, bool sync);

void bgArchiver();
void cleanup();

private:
void initSharedFile();
Expand Down
6 changes: 6 additions & 0 deletions src/jrd/replication/Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ namespace Replication
m_changeLog->forceSwitch();
}

void journalCleanup()
{
if (m_changeLog)
m_changeLog->cleanup();
}

const Replication::Config* getConfig() const
{
return m_config;
Expand Down
6 changes: 6 additions & 0 deletions src/jrd/replication/Publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,9 @@ void REPL_journal_switch(thread_db* tdbb)

replMgr->forceJournalSwitch();
}

void REPL_journal_cleanup(Database* dbb)
{
if (const auto replMgr = dbb->replManager(true))
replMgr->journalCleanup();
}
2 changes: 2 additions & 0 deletions src/jrd/replication/Publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Jrd
{
class thread_db;
class jrd_tra;
class Database;
class Savepoint;
struct record_param;
}
Expand All @@ -47,5 +48,6 @@ void REPL_gen_id(Jrd::thread_db* tdbb, SLONG genId, SINT64 value);
void REPL_exec_sql(Jrd::thread_db* tdbb, Jrd::jrd_tra* transaction, const Firebird::string& sql,
const Firebird::ObjectsArray<Firebird::MetaString>& schemaSearchPath);
void REPL_journal_switch(Jrd::thread_db* tdbb);
void REPL_journal_cleanup(Jrd::Database* dbb);

#endif // JRD_REPLICATION_PUBLISHER_H
Loading