forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daos_api.py
executable file
·1893 lines (1576 loc) · 69.1 KB
/
daos_api.py
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
#!/usr/bin/python
"""
(C) Copyright 2018 Intel Corporation.
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.
GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
The Government's rights to use, modify, reproduce, release, perform, display,
or disclose this software are subject to the terms of the Apache License as
provided in Contract No. B609815.
Any reproduction of computer software, computer software documentation, or
portions thereof marked with this legend must also reproduce the markings.
"""
import ctypes
import traceback
import threading
import time
import uuid
import json
import os
import inspect
import sys
from daos_cref import *
from conversion import *
class DaosPool(object):
""" A python object representing a DAOS pool."""
def __init__(self, context):
""" setup the python pool object, not the real pool. """
self.attached = 0
self.context = context
self.uuid = (ctypes.c_ubyte * 1)(0)
self.group = ctypes.create_string_buffer(b"not set")
self.handle = ctypes.c_uint64(0)
self.glob = None
self.svc = None
self.pool_info = None
self.target_info = None
def get_uuid_str(self):
return c_uuid_to_str(self.uuid)
def set_uuid_str(self, uuidstr):
self.uuid = str_to_c_uuid(uuidstr)
def create(self, mode, uid, gid, scm_size, group, target_list=None,
cb_func=None, svcn=1, nvme_size=0):
""" send a pool creation request to the daos server group """
c_mode = ctypes.c_uint(mode)
c_uid = ctypes.c_uint(uid)
c_gid = ctypes.c_uint(gid)
c_scm_size = ctypes.c_longlong(scm_size)
c_nvme_size = ctypes.c_longlong(nvme_size)
if group is not None:
self.group = ctypes.create_string_buffer(group)
else:
self.group = None
self.uuid = (ctypes.c_ubyte * 16)()
rank_t = ctypes.c_uint * svcn
# initializing with default values
rank = rank_t(*list([999999 for dummy_i in range(svcn)]))
rl_ranks = ctypes.POINTER(ctypes.c_uint)(rank)
c_whatever = ctypes.create_string_buffer(b"rubbish")
self.svc = RankList(rl_ranks, svcn)
# assuming for now target list is a server rank list
if target_list is not None:
tlist = DaosPool.__pylist_to_array(target_list)
c_tgts = RankList(tlist, len(tlist))
tgt_ptr = ctypes.byref(c_tgts)
else:
tgt_ptr = None
func = self.context.get_function('create-pool')
# the callback function is optional, if not supplied then run the
# create synchronously, if its there then run it in a thread
if cb_func == None:
rc = func(c_mode, c_uid, c_gid, self.group, tgt_ptr,
c_whatever, c_scm_size, c_nvme_size,
ctypes.byref(self.svc), self.uuid, None)
if rc != 0:
self.uuid = (ctypes.c_ubyte * 1)(0)
raise DaosApiError("Pool create returned non-zero. RC: {0}"
.format(rc))
else:
self.attached = 1
else:
event = DaosEvent()
params = [c_mode, c_uid, c_gid, self.group, tgt_ptr,
c_whatever, c_scm_size, c_nvme_size,
ctypes.byref(self.svc), self.uuid, event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def connect(self, flags, cb_func=None):
""" connect to this pool. """
# comment this out for now, so we can test bad data
#if not len(self.uuid) == 16:
# raise DaosApiError("No existing UUID for pool.")
c_flags = ctypes.c_uint(flags)
c_info = PoolInfo()
func = self.context.get_function('connect-pool')
# the callback function is optional, if not supplied then run the
# create synchronously, if its there then run it in a thread
if cb_func is None:
rc = func(self.uuid, self.group, ctypes.byref(self.svc), c_flags,
ctypes.byref(self.handle), ctypes.byref(c_info), None)
if rc != 0:
self.handle = 0
raise DaosApiError("Pool connect returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.uuid, self.group, ctypes.byref(self.svc), c_flags,
ctypes.byref(self.handle), ctypes.byref(c_info), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def disconnect(self, cb_func=None):
""" undoes the fine work done by the connect function above """
func = self.context.get_function('disconnect-pool')
if cb_func is None:
rc = func(self.handle, None)
if rc != 0:
raise DaosApiError("Pool disconnect returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.handle, event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def local2global(self):
""" Create a global pool handle that can be shared. """
c_glob = IOV()
c_glob.iov_len = 0
c_glob.iov_buf_len = 0
c_glob.iov_buf = None
func = self.context.get_function("convert-plocal")
rc = func(self.handle, ctypes.byref(c_glob))
if rc != 0:
raise DaosApiError("Pool local2global returned non-zero. RC: {0}"
.format(rc))
# now call it for real
c_buf = ctypes.create_string_buffer(c_glob.iov_buf_len)
c_glob.iov_buf = ctypes.cast(c_buf, ctypes.c_void_p)
rc = func(self.handle, ctypes.byref(c_glob))
buf = bytearray()
buf.extend(c_buf.raw)
return c_glob.iov_len, c_glob.iov_buf_len, buf
def global2local(self, context, iov_len, buf_len, buf):
func = self.context.get_function("convert-pglobal")
c_glob = IOV()
c_glob.iov_len = iov_len
c_glob.iov_buf_len = buf_len
c_buf = ctypes.create_string_buffer(str(buf))
c_glob.iov_buf = ctypes.cast(c_buf, ctypes.c_void_p)
local_handle = ctypes.c_uint64(0)
rc = func(c_glob, ctypes.byref(local_handle))
if rc != 0:
raise DaosApiError("Pool global2local returned non-zero. RC: {0}"
.format(rc))
self.handle = local_handle
return local_handle
def exclude(self, tgt_rank_list, cb_func=None):
"""Exclude a set of storage targets from a pool."""
if tgt_rank_list is None:
c_tgts = None
else:
rl_ranks = DaosPool.__pylist_to_array(tgt_rank_list)
c_tgts = ctypes.pointer(RankList(rl_ranks, len(tgt_rank_list)))
if self.svc is None:
c_svc = None
else:
c_svc = ctypes.pointer(self.svc)
func = self.context.get_function('exclude-target')
if cb_func is None:
rc = func(self.uuid, self.group, c_svc,
c_tgts, None)
if rc != 0:
raise DaosApiError("Pool exclude returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.uuid, self.group, c_svc,
ctypes.byref(c_tgts), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def extend(self):
"""Extend the pool to more targets."""
raise NotImplementedError("Extend not implemented in C API yet.")
def evict(self, cb_func=None):
"""Evict all connections to a pool."""
func = self.context.get_function('evict-client')
if cb_func is None:
rc = func(self.uuid, self.group, ctypes.byref(self.svc), None)
if rc != 0:
raise DaosApiError(
"Pool evict returned non-zero. RC: {0}".format(rc))
else:
event = DaosEvent()
params = [self.uuid, self.group, ctypes.byref(self.svc), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def tgt_add(self, tgt_rank_list, cb_func=None):
"""add a set of storage targets to a pool."""
rl_ranks = DaosPool.__pylist_to_array(tgt_rank_list)
c_tgts = RankList(rl_ranks, len(tgt_rank_list))
func = self.context.get_function("add-target")
if cb_func is None:
rc = func(self.uuid, self.group, ctypes.byref(self.svc),
ctypes.byref(c_tgts), None)
if rc != 0:
raise DaosApiError("Pool tgt_add returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.uuid, self.group, ctypes.byref(self.svc),
ctypes.byref(c_tgts), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def exclude_out(self, tgt_rank_list, cb_func=None):
"""Exclude completely a set of storage targets from a pool."""
rl_ranks = DaosPool.__pylist_to_array(tgt_rank_list)
c_tgts = RankList(rl_ranks, len(tgt_rank_list))
func = self.context.get_function('kill-target')
if cb_func is None:
rc = func(self.uuid, self.group, ctypes.byref(self.svc),
ctypes.byref(c_tgts), None)
if rc != 0:
raise DaosApiError("Pool exclude_out returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.uuid, self.group, ctypes.byref(self.svc),
ctypes.byref(c_tgts), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def pool_svc_stop(self, cb_func=None):
"""Stop the current pool service leader."""
func = self.context.get_function('service-stop')
if cb_func is None:
rc = func(self.handle, None)
if rc != 0:
raise DaosApiError("Pool svc_Stop returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.handle, event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func, self))
t.start()
def pool_query(self, cb_func=None):
"""Query pool information."""
self.pool_info = PoolInfo()
func = self.context.get_function('query-pool')
if cb_func is None:
rc = func(self.handle, None, ctypes.byref(self.pool_info), None)
if rc != 0:
raise DaosApiError("Pool query returned non-zero. RC: {0}"
.format(rc))
return self.pool_info
else:
event = DaosEvent()
params = [self.handle, None, ctypes.byref(self.pool_info), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
return None
def target_query(self, tgt):
"""Query information of storage targets within a DAOS pool."""
raise NotImplementedError("Target_query not yet implemented in C API.")
def destroy(self, force, cb_func=None):
if not len(self.uuid) == 16 or self.attached == 0:
raise DaosApiError("No existing UUID for pool.")
c_force = ctypes.c_uint(force)
func = self.context.get_function('destroy-pool')
if cb_func is None:
rc = func(self.uuid, self.group, c_force, None)
if rc != 0:
raise DaosApiError("Pool destroy returned non-zero. RC: {0}"
.format(rc))
else:
self.attached = 0
else:
event = DaosEvent()
params = [self.uuid, self.group, c_force, event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func, self))
t.start()
def set_svc(self, rank):
"""
note support for a single rank only
"""
svc_rank = ctypes.c_uint(rank)
rl_ranks = ctypes.POINTER(ctypes.c_uint)(svc_rank)
self.svc = RankList(rl_ranks, 1)
@staticmethod
def __pylist_to_array(pylist):
return (ctypes.c_uint32 * len(pylist))(*pylist)
class DaosObj(object):
""" A class representing an object stored in a DAOS container. """
def __init__(self, context, container, c_oid=None):
self.context = context
self.container = container
self.c_oid = c_oid
self.c_tgts = None
self.attr = None
self.oh = None
self.tgt_rank_list = []
def __del__(self):
""" clean up this object """
if self.oh is not None:
func = self.context.get_function('close-obj')
rc = func(self.oh, None)
if rc != 0:
raise DaosApiError("Object close returned non-zero. RC: {0}"
.format(rc))
self.oh = None
def create(self, rank=None, objcls=13):
""" generate a random oid """
func = self.context.get_function('generate-oid')
func.restype = DaosObjId
self.c_oid = func(objcls, 0, 0)
if rank is not None:
self.c_oid.hi |= rank << 24
def open(self, epoch=0):
""" open the object so we can interact with it """
func = self.context.get_function('open-obj')
c_epoch = ctypes.c_uint64(epoch)
c_mode = ctypes.c_uint(4)
self.oh = ctypes.c_uint64(0)
rc = func(self.container.coh, self.c_oid, c_epoch, c_mode,
ctypes.byref(self.oh), None)
if rc != 0:
raise DaosApiError("Object open returned non-zero. RC: {0}"
.format(rc))
def close(self):
""" close this object """
if self.oh is not None:
func = self.context.get_function('close-obj')
rc = func(self.oh, None)
if rc != 0:
raise DaosApiError("Object close returned non-zero. RC: {0}"
.format(rc))
self.oh = None
def refresh_attr(self, epoch):
""" Get object attributes and save internally
NOTE: THIS FUNCTION ISN'T IMPLEMENTED ON THE DAOS SIDE
"""
if self.c_oid is None:
raise DaosApiError("refresh_attr called but object not initialized")
if self.oh is None:
self.open()
c_epoch = ctypes.c_uint64(epoch)
rank_list = ctypes.cast(ctypes.pointer((ctypes.c_uint32 * 5)()),
ctypes.POINTER(ctypes.c_uint32))
self.c_tgts = RankList(rank_list, 5)
func = self.context.get_function('query-obj')
rc = func(self.oh, c_epoch, None, self.c_tgts, None)
def get_layout(self):
""" Get object target layout info
NOTE: THIS FUNCTION ISN'T PART OF THE PUBLIC API
"""
if self.c_oid is None:
raise DaosApiError("get_layout called but object is not initialized")
if self.oh is None:
self.open()
obj_layout_ptr = ctypes.POINTER(DaosObjLayout)()
func = self.context.get_function('get-layout')
rc = func(self.container.coh, self.c_oid, ctypes.byref(obj_layout_ptr))
if rc == 0:
shards = obj_layout_ptr[0].ol_shards[0][0].os_replica_nr
del self.tgt_rank_list[:]
for i in range(0, shards):
self.tgt_rank_list.append(
obj_layout_ptr[0].ol_shards[0][0].os_ranks[i])
else:
raise DaosApiError("get_layout returned non-zero. RC: {0}".format(rc))
def punch(self, epoch, cb_func=None):
""" Delete this object but only from the specified epoch
Function arguments:
epoch --the epoch from which keys will be deleted.
cb_func --an optional callback function
"""
if self.oh is None:
self.open()
c_epoch = ctypes.c_uint64(epoch)
# the callback function is optional, if not supplied then run the
# punch synchronously, if its there then run it in a thread
func = self.context.get_function('punch-obj')
if cb_func == None:
rc = func(self.oh, c_epoch, None)
if rc != 0:
raise DaosApiError("punch-dkeys returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.oh, c_epoch, event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def punch_dkeys(self, epoch, dkeys, cb_func=None):
""" Deletes dkeys and associated data from an object for a specific
epoch.
Function arguments:
epoch --the epoch from which keys will be deleted.
dkeys --the keys to be deleted, None will be passed as NULL
cb_func --an optional callback function
"""
if self.oh is None:
self.open()
c_epoch = ctypes.c_uint64(epoch)
if dkeys is None:
c_len_dkeys = 0
c_dkeys = None
else:
c_len_dkeys = ctypes.c_uint(len(dkeys))
c_dkeys = (IOV * len(dkeys))()
i = 0
for dkey in dkeys:
c_dkey = ctypes.create_string_buffer(dkey)
c_dkeys[i].iov_buf = ctypes.cast(c_dkey, ctypes.c_void_p)
c_dkeys[i].iov_buf_len = ctypes.sizeof(c_dkey)
c_dkeys[i].iov_len = ctypes.sizeof(c_dkey)
i += 1
# the callback function is optional, if not supplied then run the
# create synchronously, if its there then run it in a thread
func = self.context.get_function('punch-dkeys')
if cb_func == None:
rc = func(self.oh, c_epoch, c_len_dkeys, ctypes.byref(c_dkeys),
None)
if rc != 0:
raise DaosApiError("punch-dkeys returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.oh, c_epoch, c_len_dkeys, ctypes.byref(c_dkeys),
event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
def punch_akeys(self, epoch, dkey, akeys, cb_func=None):
""" Deletes akeys and associated data from a dkey/object for a specific
epoch.
Function arguments:
epoch --the epoch from which keys will be deleted.
dkey --the parent dkey from which the akeys will be deleted,
Expecting a string
akeys --a list of akeys (strings) which are to be deleted
cb_func --an optional callback function
"""
if self.oh is None:
self.open()
c_epoch = ctypes.c_uint64(epoch)
c_dkey_iov = IOV()
c_dkey = ctypes.create_string_buffer(dkey)
c_dkey_iov.iov_buf = ctypes.cast(c_dkey, ctypes.c_void_p)
c_dkey_iov.iov_buf_len = ctypes.sizeof(c_dkey)
c_dkey_iov.iov_len = ctypes.sizeof(c_dkey)
c_len_akeys = ctypes.c_uint(len(akeys))
c_akeys = (IOV * len(akeys))()
i = 0
for akey in akeys:
c_akey = ctypes.create_string_buffer(akey)
c_akeys[i].iov_buf = ctypes.cast(c_akey, ctypes.c_void_p)
c_akeys[i].iov_buf_len = ctypes.sizeof(c_akey)
c_akeys[i].iov_len = ctypes.sizeof(c_akey)
i += 1
# the callback function is optional, if not supplied then run the
# create synchronously, if its there then run it in a thread
func = self.context.get_function('punch-akeys')
if cb_func == None:
rc = func(self.oh, c_epoch, ctypes.byref(c_dkey_iov), c_len_akeys,
ctypes.byref(c_akeys), None)
if rc != 0:
raise DaosApiError("punch-akeys returned non-zero. RC: {0}"
.format(rc))
else:
event = DaosEvent()
params = [self.oh, c_epoch, ctypes.byref(c_dkey_iov), c_len_akeys,
ctypes.byref(c_akeys), event]
t = threading.Thread(target=AsyncWorker1,
args=(func,
params,
self.context,
cb_func,
self))
t.start()
class IORequest(object):
"""
Python object that centralizes details about an I/O
type is either 1 (single) or 2 (array)
"""
def __init__(self, context, container, obj, rank=None, iotype=1,
objtype=13):
"""
container --which container the object is (or will be) in
obj --None to create a new object or the OID of an existing obj
rank --utilize with certain object types to force obj to a specific
server
iotype --1 for single, 2 for array
objtype --specifies the attributes for the object
"""
self.context = context
self.container = container
if obj is None:
# create a new object
self.obj = DaosObj(context, container)
self.obj.create(rank, objtype)
self.obj.open()
else:
self.obj = obj
self.io_type = ctypes.c_int(iotype)
self.sgl = SGL()
self.iod = DaosIODescriptor()
ctypes.memset(ctypes.byref(self.iod.iod_kcsum), 0, 16)
self.epoch_range = EpochRange()
cs = CheckSum()
cs.cs_sum = ctypes.pointer(ctypes.create_string_buffer(32))
cs.cs_buf_len = 32
cs.cs_len = 0
self.iod.iod_csums = ctypes.pointer(cs)
def __del__(self):
""" cleanup this request """
pass
def insert_array(self, dkey, akey, c_data, epoch):
"""
Setup the I/O Vector and I/O descriptor for an array insertion.
This function is limited to a single descriptor and a single
scatter gather list. The single SGL can have any number of
entries as dictated by the c_data parameter.
"""
sgl_iov_list = (IOV * len(c_data))()
idx = 0
for item in c_data:
sgl_iov_list[idx].iov_len = item[1]
sgl_iov_list[idx].iov_buf_len = item[1]
sgl_iov_list[idx].iov_buf = ctypes.cast(item[0], ctypes.c_void_p)
idx += 1
self.sgl.sg_iovs = ctypes.cast(ctypes.pointer(sgl_iov_list),
ctypes.POINTER(IOV))
self.sgl.sg_nr = len(c_data)
self.sgl.sg_nr_out = len(c_data)
self.epoch_range.epr_lo = epoch
self.epoch_range.epr_hi = ~0
extent = Extent()
extent.rx_idx = 0
extent.rx_nr = len(c_data)
# setup the descriptor
self.iod.iod_name.iov_buf = ctypes.cast(akey, ctypes.c_void_p)
self.iod.iod_name.iov_buf_len = ctypes.sizeof(akey)
self.iod.iod_name.iov_len = ctypes.sizeof(akey)
self.iod.iod_type = 2
self.iod.iod_size = c_data[0][1]
self.iod.iod_nr = 1
self.iod.iod_recxs = ctypes.pointer(extent)
self.iod.iod_eprs = ctypes.cast(ctypes.pointer(self.epoch_range),
ctypes.c_void_p)
# now do it
func = self.context.get_function('update-obj')
dkey_iov = IOV()
dkey_iov.iov_buf = ctypes.cast(dkey, ctypes.c_void_p)
dkey_iov.iov_buf_len = ctypes.sizeof(dkey)
dkey_iov.iov_len = ctypes.sizeof(dkey)
rc = func(self.obj.oh, self.epoch_range.epr_lo, ctypes.byref(dkey_iov),
1, ctypes.byref(self.iod), ctypes.byref(self.sgl), None)
if rc != 0:
raise DaosApiError("Object update returned non-zero. RC: {0}"
.format(rc))
def fetch_array(self, dkey, akey, rec_count, rec_size, epoch):
"""
dkey --1st level key for the array value
akey --2nd level key for the array value
rec_count --how many array indices (records) to retrieve
rec_size --size in bytes of a single record
epoch --which epoch to read the value from
"""
# setup the descriptor, we are only handling a single descriptor that
# covers an arbitrary number of consecutive array entries
extent = Extent()
extent.rx_idx = 0
extent.rx_nr = ctypes.c_ulong(rec_count.value)
self.iod.iod_name.iov_buf = ctypes.cast(akey, ctypes.c_void_p)
self.iod.iod_name.iov_buf_len = ctypes.sizeof(akey)
self.iod.iod_name.iov_len = ctypes.sizeof(akey)
self.iod.iod_type = 2
self.iod.iod_size = rec_size
self.iod.iod_nr = 1
self.iod.iod_recxs = ctypes.pointer(extent)
# setup the scatter/gather list, we are only handling an
# an arbitrary number of consecutive array entries of the same size
sgl_iov_list = (IOV * rec_count.value)()
for i in range(rec_count.value):
sgl_iov_list[i].iov_buf_len = rec_size
sgl_iov_list[i].iov_buf = ctypes.cast(
ctypes.create_string_buffer(rec_size.value),
ctypes.c_void_p)
self.sgl.sg_iovs = ctypes.cast(ctypes.pointer(sgl_iov_list),
ctypes.POINTER(IOV))
self.sgl.sg_nr = rec_count
self.sgl.sg_nr_out = rec_count
dkey_iov = IOV()
dkey_iov.iov_buf = ctypes.cast(dkey, ctypes.c_void_p)
dkey_iov.iov_buf_len = ctypes.sizeof(dkey)
dkey_iov.iov_len = ctypes.sizeof(dkey)
# now do it
func = self.context.get_function('fetch-obj')
rc = func(self.obj.oh, epoch, ctypes.byref(dkey_iov), 1,
ctypes.byref(self.iod), ctypes.byref(self.sgl), None, None)
if rc != 0:
raise DaosApiError("Array fetch returned non-zero. RC: {0}"
.format(rc))
# convert the output into a python list rather than return C types
# outside this file
output = []
for i in range(rec_count.value):
output.append(ctypes.string_at(sgl_iov_list[i].iov_buf,
rec_size.value))
return output
def single_insert(self, dkey, akey, value, size, epoch):
"""
dkey --1st level key for the array value
akey --2nd level key for the array value
value --string value to insert
size --size of the string
epoch --which epoch to write to
"""
# put the data into the scatter gather list
sgl_iov = IOV()
sgl_iov.iov_len = size
sgl_iov.iov_buf_len = size
if value is not None:
sgl_iov.iov_buf = ctypes.cast(value, ctypes.c_void_p)
# testing only path
else:
sgl_iov.iov_buf = None
self.sgl.sg_iovs = ctypes.pointer(sgl_iov)
self.sgl.sg_nr = 1
self.sgl.sg_nr_out = 1
self.epoch_range.epr_lo = epoch
self.epoch_range.epr_hi = ~0
# setup the descriptor
if akey is not None:
self.iod.iod_name.iov_buf = ctypes.cast(akey, ctypes.c_void_p)
self.iod.iod_name.iov_buf_len = ctypes.sizeof(akey)
self.iod.iod_name.iov_len = ctypes.sizeof(akey)
self.iod.iod_type = 1
self.iod.iod_size = size
self.iod.iod_nr = 1
self.iod.iod_eprs = ctypes.cast(ctypes.pointer(self.epoch_range),
ctypes.c_void_p)
iod_ptr = ctypes.pointer(self.iod)
else:
iod_ptr = None
# now do it
func = self.context.get_function('update-obj')
if dkey is not None:
dkey_iov = IOV()
dkey_iov.iov_buf = ctypes.cast(dkey, ctypes.c_void_p)
dkey_iov.iov_buf_len = ctypes.sizeof(dkey)
dkey_iov.iov_len = ctypes.sizeof(dkey)
dkey_ptr = ctypes.pointer(dkey_iov)
else:
dkey_ptr = None
rc = func(self.obj.oh, self.epoch_range.epr_lo, dkey_ptr, 1,
ctypes.byref(self.iod), ctypes.byref(self.sgl), None)
if rc != 0:
raise DaosApiError("Object update returned non-zero. RC: {0}"
.format(rc))
def single_fetch(self, dkey, akey, size, epoch, test_hints=[]):
"""
dkey --1st level key for the single value
akey --2nd level key for the single value
size --size of the string
epoch --which epoch to read from
test_hints --optional set of values that allow for error injection,
supported values 'sglnull', 'iodnull'.
a string containing the value is returned
"""
if any("sglnull" in s for s in test_hints):
sgl_ptr = None
buf = ctypes.create_string_buffer(0)
else:
sgl_iov = IOV()
sgl_iov.iov_len = ctypes.c_size_t(size)
sgl_iov.iov_buf_len = ctypes.c_size_t(size)
buf = ctypes.create_string_buffer(size)
sgl_iov.iov_buf = ctypes.cast(buf, ctypes.c_void_p)
self.sgl.sg_iovs = ctypes.pointer(sgl_iov)
self.sgl.sg_nr = 1
self.sgl.sg_nr_out = 1
sgl_ptr = ctypes.pointer(self.sgl)
self.epoch_range.epr_lo = epoch
self.epoch_range.epr_hi = ~0
# setup the descriptor
if any("iodnull" in s for s in test_hints):
iod_ptr = None
else:
self.iod.iod_name.iov_buf = ctypes.cast(akey, ctypes.c_void_p)
self.iod.iod_name.iov_buf_len = ctypes.sizeof(akey)
self.iod.iod_name.iov_len = ctypes.sizeof(akey)
self.iod.iod_type = 1
self.iod.iod_size = ctypes.c_size_t(size)
self.iod.iod_nr = 1
self.iod.iod_eprs = ctypes.cast(ctypes.pointer(self.epoch_range),
ctypes.c_void_p)
iod_ptr = ctypes.pointer(self.iod)
if dkey is not None:
dkey_iov = IOV()
dkey_iov.iov_buf = ctypes.cast(dkey, ctypes.c_void_p)
dkey_iov.iov_buf_len = ctypes.sizeof(dkey)
dkey_iov.iov_len = ctypes.sizeof(dkey)
dkey_ptr = ctypes.pointer(dkey_iov)
else:
dkey_ptr = None
# now do it
func = self.context.get_function('fetch-obj')
rc = func(self.obj.oh, self.epoch_range.epr_lo, dkey_ptr,
1, iod_ptr, sgl_ptr, None, None)
if rc != 0:
raise DaosApiError("Object fetch returned non-zero. RC: {0}"
.format(rc))
return buf
def multi_akey_insert(self, dkey, data, epoch):
"""
Update object with with multiple values, where each value is tagged
with an akey. This is a bit of a mess but need to refactor all the
I/O functions as a group at some point.
dkey --1st level key for the values
data --a list of tuples (akey, value)
epoch --which epoch to write to
"""
# put the data into the scatter gather list
count = len(data)
c_count = ctypes.c_uint(count)
iods = (DaosIODescriptor * count)()
sgl_list = (SGL * count)()
i=0
for tup in data:
sgl_iov = IOV()
sgl_iov.iov_len = ctypes.c_size_t(len(tup[1])+1)
sgl_iov.iov_buf_len = ctypes.c_size_t(len(tup[1])+1)
sgl_iov.iov_buf = ctypes.cast(tup[1], ctypes.c_void_p)
sgl_list[i].sg_nr_out = 1
sgl_list[i].sg_nr = 1
sgl_list[i].sg_iovs = ctypes.pointer(sgl_iov)
iods[i].iod_name.iov_buf = ctypes.cast(tup[0], ctypes.c_void_p)
iods[i].iod_name.iov_buf_len = ctypes.sizeof(tup[0])
iods[i].iod_name.iov_len = ctypes.sizeof(tup[0])
iods[i].iod_type = 1
iods[i].iod_size = len(tup[1])+1
iods[i].iod_nr = 1
ctypes.memset(ctypes.byref(iods[i].iod_kcsum), 0, 16)
i += 1
iod_ptr = ctypes.pointer(iods)
sgl_ptr = ctypes.pointer(sgl_list)
if dkey is not None:
dkey_iov = IOV()
dkey_iov.iov_buf = ctypes.cast(dkey, ctypes.c_void_p)
dkey_iov.iov_buf_len = ctypes.sizeof(dkey)
dkey_iov.iov_len = ctypes.sizeof(dkey)
dkey_ptr = ctypes.pointer(dkey_iov)
else:
dkey_ptr = None
# now do it
func = self.context.get_function('update-obj')
rc = func(self.obj.oh, epoch, dkey_ptr, c_count,
iod_ptr, sgl_ptr, None)
if rc != 0:
raise DaosApiError("Object update returned non-zero. RC: {0}"
.format(rc))
def multi_akey_fetch(self, dkey, keys, epoch):
"""
Retrieve multiple akeys & associated data. This is kind of a mess but
will refactor all the I/O functions at some point.
dkey --1st level key for the array value
keys --a list of tuples where each tuple is an (akey, size), where size
is the size of the data for that key
epoch --which epoch to read from
returns a dictionary containing the akey:value pairs
"""
# create scatter gather list to hold the returned data also
# create the descriptor
count = len(keys)
c_count = ctypes.c_uint(count)
i = 0
sgl_list = (SGL * count)()
iods = (DaosIODescriptor * count)()
for key in keys:
sgl_iov = IOV()
sgl_iov.iov_len = ctypes.c_ulong(key[1].value+1)
sgl_iov.iov_buf_len = ctypes.c_ulong(key[1].value+1)
buf = ctypes.create_string_buffer(key[1].value+1)
sgl_iov.iov_buf = ctypes.cast(buf, ctypes.c_void_p)
sgl_list[i].sg_nr_out = 1
sgl_list[i].sg_nr = 1
sgl_list[i].sg_iovs = ctypes.pointer(sgl_iov)
iods[i].iod_name.iov_buf = ctypes.cast(key[0], ctypes.c_void_p)
iods[i].iod_name.iov_buf_len = ctypes.sizeof(key[0])
iods[i].iod_name.iov_len = ctypes.sizeof(key[0])
iods[i].iod_type = 1
iods[i].iod_size = ctypes.c_ulong(key[1].value+1)