package cactus

  1. Overview
  2. Docs

Source file store.ml

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
(*
 * Copyright (c) 2021 Tarides <contact@tarides.com>
 * Copyright (c) 2021 Gabriel Belouze <gabriel.belouze@ens.psl.eu>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

include Store_intf

module Make (Params : Params.S) (Common : Field.COMMON) = struct
  module Common = Common

  module Syscalls = struct
    include Syscalls

    exception RandomFailure (* raised to test recoverability at a given point *)

    let pwrite ~fd ~fd_offset ~buffer ~buffer_offset ~length =
      if Params.Debug.random_failure && Random.int 500 = 1 then raise RandomFailure
      else pwrite ~fd ~fd_offset ~buffer ~buffer_offset ~length
  end

  exception RandomFailure = Syscalls.RandomFailure

  module Header : HEADER with module Common := Common = struct
    type t = bytes

    open Common

    let sizes = [ Magic.size; Address.size ]

    type offsets = { magic : int; root : int }

    let offsets =
      match Utils.sizes_to_offsets sizes with
      | [ magic; root ] -> { magic; root }
      | _ -> failwith "Incorrect offsets"

    let size = List.fold_left ( + ) 0 sizes

    let load buff = buff

    let g_magic t = Magic.get t ~off:offsets.magic

    let s_magic t magic = Magic.set ~marker:Utils.nop t ~off:offsets.magic magic

    let g_root t = Address.get t ~off:offsets.root

    let s_root t root = Address.set ~marker:Utils.nop t ~off:offsets.root root

    let init t ~root =
      s_magic t @@ Magic.to_t @@ Params.page_magic;
      s_root t @@ Address.to_t @@ root

    let pp ppf t =
      Fmt.pf ppf "@[<v 2>Header:@;Magic:%a@;Root:%a@]" Magic.pp (g_magic t) Address.pp (g_root t)

    let dump t = t
  end

  open Stats.Func
  (** STAT WRAPPERS **)

  open Stats.Store

  type address = int
  (* The address of a page [p] is the page number of [p] inside the btree
     file. *)

  type content = { buff : bytes; mutable dirty : bool }

  module AddressHash = struct
    type t = address

    let equal = Int.equal

    let hash = Hashtbl.hash
  end

  module Content = struct
    type t = content
  end

  module CaliforniaCache = Cache.Make (AddressHash) (Content)

  type fd = Unix.file_descr

  type t = {
    mutable n_pages : int;
    mutable dead_pages : int list;
    header : Header.t;
    fd : fd;
    dir : string;
    cache : CaliforniaCache.t;
  }

  type page = { address : address; fd : fd; content : content }

  module Page = struct
    type pointer = int
    (* A pointer is an offset inside a page. *)

    type store = t

    type t = page

    let max_size = Params.page_sz

    let real_offset address = max_size * address

    let _load fd address buff =
      let start = real_offset address in
      Utils.assert_pread fd buff start max_size;
      increment stat_io_r "nb_bytes" max_size;
      let content = { buff; dirty = false } in
      content

    let load fd address =
      let buff = Bytes.create max_size in
      _load fd address buff

    let load_using fd ?available address =
      match available with
      | None -> _load fd address (Bytes.create max_size)
      | Some content -> _load fd address content.buff

    let _flush fd address content =
      if content.dirty then (
        content.dirty <- false;
        tic stat_io_w;
        let write_size =
          Syscalls.pwrite ~fd
            ~fd_offset:(real_offset address |> Optint.Int63.of_int)
            ~buffer:content.buff ~buffer_offset:0 ~length:max_size
        in
        assert (write_size = max_size);
        Index_stats.add_write write_size;
        tac stat_io_w;
        increment stat_io_w "nb_bytes" write_size)

    let flush t = _flush t.fd t.address t.content

    let buff t = t.content.buff

    let marker t () = t.content.dirty <- true

    let _buff0 = Bytes.make max_size '\000'

    let init (store : store) address =
      tic stat_io_w;
      let write_size =
        Syscalls.pwrite ~fd:store.fd
          ~fd_offset:(real_offset address |> Optint.Int63.of_int)
          ~buffer:_buff0 ~buffer_offset:0 ~length:max_size
      in
      assert (write_size = max_size);
      Index_stats.add_write write_size;
      tac stat_io_w;
      increment stat_io_w "nb_bytes" write_size

    (* Header functions *)

    type offsets = { magic : int; kind : int }

    let sizes = Common.[ Magic.size; Kind.size ]

    let offsets =
      match Utils.sizes_to_offsets sizes with
      | [ magic; kind ] -> { magic; kind }
      | _ -> failwith "Invalid offsets"

    let _kind content = Common.Kind.get content.buff ~off:offsets.kind

    let kind t = _kind t.content
  end

  let fsync (t : t) =
    tic stat_fsync;
    Unix.fsync t.fd;
    tac stat_fsync

  let release t = CaliforniaCache.release t.cache

  let flush t =
    Log.debug (fun reporter -> reporter "Running flush");
    tic stat_flush;
    CaliforniaCache.flush t.cache;
    tac stat_flush

  let load (t : t) address = { address; fd = t.fd; content = CaliforniaCache.find t.cache address }

  let reload t address = CaliforniaCache.reload t.cache address

  let allocate (t : t) =
    match t.dead_pages with
    | [] ->
        let n = t.n_pages in
        Page.init t n;
        (* Write junk bytes to increase the b.tree file length. This allows us
           to subsequently flush pages in any order. *)
        t.n_pages <- n + 1;
        n
    | r :: q ->
        t.dead_pages <- q;
        r

  let deallocate t address =
    t.dead_pages <- address :: t.dead_pages;
    CaliforniaCache.deallocate t.cache address

  let flush_header (t : t) = Utils.assert_pwrite t.fd (Header.dump t.header) 0 Header.size

  let mkdir dirname =
    let rec aux dir k =
      if Sys.file_exists dir && Sys.is_directory dir then k ()
      else (
        if Sys.file_exists dir then Unix.unlink dir;
        (aux [@tailcall]) (Filename.dirname dir) (fun () ->
            Unix.mkdir dir 0o755;
            k ()))
    in
    aux dirname (fun () -> ())

  let max_pages = Params.cache_sz * 1_000_000 / Params.page_sz

  (* The height of the california cache is the maximum height such that
     if the cache is full (all pages at 2*f and all levels full), the number of
     pages is smaller than max_pages: i.e. california_capacity <= max_pages. *)
  let cache_height =
    let f = Params.fanout |> Float.of_int in
    let max_levels =
      Float.to_int
      @@ (Float.log ((Float.of_int max_pages *. ((2. *. f) -. 1.)) +. 1.) /. Float.log (2. *. f))
    in
    max_levels - 1
  (* leaf height is 0 *)

  (* The number of pages in the california cache based on the cache_height. *)
  let california_capacity =
    (Utils.pow (2 * Params.fanout) (cache_height + 1) - 1) / ((2 * Params.fanout) - 1)

  (* Store in the lru everything that does not fit in california cache. *)
  let lru_capacity = max_pages - california_capacity

  let root t = Header.g_root t.header |> Common.Address.from_t

  let check_height t =
    let tree_height = root t |> load t |> Page.kind |> Common.Kind.to_depth in
    Log.debug (fun reporter -> reporter "Tree height is %i" tree_height);
    if tree_height > cache_height then
      Log.warn (fun reporter ->
          reporter "Last %i leaf/node levels are not cached" (tree_height - cache_height))

  let init ~root =
    Log.info (fun reporter -> reporter "Cache height is %i" cache_height);
    let ( // ) x y = x ^ "/" ^ y in
    if not (Sys.file_exists root) then mkdir root;
    let file = root // "b.tree" in

    Log.debug (fun reporter -> reporter "California can hold up to %i pages" california_capacity);
    Log.debug (fun reporter -> reporter "Lru can hold up to %i pages" lru_capacity);

    if Sys.file_exists file then (
      Log.debug (fun reporter -> reporter "Loading btree file found at %s" file);

      let fd = Unix.openfile file Unix.[ O_RDWR ] 0o600 in

      let buff = Bytes.create Header.size in
      Utils.assert_pread fd buff 0 Header.size;
      let header = Header.load buff in

      let file_size = fd |> Unix.fstat |> fun x -> x.st_size in
      let n_pages = file_size / Params.page_sz in

      let tree_height =
        Header.g_root header
        |> Common.Address.from_t
        |> Page.load fd
        |> Page._kind
        |> Common.Kind.to_depth
      in

      let cache =
        CaliforniaCache.v ~flush:(Page._flush fd) ~load:(Page.load_using fd)
          ~filter:(fun content ->
            let depth = Page._kind content |> Common.Kind.to_depth in
            if depth = 0 then `Volatile
            else if tree_height - depth <= cache_height then `California
            else if tree_height - depth = cache_height + 1 then `Lru
            else `Volatile)
          (* only cache the top cache_height part of the tree *)
          lru_capacity
      in

      let t = { n_pages; dead_pages = []; header; fd; dir = root; cache } in
      check_height t;
      t)
    else
      let fd = Unix.openfile file Unix.[ O_RDWR; O_CREAT; O_EXCL ] 0o600 in
      let cache =
        CaliforniaCache.v ~flush:(Page._flush fd) ~load:(Page.load_using fd)
          ~filter:(fun content ->
            let depth = Page._kind content |> Common.Kind.to_depth in
            if depth = 0 then `Volatile else `California)
          lru_capacity
      in
      let header = Bytes.create Header.size |> Header.load in
      let store = { n_pages = 0; dead_pages = []; header; fd; dir = root; cache } in

      ignore (allocate store) (* create page_0 for the header *);
      let root = allocate store in
      Header.init header ~root;
      flush_header store;
      store

  let clear_cache t = CaliforniaCache.clear t.cache

  let clear t =
    t.n_pages <- 0;
    ignore (allocate t);
    (* create page_0 for the header *)
    allocate t |> Common.Address.to_t |> Header.s_root t.header;
    CaliforniaCache.clear t.cache

  let close t =
    flush t;
    Unix.close t.fd

  let iter t func =
    let max_dead = List.fold_left max 0 t.dead_pages in
    let deads = Array.make (max_dead + 1) false in
    List.iter (fun i -> deads.(i) <- true) t.dead_pages;
    for i = 1 to t.n_pages - 1 do
      if i > max_dead || not deads.(i) then load t i |> func i;
      if i mod 10 = 0 then release t
    done

  let reroot t address =
    ignore CaliforniaCache.update_filter;
    address |> Common.Address.to_t |> Header.s_root t.header;
    flush_header t;
    check_height t;
    let tree_height = address |> load t |> Page.kind |> Common.Kind.to_depth in
    (* only cache the top cache_height part of the tree *)
    if tree_height > cache_height then
      CaliforniaCache.update_filter t.cache ~filter:(fun content ->
          let depth = Page._kind content |> Common.Kind.to_depth in
          if depth = 0 then `Volatile
          else if tree_height - depth <= cache_height then `California
          else if tree_height - depth = cache_height + 1 then `Lru
          else `Volatile)

  let pp_header ppf t =
    Fmt.pf ppf "@[<v 2>Header:@;%a@]@;@[Deallocated pages:@;%a@]" Header.pp t.header
      Fmt.(list int)
      t.dead_pages

  module Private = struct
    let dir t = t.dir

    let cache_size t = (t.cache |> Obj.repr |> Obj.reachable_words) * Sys.word_size / 8

    let write (t : t) s =
      let l = String.length s in
      let write_size = Unix.write_substring t.fd s 0 l in
      assert (write_size = l)

    let init_migration (t : t) = Unix.(lseek t.fd (root t * Params.page_sz) SEEK_SET) |> ignore

    let end_migration t n root =
      t.n_pages <- n;
      reroot t root
  end
end
OCaml

Innovation. Community. Security.