forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fstdump.c
1675 lines (1416 loc) · 51.6 KB
/
fstdump.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.
*/
/* It's a bit late now, but probably all the IO should be in the main
* database, with just the fast iteration in here. */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <tcputil.h>
#include <epochlib.h>
#include <build/db.h>
#include "debug_switches.h"
#include <ctrace.h>
#include <locks_wrap.h>
#include <net.h>
#include "bdb_int.h"
#include "locks.h"
#include <str0.h>
#include <sbuf2.h>
#include "logmsg.h"
#include "thread_stats.h"
struct error_extension {
uint32_t length; /* length of this struct in bytes */
int32_t bdberr;
uint32_t msglen;
/* Error string follows length bytes after start of struct. String is
* exactly msglen bytes - no \0 terminator */
};
/* fstdump thread coordination structure */
typedef struct {
bdb_state_type *bdb_state;
bdb_state_type *bdb_parent_state;
pthread_mutex_t lock; /* must lock on this before writing data to fd */
int fd; /* fd to write to */
int bdberr; /* check/set this under lock. indicates a thread
hit an error so all threads should stop. */
char errmsg[128]; /* also check under lock - gives error to return to
client. */
size_t sendrecsz; /* record size that we will send */
bdb_fstdumpdta_callback_t convert_callback;
int callback_flags; /* Used for linux-clients. */
void *userptr;
void *userptr2;
int timeoutms;
unsigned long long count; /* how many records dumped in total, gets updated
under lock. */
int epoch_start; /* when this dump started */
int close_cursor; /* whether to close the cursor in between socket sends.
Defaults to 1. If off, performance will be slightly
higher,
but the socket has a short timeout (5s) due to page
locking
by bdb. So if you turn this off, make sure the dumping
application
doesn't idle on the socket for too long. */
const char *tzname; /* pointer to constant tzname store on the stack of
dumping thread
creating thread, hah */
} fstdump_t;
typedef struct {
fstdump_t *common;
DB *dbp; /* what db to dump */
pthread_t tid;
int get_genids;
unsigned long long count; /* how many records dumped in this thread */
int real_thread; /* 1 if this really is a thread, in which case
it needs to call bdb_thread_event() */
} fstdump_per_thread_t;
struct fstdump_thread2_arg {
pthread_mutex_t mutex;
fstdump_per_thread_t perthread[MAXDTAFILES];
int num_stripes;
int num_done;
};
static int write_records(fstdump_per_thread_t *fstdump, DBT *data,
void *sendrec, unsigned char **retkey_p);
static void *fstdump_thread_inner(fstdump_per_thread_t *fstdump, void *sendrec,
void *databuf, size_t buffer_length);
static int bdb_fstdumpdta_sendsz_int(bdb_state_type *bdb_state, SBUF2 *sb,
size_t sendrecsz,
bdb_fstdumpdta_callback_t convert_callback,
int callback_flags, void *userptr,
void *userptr2, int timeoutms,
int safe_mode, int *bdberr,
const char *tzname, int get_genids);
static int close_retry(DBC *dbcp, fstdump_t *common);
static int get_retry(DBC *dbcp, fstdump_t *common, DBT *key, DBT *data,
u_int32_t flags);
static int open_retry(DBC **dbcp, fstdump_per_thread_t *fstdump,
fstdump_t *common);
/* from comdb2.c - current value is 500 */
extern int gbl_maxretries;
/* in fstdump,
after this many retries resulting in deadlocks,
start sleeping between calls to c_get and c_open */
static int deadlock_sleep_start = 10;
/* as above -
sleep this long between calls to c_get and c_open
(ms) */
static int deadlock_sleep_amt = 100 * 1000;
/* Hijacked from tcplib.c */
static int tcpwritev(int fd, int niov, struct iovec *iniov, int timeoutms)
{
/*returns 0 if timed out*/
int rc;
struct pollfd pol;
if (timeoutms > 0) {
pol.fd = fd;
pol.events = POLLOUT;
rc = poll(&pol, 1, timeoutms);
if (rc <= 0)
return rc; /*timed out or error*/
if ((pol.revents & POLLOUT) == 0)
return -1;
/*can write*/
}
return writev(fd, iniov, niov);
}
/* TODO does this conflict with tcplib? We at least include tcputil.h so this
* definition here seems suspect */
static int stcpwritemsgv(int fd, int niov, struct iovec *iniov, int timeoutms)
{
int curiov, curoff, left, sent, tot;
char *base;
struct iovec iov[12];
if (niov > 12)
return -7777; /*internal limit*/
if (niov < 1 || iniov[0].iov_len < 1)
return -7778; /*bad input*/
memcpy(iov, iniov, sizeof(struct iovec) * niov);
tot = 0;
sent = 0;
curoff = 0;
curiov = 0;
do {
base = (char *)iniov[curiov].iov_base;
iov[curiov].iov_base = (void *)(&base[curoff]);
iov[curiov].iov_len = iniov[curiov].iov_len - curoff;
sent = tcpwritev(fd, niov - curiov, &iov[curiov], timeoutms);
if (sent == 0) {
/* Timeout */
return 0;
}
if (sent < 0)
return -1;
tot += sent;
while (sent > 0) {
left = iniov[curiov].iov_len - curoff;
if (left <= sent) {
/*advance to next entry*/
sent -= left;
curiov++;
curoff = 0;
continue;
}
curoff += sent;
break;
}
} while (curiov < niov);
return tot;
}
/* naming a function "fast" makes it faster */
static int fastwritev(int fd, struct iovec *iniov, int niov, int timeoutms)
{
int rc = stcpwritemsgv(fd, niov, iniov, timeoutms);
if (rc == 0) {
logmsg(LOGMSG_ERROR, "fast dump timed out to socket fd %d\n", fd);
} else if (rc < 0) {
logmsg(LOGMSG_ERROR, "fast dump error writing to socket fd %d: %d %s\n", fd,
errno, strerror(errno));
}
return rc;
}
static void *fstdump_thread(void *arg)
{
fstdump_per_thread_t *fstdump = (fstdump_per_thread_t *)arg;
fstdump_t *common = fstdump->common;
void *ret = NULL;
void *sendrec;
void *databuf;
size_t buffer_length;
bdb_state_type *bdb_state = common->bdb_state;
/*thread_started("bdb fstdump");*/
/* work out how large a buffer we need. It must be a multiple of 1KB,
* larger than the page size and unsigned int aligned
* (see berkdb docs). */
buffer_length = bdb_state->attr->fstdump_buffer_length;
if ((buffer_length & (1024 - 1)) != 0)
buffer_length &= ~(1024 - 1);
if (buffer_length < bdb_state->attr->pagesizedta)
buffer_length = bdb_state->attr->pagesizedta;
sendrec = mymalloc(common->sendrecsz);
if (!sendrec)
logmsg(LOGMSG_ERROR, "fstdump_thread: mymalloc %zu failed (sendrec)\n",
common->sendrecsz);
databuf = mymalloc(buffer_length);
if (!databuf)
logmsg(LOGMSG_ERROR, "fstdump_thread: mymalloc %u failed (databuf)\n",
(unsigned)buffer_length);
if (sendrec && databuf) {
/* If not threaded then read lock etc already acquired */
if (fstdump->real_thread) {
bdb_thread_event(common->bdb_state, BDBTHR_EVENT_START_RDONLY);
BDB_READLOCK("fstdump_thread");
}
ret = fstdump_thread_inner(fstdump, sendrec, databuf, buffer_length);
if (fstdump->real_thread) {
BDB_RELLOCK();
bdb_thread_event(common->bdb_state, BDBTHR_EVENT_DONE_RDONLY);
}
} else {
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_MALLOC;
snprintf0(common->errmsg, sizeof(common->errmsg), "out of memory");
}
Pthread_mutex_unlock(&common->lock);
}
if (sendrec)
free(sendrec);
if (databuf)
free(databuf);
return ret;
}
/*
* The newest multithreaded approach is to have a pool of threads to process a
* larger pool of stripes.
*/
static void *fstdump_thread2(void *voidarg)
{
struct fstdump_thread2_arg *args = voidarg;
/*thread_started("bdb fstdump2");*/
while (1) {
fstdump_per_thread_t *work = NULL;
/* Get the nxt stripe to process */
Pthread_mutex_lock(&args->mutex);
if (args->num_done < args->num_stripes) {
work = &args->perthread[args->num_done];
args->num_done++;
}
Pthread_mutex_unlock(&args->mutex);
if (work) {
int bdberr;
work->tid = pthread_self();
fstdump_thread(work);
Pthread_mutex_lock(&work->common->lock);
bdberr = work->common->bdberr;
Pthread_mutex_unlock(&work->common->lock);
/* If the dump has errored then don't continue on to next stripe */
if (bdberr != 0)
break;
} else {
break;
}
}
return NULL;
}
static void *fstdump_thread_inner(fstdump_per_thread_t *fstdump, void *sendrec,
void *databuf, size_t buffer_length)
{
fstdump_t *common = fstdump->common;
int rc, rrn;
unsigned long long genid;
unsigned char *retkey = NULL;
unsigned long long lastkey;
DBC *dbcp;
DBT key, data;
int need_advance = 1;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
data.data = databuf;
data.ulen = buffer_length;
data.flags = DB_DBT_USERMEM;
/* Acquire a cursor for the database. */
if (open_retry(&dbcp, fstdump, common))
return NULL;
/* start dumping at rrn 2, unless this is a stripey db in which case
* we just dump every record. */
if (!common->bdb_state->attr->dtastripe) {
rrn = 2;
key.data = &rrn;
key.size = sizeof(int);
key.ulen = sizeof(int);
} else {
genid = 0;
key.data = &genid;
key.size = sizeof(genid);
key.ulen = sizeof(genid);
}
for (;;) {
int ms_before, ms_after, ms_diff;
u_int32_t flags;
if (common->bdb_parent_state->bdb_lock_desired) {
logmsg(LOGMSG_ERROR, "fstdump_thread: aborting due to write lock desired\n");
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_DEADLOCK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"aborted because database write lock desired");
}
Pthread_mutex_unlock(&common->lock);
dbcp->c_close(dbcp);
return NULL;
}
if (db_is_stopped()) {
logmsg(LOGMSG_ERROR, "fstdump_thread: aborting due to stop_threads\n");
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_DEADLOCK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"aborted because database stop_threads");
}
Pthread_mutex_unlock(&common->lock);
dbcp->c_close(dbcp);
return NULL;
}
/*
* Acquire the next set of key/data pairs. This code does
* not handle single key/data pairs that won't fit in
* the buffer, instead returning ENOMEM to our caller.
*/
bdb_reset_thread_stats();
ms_before = comdb2_time_epochms();
data.data = databuf;
flags = DB_MULTIPLE_KEY;
if (need_advance || !common->close_cursor)
flags = flags | DB_NEXT;
if ((rc = get_retry(dbcp, common, &key, &data, flags)) != 0) {
if (rc == DB_NOTFOUND)
break;
return NULL;
}
ms_after = comdb2_time_epochms();
ms_diff = ms_after - ms_before;
if (ms_diff > common->bdb_parent_state->attr->fstdump_longreq) {
const struct berkdb_thread_stats *thread_stats =
bdb_get_thread_stats();
logmsg(LOGMSG_ERROR, "fstdump_thread: LONG REQUEST dbcp->c_get %d ms\n",
ms_diff);
bdb_fprintf_stats(thread_stats, " ", stderr);
}
/* if common->close_cursor != 0, we close and re-open the bdb cursor
in between socket sends. This permits us to wait on the socket
indefinitely
without locking pages for arbitrary amounts of time.
- Johan Nystrom */
if (common->close_cursor) {
if (close_retry(dbcp, common))
return NULL;
}
rc = write_records(fstdump, &data, sendrec, &retkey);
if (rc != 0) {
if (!common->close_cursor) {
dbcp->c_close(dbcp);
}
return NULL;
}
if (retkey) {
memcpy(&lastkey, retkey, key.size);
}
if (common->close_cursor) {
if (open_retry(&dbcp, fstdump, common))
return NULL;
/* Use to seek to previous position */
if (retkey == NULL)
break;
key.data = retkey;
/* DB_SET_RANGE guarantees to get us to the smallest key
less than or equal to the given one. It is thus possible that
DB_SET_RANGE will not advance us, so we must test if the key has
changed. The reason we use this and not DB_SET is that records
may be deleted while our cursor is closed. */
if ((rc = get_retry(dbcp, common, &key, &data, DB_SET_RANGE)) !=
0) {
if (rc == DB_NOTFOUND)
break;
return NULL;
}
if (!memcmp(&lastkey, key.data, key.size))
need_advance = 1;
else
need_advance = 0;
}
}
close_retry(dbcp, common);
return NULL;
}
#define UNUSED(x) ((void)(x))
static int write_records(fstdump_per_thread_t *fstdump, DBT *data,
void *sendrec, unsigned char **retkey_p)
{
fstdump_t *common = fstdump->common;
void *p;
*retkey_p = NULL;
for (DB_MULTIPLE_INIT(p, data);;) {
size_t retklen, retdlen;
unsigned char *retkey;
unsigned char *retdata;
int fndrrn;
long long genid = 0;
void *fnddta;
size_t fndlen;
int rc;
struct iovec iov[2];
struct odh odh;
unsigned char buffer[24 * 1024];
DB_MULTIPLE_KEY_NEXT(p, data, retkey, retklen, retdata, retdlen);
UNUSED(retklen);
if (p == NULL)
break;
*retkey_p = retkey;
if (common->bdb_parent_state->bdb_lock_desired) {
logmsg(LOGMSG_ERROR, "fstdump_thread: aborting due to write lock desired\n");
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_DEADLOCK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"aborted because database write lock desired");
}
Pthread_mutex_unlock(&common->lock);
return -1;
}
if (db_is_stopped()) {
logmsg(LOGMSG_ERROR, "fstdump_thread: aborting due to stop_threads\n");
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_DEADLOCK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"aborted because database stop_threads");
}
Pthread_mutex_unlock(&common->lock);
return -1;
}
/*
data that came back from berkeley is in
retklen, retkey, retdlen, retdata , possibly compressed
*/
rc = bdb_unpack(common->bdb_state, retdata, retdlen, buffer,
sizeof(buffer), &odh, NULL);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "%s: bdb_unpack %d %s\n", __func__, rc,
bdb_strerror(rc));
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_CALLBACK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"bdb_unpack failure");
}
Pthread_mutex_unlock(&common->lock);
return -1;
}
retdata = odh.recptr;
retdlen = odh.length;
/*
now get fndrrn, fndlen, and fnddta to have the correct values
*/
if (common->bdb_state->attr->dtastripe) {
/* Return time stamp as rrn - neat way of getting dates on dumps */
if (fstdump->get_genids)
genid = *((long long *)retkey);
else
fndrrn = *((int *)retkey);
} else
memcpy(&fndrrn, retkey, sizeof(int));
if (fstdump->get_genids) {
if (genid == 0 || genid == 1)
continue;
} else if (fndrrn == 0 || fndrrn == 1)
continue;
if (common->convert_callback) {
size_t fndreclen = retdlen;
if (common->bdb_state->attr->genids &&
!common->bdb_state->attr->dtastripe) {
fnddta = (void *)(((unsigned long long *)retdata) + 1);
retdlen -= sizeof(unsigned long long);
} else
fnddta = retdata;
rc = common->convert_callback(fnddta, fndreclen, sendrec,
common->sendrecsz, common->userptr,
common->userptr2, common->tzname,
odh.csc2vers, common->callback_flags);
if (rc) {
logmsg(LOGMSG_ERROR, "write_records: convert returns bad rc, %d\n",
rc);
Pthread_mutex_lock(&common->lock);
{
common->bdberr = BDBERR_CALLBACK;
snprintf0(common->errmsg, sizeof(common->errmsg),
"conversion failure");
}
Pthread_mutex_unlock(&common->lock);
return -1;
}
fnddta = (unsigned char *)sendrec;
fndlen = common->sendrecsz;
} else {
fnddta = (void *)retdata;
fndlen = common->sendrecsz;
}
/* we dont have the proper comdb rc, but it seems meaningless here */
/* write the record size as first thing in the stream */
Pthread_mutex_lock(&common->lock);
{
rc = 0;
/* if another thread already bombed then so do we */
if (!common->bdberr) {
if (fstdump->get_genids && genid) {
iov[0].iov_base = (char *)&genid;
iov[0].iov_len = 8;
} else {
iov[0].iov_base = (char *)&fndrrn;
iov[0].iov_len = 4;
}
iov[1].iov_base = (char *)fnddta;
iov[1].iov_len = fndlen;
if (common->close_cursor) {
/* wait indefinitely - page locks not held*/
rc = fastwritev(common->fd, iov, 2, -1);
} else {
/* short socket timeout */
rc = fastwritev(common->fd, iov, 2, common->timeoutms);
}
if (rc == 0) {
snprintf0(common->errmsg, sizeof(common->errmsg),
"timeout writing to socket");
common->bdberr = BDBERR_TIMEOUT;
} else if (rc < 0) {
snprintf0(common->errmsg, sizeof(common->errmsg),
"error writing to socket");
common->bdberr = BDBERR_IO;
} else {
fstdump->count++;
common->count++;
}
}
if (common->bdberr != BDBERR_NOERROR)
rc = -1;
}
Pthread_mutex_unlock(&common->lock);
if (rc <= 0)
return -1;
}
return 0;
}
static int bdb_fstdumpdta_sendsz_int(bdb_state_type *bdb_state, SBUF2 *sb,
size_t sendrecsz,
bdb_fstdumpdta_callback_t convert_callback,
int callback_flags, void *userptr,
void *userptr2, int timeoutms,
int safe_mode, int *bdberr,
const char *tzname, const int get_genids)
{
int sockfd;
fstdump_t fstdump;
int flag;
int rc;
bbuint32_t fndlen32 = htonl(sendrecsz);
struct iovec iov[2];
*bdberr = BDBERR_NOERROR;
bzero(&fstdump, sizeof(fstdump));
sbuf2flush(sb);
sockfd = sbuf2fileno(sb);
/* turn nagel back on ... */
flag = 0;
rc = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
sizeof(int));
if (rc != 0) {
logmsg(LOGMSG_ERROR,
"bdb_fstdumpdta_sendsz: error setting TCP_NODELAY %d %s\n",
errno, strerror(errno));
}
/* Send record length down. Client expects this in first 4 bytes. */
iov[0].iov_base = (char *)&fndlen32;
iov[0].iov_len = 4;
if ((rc = fastwritev(sockfd, iov, 1, timeoutms)) <= 0) {
fstdump.bdberr = BDBERR_IO;
goto done;
}
fstdump.sendrecsz = sendrecsz;
fstdump.bdb_state = bdb_state;
fstdump.bdb_parent_state =
bdb_state->parent ? bdb_state->parent : bdb_state;
Pthread_mutex_init(&fstdump.lock, NULL);
fstdump.fd = sockfd;
fstdump.bdberr = 0;
fstdump.convert_callback = convert_callback;
fstdump.callback_flags = callback_flags;
fstdump.userptr = userptr;
fstdump.userptr2 = userptr2;
fstdump.timeoutms = timeoutms;
fstdump.close_cursor = safe_mode;
fstdump.tzname = tzname;
if (bdb_state->attr->dtastripe &&
bdb_state->attr->fstdump_maxthreads >= bdb_state->attr->dtastripe) {
/* stripey db - let's try one thread per database! */
fstdump_per_thread_t perthread[MAXDTAFILES];
int nthr;
int numthreads = 0;
pthread_attr_t attr;
Pthread_attr_init(&attr);
Pthread_attr_setstacksize(&attr,
bdb_state->attr->fstdump_thread_stacksz);
bzero(perthread, sizeof(perthread));
for (nthr = 0; nthr < bdb_state->attr->dtastripe; nthr++) {
perthread[nthr].common = &fstdump;
perthread[nthr].dbp = bdb_state->dbp_data[0][nthr];
perthread[nthr].real_thread = 1;
perthread[nthr].get_genids = get_genids;
rc = pthread_create(&perthread[nthr].tid, &attr, fstdump_thread,
&perthread[nthr]);
if (rc != 0) {
logmsg(LOGMSG_ERROR,
"bdb_fstdumpdta_sendsz: pthread_create failed rc %d %s\n",
rc, strerror(rc));
Pthread_mutex_lock(&fstdump.lock);
{
snprintf0(fstdump.errmsg, sizeof(fstdump.errmsg),
"pthread_create failed");
fstdump.bdberr = 1;
}
Pthread_mutex_unlock(&fstdump.lock);
break;
}
numthreads++;
}
/* join all threads */
for (nthr = 0; nthr < numthreads; nthr++) {
rc = pthread_join(perthread[nthr].tid, NULL);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "bdb_fstdumpdta_sendsz: pthread_join failed "
"thr #%d rc %d %s\n",
nthr, rc, strerror(rc));
}
}
Pthread_attr_destroy(&attr);
} else if (bdb_state->attr->dtastripe &&
bdb_state->attr->fstdump_maxthreads > 0) {
/* new multithreaded model - limit the number of threads to reduce
* overhead on this node. */
struct fstdump_thread2_arg args;
pthread_t tids[MAXDTAFILES];
int nthr;
int numthreads = 0;
pthread_attr_t attr;
Pthread_attr_init(&attr);
Pthread_attr_setstacksize(&attr,
bdb_state->attr->fstdump_thread_stacksz);
bzero(&args, sizeof(args));
for (nthr = 0; nthr < bdb_state->attr->dtastripe; nthr++) {
args.perthread[nthr].common = &fstdump;
args.perthread[nthr].dbp = bdb_state->dbp_data[0][nthr];
args.perthread[nthr].real_thread = 1;
args.perthread[nthr].get_genids = get_genids;
}
Pthread_mutex_init(&args.mutex, NULL);
args.num_stripes = bdb_state->attr->dtastripe;
args.num_done = 0;
for (nthr = 0; nthr < bdb_state->attr->fstdump_maxthreads; nthr++) {
rc = pthread_create(&tids[nthr], &attr, fstdump_thread2, &args);
if (rc != 0) {
logmsg(LOGMSG_ERROR,
"bdb_fstdumpdta_sendsz: pthread_create failed rc %d %s\n",
rc, strerror(rc));
Pthread_mutex_lock(&fstdump.lock);
{
snprintf0(fstdump.errmsg, sizeof(fstdump.errmsg),
"pthread_create failed");
fstdump.bdberr = 1;
}
Pthread_mutex_unlock(&fstdump.lock);
break;
}
numthreads++;
}
/* join all threads */
for (nthr = 0; nthr < numthreads; nthr++) {
rc = pthread_join(tids[nthr], NULL);
if (rc != 0) {
logmsg(LOGMSG_ERROR,
"bdb_fstdumpdta_sendsz: pthread_join failed "
"thr #%d rc %d %s\n",
nthr, rc, strerror(rc));
}
}
Pthread_attr_destroy(&attr);
} else if (bdb_state->attr->dtastripe) {
/* non-multithreaded approach, for comparison */
int ndta;
for (ndta = 0; fstdump.bdberr == 0 && ndta < bdb_state->attr->dtastripe;
ndta++) {
fstdump_per_thread_t onethread;
bzero(&onethread, sizeof(onethread));
onethread.common = &fstdump;
onethread.dbp = bdb_state->dbp_data[0][ndta];
onethread.get_genids = get_genids;
fstdump_thread(&onethread);
}
} else {
/* non-stripey db, so we don't thread. */
fstdump_per_thread_t onethread;
bzero(&onethread, sizeof(onethread));
onethread.common = &fstdump;
onethread.dbp = bdb_state->dbp_data[0][0];
/* Genid thing works only with data stripes*/
fstdump_thread(&onethread);
}
/* Signal the end of the stream with a dummy last record of rrn 1.
* EXTENSION: It used to be that the dummy record was all zeroes. Now
* I set the convention that if the first byte of the dummy record is 0xee
* then an error occured. In this case an error string will follow the
* dummy record. Older clients will ignore this - newer clients can read
* it and display it.
*/
done:
if (fstdump.bdberr != BDBERR_TIMEOUT && fstdump.bdberr != BDBERR_IO) {
struct error_extension errext;
int dummyrrn = htonl(1);
long long dummygenid = htonl(1);
struct iovec iov[4];
int niov;
unsigned char *rec = mymalloc(fstdump.sendrecsz);
if (!rec)
logmsg(LOGMSG_ERROR,
"bdb_fstdumpdta_sendsz: mymalloc %zu failed at eof\n",
fstdump.sendrecsz);
else {
bzero(rec, fstdump.sendrecsz);
if (get_genids) {
iov[0].iov_base = (char *)&dummygenid;
iov[0].iov_len = 8;
} else {
iov[0].iov_base = (char *)&dummyrrn;
iov[0].iov_len = 4;
}
iov[1].iov_base = (void *)rec;
iov[1].iov_len = fstdump.sendrecsz;
niov = 2;
/* if an error occured then send the extended error information */
if (fstdump.bdberr != 0) {
rec[0] = 0xee;
bzero(&errext, sizeof(errext));
errext.length = htonl(sizeof(errext));
errext.bdberr = htonl(fstdump.bdberr);
errext.msglen = htonl(strlen(fstdump.errmsg));
iov[2].iov_base = (void *)&errext;
iov[2].iov_len = sizeof(errext);
iov[3].iov_base = (void *)fstdump.errmsg;
iov[3].iov_len = errext.msglen;
niov = 4;
}
fastwritev(sockfd, iov, niov,
fstdump.close_cursor ? -1 : timeoutms);
free(rec);
}
}
*bdberr = fstdump.bdberr;
sbuf2flush(sb);
return 0;
}
int bdb_fstdumpdta_sendsz(bdb_state_type *bdb_state, SBUF2 *sb,
size_t sendrecsz,
bdb_fstdumpdta_callback_t convert_callback,
int callback_flags, void *userptr, void *userptr2,
int timeoutms, int safe_mode, int *bdberr,
const char *tzname, int get_genids)
{
int rc;
BDB_READLOCK("bdb_fstdumpdta_sendsz");
rc = bdb_fstdumpdta_sendsz_int(bdb_state, sb, sendrecsz, convert_callback,
callback_flags, userptr, userptr2, timeoutms,
safe_mode, bdberr, tzname, get_genids);
BDB_RELLOCK();
return rc;
}
struct dtadump {
DBC *cur;
int dtafile;
int bufsz;
char *buf;
void *p; /* for DB_MULTIPLE_NEXT, etc. */
DBT dbt_dta;
DBT dbt_key;
DB *dbps[MAXDTAFILES];
int num_dbps;
int have_keys;
unsigned long long keybuf;
void *freeptr;
void *bdb_unpack_buf;
};
/* if is_blob == TRUE, we want to read a blob file */
/* nr -> nr of file we want to read */
/* works only for tagged databases */
struct dtadump *bdb_dtadump_start(bdb_state_type *bdb_state, int *bdberr,
int is_blob, int nr)
{
struct dtadump *dump;
int i;
int dtanum;
if (bdb_state->parent == NULL) {
*bdberr = BDBERR_BADARGS;
return NULL;
}
if (is_blob && ((nr < -1) || (nr > bdb_state->numdtafiles - 2))) {
*bdberr = BDBERR_BADARGS;
return NULL;
}
if ((!is_blob) && ((nr < -1) || (nr > bdb_state->attr->dtastripe))) {
*bdberr = BDBERR_BADARGS;
return NULL;
}
dump = malloc(sizeof(struct dtadump));
if (!dump) {
logmsg(LOGMSG_ERROR, "bdb_dtadump_start: out of memory\n");
return NULL;
}
bzero(dump, sizeof(struct dtadump));
dtanum = is_blob ? nr + 1 : 0;
dump->num_dbps = bdb_get_datafile_num_files(bdb_state, dtanum);
for (i = 0; i < dump->num_dbps; i++)
dump->dbps[i] = bdb_state->dbp_data[dtanum][i];
dump->bufsz = 1024 * 1024; /* 1 meg */
if (is_blob) {
dump->bufsz *= 64; /* 64 meg max for blobs here... */
} else {
/* if not a blob file then we expect a fixed record size so reserve some