forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.c
8535 lines (7358 loc) · 277 KB
/
cursor.c
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 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
THIS IS THE FILE FORMERLY KNOWN AS BDB_SQL.C
it's cursor.c now to reflect the new intent. this file
implements the bdb_cursor object. this object is
our cursor abstraction. it should be able to be backed
by real berkeley cursors, by the "fetch.c" codepath (formerly 'ix mode'),
by remote databases through marhsalling, whatever. calling code in db
should use bdb_cursors and open them with the correct intent.
for now, none of this exists.
all routines in this file DO NOT GET the bdb lock.
they assume it is ALREADY BEING HELD by a cutran or tran
or whatever you allocated already
*/
/*
NOTES ON THE NEW BDB CURSOR
Assumptions:
- using data/datalen/genid
will give you the data and its genid for both data and
indexes
- data == NULL means not positioned (or empty) btree
- data/datalen/genid are changed only by a successful
move in the btree/shadow
- a cursor points to a row in the btree or its shadow,
as long as there was a successful move in the past
(ie. btree was not empty);
*/
/* TODO:
1) additional pointer to index-inline data
2) allocate DBTs to match row sizes
*/
#ifdef __sun
/* for PTHREAD_STACK_MIN on Solaris */
# define __EXTENSIONS__
#endif
#ifdef NEWSI_STAT
#include <time.h>
#include <sys/time.h>
#include <util.h>
#endif
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <strings.h>
#include <assert.h>
#include <poll.h>
#include <netinet/in.h>
#include <build/db.h>
#include <fsnap.h>
#include <ctrace.h>
#include "net.h"
#include "bdb_int.h"
#include "bdb_cursor.h"
#include "locks.h"
#include "locks_wrap.h"
#include "bdb_osqlcur.h"
#include "bdb_osqllog.h"
#include "bdb_osqltrn.h"
#include <dlfcn.h>
#include <list.h>
#include <plhash.h>
#include "logmsg.h"
#include "util.h"
#include "tohex.h"
#include "genid.h"
#define MERGE_DEBUG (0)
struct datacopy_info {
void *datacopy;
int size;
};
pthread_mutex_t pr_lk = PTHREAD_MUTEX_INITIALIZER;
int lkprintf(loglvl lvl, const char *fmt, ...)
{
va_list args;
Pthread_mutex_lock(&pr_lk);
va_start(args, fmt);
logmsgv(lvl, fmt, args);
va_end(args);
Pthread_mutex_unlock(&pr_lk);
return 0;
}
/* switch a cursor to a different dta file
we add ONE virtual data stripe,
indexed dtastripe (where add/upd are stored)
*/
#define IS_VALID_DTA(id) \
((id) >= 0 && (((id) < cur->state->attr->dtastripe) || \
((id) == cur->state->attr->dtastripe && cur->addcur)))
hash_t *logfile_pglogs_repo = NULL;
static unsigned first_logfile;
static unsigned last_logfile;
static int logfile_pglogs_repo_ready = 0;
static pthread_mutex_t logfile_pglogs_repo_mutex;
#ifdef NEWSI_STAT
struct timeval logfile_relink_time;
struct timeval logfile_pglog_time;
struct timeval ltran_pglog_time;
struct timeval txn_pglog_time;
struct timeval queue_pglog_time;
struct timeval queue_relink_time;
struct timeval logical_undo_time;
struct timeval client_copy_time;
struct timeval log_read_time;
struct timeval log_read_time2;
struct timeval log_apply_time;
struct timeval comprec_time;
unsigned long long num_log_read = 0;
unsigned long long num_log_applied_opt = 0;
unsigned long long num_log_applied_unopt = 0;
unsigned long long num_comprec = 0;
#endif
struct temp_table *bdb_gbl_timestamp_lsn;
pthread_mutex_t bdb_gbl_timestamp_lsn_mutex;
tmpcursor_t *bdb_gbl_timestamp_lsn_cur;
int bdb_gbl_timestamp_lsn_ready = 0;
extern int gbl_newsi_use_timestamp_table;
hash_t *bdb_gbl_ltran_pglogs_hash;
pthread_mutex_t bdb_gbl_ltran_pglogs_mutex;
int bdb_gbl_ltran_pglogs_hash_ready = 0;
int bdb_gbl_ltran_pglogs_hash_processed = 0;
extern pthread_mutex_t bdb_gbl_recoverable_lsn_mutex;
extern DB_LSN bdb_gbl_recoverable_lsn;
extern int32_t bdb_gbl_recoverable_timestamp;
static hash_t *bdb_asof_cursor_hash;
extern pthread_mutex_t bdb_asof_current_lsn_mutex;
extern DB_LSN bdb_asof_current_lsn;
extern DB_LSN bdb_latest_commit_lsn;
extern uint32_t bdb_latest_commit_gen;
extern pthread_cond_t bdb_asof_current_lsn_cond;
extern int db_is_stopped(void);
static int bdb_switch_stripe(bdb_cursor_impl_t *cur, int dtafile, int *bdberr);
static int bdb_cursor_find_merge(bdb_cursor_impl_t *cur, void *key, int keylen,
int *bdberr);
static int bdb_cursor_move(bdb_cursor_impl_t *cur, int how, int *bdberr);
static int bdb_btree_merge(bdb_cursor_impl_t *cur, int stripe_rl, int page_rl,
int index_rl, char *data_rl, int datalen_rl,
char *data_sd, int datalen_sd, char *key_rl,
int keylen_rl, char *key_sd, int keylen_sd,
unsigned long long genid_rl,
unsigned long long genid_sd, uint8_t ver_rl,
int how);
static int bdb_btree_update_shadows(bdb_cursor_impl_t *cur, int how,
int *bdberr);
static int bdb_btree_update_shadows_with_pglogs_int(bdb_cursor_impl_t *cur,
db_pgno_t *inpgno,
unsigned char *infileid,
int *bdberr);
/* local bdb cursor functionality */
static int bdb_cursor_first(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_next(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_prev(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_last(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_find(bdb_cursor_ifn_t *cur, void *key, int keylen,
int dirLeft, int *bdberr);
static int bdb_cursor_find_last_dup(bdb_cursor_ifn_t *, void *key, int keylen,
int keymax, bias_info *, int *bdberr);
static int bdb_cursor_close(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_getpageorder(bdb_cursor_ifn_t *pcur_ifn);
static int bdb_cursor_update_shadows(bdb_cursor_ifn_t *pcur_ifn, int *bdberr);
static void *bdb_cursor_get_shadowtran(bdb_cursor_ifn_t *pcur_ifn);
static int bdb_cursor_update_shadows_with_pglogs(bdb_cursor_ifn_t *pcur_ifn,
unsigned *inpgno,
unsigned char *infileid,
int *bdberr);
static int bdb_cursor_set_null_blob_in_shadows(bdb_cursor_ifn_t *pcur_ifn,
unsigned long long genid,
int dbnum, int blobno,
int *bdberr);
static int bdb_cursor_pause(bdb_cursor_ifn_t *pcur_ifn, int *bdberr);
static void *bdb_cursor_data(bdb_cursor_ifn_t *cur);
static int bdb_cursor_datalen(bdb_cursor_ifn_t *cur);
static unsigned long long bdb_cursor_genid(bdb_cursor_ifn_t *cur);
static int bdb_cursor_rrn(bdb_cursor_ifn_t *cur);
static int bdb_cursor_dbnum(bdb_cursor_ifn_t *cur);
static void *bdb_cursor_datacopy(bdb_cursor_ifn_t *cur);
static uint8_t bdb_cursor_ver(bdb_cursor_ifn_t *cur);
static void bdb_cursor_found_data(struct bdb_cursor_ifn *cur, int *rrn,
unsigned long long *genid, int *datalen,
void **data, uint8_t *ver);
static void *bdb_cursor_collattr(bdb_cursor_ifn_t *cur);
static int bdb_cursor_collattrlen(bdb_cursor_ifn_t *cur);
static int bdb_cursor_insert(bdb_cursor_ifn_t *cur, unsigned long long genid,
void *data, int datalen, void *datacopy,
int datacopylen, int *bdberr);
static int bdb_cursor_delete(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_unlock(bdb_cursor_ifn_t *cur, int *bdberr);
static int bdb_cursor_lock(bdb_cursor_ifn_t *cur, cursor_tran_t *curtran,
int how, int *bdberr);
static int bdb_cursor_set_curtran(bdb_cursor_ifn_t *cur,
cursor_tran_t *curtran);
static inline int calculate_discard_pages(bdb_cursor_impl_t *cur);
static void *unpack_datacopy_odh(bdb_cursor_ifn_t *cur, uint8_t *to,
int to_size, uint8_t *from, int from_size,
uint8_t *ver);
static int bdb_cursor_reposition(bdb_cursor_ifn_t *pcur_ifn, int how,
int *bdberr);
static int bdb_cursor_reposition_noupdate(bdb_cursor_ifn_t *pcur_ifn,
bdb_berkdb_t *berkdb, char *key,
int keylen, int how, int *bdberr);
static int bdb_cursor_move_and_skip_int(bdb_cursor_impl_t *cur,
bdb_berkdb_t *berkdb, int how,
int retrieved, int update_shadows,
int *bdberr);
static inline int berkdb_get_genid_from_dtakey(bdb_cursor_impl_t *cur,
void *dta, void *key,
unsigned long long *genid,
int *bdberr)
{
void *val;
if (cur->type == BDBC_DT)
val = key;
else
val = dta;
#ifdef _SUN_SOURCE
memcpy(genid, val, 8);
#else
*genid = *(unsigned long long *)val;
#endif
return 0;
}
static char *tellmehow(int how)
{
switch (how) {
case DB_NEXT:
return "NEXT";
case DB_PREV:
return "PREV";
case DB_FIRST:
return "FIRST";
case DB_LAST:
return "LAST";
default:
return "UNKNOWN";
}
}
#if MERGE_DEBUG
enum { BDB_SHOW_RL = 1, BDB_SHOW_SD = 2, BDB_SHOW_BOTH = 3 };
static void print_cursor_keys(bdb_cursor_impl_t *cur, int which)
{
int bdberr;
if (which & BDB_SHOW_RL && cur->rl) {
logmsg(LOGMSG_USER, "ptr in RL: \n");
char *loc_key_rl = NULL;
int loc_keysize_rl = 0;
cur->rl->key(cur->rl, &loc_key_rl, &bdberr);
cur->rl->keysize(cur->rl, &loc_keysize_rl, &bdberr);
hexdump(LOGMSG_USER, loc_key_rl, loc_keysize_rl);
logmsg(LOGMSG_USER, "\n");
}
if (which & BDB_SHOW_SD && cur->sd) {
logmsg(LOGMSG_USER, "ptr in SD: \n");
char *loc_key_sd = NULL;
int loc_keysize_sd = 0;
cur->sd->key(cur->sd, &loc_key_sd, &bdberr);
cur->sd->keysize(cur->sd, &loc_keysize_sd, &bdberr);
hexdump(LOGMSG_USER, loc_key_sd, loc_keysize_sd);
logmsg(LOGMSG_USER, "\n");
}
}
#endif
static inline int calculate_discard_pages(bdb_cursor_impl_t *cur)
{
bdb_state_type *bdb_state = cur->state;
DB_ENV *dbenv;
db_pgno_t numpages;
u_int32_t pagesize;
u_int32_t gb;
u_int32_t bytes;
u_int64_t tablesize = 0;
u_int64_t bpoolsize;
u_int32_t utilization;
DB *db = NULL;
int ii;
/* Short circuit if we can consume the entire bufferpool. */
if (bdb_state->attr->tablescan_cache_utilization == 100)
return 0;
/* Grab the dbenv. */
dbenv = cur->state->dbenv;
/* Calculate the buffer pool size. */
dbenv->get_cachesize(dbenv, &gb, &bytes, NULL);
#define GIGABYTE (1024 * 1024 * 1024ULL) // for alex: 1024 ^ 3
bpoolsize = gb * GIGABYTE + bytes;
/* Calculate the table size. */
for (ii = 0; ii < cur->state->attr->dtastripe; ii++) {
db = bdb_state->dbp_data[0][ii];
db->get_numpages(db, &numpages);
db->get_pagesize(db, &pagesize);
tablesize += (numpages * pagesize);
}
/* Calculate what the table will utilize. */
utilization = (100 * tablesize) / bpoolsize;
return (utilization > bdb_state->attr->tablescan_cache_utilization);
}
/* Make sure an expanding recordsize doesn't spill onto overflow pages. */
static inline int allow_pageorder_tablescan(bdb_cursor_impl_t *cur)
{
bdb_state_type *bdb_state = cur->state;
DB *db = bdb_state->dbp_data[0][0];
u_int32_t pagesize;
u_int32_t recordsize;
/* Verify that it's enabled for this table */
if (bdb_state->disable_page_order_tablescan)
return 0;
/* Disable for snapshot / serializable for now */
if (cur->shadow_tran &&
(cur->shadow_tran->tranclass == TRANCLASS_SNAPISOL ||
cur->shadow_tran->tranclass == TRANCLASS_SERIALIZABLE))
return 0;
/* Short circuit if this check is disabled. */
if (cur->state->attr->disable_pageorder_recsz_chk)
return 1;
/* Retrive the pagesize for this table. */
db->get_pagesize(db, &pagesize);
/* Subtract maximum possible header: base + cksum + crypto + align + fudge.
*/
pagesize -= (26 + 20 + 16 + 2 + 8);
/* Start with record size. */
recordsize = bdb_state->lrl;
/* Add space for the genid key. */
recordsize += sizeof(unsigned long long);
/* Add space for the index for each + fudge: the index is actually a short.
*/
recordsize += (2 * sizeof(int));
/* Allow pageorder if we can fit at least 4 records on a page. */
return (pagesize / recordsize) >= 4;
}
/* Trace flag */
extern int gbl_skip_ratio_trace;
/* Print page-order skip trace */
static inline int pageorder_skip_trace(bdb_cursor_impl_t *cur)
{
u_int64_t nextcount;
u_int64_t skipcount;
u_int64_t ratio = 0;
static int lastpr = 0;
int interval = 1;
int now;
int rc;
/* Get time */
now = time(NULL);
/* Return if too early */
if ((now - lastpr) < interval)
return 0;
/* Remember time */
lastpr = now;
/* Disabled globally */
if (!cur->state->attr->page_order_tablescan) {
logmsg(LOGMSG_USER, "%s: global page order tablescan is disabled\n",
__func__);
return 0;
}
/* Disabled for table */
if (cur->state->disable_page_order_tablescan) {
logmsg(LOGMSG_USER, "%s: page order tablescan is disabled for table '%s'.\n",
__func__, cur->state->name);
return 0;
}
/* Check cursor */
if (!cur->rl) {
logmsg(LOGMSG_USER, "%s: NULL cursor for table '%s'\n", __func__,
cur->state->name);
return 0;
}
/* Check max threshold */
if (cur->state->attr->disable_pgorder_threshold >= 100) {
logmsg(LOGMSG_USER, "%s: threshold is 100%% for table '%s'\n", __func__,
cur->state->name);
return 0;
}
/* Irrelavant skip-stats */
if ((rc = cur->rl->get_skip_stat(cur->rl, &nextcount, &skipcount)) != 0) {
logmsg(LOGMSG_USER, "%s: get_skip_stat returned %d\n", __func__, rc);
return 0;
}
/* Skip to next ratio */
if (nextcount > 0)
ratio = (100 * skipcount) / nextcount;
/* Sanity */
assert(ratio >= 0 && ratio <= 100);
/* Not enough pages */
if (nextcount < cur->state->attr->disable_pgorder_min_nexts) {
logmsg(LOGMSG_USER, "%s: below next threshold for table '%s'\n", __func__,
cur->state->name);
}
logmsg(LOGMSG_USER,
"Table scan for table '%s' skipcount = %lu nextcount = %lu "
"ratio = %lu (threshold = %d)\n",
cur->state->name, skipcount, nextcount, ratio,
cur->state->attr->disable_pgorder_threshold);
return 0;
}
/* Analyze page-order skips at the end of a datafile traversal */
static inline int verify_pageorder_tablescan(bdb_cursor_impl_t *cur)
{
u_int64_t nextcount;
u_int64_t skipcount;
u_int64_t ratio = 0;
int rc;
/* Print trace if enabled */
if (gbl_skip_ratio_trace)
pageorder_skip_trace(cur);
/* Page-order tablescan already disabled */
if (!cur->state->attr->page_order_tablescan)
return 0;
/* Return immediately if this is disabled */
if (cur->state->disable_page_order_tablescan)
return 0;
/* Disable switch disabled */
if (!cur->rl || cur->state->attr->disable_pgorder_threshold >= 100)
return 0;
/* Irrelavant skip-stats */
if ((rc = cur->rl->get_skip_stat(cur->rl, &nextcount, &skipcount)) != 0)
return 0;
/* Skip to next ratio */
if (nextcount > 0)
ratio = (100 * skipcount) / nextcount;
/* Sanity */
assert(ratio >= 0 && ratio <= 100);
/* Not enough pages */
if (nextcount < cur->state->attr->disable_pgorder_min_nexts)
return 0;
/* Ratio of skips to nexts */
if (ratio > cur->state->attr->disable_pgorder_threshold) {
logmsg(LOGMSG_WARN,
"Disable page-order tablescan for table %s skipcount = "
"%lu nextcount = %lu ratio = %lu%% threshold = %d%%\n",
cur->state->name, skipcount, nextcount, ratio,
cur->state->attr->disable_pgorder_threshold);
/* Disable pageorder tablescan */
cur->state->disable_page_order_tablescan = 1;
cur->pageorder = 0;
cur->discardpages = 0;
return 1;
}
return 0;
}
int bdb_set_check_shadows(tran_type *shadow_tran)
{
shadow_tran->check_shadows = 1;
return 0;
}
/**
* ixnum == -1 of BDBC_DT, ixnum >= 0 for BDBC_IX
*
*/
bdb_cursor_ifn_t *bdb_cursor_open(
bdb_state_type *bdb_state, cursor_tran_t *curtran, tran_type *shadow_tran,
int ixnum, enum bdb_open_type type, void *shadadd, int pageorder,
int rowlocks, int *holding_pagelocks_flag,
int (*pause_pagelock_cursors)(void *), void *pausearg,
int (*count_cursors)(void *), void *countarg, int trak, int *bdberr)
{
bdb_cursor_ifn_t *pcur_ifn = NULL;
bdb_cursor_impl_t *cur = NULL;
int maxdta = 0;
int maxkey = 0;
int rc = 0;
int dbnum = 0;
/* this bdb state has to be a table, not an env */
if (!bdb_state->parent) {
logmsg(LOGMSG_ERROR, "%s: calling this for parent bdb_state\n", __func__);
cheap_stack_trace();
*bdberr = BDBERR_BADARGS;
return NULL;
}
/* we expect to always be running with a curtran these days */
if (!curtran || !bdb_get_lid_from_cursortran(curtran)) {
logmsg(LOGMSG_ERROR, "%s: no curtran provided!!!\n", __func__);
cheap_stack_trace();
*bdberr = BDBERR_BADARGS;
return NULL;
}
dbnum = get_dbnum_by_handle(bdb_state);
if (dbnum == -1) {
*bdberr = BDBERR_BADARGS;
return NULL;
}
pcur_ifn = calloc(1, sizeof(bdb_cursor_ifn_t) + sizeof(bdb_cursor_impl_t));
if (!pcur_ifn) {
logmsg(LOGMSG_ERROR, "%s: malloc %zu\n", __func__,
sizeof(bdb_cursor_impl_t));
*bdberr = BDBERR_MALLOC;
return NULL;
}
cur = (bdb_cursor_impl_t *)((char *)pcur_ifn + sizeof(bdb_cursor_ifn_t));
/* set this to 0 to get repeatable reads in row lock mode */
cur->state = bdb_state;
cur->curtran = curtran;
cur->shadow_tran = shadow_tran;
cur->dbnum = dbnum;
cur->used_rl = 1;
cur->used_sd = 1;
cur->pagelockflag = holding_pagelocks_flag;
cur->laststripe = cur->lastpage = cur->lastindex = -1;
rowlocks = cur->rowlocks = 0;
cur->upd_shadows_count = 0;
cur->trak = trak | ((shadow_tran) ? shadow_tran->trak : 0);
if (cur->trak && shadow_tran) {
logmsg(LOGMSG_USER, "Cur %p opened as tranclass %d startgenid %llx\n", cur,
cur->shadow_tran->tranclass, cur->shadow_tran->startgenid);
} else if (cur->trak && !shadow_tran) {
logmsg(LOGMSG_USER, "Cur %p opened with no shadow tran\n", cur);
}
if (bdb_state->isopen == 0)
return NULL;
if (ixnum >= 0) {
cur->idx = ixnum;
cur->type = BDBC_IX;
if (bdb_state->ixdta[ixnum]) {
cur->datacopy =
malloc(bdb_state->lrl + 2 * sizeof(unsigned long long));
if (!cur->datacopy) {
logmsg(LOGMSG_ERROR, "%s: malloc %zu\n", __func__,
bdb_state->lrl + 2 * sizeof(unsigned long long));
*bdberr = BDBERR_MALLOC;
return NULL;
}
}
} else {
cur->idx = 0;
cur->type = BDBC_DT;
}
/* aparently the limits must be the same for index and data
(obviously they don't have to)
this will fix the datacopy issue until we fix the length
to more accurate values
*/
maxdta = MAXRECSZ;
maxkey = MAXKEYSZ;
/* gear up the bdb cursor
Do it here, before trying to open berkdb objects
that will might try to use cur->ifn
*/
pcur_ifn->impl = cur;
pcur_ifn->first = bdb_cursor_first;
pcur_ifn->next = bdb_cursor_next;
pcur_ifn->prev = bdb_cursor_prev;
pcur_ifn->last = bdb_cursor_last;
pcur_ifn->find = bdb_cursor_find;
pcur_ifn->find_last_dup = bdb_cursor_find_last_dup;
pcur_ifn->insert = bdb_cursor_insert;
pcur_ifn->delete = bdb_cursor_delete;
pcur_ifn->data = bdb_cursor_data;
pcur_ifn->datalen = bdb_cursor_datalen;
pcur_ifn->genid = bdb_cursor_genid;
pcur_ifn->rrn = bdb_cursor_rrn;
pcur_ifn->dbnum = bdb_cursor_dbnum;
pcur_ifn->datacopy = bdb_cursor_datacopy;
pcur_ifn->ver = bdb_cursor_ver;
pcur_ifn->get_found_data = bdb_cursor_found_data;
pcur_ifn->collattr = bdb_cursor_collattr;
pcur_ifn->collattrlen = bdb_cursor_collattrlen;
pcur_ifn->unlock = bdb_cursor_unlock;
pcur_ifn->lock = bdb_cursor_lock;
pcur_ifn->set_curtran = bdb_cursor_set_curtran;
pcur_ifn->getpageorder = bdb_cursor_getpageorder;
pcur_ifn->updateshadows = bdb_cursor_update_shadows;
pcur_ifn->updateshadows_pglogs = bdb_cursor_update_shadows_with_pglogs;
pcur_ifn->getshadowtran = bdb_cursor_get_shadowtran;
pcur_ifn->setnullblob = bdb_cursor_set_null_blob_in_shadows;
pcur_ifn->pause = bdb_cursor_pause;
pcur_ifn->pauseall = pause_pagelock_cursors;
pcur_ifn->pausearg = pausearg;
pcur_ifn->count = count_cursors;
pcur_ifn->countarg = countarg;
pcur_ifn->close = bdb_cursor_close;
cur->ifn = pcur_ifn;
/* Determine the page-order flags at the first open. */
if (cur->type == BDBC_DT && cur->idx == 0 && pageorder &&
allow_pageorder_tablescan(cur)) {
cur->pageorder = 1;
cur->discardpages = calculate_discard_pages(cur);
if (cur->trak) {
logmsg(LOGMSG_USER, "Cur %p opened in page-order mode.\n", cur);
}
}
if (type == BDB_OPEN_REAL || type == BDB_OPEN_BOTH ||
type == BDB_OPEN_BOTH_CREATE) {
cur->rl = bdb_berkdb_open(cur, BERKDB_REAL, maxdta, maxkey, bdberr);
if (!cur->rl) {
logmsg(LOGMSG_ERROR, "%s: fail to create index berkdb\n", __func__);
if (cur->datacopy)
free(cur->datacopy);
free(pcur_ifn);
return NULL;
}
if (cur->trak) {
logmsg(LOGMSG_USER, "Cur %p opened cur->rl in bdb_cursor_open\n", cur);
}
}
/* we only open if there is already one (we'll open when we insert, if any)
*/
if (type == BDB_OPEN_SHAD || type == BDB_OPEN_BOTH ||
type == BDB_OPEN_BOTH_CREATE) {
int openhow = BERKDB_SHAD;
/* Always create cur->sd for non-page order snapisol or you can lose
* data in cursor_move_merge. */
if (cur->shadow_tran &&
(cur->shadow_tran->tranclass == TRANCLASS_SNAPISOL ||
cur->shadow_tran->tranclass == TRANCLASS_SERIALIZABLE ||
type == BDB_OPEN_BOTH_CREATE))
openhow = BERKDB_SHAD_CREATE;
/* open the cursor now */
cur->sd = bdb_berkdb_open(cur, openhow, maxdta, maxkey, bdberr);
if (*bdberr) {
logmsg(LOGMSG_ERROR, "%s: fail to create shadow index berkdb\n",
__func__);
if (cur->rl) {
int newbdberr;
rc = cur->rl->close(cur->rl, &newbdberr);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: bdb_berkdb_close failed %d %d\n",
__func__, rc, newbdberr);
}
}
if (cur->datacopy)
free(cur->datacopy);
free(pcur_ifn);
return NULL;
}
if (cur->trak) {
logmsg(LOGMSG_USER, "Cur %p opened cur->sd in bdb_cursor_open\n", cur);
}
} else if (cur->trak) {
logmsg(LOGMSG_USER, "Cur %p did not open cur->sd because type is %d\n", cur,
type);
}
/* A cursor into the virtual stripe. */
cur->addcur = shadadd;
/* Each cursor needs it's own handle to traverse the virtual stripe. */
/* Check if this requires odh logic. */
if (cur->type == BDBC_DT) {
/* Allocate the addcur odh area. */
cur->addcur_odh = malloc(MAXRECSZ);
/* Mark as an odh cursor. */
cur->addcur_use_odh = 1;
}
/* Add this to the list of cursors under this txn. */
if (cur->shadow_tran &&
(cur->shadow_tran->tranclass == TRANCLASS_SNAPISOL ||
cur->shadow_tran->tranclass == TRANCLASS_SERIALIZABLE)) {
listc_abl(&cur->shadow_tran->open_cursors, pcur_ifn);
}
/* Start with a NULL skip-list. */
cur->vs_stab = NULL;
cur->vs_skip = NULL;
#if 0
cur->cstripe = NULL; cur->cscur = NULL;
#endif
return pcur_ifn;
}
int timestamp_lsn_keycmp(void *_, int key1len, const void *key1, int key2len,
const void *key2)
{
struct timestamp_lsn_key *pkey1;
struct timestamp_lsn_key *pkey2;
assert(key1len == key2len);
pkey1 = (struct timestamp_lsn_key *)key1;
pkey2 = (struct timestamp_lsn_key *)key2;
if (pkey1->timestamp != pkey2->timestamp) {
if (pkey1->timestamp < pkey2->timestamp) {
return -1;
} else {
return 1;
}
} else if (pkey1->lsn.file != pkey2->lsn.file) {
if (pkey1->lsn.file < pkey2->lsn.file) {
return -1;
} else {
return 1;
}
} else {
if (pkey1->lsn.offset < pkey2->lsn.offset) {
return -1;
} else if (pkey1->lsn.offset == pkey2->lsn.offset) {
return 0;
} else {
return 1;
}
}
return 0;
}
static LISTC_T(struct commit_list) pglogs_commit_list;
static hash_t *pglogs_queue_fileid_hash;
static pthread_mutex_t pglogs_queue_lk;
static pool_t *fileid_pglogs_queue_pool = NULL;
static pool_t *pglogs_queue_cursor_pool = NULL;
static pool_t *ltran_pglogs_key_pool = NULL;
static pool_t *asof_cursor_pool = NULL;
static pool_t *pglogs_commit_list_pool = NULL;
static pool_t *pglogs_queue_key_pool = NULL;
static pool_t *pglogs_key_pool = NULL;
static pool_t *pglogs_logical_key_pool = NULL;
static pool_t *pglogs_lsn_list_pool = NULL;
static pool_t *pglogs_lsn_commit_list_pool = NULL;
static pool_t *pglogs_relink_key_pool = NULL;
static pool_t *pglogs_relink_list_pool = NULL;
static pthread_mutex_t fileid_pglogs_queue_pool_lk;
static pthread_mutex_t pglogs_queue_cursor_pool_lk;
static pthread_mutex_t ltran_pglogs_key_pool_lk;
static pthread_mutex_t asof_cursor_pool_lk;
static pthread_mutex_t pglogs_commit_list_pool_lk;
static pthread_mutex_t pglogs_queue_key_pool_lk;
static pthread_mutex_t pglogs_key_pool_lk;
static pthread_mutex_t pglogs_logical_key_pool_lk;
static pthread_mutex_t pglogs_lsn_list_pool_lk;
static pthread_mutex_t pglogs_lsn_commit_list_pool_lk;
static pthread_mutex_t pglogs_relink_key_pool_lk;
static pthread_mutex_t pglogs_relink_list_pool_lk;
void bdb_newsi_mempool_stat()
{
Pthread_mutex_lock(&fileid_pglogs_queue_pool_lk);
pool_dumpx(fileid_pglogs_queue_pool, "fileid_pglogs_queue_pool");
Pthread_mutex_unlock(&fileid_pglogs_queue_pool_lk);
Pthread_mutex_lock(&pglogs_queue_cursor_pool_lk);
pool_dumpx(pglogs_queue_cursor_pool, "pglogs_queue_cursor_pool");
Pthread_mutex_unlock(&pglogs_queue_cursor_pool_lk);
Pthread_mutex_lock(<ran_pglogs_key_pool_lk);
pool_dumpx(ltran_pglogs_key_pool, "ltran_pglogs_key_pool");
Pthread_mutex_unlock(<ran_pglogs_key_pool_lk);
Pthread_mutex_lock(&asof_cursor_pool_lk);
pool_dumpx(asof_cursor_pool, "asof_cursor_pool");
Pthread_mutex_unlock(&asof_cursor_pool_lk);
Pthread_mutex_lock(&pglogs_commit_list_pool_lk);
pool_dumpx(pglogs_commit_list_pool, "pglogs_commit_list_pool");
Pthread_mutex_unlock(&pglogs_commit_list_pool_lk);
Pthread_mutex_lock(&pglogs_queue_key_pool_lk);
pool_dumpx(pglogs_queue_key_pool, "pglogs_queue_key_pool");
Pthread_mutex_unlock(&pglogs_queue_key_pool_lk);
Pthread_mutex_lock(&pglogs_key_pool_lk);
pool_dumpx(pglogs_key_pool, "pglogs_key_pool");
Pthread_mutex_unlock(&pglogs_key_pool_lk);
Pthread_mutex_lock(&pglogs_logical_key_pool_lk);
pool_dumpx(pglogs_logical_key_pool, "pglogs_logical_key_pool");
Pthread_mutex_unlock(&pglogs_logical_key_pool_lk);
Pthread_mutex_lock(&pglogs_lsn_list_pool_lk);
pool_dumpx(pglogs_lsn_list_pool, "pglogs_lsn_list_pool");
Pthread_mutex_unlock(&pglogs_lsn_list_pool_lk);
Pthread_mutex_lock(&pglogs_lsn_commit_list_pool_lk);
pool_dumpx(pglogs_lsn_commit_list_pool, "pglogs_lsn_commit_list_pool");
Pthread_mutex_unlock(&pglogs_lsn_commit_list_pool_lk);
Pthread_mutex_lock(&pglogs_relink_key_pool_lk);
pool_dumpx(pglogs_relink_key_pool, "pglogs_relink_key_pool");
Pthread_mutex_unlock(&pglogs_relink_key_pool_lk);
Pthread_mutex_lock(&pglogs_relink_list_pool_lk);
pool_dumpx(pglogs_relink_list_pool, "pglogs_relink_list_pool");
Pthread_mutex_unlock(&pglogs_relink_list_pool_lk);
}
static pthread_mutex_t del_queue_lk = PTHREAD_MUTEX_INITIALIZER;
static struct fileid_pglogs_queue *allocate_fileid_pglogs_queue()
{
struct fileid_pglogs_queue *q;
Pthread_mutex_lock(&fileid_pglogs_queue_pool_lk);
q = pool_getablk(fileid_pglogs_queue_pool);
Pthread_mutex_unlock(&fileid_pglogs_queue_pool_lk);
#ifdef NEWSI_DEBUG_POOL
q->pool = fileid_pglogs_queue_pool;
#endif
return q;
}
static void return_fileid_pglogs_queue(struct fileid_pglogs_queue *q)
{
Pthread_mutex_lock(&fileid_pglogs_queue_pool_lk);
#ifdef NEWSI_DEBUG_POOL
assert(q->pool == fileid_pglogs_queue_pool);
#endif
pool_relablk(fileid_pglogs_queue_pool, q);
Pthread_mutex_unlock(&fileid_pglogs_queue_pool_lk);
}
static struct pglogs_queue_cursor *allocate_pglogs_queue_cursor(void)
{
struct pglogs_queue_cursor *c;
Pthread_mutex_lock(&pglogs_queue_cursor_pool_lk);
c = pool_getablk(pglogs_queue_cursor_pool);
Pthread_mutex_unlock(&pglogs_queue_cursor_pool_lk);
#ifdef NEWSI_DEBUG_POOL
c->pool = pglogs_queue_cursor_pool;
#endif
return c;
}
void return_pglogs_queue_cursor(struct pglogs_queue_cursor *c)
{
Pthread_mutex_lock(&pglogs_queue_cursor_pool_lk);
#ifdef NEWSI_DEBUG_POOL
assert(c->pool == pglogs_queue_cursor_pool);
#endif
pool_relablk(pglogs_queue_cursor_pool, c);
Pthread_mutex_unlock(&pglogs_queue_cursor_pool_lk);
}
static struct ltran_pglogs_key *allocate_ltran_pglogs_key(void)
{
struct ltran_pglogs_key *k;
Pthread_mutex_lock(<ran_pglogs_key_pool_lk);
k = pool_getablk(ltran_pglogs_key_pool);
Pthread_mutex_unlock(<ran_pglogs_key_pool_lk);
#ifdef NEWSI_DEBUG_POOL
k->pool = ltran_pglgos_key_pool;
#endif
return k;
}
static void return_ltran_pglogs_key(struct ltran_pglogs_key *k)
{
Pthread_mutex_lock(<ran_pglogs_key_pool_lk);
#ifdef NEWSI_DEBUG_POOL
assert(k->pool == ltran_pglogs_key_pool);
#endif
pool_relablk(ltran_pglogs_key_pool, k);
Pthread_mutex_unlock(<ran_pglogs_key_pool_lk);
}
static struct asof_cursor *allocate_asof_cursor(void)
{
struct asof_cursor *a;
Pthread_mutex_lock(&asof_cursor_pool_lk);
a = pool_getablk(asof_cursor_pool);
Pthread_mutex_unlock(&asof_cursor_pool_lk);
#ifdef NEWSI_DEBUG_POOL
a->pool = asof_cursor_pool;
#endif
return a;
}
static void return_asof_cursor(struct asof_cursor *a)
{
Pthread_mutex_lock(&asof_cursor_pool_lk);
#ifdef NEWSI_DEBUG_POOL
assert(a->pool == asof_cursor_pool);
#endif
pool_relablk(asof_cursor_pool, a);
Pthread_mutex_unlock(&asof_cursor_pool_lk);
}
static struct commit_list *allocate_pglogs_commit_list(void)
{