Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*-------------------------------------------------------------------------
 *
 * cfs_state.h
 *    Single ownership-state judge for the CFS sidecar pair.
 *
 * Every "did the journal reset / the .cfsmap swap / the shared generation
 * move under this handle" question in CFS is answered HERE and only here.
 * Callers derive nothing themselves: they capture a snapshot (or hold a
 * baseline incarnation/cursor) and switch on the returned flags.  The
 * per-caller masks are documented in cfs_state.c.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 2026, Diasoft
 *
 * src/include/storage/cfs/cfs_state.h
 *
 *-------------------------------------------------------------------------
 */
 
#ifndef CFS_STATE_H
#define CFS_STATE_H
 
#include "storage/cfs/cfs_journal.h"
#include "storage/cfs/cfs_storage.h"
 
/*
 * CfsStateSnapshot — the identity of the sidecar pair as one handle sees
 * it at capture time.  Sums up the four staleness signals CFS has: the
 * .cfsmap inode behind the handle, the .cfsjrn inode/size, the journal
 * header (incarnation + generation watermark) and the shared-cache map
 * generation.
 */
typedef struct CfsStateSnapshot
{
    /* .cfsmap identity by the handle's descriptor. */
    dev_t       map_dev;
    ino_t       map_ino;
    bool        map_fd_valid;
 
    /* .cfsjrn identity by the handle's descriptor. */
    dev_t       jrn_dev;
    ino_t       jrn_ino;
    off_t       jrn_size;
    bool        jrn_fd_valid;
 
    /*
     * Journal header fields; jrn_incarnation/jrn_watermark are 0 and
     * jrn_header_valid is false when the header could not be read (the
     * comparison semantics follow the old cfs_journal_identity fallback).
     */
    uint16      jrn_incarnation;
    uint32      jrn_watermark;
    bool        jrn_header_valid;
 
    /*
     * The incarnation through the raw-fields read with the handle fallback
     * (what cfs_journal_incarnation() returned): the baseline of the
     * under-append-lock phase-2 check, kept distinct from the fully validated
     * identity above because the two reads disagree exactly in the
     * torn-header window a lock-free reader must treat as "moved".
     */
    uint16      jrn_incarnation_stamped;
 
    /* Shared-cache map generation of the relation fork. */
    uint64      map_generation;
} CfsStateSnapshot;
 
/*
 * Verdict flags.  A caller masks the subset it acts on; the judge computes
 * every signal so no caller ever re-derives one.
 */
#define CFS_STATE_OK                    0
#define CFS_STATE_MAP_SWAPPED           (1 << 0)    /* path != fd */
#define CFS_STATE_JRN_RESET             (1 << 1)    /* incarnation moved */
#define CFS_STATE_JRN_SHRANK                (1 << 2)    /* size < cursor */
#define CFS_STATE_JRN_GREW              (1 << 3)    /* size > cursor */
#define CFS_STATE_JRN_HEADER_STALE      (1 << 4)    /* header unreadable now,
                                                     * was valid at base */
#define CFS_STATE_JRN_STAT_FAILED       (1 << 5)    /* fstat(jrn_fd) failed */
#define CFS_STATE_JRN_IDENTITY_MOVED    (1 << 6)    /* dev/ino/watermark/header
                                                     * validity moved vs base */
#define CFS_STATE_GEN_MOVED             (1 << 7)    /* shared generation moved */
 
/*
 * cfs_state_capture — snapshot the full sidecar identity of a handle: map
 * and journal inode pairs, journal header, shared map generation.  Used as
 * the base of cfs_state_check().
 */
extern void cfs_state_capture(CfsRelation *cfs, CfsStateSnapshot *snap);
 
/*
 * cfs_state_check — compare the handle's current sidecar identity against
 * a base captured by cfs_state_capture().  'cursor_off' is the journal
 * replay cursor the caller considers synced (the handle's
 * map_synced_jrn_off or an equivalent baseline); the SHRANK/GREW flags are
 * reported against it.  When 'fresh_jhdr' is non-NULL it receives the
 * freshly read journal header (valid unless JRN_HEADER_STALE is set).
 *
 * Returns a CFS_STATE_* flag set; never raises.
 */
extern int  cfs_state_check(const CfsStateSnapshot *base, CfsRelation *cfs,
                            off_t cursor_off,
                            CfsJournalFileHeader *fresh_jhdr);
 
/*
 * cfs_state_journal_moved — the snapshot-free variant for callers that
 * hold a baseline incarnation (the handle's jrn_incarnation or a segment
 * descriptor's stamp) plus a replay cursor: compares the on-disk journal
 * against them.  This is the whole of what cfs_map_needs_refresh(),
 * cfs_refresh_map() and cfs_shared_map_sync_segment() ever knew; they now
 * only switch on the result.  'fresh_jhdr' as in cfs_state_check().
 */
extern int  cfs_state_journal_moved(CfsRelation *cfs, uint16 base_incarnation,
                                    off_t cursor_off,
                                    CfsJournalFileHeader *fresh_jhdr);
 
/*
 * cfs_map_file_swapped — true when the .cfsmap file was replaced
 * (compaction swap, immedsync snapshot) since this handle opened it: the
 * handle's descriptor then points at the old inode.  Moved here from
 * cfs_storage.c: it is the map-side half of the judge, and the
 * CFS_STATE_MAP_SWAPPED flag is computed from it.
 */
extern bool cfs_map_file_swapped(CfsRelation *cfs);
 
/*
 * The shared-cache generation provider.  The storage layer tags freshly
 * opened handles with the relation's map generation but must not call
 * into the shared-memory layer directly (the storage -> shmem edge the
 * review called out); the relmeta layer installs its lookup here at
 * shared-memory initialisation.  A NULL provider reports 0, which reads
 * as "no slot yet" — the same value an absent relmeta slot gives.
 */
typedef uint64 (*CfsGenerationProvider) (RelFileLocatorBackend rlocator,
                                         ForkNumber forknum);
 
extern void cfs_state_set_generation_provider(CfsGenerationProvider fn);
 
/*
 * cfs_state_relation_generation — the relation's current shared-cache map
 * generation through the installed provider (0 when none).  The only
 * sanctioned way to read the generation outside the shmem layer.
 */
extern uint64 cfs_state_relation_generation(RelFileLocatorBackend rlocator,
                                            ForkNumber forknum);
 
/*
 * The online-compaction liveness provider (QDB-1514), installed by the
 * relmeta layer like the generation provider above.  The open path asks
 * cfs_state_compaction_active() before running the interrupted-swap
 * recovery: a live compaction owns its candidate files, and the recovery
 * must not touch them.  A NULL provider reports "no compaction", which is
 * correct wherever the shared cache does not exist (startup) — the
 * startup scan is single-writer anyway.
 */
typedef bool (*CfsCompactionActiveProvider) (RelFileLocatorBackend rlocator,
                                             ForkNumber forknum);
 
extern void cfs_state_set_compaction_provider(CfsCompactionActiveProvider fn);
 
/*
 * cfs_state_compaction_active — is an online compaction with a live owner
 * running for this relation fork?  The only sanctioned way to ask from the
 * storage layer and below.
 */
extern bool cfs_state_compaction_active(RelFileLocatorBackend rlocator,
                                        ForkNumber forknum);
 
/*
 * The sidecar-open guard (QDB-1514), installed by the relmeta layer like
 * the providers above.  A lock-free open of the sidecar files must
 * serialize against a compactor's exclusive swap phase, or the opened
 * descriptor set and the generation tag can straddle the swap — a mixed
 * old/new pair that no staleness check can detect afterwards.  The guard
 * is a shared hold of the per-relation append lock; it is cheap (opens are
 * per-handle, not per-read) and skipped when this backend already holds
 * the lock in any mode (checkpoint, fsck, the compactor itself).
 * cfs_state_open_guard_acquire() returns true when the caller must call
 * the release; false when there is nothing to release (no provider —
 * single-threaded startup — or the lock is already held).
 */
typedef struct CfsOpenGuardOps
{
    bool        (*open_guard_acquire) (RelFileLocatorBackend rlocator,
                                       ForkNumber forknum);
    void        (*open_guard_release) (RelFileLocatorBackend rlocator,
                                      ForkNumber forknum, bool after_error);
} CfsOpenGuardOps;
 
extern void cfs_state_set_open_guard_ops(const CfsOpenGuardOps *ops);
 
extern bool cfs_state_open_guard_acquire(RelFileLocatorBackend rlocator,
                                         ForkNumber forknum);
extern void cfs_state_open_guard_release(RelFileLocatorBackend rlocator,
                                         ForkNumber forknum, bool after_error);
 
/*
 * The authoritative .cfp tail provider, installed by the relmeta layer the
 * same way as the generation provider (see cfs_shmem.h for the protocol
 * semantics of each callback).  The storage layer consults and maintains
 * the shared tail through these hooks instead of calling into the
 * shared-memory layer directly; a NULL provider means "no shared tail"
 * (before shared-memory initialisation, or a caller with no metadata
 * slot), and every operation then degenerates to the historical
 * per-append cfs_cfp_discover_eof() walk.
 */
typedef struct CfsCfpTailOps
{
    /* Fetch the trustworthy tail; false when the slot is missing/untrusted. */
    bool       (*tail_fetch) (RelFileLocatorBackend rlocator,
                              ForkNumber forknum,
                              uint64 *out_tail);
    /* Mark the append in progress with the current backend as owner. */
    void        (*tail_begin) (RelFileLocatorBackend rlocator,
                               ForkNumber forknum);
    /* Publish the new tail after a successful pwrite. */
    void        (*tail_commit) (RelFileLocatorBackend rlocator,
                                ForkNumber forknum,
                                uint64 new_tail);
    /* Drop trust in the stored tail. */
    void        (*tail_dirty) (RelFileLocatorBackend rlocator,
                               ForkNumber forknum);
} CfsCfpTailOps;
 
extern void cfs_state_set_cfp_tail_ops(const CfsCfpTailOps *ops);
 
/*
 * cfs_state_cfp_tail_fetch/begin/commit/dirty — the provider-backed entry
 * points used by the storage layer's append path.  The fetch returns false
 * when no provider is installed; the mutators are no-ops then.
 */
extern bool cfs_state_cfp_tail_fetch(RelFileLocatorBackend rlocator,
                                     ForkNumber forknum,
                                     uint64 *out_tail);
extern void cfs_state_cfp_tail_begin(RelFileLocatorBackend rlocator,
                                     ForkNumber forknum);
extern void cfs_state_cfp_tail_commit(RelFileLocatorBackend rlocator,
                                      ForkNumber forknum,
                                      uint64 new_tail);
extern void cfs_state_cfp_tail_dirty(RelFileLocatorBackend rlocator,
                                     ForkNumber forknum);
 
#endif                          /* CFS_STATE_H */