-
Notifications
You must be signed in to change notification settings - Fork 15
/
shard.cc
3317 lines (2969 loc) · 123 KB
/
shard.cc
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
/*
* ===============================================================
* Description: Core database functionality for a shard server
*
* Created: 07/25/2013 04:02:37 PM
*
* Author: Ayush Dubey, dubey@cs.cornell.edu
* Greg Hill, gdh39@cornell.edu
*
* Copyright (C) 2013, Cornell University, see the LICENSE file
* for licensing agreement
* ===============================================================
*/
#include <deque>
#include <fstream>
#include <string>
#include <random>
#include <signal.h>
#include <e/popt.h>
#include <e/buffer.h>
#include <pugixml.hpp>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#include <dlfcn.h>
#include <wordexp.h>
#define weaver_debug_
#include "common/weaver_constants.h"
#include "common/event_order.h"
#include "common/clock.h"
#include "common/prog_write_and_dlopen.h"
#include "db/shard.h"
#include "db/message_wrapper.h"
#include "db/remote_node.h"
#include "db/node_prog_running_state.h"
#include "node_prog/node.h"
#include "node_prog/base_classes.h"
#define XML_CHUNK_SZ 10000
DECLARE_CONFIG_CONSTANTS;
using db::node_version_t;
using vc::vclock_ptr_t;
using node_prog::Node_Parameters_Base;
using node_prog::Node_State_Base;
using node_prog::np_param_ptr_t;
using node_prog::np_state_ptr_t;
using node_prog::param_ctor_func_t;
using node_prog::param_size_func_t;
using node_prog::param_pack_func_t;
using node_prog::param_unpack_func_t;
using node_prog::state_ctor_func_t;
using node_prog::state_size_func_t;
using node_prog::state_pack_func_t;
using node_prog::state_unpack_func_t;
// global static variables
static uint64_t shard_id;
// shard pointer for shard.cc
static db::shard *S;
void migrated_nbr_update(std::unique_ptr<message::message> msg);
bool migrate_node_step1(uint64_t tid, db::node*, std::vector<uint64_t>&, uint64_t);
void migrate_node_step2_req(uint64_t tid);
void migrate_node_step2_resp(uint64_t tid, std::unique_ptr<message::message> msg, order::oracle *time_oracle);
bool check_step3();
void migrate_node_step3(uint64_t tid);
void migration_begin(uint64_t tid);
void migration_wrapper(uint64_t tid);
void migration_end();
// parse the string 'line' as a uint64_t starting at index 'idx' till the first whitespace or end of string
// store result in 'n'
// if overflow occurs or unexpected char encountered, store true in 'bad'
inline void
parse_single_uint64(std::string &line, size_t &idx, uint64_t &n, bool &bad)
{
uint64_t next_digit;
static uint64_t zero = '0';
static uint64_t max64_div10 = UINT64_MAX / 10;
n = 0;
while (line[idx] != ' '
&& line[idx] != '\t'
&& line[idx] != '\r'
&& line[idx] != '\n'
&& idx < line.length()) {
next_digit = line[idx] - zero;
if (next_digit > 9) { // unexpected char
bad = true;
WDEBUG << "Unexpected char with ascii " << (int)line[idx]
<< " in parsing int, num currently is " << n << std::endl;
break;
}
if (n > max64_div10) { // multiplication overflow
bad = true;
WDEBUG << "multiplication overflow" << std::endl;
break;
}
n *= 10;
if ((n + next_digit) < n) { // addition overflow
bad = true;
WDEBUG << "addition overflow" << std::endl;
break;
}
n += next_digit;
++idx;
}
}
inline void
skip_whitespace(std::string &line, size_t &i)
{
while (i < line.length() && (line[i] == ' '
|| line[i] == '\r'
|| line[i] == '\n'
|| line[i] == '\t')) {
++i;
}
}
// parse the string 'line' as '<unsigned int> <unsigned int> '
// there can be arbitrary whitespace between the two ints, and after the second int
// store the two parsed ints in 'n1' and 'n2'
// if there is overflow or unexpected char is encountered, return n1 = n2 = 0
inline size_t
parse_two_uint64(std::string &line, uint64_t &n1, uint64_t &n2)
{
size_t i = 0;
bool bad = false; // overflow or unexpected char
parse_single_uint64(line, i, n1, bad);
if (bad || i == line.length()) {
n1 = 0;
n2 = 0;
WDEBUG << "Parsing error, line: " << line << std::endl;
return -1;
}
skip_whitespace(line, i);
parse_single_uint64(line, i, n2, bad);
if (bad) {
n1 = 0;
n2 = 0;
WDEBUG << "Parsing error" << std::endl;
}
skip_whitespace(line, i);
return i;
}
inline void
parse_weaver_edge(std::string &line, uint64_t &n1, uint64_t &n2,
std::vector<std::pair<std::string, std::string>> &props)
{
size_t i = parse_two_uint64(line, n1, n2);
while (i < line.length()) {
size_t start1 = i;
while (line[i] != ' ' && line[i] != '\t') {
i++;
}
size_t len1 = i - start1;
skip_whitespace(line, i);
size_t start2 = i;
while (i < line.length() && (line[i] != ' '
&& line[i] != '\t'
&& line[i] != '\n'
&& line[i] != '\r')) {
i++;
}
size_t len2 = i - start2;
skip_whitespace(line, i);
props.emplace_back(std::make_pair(line.substr(start1, len1), line.substr(start2, len2)));
}
}
inline void
split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
struct xml_element
{
uint32_t elem_idx;
uint64_t elem_count;
pugi::xml_document doc;
uint64_t owner_shard;
int owner_tid;
node_handle_t node;
edge_handle_t edge;
// shard id should match
bool belongs_to_shard() { return owner_shard == shard_id; }
// shard id and thread id should match
bool belongs_to_us(int tid)
{
return belongs_to_shard() && (owner_tid == tid);
}
// shard id should match, thread id should be one of tids
bool belongs_to_us(const std::vector<int> &tids)
{
if (!belongs_to_shard()) {
return false;
}
for (int tid: tids) {
if (owner_tid == tid) {
return true;
}
}
return false;
}
};
bool
get_xml_element_chunk(std::ifstream &file,
const std::vector<std::string> &names,
std::vector<xml_element> &elements,
uint32_t &elem_count,
int load_nthreads,
uint64_t num_shards)
{
size_t pos;
std::vector<std::string> start, end;
for (const std::string &n: names) {
start.emplace_back("<"+n);
end.emplace_back("</"+n+">");
}
assert(elements.size() == XML_CHUNK_SZ);
elem_count = 0;
for (uint32_t idx = 0; idx < XML_CHUNK_SZ && !file.eof(); idx++) {
std::string line, element;
uint32_t cur_elem_idx = UINT32_MAX;
bool in_elem = false;
while (std::getline(file, line)) {
bool appended = false;
bool done_elem = false;
for (uint32_t i = 0; i < names.size(); i++) {
if ((pos = line.find(start[i])) != std::string::npos) {
if (in_elem) {
WDEBUG << "unexpected xml format\n"
<< "element=" << element << std::endl
<< "line=" << line << std::endl;
WDEBUG << "aborting bulk load at current line" << std::endl;
return false;
}
in_elem = true;
element.append(line);
appended = true;
cur_elem_idx = i;
}
if ((pos = line.find(end[i])) != std::string::npos) {
if (!in_elem) {
WDEBUG << "unexpected xml format\n"
<< "element=" << element << std::endl
<< "line=" << line << std::endl;
WDEBUG << "aborting bulk load at current line" << std::endl;
return false;
}
assert(i == cur_elem_idx);
if (!appended) {
element.append(line);
appended = true;
}
done_elem = true;
break;
}
if (!appended && in_elem) {
element.append(line);
appended = true;
}
}
if (done_elem) {
break;
}
}
if (!element.empty()) {
elem_count = idx+1;
xml_element &xml_elem = elements[idx];
xml_elem.elem_idx = cur_elem_idx;
if (!xml_elem.doc.load_buffer(element.c_str(), element.size())) {
WDEBUG << "could not load xml\n"
<< "element=" << element << std::endl;
WDEBUG << "aborting bulk load at current line" << std::endl;
return false;
}
// find owner thread and shard
std::string id0, id1;
if (xml_elem.elem_idx == 0) { // node
pugi::xml_node node = xml_elem.doc.child("node");
id0 = node.attribute("id").value();
} else { // edge
pugi::xml_node edge = xml_elem.doc.child("edge");
id0 = edge.attribute("source").value();
id1 = edge.attribute("id").value();
xml_elem.edge = id1;
}
xml_elem.node = id0;
xml_elem.owner_shard = (hash_node_handle(id0) % num_shards) + ShardIdIncr;
xml_elem.owner_tid = (int)get_map_idx(id0) % load_nthreads;
} else {
assert(file.eof());
}
}
return true;
}
void
check_btc_node(const node_handle_t &h)
{
assert(h[0] == '1'
|| h[0] == '2'
|| h[0] == '3'
|| h[0] == '4'
|| h[0] == '5'
|| h[0] == '6'
|| h[0] == '7'
|| h[0] == '8'
|| h[0] == '9'
|| h[0] == '0'
|| h.substr(0, 5) == "BLOCK"
|| h.substr(0, 4) == "COIN");
}
struct load_graph_data
{
db::graph_file_format format;
const char *graph_file;
uint64_t num_shards;
int load_tid;
int load_nthreads;
bool btc_graph;
bool call_hdex;
std::vector<std::shared_ptr<db::hyper_stub>> hstubs;
};
struct load_xml_elem_static_args
{
uint64_t num_shards;
int load_nthreads;
vclock_ptr_t zero_clk;
bool prop_delim;
uint64_t cur_shard_node_count;
uint64_t nodes_in_memory;
uint64_t total_elem_count;
uint64_t cur_shard_edge_count;
uint64_t edges_in_memory;
uint64_t other_thread_elem_count;
bool btc_graph;
bool call_hdex;
uint64_t block_index;
load_xml_elem_static_args(const load_graph_data &data,
bool pdelim)
: num_shards(data.num_shards)
, load_nthreads(data.load_nthreads)
, zero_clk(new vc::vclock(0,0))
, prop_delim(pdelim)
, cur_shard_node_count(0)
, nodes_in_memory(0)
, total_elem_count(0)
, cur_shard_edge_count(0)
, edges_in_memory(0)
, other_thread_elem_count(0)
, btc_graph(data.btc_graph)
, call_hdex(data.call_hdex)
, block_index(0)
{ }
};
// ASSUME atmost 1800 shards
#define MAX_EDGES_PER_NODE 100000000ULL // at most 100M edges per node
#define MAX_NODES_PER_SHARD 100000000ULL // at most 100M nodes per shard
void
load_xml_node(pugi::xml_document &doc,
int owner_tid,
int this_tid,
db::hyper_stub &hstub,
db::hyper_stub &other_hstub,
load_xml_elem_static_args &static_args,
uint64_t node_count)
{
uint64_t &num_shards = static_args.num_shards;
int &load_nthreads = static_args.load_nthreads;
vclock_ptr_t zero_clk = static_args.zero_clk;
bool prop_delim = static_args.prop_delim;
uint64_t &cur_shard_node_count = static_args.cur_shard_node_count;
uint64_t &nodes_in_memory = static_args.nodes_in_memory;
bool btc_graph = static_args.btc_graph;
// get xml node from doc
pugi::xml_node node = doc.child("node");
// init node attributes
node_handle_t id0 = node.attribute("id").value();
uint64_t hash0 = hash_node_handle(id0);
uint64_t loc = (hash0 % num_shards) + ShardIdIncr;
uint64_t map_idx = get_map_idx(id0);
assert((loc == shard_id) && ((int)map_idx % load_nthreads == owner_tid));
db::node *n = S->create_node_bulk_load(id0, map_idx, zero_clk);
std::string block_index;
for (pugi::xml_node prop: node.children("data")) {
std::string key = prop.attribute("key").value();
std::string value = prop.child_value();
if (!prop_delim || value.empty()) {
(key == BulkLoadNodeAliasKey)? S->add_node_alias_bulk_load(n, value) :
S->set_node_property_bulk_load(n, key, value, zero_clk);
} else {
std::vector<std::string> values;
split(value, BulkLoadPropertyValueDelimiter, values);
for (std::string &v: values) {
(key == BulkLoadNodeAliasKey)? S->add_node_alias_bulk_load(n, v) :
S->set_node_property_bulk_load(n, key, v, zero_clk);
}
}
if (key == "index" || key == "block") {
block_index = value;
}
}
if (!block_index.empty()) {
size_t parse_idx;
uint64_t blk;
bool parse_bad = false;
parse_single_uint64(block_index, parse_idx, blk, parse_bad);
assert(!parse_bad);
if (blk > static_args.block_index) {
static_args.block_index = blk;
}
}
if (++cur_shard_node_count % 10000 == 0) {
WDEBUG << "GRAPHML tid=" << this_tid
<< " node=" << cur_shard_node_count
<< " nodes_in_mem=" << nodes_in_memory
<< " other_thread_elem_count=" << static_args.other_thread_elem_count
<< std::endl;
}
uint64_t start_edge_idx;
if (btc_graph) {
check_btc_node(id0);
size_t parse_idx = 0;
bool parse_bad = false;
if (id0[0] == 'B') {
uint64_t node_idx;
parse_single_uint64(block_index, parse_idx, node_idx, parse_bad);
start_edge_idx = (40000000ULL + node_idx) * MAX_EDGES_PER_NODE;
} else if (id0[0] == 'C') {
start_edge_idx = (50000000ULL + 0) * MAX_EDGES_PER_NODE;
} else {
uint64_t node_idx;
parse_single_uint64(id0, parse_idx, node_idx, parse_bad);
start_edge_idx = (1 + node_idx) * MAX_EDGES_PER_NODE;
}
assert(!parse_bad);
} else {
//start_edge_idx = (shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE
// + node_count*MAX_EDGES_PER_NODE;
start_edge_idx = node_count*MAX_EDGES_PER_NODE;
//uint64_t start_shard_range = (shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE;
//uint64_t end_shard_range = (1+shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE - 1;
//if (start_edge_idx < start_shard_range || start_edge_idx > end_shard_range) {
// WDEBUG << "edge_idx=" << start_edge_idx
// << ", start_range=" << start_shard_range
// << ", end_range=" << end_shard_range
// << std::endl;
//}
//assert(start_edge_idx >= start_shard_range && start_edge_idx <= end_shard_range);
}
bool already_exists = other_hstub.new_node(n->get_handle(), start_edge_idx);
if (!already_exists) {
bool in_mem = S->add_node_to_nodemap_bulk_load(n, map_idx, static_args.block_index);
if (static_args.call_hdex) {
S->bulk_load_put_node(hstub, n, in_mem);
} else if (!in_mem) {
S->permanent_node_delete(n);
}
if (in_mem) {
nodes_in_memory++;
}
} else {
delete n;
}
}
void
check_node(const node_handle_t &node_handle,
int owner_tid,
int this_tid,
db::hyper_stub &hstub,
db::hyper_stub &other_hstub,
load_xml_elem_static_args &static_args,
uint64_t node_count)
{
if (S->node_exists_bulk_load(node_handle)) {
return;
}
uint64_t &num_shards = static_args.num_shards;
int &load_nthreads = static_args.load_nthreads;
vclock_ptr_t zero_clk = static_args.zero_clk;
uint64_t &cur_shard_node_count = static_args.cur_shard_node_count;
uint64_t &nodes_in_memory = static_args.nodes_in_memory;
// init node attributes
const node_handle_t &id0 = node_handle;
uint64_t hash0 = hash_node_handle(id0);
uint64_t loc = (hash0 % num_shards) + ShardIdIncr;
uint64_t map_idx = get_map_idx(id0);
assert((loc == shard_id) && ((int)map_idx % load_nthreads == owner_tid));
db::node *n = S->create_node_bulk_load(id0, map_idx, zero_clk);
if (n == nullptr) {
// node already exists
return;
}
if (++cur_shard_node_count % 10000 == 0) {
WDEBUG << "GRAPHML tid=" << this_tid
<< " node=" << cur_shard_node_count
<< " nodes_in_mem=" << nodes_in_memory
<< " other_thread_elem_count=" << static_args.other_thread_elem_count
<< std::endl;
}
uint64_t start_edge_idx;
//start_edge_idx = (shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE
// + node_count*MAX_EDGES_PER_NODE;
start_edge_idx = node_count*MAX_EDGES_PER_NODE;
//uint64_t start_shard_range = (shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE;
//uint64_t end_shard_range = (1+shard_id-ShardIdIncr)*MAX_NODES_PER_SHARD*MAX_EDGES_PER_NODE - 1;
//if (start_edge_idx < start_shard_range || start_edge_idx > end_shard_range) {
// WDEBUG << "edge_idx=" << start_edge_idx
// << ", start_range=" << start_shard_range
// << ", end_range=" << end_shard_range
// << std::endl;
//}
//assert(start_edge_idx >= start_shard_range && start_edge_idx <= end_shard_range);
bool already_exists = other_hstub.new_node(n->get_handle(), start_edge_idx);
assert(!already_exists);
bool in_mem = S->add_node_to_nodemap_bulk_load(n, map_idx, static_args.block_index);
if (static_args.call_hdex) {
S->bulk_load_put_node(hstub, n, in_mem);
} else if (!in_mem) {
S->permanent_node_delete(n);
}
if (in_mem) {
nodes_in_memory++;
}
}
//#undef MAX_EDGES_PER_NODE
//#undef MAX_NODES_PER_SHARD
void
load_xml_edge(pugi::xml_document &doc,
int owner_tid,
int this_tid,
db::hyper_stub &hstub,
db::hyper_stub &other_hstub,
load_xml_elem_static_args &static_args,
uint64_t elem_count)
{
uint64_t &num_shards = static_args.num_shards;
int &load_nthreads = static_args.load_nthreads;
vclock_ptr_t zero_clk = static_args.zero_clk;
bool prop_delim = static_args.prop_delim;
uint64_t &cur_shard_edge_count = static_args.cur_shard_edge_count;
uint64_t &edges_in_memory = static_args.edges_in_memory;
// get xml edge
pugi::xml_node edge = doc.child("edge");
// initialize edge attributes
node_handle_t id0 = edge.attribute("source").value();
uint64_t hash0 = hash_node_handle(id0);
uint64_t loc0 = (hash0 % num_shards) + ShardIdIncr;
uint64_t map_idx = get_map_idx(id0);
assert((loc0 == shard_id) && ((int)map_idx % load_nthreads == owner_tid));
node_handle_t id1 = edge.attribute("target").value();
edge_handle_t edge_handle = edge.attribute("id").value();
uint64_t loc1 = (hash_node_handle(id1) % num_shards) + ShardIdIncr;
check_node(id0,
owner_tid, this_tid,
hstub, other_hstub,
static_args,
elem_count);
db::edge *e = S->create_edge_bulk_load(edge_handle, id1, loc1, zero_clk);
bool in_mem = S->add_edge_to_node_bulk_load(e, id0, map_idx);
if (in_mem || static_args.call_hdex) {
for (pugi::xml_node prop: edge.children("data")) {
std::string key = prop.attribute("key").value();
std::string value = prop.child_value();
if (!prop_delim || value.empty()) {
S->set_edge_property_bulk_load(e, key, value, zero_clk);
} else {
std::vector<std::string> values;
split(value, BulkLoadPropertyValueDelimiter, values);
for (std::string &v: values) {
S->set_edge_property_bulk_load(e, key, v, zero_clk);
}
}
}
if (static_args.call_hdex) {
uint64_t edge_id = other_hstub.new_edge(id0);
S->bulk_load_put_edge(hstub, e, id0, edge_id, in_mem);
}
}
if (in_mem) {
edges_in_memory++;
}
if (++cur_shard_edge_count % 10000 == 0) {
WDEBUG << "GRAPHML tid=" << this_tid
<< " edge=" << cur_shard_edge_count
<< " edges_in_mem=" << edges_in_memory
<< " other_thread_elem_count=" << static_args.other_thread_elem_count
<< std::endl;
}
}
void
load_xml_element(xml_element &elem,
uint64_t this_tid,
db::hyper_stub &my_hstub,
db::hyper_stub &owner_hstub,
load_xml_elem_static_args &static_args)
{
if (elem.elem_idx == 0) { // node
load_xml_node(elem.doc,
elem.owner_tid,
this_tid,
my_hstub,
owner_hstub,
static_args,
elem.elem_count);
} else { // edge
load_xml_edge(elem.doc,
elem.owner_tid,
this_tid,
my_hstub,
owner_hstub,
static_args,
elem.elem_count);
}
}
// initial bulk graph loading method
// 'format' stores the format of the graph file
// 'graph_file' stores the full path filename of the graph file
inline void*
load_graph(void *args)
{
load_graph_data *data = (load_graph_data*)args;
db::graph_file_format &format = data->format;
const char *graph_file = data->graph_file;
uint64_t num_shards = data->num_shards;
int load_tid = data->load_tid;
int load_nthreads = data->load_nthreads;
std::vector<std::shared_ptr<db::hyper_stub>> &hstubs = data->hstubs;
std::ifstream file;
std::string line;
file.open(graph_file, std::ifstream::in);
if (!file) {
WDEBUG << "File not found" << std::endl;
return nullptr;
}
file.seekg(0, file.end);
size_t graph_file_sz = file.tellg();
file.seekg(0, file.beg);
// read, validate, and create graph
uint64_t line_count = 0;
db::hyper_stub &hstub = *hstubs[load_tid];
switch(format) {
case db::SNAP: {
vclock_ptr_t zero_clk(new vc::vclock(0,0));
uint64_t cur_shard_node_count = 0;
uint64_t cur_shard_edge_count = 0;
while (std::getline(file, line)) {
line_count++;
if (line_count % 100000 == 0) {
WDEBUG << "SNAP bulk loading: processed " << line_count << " lines, cur shard stats: "
<< cur_shard_node_count << " nodes, " << cur_shard_edge_count << " edges." << std::endl;
}
/*
if ((line.length() == 0) || (line[0] == '#')) {
continue;
} else {
uint64_t node0, node1;
parse_two_uint64(line, node0, node1);
++edge_count;
node_handle_t id0 = std::to_string(node0);
uint64_t hash0 = hash_node_handle(id0);
uint64_t loc0 = ((hash0 % num_shards) + ShardIdIncr);
node_handle_t id1 = std::to_string(node1);
uint64_t hash1 = hash_node_handle(id1);
uint64_t loc1 = ((hash1 % num_shards) + ShardIdIncr);
uint64_t map_idx = get_map_idx(id0);
if ((loc0 == shard_id)
&& ((int)map_idx % load_nthreads == load_tid)) {
edge_handle_t edge_handle = BulkLoadEdgeHandlePrefix + std::to_string(edge_count);
db::node *n = nullptr;
bool node_in_mem, created;
if (S->bulk_load_node_exists(id0, map_idx)) {
n = S->bulk_load_acquire_node(id0, map_idx);
created = false;
} else {
n = S->create_node_bulk_load(id0, map_idx, zero_clk, node_in_mem);
created = true;
cur_shard_node_count++;
}
db::edge *e = S->create_edge_bulk_load(n, edge_handle, id1, loc1, zero_clk);
cur_shard_edge_count++;
if (created) {
S->bulk_load_put_node(hstub, n, node_in_mem, 0);
} else {
S->bulk_load_put_edge(hstub, e, id0, n, "");
}
S->bulk_load_flush_map(hstub);
}
if (loc1 == shard_id) {
db::node *n = nullptr;
bool node_in_mem;
uint64_t map_idx = get_map_idx(id1);
if ((int)map_idx % load_nthreads == load_tid) {
if (!S->bulk_load_node_exists(id1, map_idx)) {
n = S->create_node_bulk_load(id1, map_idx, zero_clk, node_in_mem);
cur_shard_node_count++;
}
}
if (n != nullptr) {
S->bulk_load_put_node(hstub, n, node_in_mem, 0);
}
S->bulk_load_flush_map(hstub);
}
}
*/
}
S->bulk_load_persistent(hstub, data->call_hdex);
break;
}
case db::GRAPHML: {
std::vector<xml_element> elements(XML_CHUNK_SZ);
load_xml_elem_static_args static_xml_args(*data, (BulkLoadPropertyValueDelimiter != '\0'));
std::vector<std::string> elem_name_vec;
elem_name_vec.emplace_back("node");
elem_name_vec.emplace_back("edge");
uint64_t chunk_count = 1;
uint32_t elem_count;
float prev_progress = 0;
wclock::weaver_timer timer;
uint64_t start_time = timer.get_real_time_millis();
while (get_xml_element_chunk(file, elem_name_vec, elements, elem_count, load_nthreads, num_shards)) {
if (elem_count == 0) {
break;
}
float file_cur_pos = file.tellg();
float progress = ((float)file_cur_pos)/graph_file_sz * 100;
if (progress - prev_progress >= 0.1) {
float time_elapsed = (timer.get_real_time_millis() - start_time) / 1000.0;
WDEBUG << "GRAPHML load progress=" << progress
<< "\ttime=" << time_elapsed << " s."
<< std::endl;
prev_progress = progress;
}
// first load elements that belong to this thread
for (uint32_t i = 0; i < elem_count; i++) {
elements[i].elem_count = ++static_xml_args.total_elem_count;
if (elements[i].belongs_to_us(load_tid)) {
bool loaded_chunk, loaded_elem;
hstub.check_loaded_chunk(chunk_count, i, loaded_chunk, loaded_elem);
assert(!loaded_chunk);
if (!loaded_elem) {
load_xml_element(elements[i],
load_tid,
hstub, hstub,
static_xml_args);
}
}
}
// done with my elements
hstub.mark_done_chunk(chunk_count);
// now load other threads elems
std::vector<int> done_tids(1, load_tid);
for (uint32_t i = 0; i < elem_count; i++) {
xml_element &elem = elements[i];
// don't load another shards elements
if (!elem.belongs_to_shard()) {
continue;
}
// don't load elements that have already been loaded
if (elem.belongs_to_us(done_tids)) {
continue;
}
// don't load other thread's node
if (elem.elem_idx == 0) {
continue;
}
// node not created, skip edge
// the edge will eventually be added by another thread once the node has been loaded
if (!S->node_exists_bulk_load(elem.node)) {
continue;
}
// now try loading elem
db::hyper_stub &other_hstub = *hstubs[elem.owner_tid];
bool loaded_chunk, loaded_elem;
other_hstub.check_loaded_chunk(chunk_count, i, loaded_chunk, loaded_elem);
if (loaded_chunk) {
bool found = false;
for (uint32_t idx = 0; idx < done_tids.size(); idx++) {
if (done_tids[idx] == elem.owner_tid) {
found = true;
break;
}
}
if (!found) {
done_tids.emplace_back(elem.owner_tid);
assert((int64_t)done_tids.size() <= NUM_SHARD_THREADS);
}
} else if (!loaded_elem) {
load_xml_element(elem,
load_tid,
hstub, other_hstub,
static_xml_args);
static_xml_args.other_thread_elem_count++;
}
}
// wait for all threads to finish with current chunk
for (int i = 0; i < NUM_SHARD_THREADS; i++) {
if (i == load_tid) {
continue;
}
hstubs[i]->wait_done_chunk(chunk_count);
}
// all threads done with all elems in this chunk
chunk_count++;
}
file.close();
S->bulk_load_persistent(hstub, data->call_hdex);
break;
}
default:
WDEBUG << "Unsupported graph file format " << format << std::endl;
return nullptr;
}
file.close();
return nullptr;
}
void
migrated_nbr_update(std::unique_ptr<message::message> msg)
{
node_handle_t node;
uint64_t old_loc, new_loc;
msg->unpack_message(message::MIGRATED_NBR_UPDATE, nullptr, node, old_loc, new_loc);
S->update_migrated_nbr(node, old_loc, new_loc);
}
void
migrated_nbr_ack(uint64_t from_loc, std::vector<vc::vclock_t> &target_prog_clk, uint64_t node_count)
{
S->migration_mutex.lock();
for (uint64_t i = 0; i < NumVts; i++) {
if (order::oracle::happens_before_no_kronos(S->target_prog_clk[i], target_prog_clk[i])) {
S->target_prog_clk[i] = target_prog_clk[i];
}
}
S->migr_edge_acks[from_loc - ShardIdIncr] = true;
S->shard_node_count[from_loc - ShardIdIncr] = node_count;
S->migration_mutex.unlock();
}
void
unpack_migrate_request(uint64_t tid, db::message_wrapper *request)
{
switch (request->type) {
case message::MIGRATED_NBR_UPDATE:
migrated_nbr_update(std::move(request->msg));
break;
case message::MIGRATE_SEND_NODE:
migrate_node_step2_resp(tid, std::move(request->msg), request->time_oracle);
break;
case message::MIGRATED_NBR_ACK: {
uint64_t from_loc, node_count;
std::vector<vc::vclock_t> target_prog_clk;
request->msg->unpack_message(request->type, nullptr, from_loc, target_prog_clk, node_count);
migrated_nbr_ack(from_loc, target_prog_clk, node_count);
break;
}
default:
WDEBUG << "unknown type" << std::endl;
}
delete request;
}
void
apply_writes(uint64_t tid, uint64_t vt_id, vclock_ptr_t vclk, uint64_t qts, transaction::pending_tx &tx)
{
// apply all writes
// acquire_node_write blocks if a preceding write has not been executed
for (auto upd: tx.writes) {
switch (upd->type) {
case transaction::EDGE_CREATE_REQ:
S->create_edge(tid, upd->handle, upd->handle1, upd->handle2, upd->loc2, vclk, qts);
break;
case transaction::NODE_DELETE_REQ:
S->delete_node(tid, upd->handle1, vclk, qts);
break;
case transaction::EDGE_DELETE_REQ:
S->delete_edge(tid, upd->handle1, upd->handle2, vclk, qts);
break;
case transaction::NODE_SET_PROPERTY:
S->set_node_property(tid, upd->handle1, std::move(upd->key), std::move(upd->value), vclk, qts);
break;
case transaction::EDGE_SET_PROPERTY:
S->set_edge_property(tid, upd->handle1, upd->handle2, std::move(upd->key), std::move(upd->value), vclk, qts);
break;