Skip to content

Commit 2be4aa7

Browse files
TreeHunter9Artyom Ivanov
authored andcommitted
Do not allow run concurrent sweep instances (#8320)
* Do not allow run concurrent sweep instances * Silently ignore error about concurrent sweep instance when we try to run auto sweep * Add more error messages that describe a reason of failed sweep startup * Pass ISC_STATUS instead of Arg::Gds to function --------- Co-authored-by: Artyom Ivanov <[email protected]>
1 parent c1a518f commit 2be4aa7

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

src/include/firebird/impl/msg/jrd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,8 @@ FB_IMPL_MSG(JRD, 965, ods_upgrade_err, -901, "HY", "000", "ODS upgrade failed wh
968968
FB_IMPL_MSG(JRD, 966, bad_par_workers, -924, "HY", "000", "Wrong parallel workers value @1, valid range are from 1 to @2")
969969
FB_IMPL_MSG(JRD, 967, idx_expr_not_found, -902, "42", "000", "Definition of index expression is not found for index @1")
970970
FB_IMPL_MSG(JRD, 968, idx_cond_not_found, -902, "42", "000", "Definition of index condition is not found for index @1")
971+
// Codes 969..987 are used in v6
972+
FB_IMPL_MSG(JRD, 988, sweep_unable_to_run, -901, "42", "000", "Unable to run sweep")
973+
FB_IMPL_MSG(JRD, 989, sweep_concurrent_instance, -901, "42", "000", "Another instance of sweep is already running")
974+
FB_IMPL_MSG(JRD, 990, sweep_read_only, -901, "42", "000", "Database in read only state")
975+
FB_IMPL_MSG(JRD, 991, sweep_attach_no_cleanup, -901, "42", "000", "Attachment has no cleanup flag set")

src/include/gen/Firebird.pas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,6 +5755,10 @@ IProfilerStatsImpl = class(IProfilerStats)
57555755
isc_bad_par_workers = 335545286;
57565756
isc_idx_expr_not_found = 335545287;
57575757
isc_idx_cond_not_found = 335545288;
5758+
isc_sweep_unable_to_run = 335545308;
5759+
isc_sweep_concurrent_instance = 335545309;
5760+
isc_sweep_read_only = 335545310;
5761+
isc_sweep_attach_no_cleanup = 335545311;
57585762
isc_gfix_db_name = 335740929;
57595763
isc_gfix_invalid_sw = 335740930;
57605764
isc_gfix_incmp_sw = 335740932;

src/jrd/Database.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747

4848
using namespace Firebird;
4949

50+
namespace
51+
{
52+
void unableToRunSweepException(ISC_STATUS reason)
53+
{
54+
ERR_post(Arg::Gds(isc_sweep_unable_to_run) << Arg::Gds(reason));
55+
}
56+
}
57+
5058
namespace Jrd
5159
{
5260
bool Database::onRawDevice() const
@@ -296,35 +304,35 @@ namespace Jrd
296304
}
297305
}
298306

299-
bool Database::allowSweepRun(thread_db* tdbb)
307+
void Database::initiateSweepRun(thread_db* tdbb)
300308
{
301-
SPTHR_DEBUG(fprintf(stderr, "allowSweepRun %p\n", this));
309+
SPTHR_DEBUG(fprintf(stderr, FB_FUNCTION " %p\n", this));
302310

303311
if (readOnly())
304-
return false;
312+
unableToRunSweepException(isc_sweep_read_only);
305313

306314
Jrd::Attachment* const attachment = tdbb->getAttachment();
307315
if (attachment->att_flags & ATT_no_cleanup)
308-
return false;
316+
unableToRunSweepException(isc_sweep_attach_no_cleanup);
309317

310318
while (true)
311319
{
312320
AtomicCounter::counter_type old = dbb_flags;
313321
if (old & DBB_sweep_in_progress)
314322
{
315323
clearSweepStarting();
316-
return false;
324+
unableToRunSweepException(isc_sweep_concurrent_instance);
317325
}
318326

319327
if (dbb_flags.compareExchange(old, old | DBB_sweep_in_progress))
320328
break;
321329
}
322330

323-
SPTHR_DEBUG(fprintf(stderr, "allowSweepRun - set DBB_sweep_in_progress\n"));
331+
SPTHR_DEBUG(fprintf(stderr, FB_FUNCTION " - set DBB_sweep_in_progress\n"));
324332

325333
if (!(dbb_flags & DBB_sweep_starting))
326334
{
327-
SPTHR_DEBUG(fprintf(stderr, "allowSweepRun - createSweepLock\n"));
335+
SPTHR_DEBUG(fprintf(stderr, FB_FUNCTION " - createSweepLock\n"));
328336

329337
createSweepLock(tdbb);
330338
if (!LCK_lock(tdbb, dbb_sweep_lock, LCK_EX, -1))
@@ -333,17 +341,15 @@ namespace Jrd
333341
fb_utils::init_status(tdbb->tdbb_status_vector);
334342

335343
dbb_flags &= ~DBB_sweep_in_progress;
336-
return false;
344+
unableToRunSweepException(isc_sweep_concurrent_instance);
337345
}
338346
}
339347
else
340348
{
341-
SPTHR_DEBUG(fprintf(stderr, "allowSweepRun - clearSweepStarting\n"));
349+
SPTHR_DEBUG(fprintf(stderr, FB_FUNCTION " - clearSweepStarting\n"));
342350
attachment->att_flags |= ATT_from_thread;
343351
clearSweepStarting();
344352
}
345-
346-
return true;
347353
}
348354

349355
void Database::clearSweepFlags(thread_db* tdbb)

src/jrd/Database.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,8 @@ class Database : public pool_alloc<type_dbb>
658658

659659
// returns true if sweeper thread could start
660660
bool allowSweepThread(thread_db* tdbb);
661-
// returns true if sweep could run
662-
bool allowSweepRun(thread_db* tdbb);
661+
// Throw an exception if sweep cannot be run
662+
void initiateSweepRun(thread_db* tdbb);
663663
// reset sweep flag and release sweep lock
664664
void clearSweepFlags(thread_db* tdbb);
665665
// reset sweep starting flag, release thread starting mutex

src/jrd/tra.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,11 +1846,7 @@ void TRA_sweep(thread_db* tdbb)
18461846
Database* const dbb = tdbb->getDatabase();
18471847
CHECK_DBB(dbb);
18481848

1849-
if (!dbb->allowSweepRun(tdbb))
1850-
{
1851-
dbb->clearSweepFlags(tdbb);
1852-
return;
1853-
}
1849+
dbb->initiateSweepRun(tdbb);
18541850

18551851
fb_assert(dbb->dbb_flags & DBB_sweep_in_progress);
18561852

@@ -2756,6 +2752,9 @@ namespace {
27562752
status.check();
27572753

27582754
AutoRelease<IAttachment> att(prov->attachDatabase(&status, dbName.c_str(), dpbLen, dpbBytes));
2755+
if (fb_utils::containsErrorCode(status->getErrors(), isc_sweep_unable_to_run))
2756+
return;
2757+
27592758
status.check();
27602759
}
27612760

0 commit comments

Comments
 (0)