Skip to content

Commit 399e434

Browse files
authored
Add replication journal cleanup when drop database (#8626)
Also add GUID to filename pattern of journal
1 parent 9f9729c commit 399e434

File tree

7 files changed

+53
-13
lines changed

7 files changed

+53
-13
lines changed

src/common/os/guid.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static_assert(sizeof(UUID) == 16, "Guid size mismatch");
5252
namespace Firebird {
5353

5454
inline constexpr int GUID_BUFF_SIZE = 39;
55-
inline constexpr int GUID_BODY_SIZE = 36;
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() noexcept {}
@@ -119,26 +120,25 @@ class Guid
119120
}
120121

121122
template<typename T>
122-
void toString(T& str) const
123+
void toString(T& str, bool withBrackets = true) const
123124
{
124-
str.printf(GUID_FORMAT,
125+
str.printf(withBrackets ? GUID_FORMAT : GUID_FORMAT_WITHOUT_BRACKETS,
125126
m_data.Data1, m_data.Data2, m_data.Data3,
126127
m_data.Data4[0], m_data.Data4[1], m_data.Data4[2], m_data.Data4[3],
127128
m_data.Data4[4], m_data.Data4[5], m_data.Data4[6], m_data.Data4[7]);
128129
}
129130

130-
131-
Firebird::string toString() const
131+
Firebird::string toString(bool withBrackets = true) const
132132
{
133133
Firebird::string result;
134-
toString(result);
134+
toString(result, withBrackets);
135135
return result;
136136
}
137137

138-
Firebird::PathName toPathName() const
138+
Firebird::PathName toPathName(bool withBrackets = true) const
139139
{
140140
Firebird::PathName result;
141-
toString(result);
141+
toString(result, withBrackets);
142142
return result;
143143
}
144144

src/jrd/jrd.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,6 +3122,8 @@ JAttachment* JProvider::createDatabase(CheckStatusWrapper* user_status, const ch
31223122
if (attachment2)
31233123
{
31243124
allow_overwrite = attachment2->getHandle()->locksmith(tdbb, DROP_DATABASE);
3125+
if (allow_overwrite)
3126+
REPL_journal_cleanup(attachment2->getHandle()->att_database);
31253127
attachment2->detach(user_status);
31263128
}
31273129
else
@@ -3612,6 +3614,9 @@ void JAttachment::internalDropDatabase(CheckStatusWrapper* user_status)
36123614
throw;
36133615
}
36143616

3617+
// Unlink active replication segments
3618+
REPL_journal_cleanup(dbb);
3619+
36153620
// Unlink attachment from database
36163621
release_attachment(tdbb, attachment, &threadGuard);
36173622
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
inline constexpr unsigned COPY_BLOCK_SIZE = 64 * 1024; // 64 KB
7575

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

7878
inline constexpr const char* FILENAME_WILDCARD = "$(filename)";
7979
inline constexpr 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)