]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/doc/design.lyx
c57ae2a40651f84b4efaa89e04060374bc5b0871
[ccan] / ccan / tdb2 / doc / design.lyx
1 #LyX 1.6.7 created this file. For more info see http://www.lyx.org/
2 \lyxformat 345
3 \begin_document
4 \begin_header
5 \textclass article
6 \use_default_options true
7 \language english
8 \inputencoding auto
9 \font_roman default
10 \font_sans default
11 \font_typewriter default
12 \font_default_family default
13 \font_sc false
14 \font_osf false
15 \font_sf_scale 100
16 \font_tt_scale 100
17
18 \graphics default
19 \paperfontsize default
20 \use_hyperref false
21 \papersize default
22 \use_geometry false
23 \use_amsmath 1
24 \use_esint 1
25 \cite_engine basic
26 \use_bibtopic false
27 \paperorientation portrait
28 \secnumdepth 3
29 \tocdepth 3
30 \paragraph_separation indent
31 \defskip medskip
32 \quotes_language english
33 \papercolumns 1
34 \papersides 1
35 \paperpagestyle default
36 \tracking_changes true
37 \output_changes true
38 \author "Rusty Russell,,," 
39 \author "" 
40 \end_header
41
42 \begin_body
43
44 \begin_layout Title
45 TDB2: A Redesigning The Trivial DataBase
46 \end_layout
47
48 \begin_layout Author
49 Rusty Russell, IBM Corporation
50 \end_layout
51
52 \begin_layout Date
53 1-December-2010
54 \end_layout
55
56 \begin_layout Abstract
57 The Trivial DataBase on-disk format is 32 bits; with usage cases heading
58  towards the 4G limit, that must change.
59  This required breakage provides an opportunity to revisit TDB's other design
60  decisions and reassess them.
61 \end_layout
62
63 \begin_layout Section
64 Introduction
65 \end_layout
66
67 \begin_layout Standard
68 The Trivial DataBase was originally written by Andrew Tridgell as a simple
69  key/data pair storage system with the same API as dbm, but allowing multiple
70  readers and writers while being small enough (< 1000 lines of C) to include
71  in SAMBA.
72  The simple design created in 1999 has proven surprisingly robust and performant
73 , used in Samba versions 3 and 4 as well as numerous other projects.
74  Its useful life was greatly increased by the (backwards-compatible!) addition
75  of transaction support in 2005.
76 \end_layout
77
78 \begin_layout Standard
79 The wider variety and greater demands of TDB-using code has lead to some
80  organic growth of the API, as well as some compromises on the implementation.
81  None of these, by themselves, are seen as show-stoppers, but the cumulative
82  effect is to a loss of elegance over the initial, simple TDB implementation.
83  Here is a table of the approximate number of lines of implementation code
84  and number of API functions at the end of each year:
85 \end_layout
86
87 \begin_layout Standard
88 \begin_inset Tabular
89 <lyxtabular version="3" rows="12" columns="3">
90 <features>
91 <column alignment="center" valignment="top" width="0">
92 <column alignment="center" valignment="top" width="0">
93 <column alignment="center" valignment="top" width="0">
94 <row>
95 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
96 \begin_inset Text
97
98 \begin_layout Plain Layout
99 Year End
100 \end_layout
101
102 \end_inset
103 </cell>
104 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
105 \begin_inset Text
106
107 \begin_layout Plain Layout
108 API Functions
109 \end_layout
110
111 \end_inset
112 </cell>
113 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
114 \begin_inset Text
115
116 \begin_layout Plain Layout
117 Lines of C Code Implementation
118 \end_layout
119
120 \end_inset
121 </cell>
122 </row>
123 <row>
124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
125 \begin_inset Text
126
127 \begin_layout Plain Layout
128 1999
129 \end_layout
130
131 \end_inset
132 </cell>
133 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
134 \begin_inset Text
135
136 \begin_layout Plain Layout
137 13
138 \end_layout
139
140 \end_inset
141 </cell>
142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
143 \begin_inset Text
144
145 \begin_layout Plain Layout
146 1195
147 \end_layout
148
149 \end_inset
150 </cell>
151 </row>
152 <row>
153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
154 \begin_inset Text
155
156 \begin_layout Plain Layout
157 2000
158 \end_layout
159
160 \end_inset
161 </cell>
162 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
163 \begin_inset Text
164
165 \begin_layout Plain Layout
166 24
167 \end_layout
168
169 \end_inset
170 </cell>
171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
172 \begin_inset Text
173
174 \begin_layout Plain Layout
175 1725
176 \end_layout
177
178 \end_inset
179 </cell>
180 </row>
181 <row>
182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
183 \begin_inset Text
184
185 \begin_layout Plain Layout
186 2001
187 \end_layout
188
189 \end_inset
190 </cell>
191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
192 \begin_inset Text
193
194 \begin_layout Plain Layout
195 32
196 \end_layout
197
198 \end_inset
199 </cell>
200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
201 \begin_inset Text
202
203 \begin_layout Plain Layout
204 2228
205 \end_layout
206
207 \end_inset
208 </cell>
209 </row>
210 <row>
211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
212 \begin_inset Text
213
214 \begin_layout Plain Layout
215 2002
216 \end_layout
217
218 \end_inset
219 </cell>
220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
221 \begin_inset Text
222
223 \begin_layout Plain Layout
224 35
225 \end_layout
226
227 \end_inset
228 </cell>
229 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
230 \begin_inset Text
231
232 \begin_layout Plain Layout
233 2481
234 \end_layout
235
236 \end_inset
237 </cell>
238 </row>
239 <row>
240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 \begin_inset Text
242
243 \begin_layout Plain Layout
244 2003
245 \end_layout
246
247 \end_inset
248 </cell>
249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
250 \begin_inset Text
251
252 \begin_layout Plain Layout
253 35
254 \end_layout
255
256 \end_inset
257 </cell>
258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 \begin_inset Text
260
261 \begin_layout Plain Layout
262 2552
263 \end_layout
264
265 \end_inset
266 </cell>
267 </row>
268 <row>
269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
270 \begin_inset Text
271
272 \begin_layout Plain Layout
273 2004
274 \end_layout
275
276 \end_inset
277 </cell>
278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
279 \begin_inset Text
280
281 \begin_layout Plain Layout
282 40
283 \end_layout
284
285 \end_inset
286 </cell>
287 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
288 \begin_inset Text
289
290 \begin_layout Plain Layout
291 2584
292 \end_layout
293
294 \end_inset
295 </cell>
296 </row>
297 <row>
298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
299 \begin_inset Text
300
301 \begin_layout Plain Layout
302 2005
303 \end_layout
304
305 \end_inset
306 </cell>
307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
308 \begin_inset Text
309
310 \begin_layout Plain Layout
311 38
312 \end_layout
313
314 \end_inset
315 </cell>
316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
317 \begin_inset Text
318
319 \begin_layout Plain Layout
320 2647
321 \end_layout
322
323 \end_inset
324 </cell>
325 </row>
326 <row>
327 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
328 \begin_inset Text
329
330 \begin_layout Plain Layout
331 2006
332 \end_layout
333
334 \end_inset
335 </cell>
336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
337 \begin_inset Text
338
339 \begin_layout Plain Layout
340 52
341 \end_layout
342
343 \end_inset
344 </cell>
345 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
346 \begin_inset Text
347
348 \begin_layout Plain Layout
349 3754
350 \end_layout
351
352 \end_inset
353 </cell>
354 </row>
355 <row>
356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
357 \begin_inset Text
358
359 \begin_layout Plain Layout
360 2007
361 \end_layout
362
363 \end_inset
364 </cell>
365 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
366 \begin_inset Text
367
368 \begin_layout Plain Layout
369 66
370 \end_layout
371
372 \end_inset
373 </cell>
374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
375 \begin_inset Text
376
377 \begin_layout Plain Layout
378 4398
379 \end_layout
380
381 \end_inset
382 </cell>
383 </row>
384 <row>
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
386 \begin_inset Text
387
388 \begin_layout Plain Layout
389 2008
390 \end_layout
391
392 \end_inset
393 </cell>
394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
395 \begin_inset Text
396
397 \begin_layout Plain Layout
398 71
399 \end_layout
400
401 \end_inset
402 </cell>
403 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
404 \begin_inset Text
405
406 \begin_layout Plain Layout
407 4768
408 \end_layout
409
410 \end_inset
411 </cell>
412 </row>
413 <row>
414 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
415 \begin_inset Text
416
417 \begin_layout Plain Layout
418 2009
419 \end_layout
420
421 \end_inset
422 </cell>
423 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
424 \begin_inset Text
425
426 \begin_layout Plain Layout
427 73
428 \end_layout
429
430 \end_inset
431 </cell>
432 <cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
433 \begin_inset Text
434
435 \begin_layout Plain Layout
436 5715
437 \end_layout
438
439 \end_inset
440 </cell>
441 </row>
442 </lyxtabular>
443
444 \end_inset
445
446
447 \end_layout
448
449 \begin_layout Standard
450 This review is an attempt to catalog and address all the known issues with
451  TDB and create solutions which address the problems without significantly
452  increasing complexity; all involved are far too aware of the dangers of
453  second system syndrome in rewriting a successful project like this.
454 \end_layout
455
456 \begin_layout Section
457 API Issues
458 \end_layout
459
460 \begin_layout Subsection
461 tdb_open_ex Is Not Expandable
462 \end_layout
463
464 \begin_layout Standard
465 The tdb_open() call was expanded to tdb_open_ex(), which added an optional
466  hashing function and an optional logging function argument.
467  Additional arguments to open would require the introduction of a tdb_open_ex2
468  call etc.
469 \end_layout
470
471 \begin_layout Subsubsection
472 Proposed Solution
473 \begin_inset CommandInset label
474 LatexCommand label
475 name "attributes"
476
477 \end_inset
478
479
480 \end_layout
481
482 \begin_layout Standard
483 tdb_open() will take a linked-list of attributes:
484 \end_layout
485
486 \begin_layout LyX-Code
487 enum tdb_attribute {
488 \end_layout
489
490 \begin_layout LyX-Code
491     TDB_ATTRIBUTE_LOG = 0,
492 \end_layout
493
494 \begin_layout LyX-Code
495     TDB_ATTRIBUTE_HASH = 1
496 \end_layout
497
498 \begin_layout LyX-Code
499 };
500 \end_layout
501
502 \begin_layout LyX-Code
503 struct tdb_attribute_base {
504 \end_layout
505
506 \begin_layout LyX-Code
507     enum tdb_attribute attr;
508 \end_layout
509
510 \begin_layout LyX-Code
511     union tdb_attribute *next;
512 \end_layout
513
514 \begin_layout LyX-Code
515 };
516 \end_layout
517
518 \begin_layout LyX-Code
519 struct tdb_attribute_log {
520 \end_layout
521
522 \begin_layout LyX-Code
523     struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
524 \end_layout
525
526 \begin_layout LyX-Code
527     tdb_log_func log_fn;
528 \end_layout
529
530 \begin_layout LyX-Code
531     void *log_private;
532 \end_layout
533
534 \begin_layout LyX-Code
535 };
536 \end_layout
537
538 \begin_layout LyX-Code
539 struct tdb_attribute_hash {
540 \end_layout
541
542 \begin_layout LyX-Code
543     struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
544 \end_layout
545
546 \begin_layout LyX-Code
547     tdb_hash_func hash_fn;
548 \end_layout
549
550 \begin_layout LyX-Code
551     void *hash_private;
552 \end_layout
553
554 \begin_layout LyX-Code
555 };
556 \end_layout
557
558 \begin_layout LyX-Code
559 union tdb_attribute {
560 \end_layout
561
562 \begin_layout LyX-Code
563     struct tdb_attribute_base base;
564 \end_layout
565
566 \begin_layout LyX-Code
567     struct tdb_attribute_log log;
568 \end_layout
569
570 \begin_layout LyX-Code
571     struct tdb_attribute_hash hash;
572 \end_layout
573
574 \begin_layout LyX-Code
575 };
576 \end_layout
577
578 \begin_layout Standard
579 This allows future attributes to be added, even if this expands the size
580  of the union.
581 \end_layout
582
583 \begin_layout Subsubsection
584 Status
585 \end_layout
586
587 \begin_layout Standard
588 Complete.
589 \end_layout
590
591 \begin_layout Subsection
592 tdb_traverse Makes Impossible Guarantees
593 \end_layout
594
595 \begin_layout Standard
596 tdb_traverse (and tdb_firstkey/tdb_nextkey) predate transactions, and it
597  was thought that it was important to guarantee that all records which exist
598  at the start and end of the traversal would be included, and no record
599  would be included twice.
600 \end_layout
601
602 \begin_layout Standard
603 This adds complexity (see
604 \begin_inset CommandInset ref
605 LatexCommand ref
606 reference "Reliable-Traversal-Adds"
607
608 \end_inset
609
610 ) and does not work anyway for records which are altered (in particular,
611  those which are expanded may be effectively deleted and re-added behind
612  the traversal).
613 \end_layout
614
615 \begin_layout Subsubsection
616 \begin_inset CommandInset label
617 LatexCommand label
618 name "traverse-Proposed-Solution"
619
620 \end_inset
621
622 Proposed Solution
623 \end_layout
624
625 \begin_layout Standard
626 Abandon the guarantee.
627  You will see every record if no changes occur during your traversal, otherwise
628  you will see some subset.
629  You can prevent changes by using a transaction or the locking API.
630 \end_layout
631
632 \begin_layout Subsubsection
633 Status
634 \end_layout
635
636 \begin_layout Standard
637 Complete.
638  Delete-during-traverse will still delete every record, too (assuming no
639  other changes).
640 \end_layout
641
642 \begin_layout Subsection
643 Nesting of Transactions Is Fraught
644 \end_layout
645
646 \begin_layout Standard
647 TDB has alternated between allowing nested transactions and not allowing
648  them.
649  Various paths in the Samba codebase assume that transactions will nest,
650  and in a sense they can: the operation is only committed to disk when the
651  outer transaction is committed.
652  There are two problems, however:
653 \end_layout
654
655 \begin_layout Enumerate
656 Canceling the inner transaction will cause the outer transaction commit
657  to fail, and will not undo any operations since the inner transaction began.
658  This problem is soluble with some additional internal code.
659 \end_layout
660
661 \begin_layout Enumerate
662 An inner transaction commit can be cancelled by the outer transaction.
663  This is desirable in the way which Samba's database initialization code
664  uses transactions, but could be a surprise to any users expecting a successful
665  transaction commit to expose changes to others.
666 \end_layout
667
668 \begin_layout Standard
669 The current solution is to specify the behavior at tdb_open(), with the
670  default currently that nested transactions are allowed.
671  This flag can also be changed at runtime.
672 \end_layout
673
674 \begin_layout Subsubsection
675 Proposed Solution
676 \end_layout
677
678 \begin_layout Standard
679 Given the usage patterns, it seems that the 
680 \begin_inset Quotes eld
681 \end_inset
682
683 least-surprise
684 \begin_inset Quotes erd
685 \end_inset
686
687  behavior of disallowing nested transactions should become the default.
688  Additionally, it seems the outer transaction is the only code which knows
689  whether inner transactions should be allowed, so a flag to indicate this
690  could be added to tdb_transaction_start.
691  However, this behavior can be simulated with a wrapper which uses tdb_add_flags
692 () and tdb_remove_flags(), so the API should not be expanded for this relatively
693 -obscure case.
694 \end_layout
695
696 \begin_layout Subsubsection
697 Status
698 \end_layout
699
700 \begin_layout Standard
701
702 \change_deleted 0 1298979572
703 Incomplete; nesting flag is still defined as per tdb1.
704 \change_inserted 0 1298979584
705 Complete; the nesting flag has been removed.
706 \change_unchanged
707
708 \end_layout
709
710 \begin_layout Subsection
711 Incorrect Hash Function is Not Detected
712 \end_layout
713
714 \begin_layout Standard
715 tdb_open_ex() allows the calling code to specify a different hash function
716  to use, but does not check that all other processes accessing this tdb
717  are using the same hash function.
718  The result is that records are missing from tdb_fetch().
719 \end_layout
720
721 \begin_layout Subsubsection
722 Proposed Solution
723 \end_layout
724
725 \begin_layout Standard
726 The header should contain an example hash result (eg.
727  the hash of 0xdeadbeef), and tdb_open_ex() should check that the given
728  hash function produces the same answer, or fail the tdb_open call.
729 \end_layout
730
731 \begin_layout Subsubsection
732 Status
733 \end_layout
734
735 \begin_layout Standard
736 Complete.
737 \end_layout
738
739 \begin_layout Subsection
740 tdb_set_max_dead/TDB_VOLATILE Expose Implementation
741 \end_layout
742
743 \begin_layout Standard
744 In response to scalability issues with the free list (
745 \begin_inset CommandInset ref
746 LatexCommand ref
747 reference "TDB-Freelist-Is"
748
749 \end_inset
750
751 ) two API workarounds have been incorporated in TDB: tdb_set_max_dead()
752  and the TDB_VOLATILE flag to tdb_open.
753  The latter actually calls the former with an argument of 
754 \begin_inset Quotes eld
755 \end_inset
756
757 5
758 \begin_inset Quotes erd
759 \end_inset
760
761 .
762 \end_layout
763
764 \begin_layout Standard
765 This code allows deleted records to accumulate without putting them in the
766  free list.
767  On delete we iterate through each chain and free them in a batch if there
768  are more than max_dead entries.
769  These are never otherwise recycled except as a side-effect of a tdb_repack.
770 \end_layout
771
772 \begin_layout Subsubsection
773 Proposed Solution
774 \end_layout
775
776 \begin_layout Standard
777 With the scalability problems of the freelist solved, this API can be removed.
778  The TDB_VOLATILE flag may still be useful as a hint that store and delete
779  of records will be at least as common as fetch in order to allow some internal
780  tuning, but initially will become a no-op.
781 \end_layout
782
783 \begin_layout Subsubsection
784 Status
785 \end_layout
786
787 \begin_layout Standard
788
789 \change_deleted 0 1300360559
790 Incomplete.
791  TDB_VOLATILE still defined, but implementation should fail on unknown flags
792  to be future-proof.
793 \change_inserted 0 1300360588
794 Complete.
795  Unknown flags cause tdb_open() to fail as well, so they can be detected
796  at runtime.
797 \end_layout
798
799 \begin_layout Subsection
800 \begin_inset CommandInset label
801 LatexCommand label
802 name "TDB-Files-Cannot"
803
804 \end_inset
805
806 TDB Files Cannot Be Opened Multiple Times In The Same Process
807 \end_layout
808
809 \begin_layout Standard
810 No process can open the same TDB twice; we check and disallow it.
811  This is an unfortunate side-effect of fcntl locks, which operate on a per-file
812  rather than per-file-descriptor basis, and do not nest.
813  Thus, closing any file descriptor on a file clears all the locks obtained
814  by this process, even if they were placed using a different file descriptor!
815 \end_layout
816
817 \begin_layout Standard
818 Note that even if this were solved, deadlock could occur if operations were
819  nested: this is a more manageable programming error in most cases.
820 \end_layout
821
822 \begin_layout Subsubsection
823 Proposed Solution
824 \end_layout
825
826 \begin_layout Standard
827 We could lobby POSIX to fix the perverse rules, or at least lobby Linux
828  to violate them so that the most common implementation does not have this
829  restriction.
830  This would be a generally good idea for other fcntl lock users.
831 \end_layout
832
833 \begin_layout Standard
834 Samba uses a wrapper which hands out the same tdb_context to multiple callers
835  if this happens, and does simple reference counting.
836  We should do this inside the tdb library, which already emulates lock nesting
837  internally; it would need to recognize when deadlock occurs within a single
838  process.
839  This would create a new failure mode for tdb operations (while we currently
840  handle locking failures, they are impossible in normal use and a process
841  encountering them can do little but give up).
842 \end_layout
843
844 \begin_layout Standard
845 I do not see benefit in an additional tdb_open flag to indicate whether
846  re-opening is allowed, as though there may be some benefit to adding a
847  call to detect when a tdb_context is shared, to allow other to create such
848  an API.
849 \end_layout
850
851 \begin_layout Subsubsection
852 Status
853 \end_layout
854
855 \begin_layout Standard
856
857 \change_deleted 0 1300360823
858 Incomplete.
859 \change_inserted 0 1300360824
860 Complete.
861 \change_unchanged
862
863 \end_layout
864
865 \begin_layout Subsection
866 TDB API Is Not POSIX Thread-safe
867 \end_layout
868
869 \begin_layout Standard
870 The TDB API uses an error code which can be queried after an operation to
871  determine what went wrong.
872  This programming model does not work with threads, unless specific additional
873  guarantees are given by the implementation.
874  In addition, even otherwise-independent threads cannot open the same TDB
875  (as in 
876 \begin_inset CommandInset ref
877 LatexCommand ref
878 reference "TDB-Files-Cannot"
879
880 \end_inset
881
882 ).
883 \end_layout
884
885 \begin_layout Subsubsection
886 Proposed Solution
887 \end_layout
888
889 \begin_layout Standard
890 Reachitecting the API to include a tdb_errcode pointer would be a great
891  deal of churn
892 \change_inserted 0 1298979557
893 , but fortunately most functions return 0 on success and -1 on error: we
894  can change these to return 0 on success and a negative error code on error,
895  and the API remains similar to previous.
896  The tdb_fetch, tdb_firstkey and tdb_nextkey functions need to take a TDB_DATA
897  pointer and return an error code.
898  It is also simpler to have tdb_nextkey replace its key argument in place,
899  freeing up any old .dptr.
900 \end_layout
901
902 \begin_layout Standard
903
904 \change_deleted 0 1298979438
905 ; we are better to guarantee that the tdb_errcode is per-thread so the current
906  programming model can be maintained.
907 \end_layout
908
909 \begin_layout Standard
910
911 \change_deleted 0 1298979438
912 This requires dynamic per-thread allocations, which is awkward with POSIX
913  threads (pthread_key_create space is limited and we cannot simply allocate
914  a key for every TDB).
915 \change_unchanged
916
917 \end_layout
918
919 \begin_layout Standard
920 Internal locking is required to make sure that fcntl locks do not overlap
921  between threads, and also that the global list of tdbs is maintained.
922 \end_layout
923
924 \begin_layout Standard
925 The aim is that building tdb with -DTDB_PTHREAD will result in a pthread-safe
926  version of the library, and otherwise no overhead will exist.
927  Alternatively, a hooking mechanism similar to that proposed for 
928 \begin_inset CommandInset ref
929 LatexCommand ref
930 reference "Proposed-Solution-locking-hook"
931
932 \end_inset
933
934  could be used to enable pthread locking at runtime.
935 \end_layout
936
937 \begin_layout Subsubsection
938 Status
939 \end_layout
940
941 \begin_layout Standard
942 Incomplete
943 \change_inserted 0 1298979681
944 ; API has been changed but thread safety has not been implemented.
945 \change_deleted 0 1298979669
946 .
947 \change_unchanged
948
949 \end_layout
950
951 \begin_layout Subsection
952 *_nonblock Functions And *_mark Functions Expose Implementation
953 \end_layout
954
955 \begin_layout Standard
956 CTDB
957 \begin_inset Foot
958 status collapsed
959
960 \begin_layout Plain Layout
961 Clustered TDB, see http://ctdb.samba.org
962 \end_layout
963
964 \end_inset
965
966  wishes to operate on TDB in a non-blocking manner.
967  This is currently done as follows:
968 \end_layout
969
970 \begin_layout Enumerate
971 Call the _nonblock variant of an API function (eg.
972  tdb_lockall_nonblock).
973  If this fails:
974 \end_layout
975
976 \begin_layout Enumerate
977 Fork a child process, and wait for it to call the normal variant (eg.
978  tdb_lockall).
979 \end_layout
980
981 \begin_layout Enumerate
982 If the child succeeds, call the _mark variant to indicate we already have
983  the locks (eg.
984  tdb_lockall_mark).
985 \end_layout
986
987 \begin_layout Enumerate
988 Upon completion, tell the child to release the locks (eg.
989  tdb_unlockall).
990 \end_layout
991
992 \begin_layout Enumerate
993 Indicate to tdb that it should consider the locks removed (eg.
994  tdb_unlockall_mark).
995 \end_layout
996
997 \begin_layout Standard
998 There are several issues with this approach.
999  Firstly, adding two new variants of each function clutters the API for
1000  an obscure use, and so not all functions have three variants.
1001  Secondly, it assumes that all paths of the functions ask for the same locks,
1002  otherwise the parent process will have to get a lock which the child doesn't
1003  have under some circumstances.
1004  I don't believe this is currently the case, but it constrains the implementatio
1005 n.
1006  
1007 \end_layout
1008
1009 \begin_layout Subsubsection
1010 \begin_inset CommandInset label
1011 LatexCommand label
1012 name "Proposed-Solution-locking-hook"
1013
1014 \end_inset
1015
1016 Proposed Solution
1017 \end_layout
1018
1019 \begin_layout Standard
1020 Implement a hook for locking methods, so that the caller can control the
1021  calls to create and remove fcntl locks.
1022  In this scenario, ctdbd would operate as follows:
1023 \end_layout
1024
1025 \begin_layout Enumerate
1026 Call the normal API function, eg tdb_lockall().
1027 \end_layout
1028
1029 \begin_layout Enumerate
1030 When the lock callback comes in, check if the child has the lock.
1031  Initially, this is always false.
1032  If so, return 0.
1033  Otherwise, try to obtain it in non-blocking mode.
1034  If that fails, return EWOULDBLOCK.
1035 \end_layout
1036
1037 \begin_layout Enumerate
1038 Release locks in the unlock callback as normal.
1039 \end_layout
1040
1041 \begin_layout Enumerate
1042 If tdb_lockall() fails, see if we recorded a lock failure; if so, call the
1043  child to repeat the operation.
1044 \end_layout
1045
1046 \begin_layout Enumerate
1047 The child records what locks it obtains, and returns that information to
1048  the parent.
1049 \end_layout
1050
1051 \begin_layout Enumerate
1052 When the child has succeeded, goto 1.
1053 \end_layout
1054
1055 \begin_layout Standard
1056 This is flexible enough to handle any potential locking scenario, even when
1057  lock requirements change.
1058  It can be optimized so that the parent does not release locks, just tells
1059  the child which locks it doesn't need to obtain.
1060 \end_layout
1061
1062 \begin_layout Standard
1063 It also keeps the complexity out of the API, and in ctdbd where it is needed.
1064 \end_layout
1065
1066 \begin_layout Subsubsection
1067 Status
1068 \end_layout
1069
1070 \begin_layout Standard
1071 Incomplete.
1072 \end_layout
1073
1074 \begin_layout Subsection
1075 tdb_chainlock Functions Expose Implementation
1076 \end_layout
1077
1078 \begin_layout Standard
1079 tdb_chainlock locks some number of records, including the record indicated
1080  by the given key.
1081  This gave atomicity guarantees; no-one can start a transaction, alter,
1082  read or delete that key while the lock is held.
1083 \end_layout
1084
1085 \begin_layout Standard
1086 It also makes the same guarantee for any other key in the chain, which is
1087  an internal implementation detail and potentially a cause for deadlock.
1088 \end_layout
1089
1090 \begin_layout Subsubsection
1091 Proposed Solution
1092 \end_layout
1093
1094 \begin_layout Standard
1095 None.
1096  It would be nice to have an explicit single entry lock which effected no
1097  other keys.
1098  Unfortunately, this won't work for an entry which doesn't exist.
1099  Thus while chainlock may be implemented more efficiently for the existing
1100  case, it will still have overlap issues with the non-existing case.
1101  So it is best to keep the current (lack of) guarantee about which records
1102  will be effected to avoid constraining our implementation.
1103 \end_layout
1104
1105 \begin_layout Subsection
1106 Signal Handling is Not Race-Free
1107 \end_layout
1108
1109 \begin_layout Standard
1110 The tdb_setalarm_sigptr() call allows the caller's signal handler to indicate
1111  that the tdb locking code should return with a failure, rather than trying
1112  again when a signal is received (and errno == EAGAIN).
1113  This is usually used to implement timeouts.
1114 \end_layout
1115
1116 \begin_layout Standard
1117 Unfortunately, this does not work in the case where the signal is received
1118  before the tdb code enters the fcntl() call to place the lock: the code
1119  will sleep within the fcntl() code, unaware that the signal wants it to
1120  exit.
1121  In the case of long timeouts, this does not happen in practice.
1122 \end_layout
1123
1124 \begin_layout Subsubsection
1125 Proposed Solution
1126 \end_layout
1127
1128 \begin_layout Standard
1129 The locking hooks proposed in
1130 \begin_inset CommandInset ref
1131 LatexCommand ref
1132 reference "Proposed-Solution-locking-hook"
1133
1134 \end_inset
1135
1136  would allow the user to decide on whether to fail the lock acquisition
1137  on a signal.
1138  This allows the caller to choose their own compromise: they could narrow
1139  the race by checking immediately before the fcntl call.
1140 \begin_inset Foot
1141 status collapsed
1142
1143 \begin_layout Plain Layout
1144 It may be possible to make this race-free in some implementations by having
1145  the signal handler alter the struct flock to make it invalid.
1146  This will cause the fcntl() lock call to fail with EINVAL if the signal
1147  occurs before the kernel is entered, otherwise EAGAIN.
1148 \end_layout
1149
1150 \end_inset
1151
1152
1153 \end_layout
1154
1155 \begin_layout Subsubsection
1156 Status
1157 \end_layout
1158
1159 \begin_layout Standard
1160 Incomplete.
1161 \end_layout
1162
1163 \begin_layout Subsection
1164 The API Uses Gratuitous Typedefs, Capitals
1165 \end_layout
1166
1167 \begin_layout Standard
1168 typedefs are useful for providing source compatibility when types can differ
1169  across implementations, or arguably in the case of function pointer definitions
1170  which are hard for humans to parse.
1171  Otherwise it is simply obfuscation and pollutes the namespace.
1172 \end_layout
1173
1174 \begin_layout Standard
1175 Capitalization is usually reserved for compile-time constants and macros.
1176 \end_layout
1177
1178 \begin_layout Description
1179 TDB_CONTEXT There is no reason to use this over 'struct tdb_context'; the
1180  definition isn't visible to the API user anyway.
1181 \end_layout
1182
1183 \begin_layout Description
1184 TDB_DATA There is no reason to use this over struct TDB_DATA; the struct
1185  needs to be understood by the API user.
1186 \end_layout
1187
1188 \begin_layout Description
1189 struct
1190 \begin_inset space ~
1191 \end_inset
1192
1193 TDB_DATA This would normally be called 'struct tdb_data'.
1194 \end_layout
1195
1196 \begin_layout Description
1197 enum
1198 \begin_inset space ~
1199 \end_inset
1200
1201 TDB_ERROR Similarly, this would normally be enum tdb_error.
1202 \end_layout
1203
1204 \begin_layout Subsubsection
1205 Proposed Solution
1206 \end_layout
1207
1208 \begin_layout Standard
1209 None.
1210  Introducing lower case variants would please pedants like myself, but if
1211  it were done the existing ones should be kept.
1212  There is little point forcing a purely cosmetic change upon tdb users.
1213 \end_layout
1214
1215 \begin_layout Subsection
1216 \begin_inset CommandInset label
1217 LatexCommand label
1218 name "tdb_log_func-Doesnt-Take"
1219
1220 \end_inset
1221
1222 tdb_log_func Doesn't Take The Private Pointer
1223 \end_layout
1224
1225 \begin_layout Standard
1226 For API compatibility reasons, the logging function needs to call tdb_get_loggin
1227 g_private() to retrieve the pointer registered by the tdb_open_ex for logging.
1228 \end_layout
1229
1230 \begin_layout Subsubsection
1231 Proposed Solution
1232 \end_layout
1233
1234 \begin_layout Standard
1235 It should simply take an extra argument, since we are prepared to break
1236  the API/ABI.
1237 \end_layout
1238
1239 \begin_layout Subsubsection
1240 Status
1241 \end_layout
1242
1243 \begin_layout Standard
1244 Complete.
1245 \end_layout
1246
1247 \begin_layout Subsection
1248 Various Callback Functions Are Not Typesafe
1249 \end_layout
1250
1251 \begin_layout Standard
1252 The callback functions in tdb_set_logging_function (after 
1253 \begin_inset CommandInset ref
1254 LatexCommand ref
1255 reference "tdb_log_func-Doesnt-Take"
1256
1257 \end_inset
1258
1259  is resolved), tdb_parse_record, tdb_traverse, tdb_traverse_read and tdb_check
1260  all take void * and must internally convert it to the argument type they
1261  were expecting.
1262 \end_layout
1263
1264 \begin_layout Standard
1265 If this type changes, the compiler will not produce warnings on the callers,
1266  since it only sees void *.
1267 \end_layout
1268
1269 \begin_layout Subsubsection
1270 Proposed Solution
1271 \end_layout
1272
1273 \begin_layout Standard
1274 With careful use of macros, we can create callback functions which give
1275  a warning when used on gcc and the types of the callback and its private
1276  argument differ.
1277  Unsupported compilers will not give a warning, which is no worse than now.
1278  In addition, the callbacks become clearer, as they need not use void *
1279  for their parameter.
1280 \end_layout
1281
1282 \begin_layout Standard
1283 See CCAN's typesafe_cb module at http://ccan.ozlabs.org/info/typesafe_cb.html
1284 \end_layout
1285
1286 \begin_layout Subsubsection
1287 Status
1288 \end_layout
1289
1290 \begin_layout Standard
1291
1292 \change_deleted 0 1300360712
1293 Incomplete.
1294 \change_inserted 0 1300360716
1295 Complete.
1296 \change_unchanged
1297
1298 \end_layout
1299
1300 \begin_layout Subsection
1301 TDB_CLEAR_IF_FIRST Must Be Specified On All Opens, tdb_reopen_all Problematic
1302 \end_layout
1303
1304 \begin_layout Standard
1305 The TDB_CLEAR_IF_FIRST flag to tdb_open indicates that the TDB file should
1306  be cleared if the caller discovers it is the only process with the TDB
1307  open.
1308  However, if any caller does not specify TDB_CLEAR_IF_FIRST it will not
1309  be detected, so will have the TDB erased underneath them (usually resulting
1310  in a crash).
1311 \end_layout
1312
1313 \begin_layout Standard
1314 There is a similar issue on fork(); if the parent exits (or otherwise closes
1315  the tdb) before the child calls tdb_reopen_all() to establish the lock
1316  used to indicate the TDB is opened by someone, a TDB_CLEAR_IF_FIRST opener
1317  at that moment will believe it alone has opened the TDB and will erase
1318  it.
1319 \end_layout
1320
1321 \begin_layout Subsubsection
1322 Proposed Solution
1323 \end_layout
1324
1325 \begin_layout Standard
1326 Remove TDB_CLEAR_IF_FIRST.
1327  Other workarounds are possible, but see 
1328 \begin_inset CommandInset ref
1329 LatexCommand ref
1330 reference "TDB_CLEAR_IF_FIRST-Imposes-Performance"
1331
1332 \end_inset
1333
1334 .
1335 \end_layout
1336
1337 \begin_layout Subsubsection
1338 Status
1339 \end_layout
1340
1341 \begin_layout Standard
1342
1343 \change_deleted 0 1298979699
1344 Incomplete, TDB_CLEAR_IF_FIRST still defined, but not implemented.
1345 \change_inserted 0 1298979700
1346 Complete.
1347 \change_unchanged
1348
1349 \end_layout
1350
1351 \begin_layout Subsection
1352 Extending The Header Is Difficult
1353 \end_layout
1354
1355 \begin_layout Standard
1356 We have reserved (zeroed) words in the TDB header, which can be used for
1357  future features.
1358  If the future features are compulsory, the version number must be updated
1359  to prevent old code from accessing the database.
1360  But if the future feature is optional, we have no way of telling if older
1361  code is accessing the database or not.
1362 \end_layout
1363
1364 \begin_layout Subsubsection
1365 Proposed Solution
1366 \end_layout
1367
1368 \begin_layout Standard
1369 The header should contain a 
1370 \begin_inset Quotes eld
1371 \end_inset
1372
1373 format variant
1374 \begin_inset Quotes erd
1375 \end_inset
1376
1377  value (64-bit).
1378  This is divided into two 32-bit parts:
1379 \end_layout
1380
1381 \begin_layout Enumerate
1382 The lower part reflects the format variant understood by code accessing
1383  the database.
1384 \end_layout
1385
1386 \begin_layout Enumerate
1387 The upper part reflects the format variant you must understand to write
1388  to the database (otherwise you can only open for reading).
1389 \end_layout
1390
1391 \begin_layout Standard
1392 The latter field can only be written at creation time, the former should
1393  be written under the OPEN_LOCK when opening the database for writing, if
1394  the variant of the code is lower than the current lowest variant.
1395 \end_layout
1396
1397 \begin_layout Standard
1398 This should allow backwards-compatible features to be added, and detection
1399  if older code (which doesn't understand the feature) writes to the database.
1400 \end_layout
1401
1402 \begin_layout Subsubsection
1403 Status
1404 \end_layout
1405
1406 \begin_layout Standard
1407
1408 \change_deleted 0 1300360753
1409 Incomplete.
1410 \change_inserted 0 1300360754
1411 Complete.
1412 \change_unchanged
1413
1414 \end_layout
1415
1416 \begin_layout Subsection
1417 Record Headers Are Not Expandible
1418 \end_layout
1419
1420 \begin_layout Standard
1421 If we later want to add (say) checksums on keys and data, it would require
1422  another format change, which we'd like to avoid.
1423 \end_layout
1424
1425 \begin_layout Subsubsection
1426 Proposed Solution
1427 \end_layout
1428
1429 \begin_layout Standard
1430 We often have extra padding at the tail of a record.
1431  If we ensure that the first byte (if any) of this padding is zero, we will
1432  have a way for future changes to detect code which doesn't understand a
1433  new format: the new code would write (say) a 1 at the tail, and thus if
1434  there is no tail or the first byte is 0, we would know the extension is
1435  not present on that record.
1436 \end_layout
1437
1438 \begin_layout Subsubsection
1439 Status
1440 \end_layout
1441
1442 \begin_layout Standard
1443
1444 \change_deleted 0 1300360766
1445 Incomplete.
1446 \change_inserted 0 1300360767
1447 Complete.
1448 \change_unchanged
1449
1450 \end_layout
1451
1452 \begin_layout Subsection
1453 TDB Does Not Use Talloc
1454 \end_layout
1455
1456 \begin_layout Standard
1457 Many users of TDB (particularly Samba) use the talloc allocator, and thus
1458  have to wrap TDB in a talloc context to use it conveniently.
1459 \end_layout
1460
1461 \begin_layout Subsubsection
1462 Proposed Solution
1463 \end_layout
1464
1465 \begin_layout Standard
1466 The allocation within TDB is not complicated enough to justify the use of
1467  talloc, and I am reluctant to force another (excellent) library on TDB
1468  users.
1469  Nonetheless a compromise is possible.
1470  An attribute (see 
1471 \begin_inset CommandInset ref
1472 LatexCommand ref
1473 reference "attributes"
1474
1475 \end_inset
1476
1477 ) can be added later to tdb_open() to provide an alternate allocation mechanism,
1478  specifically for talloc but usable by any other allocator (which would
1479  ignore the 
1480 \begin_inset Quotes eld
1481 \end_inset
1482
1483 context
1484 \begin_inset Quotes erd
1485 \end_inset
1486
1487  argument).
1488 \end_layout
1489
1490 \begin_layout Standard
1491 This would form a talloc heirarchy as expected, but the caller would still
1492  have to attach a destructor to the tdb context returned from tdb_open to
1493  close it.
1494  All TDB_DATA fields would be children of the tdb_context, and the caller
1495  would still have to manage them (using talloc_free() or talloc_steal()).
1496 \end_layout
1497
1498 \begin_layout Subsubsection
1499 Status
1500 \end_layout
1501
1502 \begin_layout Standard
1503 Deferred.
1504 \end_layout
1505
1506 \begin_layout Section
1507 Performance And Scalability Issues
1508 \end_layout
1509
1510 \begin_layout Subsection
1511 \begin_inset CommandInset label
1512 LatexCommand label
1513 name "TDB_CLEAR_IF_FIRST-Imposes-Performance"
1514
1515 \end_inset
1516
1517 TDB_CLEAR_IF_FIRST Imposes Performance Penalty
1518 \end_layout
1519
1520 \begin_layout Standard
1521 When TDB_CLEAR_IF_FIRST is specified, a 1-byte read lock is placed at offset
1522  4 (aka.
1523  the ACTIVE_LOCK).
1524  While these locks never conflict in normal tdb usage, they do add substantial
1525  overhead for most fcntl lock implementations when the kernel scans to detect
1526  if a lock conflict exists.
1527  This is often a single linked list, making the time to acquire and release
1528  a fcntl lock O(N) where N is the number of processes with the TDB open,
1529  not the number actually doing work.
1530 \end_layout
1531
1532 \begin_layout Standard
1533 In a Samba server it is common to have huge numbers of clients sitting idle,
1534  and thus they have weaned themselves off the TDB_CLEAR_IF_FIRST flag.
1535 \begin_inset Foot
1536 status collapsed
1537
1538 \begin_layout Plain Layout
1539 There is a flag to tdb_reopen_all() which is used for this optimization:
1540  if the parent process will outlive the child, the child does not need the
1541  ACTIVE_LOCK.
1542  This is a workaround for this very performance issue.
1543 \end_layout
1544
1545 \end_inset
1546
1547
1548 \end_layout
1549
1550 \begin_layout Subsubsection
1551 Proposed Solution
1552 \end_layout
1553
1554 \begin_layout Standard
1555 Remove the flag.
1556  It was a neat idea, but even trivial servers tend to know when they are
1557  initializing for the first time and can simply unlink the old tdb at that
1558  point.
1559 \end_layout
1560
1561 \begin_layout Subsubsection
1562 Status
1563 \end_layout
1564
1565 \begin_layout Standard
1566
1567 \change_deleted 0 1298979837
1568 Incomplete; TDB_CLEAR_IF_FIRST still defined, but does nothing.
1569 \change_inserted 0 1298979837
1570 Complete.
1571 \change_unchanged
1572
1573 \end_layout
1574
1575 \begin_layout Subsection
1576 TDB Files Have a 4G Limit
1577 \end_layout
1578
1579 \begin_layout Standard
1580 This seems to be becoming an issue (so much for 
1581 \begin_inset Quotes eld
1582 \end_inset
1583
1584 trivial
1585 \begin_inset Quotes erd
1586 \end_inset
1587
1588 !), particularly for ldb.
1589 \end_layout
1590
1591 \begin_layout Subsubsection
1592 Proposed Solution
1593 \end_layout
1594
1595 \begin_layout Standard
1596 A new, incompatible TDB format which uses 64 bit offsets internally rather
1597  than 32 bit as now.
1598  For simplicity of endian conversion (which TDB does on the fly if required),
1599  all values will be 64 bit on disk.
1600  In practice, some upper bits may be used for other purposes, but at least
1601  56 bits will be available for file offsets.
1602 \end_layout
1603
1604 \begin_layout Standard
1605 tdb_open() will automatically detect the old version, and even create them
1606  if TDB_VERSION6 is specified to tdb_open.
1607 \end_layout
1608
1609 \begin_layout Standard
1610 32 bit processes will still be able to access TDBs larger than 4G (assuming
1611  that their off_t allows them to seek to 64 bits), they will gracefully
1612  fall back as they fail to mmap.
1613  This can happen already with large TDBs.
1614 \end_layout
1615
1616 \begin_layout Standard
1617 Old versions of tdb will fail to open the new TDB files (since 28 August
1618  2009, commit 398d0c29290: prior to that any unrecognized file format would
1619  be erased and initialized as a fresh tdb!)
1620 \end_layout
1621
1622 \begin_layout Subsubsection
1623 Status
1624 \end_layout
1625
1626 \begin_layout Standard
1627 Complete.
1628 \end_layout
1629
1630 \begin_layout Subsection
1631 TDB Records Have a 4G Limit
1632 \end_layout
1633
1634 \begin_layout Standard
1635 This has not been a reported problem, and the API uses size_t which can
1636  be 64 bit on 64 bit platforms.
1637  However, other limits may have made such an issue moot.
1638 \end_layout
1639
1640 \begin_layout Subsubsection
1641 Proposed Solution
1642 \end_layout
1643
1644 \begin_layout Standard
1645 Record sizes will be 64 bit, with an error returned on 32 bit platforms
1646  which try to access such records (the current implementation would return
1647  TDB_ERR_OOM in a similar case).
1648  It seems unlikely that 32 bit keys will be a limitation, so the implementation
1649  may not support this (see 
1650 \begin_inset CommandInset ref
1651 LatexCommand ref
1652 reference "sub:Records-Incur-A"
1653
1654 \end_inset
1655
1656 ).
1657 \end_layout
1658
1659 \begin_layout Subsubsection
1660 Status
1661 \end_layout
1662
1663 \begin_layout Standard
1664 Complete.
1665 \end_layout
1666
1667 \begin_layout Subsection
1668 Hash Size Is Determined At TDB Creation Time
1669 \end_layout
1670
1671 \begin_layout Standard
1672 TDB contains a number of hash chains in the header; the number is specified
1673  at creation time, and defaults to 131.
1674  This is such a bottleneck on large databases (as each hash chain gets quite
1675  long), that LDB uses 10,000 for this hash.
1676  In general it is impossible to know what the 'right' answer is at database
1677  creation time.
1678 \end_layout
1679
1680 \begin_layout Subsubsection
1681 \begin_inset CommandInset label
1682 LatexCommand label
1683 name "sub:Hash-Size-Solution"
1684
1685 \end_inset
1686
1687 Proposed Solution
1688 \end_layout
1689
1690 \begin_layout Standard
1691 After comprehensive performance testing on various scalable hash variants
1692 \begin_inset Foot
1693 status collapsed
1694
1695 \begin_layout Plain Layout
1696 http://rusty.ozlabs.org/?p=89 and http://rusty.ozlabs.org/?p=94 This was annoying
1697  because I was previously convinced that an expanding tree of hashes would
1698  be very close to optimal.
1699 \end_layout
1700
1701 \end_inset
1702
1703 , it became clear that it is hard to beat a straight linear hash table which
1704  doubles in size when it reaches saturation.
1705  Unfortunately, altering the hash table introduces serious locking complications
1706 : the entire hash table needs to be locked to enlarge the hash table, and
1707  others might be holding locks.
1708  Particularly insidious are insertions done under tdb_chainlock.
1709 \end_layout
1710
1711 \begin_layout Standard
1712 Thus an expanding layered hash will be used: an array of hash groups, with
1713  each hash group exploding into pointers to lower hash groups once it fills,
1714  turning into a hash tree.
1715  This has implications for locking: we must lock the entire group in case
1716  we need to expand it, yet we don't know how deep the tree is at that point.
1717 \end_layout
1718
1719 \begin_layout Standard
1720 Note that bits from the hash table entries should be stolen to hold more
1721  hash bits to reduce the penalty of collisions.
1722  We can use the otherwise-unused lower 3 bits.
1723  If we limit the size of the database to 64 exabytes, we can use the top
1724  8 bits of the hash entry as well.
1725  These 11 bits would reduce false positives down to 1 in 2000 which is more
1726  than we need: we can use one of the bits to indicate that the extra hash
1727  bits are valid.
1728  This means we can choose not to re-hash all entries when we expand a hash
1729  group; simply use the next bits we need and mark them invalid.
1730 \end_layout
1731
1732 \begin_layout Subsubsection
1733 Status
1734 \end_layout
1735
1736 \begin_layout Standard
1737 Complete.
1738 \end_layout
1739
1740 \begin_layout Subsection
1741 \begin_inset CommandInset label
1742 LatexCommand label
1743 name "TDB-Freelist-Is"
1744
1745 \end_inset
1746
1747 TDB Freelist Is Highly Contended
1748 \end_layout
1749
1750 \begin_layout Standard
1751 TDB uses a single linked list for the free list.
1752  Allocation occurs as follows, using heuristics which have evolved over
1753  time:
1754 \end_layout
1755
1756 \begin_layout Enumerate
1757 Get the free list lock for this whole operation.
1758 \end_layout
1759
1760 \begin_layout Enumerate
1761 Multiply length by 1.25, so we always over-allocate by 25%.
1762 \end_layout
1763
1764 \begin_layout Enumerate
1765 Set the slack multiplier to 1.
1766 \end_layout
1767
1768 \begin_layout Enumerate
1769 Examine the current freelist entry: if it is > length but < the current
1770  best case, remember it as the best case.
1771 \end_layout
1772
1773 \begin_layout Enumerate
1774 Multiply the slack multiplier by 1.05.
1775 \end_layout
1776
1777 \begin_layout Enumerate
1778 If our best fit so far is less than length * slack multiplier, return it.
1779  The slack will be turned into a new free record if it's large enough.
1780 \end_layout
1781
1782 \begin_layout Enumerate
1783 Otherwise, go onto the next freelist entry.
1784 \end_layout
1785
1786 \begin_layout Standard
1787 Deleting a record occurs as follows:
1788 \end_layout
1789
1790 \begin_layout Enumerate
1791 Lock the hash chain for this whole operation.
1792 \end_layout
1793
1794 \begin_layout Enumerate
1795 Walk the chain to find the record, keeping the prev pointer offset.
1796 \end_layout
1797
1798 \begin_layout Enumerate
1799 If max_dead is non-zero:
1800 \end_layout
1801
1802 \begin_deeper
1803 \begin_layout Enumerate
1804 Walk the hash chain again and count the dead records.
1805 \end_layout
1806
1807 \begin_layout Enumerate
1808 If it's more than max_dead, bulk free all the dead ones (similar to steps
1809  4 and below, but the lock is only obtained once).
1810 \end_layout
1811
1812 \begin_layout Enumerate
1813 Simply mark this record as dead and return.
1814  
1815 \end_layout
1816
1817 \end_deeper
1818 \begin_layout Enumerate
1819 Get the free list lock for the remainder of this operation.
1820 \end_layout
1821
1822 \begin_layout Enumerate
1823 \begin_inset CommandInset label
1824 LatexCommand label
1825 name "right-merging"
1826
1827 \end_inset
1828
1829 Examine the following block to see if it is free; if so, enlarge the current
1830  block and remove that block from the free list.
1831  This was disabled, as removal from the free list was O(entries-in-free-list).
1832 \end_layout
1833
1834 \begin_layout Enumerate
1835 Examine the preceeding block to see if it is free: for this reason, each
1836  block has a 32-bit tailer which indicates its length.
1837  If it is free, expand it to cover our new block and return.
1838 \end_layout
1839
1840 \begin_layout Enumerate
1841 Otherwise, prepend ourselves to the free list.
1842 \end_layout
1843
1844 \begin_layout Standard
1845 Disabling right-merging (step 
1846 \begin_inset CommandInset ref
1847 LatexCommand ref
1848 reference "right-merging"
1849
1850 \end_inset
1851
1852 ) causes fragmentation; the other heuristics proved insufficient to address
1853  this, so the final answer to this was that when we expand the TDB file
1854  inside a transaction commit, we repack the entire tdb.
1855 \end_layout
1856
1857 \begin_layout Standard
1858 The single list lock limits our allocation rate; due to the other issues
1859  this is not currently seen as a bottleneck.
1860 \end_layout
1861
1862 \begin_layout Subsubsection
1863 Proposed Solution
1864 \end_layout
1865
1866 \begin_layout Standard
1867 The first step is to remove all the current heuristics, as they obviously
1868  interact, then examine them once the lock contention is addressed.
1869 \end_layout
1870
1871 \begin_layout Standard
1872 The free list must be split to reduce contention.
1873  Assuming perfect free merging, we can at most have 1 free list entry for
1874  each entry.
1875  This implies that the number of free lists is related to the size of the
1876  hash table, but as it is rare to walk a large number of free list entries
1877  we can use far fewer, say 1/32 of the number of hash buckets.
1878 \end_layout
1879
1880 \begin_layout Standard
1881 It seems tempting to try to reuse the hash implementation which we use for
1882  records here, but we have two ways of searching for free entries: for allocatio
1883 n we search by size (and possibly zone) which produces too many clashes
1884  for our hash table to handle well, and for coalescing we search by address.
1885  Thus an array of doubly-linked free lists seems preferable.
1886 \end_layout
1887
1888 \begin_layout Standard
1889 There are various benefits in using per-size free lists (see 
1890 \begin_inset CommandInset ref
1891 LatexCommand ref
1892 reference "sub:TDB-Becomes-Fragmented"
1893
1894 \end_inset
1895
1896 ) but it's not clear this would reduce contention in the common case where
1897  all processes are allocating/freeing the same size.
1898  Thus we almost certainly need to divide in other ways: the most obvious
1899  is to divide the file into zones, and using a free list (or table of free
1900  lists) for each.
1901  This approximates address ordering.
1902 \end_layout
1903
1904 \begin_layout Standard
1905 Unfortunately it is difficult to know what heuristics should be used to
1906  determine zone sizes, and our transaction code relies on being able to
1907  create a 
1908 \begin_inset Quotes eld
1909 \end_inset
1910
1911 recovery area
1912 \begin_inset Quotes erd
1913 \end_inset
1914
1915  by simply appending to the file (difficult if it would need to create a
1916  new zone header).
1917  Thus we use a linked-list of free tables; currently we only ever create
1918  one, but if there is more than one we choose one at random to use.
1919  In future we may use heuristics to add new free tables on contention.
1920  We only expand the file when all free tables are exhausted.
1921 \end_layout
1922
1923 \begin_layout Standard
1924 The basic algorithm is as follows.
1925  Freeing is simple:
1926 \end_layout
1927
1928 \begin_layout Enumerate
1929 Identify the correct free list.
1930 \end_layout
1931
1932 \begin_layout Enumerate
1933 Lock the corresponding list.
1934 \end_layout
1935
1936 \begin_layout Enumerate
1937 Re-check the list (we didn't have a lock, sizes could have changed): relock
1938  if necessary.
1939 \end_layout
1940
1941 \begin_layout Enumerate
1942 Place the freed entry in the list.
1943 \end_layout
1944
1945 \begin_layout Standard
1946 Allocation is a little more complicated, as we perform delayed coalescing
1947  at this point:
1948 \end_layout
1949
1950 \begin_layout Enumerate
1951 Pick a free table; usually the previous one.
1952 \end_layout
1953
1954 \begin_layout Enumerate
1955 Lock the corresponding list.
1956 \end_layout
1957
1958 \begin_layout Enumerate
1959 If the top entry is -large enough, remove it from the list and return it.
1960 \end_layout
1961
1962 \begin_layout Enumerate
1963 Otherwise, coalesce entries in the list.If there was no entry large enough,
1964  unlock the list and try the next largest list
1965 \end_layout
1966
1967 \begin_layout Enumerate
1968 If no list has an entry which meets our needs, try the next free table.
1969 \end_layout
1970
1971 \begin_layout Enumerate
1972 If no zone satisfies, expand the file.
1973 \end_layout
1974
1975 \begin_layout Standard
1976 This optimizes rapid insert/delete of free list entries by not coalescing
1977  them all the time..
1978  First-fit address ordering ordering seems to be fairly good for keeping
1979  fragmentation low (see 
1980 \begin_inset CommandInset ref
1981 LatexCommand ref
1982 reference "sub:TDB-Becomes-Fragmented"
1983
1984 \end_inset
1985
1986 ).
1987  Note that address ordering does not need a tailer to coalesce, though if
1988  we needed one we could have one cheaply: see 
1989 \begin_inset CommandInset ref
1990 LatexCommand ref
1991 reference "sub:Records-Incur-A"
1992
1993 \end_inset
1994
1995 .
1996  
1997 \end_layout
1998
1999 \begin_layout Standard
2000 Each free entry has the free table number in the header: less than 255.
2001  It also contains a doubly-linked list for easy deletion.
2002 \end_layout
2003
2004 \begin_layout Subsection
2005 \begin_inset CommandInset label
2006 LatexCommand label
2007 name "sub:TDB-Becomes-Fragmented"
2008
2009 \end_inset
2010
2011 TDB Becomes Fragmented
2012 \end_layout
2013
2014 \begin_layout Standard
2015 Much of this is a result of allocation strategy
2016 \begin_inset Foot
2017 status collapsed
2018
2019 \begin_layout Plain Layout
2020 The Memory Fragmentation Problem: Solved? Johnstone & Wilson 1995 ftp://ftp.cs.ute
2021 xas.edu/pub/garbage/malloc/ismm98.ps
2022 \end_layout
2023
2024 \end_inset
2025
2026  and deliberate hobbling of coalescing; internal fragmentation (aka overallocati
2027 on) is deliberately set at 25%, and external fragmentation is only cured
2028  by the decision to repack the entire db when a transaction commit needs
2029  to enlarge the file.
2030 \end_layout
2031
2032 \begin_layout Subsubsection
2033 Proposed Solution
2034 \end_layout
2035
2036 \begin_layout Standard
2037 The 25% overhead on allocation works in practice for ldb because indexes
2038  tend to expand by one record at a time.
2039  This internal fragmentation can be resolved by having an 
2040 \begin_inset Quotes eld
2041 \end_inset
2042
2043 expanded
2044 \begin_inset Quotes erd
2045 \end_inset
2046
2047  bit in the header to note entries that have previously expanded, and allocating
2048  more space for them.
2049 \end_layout
2050
2051 \begin_layout Standard
2052 There are is a spectrum of possible solutions for external fragmentation:
2053  one is to use a fragmentation-avoiding allocation strategy such as best-fit
2054  address-order allocator.
2055  The other end of the spectrum would be to use a bump allocator (very fast
2056  and simple) and simply repack the file when we reach the end.
2057 \end_layout
2058
2059 \begin_layout Standard
2060 There are three problems with efficient fragmentation-avoiding allocators:
2061  they are non-trivial, they tend to use a single free list for each size,
2062  and there's no evidence that tdb allocation patterns will match those recorded
2063  for general allocators (though it seems likely).
2064 \end_layout
2065
2066 \begin_layout Standard
2067 Thus we don't spend too much effort on external fragmentation; we will be
2068  no worse than the current code if we need to repack on occasion.
2069  More effort is spent on reducing freelist contention, and reducing overhead.
2070 \end_layout
2071
2072 \begin_layout Subsection
2073 \begin_inset CommandInset label
2074 LatexCommand label
2075 name "sub:Records-Incur-A"
2076
2077 \end_inset
2078
2079 Records Incur A 28-Byte Overhead
2080 \end_layout
2081
2082 \begin_layout Standard
2083 Each TDB record has a header as follows:
2084 \end_layout
2085
2086 \begin_layout LyX-Code
2087 struct tdb_record {
2088 \end_layout
2089
2090 \begin_layout LyX-Code
2091         tdb_off_t next; /* offset of the next record in the list */
2092 \end_layout
2093
2094 \begin_layout LyX-Code
2095         tdb_len_t rec_len; /* total byte length of record */
2096 \end_layout
2097
2098 \begin_layout LyX-Code
2099         tdb_len_t key_len; /* byte length of key */
2100 \end_layout
2101
2102 \begin_layout LyX-Code
2103         tdb_len_t data_len; /* byte length of data */
2104 \end_layout
2105
2106 \begin_layout LyX-Code
2107         uint32_t full_hash; /* the full 32 bit hash of the key */
2108 \end_layout
2109
2110 \begin_layout LyX-Code
2111         uint32_t magic;   /* try to catch errors */
2112 \end_layout
2113
2114 \begin_layout LyX-Code
2115         /* the following union is implied:
2116 \end_layout
2117
2118 \begin_layout LyX-Code
2119                 union {
2120 \end_layout
2121
2122 \begin_layout LyX-Code
2123                         char record[rec_len];
2124 \end_layout
2125
2126 \begin_layout LyX-Code
2127                         struct {
2128 \end_layout
2129
2130 \begin_layout LyX-Code
2131                                 char key[key_len];
2132 \end_layout
2133
2134 \begin_layout LyX-Code
2135                                 char data[data_len];
2136 \end_layout
2137
2138 \begin_layout LyX-Code
2139                         }
2140 \end_layout
2141
2142 \begin_layout LyX-Code
2143                         uint32_t totalsize; (tailer)
2144 \end_layout
2145
2146 \begin_layout LyX-Code
2147                 }
2148 \end_layout
2149
2150 \begin_layout LyX-Code
2151         */
2152 \end_layout
2153
2154 \begin_layout LyX-Code
2155 };
2156 \end_layout
2157
2158 \begin_layout Standard
2159 Naively, this would double to a 56-byte overhead on a 64 bit implementation.
2160 \end_layout
2161
2162 \begin_layout Subsubsection
2163 Proposed Solution
2164 \end_layout
2165
2166 \begin_layout Standard
2167 We can use various techniques to reduce this for an allocated block:
2168 \end_layout
2169
2170 \begin_layout Enumerate
2171 The 'next' pointer is not required, as we are using a flat hash table.
2172 \end_layout
2173
2174 \begin_layout Enumerate
2175 'rec_len' can instead be expressed as an addition to key_len and data_len
2176  (it accounts for wasted or overallocated length in the record).
2177  Since the record length is always a multiple of 8, we can conveniently
2178  fit it in 32 bits (representing up to 35 bits).
2179 \end_layout
2180
2181 \begin_layout Enumerate
2182 'key_len' and 'data_len' can be reduced.
2183  I'm unwilling to restrict 'data_len' to 32 bits, but instead we can combine
2184  the two into one 64-bit field and using a 5 bit value which indicates at
2185  what bit to divide the two.
2186  Keys are unlikely to scale as fast as data, so I'm assuming a maximum key
2187  size of 32 bits.
2188 \end_layout
2189
2190 \begin_layout Enumerate
2191 'full_hash' is used to avoid a memcmp on the 
2192 \begin_inset Quotes eld
2193 \end_inset
2194
2195 miss
2196 \begin_inset Quotes erd
2197 \end_inset
2198
2199  case, but this is diminishing returns after a handful of bits (at 10 bits,
2200  it reduces 99.9% of false memcmp).
2201  As an aside, as the lower bits are already incorporated in the hash table
2202  resolution, the upper bits should be used here.
2203  Note that it's not clear that these bits will be a win, given the extra
2204  bits in the hash table itself (see 
2205 \begin_inset CommandInset ref
2206 LatexCommand ref
2207 reference "sub:Hash-Size-Solution"
2208
2209 \end_inset
2210
2211 ).
2212 \end_layout
2213
2214 \begin_layout Enumerate
2215 'magic' does not need to be enlarged: it currently reflects one of 5 values
2216  (used, free, dead, recovery, and unused_recovery).
2217  It is useful for quick sanity checking however, and should not be eliminated.
2218 \end_layout
2219
2220 \begin_layout Enumerate
2221 'tailer' is only used to coalesce free blocks (so a block to the right can
2222  find the header to check if this block is free).
2223  This can be replaced by a single 'free' bit in the header of the following
2224  block (and the tailer only exists in free blocks).
2225 \begin_inset Foot
2226 status collapsed
2227
2228 \begin_layout Plain Layout
2229 This technique from Thomas Standish.
2230  Data Structure Techniques.
2231  Addison-Wesley, Reading, Massachusetts, 1980.
2232 \end_layout
2233
2234 \end_inset
2235
2236  The current proposed coalescing algorithm doesn't need this, however.
2237 \end_layout
2238
2239 \begin_layout Standard
2240 This produces a 16 byte used header like this:
2241 \end_layout
2242
2243 \begin_layout LyX-Code
2244 struct tdb_used_record {
2245 \end_layout
2246
2247 \begin_layout LyX-Code
2248         uint32_t used_magic : 16,
2249 \end_layout
2250
2251 \begin_layout LyX-Code
2252
2253 \end_layout
2254
2255 \begin_layout LyX-Code
2256                  key_data_divide: 5,
2257 \end_layout
2258
2259 \begin_layout LyX-Code
2260                  top_hash: 11;
2261 \end_layout
2262
2263 \begin_layout LyX-Code
2264         uint32_t extra_octets;
2265 \end_layout
2266
2267 \begin_layout LyX-Code
2268         uint64_t key_and_data_len;
2269 \end_layout
2270
2271 \begin_layout LyX-Code
2272 };
2273 \end_layout
2274
2275 \begin_layout Standard
2276 And a free record like this:
2277 \end_layout
2278
2279 \begin_layout LyX-Code
2280 struct tdb_free_record {
2281 \end_layout
2282
2283 \begin_layout LyX-Code
2284         uint64_t free_magic: 8,
2285 \end_layout
2286
2287 \begin_layout LyX-Code
2288                    prev : 56;
2289 \end_layout
2290
2291 \begin_layout LyX-Code
2292
2293 \end_layout
2294
2295 \begin_layout LyX-Code
2296         uint64_t free_table: 8,
2297 \end_layout
2298
2299 \begin_layout LyX-Code
2300                  total_length : 56
2301 \end_layout
2302
2303 \begin_layout LyX-Code
2304         uint64_t next;;
2305 \end_layout
2306
2307 \begin_layout LyX-Code
2308 };
2309 \end_layout
2310
2311 \begin_layout Standard
2312
2313 \change_deleted 0 1291206079
2314  
2315 \change_unchanged
2316 Note that by limiting valid offsets to 56 bits, we can pack everything we
2317  need into 3 64-byte words, meaning our minimum record size is 8 bytes.
2318 \end_layout
2319
2320 \begin_layout Subsubsection
2321 Status
2322 \end_layout
2323
2324 \begin_layout Standard
2325 Complete.
2326 \end_layout
2327
2328 \begin_layout Subsection
2329 Transaction Commit Requires 4 fdatasync
2330 \end_layout
2331
2332 \begin_layout Standard
2333 The current transaction algorithm is:
2334 \end_layout
2335
2336 \begin_layout Enumerate
2337 write_recovery_data();
2338 \end_layout
2339
2340 \begin_layout Enumerate
2341 sync();
2342 \end_layout
2343
2344 \begin_layout Enumerate
2345 write_recovery_header();
2346 \end_layout
2347
2348 \begin_layout Enumerate
2349 sync();
2350 \end_layout
2351
2352 \begin_layout Enumerate
2353 overwrite_with_new_data();
2354 \end_layout
2355
2356 \begin_layout Enumerate
2357 sync();
2358 \end_layout
2359
2360 \begin_layout Enumerate
2361 remove_recovery_header();
2362 \end_layout
2363
2364 \begin_layout Enumerate
2365 sync(); 
2366 \end_layout
2367
2368 \begin_layout Standard
2369 On current ext3, each sync flushes all data to disk, so the next 3 syncs
2370  are relatively expensive.
2371  But this could become a performance bottleneck on other filesystems such
2372  as ext4.
2373 \end_layout
2374
2375 \begin_layout Subsubsection
2376 Proposed Solution
2377 \end_layout
2378
2379 \begin_layout Standard
2380 Neil Brown points out that this is overzealous, and only one sync is needed:
2381 \end_layout
2382
2383 \begin_layout Enumerate
2384 Bundle the recovery data, a transaction counter and a strong checksum of
2385  the new data.
2386 \end_layout
2387
2388 \begin_layout Enumerate
2389 Strong checksum that whole bundle.
2390 \end_layout
2391
2392 \begin_layout Enumerate
2393 Store the bundle in the database.
2394 \end_layout
2395
2396 \begin_layout Enumerate
2397 Overwrite the oldest of the two recovery pointers in the header (identified
2398  using the transaction counter) with the offset of this bundle.
2399 \end_layout
2400
2401 \begin_layout Enumerate
2402 sync.
2403 \end_layout
2404
2405 \begin_layout Enumerate
2406 Write the new data to the file.
2407 \end_layout
2408
2409 \begin_layout Standard
2410 Checking for recovery means identifying the latest bundle with a valid checksum
2411  and using the new data checksum to ensure that it has been applied.
2412  This is more expensive than the current check, but need only be done at
2413  open.
2414  For running databases, a separate header field can be used to indicate
2415  a transaction in progress; we need only check for recovery if this is set.
2416 \end_layout
2417
2418 \begin_layout Subsubsection
2419 Status
2420 \end_layout
2421
2422 \begin_layout Standard
2423 Deferred.
2424 \end_layout
2425
2426 \begin_layout Subsection
2427 \begin_inset CommandInset label
2428 LatexCommand label
2429 name "sub:TDB-Does-Not"
2430
2431 \end_inset
2432
2433 TDB Does Not Have Snapshot Support
2434 \end_layout
2435
2436 \begin_layout Subsubsection
2437 Proposed SolutionNone.
2438  At some point you say 
2439 \begin_inset Quotes eld
2440 \end_inset
2441
2442 use a real database
2443 \begin_inset Quotes erd
2444 \end_inset
2445
2446  (but see 
2447 \begin_inset CommandInset ref
2448 LatexCommand ref
2449 reference "replay-attribute"
2450
2451 \end_inset
2452
2453 ).
2454 \end_layout
2455
2456 \begin_layout Standard
2457 But as a thought experiment, if we implemented transactions to only overwrite
2458  free entries (this is tricky: there must not be a header in each entry
2459  which indicates whether it is free, but use of presence in metadata elsewhere),
2460  and a pointer to the hash table, we could create an entirely new commit
2461  without destroying existing data.
2462  Then it would be easy to implement snapshots in a similar way.
2463 \end_layout
2464
2465 \begin_layout Standard
2466 This would not allow arbitrary changes to the database, such as tdb_repack
2467  does, and would require more space (since we have to preserve the current
2468  and future entries at once).
2469  If we used hash trees rather than one big hash table, we might only have
2470  to rewrite some sections of the hash, too.
2471 \end_layout
2472
2473 \begin_layout Standard
2474 We could then implement snapshots using a similar method, using multiple
2475  different hash tables/free tables.
2476 \end_layout
2477
2478 \begin_layout Subsubsection
2479 Status
2480 \end_layout
2481
2482 \begin_layout Standard
2483 Deferred.
2484 \end_layout
2485
2486 \begin_layout Subsection
2487 Transactions Cannot Operate in Parallel
2488 \end_layout
2489
2490 \begin_layout Standard
2491 This would be useless for ldb, as it hits the index records with just about
2492  every update.
2493  It would add significant complexity in resolving clashes, and cause the
2494  all transaction callers to write their code to loop in the case where the
2495  transactions spuriously failed.
2496 \end_layout
2497
2498 \begin_layout Subsubsection
2499 Proposed Solution
2500 \end_layout
2501
2502 \begin_layout Standard
2503 None (but see 
2504 \begin_inset CommandInset ref
2505 LatexCommand ref
2506 reference "replay-attribute"
2507
2508 \end_inset
2509
2510 ).
2511  We could solve a small part of the problem by providing read-only transactions.
2512  These would allow one write transaction to begin, but it could not commit
2513  until all r/o transactions are done.
2514  This would require a new RO_TRANSACTION_LOCK, which would be upgraded on
2515  commit.
2516 \end_layout
2517
2518 \begin_layout Subsubsection
2519 Status
2520 \end_layout
2521
2522 \begin_layout Standard
2523 Deferred.
2524 \end_layout
2525
2526 \begin_layout Subsection
2527 Default Hash Function Is Suboptimal
2528 \end_layout
2529
2530 \begin_layout Standard
2531 The Knuth-inspired multiplicative hash used by tdb is fairly slow (especially
2532  if we expand it to 64 bits), and works best when the hash bucket size is
2533  a prime number (which also means a slow modulus).
2534  In addition, it is highly predictable which could potentially lead to a
2535  Denial of Service attack in some TDB uses.
2536 \end_layout
2537
2538 \begin_layout Subsubsection
2539 Proposed Solution
2540 \end_layout
2541
2542 \begin_layout Standard
2543 The Jenkins lookup3 hash
2544 \begin_inset Foot
2545 status open
2546
2547 \begin_layout Plain Layout
2548 http://burtleburtle.net/bob/c/lookup3.c
2549 \end_layout
2550
2551 \end_inset
2552
2553  is a fast and superbly-mixing hash.
2554  It's used by the Linux kernel and almost everything else.
2555  This has the particular properties that it takes an initial seed, and produces
2556  two 32 bit hash numbers, which we can combine into a 64-bit hash.
2557 \end_layout
2558
2559 \begin_layout Standard
2560 The seed should be created at tdb-creation time from some random source,
2561  and placed in the header.
2562  This is far from foolproof, but adds a little bit of protection against
2563  hash bombing.
2564 \end_layout
2565
2566 \begin_layout Subsubsection
2567 Status
2568 \end_layout
2569
2570 \begin_layout Standard
2571 Complete.
2572 \end_layout
2573
2574 \begin_layout Subsection
2575 \begin_inset CommandInset label
2576 LatexCommand label
2577 name "Reliable-Traversal-Adds"
2578
2579 \end_inset
2580
2581 Reliable Traversal Adds Complexity
2582 \end_layout
2583
2584 \begin_layout Standard
2585 We lock a record during traversal iteration, and try to grab that lock in
2586  the delete code.
2587  If that grab on delete fails, we simply mark it deleted and continue onwards;
2588  traversal checks for this condition and does the delete when it moves off
2589  the record.
2590 \end_layout
2591
2592 \begin_layout Standard
2593 If traversal terminates, the dead record may be left indefinitely.
2594 \end_layout
2595
2596 \begin_layout Subsubsection
2597 Proposed Solution
2598 \end_layout
2599
2600 \begin_layout Standard
2601 Remove reliability guarantees; see 
2602 \begin_inset CommandInset ref
2603 LatexCommand ref
2604 reference "traverse-Proposed-Solution"
2605
2606 \end_inset
2607
2608 .
2609 \end_layout
2610
2611 \begin_layout Subsubsection
2612 Status
2613 \end_layout
2614
2615 \begin_layout Standard
2616 Complete.
2617 \end_layout
2618
2619 \begin_layout Subsection
2620 Fcntl Locking Adds Overhead
2621 \end_layout
2622
2623 \begin_layout Standard
2624 Placing a fcntl lock means a system call, as does removing one.
2625  This is actually one reason why transactions can be faster (everything
2626  is locked once at transaction start).
2627  In the uncontended case, this overhead can theoretically be eliminated.
2628 \end_layout
2629
2630 \begin_layout Subsubsection
2631 Proposed Solution
2632 \end_layout
2633
2634 \begin_layout Standard
2635 None.
2636 \end_layout
2637
2638 \begin_layout Standard
2639 We tried this before with spinlock support, in the early days of TDB, and
2640  it didn't make much difference except in manufactured benchmarks.
2641 \end_layout
2642
2643 \begin_layout Standard
2644 We could use spinlocks (with futex kernel support under Linux), but it means
2645  that we lose automatic cleanup when a process dies with a lock.
2646  There is a method of auto-cleanup under Linux, but it's not supported by
2647  other operating systems.
2648  We could reintroduce a clear-if-first-style lock and sweep for dead futexes
2649  on open, but that wouldn't help the normal case of one concurrent opener
2650  dying.
2651  Increasingly elaborate repair schemes could be considered, but they require
2652  an ABI change (everyone must use them) anyway, so there's no need to do
2653  this at the same time as everything else.
2654 \end_layout
2655
2656 \begin_layout Subsection
2657 Some Transactions Don't Require Durability
2658 \end_layout
2659
2660 \begin_layout Standard
2661 Volker points out that gencache uses a CLEAR_IF_FIRST tdb for normal (fast)
2662  usage, and occasionally empties the results into a transactional TDB.
2663  This kind of usage prioritizes performance over durability: as long as
2664  we are consistent, data can be lost.
2665 \end_layout
2666
2667 \begin_layout Standard
2668 This would be more neatly implemented inside tdb: a 
2669 \begin_inset Quotes eld
2670 \end_inset
2671
2672 soft
2673 \begin_inset Quotes erd
2674 \end_inset
2675
2676  transaction commit (ie.
2677  syncless) which meant that data may be reverted on a crash.
2678 \end_layout
2679
2680 \begin_layout Subsubsection
2681 Proposed Solution
2682 \end_layout
2683
2684 \begin_layout Standard
2685 None.
2686 \end_layout
2687
2688 \begin_layout Standard
2689 Unfortunately any transaction scheme which overwrites old data requires
2690  a sync before that overwrite to avoid the possibility of corruption.
2691 \end_layout
2692
2693 \begin_layout Standard
2694 It seems possible to use a scheme similar to that described in 
2695 \begin_inset CommandInset ref
2696 LatexCommand ref
2697 reference "sub:TDB-Does-Not"
2698
2699 \end_inset
2700
2701 ,where transactions are committed without overwriting existing data, and
2702  an array of top-level pointers were available in the header.
2703  If the transaction is 
2704 \begin_inset Quotes eld
2705 \end_inset
2706
2707 soft
2708 \begin_inset Quotes erd
2709 \end_inset
2710
2711  then we would not need a sync at all: existing processes would pick up
2712  the new hash table and free list and work with that.
2713 \end_layout
2714
2715 \begin_layout Standard
2716 At some later point, a sync would allow recovery of the old data into the
2717  free lists (perhaps when the array of top-level pointers filled).
2718  On crash, tdb_open() would examine the array of top levels, and apply the
2719  transactions until it encountered an invalid checksum.
2720 \end_layout
2721
2722 \begin_layout Subsection
2723 Tracing Is Fragile, Replay Is External
2724 \end_layout
2725
2726 \begin_layout Standard
2727 The current TDB has compile-time-enabled tracing code, but it often breaks
2728  as it is not enabled by default.
2729  In a similar way, the ctdb code has an external wrapper which does replay
2730  tracing so it can coordinate cluster-wide transactions.
2731 \end_layout
2732
2733 \begin_layout Subsubsection
2734 Proposed Solution
2735 \begin_inset CommandInset label
2736 LatexCommand label
2737 name "replay-attribute"
2738
2739 \end_inset
2740
2741
2742 \end_layout
2743
2744 \begin_layout Standard
2745 Tridge points out that an attribute can be later added to tdb_open (see
2746  
2747 \begin_inset CommandInset ref
2748 LatexCommand ref
2749 reference "attributes"
2750
2751 \end_inset
2752
2753 ) to provide replay/trace hooks, which could become the basis for this and
2754  future parallel transactions and snapshot support.
2755 \end_layout
2756
2757 \begin_layout Subsubsection
2758 Status
2759 \end_layout
2760
2761 \begin_layout Standard
2762 Deferred.
2763 \end_layout
2764
2765 \end_body
2766 \end_document