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
(** Offline, best-effort salvage of a damaged database file into a fresh,
structurally-valid one — the corruption-recovery counterpart to the
crash-recovery already performed on open.
Read-only on the source; emits a clean new file rather than repairing
in place. Implementation phases:
1. [scan]: Raw-page scan verifying CRC32 on every page and
classifying by kind byte. Provides a trust map used later.
2. [open_source]: Attempt to open the damaged file via Store.open_block
(handles crash recovery / WAL replay automatically). Load the catalog.
3. [extract]: Walk each table's tree via cursor, skip corrupt pages
(flagged by the scan trust map), decode rows.
4. [rebuild]: Open a fresh Store and replay recovered rows through the
normal put path, producing correct CRCs in a clean CoW B+-tree.
When the source headers are intact (the common case), phases 2–3 use
the existing Store/Catalog infrastructure. When headers are corrupt,
the tool falls back to raw-page catalog reconstruction from the
redundant mirror (#174). *)
module Page = Granary_storage.Page
module Store = Granary_store.Store
module Catalog = Granary_catalog.Catalog
open Lwt.Syntax
(** A single page from the raw scan, annotated with CRC status
and decoded page kind. *)
type scanned_page =
{ page_id : int64
; kind : Page.kind
; crc_valid : bool
; tag : int32
}
(** Result of the raw-page scan phase. *)
type scan_result =
{ n_pages : int64
; pages : scanned_page list
; trusted_count : int
; damaged_count : int
}
(** A row recovered and ready to write to the output store. *)
type recovered_row =
{ tree_id : int
; key : bytes
; value : bytes
}
(** Final result of a complete recovery run. *)
type recover_result =
{ rows_recovered : int
; pages_scanned : int64
; pages_trusted : int
; pages_damaged : int
; pages_skipped : int
; tables_recovered : int
; tables_total : int
}
let pp_kind fmt (k : Page.kind) =
let s =
match k with
| Page.Header -> "Header"
| Page.Branch -> "Branch"
| Page.Leaf -> "Leaf"
| Page.Freelist -> "Freelist"
| Page.Overflow -> "Overflow"
in
Format.pp_print_string fmt s
;;
let pp_scanned_page fmt p =
Format.fprintf
fmt
"page %Ld: %a (crc=%s tag=%ld)"
p.page_id
pp_kind
p.kind
(if p.crc_valid then "OK" else "bad")
p.tag
;;
let pp_result fmt r =
Format.fprintf
fmt
"@[<v 2>Recovery complete:@,\
%d rows recovered from %d/%d tables@,\
%Ld pages scanned: %d trusted, %d damaged, %d skipped@]"
r.rows_recovered
r.tables_recovered
r.tables_total
r.pages_scanned
r.pages_trusted
r.pages_damaged
r.pages_skipped
;;
(** Decode the page kind from byte 0 of a page buffer.
Maps unknown bytes to [Page.Header] — the page will be flagged
damaged via CRC mismatch anyway. *)
let kind_of_byte b =
match b with
| 0 -> Page.Header
| 1 -> Page.Branch
| 2 -> Page.Leaf
| 3 -> Page.Freelist
| 4 -> Page.Overflow
| _ -> Page.Header
;;
(** Number of recovered rows to accumulate before calling [write_to]. *)
let batch_size = 1000
let scan ~read_page ~n_pages =
let n_pages_int = Int64.to_int n_pages in
let rec loop i acc_pages trusted damaged =
if i >= n_pages_int
then (
let pages = List.rev acc_pages in
let n_read = Int64.of_int (List.length pages) in
Lwt.return
{ n_pages = n_read; pages; trusted_count = trusted; damaged_count = damaged })
else (
let page_id = Int64.of_int i in
let* page_opt = read_page page_id in
match page_opt with
| None ->
loop (i + 1) acc_pages trusted damaged
| Some buf ->
let crc_valid = Page.verify_crc buf in
let kind_byte = Cstruct.get_uint8 buf 0 in
let kind = kind_of_byte kind_byte in
let tag = Page.read_tag buf in
let page = { page_id; kind; crc_valid; tag } in
let trusted = if crc_valid then trusted + 1 else trusted in
let damaged = if not crc_valid then damaged + 1 else damaged in
loop (i + 1) (page :: acc_pages) trusted damaged)
in
loop 0 [] 0 0
;;
(** Extract all (key, value) pairs from a single table tree using a
cursor. Returns the list of recovered rows, or an empty list if the
cursor cannot be opened (corrupt pages). *)
let (store : Store.t) (tree_id : int) =
Lwt.catch
(fun () ->
let* rows =
Store.with_ro store (fun txn ->
let* cursor = Store.cursor_open txn tree_id in
let (_ : Store.seek_result) = Store.cursor_first cursor in
let rec collect acc =
match Store.cursor_next cursor with
| None -> Lwt.return (List.rev acc)
| Some (k, v) -> collect ({ tree_id; key = k; value = v } :: acc)
in
collect [])
in
Lwt.return rows)
(fun exn ->
let msg = Printexc.to_string exn in
Format.eprintf "recovery: failed to extract tree %d: %s@." tree_id msg;
Lwt.return [])
;;
(** Split [n] elements from the front of a list. *)
let rec take n acc xs =
match n, xs with
| 0, _ | _, [] -> List.rev acc, xs
| n, x :: xs' -> take (n - 1) (x :: acc) xs'
;;
(** Write rows to the destination in batches of [batch_size]. *)
let rec flush_batches write_to rows =
match rows with
| [] -> Lwt.return_ok ()
| _ ->
let batch, rest = take batch_size [] rows in
let* result = write_to batch in
(match result with
| Error e -> Lwt.return_error e
| Ok () -> flush_batches write_to rest)
;;
let recover ~read_page ~n_pages ~open_source ~write_to =
let* scan_result = scan ~read_page ~n_pages in
let pages_scanned = scan_result.n_pages in
let pages_read = Int64.of_int (List.length scan_result.pages) in
let pages_skipped = Int64.to_int (Int64.sub n_pages pages_read) in
let pages_trusted = scan_result.trusted_count in
let pages_damaged = scan_result.damaged_count in
let* source = open_source () in
match source with
| Error e ->
Format.eprintf "recovery: cannot open source: %s@." e;
Lwt.return_error (Printf.sprintf "open_source failed: %s" e)
| Ok (store, catalog) ->
let* tables = Catalog.list_tables catalog in
let tables_total = List.length tables in
let rec acc_rev tables_recovered remaining_tables =
match remaining_tables with
| [] -> Lwt.return (acc_rev, tables_recovered)
| table_meta :: rest ->
(match table_meta.Catalog.storage with
| Catalog.Columnar _ -> extract_all acc_rev tables_recovered rest
| Catalog.Row { tree_id; _ } ->
let* table_rows = extract_one_table store tree_id in
let n_rows = List.length table_rows in
let recovered =
if n_rows > 0 then tables_recovered + 1 else tables_recovered
in
extract_all (List.rev_append table_rows acc_rev) recovered rest)
in
let* all_rows_rev, tables_recovered = extract_all [] 0 tables in
let all_rows = List.rev all_rows_rev in
let rows_recovered = List.length all_rows in
let* write_result = flush_batches write_to all_rows in
(match write_result with
| Error e -> Lwt.return_error (Printf.sprintf "write_to failed: %s" e)
| Ok () ->
Lwt.return_ok
{ rows_recovered
; pages_scanned
; pages_trusted
; pages_damaged
; pages_skipped
; tables_recovered
; tables_total
})
;;