-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvfs-ramcloud.c
1572 lines (1393 loc) · 47.4 KB
/
vfs-ramcloud.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
#if !defined(SQLITE_TEST) || SQLITE_OS_UNIX
#include <alloca.h>
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/select.h>
#include <time.h>
#include <unistd.h>
#include "md5.h"
#include "sqlite3.h"
#include "vfs-ramcloud-stats.h"
#include <ramcloud/CRamCloud.h>
#ifdef __cplusplus
extern "C" {
#endif
// Default page size
#ifndef SQLITE_RCVFS_BLOCKSZ
# define SQLITE_RCVFS_BLOCKSZ 1024
#endif
#ifndef DPRINTF
# ifdef DEBUG
# define DPRINTF(...) printf(__VA_ARGS__)
# else
# define DPRINTF(...) (0)
# endif
#endif
#define SQLITE_RCVFS_TIMESKEW 2 // 2 seconds maximum time de-syncronization
// Allocate so many leases by default with a database connection
#define SQLITE_RCVFS_NLEASES_DEFAULT 8
#define SQLITE_RCVFS_LEASETIME 120 // 2 minutes lease time
#define SQLITE_RCVFS_WBUF_NBLOCKS 128
/**
* The maximum pathname length supported by this VFS.
*/
#define MAXPATHNAME 4096
/*static void hex_dump(const char *buf, size_t size) {
printf("\n HEXDUMP");
unsigned i;
for (i = 0; i < size; ++i) {
if (i % 32 == 0)
printf("\n");
printf("%2x ", buf[i] & 0xff);
}
printf("\n");
}*/
static int rcRandomness(sqlite3_vfs *pVfs, int nByte, char *zByte);
typedef struct sqlite3_rcvfs_connection SQLITE_RCVFS_CONNECTION;
struct sqlite3_rcvfs_connection {
char *locator;
char *cluster_name;
char *table_name;
uint64_t tblid;
pthread_key_t tls; // Points to a SQLITE_RCVFS_SESSION
};
/**
* RAMCloud client sessions
*/
typedef struct sqlite3_rcvfs_session SQLITE_RCVFS_SESSION;
struct sqlite3_rcvfs_session {
SQLITE_RCVFS_CONNECTION *conn;
struct rc_client *client;
};
static SQLITE_RCVFS_SESSION *get_rc_session(SQLITE_RCVFS_CONNECTION *conn) {
SQLITE_RCVFS_SESSION *rcs = pthread_getspecific(conn->tls);
if (!rcs) {
rcs = (SQLITE_RCVFS_SESSION *)sqlite3_malloc(sizeof(SQLITE_RCVFS_SESSION));
if (!rcs) return NULL;
memset(rcs, 0, sizeof(SQLITE_RCVFS_SESSION));
rcs->conn = conn;
Status status = rc_connect(conn->locator, conn->cluster_name, &rcs->client);
if (status != STATUS_OK) {
sqlite3_free(rcs);
return NULL;
}
int retval = pthread_setspecific(conn->tls, rcs);
if (retval != 0) {
rc_disconnect(rcs->client);
sqlite3_free(rcs);
return NULL;
}
}
return rcs;
}
/**
* Database locks
*/
typedef struct sqlite3_rcvfs_token SQLITE_RCVFS_TOKEN;
struct sqlite3_rcvfs_token {
unsigned char digest[19]; // Random 152bits token (0: unlocked)
};
static SQLITE_RCVFS_TOKEN mk_token() {
SQLITE_RCVFS_TOKEN result;
int retval = rcRandomness(NULL, 19, (char *)result.digest);
assert(retval == 19);
return result;
}
typedef struct sqlite3_rcvfs_lease SQLITE_RCVFS_LEASE;
struct sqlite3_rcvfs_lease {
char lease_type;
SQLITE_RCVFS_TOKEN token;
time_t deadline; // Lease expires once dealine passed
};
static int is_owned(
const SQLITE_RCVFS_LEASE *lease,
const SQLITE_RCVFS_TOKEN *my_token
){
unsigned i;
for (i = 0; i < sizeof(lease->token.digest); ++i) {
if (lease->token.digest[i] != my_token->digest[i])
return 0;
}
return 1;
}
static int is_locked(const SQLITE_RCVFS_LEASE *lease) {
unsigned i;
for (i = 0; i < sizeof(lease->token.digest); ++i) {
if (lease->token.digest[i])
return lease->deadline + SQLITE_RCVFS_TIMESKEW > time(NULL);
}
return 0;
}
/**
* SQlite database registry and open handles
*/
typedef struct sqlite3_rcvfs_dbid SQLITE_RCVFS_DBID;
struct sqlite3_rcvfs_dbid {
unsigned char digest[16];
};
static SQLITE_RCVFS_DBID mk_dbid(const char *path) {
SQLITE_RCVFS_DBID result;
md5_state_t pms;
if (path) {
md5_init(&pms);
md5_append(&pms, (const md5_byte_t *)path, strlen(path));
md5_finish(&pms, result.digest);
} else {
int retval = rcRandomness(NULL, 16, (char *)result.digest);
assert(retval == 16);
}
/*unsigned i;
for (i = 0; i < 16; ++i) {
char dgt1 = (unsigned)result.digest[i] / 16;
char dgt2 = (unsigned)result.digest[i] % 16;
dgt1 += (dgt1 <= 9) ? '0' : 'a' - 10;
dgt2 += (dgt2 <= 9) ? '0' : 'a' - 10;
result.table_name[i*2] = dgt1;
result.table_name[i*2+1] = dgt2;
}
result.table_name[32] = '\0';*/
return result;
}
typedef struct sqlite3_rcvfs_dbheader SQLITE_RCVFS_DBHEADER;
struct sqlite3_rcvfs_dbheader {
int version; // Currently 1
uint64_t size; // Size in bytes
uint64_t blocksz; // Size of the chunks in the block table
};
typedef struct sqlite3_rcvfs_handle SQLITE_RCVFS_HANDLE;
struct sqlite3_rcvfs_handle {
SQLITE_RCVFS_CONNECTION *conn;
uint64_t tblid;
uint64_t size;
uint64_t blocksz;
SQLITE_RCVFS_DBID dbid;
SQLITE_RCVFS_TOKEN token;
SQLITE_RCVFS_LEASE *leases; // cache of leases
unsigned nLeases;
unsigned capacityLeases;
uint64_t versionLeases;
};
/**
* Block storage
*/
typedef struct sqlite3_rcvfs_blockid SQLITE_RCVFS_BLOCKID;
struct sqlite3_rcvfs_blockid {
uint64_t blockno;
};
SQLITE_RCVFS_BLOCKID SQLITE_RCVFS_HEADERBLOCK = { (uint64_t)(-1) };
SQLITE_RCVFS_BLOCKID SQLITE_RCVFS_LCBLOCK = { (uint64_t)(-2) };
SQLITE_RCVFS_BLOCKID SQLITE_RCVFS_INVALIDBLOCK = { (uint64_t)(-4) };
static inline int isInvalidBlock(SQLITE_RCVFS_BLOCKID *blockid) {
return blockid->blockno == SQLITE_RCVFS_INVALIDBLOCK.blockno;
}
typedef struct sqlite3_rcvfs_blockkey SQLITE_RCVFS_BLOCKKEY;
struct sqlite3_rcvfs_blockkey {
SQLITE_RCVFS_DBID dbid;
SQLITE_RCVFS_BLOCKID blockid;
};
typedef struct sqlite3_rcvfs_wbuffer SQLITE_RCVFS_WBUFFER;
struct sqlite3_rcvfs_wbuffer {
SQLITE_RCVFS_BLOCKID blockIds[SQLITE_RCVFS_WBUF_NBLOCKS];
uint16_t blockSizes[SQLITE_RCVFS_WBUF_NBLOCKS];
unsigned char buf[SQLITE_RCVFS_WBUF_NBLOCKS][SQLITE_RCVFS_BLOCKSZ];
unsigned gauge;
};
static inline void clearBufferBlock(SQLITE_RCVFS_WBUFFER *buf, unsigned idx) {
buf->blockIds[idx] = SQLITE_RCVFS_INVALIDBLOCK;
buf->blockSizes[idx] = 0;
memset(buf->buf[idx], 0, SQLITE_RCVFS_BLOCKSZ);
}
typedef struct RcFile RcFile;
struct RcFile {
sqlite3_file base; /* Base class. Must be first. */
SQLITE_RCVFS_HANDLE handle;
int flags; /* Open flags */
SQLITE_RCVFS_WBUFFER *blockBuffer;
uint64_t permanentSize;
};
//------------------------------------------------------------------------------
typedef int64_t atomic_int64;
static void inline __attribute__((used)) atomic_init64(atomic_int64 *a) {
*a = 0;
}
static int64_t inline __attribute__((used)) atomic_read64(atomic_int64 *a) {
return __sync_fetch_and_add(a, 0);
}
static void inline __attribute__((used)) atomic_inc64(atomic_int64 *a) {
(void) __sync_fetch_and_add(a, 1);
}
static int64_t inline __attribute__((used)) atomic_xadd64(atomic_int64 *a,
int64_t offset)
{
if (offset < 0)
return __sync_fetch_and_sub(a, -offset);
return __sync_fetch_and_add(a, offset);
}
atomic_int64 sqlite_rcvfs_nread = 0;
atomic_int64 sqlite_rcvfs_nwrite = 0;
atomic_int64 sqlite_rcvfs_nmwrite = 0;
atomic_int64 sqlite_rcvfs_nremove = 0;
atomic_int64 sqlite_rcvfs_nmremove = 0;
atomic_int64 sqlite_rcvfs_szread = 0;
atomic_int64 sqlite_rcvfs_szwrite = 0;
//------------------------------------------------------------------------------
static char *sqlite3_strdup(const char *str) {
size_t len = strlen(str) + 1;
char *result = sqlite3_malloc(len);
if (!result) return NULL;
memcpy(result, str, len);
return result;
}
static void sqlite3_rcvfs_tls_destructor(void *data) {
SQLITE_RCVFS_SESSION *rcs = (SQLITE_RCVFS_SESSION *)data;
if (rcs->client) rc_disconnect(rcs->client);
sqlite3_free(rcs);
}
void sqlite3_rcvfs_get_stats(SQLITE_RCVFS_STATS *stats) {
stats->nread = atomic_read64(&sqlite_rcvfs_nread);
stats->nwrite = atomic_read64(&sqlite_rcvfs_nwrite);
stats->nmwrite = atomic_read64(&sqlite_rcvfs_nmwrite);
stats->nremove = atomic_read64(&sqlite_rcvfs_nremove);
stats->nmremove = atomic_read64(&sqlite_rcvfs_nmremove);
stats->szread = atomic_read64(&sqlite_rcvfs_szread);
stats->szwrite = atomic_read64(&sqlite_rcvfs_szwrite);
}
/**
* Establishes the connection to a RAMCloud cluster
*/
SQLITE_RCVFS_CONNECTION *sqlite3_rcvfs_connect(
const char *locator,
const char *cluster_name,
const char *table_name
){
int retval;
SQLITE_RCVFS_CONNECTION *conn = NULL;
SQLITE_RCVFS_SESSION *rcs = NULL;
conn =
(SQLITE_RCVFS_CONNECTION *)sqlite3_malloc(sizeof(SQLITE_RCVFS_CONNECTION));
if (!conn) goto sqlite3_rcvfs_connect_fail;
memset(conn, 0, sizeof(SQLITE_RCVFS_CONNECTION));
conn->locator = sqlite3_strdup(locator);
conn->cluster_name = sqlite3_strdup(cluster_name);
conn->table_name = sqlite3_strdup(table_name);
if (!conn->locator || !conn->cluster_name || !conn->table_name)
goto sqlite3_rcvfs_connect_fail;
rcs = (SQLITE_RCVFS_SESSION *)sqlite3_malloc(sizeof(SQLITE_RCVFS_SESSION));
if (!rcs) goto sqlite3_rcvfs_connect_fail;
memset(rcs, 0, sizeof(SQLITE_RCVFS_SESSION));
rcs->conn = conn;
Status status = rc_connect(locator, cluster_name, &rcs->client);
if (status != STATUS_OK) goto sqlite3_rcvfs_connect_fail;
status = rc_createTable(rcs->client, conn->table_name, 1);
if (status != STATUS_OK) goto sqlite3_rcvfs_connect_fail;
status = rc_getTableId(rcs->client, conn->table_name, &(conn->tblid));
if (status != STATUS_OK) goto sqlite3_rcvfs_connect_fail;
retval = pthread_key_create(&conn->tls, sqlite3_rcvfs_tls_destructor);
if (retval != 0) goto sqlite3_rcvfs_connect_fail;
retval = pthread_setspecific(conn->tls, rcs);
if (retval != 0) {
pthread_key_delete(conn->tls);
goto sqlite3_rcvfs_connect_fail;
}
return conn;
sqlite3_rcvfs_connect_fail:
if (conn) {
if (conn->locator) sqlite3_free(conn->locator);
if (conn->cluster_name) sqlite3_free(conn->cluster_name);
if (conn->table_name) sqlite3_free(conn->table_name);
sqlite3_free(conn);
}
if (rcs) {
if (rcs->client) rc_disconnect(rcs->client);
sqlite3_free(rcs);
}
return NULL;
}
void sqlite3_rcvfs_disconnect(SQLITE_RCVFS_CONNECTION *conn) {
SQLITE_RCVFS_SESSION *rcs = pthread_getspecific(conn->tls);
if (rcs) {
sqlite3_rcvfs_tls_destructor((void *)rcs);
}
pthread_key_delete(conn->tls);
sqlite3_free(conn->locator);
sqlite3_free(conn->cluster_name);
sqlite3_free(conn->table_name);
sqlite3_free(conn);
}
//------------------------------------------------------------------------------
static int rcFlushBlockBuffer(
SQLITE_RCVFS_SESSION *rcs,
RcFile *p
){
DPRINTF("flush block buffer\n");
if (!p->blockBuffer) return SQLITE_OK;
uint16_t N = SQLITE_RCVFS_WBUF_NBLOCKS + 1;
uint16_t szMultiOpWrite = rc_multiOpSizeOf(MULTI_OP_WRITE);
SQLITE_RCVFS_BLOCKKEY *block_keys = (SQLITE_RCVFS_BLOCKKEY *)
alloca(N * sizeof(SQLITE_RCVFS_BLOCKKEY));
unsigned char *mWriteObjects = (unsigned char *) alloca(N * szMultiOpWrite);
void **pmWriteObjects = (void **)alloca(N * sizeof(void *));
unsigned num_requests = 0;
unsigned i;
for (i = 0; i < p->blockBuffer->gauge; ++i) {
block_keys[num_requests].dbid = p->handle.dbid;
block_keys[num_requests].blockid = p->blockBuffer->blockIds[i];
pmWriteObjects[num_requests] =
mWriteObjects + (num_requests * szMultiOpWrite);
rc_multiWriteCreate(rcs->conn->tblid,
&(block_keys[num_requests]), sizeof(SQLITE_RCVFS_BLOCKKEY),
p->blockBuffer->buf[num_requests],
p->blockBuffer->blockSizes[num_requests],
NULL, pmWriteObjects[num_requests]);
atomic_xadd64(&sqlite_rcvfs_szwrite,
p->blockBuffer->blockSizes[num_requests]);
num_requests++;
}
if (num_requests == 0) return SQLITE_OK;
// Write modified file size
SQLITE_RCVFS_DBHEADER dbheader;
memset(&dbheader, 0, sizeof(dbheader));
dbheader.version = 1;
dbheader.size = p->handle.size;
dbheader.blocksz = p->handle.blocksz;
block_keys[num_requests].dbid = p->handle.dbid;
block_keys[num_requests].blockid = SQLITE_RCVFS_HEADERBLOCK;
pmWriteObjects[num_requests] =
mWriteObjects + (num_requests * szMultiOpWrite);
rc_multiWriteCreate(rcs->conn->tblid,
&(block_keys[num_requests]), sizeof(SQLITE_RCVFS_BLOCKKEY),
&dbheader, sizeof(dbheader),
NULL, pmWriteObjects[num_requests]);
atomic_xadd64(&sqlite_rcvfs_szwrite, sizeof(dbheader));
num_requests++;
atomic_inc64(&sqlite_rcvfs_nmwrite);
rc_multiWrite(rcs->client, pmWriteObjects, num_requests);
memset(p->blockBuffer, 0, sizeof(p->blockBuffer));
for (i = 0; i < SQLITE_RCVFS_WBUF_NBLOCKS; ++i)
clearBufferBlock(p->blockBuffer, i);
p->blockBuffer->gauge = 0;
int result = SQLITE_OK;
for (i = 0; i < num_requests; ++i) {
Status status = rc_multiOpStatus(pmWriteObjects[i], MULTI_OP_WRITE);
if (status != STATUS_OK) result = SQLITE_IOERR;
rc_multiOpDestroy(pmWriteObjects[i], MULTI_OP_WRITE);
}
return result;
}
/**
* Write directly to the file passed as the first argument.
*/
static int rcBufferedWrite(
SQLITE_RCVFS_SESSION *rcs,
RcFile *p, /* File handle */
const void *zBuf, /* Buffer containing data to write */
unsigned iAmt, /* Size of data to write in bytes */
sqlite_int64 iOfst /* File offset to write to */
){
DPRINTF("write into block buffer %d %lld\n", iAmt, iOfst);
//hex_dump(zBuf, iAmt);
if (p->flags & SQLITE_OPEN_READONLY) return SQLITE_READONLY;
SQLITE_RCVFS_BLOCKKEY block_key;
block_key.dbid = p->handle.dbid;
block_key.blockid.blockno = iOfst / p->handle.blocksz;
// Write block-wise
Status status;
unsigned char *block = NULL;
unsigned remaining = iAmt;
unsigned pos_in_block = iOfst % p->handle.blocksz;
while (remaining > 0) {
block = NULL;
unsigned free_in_block = p->handle.blocksz - pos_in_block;
unsigned nbytes = (remaining > free_in_block) ? free_in_block : remaining;
uint64_t absolute_offset = iOfst + iAmt - remaining;
uint32_t this_blocksz = 0;
// Check in buffer
int in_cache = 0;
unsigned i;
for (i = 0; i < p->blockBuffer->gauge; ++i) {
if (p->blockBuffer->blockIds[i].blockno == block_key.blockid.blockno) {
block = p->blockBuffer->buf[i];
this_blocksz = p->blockBuffer->blockSizes[i];
in_cache = 1;
break;
}
}
// If not already in buffer, occupy a new block. Flush if necessary.
if (!in_cache) {
if (p->blockBuffer->gauge == SQLITE_RCVFS_WBUF_NBLOCKS) {
int retval = rcFlushBlockBuffer(rcs, p);
if (retval != SQLITE_OK) return retval;
}
unsigned idx = p->blockBuffer->gauge++;
block = p->blockBuffer->buf[idx];
p->blockBuffer->blockIds[idx] = block_key.blockid;
if ((pos_in_block + nbytes) > p->blockBuffer->blockSizes[idx])
p->blockBuffer->blockSizes[idx] = pos_in_block + nbytes;
}
// Read only if this is not a full block and not in cache and if there
// is a block for it in RAMCloud
if (!in_cache &&
(absolute_offset < p->permanentSize) &&
((pos_in_block != 0) || (remaining < p->handle.blocksz)))
{
atomic_inc64(&sqlite_rcvfs_nread);
status = rc_read(rcs->client, p->handle.tblid,
&block_key, sizeof(block_key), NULL, NULL,
block, p->handle.blocksz, &this_blocksz);
atomic_xadd64(&sqlite_rcvfs_szread, this_blocksz);
if ((status != STATUS_OK) && (status != STATUS_OBJECT_DOESNT_EXIST))
return SQLITE_IOERR_WRITE;
}
memcpy(block + pos_in_block, (const char *)zBuf+(iAmt-remaining), nbytes);
if ((pos_in_block + nbytes) > this_blocksz)
this_blocksz = pos_in_block + nbytes;
remaining -= nbytes;
pos_in_block = 0;
block_key.blockid.blockno++;
}
p->handle.size = (p->handle.size > iOfst + iAmt) ?
p->handle.size : iOfst + iAmt;
DPRINTF("buffered write OK, file size %lu\n", p->handle.size);
return SQLITE_OK;
}
static int rcDeleteInternal(
SQLITE_RCVFS_SESSION *rcs,
SQLITE_RCVFS_DBID dbid,
uint64_t blocksz,
uint64_t size
){
uint64_t max_block = size / blocksz;
unsigned nbatch = (max_block + 2) > 1024 ? 1024 : max_block + 2;
uint16_t szMultiOpRemove = rc_multiOpSizeOf(MULTI_OP_REMOVE);
DPRINTF("delete internal %lu blocks\n", max_block);
SQLITE_RCVFS_BLOCKKEY *block_keys = (SQLITE_RCVFS_BLOCKKEY *)
alloca(nbatch * sizeof(SQLITE_RCVFS_BLOCKKEY));
unsigned char *mRemoveObjects = (unsigned char *)
alloca(nbatch * szMultiOpRemove);
void **pmRemoveObjects = (void **)alloca(nbatch * sizeof(void *));
uint64_t nremoved = 0;
while (nremoved < max_block + 2) {
unsigned i;
for (i = 0; (i < nbatch) && (nremoved < (max_block + 2)); ++i, ++nremoved) {
block_keys[i].dbid = dbid;
block_keys[i].blockid.blockno = nremoved - 2;
pmRemoveObjects[i] = mRemoveObjects + (i * szMultiOpRemove);
rc_multiRemoveCreate(rcs->conn->tblid,
&(block_keys[i]), sizeof(SQLITE_RCVFS_BLOCKKEY),
NULL,
pmRemoveObjects[i]);
}
atomic_inc64(&sqlite_rcvfs_nmremove);
rc_multiRemove(rcs->client, pmRemoveObjects, i);
unsigned j;
for (j = 0; j < i; ++j)
rc_multiOpDestroy(pmRemoveObjects[j], MULTI_OP_REMOVE);
for (j = 0; j < i; ++j) {
Status status = rc_multiOpStatus(pmRemoveObjects[j], MULTI_OP_REMOVE);
if ((status != STATUS_OK) && (status != STATUS_OBJECT_DOESNT_EXIST))
return SQLITE_IOERR;
}
}
return SQLITE_OK;
}
/**
* Close a database. All changes must be already committed.
*/
static int rcClose(sqlite3_file *pFile) {
RcFile *p = (RcFile*)pFile;
DPRINTF("close (my token %d)\n", p->handle.token.digest[0] & 0xff);
sqlite3_free(p->blockBuffer);
sqlite3_free(p->handle.leases);
//int retval = p->base.pMethods->xUnlock(pFile, 0);
//assert(retval == SQLITE_OK);
if (p->flags & SQLITE_OPEN_DELETEONCLOSE) {
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR;
return rcDeleteInternal(rcs,
p->handle.dbid, p->handle.blocksz, p->permanentSize);
}
DPRINTF("RETURN close\n");
return SQLITE_OK;
}
/**
* Read data from a file.
*/
static int rcRead(
sqlite3_file *pFile,
void *zBuf,
int iAmt,
sqlite_int64 iOfst
){
//printf("R %d %lld\n", iAmt, iOfst);
DPRINTF("read %d %lld\n", iAmt, iOfst);
memset(zBuf, 0, iAmt);
RcFile *p = (RcFile*)pFile;
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR_READ;
if (iOfst >= p->handle.size) return SQLITE_IOERR_SHORT_READ;
// Read block-wise
unsigned char *block = (unsigned char *)alloca(p->handle.blocksz);
unsigned char *block_on_stack = block;
SQLITE_RCVFS_BLOCKKEY block_key;
block_key.dbid = p->handle.dbid;
block_key.blockid.blockno = iOfst / p->handle.blocksz;
uint64_t written = 0;
uint64_t remaining = iAmt;
unsigned pos_in_block = iOfst % p->handle.blocksz;
while (written < iAmt) {
block = block_on_stack;
uint32_t size_of_block = 0;
// Check in blockBuffer
if (p->blockBuffer) {
unsigned i;
for (i = 0; i < p->blockBuffer->gauge; ++i) {
if (p->blockBuffer->blockIds[i].blockno == block_key.blockid.blockno) {
block = p->blockBuffer->buf[i];
size_of_block = p->blockBuffer->blockSizes[i];
break;
}
}
}
// Not in buffer, fetch from RAMCloud
if (block == block_on_stack) {
DPRINTF("real block read\n");
atomic_inc64(&sqlite_rcvfs_nread);
Status status = rc_read(rcs->client, p->handle.tblid,
&block_key, sizeof(block_key), NULL, NULL,
block, p->handle.blocksz, &size_of_block);
atomic_xadd64(&sqlite_rcvfs_szread, size_of_block);
if ((status == STATUS_OBJECT_DOESNT_EXIST) &&
((iOfst == 0) || (written > 0)))
{
return SQLITE_IOERR_SHORT_READ;
}
//DPRINTF("read block returned %d\n", status);
if (status != STATUS_OK) return SQLITE_IOERR_READ;
}
if (size_of_block <= pos_in_block) return SQLITE_IOERR_READ;
size_of_block -= pos_in_block;
const unsigned nbytes =
(remaining > size_of_block) ? size_of_block : remaining;
memcpy((char *)zBuf + written, block + pos_in_block, nbytes);
block_key.blockid.blockno++;
written += nbytes;
remaining -= nbytes;
pos_in_block = 0;
}
DPRINTF("RETURN read was fine\n");
//hex_dump(zBuf, iAmt);
return SQLITE_OK;
}
/**
* Write data to a file.
*/
static int rcWrite(
sqlite3_file *pFile,
const void *zBuf,
int iAmt,
sqlite_int64 iOfst
){
//printf("W %d %lld\n", iAmt, iOfst);
DPRINTF("write %d %lld\n", iAmt, iOfst);
//hex_dump(zBuf, iAmt);
RcFile *p = (RcFile*)pFile;
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR_WRITE;
return rcBufferedWrite(rcs, p, zBuf, iAmt, iOfst);
}
/**
* Truncate a file. This is a no-op for this VFS (see header comments at
* the top of the file).
*/
// TODO
static int rcTruncate(sqlite3_file *pFile, sqlite_int64 size){
printf("truncate\n");
#if 0
if( ftruncate(((DemoFile *)pFile)->fd, size) ) return SQLITE_IOERR_TRUNCATE;
#endif
return SQLITE_OK;
}
/**
* Sync the contents of the file to the persistent media.
*/
static int rcSync(sqlite3_file *pFile, int flags){
DPRINTF("syncing\n");
RcFile *p = (RcFile*)pFile;
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR_FSYNC;
int retval;
retval = rcFlushBlockBuffer(rcs, p);
if (retval != SQLITE_OK) return retval;
DPRINTF("syncing ok\n");
return SQLITE_OK;
}
/**
* Write the size of the file in bytes to *pSize.
*/
static int rcFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
DPRINTF("file size\n");
RcFile *p = (RcFile*)pFile;
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR;
*pSize = p->handle.size;
DPRINTF("return file size %lld\n", *pSize);
return SQLITE_OK;
}
#define NO_LOCK 0
#define SHARED_LOCK 1
#define RESERVED_LOCK 2
#define PENDING_LOCK 3
#define EXCLUSIVE_LOCK 4
/**
* Reads the leases from the lock control block. The leases array has
* room for at least one more lock upon return.
* Returns the version number of the lock control block in lcbVersion.
*/
static int get_lockcb(
SQLITE_RCVFS_SESSION *rcs,
SQLITE_RCVFS_DBID dbid,
uint64_t tblid,
SQLITE_RCVFS_HANDLE *handle,
uint64_t *lcbVersion
){
Status status;
uint32_t nbytes = 0;
int short_read = 0;
SQLITE_RCVFS_BLOCKKEY block_key;
block_key.dbid = dbid;
block_key.blockid = SQLITE_RCVFS_LCBLOCK;
do {
nbytes = 0;
atomic_inc64(&sqlite_rcvfs_nread);
status =
rc_read(rcs->client, tblid,
&block_key, sizeof(block_key),
NULL, lcbVersion,
handle->leases, handle->capacityLeases*sizeof(SQLITE_RCVFS_LEASE),
&nbytes);
atomic_xadd64(&sqlite_rcvfs_szread, nbytes);
switch (status) {
case STATUS_OBJECT_DOESNT_EXIST:
nbytes = 0;
// Fall through
case STATUS_OK:
handle->nLeases = nbytes / sizeof(SQLITE_RCVFS_LEASE);
if (handle->nLeases >= handle->capacityLeases) {
short_read = 1;
handle->capacityLeases = handle->nLeases + 1;
handle->leases = (SQLITE_RCVFS_LEASE *)
sqlite3_realloc(handle->leases,
handle->capacityLeases * sizeof(SQLITE_RCVFS_LEASE));
} else {
short_read = 0;
}
break;
default:
return SQLITE_IOERR_LOCK;
}
} while (short_read);
DPRINTF("retrieved lock control block of size %d\n", handle->nLeases);
handle->versionLeases = *lcbVersion;
return SQLITE_OK;
}
static void cleanup_lockcb(SQLITE_RCVFS_HANDLE *handle, char max_lease_type) {
uint32_t i;
for (i = 0; i < handle->nLeases; ) {
int owned = is_owned(&(handle->leases[i]), &(handle->token));
int valid_lease = is_locked(&(handle->leases[i]));
if (owned && (handle->leases[i].lease_type == PENDING_LOCK))
valid_lease = 0;
if (owned && (handle->leases[i].lease_type > max_lease_type))
valid_lease = 0;
if (!valid_lease) {
DPRINTF("removing a lock (mylock: %d)\n",
is_owned(&(handle->leases[i]), &(handle->token)));
// Shrink array
uint32_t j;
for (j = i+1; j < handle->nLeases; ++j)
handle->leases[j-1] = handle->leases[j];
handle->nLeases--;
} else {
++i;
}
}
}
/**
* Locking.
*/
static int rcLock(sqlite3_file *pFile, int eLock){
RcFile *p = (RcFile *)pFile;
DPRINTF("lock %d (mytoken %d) (%p)\n",
eLock, p->handle.token.digest[0] & 0xff, pFile);
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR_LOCK;
uint64_t tblid = p->handle.tblid;
SQLITE_RCVFS_LEASE new_lease;
memset(&new_lease, 0, sizeof(new_lease));
new_lease.lease_type = eLock;
new_lease.token = p->handle.token;
new_lease.deadline = time(NULL) + SQLITE_RCVFS_LEASETIME;
int result;
uint64_t lcbVersion = 0;
do {
result = -1;
if ((p->handle.versionLeases == 0) || (lcbVersion > 0)) {
int retval;
retval = get_lockcb(rcs, p->handle.dbid, tblid, &(p->handle), &lcbVersion);
if (retval != SQLITE_OK) return retval;
} else {
lcbVersion = p->handle.versionLeases;
}
struct RejectRules rrules;
memset(&rrules, 0, sizeof(rrules));
if (lcbVersion > 0) {
rrules.givenVersion = lcbVersion;
rrules.versionNeGiven = 1;
}
cleanup_lockcb(&(p->handle), EXCLUSIVE_LOCK);
int other_shared = 0;
int other_reserved = 0;
int other_pending = 0;
int other_exclusive = 0;
unsigned i;
for (i = 0; i < p->handle.nLeases; ++i) {
DPRINTF("found a lock of type %d, token %d (mytoken %d) (%p)\n",
p->handle.leases[i].lease_type & 0xff,
p->handle.leases[i].token.digest[0] & 0xff,
p->handle.token.digest[0] & 0xff,
p);
if (is_owned(&(p->handle.leases[i]), &(p->handle.token))) {
// Do I have already a lock of the required type or better?
if (p->handle.leases[i].lease_type >= eLock) return SQLITE_OK;
} else {
// Not my locks
if (p->handle.leases[i].lease_type == SHARED_LOCK) other_shared = 1;
if (p->handle.leases[i].lease_type == RESERVED_LOCK) other_reserved = 1;
if (p->handle.leases[i].lease_type == PENDING_LOCK) other_pending = 1;
if (p->handle.leases[i].lease_type == EXCLUSIVE_LOCK)
other_exclusive = 1;
}
}
switch (eLock) {
case SHARED_LOCK:
if (!other_pending && !other_exclusive) result = SQLITE_OK;
else result = SQLITE_BUSY;
break;
case RESERVED_LOCK:
if (!other_reserved && !other_pending && !other_exclusive)
result = SQLITE_OK;
else
result = SQLITE_BUSY;
break;
case EXCLUSIVE_LOCK:
if (other_shared || other_reserved || other_pending || other_exclusive)
result = SQLITE_BUSY;
else
result = SQLITE_OK;
break;
default:
result = SQLITE_ERROR;
}
int new_lockcb = 0;
if (result == SQLITE_OK) new_lockcb = 1;
if ((result == SQLITE_BUSY) && (eLock == EXCLUSIVE_LOCK) &&
!other_pending && !other_reserved)
{
new_lockcb = 1;
new_lease.lease_type = PENDING_LOCK;
}
if (new_lockcb) {
DPRINTF("writing new lockcb of size %d (%p)\n", p->handle.nLeases+1, p);
p->handle.leases[p->handle.nLeases] = new_lease;
p->handle.nLeases++;
SQLITE_RCVFS_BLOCKKEY block_key;
block_key.dbid = p->handle.dbid;
block_key.blockid = SQLITE_RCVFS_LCBLOCK;
atomic_inc64(&sqlite_rcvfs_nwrite);
Status status =
rc_write(rcs->client, tblid,
&block_key, sizeof(block_key),
p->handle.leases, p->handle.nLeases*sizeof(SQLITE_RCVFS_LEASE),
&rrules, &lcbVersion);
switch (status) {
case STATUS_OK:
atomic_xadd64(&sqlite_rcvfs_szwrite,
p->handle.nLeases * sizeof(SQLITE_RCVFS_LEASE));
p->handle.versionLeases = lcbVersion;
break;
case STATUS_OBJECT_EXISTS:
case STATUS_OBJECT_DOESNT_EXIST:
case STATUS_WRONG_VERSION:
result = -1;
break;
default:
result = SQLITE_IOERR_LOCK;
}
}
} while (result < 0);
DPRINTF("lock result %d (%p)\n", result, p);
return result;
}
static int rcUnlock(sqlite3_file *pFile, int eLock) {
RcFile *p = (RcFile *)pFile;
DPRINTF("unlock to new level %d (mytoken %d) (%p)\n",
eLock, p->handle.token.digest[0] & 0xff, pFile);
SQLITE_RCVFS_SESSION *rcs = get_rc_session(p->handle.conn);
if (!rcs) return SQLITE_IOERR_LOCK;
uint64_t tblid = p->handle.tblid;
int result;
uint64_t lcbVersion = 0;
do {
if (lcbVersion > 0) {
int retval;
retval = get_lockcb(rcs, p->handle.dbid, tblid, &(p->handle), &lcbVersion);
if (retval != SQLITE_OK) return retval;
DPRINTF("retrieved %d leases (%p)\n", p->handle.nLeases, p);
if (p->handle.nLeases == 0) return SQLITE_OK;
} else {
lcbVersion = p->handle.versionLeases;
}
struct RejectRules rrules;
memset(&rrules, 0, sizeof(rrules));
rrules.givenVersion = lcbVersion;
rrules.versionNeGiven = 1;
cleanup_lockcb(&(p->handle), NO_LOCK);
if (eLock == SHARED_LOCK) {
SQLITE_RCVFS_LEASE new_lease;
memset(&new_lease, 0, sizeof(new_lease));
new_lease.lease_type = SHARED_LOCK;
new_lease.token = p->handle.token;
new_lease.deadline = time(NULL) + SQLITE_RCVFS_LEASETIME;
p->handle.leases[p->handle.nLeases] = new_lease;
p->handle.nLeases++;
}
DPRINTF("cleanup+mod: now %d leases (%p)\n", p->handle.nLeases, p);
SQLITE_RCVFS_BLOCKKEY block_key;
block_key.dbid = p->handle.dbid;
block_key.blockid = SQLITE_RCVFS_LCBLOCK;
Status status;
uint32_t nbytes = p->handle.nLeases * sizeof(SQLITE_RCVFS_LEASE);
if (nbytes == 0) nbytes = 1;
atomic_inc64(&sqlite_rcvfs_nwrite);
status = rc_write(rcs->client, tblid,
&block_key, sizeof(block_key),
p->handle.leases, nbytes,