- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathSavepoint.cpp
699 lines (555 loc) · 17.8 KB
/
Savepoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include"../common/gdsassert.h"
#include"../jrd/tra.h"
#include"../jrd/blb_proto.h"
#include"../jrd/cch_proto.h"
#include"../jrd/dfw_proto.h"
#include"../jrd/dpm_proto.h"
#include"../jrd/idx_proto.h"
#include"../jrd/vio_proto.h"
#include"Savepoint.h"
usingnamespaceFirebird;
usingnamespaceJrd;
// UndoItem implementation
UndoItem::UndoItem(jrd_tra* transaction, RecordNumber recordNumber, const Record* record)
: m_number(recordNumber.getValue()), m_format(record->getFormat())
{
fb_assert(m_format);
m_offset = transaction->getUndoSpace()->allocateSpace(m_format->fmt_length);
transaction->getUndoSpace()->write(m_offset, record->getData(), record->getLength());
}
Record* UndoItem::setupRecord(jrd_tra* transaction) const
{
if (m_format)
{
Record* const record = transaction->getUndoRecord(m_format);
transaction->getUndoSpace()->read(m_offset, record->getData(), record->getLength());
return record;
}
returnNULL;
}
voidUndoItem::release(jrd_tra* transaction)
{
if (m_format)
{
transaction->getUndoSpace()->releaseSpace(m_offset, m_format->fmt_length);
m_format = NULL;
}
}
// VerbAction implementation
voidVerbAction::garbageCollectIdxLite(thread_db* tdbb, jrd_tra* transaction, SINT64 recordNumber,
VerbAction* nextAction, Record* goingRecord)
{
// Clean up index entries and referenced BLOBs.
// This routine uses smaller set of staying record than original VIO_garbage_collect_idx().
//
// Notes:
//
// This speed trick is possible only because btr.cpp:insert_node() allows duplicate nodes
// which work as an index entry reference counter.
record_param rpb;
rpb.rpb_relation = vct_relation;
rpb.rpb_number.setValue(recordNumber);
rpb.rpb_record = NULL;
rpb.getWindow(tdbb).win_flags = 0;
rpb.rpb_transaction_nr = transaction->tra_number;
Record* next_ver;
AutoTempRecord undo_next_ver(transaction->findNextUndo(this, vct_relation, recordNumber));
AutoPtr<Record> real_next_ver;
next_ver = undo_next_ver;
if (!DPM_get(tdbb, &rpb, LCK_read))
BUGCHECK(186); // msg 186 record disappeared
else
{
if (next_ver || (rpb.rpb_flags & rpb_deleted))
CCH_RELEASE(tdbb, &rpb.getWindow(tdbb));
else
{
VIO_data(tdbb, &rpb, transaction->tra_pool);
next_ver = real_next_ver = rpb.rpb_record;
}
}
if (rpb.rpb_transaction_nr != transaction->tra_number)
BUGCHECK(185); // msg 185 wrong record version
Record* prev_ver = NULL;
AutoTempRecord undo_prev_ver;
AutoPtr<Record> real_prev_ver;
if (nextAction && nextAction->vct_undo && nextAction->vct_undo->locate(recordNumber))
{
prev_ver = undo_prev_ver = nextAction->vct_undo->current().setupRecord(transaction);
}
elseif (rpb.rpb_b_page) // previous version exists and we have to find it in a hard way
{
record_param temp = rpb;
temp.rpb_record = NULL;
temp.rpb_page = rpb.rpb_b_page;
temp.rpb_line = rpb.rpb_b_line;
if (!DPM_fetch(tdbb, &temp, LCK_read))
BUGCHECK(291); // Back version disappeared
if (temp.rpb_flags & rpb_deleted)
CCH_RELEASE(tdbb, &temp.getWindow(tdbb));
else
VIO_data(tdbb, &temp, transaction->tra_pool);
prev_ver = real_prev_ver = temp.rpb_record;
}
RecordStack going, staying;
going.push(goingRecord);
if (prev_ver)
staying.push(prev_ver);
if (next_ver)
staying.push(next_ver);
IDX_garbage_collect(tdbb, &rpb, going, staying);
BLB_garbage_collect(tdbb, going, staying, rpb.rpb_page, vct_relation);
}
voidVerbAction::mergeTo(thread_db* tdbb, jrd_tra* transaction, VerbAction* nextAction)
{
// Post bitmap of modified records and undo data to the next savepoint.
//
// Notes:
//
// If previous savepoint already touched a record, undo data must be dropped and
// all BLOBs it refers to should be cleaned out because under no circumstances
// this undo data can become an active record.
// Merge undo records first
if (vct_undo && vct_undo->getFirst())
{
do
{
UndoItem& item = vct_undo->current();
if (item.hasData()) // this item wasn't released yet
{
const SINT64 recordNumber = item.generate(NULL, item);
if (nextAction && !(RecordBitmap::test(nextAction->vct_records, recordNumber)))
{
if (!nextAction->vct_undo)
{
nextAction->vct_undo =
FB_NEW_POOL(*transaction->tra_pool) UndoItemTree(*transaction->tra_pool);
// We cannot just push whole current undo items list, some items still may be released
}
else
{
if (nextAction->vct_undo->locate(recordNumber))
{
// It looks like something went wrong on previous loop and undo record
// was moved to next action but record bit in bitmap wasn't set
fb_assert(false);
item.clear();
continue;
}
}
nextAction->vct_undo->add(item);
item.clear(); // Do not release undo data, it now belongs to next action
continue;
}
// garbage cleanup and release
// because going version for sure has all index entries successfully set up (in contrast with undo)
// we can use lightweigth version of garbage collection without collection of full staying list
AutoTempRecord this_ver(item.setupRecord(transaction));
garbageCollectIdxLite(tdbb, transaction, recordNumber, nextAction, this_ver);
item.release(transaction);
}
} while (vct_undo->getNext());
delete vct_undo;
vct_undo = NULL;
}
// Now merge bitmap
if (nextAction)
{
if (nextAction->vct_records)
{
RecordBitmap** bm_or = RecordBitmap::bit_or(&vct_records, &nextAction->vct_records);
if (*bm_or == vct_records) // if next bitmap has been merged into this bitmap - swap them
{
RecordBitmap* temp = nextAction->vct_records;
nextAction->vct_records = vct_records;
vct_records = temp;
}
}
else// just push current bitmap as is
{
nextAction->vct_records = vct_records;
vct_records = NULL;
}
}
release(transaction);
}
voidVerbAction::undo(thread_db* tdbb, jrd_tra* transaction, bool preserveLocks, VerbAction* preserveAction)
{
// Undo changes recorded for this verb action.
// After that, clear the verb action and prepare it for later reuse.
record_param rpb;
rpb.rpb_relation = vct_relation;
rpb.rpb_number.setValue(BOF_NUMBER);
rpb.rpb_record = NULL;
rpb.getWindow(tdbb).win_flags = 0;
rpb.rpb_transaction_nr = transaction->tra_number;
RecordBitmap::Accessor accessor(vct_records);
if (accessor.getFirst())
{
do
{
rpb.rpb_number.setValue(accessor.current());
constbool have_undo = vct_undo && vct_undo->locate(rpb.rpb_number.getValue());
if (!DPM_get(tdbb, &rpb, LCK_read))
BUGCHECK(186); // msg 186 record disappeared
if ((have_undo || preserveLocks) && !(rpb.rpb_flags & rpb_deleted))
VIO_data(tdbb, &rpb, transaction->tra_pool);
else
CCH_RELEASE(tdbb, &rpb.getWindow(tdbb));
if (rpb.rpb_transaction_nr != transaction->tra_number)
BUGCHECK(185); // msg 185 wrong record version
if (!have_undo)
{
if (preserveLocks && rpb.rpb_b_page)
{
// Fetch previous record version and update in place current version with it
record_param temp = rpb;
temp.rpb_page = rpb.rpb_b_page;
temp.rpb_line = rpb.rpb_b_line;
temp.rpb_record = NULL;
if (temp.rpb_flags & rpb_delta)
fb_assert(temp.rpb_prior != NULL);
else
fb_assert(temp.rpb_prior == NULL);
if (!DPM_fetch(tdbb, &temp, LCK_read))
BUGCHECK(291); // msg 291 cannot find record back version
if (!(temp.rpb_flags & rpb_chained) || (temp.rpb_flags & (rpb_blob | rpb_fragment)))
ERR_bugcheck_msg("invalid back version");
VIO_data(tdbb, &temp, tdbb->getDefaultPool());
Record* const save_record = rpb.rpb_record;
if (rpb.rpb_flags & rpb_deleted)
rpb.rpb_record = NULL;
Record* const dead_record = rpb.rpb_record;
VIO_update_in_place(tdbb, transaction, &rpb, &temp);
if (dead_record)
{
rpb.rpb_record = NULL; // VIO_garbage_collect_idx will play with this record dirty tricks
VIO_garbage_collect_idx(tdbb, transaction, &rpb, dead_record);
}
rpb.rpb_record = save_record;
delete temp.rpb_record;
if (preserveAction)
RBM_SET(transaction->tra_pool, &preserveAction->vct_records, rpb.rpb_number.getValue());
}
else
VIO_backout(tdbb, &rpb, transaction);
}
else
{
AutoTempRecord record(vct_undo->current().setupRecord(transaction));
Record* const save_record = rpb.rpb_record;
record_param new_rpb = rpb;
if (rpb.rpb_flags & rpb_deleted)
rpb.rpb_record = NULL;
new_rpb.rpb_record = record;
new_rpb.rpb_address = record->getData();
new_rpb.rpb_length = record->getLength();
new_rpb.rpb_flags = 0;
Record* const dead_record = rpb.rpb_record;
// This record will be in staying list twice. Ignorable overhead.
VIO_update_in_place(tdbb, transaction, &rpb, &new_rpb);
if (dead_record)
{
rpb.rpb_record = NULL; // VIO_garbage_collect_idx will play with this record dirty tricks
VIO_garbage_collect_idx(tdbb, transaction, &rpb, dead_record);
}
rpb.rpb_record = save_record;
}
} while (accessor.getNext());
delete rpb.rpb_record;
}
release(transaction);
}
voidVerbAction::release(jrd_tra* transaction)
{
// Release resources used by this verb action
RecordBitmap::reset(vct_records);
if (vct_undo)
{
if (vct_undo->getFirst())
{
do {
vct_undo->current().release(transaction);
} while (vct_undo->getNext());
}
delete vct_undo;
vct_undo = NULL;
}
}
// Savepoint implementation
VerbAction* Savepoint::createAction(jrd_rel* relation)
{
// Create action for the given relation. If it already exists, just return.
VerbAction* action = getAction(relation);
if (!action)
{
if ( (action = m_freeActions) )
m_freeActions = action->vct_next;
else
action = FB_NEW_POOL(*m_transaction->tra_pool) VerbAction();
action->vct_next = m_actions;
m_actions = action;
action->vct_relation = relation;
}
return action;
}
voidSavepoint::cleanupTempData()
{
// Find all global temporary tables with DELETE ROWS action
// and release their undo data
for (VerbAction* action = m_actions; action; action = action->vct_next)
{
if (action->vct_relation->rel_flags & REL_temp_tran)
{
RecordBitmap::reset(action->vct_records);
if (action->vct_undo)
{
if (action->vct_undo->getFirst())
{
do
{
action->vct_undo->current().release(m_transaction);
} while (action->vct_undo->getNext());
}
delete action->vct_undo;
action->vct_undo = NULL;
}
}
}
}
Savepoint* Savepoint::rollback(thread_db* tdbb, Savepoint* prior, bool preserveLocks)
{
// Undo changes made in this savepoint.
// Perform index and BLOB cleanup if needed.
// At the exit savepoint is clear and safe to reuse.
jrd_tra* const old_tran = tdbb->getTransaction();
try
{
DFW_delete_deferred(m_transaction, m_number);
m_flags &= ~SAV_force_dfw;
AutoSetRestoreFlag<ULONG> verbCleanupFlag(&tdbb->tdbb_flags, TDBB_verb_cleanup, true);
tdbb->setTransaction(m_transaction);
while (m_actions)
{
VerbAction* const action = m_actions;
VerbAction* preserveAction = nullptr;
if (preserveLocks && m_next)
preserveAction = m_next->createAction(action->vct_relation);
action->undo(tdbb, m_transaction, preserveLocks, preserveAction);
m_actions = action->vct_next;
action->vct_next = m_freeActions;
m_freeActions = action;
}
tdbb->setTransaction(old_tran);
}
catch (const Exception& ex)
{
Arg::StatusVector error(ex);
tdbb->setTransaction(old_tran);
m_transaction->tra_flags |= TRA_invalidated;
error.prepend(Arg::Gds(isc_savepoint_backout_err));
error.raise();
}
returnrelease(prior);
}
Savepoint* Savepoint::rollforward(thread_db* tdbb, Savepoint* prior)
{
// Merge changes made in this savepoint into next one.
// Perform index and BLOB cleanup if needed.
// At the exit savepoint is clear and safe to reuse.
jrd_tra* const old_tran = tdbb->getTransaction();
try
{
// If the current to-be-cleaned-up savepoint is very big, and the next
// level savepoint is the transaction level savepoint, then get rid of
// the transaction level savepoint now (instead of after making the
// transaction level savepoint very very big).
if (m_next && m_next->isRoot() && this->isLarge())
{
fb_assert(!m_next->m_next); // check that transaction savepoint is the last in list
// get rid of tx-level savepoint
m_next->rollforward(tdbb);
m_next = NULL;
}
// Cleanup/merge deferred work/event post
if (m_actions || (m_flags & SAV_force_dfw))
{
DFW_merge_work(m_transaction, m_number, m_next ? m_next->m_number : 0);
if (m_next && (m_flags & SAV_force_dfw))
m_next->m_flags |= SAV_force_dfw;
m_flags &= ~SAV_force_dfw;
}
tdbb->tdbb_flags |= TDBB_verb_cleanup;
tdbb->setTransaction(m_transaction);
while (m_actions)
{
VerbAction* const action = m_actions;
VerbAction* nextAction = NULL;
if (m_next)
{
nextAction = m_next->getAction(action->vct_relation);
if (!nextAction) // next savepoint didn't touch this table yet - send whole action
{
m_actions = action->vct_next;
action->vct_next = m_next->m_actions;
m_next->m_actions = action;
continue;
}
}
// No luck, merge action in a slow way
action->mergeTo(tdbb, m_transaction, nextAction);
// and release it afterwards
m_actions = action->vct_next;
action->vct_next = m_freeActions;
m_freeActions = action;
}
tdbb->setTransaction(old_tran);
tdbb->tdbb_flags &= ~TDBB_verb_cleanup;
}
catch (...)
{
m_transaction->tra_flags |= TRA_invalidated;
tdbb->setTransaction(old_tran);
tdbb->tdbb_flags &= ~TDBB_verb_cleanup;
throw;
}
// If the only remaining savepoint is the 'transaction-level' savepoint
// that was started by TRA_start, then check if it hasn't grown out of
// bounds yet. If it has, then give up on this transaction-level savepoint.
if (m_next && m_next->isRoot() && m_next->isLarge())
{
fb_assert(!m_next->m_next); // check that transaction savepoint is the last in list
// get rid of tx-level savepoint
m_next->rollforward(tdbb);
m_next = NULL;
}
returnrelease(prior);
}
boolSavepoint::isLarge() const
{
// Returns whether the current savepoint is large enough (has many verbs posted).
//
// Notes:
//
// - This routine does not take into account the data allocated to 'vct_undo'.
// Why? Because this routine is used to estimate size of transaction-level
// savepoint and transaction-level savepoint may not contain undo data as it is
// always the first savepoint in transaction.
//
// - We use U_IPTR, not ULONG to care of case when user savepoint gets very,
// very big on 64-bit machine. Its size may overflow 32 significant bits of
// ULONG in this case
U_IPTR size = 0;
// Iterate all tables changed under this savepoint
for (VerbAction* action = m_actions; action; action = action->vct_next)
{
// Estimate size used for record backout bitmaps for this table
if (action->vct_records)
{
size += action->vct_records->approxSize();
if (size > SIZE_THRESHOLD)
returntrue;
}
}
returnfalse;
}
Savepoint* Savepoint::release(Savepoint* prior)
{
// Clear savepoint and prepare it for later reuse.
// If prior savepoint is specified, relink its next pointer.
// Return the next savepoint, if exists.
m_flags = 0;
m_count = 0;
m_name = "";
Savepoint* const next = m_next;
if (prior)
{
fb_assert(prior != next);
prior->m_next = next;
}
m_next = m_transaction->tra_save_free;
m_transaction->tra_save_free = this;
fb_assert(m_next != this);
return next;
}
// AutoSavePoint implementation
AutoSavePoint::AutoSavePoint(thread_db* tdbb, jrd_tra* trans, bool cond)
: m_tdbb(tdbb), m_transaction(trans), m_number(0)
{
if (!cond)
return;
constauto savepoint = trans->startSavepoint();
m_number = savepoint->getNumber();
}
AutoSavePoint::~AutoSavePoint()
{
if (m_number && !(m_tdbb->getDatabase()->dbb_flags & DBB_bugcheck))
{
fb_assert(m_transaction->tra_save_point);
fb_assert(m_transaction->tra_save_point->getNumber() == m_number);
m_transaction->rollbackSavepoint(m_tdbb);
}
}
voidAutoSavePoint::release()
{
if (!m_number)
return;
fb_assert(m_transaction->tra_save_point);
fb_assert(m_transaction->tra_save_point->getNumber() == m_number);
fb_assert(!m_transaction->tra_save_point->isChanging());
m_transaction->releaseSavepoint(m_tdbb);
m_number = 0;
}
voidAutoSavePoint::rollback(bool preserveLocks)
{
if (!m_number)
return;
fb_assert(m_transaction->tra_save_point);
fb_assert(m_transaction->tra_save_point->getNumber() == m_number);
m_transaction->rollbackSavepoint(m_tdbb, preserveLocks);
m_number = 0;
}
// StableCursorSavePoint implementation
StableCursorSavePoint::StableCursorSavePoint(thread_db* tdbb, jrd_tra* trans, bool start)
: m_tdbb(tdbb), m_transaction(trans), m_number(0)
{
if (!start)
return;
if (trans->tra_flags & TRA_system)
return;
if (!trans->tra_save_point)
return;
constauto savepoint = trans->startSavepoint();
m_number = savepoint->getNumber();
}
voidStableCursorSavePoint::release()
{
if (!m_number)
return;
while (m_transaction->tra_save_point &&
m_transaction->tra_save_point->getNumber() >= m_number)
{
fb_assert(!m_transaction->tra_save_point->isChanging());
m_transaction->releaseSavepoint(m_tdbb);
}
m_number = 0;
}