Skip to content

Commit 5b85f04

Browse files
committed
Successful CREATE DATABASE with shared cache (still has some limitations)
1 parent 0164b8a commit 5b85f04

36 files changed

+346
-270
lines changed

src/dsql/ExprNodes.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5824,9 +5824,10 @@ DmlNode* FieldNode::parse(thread_db* tdbb, MemoryPool& pool, CompilerScratch* cs
58245824
PAR_error(csb, Arg::Gds(isc_ctxnotdef));
58255825

58265826
// make sure relation has been scanned before using it
5827-
5828-
if (!(relation->rel_flags & REL_scanned) || (relation->rel_flags & REL_being_scanned))
5829-
MET_scan_relation(tdbb, relation);
5827+
HazardPtr<jrd_rel> wrk = MET_scan_relation(tdbb, relation->rel_id);
5828+
if (!wrk)
5829+
PAR_error(csb, Arg::Gds(isc_ctxnotdef));
5830+
relation = wrk.getPointer();
58305831

58315832
csb->csb_blr_reader.getMetaName(name);
58325833

src/dsql/StmtNodes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ void DeclareSubFuncNode::genParameters(DsqlCompilerScratch* dsqlScratch,
17791779
DeclareSubFuncNode* DeclareSubFuncNode::pass1(thread_db* tdbb, CompilerScratch* /*csb*/)
17801780
{
17811781
ContextPoolHolder context(tdbb, &subCsb->csb_pool);
1782-
PAR_blr(tdbb, NULL, blrStart, blrLength, NULL, &subCsb, NULL, false, 0);
1782+
PAR_blr(tdbb, nullRel, blrStart, blrLength, NULL, &subCsb, NULL, false, 0);
17831783

17841784
return this;
17851785
}
@@ -2120,7 +2120,7 @@ void DeclareSubProcNode::genParameters(DsqlCompilerScratch* dsqlScratch,
21202120
DeclareSubProcNode* DeclareSubProcNode::pass1(thread_db* tdbb, CompilerScratch* /*csb*/)
21212121
{
21222122
ContextPoolHolder context(tdbb, &subCsb->csb_pool);
2123-
PAR_blr(tdbb, NULL, blrStart, blrLength, NULL, &subCsb, NULL, false, 0);
2123+
PAR_blr(tdbb, nullRel, blrStart, blrLength, NULL, &subCsb, NULL, false, 0);
21242124

21252125
return this;
21262126
}

src/jrd/Attachment.cpp

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,23 @@ void Jrd::Attachment::destroy(Attachment* const attachment)
120120
sAtt->manualUnlock(attachment->att_flags);
121121
}
122122

123-
thread_db* tdbb = JRD_get_thread_data();
124-
125-
jrd_tra* sysTransaction = attachment->getSysTransaction();
126-
if (sysTransaction)
127-
{
128-
// unwind any active system requests
129-
while (sysTransaction->tra_requests)
130-
EXE_unwind(tdbb, sysTransaction->tra_requests);
131-
132-
jrd_tra::destroy(NULL, sysTransaction);
133-
}
134-
135123
Database* const dbb = attachment->att_database;
136124
{
137125
// context scope is needed here for correct GC of hazard pointers
138126
ThreadContextHolder tdbb(dbb, attachment);
139127

128+
jrd_tra* sysTransaction = attachment->getSysTransaction();
129+
if (sysTransaction)
130+
{
131+
// unwind any active system requests
132+
while (sysTransaction->tra_requests)
133+
EXE_unwind(tdbb, sysTransaction->tra_requests);
134+
135+
jrd_tra::destroy(NULL, sysTransaction);
136+
}
137+
140138
attachment->att_delayed_delete.garbageCollect(HazardDelayedDelete::GarbageCollectMethod::GC_FORCE);
139+
HZ_DEB(fprintf(stderr, "Attachment::destroy=>delayedDelete to DBB\n"));
141140
dbb->dbb_delayed_delete.delayedDelete(attachment->att_delayed_delete.getHazardPointers());
142141
}
143142

@@ -236,6 +235,8 @@ Jrd::Attachment::Attachment(MemoryPool* pool, Database* dbb, JProvider* provider
236235
att_active_snapshots(*pool),
237236
att_statements(*pool),
238237
att_requests(*pool),
238+
att_internal(*pool),
239+
att_dyn_req(*pool),
239240
att_lock_owner_id(Database::getLockOwnerId()),
240241
att_backup_state_counter(0),
241242
att_stats(*pool),
@@ -273,7 +274,10 @@ Jrd::Attachment::Attachment(MemoryPool* pool, Database* dbb, JProvider* provider
273274
att_initial_options(*pool),
274275
att_provider(provider),
275276
att_delayed_delete(*dbb->dbb_permanent, *pool)
276-
{ }
277+
{
278+
att_internal.grow(irq_MAX);
279+
att_dyn_req.grow(drq_MAX);
280+
}
277281

278282

279283
Jrd::Attachment::~Attachment()
@@ -624,8 +628,6 @@ void Jrd::Attachment::initLocks(thread_db* tdbb)
624628

625629
void Jrd::Attachment::releaseLocks(thread_db* tdbb)
626630
{
627-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! to database att_mdc.releaseLocks(tdbb);
628-
629631
// Release the DSQL cache locks
630632

631633
DSqlCache::Accessor accessor(&att_dsql_cache);
@@ -915,3 +917,53 @@ int Attachment::blockingAstReplSet(void* ast_object)
915917

916918
return 0;
917919
}
920+
921+
void Attachment::cacheRequest(InternalRequest which, USHORT id, Statement* stmt)
922+
{
923+
if (which == IRQ_REQUESTS)
924+
att_internal[id] = stmt;
925+
else if (which == DYN_REQUESTS)
926+
att_dyn_req[id] = stmt;
927+
else
928+
{
929+
fb_assert(false);
930+
}
931+
}
932+
933+
// Find an inactive incarnation of a system request. If necessary, clone it.
934+
Jrd::Request* Attachment::findSystemRequest(thread_db* tdbb, USHORT id, InternalRequest which)
935+
{
936+
static const int MAX_RECURSION = 100;
937+
938+
// If the request hasn't been compiled or isn't active, there're nothing to do.
939+
940+
//Database::CheckoutLockGuard guard(this, dbb_cmp_clone_mutex);
941+
942+
fb_assert(which == IRQ_REQUESTS || which == DYN_REQUESTS);
943+
944+
Statement* statement = (which == IRQ_REQUESTS ? att_internal[id] : att_dyn_req[id]);
945+
946+
if (!statement)
947+
return NULL;
948+
949+
// Look for requests until we find one that is available.
950+
951+
for (int n = 0;; ++n)
952+
{
953+
if (n > MAX_RECURSION)
954+
{
955+
ERR_post(Arg::Gds(isc_no_meta_update) <<
956+
Arg::Gds(isc_req_depth_exceeded) << Arg::Num(MAX_RECURSION));
957+
// Msg363 "request depth exceeded. (Recursive definition?)"
958+
}
959+
960+
Request* clone = statement->getRequest(tdbb, n);
961+
962+
if (!(clone->req_flags & (req_active | req_reserved)))
963+
{
964+
clone->req_flags |= req_reserved;
965+
return clone;
966+
}
967+
}
968+
}
969+

src/jrd/Attachment.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ namespace Jrd
9696
class Statement;
9797
class Validation;
9898
class Applier;
99+
enum InternalRequest : USHORT;
99100

100101

101102
struct DSqlCacheItem
@@ -496,7 +497,10 @@ class Attachment : public pool_alloc<type_att>
496497

497498
public:
498499
Firebird::SortedArray<Statement*> att_statements; // Statements belonging to attachment
499-
Firebird::SortedArray<Request*> att_requests; // Requests belonging to attachment
500+
Firebird::SortedArray<Request*> att_requests; // Requests belonging to attachment
501+
Firebird::Array<Statement*> att_internal; // internal statements
502+
Firebird::Array<Statement*> att_dyn_req; // internal dyn statements
503+
500504
Lock* att_id_lock; // Attachment lock (if any)
501505
AttNumber att_attachment_id; // Attachment ID
502506
Lock* att_cancel_lock; // Lock to cancel the active request
@@ -586,6 +590,9 @@ class Attachment : public pool_alloc<type_att>
586590
jrd_tra* getSysTransaction();
587591
void setSysTransaction(jrd_tra* trans); // used only by TRA_init
588592

593+
void cacheRequest(InternalRequest which, USHORT id, Statement* stmt);
594+
Request* findSystemRequest(thread_db* tdbb, USHORT id, InternalRequest which);
595+
589596
bool isSystem() const
590597
{
591598
return (att_flags & ATT_system);

src/jrd/Collation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ void Collation::release(thread_db* tdbb)
11371137

11381138
void Collation::destroy(thread_db* tdbb)
11391139
{
1140+
fprintf(stderr, "Collation::destroy(%p) tt=%p\n", this, tt);
11401141
fb_assert(useCount == 0);
11411142

11421143
if (tt->texttype_fn_destroy)
@@ -1149,6 +1150,7 @@ void Collation::destroy(thread_db* tdbb)
11491150
delete existenceLock;
11501151
existenceLock = NULL;
11511152

1153+
fprintf(stderr, "delayedDelete collation %p\n", this);
11521154
this->delayedDelete(tdbb);
11531155
}
11541156

src/jrd/ExtEngineManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ void ExtEngineManager::makeFunction(thread_db* tdbb, CompilerScratch* csb, Jrd::
13891389
csbPool, extOutMessageNode, intOutMessageNode);
13901390

13911391
Statement* statement = udf->getStatement();
1392-
PAR_preparsed_node(tdbb, NULL, mainNode, NULL, &csb, &statement, false, 0);
1392+
PAR_preparsed_node(tdbb, nullRel, mainNode, NULL, &csb, &statement, false, 0);
13931393
udf->setStatement(statement);
13941394
}
13951395
catch (...)
@@ -1521,7 +1521,7 @@ void ExtEngineManager::makeProcedure(thread_db* tdbb, CompilerScratch* csb, Haza
15211521
mainNode->statements.add(extProcedureNode);
15221522

15231523
Statement* statement = prc->getStatement();
1524-
PAR_preparsed_node(tdbb, NULL, mainNode, NULL, &csb, &statement, false, 0);
1524+
PAR_preparsed_node(tdbb, nullRel, mainNode, NULL, &csb, &statement, false, 0);
15251525
prc->setStatement(statement);
15261526
}
15271527
catch (...)
@@ -1621,7 +1621,9 @@ void ExtEngineManager::makeTrigger(thread_db* tdbb, CompilerScratch* csb, Jrd::T
16211621
trg->extTrigger);
16221622
mainNode->statements.add(extTriggerNode);
16231623

1624-
PAR_preparsed_node(tdbb, trg->relation, mainNode, NULL, &csb, &trg->statement, true, 0);
1624+
HazardPtr<jrd_rel> rel;
1625+
rel.safePointer(trg->relation);
1626+
PAR_preparsed_node(tdbb, rel, mainNode, NULL, &csb, &trg->statement, true, 0);
16251627
}
16261628
catch (...)
16271629
{

src/jrd/Function.epp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ HazardPtr<Function> Function::loadMetadata(thread_db* tdbb, USHORT id, bool nosc
334334
try
335335
{
336336
parameter->prm_default_value = static_cast<ValueExprNode*>(MET_parse_blob(
337-
tdbb, nullptr, &default_value, nullptr, nullptr, false, false));
337+
tdbb, nullRel, &default_value, nullptr, nullptr, false, false));
338338
}
339339
catch (const Exception&)
340340
{

src/jrd/HazardPtr.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,26 @@ HazardObject::~HazardObject()
3939

4040
int HazardObject::delayedDelete(thread_db* tdbb)
4141
{
42-
HazardDelayedDelete& dd = tdbb->getAttachment()->att_delayed_delete;
43-
dd.delayedDelete(this);
42+
HazardDelayedDelete* dd = HazardBase::getHazardDelayed(tdbb);
43+
dd->delayedDelete(this);
4444
return 0;
4545
}
4646

4747
HazardDelayedDelete* HazardBase::getHazardDelayed(thread_db* tdbb)
4848
{
4949
if (!tdbb)
5050
tdbb = JRD_get_thread_data();
51-
return &tdbb->getAttachment()->att_delayed_delete;
51+
fb_assert(tdbb);
52+
53+
Attachment* att = tdbb->getAttachment();
54+
if (att)
55+
return &att->att_delayed_delete;
56+
57+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
58+
// what about locking object in database? (dbb_delayed_delete)
59+
Database* dbb = tdbb->getDatabase();
60+
fb_assert(dbb);
61+
return &dbb->dbb_delayed_delete;
5262
}
5363

5464
HazardDelayedDelete* HazardBase::getHazardDelayed(Attachment* att)
@@ -101,6 +111,8 @@ void HazardDelayedDelete::remove(const void* ptr)
101111

102112
void HazardDelayedDelete::delayedDelete(HazardObject* mem, bool gc)
103113
{
114+
HZ_DEB(fprintf(stderr, "HazardDelayedDelete::delayedDelete %p\n", mem));
115+
104116
if (mem)
105117
toDelete.push(mem);
106118

0 commit comments

Comments
 (0)