-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtrunk.c
More file actions
7377 lines (6554 loc) · 247 KB
/
Copy pathtrunk.c
File metadata and controls
7377 lines (6554 loc) · 247 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2026 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
/*
* trunk.c --
*
* This file contains the implementation of the SplinterDB trunk.
*/
#include "trunk.h"
#include "platform.h"
#include "data_internal.h"
#include "platform_heap.h"
#include "util.h"
#include "btree.h"
#include "routing_filter.h"
#include "vector.h"
#include "merge.h"
#include "data_internal.h"
#include "task.h"
#include "notification.h"
#include "prefetch.h"
#include "poison.h"
typedef VECTOR(routing_filter) routing_filter_vector;
struct ONDISK trunk_ondisk_bundle {
routing_filter maplet;
uint16 num_branches;
// branches[0] is the oldest branch
branch_ref branches[];
};
typedef struct ONDISK trunk_pivot_stats {
int64 num_kv_bytes;
int64 num_tuples;
} trunk_pivot_stats;
struct trunk_pivot {
trunk_pivot_stats prereceive_stats;
trunk_pivot_stats stats;
uint64 child_addr;
// Index of the oldest bundle that is live for this pivot
uint64 inflight_bundle_start;
ondisk_key key;
};
typedef VECTOR(trunk_ondisk_node_ref *) trunk_ondisk_node_ref_vector;
struct ONDISK trunk_ondisk_pivot {
trunk_pivot_stats stats;
uint64 child_addr;
uint64 num_live_inflight_bundles;
ondisk_key key;
};
typedef struct ONDISK trunk_ondisk_node {
uint16 height;
uint16 num_pivots;
// On disk, inflight bundles are ordered from newest to oldest.
uint16 num_inflight_bundles;
uint32 inflight_bundles_offset;
uint32 pivot_offsets[];
} trunk_ondisk_node;
typedef enum bundle_compaction_phase {
BUNDLE_COMPACTION_IN_PROGRESS = 0,
// Terminal but not consumable by maplet compaction. See output.rc.
BUNDLE_COMPACTION_ENDED = 1,
BUNDLE_COMPACTION_SUCCEEDED = 2
} bundle_compaction_phase;
typedef VECTOR(trunk_branch_info) trunk_branch_info_vector;
typedef struct trunk_branch_merger {
platform_heap_id hid;
const trunk_config *cfg;
key min_key;
key max_key;
uint64 height;
trunk_branch_info_vector branches;
merge_iterator *merge_itor;
iterator_vector itors;
} trunk_branch_merger;
typedef struct bundle_compaction {
struct bundle_compaction *next;
task tsk; // bundle_comaction_task
task_tracker *tracker;
// Immutable after creation.
struct {
trunk_pivot_state *pivot_state;
uint64 num_bundles;
trunk_pivot_stats stats;
trunk_branch_info_vector branches;
merge_behavior merge_mode;
} input;
// Written by bundle_compaction_task before terminal publication.
struct {
// For ENDED compactions, rc is the local compaction error. If the
// compaction ended because the pivot was abandoned, rc remains STATUS_OK
// and the pivot/maplet compaction status determines notification status.
platform_status rc;
branch_ref branch;
trunk_pivot_stats stats;
uint32 *fingerprints;
uint64 time_ns;
} output;
// IN_PROGRESS covers both queued and actively running compaction tasks.
//
// bundle_compaction_task must not access a bundle_compaction after
// transitioning it to a terminal phase, except possibly to garbage-collect
// the pivot_state and its bundle_compactions as part of releasing the
// pivot_state.
//
// When a bundle_compaction becomes both (1) SUCCEEDED, and (2) the front of
// its pivot_state's compactions list, then we must ensure that there exists
// a maplet compaction that will consume it. There are two ways that a
// bundle_compaction could enter this state:
// * It is the head of the list and bundle_compaction_task transitions it to
// SUCCEEDED. In this case, bundle_compaction_task must launch the
// maplet compaction.
// * It is SUCCEEDED and then maplet_compaction_task removes preceding
// compactions so that this one becomes the head. In that case, the
// maplet_compaction_task should enqueue another maplet_compaction or
// perform the maplet_compaction for this bundle_compaction.
// Thus transitions to SUCCEEDED, and mutations of the pivot_state's
// compactions list, must both be done under the pivot_state->lock.
bundle_compaction_phase phase;
} bundle_compaction;
typedef struct trunk_context trunk_context;
struct trunk_pivot_state {
struct trunk_pivot_state *next;
task tsk; // maplet_compaction_task
trunk_context *context;
key_buffer key;
key_buffer ubkey;
uint64 height;
uint64 refcount;
// lock covers all fields in protected.
platform_spinlock lock;
struct {
// Abandonment may occur because of tree structure changes that make these
// compactions inapplicable to the tree, or because of a maplet compaction
// failure that blocks all subsequent maplet compactions of this pivot,
// which means we cannot use the results of any of the remaining
// bundle_compactions. (A failed bundle_compaction blocks subsequent
// bundle_compactions, but not logically earlier bundle_compactions, so it
// is not cause to immediately abandon the pivot_state.)
bool32 abandoned;
platform_status maplet_compaction_rc;
routing_filter maplet;
uint64 num_branches;
uint64 total_bundles;
// Once a maplet_compaction_task removes some prefix of bundle_compactions
// from this list so that the new head of the list is not in a >=
// MIN_ENDED state, it must not touch the pivot_state again after
// releasing lock, because the bundle_compaction_task of the new head of
// this list could launch another maplet_compaction_task.
bundle_compaction *bundle_compactions;
} protected;
};
typedef enum trunk_flush_policy_type {
TRUNK_FLUSH_POLICY_STANDARD,
TRUNK_FLUSH_POLICY_RANGE,
} trunk_flush_policy_type;
typedef struct trunk_flush_policy {
trunk_flush_policy_type type;
bool32 full_leaf_compactions;
union {
struct {
key minkey;
key maxkey;
} range;
} u;
} trunk_flush_policy;
struct pending_gc {
pending_gc *next;
uint64 addr;
};
/***************************************************
* branch_ref operations
***************************************************/
static branch_ref
create_branch_ref(uint64 addr)
{
return (branch_ref){.addr = addr};
}
static uint64
branch_ref_addr(branch_ref bref)
{
return bref.addr;
}
#define NULL_BRANCH_REF ((branch_ref){.addr = 0})
static bool32
branch_is_null(branch_ref bref)
{
return bref.addr == 0;
}
/**************************
* routed_bundle operations
**************************/
static void
bundle_init(bundle *bndl, platform_heap_id hid)
{
bndl->maplet = NULL_ROUTING_FILTER;
vector_init(&bndl->branches, hid);
}
static platform_status
bundle_init_single(bundle *bndl,
platform_heap_id hid,
routing_filter maplet,
branch_ref branch)
{
bndl->maplet = maplet;
vector_init(&bndl->branches, hid);
platform_status rc = vector_append(&bndl->branches, branch);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: vector_append() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
vector_deinit(&bndl->branches);
}
return rc;
}
static platform_status
bundle_init_copy(bundle *dst, const bundle *src, platform_heap_id hid)
{
vector_init(&dst->branches, hid);
platform_status rc = vector_copy(&dst->branches, &src->branches);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: vector_copy() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
vector_deinit(&dst->branches);
return rc;
}
dst->maplet = src->maplet;
return rc;
}
static void
bundle_deinit(bundle *bndl)
{
vector_deinit(&bndl->branches);
}
static platform_status
bundle_add_branches(bundle *bndl,
routing_filter new_maplet,
branch_ref_vector *new_branches)
{
platform_status rc;
rc = vector_append_vector(&bndl->branches, new_branches);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: vector_append_vector() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
return rc;
}
bndl->maplet = new_maplet;
return STATUS_OK;
}
static routing_filter
bundle_maplet(const bundle *bndl)
{
return bndl->maplet;
}
static uint64
bundle_num_branches(const bundle *bndl)
{
return vector_length(&bndl->branches);
}
static branch_ref
bundle_branch(const bundle *bndl, uint64 i)
{
return vector_get(&bndl->branches, i);
}
static const branch_ref *
bundle_branch_array(const bundle *bndl)
{
return vector_data(&bndl->branches);
}
static page_type
bundle_branch_type(const bundle *bndl)
{
platform_assert(!routing_filters_equal(&bndl->maplet, &NULL_ROUTING_FILTER)
|| bundle_num_branches(bndl) <= 1);
if (routing_filters_equal(&bndl->maplet, &NULL_ROUTING_FILTER)
&& bundle_num_branches(bndl) == 1)
{
return PAGE_TYPE_BRANCH;
} else {
return PAGE_TYPE_BRANCH;
}
}
debug_only static void
bundle_print(const bundle *bndl, platform_log_handle *log, int indent)
{
platform_log(
log, "%*sBundle(maplet: %lu, branches: ", indent, "", bndl->maplet.addr);
for (uint64 i = 0; i < bundle_num_branches(bndl); i++) {
platform_log(log, "%lu ", branch_ref_addr(bundle_branch_array(bndl)[i]));
}
platform_log(log, ")\n");
}
debug_only static void
bundle_vector_print(const bundle_vector *bv,
platform_log_handle *log,
int indent)
{
platform_log(
log, "%*s%3s %12s %-12s\n", indent, "", "i", "maplet", "branches");
for (uint64 i = 0; i < vector_length(bv); i++) {
const bundle *bndl = vector_get_ptr(bv, i);
platform_log(
log, "%*s%3lu %12lu ", indent, "", i, bundle_maplet(bndl).addr);
for (uint64 j = 0; j < bundle_num_branches(bndl); j++) {
platform_log(
log, "%lu ", branch_ref_addr(bundle_branch_array(bndl)[j]));
}
platform_log(log, "\n");
}
}
/********************
* Pivot stats
********************/
static trunk_pivot_stats
trunk_pivot_stats_from_btree_pivot_stats(btree_pivot_stats stats)
{
return (trunk_pivot_stats){.num_kv_bytes =
stats.key_bytes + stats.message_bytes,
.num_tuples = stats.num_kvs};
}
static trunk_pivot_stats
trunk_pivot_stats_subtract(trunk_pivot_stats a, trunk_pivot_stats b)
{
return (trunk_pivot_stats){.num_kv_bytes = a.num_kv_bytes - b.num_kv_bytes,
.num_tuples = a.num_tuples - b.num_tuples};
}
static trunk_pivot_stats
trunk_pivot_stats_add(trunk_pivot_stats a, trunk_pivot_stats b)
{
return (trunk_pivot_stats){.num_kv_bytes = a.num_kv_bytes + b.num_kv_bytes,
.num_tuples = a.num_tuples + b.num_tuples};
}
static bool32
trunk_pivot_stats_are_nonnegative(trunk_pivot_stats stats)
{
return stats.num_kv_bytes >= 0 && stats.num_tuples >= 0;
}
/******************
* pivot operations
******************/
#define TRUNK_STATS_ZERO \
((trunk_pivot_stats){.num_kv_bytes = 0, .num_tuples = 0})
static trunk_pivot *
trunk_pivot_create(platform_heap_id hid,
key k,
uint64 child_addr,
uint64 inflight_bundle_start,
trunk_pivot_stats prereceive_stats,
trunk_pivot_stats stats)
{
trunk_pivot *result = TYPED_FLEXIBLE_STRUCT_ZALLOC(
hid, result, key.bytes, ondisk_key_required_data_capacity(k));
if (result == NULL) {
platform_error_log(
"%s():%d: TYPED_FLEXIBLE_STRUCT_ZALLOC() failed", __func__, __LINE__);
return NULL;
}
copy_key_to_ondisk_key(&result->key, k);
result->child_addr = child_addr;
result->inflight_bundle_start = inflight_bundle_start;
platform_assert(trunk_pivot_stats_are_nonnegative(prereceive_stats));
platform_assert(trunk_pivot_stats_are_nonnegative(stats));
result->prereceive_stats = prereceive_stats;
result->stats = stats;
return result;
}
static trunk_pivot *
trunk_pivot_copy(const trunk_pivot *src, platform_heap_id hid)
{
return trunk_pivot_create(hid,
ondisk_key_to_key(&src->key),
src->child_addr,
src->inflight_bundle_start,
src->prereceive_stats,
src->stats);
}
static void
trunk_pivot_destroy(trunk_pivot *pvt, platform_heap_id hid)
{
platform_free(hid, pvt);
}
static key
trunk_pivot_key(const trunk_pivot *pvt)
{
return ondisk_key_to_key(&pvt->key);
}
static uint64
trunk_pivot_child_addr(const trunk_pivot *pvt)
{
return pvt->child_addr;
}
static void
trunk_pivot_set_child_addr(trunk_pivot *pvt, uint64 new_child_addr)
{
pvt->child_addr = new_child_addr;
}
static trunk_pivot_stats
trunk_pivot_get_stats(const trunk_pivot *pvt)
{
return pvt->stats;
}
static uint64
trunk_pivot_inflight_bundle_start(const trunk_pivot *pvt)
{
return pvt->inflight_bundle_start;
}
static void
trunk_pivot_set_inflight_bundle_start(trunk_pivot *pvt, uint64 start)
{
pvt->inflight_bundle_start = start;
}
static trunk_pivot_stats
trunk_pivot_received_bundles_stats(const trunk_pivot *pvt)
{
trunk_pivot_stats result =
trunk_pivot_stats_subtract(pvt->stats, pvt->prereceive_stats);
platform_assert(trunk_pivot_stats_are_nonnegative(result));
return result;
}
static uint64
trunk_pivot_num_kv_bytes(const trunk_pivot *pvt)
{
return pvt->stats.num_kv_bytes;
}
/*
* When new bundles get flushed to this pivot's node, you must
* inform the pivot of the tuple counts of the new bundles.
*/
static void
trunk_pivot_add_tuple_counts(trunk_pivot *pvt,
int coefficient,
trunk_pivot_stats stats)
{
if (coefficient == 1) {
pvt->stats.num_tuples += stats.num_tuples;
pvt->stats.num_kv_bytes += stats.num_kv_bytes;
} else if (coefficient == -1) {
platform_assert(stats.num_tuples <= pvt->stats.num_tuples);
platform_assert(stats.num_kv_bytes <= pvt->stats.num_kv_bytes);
pvt->stats.num_tuples -= stats.num_tuples;
pvt->stats.num_kv_bytes -= stats.num_kv_bytes;
} else {
platform_assert(0);
}
platform_assert(trunk_pivot_stats_are_nonnegative(pvt->stats));
}
debug_only static void
trunk_pivot_print(const trunk_pivot *pvt,
platform_log_handle *log,
const data_config *data_cfg,
int indent)
{
platform_log(
log,
"%*sPivot(pr_kvbytes: %lu pr_tuples: %lu kvbytes: %lu tuples: %lu "
"child: %lu ifstart: %lu %s)\n",
indent,
"",
pvt->prereceive_stats.num_kv_bytes,
pvt->prereceive_stats.num_tuples,
pvt->stats.num_kv_bytes,
pvt->stats.num_tuples,
pvt->child_addr,
pvt->inflight_bundle_start,
key_string(data_cfg, trunk_pivot_key(pvt)));
}
debug_only static void
trunk_pivot_vector_print(const trunk_pivot_vector *pivots,
platform_log_handle *log,
const data_config *data_cfg,
int indent)
{
platform_log(log,
"%*s%3s %12s %12s %12s %12s %12s %12s %-24s\n",
indent,
"",
"i",
"pr_kvbytes",
"pr_tuples",
"kvbytes",
"tuples",
"child_addr",
"if_start",
"key");
for (uint64 i = 0; i < vector_length(pivots); i++) {
trunk_pivot *pvt = vector_get(pivots, i);
platform_log(log,
"%*s%3lu %12lu %12lu %12lu %12lu %12lu %12lu %-24s\n",
indent,
"",
i,
pvt->prereceive_stats.num_kv_bytes,
pvt->prereceive_stats.num_tuples,
pvt->stats.num_kv_bytes,
pvt->stats.num_tuples,
pvt->child_addr,
pvt->inflight_bundle_start,
key_string(data_cfg, trunk_pivot_key(pvt)));
}
}
/***********************
* basic node operations
***********************/
/* Steals pivots, pivot_bundles, and inflight_bundles. */
static void
trunk_node_init(trunk_node *node,
uint16 height,
trunk_pivot_vector pivots,
bundle_vector pivot_bundles,
uint64 num_old_bundles,
bundle_vector inflight_bundles)
{
node->height = height;
node->pivots = pivots;
node->pivot_bundles = pivot_bundles;
node->num_old_bundles = num_old_bundles;
node->inflight_bundles = inflight_bundles;
}
static platform_status
trunk_node_copy_init(trunk_node *dst,
const trunk_node *src,
platform_heap_id hid)
{
trunk_pivot_vector pivots;
bundle_vector pivot_bundles;
bundle_vector inflight_bundles;
platform_status rc;
vector_init(&pivots, hid);
vector_init(&pivot_bundles, hid);
vector_init(&inflight_bundles, hid);
rc = VECTOR_MAP_ELTS_TO_PTRS(&pivots, trunk_pivot_copy, &src->pivots, hid);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: VECTOR_MAP_ELTS_TO_PTRS() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
goto cleanup_vectors;
}
rc = VECTOR_EMPLACE_MAP_PTRS(
&pivot_bundles, bundle_init_copy, &src->pivot_bundles, hid);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: VECTOR_EMPLACE_MAP_PTRS() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
goto cleanup_vectors;
}
rc = VECTOR_EMPLACE_MAP_PTRS(
&inflight_bundles, bundle_init_copy, &src->inflight_bundles, hid);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: VECTOR_EMPLACE_MAP_PTRS() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
goto cleanup_vectors;
}
trunk_node_init(dst,
src->height,
pivots,
pivot_bundles,
src->num_old_bundles,
inflight_bundles);
return STATUS_OK;
cleanup_vectors:
VECTOR_APPLY_TO_ELTS(&pivots, trunk_pivot_destroy, hid);
vector_deinit(&pivots);
VECTOR_APPLY_TO_PTRS(&pivot_bundles, bundle_deinit);
vector_deinit(&pivot_bundles);
VECTOR_APPLY_TO_PTRS(&inflight_bundles, bundle_deinit);
vector_deinit(&inflight_bundles);
return rc;
}
static platform_status
trunk_node_init_empty_leaf(trunk_node *node,
platform_heap_id hid,
key lb,
key ub)
{
trunk_pivot_vector pivots;
bundle_vector pivot_bundles;
bundle_vector inflight_bundles;
platform_status rc;
vector_init(&pivots, hid);
vector_init(&pivot_bundles, hid);
vector_init(&inflight_bundles, hid);
rc = vector_ensure_capacity(&pivots, 2);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: vector_ensure_capacity() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
goto cleanup_vectors;
}
rc = vector_ensure_capacity(&pivot_bundles, 1);
if (!SUCCESS(rc)) {
platform_error_log("%s():%d: vector_ensure_capacity() failed: %s",
__func__,
__LINE__,
platform_status_to_string(rc));
goto cleanup_vectors;
}
trunk_pivot *lb_pivot =
trunk_pivot_create(hid, lb, 0, 0, TRUNK_STATS_ZERO, TRUNK_STATS_ZERO);
trunk_pivot *ub_pivot =
trunk_pivot_create(hid, ub, 0, 0, TRUNK_STATS_ZERO, TRUNK_STATS_ZERO);
if (lb_pivot == NULL || ub_pivot == NULL) {
platform_error_log(
"%s():%d: pivot_create() failed. lb_pivot=%p ub_pivot=%p",
__func__,
__LINE__,
lb_pivot,
ub_pivot);
rc = STATUS_NO_MEMORY;
goto cleanup_pivots;
}
rc = vector_append(&pivots, lb_pivot);
platform_assert_status_ok(rc);
rc = vector_append(&pivots, ub_pivot);
platform_assert_status_ok(rc);
rc = VECTOR_EMPLACE_APPEND(&pivot_bundles, bundle_init, hid);
platform_assert_status_ok(rc);
trunk_node_init(node, 0, pivots, pivot_bundles, 0, inflight_bundles);
return STATUS_OK;
cleanup_pivots:
if (lb_pivot != NULL) {
trunk_pivot_destroy(lb_pivot, hid);
}
if (ub_pivot != NULL) {
trunk_pivot_destroy(ub_pivot, hid);
}
cleanup_vectors:
VECTOR_APPLY_TO_ELTS(&pivots, trunk_pivot_destroy, hid);
vector_deinit(&pivots);
VECTOR_APPLY_TO_PTRS(&pivot_bundles, bundle_deinit);
vector_deinit(&pivot_bundles);
vector_deinit(&inflight_bundles);
return rc;
}
static uint64
trunk_node_num_children(const trunk_node *node)
{
return vector_length(&node->pivots) - 1;
}
static trunk_pivot *
trunk_node_pivot(const trunk_node *node, uint64 i)
{
return vector_get(&node->pivots, i);
}
static key
trunk_node_pivot_key(const trunk_node *node, uint64 i)
{
return trunk_pivot_key(vector_get(&node->pivots, i));
}
static key
trunk_node_pivot_min_key(const trunk_node *node)
{
return trunk_pivot_key(vector_get(&node->pivots, 0));
}
debug_only static key
trunk_node_pivot_max_key(const trunk_node *node)
{
return trunk_pivot_key(
vector_get(&node->pivots, vector_length(&node->pivots) - 1));
}
static bundle *
trunk_node_pivot_bundle(trunk_node *node, uint64 i)
{
return vector_get_ptr(&node->pivot_bundles, i);
}
static uint64
trunk_node_height(const trunk_node *node)
{
return node->height;
}
static bool32
trunk_node_is_leaf(const trunk_node *node)
{
return node->height == 0;
}
static uint64
trunk_node_first_live_inflight_bundle(const trunk_node *node)
{
uint64 result = UINT64_MAX;
for (uint64 i = 0; i < vector_length(&node->pivots) - 1; i++) {
trunk_pivot *pvt = vector_get(&node->pivots, i);
result = MIN(result, pvt->inflight_bundle_start);
}
return result;
}
static uint64
trunk_leaf_num_tuples(const trunk_node *node)
{
trunk_pivot_stats stats =
trunk_pivot_get_stats(vector_get(&node->pivots, 0));
return stats.num_tuples;
}
static uint64
trunk_leaf_num_kv_bytes(const trunk_node *node)
{
trunk_pivot_stats stats =
trunk_pivot_get_stats(vector_get(&node->pivots, 0));
return stats.num_kv_bytes;
}
static uint64
trunk_node_num_old_bundles(const trunk_node *node)
{
return node->num_old_bundles;
}
static bool32
trunk_node_pivot_has_received_bundles(const trunk_node *node, uint64 i)
{
trunk_pivot *pvt = vector_get(&node->pivots, i);
return trunk_pivot_inflight_bundle_start(pvt) <= node->num_old_bundles
&& node->num_old_bundles < vector_length(&node->inflight_bundles);
}
void
trunk_node_print(const trunk_node *node,
platform_log_handle *log,
const data_config *data_cfg,
int indent)
{
platform_log(
log, "%*sNode height: %lu\n", indent, "", trunk_node_height(node));
platform_log(
log, "%*sNum old bundles: %lu\n", indent, "", node->num_old_bundles);
platform_log(log, "%*s--------------Pivots-----------\n", indent, "");
trunk_pivot_vector_print(&node->pivots, log, data_cfg, indent + 4);
platform_log(log, "%*s--------------Pivot Bundles-----------\n", indent, "");
bundle_vector_print(&node->pivot_bundles, log, indent + 4);
platform_log(
log, "%*s--------------Inflight Bundles-----------\n", indent, "");
bundle_vector_print(&node->inflight_bundles, log, indent + 4);
}
debug_only static bool
trunk_node_is_well_formed_leaf(const data_config *data_cfg,
const trunk_node *node)
{
bool basics =
node->height == 0 && vector_length(&node->pivots) == 2
&& vector_length(&node->pivot_bundles) == 1
&& node->num_old_bundles <= vector_length(&node->inflight_bundles);
if (!basics) {
platform_error_log("ILL-FORMED LEAF: basics failed\n");
trunk_node_print(node, Platform_error_log_handle, data_cfg, 4);
return FALSE;
}
trunk_pivot *lb = vector_get(&node->pivots, 0);
trunk_pivot *ub = vector_get(&node->pivots, 1);
key lbkey = trunk_pivot_key(lb);
key ubkey = trunk_pivot_key(ub);
bool32 ret =
lb->child_addr == 0 && data_key_compare(data_cfg, lbkey, ubkey) < 0;
if (!ret) {
platform_error_log("ILL-FORMED LEAF:\n");
trunk_node_print(node, Platform_error_log_handle, data_cfg, 4);
}
return ret;
}
debug_only static bool
trunk_node_is_well_formed_index(const data_config *data_cfg,
const trunk_node *node)
{
bool basics =
0 < node->height && 1 < vector_length(&node->pivots)
&& vector_length(&node->pivot_bundles) == vector_length(&node->pivots) - 1
&& node->num_old_bundles <= vector_length(&node->inflight_bundles);
if (!basics) {
platform_error_log("ILL-FORMED INDEX: basics failed\n");
trunk_node_print(node, Platform_error_log_handle, data_cfg, 4);
return FALSE;
}
for (uint64 i = 0; i < trunk_node_num_children(node); i++) {
trunk_pivot *lb = vector_get(&node->pivots, i);
trunk_pivot *ub = vector_get(&node->pivots, i + 1);
key lbkey = trunk_pivot_key(lb);
key ubkey = trunk_pivot_key(ub);
bool valid_pivots =
lb->child_addr != 0
&& lb->inflight_bundle_start <= vector_length(&node->inflight_bundles)
&& data_key_compare(data_cfg, lbkey, ubkey) < 0
&& trunk_pivot_stats_are_nonnegative(lb->prereceive_stats)
&& trunk_pivot_stats_are_nonnegative(lb->stats);
if (!valid_pivots) {
platform_error_log("ILL-FORMED INDEX: invalid pivots\n");
trunk_node_print(node, Platform_error_log_handle, data_cfg, 4);
return FALSE;
}
}
return TRUE;
}
static void
trunk_node_deinit(trunk_node *node, const trunk_context *context)
{
VECTOR_APPLY_TO_ELTS(
&node->pivots, vector_apply_platform_free, PROCESS_PRIVATE_HEAP_ID);
VECTOR_APPLY_TO_PTRS(&node->pivot_bundles, bundle_deinit);
VECTOR_APPLY_TO_PTRS(&node->inflight_bundles, bundle_deinit);
vector_deinit(&node->pivots);
vector_deinit(&node->pivot_bundles);
vector_deinit(&node->inflight_bundles);
}
/**************************************************
* Basic accessors for ondisk bundles
**************************************************/
static uint64
sizeof_trunk_ondisk_bundle(trunk_ondisk_bundle *odb)
{
return sizeof(*odb) + sizeof(odb->branches[0]) * odb->num_branches;
}
static uint64
trunk_ondisk_bundle_size(uint64 num_branches)
{
return sizeof(trunk_ondisk_bundle) + sizeof(branch_ref) * num_branches;
}
static page_type
trunk_ondisk_bundle_branch_type(const trunk_ondisk_bundle *odb)
{
return routing_filters_equal(&odb->maplet, &NULL_ROUTING_FILTER)
&& odb->num_branches == 1
? PAGE_TYPE_BRANCH
: PAGE_TYPE_BRANCH;
}
/****************************************************
* Basic accessors for ondisk pivots
****************************************************/
static uint64
sizeof_trunk_ondisk_pivot(trunk_ondisk_pivot *odp)
{
return sizeof(*odp) + sizeof_ondisk_key_data(&odp->key);
}
static uint64
trunk_ondisk_pivot_size(key k)
{
return sizeof(trunk_ondisk_pivot) + ondisk_key_required_data_capacity(k);
}
static key
trunk_ondisk_pivot_key(trunk_ondisk_pivot *odp)
{
return ondisk_key_to_key(&odp->key);
}
static trunk_ondisk_bundle *
trunk_ondisk_pivot_bundle(trunk_ondisk_pivot *odp)
{
return (trunk_ondisk_bundle *)((char *)odp + sizeof_trunk_ondisk_pivot(odp));
}
/********************************************************
* Node serialization/deserialization and refcounting.
********************************************************/
static platform_status
trunk_ondisk_node_handle_init(trunk_ondisk_node_handle *handle,
cache *cc,
uint64 addr)
{
platform_assert(addr != 0);
handle->cc = cc;
handle->header_page = cache_get(cc, addr, TRUE, PAGE_TYPE_TRUNK);
if (handle->header_page == NULL) {
platform_error_log("%s():%d: cache_get() failed", __func__, __LINE__);
return STATUS_IO_ERROR;
}
handle->pivot_page = NULL;
handle->inflight_bundle_page = NULL;
return STATUS_OK;
}
/*
* IN Parameters:
* - state->context: the trunk_node_context
* - state->pivot->child_addr: the address of the node
*
* OUT Parameters:
* - state->child_handle: the ondisk_node_handle
* - state->rc: the return code
*/
static async_status
trunk_ondisk_node_handle_init_async(trunk_merge_lookup_async_state *state,
uint64 depth)
{
async_begin(state, depth);
platform_assert(state->pivot->child_addr != 0);
state->child_handle.cc = state->context->cc;