-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
cch.cpp
5437 lines (4651 loc) · 142 KB
/
cch.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROGRAM: JRD Access Method
* MODULE: cch.cpp
* DESCRIPTION: Disk cache manager
*
* 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): ______________________________________.
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
*/
#include "firebird.h"
#include "../common/common.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../jrd/jrd.h"
#include "../jrd/que.h"
#include "../jrd/lck.h"
#include "../jrd/ods.h"
#include "../jrd/os/pio.h"
#include "../jrd/cch.h"
#include "gen/iberror.h"
#include "../jrd/lls.h"
#include "../jrd/req.h"
#include "../jrd/sdw.h"
#include "../jrd/tra.h"
#include "../jrd/sbm.h"
#include "../jrd/nbak.h"
#include "../common/gdsassert.h"
#include "../jrd/cch_proto.h"
#include "../jrd/err_proto.h"
#include "../yvalve/gds_proto.h"
#include "../common/isc_proto.h"
#include "../common/isc_s_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../jrd/sdw_proto.h"
#include "../jrd/shut_proto.h"
#include "../common/ThreadStart.h"
#include "../jrd/thread_proto.h"
#include "../jrd/tra_proto.h"
#include "../common/config/config.h"
#include "../common/classes/MsgPrint.h"
using namespace Jrd;
using namespace Ods;
using namespace Firebird;
// In the superserver mode, no page locks are acquired through the lock manager.
// Instead, a latching mechanism is used. So the calls to lock subsystem for
// database pages in the original code should not be made, lest they should cause
// any undesirable side-effects. The following defines help us achieve that.
#ifdef CCH_DEBUG
#include <stdarg.h>
IMPLEMENT_TRACE_ROUTINE(cch_trace, "CCH")
#endif
#ifdef SUPERSERVER
#define CACHE_WRITER
#endif
#ifdef SUPERSERVER_V2
#define CACHE_READER
#endif
#ifdef SUPERSERVER
#define PAGE_LOCK_RELEASE(lock)
#define PAGE_LOCK_ASSERT(lock)
#define PAGE_LOCK_RE_POST(lock)
#define PAGE_OVERHEAD (sizeof (bcb_repeat) + sizeof(BufferDesc) + (int) dbb->dbb_page_size)
#else
#define PAGE_LOCK_RELEASE(lock) LCK_release (tdbb, lock)
#define PAGE_LOCK_ASSERT(lock) LCK_assert (tdbb, lock)
#define PAGE_LOCK_RE_POST(lock) LCK_re_post (tdbb, lock)
#define PAGE_OVERHEAD (sizeof (bcb_repeat) + sizeof(BufferDesc) + \
sizeof (Lock) + (int) dbb->dbb_page_size)
#endif
static BufferDesc* alloc_bdb(thread_db*, BufferControl*, UCHAR **);
#ifndef SUPERSERVER
static Lock* alloc_page_lock(Jrd::thread_db*, BufferDesc*);
static int blocking_ast_bdb(void*);
#endif
#ifdef CACHE_READER
static THREAD_ENTRY_DECLARE cache_reader(THREAD_ENTRY_PARAM);
#endif
#ifdef CACHE_WRITER
static THREAD_ENTRY_DECLARE cache_writer(THREAD_ENTRY_PARAM);
#endif
static void check_precedence(thread_db*, WIN*, PageNumber);
static void clear_precedence(thread_db*, BufferDesc*);
static BufferDesc* dealloc_bdb(BufferDesc*);
#ifndef SUPERSERVER
static void down_grade(thread_db*, BufferDesc*);
#endif
static void expand_buffers(thread_db*, ULONG);
static BufferDesc* get_buffer(thread_db*, const PageNumber, LATCH, SSHORT);
static int get_related(BufferDesc*, PagesArray&, int, const ULONG);
static ULONG get_prec_walk_mark(BufferControl*);
static void invalidate_and_release_buffer(thread_db*, BufferDesc*);
static SSHORT latch_bdb(thread_db*, LATCH, BufferDesc*, const PageNumber, SSHORT);
static SSHORT lock_buffer(thread_db*, BufferDesc*, const SSHORT, const SCHAR);
static ULONG memory_init(thread_db*, BufferControl*, SLONG);
static void page_validation_error(thread_db*, win*, SSHORT);
#ifdef CACHE_READER
static void prefetch_epilogue(Prefetch*, ISC_STATUS *);
static void prefetch_init(Prefetch*, thread_db*);
static void prefetch_io(Prefetch*, ISC_STATUS *);
static void prefetch_prologue(Prefetch*, SLONG *);
#endif
static SSHORT related(BufferDesc*, const BufferDesc*, SSHORT, const ULONG);
static void release_bdb(thread_db*, BufferDesc*, const bool, const bool, const bool);
static void unmark(thread_db*, WIN*);
static bool writeable(BufferDesc*);
static bool is_writeable(BufferDesc*, const ULONG);
static int write_buffer(thread_db*, BufferDesc*, const PageNumber, const bool, ISC_STATUS* const,
const bool);
static bool write_page(thread_db*, BufferDesc*, ISC_STATUS* const, const bool);
static void set_diff_page(thread_db*, BufferDesc*);
static void set_dirty_flag(thread_db*, BufferDesc*);
static void clear_dirty_flag(thread_db*, BufferDesc*);
static inline void insertDirty(BufferControl* bcb, BufferDesc* bdb)
{
if (bdb->bdb_dirty.que_forward == &bdb->bdb_dirty)
{
bcb->bcb_dirty_count++;
QUE_INSERT(bcb->bcb_dirty, bdb->bdb_dirty);
}
}
static inline void removeDirty(BufferControl* bcb, BufferDesc* bdb)
{
if (bdb->bdb_dirty.que_forward != &bdb->bdb_dirty)
{
fb_assert(bcb->bcb_dirty_count > 0);
bcb->bcb_dirty_count--;
QUE_DELETE(bdb->bdb_dirty);
QUE_INIT(bdb->bdb_dirty);
}
}
static void flushDirty(thread_db* tdbb, SLONG transaction_mask, const bool sys_only, ISC_STATUS* status);
static void flushAll(thread_db* tdbb, USHORT flush_flag);
const ULONG MIN_BUFFER_SEGMENT = 65536;
// Given pointer a field in the block, find the block
#define BLOCK(fld_ptr, type, fld) (type)((SCHAR*) fld_ptr - OFFSET (type, fld))
static inline SharedLatch* allocSharedLatch(thread_db* tdbb, BufferDesc* bdb)
{
BufferControl* const bcb = bdb->bdb_dbb->dbb_bcb;
SharedLatch* latch;
if (QUE_NOT_EMPTY(bcb->bcb_free_slt))
{
QUE que_inst = bcb->bcb_free_slt.que_forward;
QUE_DELETE(*que_inst);
latch = BLOCK(que_inst, SharedLatch*, slt_bdb_que);
}
else
{
const int BATCH_ALLOC = 64;
Database* dbb = bdb->bdb_dbb;
SharedLatch* latches = latch = FB_NEW(*dbb->dbb_bufferpool) SharedLatch[BATCH_ALLOC];
for (int i = 1; i < BATCH_ALLOC; i++) {
QUE_APPEND(bcb->bcb_free_slt, latches[i].slt_bdb_que);
}
}
latch->slt_bdb = bdb;
QUE_APPEND(bdb->bdb_shared, latch->slt_bdb_que);
latch->slt_tdbb = tdbb;
QUE_APPEND(tdbb->tdbb_latches, latch->slt_tdbb_que);
return latch;
}
static inline void freeSharedLatch(thread_db* /*tdbb*/, BufferControl* bcb, SharedLatch* latch)
{
latch->slt_bdb = NULL;
QUE_DELETE(latch->slt_bdb_que);
QUE_INSERT(bcb->bcb_free_slt, latch->slt_bdb_que);
latch->slt_tdbb = NULL;
QUE_DELETE(latch->slt_tdbb_que);
}
static inline SharedLatch* findSharedLatch(thread_db* tdbb, BufferDesc* bdb)
{
for (QUE que_inst = tdbb->tdbb_latches.que_forward; que_inst != &tdbb->tdbb_latches;
que_inst = que_inst->que_forward)
{
SharedLatch* latch = BLOCK(que_inst, SharedLatch*, slt_tdbb_que);
fb_assert(latch->slt_tdbb == tdbb);
if (latch->slt_bdb == bdb) {
return latch;
}
}
return NULL;
}
//
//#define BCB_MUTEX_ACQUIRE
//#define BCB_MUTEX_RELEASE
//
//#define PRE_MUTEX_ACQUIRE
//#define PRE_MUTEX_RELEASE
//
//#define BTC_MUTEX_ACQUIRE
//#define BTC_MUTEX_RELEASE
//
//#define LATCH_MUTEX_ACQUIRE
//#define LATCH_MUTEX_RELEASE
//
#pragma FB_COMPILER_MESSAGE("These special values must die!")
const PageNumber JOURNAL_PAGE(DB_PAGE_SPACE, -1);
const PageNumber FREE_PAGE(DB_PAGE_SPACE, -2);
const int PRE_SEARCH_LIMIT = 256;
const int PRE_EXISTS = -1;
const int PRE_UNKNOWN = -2;
int CCH_down_grade_dbb(void* ast_object)
{
/**************************************
*
* C C H _ d o w n _ g r a d e _ d b b
*
**************************************
*
* Functional description
* Down grade the lock on the database in response to a blocking
* AST.
*
**************************************/
Database* dbb = static_cast<Database*>(ast_object);
try
{
Database::SyncGuard dsGuard(dbb, true);
if (dbb->dbb_flags & DBB_not_in_use)
return 0;
Lock* const lock = dbb->dbb_lock;
// Since this routine will be called asynchronously,
// we must establish a thread context
ThreadContextHolder tdbb;
tdbb->setDatabase(dbb);
tdbb->setAttachment(lock->lck_attachment);
dbb->dbb_ast_flags |= DBB_blocking;
// Database shutdown will release the database lock; just return
if (SHUT_blocking_ast(tdbb))
{
dbb->dbb_ast_flags &= ~DBB_blocking;
return 0;
}
// If we are already shared, there is nothing more we can do.
// If any case, the other guy probably wants exclusive access,
// and we can't give it anyway
if ((lock->lck_logical == LCK_SW) || (lock->lck_logical == LCK_SR)) {
return 0;
}
if (dbb->dbb_flags & DBB_bugcheck)
{
LCK_convert(tdbb, lock, LCK_SW, LCK_WAIT);
dbb->dbb_ast_flags &= ~DBB_blocking;
return 0;
}
// If we are supposed to be exclusive, stay exclusive
if ((dbb->dbb_flags & DBB_exclusive) || (dbb->dbb_ast_flags & DBB_shutdown_single)) {
return 0;
}
// Assert any page locks that have been requested, but not asserted
dbb->dbb_ast_flags |= DBB_assert_locks;
BufferControl* bcb = dbb->dbb_bcb;
if (bcb && bcb->bcb_count)
{
const bcb_repeat* tail = bcb->bcb_rpt;
for (const bcb_repeat* const end = tail + bcb->bcb_count; tail < end; ++tail)
{
PAGE_LOCK_ASSERT(tail->bcb_bdb->bdb_lock);
}
}
// Down grade the lock on the database itself
if (lock->lck_physical == LCK_EX) {
LCK_convert(tdbb, lock, LCK_PW, LCK_WAIT); // This lets waiting cache manager in first
}
else {
LCK_convert(tdbb, lock, LCK_SW, LCK_WAIT);
}
dbb->dbb_ast_flags &= ~DBB_blocking;
}
catch (const Firebird::Exception&)
{} // no-op
return 0;
}
bool CCH_exclusive(thread_db* tdbb, USHORT level, SSHORT wait_flag)
{
/**************************************
*
* C C H _ e x c l u s i v e
*
**************************************
*
* Functional description
* Get exclusive access to a database. If we get it, return true.
* If the wait flag is FALSE, and we can't get it, give up and
* return false. There are two levels of database exclusivity: LCK_PW
* guarantees there are no normal users in the database while LCK_EX
* additionally guarantes background database processes like the
* shared cache manager have detached.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
#ifdef SUPERSERVER
if (!CCH_exclusive_attachment(tdbb, level, wait_flag)) {
return false;
}
#endif
Lock* lock = dbb->dbb_lock;
if (!lock) {
return false;
}
dbb->dbb_flags |= DBB_exclusive;
switch (level)
{
case LCK_PW:
if (lock->lck_physical >= LCK_PW || LCK_convert(tdbb, lock, LCK_PW, wait_flag))
{
return true;
}
break;
case LCK_EX:
if (lock->lck_physical == LCK_EX || LCK_convert(tdbb, lock, LCK_EX, wait_flag))
{
return true;
}
break;
default:
break;
}
// Clear the status vector, as our callers check the return value
// and throw custom exceptions themselves
fb_utils::init_status(tdbb->tdbb_status_vector);
// If we are supposed to wait (presumably patiently),
// but can't get the lock, generate an error
if (wait_flag == LCK_WAIT) {
ERR_post(Arg::Gds(isc_deadlock));
}
dbb->dbb_flags &= ~DBB_exclusive;
return false;
}
bool CCH_exclusive_attachment(thread_db* tdbb, USHORT level, SSHORT wait_flag)
{
/**************************************
*
* C C H _ e x c l u s i v e _ a t t a c h m e n t
*
**************************************
*
* Functional description
* Get exclusive access to a database. If we get it, return true.
* If the wait flag is FALSE, and we can't get it, give up and
* return false.
*
**************************************/
const int CCH_EXCLUSIVE_RETRY_INTERVAL = 1; // retry interval in seconds
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
Jrd::Attachment* attachment = tdbb->getAttachment();
if (attachment->att_flags & ATT_exclusive) {
return true;
}
attachment->att_flags |= (level == LCK_none) ? ATT_attach_pending : ATT_exclusive_pending;
const SLONG timeout = (SLONG) (wait_flag < 0) ?
-wait_flag : ((wait_flag == LCK_WAIT) ? 1L << 30 : CCH_EXCLUSIVE_RETRY_INTERVAL);
// If requesting exclusive database access, then re-position attachment as the
// youngest so that pending attachments may pass.
if (level != LCK_none)
{
for (Jrd::Attachment** ptr = &dbb->dbb_attachments; *ptr; ptr = &(*ptr)->att_next)
{
if (*ptr == attachment)
{
*ptr = attachment->att_next;
break;
}
}
attachment->att_next = dbb->dbb_attachments;
dbb->dbb_attachments = attachment;
}
for (SLONG remaining = timeout; remaining > 0; remaining -= CCH_EXCLUSIVE_RETRY_INTERVAL)
{
if (tdbb->getAttachment()->att_flags & ATT_shutdown) {
break;
}
bool found = false;
for (attachment = tdbb->getAttachment()->att_next; attachment;
attachment = attachment->att_next)
{
if (attachment->att_flags & ATT_shutdown) {
continue;
}
if (level == LCK_none)
{
// Wait for other attachments requesting exclusive access
if (attachment->att_flags & (ATT_exclusive | ATT_exclusive_pending))
{
found = true;
break;
}
// Forbid multiple attachments in single-user maintenance mode
if (attachment != tdbb->getAttachment() && (dbb->dbb_ast_flags & DBB_shutdown_single) )
{
found = true;
break;
}
}
else
{
// Requesting exclusive database access
found = true;
if (attachment->att_flags & ATT_exclusive_pending)
{
tdbb->getAttachment()->att_flags &= ~ATT_exclusive_pending;
if (wait_flag == LCK_WAIT) {
ERR_post(Arg::Gds(isc_deadlock));
}
else {
return false;
}
}
break;
}
}
if (!found)
{
tdbb->getAttachment()->att_flags &= ~(ATT_exclusive_pending | ATT_attach_pending);
if (level != LCK_none) {
tdbb->getAttachment()->att_flags |= ATT_exclusive;
}
return true;
}
// Our thread needs to sleep for CCH_EXCLUSIVE_RETRY_INTERVAL seconds.
if (remaining > CCH_EXCLUSIVE_RETRY_INTERVAL)
{
Database::Checkout dcoHolder(dbb);
THREAD_SLEEP(CCH_EXCLUSIVE_RETRY_INTERVAL * 1000);
}
if (tdbb->getAttachment()->att_flags & ATT_cancel_raise)
{
if (JRD_reschedule(tdbb, 0, false))
{
tdbb->getAttachment()->att_flags &= ~(ATT_exclusive_pending | ATT_attach_pending);
ERR_punt();
}
}
}
tdbb->getAttachment()->att_flags &= ~(ATT_exclusive_pending | ATT_attach_pending);
return false;
}
void CCH_expand(thread_db* tdbb, ULONG number)
{
/**************************************
*
* C C H _ e x p a n d
*
**************************************
*
* Functional description
* Expand the cache to at least a given number of buffers. If
* it's already that big, don't do anything.
*
**************************************/
SET_TDBB(tdbb);
//BCB_MUTEX_ACQUIRE;
expand_buffers(tdbb, number);
//BCB_MUTEX_RELEASE;
}
pag* CCH_fake(thread_db* tdbb, WIN* window, SSHORT latch_wait)
{
/**************************************
*
* C C H _ f a k e
*
**************************************
*
* Functional description
* Fake a fetch to a page. Rather than reading it, however,
* zero it in memory. This is used when allocating a new page.
*
* input
* latch_wait: 1 => Wait as long as necessary to get the latch.
* This can cause deadlocks of course.
* 0 => If the latch can't be acquired immediately,
* or an IO would be necessary, then give
* up and return 0.
* <negative number> => Latch timeout interval in seconds.
*
* return
* pag pointer if successful.
* NULL pointer if timeout occurred (only possible if latch_wait <> 1).
* NULL pointer if latch_wait=0 and the faked page would have to be
* before reuse.
*
**************************************/
SET_TDBB(tdbb);
Database* const dbb = tdbb->getDatabase();
CCH_TRACE(("FK %d:%06d", window->win_page.getPageSpaceID(), window->win_page.getPageNum()));
// if there has been a shadow added recently, go out and
// find it before we grant any more write locks
if (dbb->dbb_ast_flags & DBB_get_shadows) {
SDW_get_shadows(tdbb);
}
Jrd::Attachment* attachment = tdbb->getAttachment();
if (!attachment->backupStateReadLock(tdbb, latch_wait))
return NULL;
BufferDesc* bdb = get_buffer(tdbb, window->win_page, LATCH_exclusive, latch_wait);
if (!bdb)
{
attachment->backupStateReadUnLock(tdbb);
return NULL; // latch timeout occurred
}
// If a dirty orphaned page is being reused - better write it first
// to clear current precedences and checkpoint state. This would also
// update the bcb_free_pages field appropriately.
if (bdb->bdb_flags & (BDB_dirty | BDB_db_dirty))
{
// If the caller didn't want to wait at all, then
// return 'try to fake an other page' to the caller.
if (!latch_wait)
{
attachment->backupStateReadUnLock(tdbb);
release_bdb(tdbb, bdb, false, false, false);
return NULL;
}
if (!write_buffer(tdbb, bdb, bdb->bdb_page, true, tdbb->tdbb_status_vector, true))
{
attachment->backupStateReadUnLock(tdbb);
CCH_unwind(tdbb, true);
}
}
else if (QUE_NOT_EMPTY(bdb->bdb_lower))
{
// Clear residual precedence left over from AST-level I/O.
clear_precedence(tdbb, bdb);
}
// Here the page must not be dirty and have no backup lock owner
fb_assert((bdb->bdb_flags & (BDB_dirty | BDB_db_dirty)) == 0);
bdb->bdb_flags = (BDB_writer | BDB_faked);
bdb->bdb_scan_count = 0;
lock_buffer(tdbb, bdb, LCK_WAIT, pag_undefined);
MOVE_CLEAR(bdb->bdb_buffer, (SLONG) dbb->dbb_page_size);
window->win_buffer = bdb->bdb_buffer;
window->win_bdb = bdb;
window->win_flags = 0;
CCH_MARK(tdbb, window);
return bdb->bdb_buffer;
}
pag* CCH_fetch(thread_db* tdbb, WIN* window, USHORT lock_type, SCHAR page_type, SSHORT latch_wait,
const bool read_shadow, const bool merge_flag)
{
/**************************************
*
* C C H _ f e t c h
*
**************************************
*
* Functional description
* Fetch a specific page. If it's already in cache,
* so much the better.
*
* input
* latch_wait: 1 => Wait as long as necessary to get the latch.
* This can cause deadlocks of course.
* 0 => If the latch can't be acquired immediately,
* give up and return 0.
* <negative number> => Latch timeout interval in seconds.
*
* return
* PAG if successful.
* NULL pointer if timeout occurred (only possible if latch_wait <> 1).
*
**************************************/
SET_TDBB(tdbb);
//Database* dbb = tdbb->getDatabase();
CCH_TRACE(("FE %d:%06d", window->win_page.getPageSpaceID(), window->win_page.getPageNum()));
// FETCH_LOCK will return 0, 1, -1 or -2
const SSHORT fetch_lock_return = CCH_FETCH_LOCK(tdbb, window, lock_type, latch_wait, page_type);
switch (fetch_lock_return)
{
case 1:
CCH_TRACE(("FE %d:%06d", window->win_page.getPageSpaceID(), window->win_page.getPageNum()));
CCH_FETCH_PAGE(tdbb, window, read_shadow, merge_flag); // must read page from disk
break;
case -2:
case -1:
return NULL; // latch or lock timeout
}
BufferDesc* bdb = window->win_bdb;
// if merge_flag is not set the page will be changed before or after merge
// and should be written into delta again
if (!merge_flag)
bdb->bdb_flags &= ~BDB_merge;
// If a page was read or prefetched on behalf of a large scan
// then load the window scan count into the buffer descriptor.
// This buffer scan count is decremented by releasing a buffer
// with CCH_RELEASE_TAIL.
// Otherwise zero the buffer scan count to prevent the buffer
// from being queued to the LRU tail.
if (window->win_flags & WIN_large_scan)
{
if (fetch_lock_return == 1 || bdb->bdb_flags & BDB_prefetch || bdb->bdb_scan_count < 0)
{
bdb->bdb_scan_count = window->win_scans;
}
}
else if (window->win_flags & WIN_garbage_collector)
{
if (fetch_lock_return == 1) {
bdb->bdb_scan_count = -1;
}
if (bdb->bdb_flags & BDB_garbage_collect) {
window->win_flags |= WIN_garbage_collect;
}
}
else if (window->win_flags & WIN_secondary)
{
if (fetch_lock_return == 1) {
bdb->bdb_scan_count = -1;
}
}
else
{
bdb->bdb_scan_count = 0;
if (bdb->bdb_flags & BDB_garbage_collect) {
bdb->bdb_flags &= ~BDB_garbage_collect;
}
}
// Validate the fetched page matches the expected type
if (bdb->bdb_buffer->pag_type != page_type && page_type != pag_undefined)
{
page_validation_error(tdbb, window, page_type);
}
return window->win_buffer;
}
SSHORT CCH_fetch_lock(thread_db* tdbb, WIN* window, USHORT lock_type, SSHORT wait, SCHAR page_type)
{
/**************************************
*
* C C H _ f e t c h _ l o c k
*
**************************************
*
* Functional description
* Fetch a latch and lock for a specific page.
*
* input
*
* wait:
* LCK_WAIT (1) => Wait as long a necessary to get the lock.
* This can cause deadlocks of course.
*
* LCK_NO_WAIT (0) =>
* If the latch can't be acquired immediately, give up and return -2.
* If the lock can't be acquired immediately, give up and return -1.
*
* <negative number> => Lock (latch) timeout interval in seconds.
*
* return
* 0: fetch & lock were successful, page doesn't need to be read.
* 1: fetch & lock were successful, page needs to be read from disk.
* -1: lock timed out, fetch failed.
* -2: latch timed out, fetch failed, lock not attempted.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
// if there has been a shadow added recently, go out and
// find it before we grant any more write locks
if (dbb->dbb_ast_flags & DBB_get_shadows) {
SDW_get_shadows(tdbb);
}
// Look for the page in the cache.
Jrd::Attachment* attachment = tdbb->getAttachment();
if (!attachment->backupStateReadLock(tdbb, wait))
return -2;
BufferDesc* bdb = get_buffer(tdbb, window->win_page,
((lock_type >= LCK_write) ? LATCH_exclusive : LATCH_shared), wait);
if (wait != 1 && bdb == 0) {
return -2; // latch timeout
}
if (lock_type >= LCK_write)
{
bdb->bdb_flags |= BDB_writer;
}
window->win_bdb = bdb;
window->win_buffer = bdb->bdb_buffer;
// lock_buffer returns 0 or 1 or -1.
const SSHORT lock_result = lock_buffer(tdbb, bdb, wait, page_type);
if (lock_result == -1)
attachment->backupStateReadUnLock(tdbb);
return lock_result;
}
void CCH_fetch_page(thread_db* tdbb, WIN* window, const bool read_shadow, const bool merge_flag)
{
/**************************************
*
* C C H _ f e t c h _ p a g e
*
**************************************
*
* Functional description
* Fetch a specific page. If it's already in cache,
* so much the better. When "compute_checksum" is 1, compute
* the checksum of the page. When it is 2, compute
* the checksum only when the page type is nonzero.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
BufferDesc* bdb = window->win_bdb;
ISC_STATUS* const status = tdbb->tdbb_status_vector;
pag* page = bdb->bdb_buffer;
bdb->bdb_incarnation = ++dbb->dbb_page_incarnation;
tdbb->bumpStats(RuntimeStatistics::PAGE_READS);
page = bdb->bdb_buffer;
PageSpace* pageSpace = dbb->dbb_page_manager.findPageSpace(bdb->bdb_page.getPageSpaceID());
fb_assert(pageSpace);
jrd_file* file = pageSpace->file;
SSHORT retryCount = 0;
const bool isTempPage = pageSpace->isTemporary();
/*
We will read a page, and if there is an I/O error we will try to
use the shadow file, and try reading again, for a maximum of
3 tries, before it gives up.
The read_shadow flag is set to false only in the call to
FETCH_NO_SHADOW, which is only called from validate
code.
read_shadow = false -> IF an I/O error occurs give up (exit
the loop, clean up, and return). So the caller,
validate in most cases, can know about it and attempt
to remedy the situation.
read_shadow = true -> IF an I/O error occurs attempt
to rollover to the shadow file. If the I/O error is
persistant (more than 3 times) error out of the routine by
calling CCH_unwind, and eventually punting out.
*/
BackupManager* bm = dbb->dbb_backup_manager;
const int bak_state = bm->getState();
fb_assert(bak_state != nbak_state_unknown);
ULONG diff_page = 0;
if (!isTempPage && bak_state != nbak_state_normal)
{
BackupManager::AllocReadGuard allocGuard(tdbb, dbb->dbb_backup_manager);
diff_page = bm->getPageIndex(tdbb, bdb->bdb_page.getPageNum());
NBAK_TRACE(("Reading page %d:%06d, state=%d, diff page=%d",
bdb->bdb_page.getPageSpaceID(), bdb->bdb_page.getPageNum(), bak_state, diff_page));
}
// In merge mode, if we are reading past beyond old end of file and page is in .delta file
// then we maintain actual page in difference file. Always read it from there.
if (isTempPage || bak_state == nbak_state_normal || !diff_page)
{
NBAK_TRACE(("Reading page %d:%06d, state=%d, diff page=%d from DISK",
bdb->bdb_page.getPageSpaceID(), bdb->bdb_page.getPageNum(), bak_state, diff_page));
// Read page from disk as normal
while (!PIO_read(file, bdb, page, status))
{
if (isTempPage || !read_shadow) {
break;
}
if (!CCH_rollover_to_shadow(tdbb, dbb, file, false))
{
PAGE_LOCK_RELEASE(bdb->bdb_lock);
CCH_unwind(tdbb, true);
}
if (file != pageSpace->file) {
file = pageSpace->file;
}
else
{
if (retryCount++ == 3)
{
fprintf(stderr, "IO error loop Unwind to avoid a hang\n");
PAGE_LOCK_RELEASE(bdb->bdb_lock);
CCH_unwind(tdbb, true);
}
}
}
}
else
{
NBAK_TRACE(("Reading page %d, state=%d, diff page=%d from DIFFERENCE",
bdb->bdb_page, bak_state, diff_page));
if (!bm->readDifference(tdbb, diff_page, page))
{
PAGE_LOCK_RELEASE(bdb->bdb_lock);
CCH_unwind(tdbb, true);
}
// This is the first reading the page from delta and it must not be written into it again
if (merge_flag)
bdb->bdb_flags |= BDB_merge;
if ((page->pag_type == 0) && !merge_flag)
{
// We encountered a page which was allocated, but never written to the
// difference file. In this case we try to read the page from database. With
// this approach if the page was old we get it from DISK, and if the page
// was new IO error (EOF) or BUGCHECK (checksum error) will be the result.
// Engine is not supposed to read a page which was never written unless
// this is a merge process.
NBAK_TRACE(("Re-reading page %d, state=%d, diff page=%d from DISK",
bdb->bdb_page, bak_state, diff_page));
while (!PIO_read(file, bdb, page, status))
{
if (!read_shadow) {
break;
}
if (!CCH_rollover_to_shadow(tdbb, dbb, file, false))
{
PAGE_LOCK_RELEASE(bdb->bdb_lock);
CCH_unwind(tdbb, true);
}
if (file != pageSpace->file) {
file = pageSpace->file;
}
else
{
if (retryCount++ == 3)
{
fprintf(stderr, "IO error loop Unwind to avoid a hang\n");
PAGE_LOCK_RELEASE(bdb->bdb_lock);
CCH_unwind(tdbb, true);
}
}
}
}
}
bdb->bdb_flags &= ~(BDB_not_valid | BDB_read_pending);
window->win_buffer = bdb->bdb_buffer;
}
void CCH_forget_page(thread_db* tdbb, WIN* window)
{
/**************************************
*
* C C H _ f o r g e t _ p a g e
*
**************************************
*
* Functional description
* Page was faked but can't be written on disk. Most probably because
* of out of disk space. Release page buffer and others resources and
* unlink page from various queues
*
**************************************/
SET_TDBB(tdbb);