-
Notifications
You must be signed in to change notification settings - Fork 31
/
unifyfs_p2p_rpc.c
1623 lines (1431 loc) · 50.8 KB
/
unifyfs_p2p_rpc.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 (c) 2020, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
*
* Copyright 2020, UT-Battelle, LLC.
*
* LLNL-CODE-741539
* All rights reserved.
*
* This is the license for UnifyFS.
* For details, see https://github.com/LLNL/UnifyFS.
* Please read https://github.com/LLNL/UnifyFS/LICENSE for full license text.
*/
#include "unifyfs_global.h"
#include "unifyfs_p2p_rpc.h"
#include "unifyfs_group_rpc.h"
#include "unifyfs_rpc_util.h"
/*************************************************************************
* Peer-to-peer RPC helper methods
*************************************************************************/
/* determine server responsible for maintaining target file's metadata */
int hash_gfid_to_server(int gfid)
{
return gfid % glb_pmi_size;
}
/* helper method to initialize peer request rpc handle */
int init_p2p_request_handle(hg_id_t request_hgid,
int peer_rank,
p2p_request* req)
{
int rc = UNIFYFS_SUCCESS;
/* get address for specified server rank */
req->peer = get_margo_server_address(peer_rank);
if (HG_ADDR_NULL == req->peer) {
LOGERR("missing margo address for rank=%d", peer_rank);
return UNIFYFS_ERROR_MARGO;
}
/* get handle to rpc function */
hg_return_t hret = margo_create(unifyfsd_rpc_context->svr_mid, req->peer,
request_hgid, &(req->handle));
if (hret != HG_SUCCESS) {
LOGERR("failed to get handle for p2p request(%p) to server %d - %s",
req, peer_rank, HG_Error_to_string(hret));
rc = UNIFYFS_ERROR_MARGO;
}
return rc;
}
/* helper method to forward peer rpc request */
int forward_p2p_request(void* input_ptr,
p2p_request* req)
{
int rc = UNIFYFS_SUCCESS;
/* call rpc function */
double timeout_ms = margo_server_server_timeout_msec;
hg_return_t hret = margo_iforward_timed(req->handle, input_ptr,
timeout_ms, &(req->request));
if (hret != HG_SUCCESS) {
LOGERR("failed to forward p2p request(%p) - %s",
req, HG_Error_to_string(hret));
rc = UNIFYFS_ERROR_MARGO;
}
return rc;
}
/* helper method to wait for peer rpc request completion */
int wait_for_p2p_request(p2p_request* req)
{
int rc = UNIFYFS_SUCCESS;
/* call rpc function */
hg_return_t hret = margo_wait(req->request);
if (hret != HG_SUCCESS) {
LOGERR("wait on p2p request(%p) failed - %s",
req, HG_Error_to_string(hret));
//margo_state_dump(unifyfsd_rpc_context->svr_mid, "-", 0, NULL);
rc = UNIFYFS_ERROR_MARGO;
}
return rc;
}
/*************************************************************************
* File chunk reads request/response
*************************************************************************/
/* invokes the server-server chunk read request rpc */
int invoke_chunk_read_request_rpc(int dst_srvr_rank,
server_read_req_t* rdreq,
server_chunk_reads_t* remote_reads)
{
int num_chunks = remote_reads->num_chunks;
if (dst_srvr_rank == glb_pmi_rank) {
// short-circuit for local requests
return sm_issue_chunk_reads(glb_pmi_rank,
rdreq->app_id,
rdreq->client_id,
rdreq->req_ndx,
num_chunks,
remote_reads->total_sz,
(char*)(remote_reads->reqs));
}
int ret = UNIFYFS_SUCCESS;
chunk_read_request_in_t in;
chunk_read_request_out_t out;
hg_return_t hret;
hg_size_t bulk_sz = (hg_size_t)num_chunks * sizeof(chunk_read_req_t);
/* forward request to file owner */
p2p_request preq;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.chunk_read_request_id;
int rc = init_p2p_request_handle(req_hgid, dst_srvr_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
return rc;
}
/* fill input struct */
in.src_rank = (int32_t) glb_pmi_rank;
in.app_id = (int32_t) rdreq->app_id;
in.client_id = (int32_t) rdreq->client_id;
in.req_id = (int32_t) rdreq->req_ndx;
in.num_chks = (int32_t) num_chunks;
in.total_data_size = (hg_size_t) remote_reads->total_sz;
in.bulk_size = bulk_sz;
/* register request buffer for bulk remote access */
void* data_buf = remote_reads->reqs;
hret = margo_bulk_create(unifyfsd_rpc_context->svr_mid, 1,
&data_buf, &bulk_sz,
HG_BULK_READ_ONLY, &in.bulk_handle);
if (hret != HG_SUCCESS) {
LOGERR("margo_bulk_create() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
LOGDBG("invoking the chunk-read-request rpc function");
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
LOGERR("forward of chunk-read request rpc to server[%d] failed",
dst_srvr_rank);
margo_bulk_free(in.bulk_handle);
margo_destroy(preq.handle);
return UNIFYFS_ERROR_MARGO;
}
/* wait for request completion */
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
} else {
/* decode response */
hret = margo_get_output(preq.handle, &out);
if (hret == HG_SUCCESS) {
ret = (int)out.ret;
LOGDBG("Got chunk-read response from server[%d] - ret=%d",
dst_srvr_rank, ret);
margo_free_output(preq.handle, &out);
} else {
LOGERR("margo_get_output() failed - %s",
HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
}
}
margo_bulk_free(in.bulk_handle);
}
margo_destroy(preq.handle);
return ret;
}
/* handler for server-server chunk read request */
static void chunk_read_request_rpc(hg_handle_t handle)
{
int32_t ret = UNIFYFS_SUCCESS;
hg_return_t hret;
/* get input params */
chunk_read_request_in_t* in = calloc(1, sizeof(*in));
server_rpc_req_t* req = calloc(1, sizeof(*req));
if ((NULL == in) || (NULL == req)) {
ret = ENOMEM;
} else {
hret = margo_get_input(handle, in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed");
ret = UNIFYFS_ERROR_MARGO;
} else {
/* extract params from input struct */
size_t bulk_sz = (size_t)in->bulk_size;
if (bulk_sz) {
/* allocate and register local target buffer for bulk access */
void* reqbuf = pull_margo_bulk_buffer(handle, in->bulk_handle,
in->bulk_size, NULL);
if (NULL == reqbuf) {
LOGERR("failed to get bulk chunk reads");
ret = UNIFYFS_ERROR_MARGO;
} else {
req->req_type = UNIFYFS_SERVER_RPC_CHUNK_READ;
req->handle = handle;
req->input = (void*) in;
req->bulk_buf = reqbuf;
req->bulk_sz = bulk_sz;
ret = sm_submit_service_request(req);
}
if (ret != UNIFYFS_SUCCESS) {
margo_free_input(handle, in);
}
} else {
LOGWARN("empty chunk read request");
ret = EINVAL;
}
}
}
/* if we hit an error during request submission, respond with the error */
if (ret != UNIFYFS_SUCCESS) {
if (NULL != in) {
free(in);
}
if (NULL != req) {
if (NULL != req->bulk_buf) {
free(req->bulk_buf);
}
free(req);
}
/* return to caller */
chunk_read_request_out_t out;
out.ret = ret;
hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed");
}
/* free margo resources */
margo_destroy(handle);
}
}
DEFINE_MARGO_RPC_HANDLER(chunk_read_request_rpc)
/* Respond to chunk read request. Sends a set of read reply
* headers and corresponding data back to the requesting server.
* The headers and data are posted as a bulk transfer buffer */
int invoke_chunk_read_response_rpc(server_chunk_reads_t* scr)
{
/* assume we'll succeed */
int ret = UNIFYFS_SUCCESS;
/* rank of destination server */
int dst_rank = scr->rank;
assert(dst_rank < (int)glb_num_servers);
/* forward response to requesting server */
p2p_request preq;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.chunk_read_response_id;
int rc = init_p2p_request_handle(req_hgid, dst_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
return rc;
}
/* get address and size of our response buffer */
void* data_buf = (void*) scr->resp;
hg_size_t bulk_sz = scr->total_sz;
/* register our response buffer for bulk remote read access */
chunk_read_response_in_t in;
hg_return_t hret = margo_bulk_create(unifyfsd_rpc_context->svr_mid,
1, &data_buf, &bulk_sz,
HG_BULK_READ_ONLY, &in.bulk_handle);
if (hret != HG_SUCCESS) {
LOGERR("margo_bulk_create() failed - %s", HG_Error_to_string(hret));
margo_destroy(preq.handle);
return UNIFYFS_ERROR_MARGO;
}
/* fill input struct */
in.src_rank = (int32_t) glb_pmi_rank;
in.app_id = (int32_t) scr->app_id;
in.client_id = (int32_t) scr->client_id;
in.req_id = (int32_t) scr->rdreq_id;
in.num_chks = (int32_t) scr->num_chunks;
in.bulk_size = bulk_sz;
/* call the read response rpc */
LOGDBG("invoking the chunk-read-response rpc function");
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
} else {
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
} else {
/* rpc executed, now decode response */
chunk_read_response_out_t out;
hret = margo_get_output(preq.handle, &out);
if (hret == HG_SUCCESS) {
ret = (int)out.ret;
LOGDBG("chunk-read-response rpc to server[%d] - ret=%d",
dst_rank, rc);
margo_free_output(preq.handle, &out);
} else {
LOGERR("margo_get_output() failed - %s",
HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
}
}
}
/* free resources allocated for executing margo rpc */
margo_bulk_free(in.bulk_handle);
margo_destroy(preq.handle);
/* free response data buffer */
free(data_buf);
scr->resp = NULL;
return ret;
}
/* handler for server-server chunk read response */
static void chunk_read_response_rpc(hg_handle_t handle)
{
int32_t ret = UNIFYFS_SUCCESS;
chunk_read_response_out_t out;
/* get input params */
chunk_read_response_in_t in;
hg_return_t hret = margo_get_input(handle, &in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed");
ret = (int32_t) UNIFYFS_ERROR_MARGO;
} else {
/* extract params from input struct */
int src_rank = (int)in.src_rank;
int app_id = (int)in.app_id;
int client_id = (int)in.client_id;
int req_id = (int)in.req_id;
int num_chks = (int)in.num_chks;
size_t bulk_sz = (size_t)in.bulk_size;
LOGDBG("received read response from server[%d] (%d chunks)",
src_rank, num_chks);
/* The input parameters specify the info for a bulk transfer
* buffer on the sending process. We use that info to pull data
* from the sender into a local buffer. This buffer contains
* the read reply headers and associated read data for requests
* we had sent earlier. */
/* pull the remote data via bulk transfer */
if (0 == bulk_sz) {
/* sender is trying to send an empty buffer,
* don't think that should happen unless maybe
* we had sent a read request list that was empty? */
LOGERR("empty response buffer");
ret = (int32_t)EINVAL;
} else {
/* allocate a buffer to hold the incoming data */
char* resp_buf = (char*) pull_margo_bulk_buffer(handle,
in.bulk_handle,
in.bulk_size,
NULL);
if (NULL == resp_buf) {
/* allocation failed, that's bad */
LOGERR("failed to get chunk read responses buffer");
ret = (int32_t)UNIFYFS_ERROR_MARGO;
} else {
LOGDBG("got chunk read responses buffer (%zu bytes)", bulk_sz);
/* process read replies we just received */
int rc = rm_post_chunk_read_responses(app_id, client_id,
src_rank, req_id,
num_chks, bulk_sz,
resp_buf);
if (rc != UNIFYFS_SUCCESS) {
LOGERR("failed to handle chunk read responses");
ret = rc;
}
}
}
margo_free_input(handle, &in);
}
/* return to caller */
out.ret = ret;
hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed");
}
/* free margo resources */
margo_destroy(handle);
}
DEFINE_MARGO_RPC_HANDLER(chunk_read_response_rpc)
/*************************************************************************
* File extents metadata update request
*************************************************************************/
/* Add extents to target file */
int unifyfs_invoke_add_extents_rpc(int gfid,
unsigned int num_extents,
extent_metadata* extents)
{
int owner_rank = hash_gfid_to_server(gfid);
if (owner_rank == glb_pmi_rank) {
/* I'm the owner, already did local add */
return UNIFYFS_SUCCESS;
}
/* forward request to file owner */
p2p_request preq;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.extent_add_id;
int rc = init_p2p_request_handle(req_hgid, owner_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
return rc;
}
/* create a margo bulk transfer handle for extents array */
hg_bulk_t bulk_handle;
void* buf = (void*) extents;
size_t buf_sz = (size_t)num_extents * sizeof(extent_metadata);
hg_return_t hret = margo_bulk_create(unifyfsd_rpc_context->svr_mid,
1, &buf, &buf_sz,
HG_BULK_READ_ONLY, &bulk_handle);
if (hret != HG_SUCCESS) {
LOGERR("margo_bulk_create() failed - %s", HG_Error_to_string(hret));
margo_destroy(preq.handle);
return UNIFYFS_ERROR_MARGO;
}
/* fill rpc input struct and forward request */
add_extents_in_t in;
in.src_rank = (int32_t) glb_pmi_rank;
in.gfid = (int32_t) gfid;
in.num_extents = (int32_t) num_extents;
in.extents = bulk_handle;
LOGDBG("forwarding add_extents(gfid=%d) to server[%d]", gfid, owner_rank);
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
margo_bulk_free(bulk_handle);
margo_destroy(preq.handle);
return rc;
}
/* wait for request completion */
int ret = UNIFYFS_SUCCESS;
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
} else {
/* get the output of the rpc */
add_extents_out_t out;
hret = margo_get_output(preq.handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_output() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
/* set return value */
ret = out.ret;
margo_free_output(preq.handle, &out);
}
}
margo_bulk_free(bulk_handle);
margo_destroy(preq.handle);
return ret;
}
/* Add extents rpc handler */
static void add_extents_rpc(hg_handle_t handle)
{
LOGDBG("add_extents rpc handler");
int ret = UNIFYFS_SUCCESS;
/* get input params */
add_extents_in_t* in = calloc(1, sizeof(*in));
server_rpc_req_t* req = calloc(1, sizeof(*req));
if ((NULL == in) || (NULL == req)) {
ret = ENOMEM;
} else {
hg_return_t hret = margo_get_input(handle, in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed");
ret = UNIFYFS_ERROR_MARGO;
} else {
size_t num_extents = (size_t) in->num_extents;
size_t bulk_sz = num_extents * sizeof(extent_metadata);
/* allocate memory for extents */
void* extents_buf = pull_margo_bulk_buffer(handle, in->extents,
bulk_sz, NULL);
if (NULL == extents_buf) {
LOGERR("failed to get bulk extents");
ret = UNIFYFS_ERROR_MARGO;
} else {
req->req_type = UNIFYFS_SERVER_RPC_EXTENTS_ADD;
req->handle = handle;
req->input = (void*) in;
req->bulk_buf = extents_buf;
req->bulk_sz = bulk_sz;
ret = sm_submit_service_request(req);
}
if (ret != UNIFYFS_SUCCESS) {
margo_free_input(handle, in);
}
}
}
/* if we hit an error during request submission, respond with the error */
if (ret != UNIFYFS_SUCCESS) {
if (NULL != in) {
free(in);
}
if (NULL != req) {
if (NULL != req->bulk_buf) {
free(req->bulk_buf);
}
free(req);
}
/* return to caller */
add_extents_out_t out;
out.ret = (int32_t) ret;
hg_return_t hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed");
}
/* free margo resources */
margo_destroy(handle);
}
}
DEFINE_MARGO_RPC_HANDLER(add_extents_rpc)
/*************************************************************************
* File extents metadata lookup request
*************************************************************************/
/* Lookup extent locations for target file */
int unifyfs_invoke_find_extents_rpc(int gfid,
unsigned int num_extents,
unifyfs_extent_t* extents,
unsigned int* num_chunks,
chunk_read_req_t** chunks)
{
if ((NULL == num_chunks) || (NULL == chunks)) {
return EINVAL;
}
*num_chunks = 0;
*chunks = NULL;
int owner_rank = hash_gfid_to_server(gfid);
int is_owner = (owner_rank == glb_pmi_rank);
/* do local inode metadata lookup */
unifyfs_file_attr_t attrs;
int ret = sm_get_fileattr(gfid, &attrs);
if (ret == UNIFYFS_SUCCESS) {
int file_laminated = (attrs.is_shared && attrs.is_laminated);
if (is_owner || use_server_local_extents || file_laminated) {
/* try local lookup */
int full_coverage = 0;
ret = sm_find_extents(gfid, (size_t)num_extents, extents,
num_chunks, chunks, &full_coverage);
if (ret) {
LOGERR("failed to find extents for gfid=%d (ret=%d)",
gfid, ret);
} else if (0 == *num_chunks) { /* found no data */
LOGDBG("local lookup found no matching chunks");
} else { /* found some chunks */
if (full_coverage) {
LOGDBG("local lookup found chunks with full coverage");
} else {
LOGDBG("local lookup found chunks with partial coverage");
}
}
if (is_owner || file_laminated || full_coverage) {
return ret;
}
/* else, fall through to owner lookup */
if (*num_chunks > 0) {
/* release local results */
*num_chunks = 0;
free(*chunks);
*chunks = NULL;
}
}
}
/* forward request to file owner */
p2p_request preq;
margo_instance_id mid = unifyfsd_rpc_context->svr_mid;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.extent_lookup_id;
int rc = init_p2p_request_handle(req_hgid, owner_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
return rc;
}
/* create a margo bulk transfer handle for extents array */
hg_bulk_t bulk_req_handle;
void* buf = (void*) extents;
size_t buf_sz = (size_t)num_extents * sizeof(unifyfs_extent_t);
hg_return_t hret = margo_bulk_create(mid, 1, &buf, &buf_sz,
HG_BULK_READ_ONLY, &bulk_req_handle);
if (hret != HG_SUCCESS) {
LOGERR("margo_bulk_create() failed - %s", HG_Error_to_string(hret));
margo_destroy(preq.handle);
return UNIFYFS_ERROR_MARGO;
}
/* fill rpc input struct and forward request */
find_extents_in_t in;
in.src_rank = (int32_t) glb_pmi_rank;
in.gfid = (int32_t) gfid;
in.num_extents = (int32_t) num_extents;
in.extents = bulk_req_handle;
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
margo_destroy(preq.handle);
return rc;
}
margo_bulk_free(bulk_req_handle);
/* wait for request completion */
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
margo_destroy(preq.handle);
return rc;
}
/* get the output of the rpc */
find_extents_out_t out;
hret = margo_get_output(preq.handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_output() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
/* set return value */
ret = out.ret;
if (ret == UNIFYFS_SUCCESS) {
/* get number of chunks */
unsigned int n_chks = (unsigned int) out.num_locations;
if (n_chks > 0) {
/* get bulk buffer with chunk locations */
buf_sz = (size_t)n_chks * sizeof(chunk_read_req_t);
buf = pull_margo_bulk_buffer(preq.handle, out.locations,
buf_sz, NULL);
if (NULL == buf) {
LOGERR("failed to get bulk chunk locations");
ret = UNIFYFS_ERROR_MARGO;
} else {
/* lookup requested extents */
LOGDBG("received %u chunk locations for gfid=%d",
n_chks, gfid);
*chunks = (chunk_read_req_t*) buf;
*num_chunks = (unsigned int) n_chks;
}
}
}
margo_free_output(preq.handle, &out);
}
margo_destroy(preq.handle);
return ret;
}
/* find extents rpc handler */
static void find_extents_rpc(hg_handle_t handle)
{
LOGDBG("find_extents rpc handler");
int32_t ret;
/* get input params */
find_extents_in_t* in = calloc(1, sizeof(*in));
server_rpc_req_t* req = calloc(1, sizeof(*req));
if ((NULL == in) || (NULL == req)) {
ret = ENOMEM;
} else {
hg_return_t hret = margo_get_input(handle, in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed");
ret = UNIFYFS_ERROR_MARGO;
} else {
size_t num_extents = (size_t) in->num_extents;
size_t bulk_sz = num_extents * sizeof(unifyfs_extent_t);
/* allocate memory for extents */
void* extents_buf = pull_margo_bulk_buffer(handle, in->extents,
bulk_sz, NULL);
if (NULL == extents_buf) {
LOGERR("failed to get bulk extents");
ret = UNIFYFS_ERROR_MARGO;
} else {
req->req_type = UNIFYFS_SERVER_RPC_EXTENTS_FIND;
req->handle = handle;
req->input = (void*) in;
req->bulk_buf = extents_buf;
req->bulk_sz = bulk_sz;
ret = sm_submit_service_request(req);
}
if (ret != UNIFYFS_SUCCESS) {
margo_free_input(handle, in);
}
}
}
/* if we hit an error during request submission, respond with the error */
if (ret != UNIFYFS_SUCCESS) {
if (NULL != in) {
free(in);
}
if (NULL != req) {
if (NULL != req->bulk_buf) {
free(req->bulk_buf);
}
free(req);
}
/* return to caller */
find_extents_out_t out;
out.ret = (int32_t) ret;
out.num_locations = 0;
out.locations = HG_BULK_NULL;
/* send output back to caller */
hg_return_t hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed");
}
margo_destroy(handle);
}
}
DEFINE_MARGO_RPC_HANDLER(find_extents_rpc)
/*************************************************************************
* File attributes request
*************************************************************************/
/* Get file attributes for target file */
int unifyfs_invoke_metaget_rpc(int gfid,
unifyfs_file_attr_t* attrs)
{
if (NULL == attrs) {
return EINVAL;
}
int owner_rank = hash_gfid_to_server(gfid);
int need_local_metadata = 0;
/* do local inode metadata lookup */
int rc = sm_get_fileattr(gfid, attrs);
if (owner_rank == glb_pmi_rank) {
/* local server is the owner */
return rc;
} else if (rc == UNIFYFS_SUCCESS) {
if (attrs->is_laminated) {
/* if laminated, we already have final metadata locally */
return UNIFYFS_SUCCESS;
}
/* use cached attributes if within threshold */
struct timespec tp = {0};
clock_gettime(CLOCK_REALTIME, &tp);
time_t expire = attrs->last_update + UNIFYFS_METADATA_CACHE_SECONDS;
if (tp.tv_sec <= expire) {
LOGINFO("using cached attributes for gfid=%d", gfid);
return UNIFYFS_SUCCESS;
} else {
LOGINFO("cached attributes for gfid=%d have expired "
"(now=%d, expiration=%d)", gfid, tp.tv_sec, expire);
}
} else if (rc == ENOENT) {
/* local metaget gave ENOENT, need to create inode if file exists */
need_local_metadata = 1;
}
int ret = UNIFYFS_SUCCESS;
rc = add_pending_metaget(gfid);
if (EEXIST == rc) {
/* wait for pending to finish */
do {
LOGDBG("waiting for pending metaget gfid=%d", gfid);
usleep(10000); /* sleep 10 ms */
} while (check_pending_metaget(gfid));
/* should have local copy now if file existed */
rc = sm_get_fileattr(gfid, attrs);
return rc;
} else {
LOGDBG("added pending metaget gfid=%d", gfid);
/* forward request to file owner */
p2p_request preq;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.metaget_id;
rc = init_p2p_request_handle(req_hgid, owner_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
goto clear_pending_metaget;
}
/* fill rpc input struct and forward request */
metaget_in_t in;
in.gfid = (int32_t) gfid;
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
goto clear_pending_metaget;
}
/* wait for request completion */
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
goto clear_pending_metaget;
}
/* get the output of the rpc */
metaget_out_t out;
hg_return_t hret = margo_get_output(preq.handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_output() failed - %s", HG_Error_to_string(hret));
ret = UNIFYFS_ERROR_MARGO;
} else {
/* set return value */
ret = out.ret;
if (ret == UNIFYFS_SUCCESS) {
*attrs = out.attr;
if (out.attr.filename != NULL) {
attrs->filename = strdup(out.attr.filename);
}
if (need_local_metadata) {
sm_set_fileattr(gfid, UNIFYFS_FILE_ATTR_OP_CREATE, attrs);
} else {
sm_set_fileattr(gfid, UNIFYFS_FILE_ATTR_OP_UTIME, attrs);
}
}
margo_free_output(preq.handle, &out);
}
clear_pending_metaget:
LOGDBG("clearing pending metaget gfid=%d", gfid);
rc = clear_pending_metaget(gfid);
if (rc != UNIFYFS_SUCCESS) {
LOGWARN("failed to clear pending metaget for gfid=%d", gfid);
}
margo_destroy(preq.handle);
}
return ret;
}
/* Metaget rpc handler */
static void metaget_rpc(hg_handle_t handle)
{
LOGDBG("metaget rpc handler");
int ret = UNIFYFS_SUCCESS;
/* get input params */
metaget_in_t* in = calloc(1, sizeof(*in));
server_rpc_req_t* req = calloc(1, sizeof(*req));
if ((NULL == in) || (NULL == req)) {
ret = ENOMEM;
} else {
hg_return_t hret = margo_get_input(handle, in);
if (hret != HG_SUCCESS) {
LOGERR("margo_get_input() failed");
ret = UNIFYFS_ERROR_MARGO;
} else {
req->req_type = UNIFYFS_SERVER_RPC_METAGET;
req->handle = handle;
req->input = (void*) in;
req->bulk_buf = NULL;
req->bulk_sz = 0;
ret = sm_submit_service_request(req);
if (ret != UNIFYFS_SUCCESS) {
margo_free_input(handle, in);
}
}
}
/* if we hit an error during request submission, respond with the error */
if (ret != UNIFYFS_SUCCESS) {
if (NULL != in) {
free(in);
}
if (NULL != req) {
free(req);
}
/* return to caller */
metaget_out_t out;
out.ret = (int32_t) ret;
unifyfs_file_attr_set_invalid(&(out.attr));
hg_return_t hret = margo_respond(handle, &out);
if (hret != HG_SUCCESS) {
LOGERR("margo_respond() failed");
}
/* free margo resources */
margo_destroy(handle);
}
}
DEFINE_MARGO_RPC_HANDLER(metaget_rpc)
/*************************************************************************
* File size request
*************************************************************************/
/* Get current global size for the target file */
int unifyfs_invoke_filesize_rpc(int gfid,
size_t* filesize)
{
if (NULL == filesize) {
return EINVAL;
}
*filesize = 0;
int owner_rank = hash_gfid_to_server(gfid);
int need_local_metadata = 0;
unifyfs_file_attr_t attrs;
memset(&attrs, 0, sizeof(attrs));
/* do local inode metadata lookup to check for laminated */
int rc = sm_get_fileattr(gfid, &attrs);
if (owner_rank == glb_pmi_rank) {
*filesize = (size_t) attrs.size;
return rc;
} else if (rc == UNIFYFS_SUCCESS) {
if (attrs.is_laminated) {
/* if laminated, we already have final metadata stored locally */
*filesize = (size_t) attrs.size;
return UNIFYFS_SUCCESS;
}
/* NOTE: unlike metaget above, we don't use cached metadata
* for explicit file size lookups */
} else if (rc == ENOENT) {
/* local metaget gave ENOENT, need to create inode if file exists */
need_local_metadata = 1;
}
int ret = UNIFYFS_SUCCESS;
rc = add_pending_metaget(gfid);
if (EEXIST == rc) {
/* wait for pending to finish */
do {
usleep(10000); /* sleep 10 ms */
} while (check_pending_metaget(gfid));
/* should have local copy now if file existed */
rc = sm_get_fileattr(gfid, &attrs);
*filesize = (size_t) attrs.size;
return rc;
} else {
/* forward request to file owner */
p2p_request preq;
hg_id_t req_hgid = unifyfsd_rpc_context->rpcs.metaget_id;
rc = init_p2p_request_handle(req_hgid, owner_rank, &preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
goto clear_pending_fileattr;
}
/* fill rpc input struct and forward request */
metaget_in_t in;
in.gfid = (int32_t) gfid;
rc = forward_p2p_request((void*)&in, &preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;
goto clear_pending_fileattr;
}
/* wait for request completion */
rc = wait_for_p2p_request(&preq);
if (rc != UNIFYFS_SUCCESS) {
ret = rc;