Skip to content

Commit 16b0169

Browse files
committed
Add replication journal cleanup when drop database
Also add GUID to filename pattern of journal
1 parent 10b585b commit 16b0169

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

src/common/os/guid.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ static_assert(sizeof(UUID) == 16, "Guid size mismatch");
5151

5252
namespace Firebird {
5353

54-
const int GUID_BUFF_SIZE = 39;
55-
const int GUID_BODY_SIZE = 36;
54+
constexpr int GUID_BUFF_SIZE = 39;
5655

5756
void GenerateRandomBytes(void* buffer, FB_SIZE_T size);
5857

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

7273
Guid()
@@ -121,25 +122,25 @@ class Guid
121122
memcpy(&m_data, buffer, SIZE);
122123
}
123124

124-
void toString(char* buffer) const
125+
void toString(char* buffer, bool withBrackets = true) const
125126
{
126-
sprintf(buffer, GUID_FORMAT,
127+
sprintf(buffer, withBrackets ? GUID_FORMAT : GUID_FORMAT_WITHOUT_BRACKETS,
127128
m_data.Data1, m_data.Data2, m_data.Data3,
128129
m_data.Data4[0], m_data.Data4[1], m_data.Data4[2], m_data.Data4[3],
129130
m_data.Data4[4], m_data.Data4[5], m_data.Data4[6], m_data.Data4[7]);
130131
}
131132

132-
Firebird::string toString() const
133+
Firebird::string toString(bool withBrackets = true) const
133134
{
134135
Firebird::string result;
135-
toString(result.getBuffer(GUID_BUFF_SIZE - 1));
136+
toString(result.getBuffer(GUID_BUFF_SIZE - (withBrackets ? 1 : 3)), withBrackets);
136137
return result;
137138
}
138139

139-
Firebird::PathName toPathName() const
140+
Firebird::PathName toPathName(bool withBrackets = true) const
140141
{
141142
Firebird::PathName result;
142-
toString(result.getBuffer(GUID_BUFF_SIZE - 1));
143+
toString(result.getBuffer(GUID_BUFF_SIZE - (withBrackets ? 1 : 3)), withBrackets);
143144
return result;
144145
}
145146

src/jrd/jrd.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,8 @@ JAttachment* JProvider::createDatabase(CheckStatusWrapper* user_status, const ch
31163116
if (attachment2)
31173117
{
31183118
allow_overwrite = attachment2->getHandle()->locksmith(tdbb, DROP_DATABASE);
3119+
if (allow_overwrite)
3120+
REPL_journal_cleanup(attachment2->getHandle()->att_database);
31193121
attachment2->detach(user_status);
31203122
}
31213123
else
@@ -3604,6 +3606,9 @@ void JAttachment::internalDropDatabase(CheckStatusWrapper* user_status)
36043606
throw;
36053607
}
36063608

3609+
// Unlink active replication segments
3610+
REPL_journal_cleanup(dbb);
3611+
36073612
// Unlink attachment from database
36083613
release_attachment(tdbb, attachment, &threadGuard);
36093614
att = NULL;

src/jrd/replication/ChangeLog.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace
7373

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

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

7878
const char* FILENAME_WILDCARD = "$(filename)";
7979
const char* PATHNAME_WILDCARD = "$(pathname)";
@@ -885,6 +885,26 @@ void ChangeLog::bgArchiver()
885885
}
886886
}
887887

888+
void ChangeLog::cleanup()
889+
{
890+
LockGuard guard(this);
891+
892+
while (m_segments.hasData())
893+
{
894+
const auto segment = m_segments.pop();
895+
896+
if (segment->getState() == SEGMENT_STATE_USED && segment->hasData())
897+
segment->setState(SEGMENT_STATE_FULL);
898+
899+
if (segment->getState() == SEGMENT_STATE_FULL)
900+
archiveSegment(segment);
901+
902+
const PathName filename = segment->getPathName();
903+
segment->release();
904+
unlink(filename.c_str());
905+
}
906+
}
907+
888908
void ChangeLog::initSegments()
889909
{
890910
clearSegments();
@@ -939,7 +959,7 @@ ChangeLog::Segment* ChangeLog::createSegment()
939959
const auto sequence = state->sequence + 1;
940960

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

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

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

9951015
// If renaming fails, then we just create a new file.

src/jrd/replication/ChangeLog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ namespace Replication
213213
FB_UINT64 write(ULONG length, const UCHAR* data, bool sync);
214214

215215
void bgArchiver();
216+
void cleanup();
216217

217218
private:
218219
void initSharedFile();

src/jrd/replication/Manager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ namespace Replication
8888
m_changeLog->forceSwitch();
8989
}
9090

91+
void journalCleanup()
92+
{
93+
if (m_changeLog)
94+
m_changeLog->cleanup();
95+
}
96+
9197
const Replication::Config* getConfig() const
9298
{
9399
return m_config;

src/jrd/replication/Publisher.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,9 @@ void REPL_journal_switch(thread_db* tdbb)
707707

708708
replMgr->forceJournalSwitch();
709709
}
710+
711+
void REPL_journal_cleanup(Database* dbb)
712+
{
713+
if (const auto replMgr = dbb->replManager(true))
714+
replMgr->journalCleanup();
715+
}

src/jrd/replication/Publisher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Jrd
2727
{
2828
class thread_db;
2929
class jrd_tra;
30+
class Database;
3031
class Savepoint;
3132
struct record_param;
3233
}
@@ -47,5 +48,6 @@ void REPL_gen_id(Jrd::thread_db* tdbb, SLONG genId, SINT64 value);
4748
void REPL_exec_sql(Jrd::thread_db* tdbb, Jrd::jrd_tra* transaction, const Firebird::string& sql,
4849
const Firebird::ObjectsArray<Firebird::MetaString>& schemaSearchPath);
4950
void REPL_journal_switch(Jrd::thread_db* tdbb);
51+
void REPL_journal_cleanup(Jrd::Database* dbb);
5052

5153
#endif // JRD_REPLICATION_PUBLISHER_H

0 commit comments

Comments
 (0)