-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_commands.cpp
2174 lines (1800 loc) · 81.2 KB
/
handle_commands.cpp
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
//*****************************************************************
//*****************************************************************
#include <string.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include "dns_cache.h"
#include "ibp_ClientLib.h"
#include "ibp_server.h"
#include "log.h"
#include "debug.h"
#include "security_log.h"
#include "allocation.h"
#include "resource.h"
#include "db_resource.h"
#include "network.h"
#include "net_sock.h"
#include "net_phoebus.h"
#include "ibp_task.h"
#include "ibp_protocol.h"
#include "task_coordinator.h"
#include "server_version.h"
#include "lock_alloc.h"
#include "cap_timestamp.h"
#include "activity_log.h"
#include "fmttypes.h"
//*****************************************************************
// handle_allocate - Processes the allocate command
//
// Results:
// status readCap writeCap manageCap
//*****************************************************************
int handle_allocate(ibp_task_t *task)
{
int d, err;
unsigned int ui;
Resource_t *res;
char token[4096];
Allocation_t a, ma;
Cmd_state_t *cmd = &(task->cmd);
debug_printf(1, "handle_allocate: Starting to process command\n");
Allocation_t *alloc = &(cmd->cargs.allocate.a);
RID_t *rid = &(cmd->cargs.allocate.rid);
err = 0;
if (is_empty_rid(rid)) { //** Pick a random resource to use
get_random(&ui, sizeof(ui));
double r = (1.0 * ui) / (UINT_MAX + 1.0);
d = global_config->n_resources * r;
*rid = global_config->res[d].rid;
log_printf(10, "handle_allocate: Picking random resource %s\n", rid2str(rid, token, sizeof(token)));
}
//** Check the resource **
res = resource_lookup(global_config->rl, rid2str(rid, token, sizeof(token)));
if (res == NULL) { //**Can't find the resource
log_printf(1, "handle_allocate: Invalid resource: %s\n", rid2str(rid, token, sizeof(token)));
alog_append_ibp_allocate(task->myid, -1, alloc->max_size, alloc->type, alloc->reliability, alloc->expiration);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
//** Check the duration **
if (alloc->expiration == INT_MAX) {
alloc->expiration = res->max_duration + time(NULL);
}
if (cmd->command == IBP_SPLIT_ALLOCATE) {
//** Get the Master allocation ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(cmd->cargs.allocate.master_cap), &ma)) != 0) {
log_printf(10, "handle_split_allocate: Invalid master cap: %s rid=%s\n", cmd->cargs.allocate.master_cap.v, res->name);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
alog_append_ibp_split_allocate(task->myid, res->rl_index, ma.id, alloc->max_size, alloc->type, alloc->reliability, alloc->expiration);
lock_osd_id(ma.id); //** Redo the get allocation again with the lock enabled ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(cmd->cargs.allocate.master_cap), &ma)) != 0) {
log_printf(10, "handle_split_allocate: Invalid master cap: %s rid=%s\n", cmd->cargs.allocate.master_cap.v, res->name);
unlock_osd_id(ma.id);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
} else {
alog_append_ibp_allocate(task->myid, res->rl_index, alloc->max_size, alloc->type, alloc->reliability, alloc->expiration);
}
d = alloc->expiration - time(NULL);
debug_printf(10, "handle_allocate: expiration: %u (%d sec from now)\n", alloc->expiration, d); flush_debug();
if (res->max_duration < d) {
log_printf(1, "handle_allocate: Duration(%d sec) exceeds that for RID %s of %d sec\n", d, res->name, res->max_duration);
if (cmd->command == IBP_SPLIT_ALLOCATE) unlock_osd_id(ma.id);
send_cmd_result(task, IBP_E_LONG_DURATION);
return(-1);
}
//** Perform the allocation **
d = (global_config->server.lazy_allocate == 1) ? 0 : 1;
if (cmd->command == IBP_SPLIT_ALLOCATE) {
if ((d = split_allocation_resource(res, &ma, &a, alloc->max_size, alloc->type, alloc->reliability, alloc->expiration, 0, d)) != 0) {
log_printf(1, "handle_allocate: split_allocation_resource failed on RID %s! Error=%d\n", res->name, d);
unlock_osd_id(ma.id); //** Now we can unlock the master
send_cmd_result(task, IBP_E_WOULD_EXCEED_LIMIT);
return(-1);
}
//** Update the master's manage timestamp
update_manage_history(res, ma.id, ma.is_alias, &(task->ipadd), cmd->command, 0, a.reliability, a.expiration, a.max_size, 0);
unlock_osd_id(ma.id); //** Now we can unlock the master
} else {
if ((d = create_allocation_resource(res, &a, alloc->max_size, alloc->type, alloc->reliability, alloc->expiration, 0, d)) != 0) {
log_printf(1, "handle_allocate: create_allocation_resource failed on RID %s! Error=%d\n", res->name, d);
send_cmd_result(task, IBP_E_WOULD_EXCEED_LIMIT);
return(-1);
}
}
if (a.type != IBP_BYTEARRAY) { //** Set the size to be 0 for all the Queue types
a.size = 0;
a.w_pos = 0;
a.r_pos = 0;
}
//** Store the creation timestamp **
set_alloc_timestamp(&(a.creation_ts), &(task->ipadd));
//log_printf(15, "handle_allocate: create time =" TT " ns=%d\n", a.creation_ts.time, ns_getid(task->ns));
err = modify_allocation_resource(res, a.id, &a);
if (err != 0) {
log_printf(0, "handle_allocate: Error with modify_allocation_resource for new queue allocation! err=%d, type=%d\n", err, a.type);
}
//** Send the result back **
ns_monitor_t *nm = ns_get_monitor(task->ns);
snprintf(token, sizeof(token), "%d ibp://%s:%d/%s#%s/3862277/READ "
"ibp://%s:%d/%s#%s/3862277/WRITE "
"ibp://%s:%d/%s#%s/3862277/MANAGE \n",
IBP_OK,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[READ_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[WRITE_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[MANAGE_CAP].v);
Net_timeout_t dt;
convert_epoch_time2net(&dt, task->cmd_timeout);
debug_code(time_t tt=time(NULL);)
debug_printf(1, "handle_allocate: before sending result time: %s\n", ctime(&tt));
err = write_netstream_block(task->ns, task->cmd_timeout, token, strlen(token));
alog_append_osd_id(task->myid, a.id);
debug_printf(1, "handle_allocate: Allocation: %s", token);
debug_code(
if (debug_level() > 5) print_allocation_resource(res, log_fd(), &a);
)
//Allocation_history_t h1;
//get_history_table(res, a.id, &h1);
//log_printf(0, "handle_allocate history: r=%s id=" LU " h.id=" LU " write_slot=%d\n", res->name, a.id, h1.id, h1.write_slot);
return(err);
}
//*****************************************************************
// handle_merge - Merges 2 allocations
//*****************************************************************
int handle_merge(ibp_task_t *task)
{
int err;
Resource_t *r;
Allocation_t ma, ca;
Cmd_state_t *cmd = &(task->cmd);
Cmd_merge_t *op = &(cmd->cargs.merge);
debug_printf(1, "handle_merge: Starting to process command\n");
//** Check the resource **
r = resource_lookup(global_config->rl, op->crid);
if (r == NULL) { //**Can't find the resource
log_printf(1, "handle_merge: Invalid resource: %s\n", op->crid);
alog_append_ibp_merge(task->myid, 0, 0, -1);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
//** Get the master allocation ***
if ((err = get_allocation_by_cap_resource(r, MANAGE_CAP, &(op->mkey), &ma)) != 0) {
log_printf(10, "handle_merge: Invalid mcap: %s rid=%s\n", op->mkey.v, r->name);
alog_append_ibp_merge(task->myid, 0, 0, r->rl_index);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
//** and the child allocation
if ((err = get_allocation_by_cap_resource(r, MANAGE_CAP, &(op->ckey), &ca)) != 0) {
log_printf(10, "handle_merge: Invalid childcap: %s rid=%s\n", op->ckey.v, r->name);
alog_append_ibp_merge(task->myid, ma.id, 0, r->rl_index);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
alog_append_ibp_merge(task->myid, ma.id, ca.id, r->rl_index);
//** Now do the same thing with locks enabled
lock_osd_id_pair(ma.id, ca.id);
//** Get the master allocation ***
if ((err = get_allocation_by_cap_resource(r, MANAGE_CAP, &(op->mkey), &ma)) != 0) {
log_printf(10, "handle_merge: Invalid mcap: %s rid=%s\n", op->mkey.v, r->name);
alog_append_ibp_merge(task->myid, 0, 0, r->rl_index);
unlock_osd_id_pair(ma.id, ca.id);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
//** and the child allocation
if ((err = get_allocation_by_cap_resource(r, MANAGE_CAP, &(op->ckey), &ca)) != 0) {
log_printf(10, "handle_merge: Invalid childcap: %s rid=%s\n", op->ckey.v, r->name);
alog_append_ibp_merge(task->myid, ma.id, 0, r->rl_index);
unlock_osd_id_pair(ma.id, ca.id);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
//** Update the manage timestamp
update_manage_history(r, ca.id, ca.is_alias, &(task->ipadd), cmd->command, 0, 0, 0, ca.max_size, 0);
//** Check the child ref count
if ((ca.read_refcount == 1) && (ca.write_refcount == 0)) {
//** merge the allocation **
if ((err = merge_allocation_resource(r, &ma, &ca)) != 0) {
log_printf(1, "handle_merge: merge_allocation_resource failed on RID %s! Error=%d\n", r->name, err);
unlock_osd_id_pair(ma.id, ca.id);
send_cmd_result(task, IBP_E_GENERIC);
return(-1);
}
} else {
log_printf(15, "handle_merge: Bad child refcount! RID=%s ca.read=%d ca.write=%d\n", r->name, ca.read_refcount, ca.write_refcount);
unlock_osd_id_pair(ma.id, ca.id);
send_cmd_result(task, IBP_E_GENERIC);
return(-1);
}
unlock_osd_id_pair(ma.id, ca.id);
send_cmd_result(task, IBP_OK);
debug_printf(1, "handle_merge: completed\n");
return(0);
}
//*****************************************************************
// handle_rename - Processes the allocation rename command
//
// Results:
// status readCap writeCap manageCap
//*****************************************************************
int handle_rename(ibp_task_t *task)
{
int d, err;
Resource_t *res;
char token[4096];
Allocation_t a;
Cmd_state_t *cmd = &(task->cmd);
Cmd_manage_t *manage = &(cmd->cargs.manage);
debug_printf(1, "handle_rename: Starting to process command\n");
err = 0;
//** Check the resource **
res = resource_lookup(global_config->rl, rid2str(&(manage->rid), token, sizeof(token)));
if (res == NULL) { //**Can't find the resource
log_printf(1, "handle_allocate: Invalid resource: %s\n", rid2str(&(manage->rid), token, sizeof(token)));
alog_append_ibp_rename(task->myid, -1, 0);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
//** Get the allocation ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(manage->cap), &a)) != 0) {
log_printf(10, "handle_rename: Invalid cap: %s rid=%s\n", manage->cap.v, res->name);
alog_append_ibp_rename(task->myid, -1, 0);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
alog_append_ibp_rename(task->myid, res->rl_index, a.id);
lock_osd_id(a.id);
//** Get the allocation again with the lock enabled ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(manage->cap), &a)) != 0) {
log_printf(10, "handle_rename: Invalid cap: %s rid=%s\n", manage->cap.v, res->name);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
unlock_osd_id(a.id);
return(-1);
}
//** Update the manage timestamp
update_manage_history(res, a.id, a.is_alias, &(task->ipadd), cmd->command, 0, a.reliability, a.expiration, a.max_size, 0);
//** Rename the allocation **
if ((d = rename_allocation_resource(res, &a)) != 0) {
log_printf(1, "handle_rename: rename_allocation_resource failed on RID %s! Error=%d\n", res->name, d);
send_cmd_result(task, IBP_E_GENERIC);
unlock_osd_id(a.id);
return(-1);
}
unlock_osd_id(a.id);
//** Send the result back **
//** Send the result back **
ns_monitor_t *nm = ns_get_monitor(task->ns);
snprintf(token, sizeof(token), "%d ibp://%s:%d/%s#%s/3862277/READ "
"ibp://%s:%d/%s#%s/3862277/WRITE "
"ibp://%s:%d/%s#%s/3862277/MANAGE \n",
IBP_OK,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[READ_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[WRITE_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, a.caps[MANAGE_CAP].v);
debug_code(time_t tt=time(NULL);)
debug_printf(1, "handle_rename: before sending result time: %s\n", ctime(&tt));
err = write_netstream_block(task->ns, task->cmd_timeout, token, strlen(token));
alog_append_osd_id(task->myid, a.id);
debug_printf(1, "handle_rename: Allocation: %s", token);
debug_code(
if (debug_level() > 5) print_allocation_resource(res, log_fd(), &a);
)
return(err);
}
//*****************************************************************
// handle_internal_get_alloc - Processes the raw internal get allocation
//
// Results:
// status ndatabytes\n
// ..raw allocation..
//*****************************************************************
int handle_internal_get_alloc(ibp_task_t *task)
{
int bufsize = 1024*1024;
char buffer[bufsize];
int err;
uint64_t nbytes;
Resource_t *res;
char token[4096];
Allocation_t a;
Allocation_history_t h;
Cmd_state_t *cmd = &(task->cmd);
Cmd_internal_get_alloc_t *arg = &(cmd->cargs.get_alloc);
debug_printf(1, "handle_internal_get_alloc: Starting to process command\n");
err = 0;
//** Check the resource **
res = resource_lookup(global_config->rl, rid2str(&(arg->rid), token, sizeof(token)));
if (res == NULL) { //**Can't find the resource
log_printf(1, "handle_internal_get_alloc: Invalid resource: %s\n", rid2str(&(arg->rid), token, sizeof(token)));
alog_append_internal_get_alloc(task->myid, -1, 0);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
err = -1;
a.id = 0;
switch(arg->key_type) {
case IBP_READCAP:
err = get_allocation_by_cap_resource(res, READ_CAP, &(arg->cap), &a);
break;
case IBP_WRITECAP:
err = get_allocation_by_cap_resource(res, WRITE_CAP, &(arg->cap), &a);
break;
case IBP_MANAGECAP:
err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(arg->cap), &a);
break;
case INTERNAL_ID:
err = get_allocation_resource(res, arg->id, &a);
break;
}
alog_append_internal_get_alloc(task->myid, res->rl_index, a.id);
if (err != 0) {
log_printf(10, "handle_internal_get_alloc: Invalid cap/id: rid=%s ns=%d\n",res->name, ns_getid(task->ns));
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
err = get_history_table(res, arg->id, &h);
if (err != 0) {
log_printf(10, "handle_internal_get_alloc: Cant read the history! rid=%s ns=%d\n",res->name, ns_getid(task->ns));
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
nbytes = 0;
if (arg->offset > -1) {
if (arg->len == 0) {
nbytes = a.size - arg->offset;
} else if (arg->offset > a.size) {
nbytes = 0;
} else if ((arg->len + arg->offset) > a.size) {
nbytes = a.size - arg->offset;
} else {
nbytes = arg->len;
}
}
//*** Send back the results ***
sprintf(buffer, "%d " LU " \n",IBP_OK, nbytes);
err = write_netstream_block(task->ns, task->cmd_timeout, buffer, strlen(buffer));
//** Send the allocation
err = write_netstream_block(task->ns, task->cmd_timeout, (char *)&a, sizeof(a));
//** ...and the history
err = write_netstream_block(task->ns, task->cmd_timeout, (char *)&h, sizeof(h));
//** Send back the data if needed **
if (arg->offset > -1) {
ibp_task_t rtask;
Cmd_read_t *rcmd = &(rtask.cmd.cargs.read);
rcmd->a = a;
rcmd->r = res;
a.r_pos = arg->offset;
rcmd->left = nbytes;
rcmd->len = nbytes;
rcmd->offset = arg->offset;
rtask.ns = task->ns;
err = read_from_disk(&rtask, &a, &(rcmd->left), rcmd->r);
}
debug_printf(1, "handle_internal_get_alloc: Allocation:\n");
debug_code(
if (debug_level() > 5) print_allocation_resource(res, log_fd(), &a);
)
debug_printf(1, "handle_internal_get_alloc: completed\n");
return(err);
}
//*****************************************************************
// handle_alias_allocate - Generates a alias allocation
//
// Results:
// status readCap writeCap manageCap
//*****************************************************************
int handle_alias_allocate(ibp_task_t *task)
{
int d, err;
Resource_t *res;
char token[4096];
Allocation_t a, alias_alloc;
Cmd_state_t *cmd = &(task->cmd);
Cmd_alias_alloc_t *pa = &(cmd->cargs.alias_alloc);
debug_printf(1, "handle_alias_allocate: Starting to process command\n");
err = 0;
//** Check the resource **
res = resource_lookup(global_config->rl, rid2str(&(pa->rid), token, sizeof(token)));
if (res == NULL) { //**Can't find the resource
log_printf(1, "handle_alias_allocate: Invalid resource: %s\n", rid2str(&(pa->rid), token, sizeof(token)));
alog_append_alias_alloc(task->myid, -1, 0, 0, 0, 0);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
//** Get the original allocation ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(pa->cap), &a)) != 0) {
log_printf(10, "handle_alias_allocate: Invalid cap: %s rid=%s\n", pa->cap.v, res->name);
alog_append_alias_alloc(task->myid, -1, 0, 0, 0, 0);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
lock_osd_id(a.id);
//** Get the original allocation again with the lock ***
if ((err = get_allocation_by_cap_resource(res, MANAGE_CAP, &(pa->cap), &a)) != 0) {
log_printf(10, "handle_alias_allocate: Invalid cap: %s rid=%s\n", pa->cap.v, res->name);
alog_append_alias_alloc(task->myid, -1, 0, 0, 0, 0);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
unlock_osd_id(a.id);
return(-1);
}
//** Validate the range and duration **
// log_printf(1, "handle_alias_alloc: pa->duration= %u\n", pa->expiration);
if (pa->expiration == 0) pa->expiration = a.expiration;
alog_append_alias_alloc(task->myid, res->rl_index, a.id, pa->offset, pa->len, pa->expiration);
if (pa->expiration > a.expiration) {
log_printf(1, "handle_alias_alloc: ALIAS duration > actual allocation! alias= %u alloc = %u\n", pa->expiration, a.expiration);
send_cmd_result(task, IBP_E_LONG_DURATION);
unlock_osd_id(a.id);
return(-1);
}
if ((pa->len + pa->offset) > a.max_size) {
uint64_t epos = pa->len + pa->offset;
log_printf(1, "handle_alias_alloc: ALIAS range > actual allocation! alias= " LU " alloc = " LU "\n", epos, a.max_size);
send_cmd_result(task, IBP_E_WOULD_EXCEED_LIMIT);
unlock_osd_id(a.id);
return(-1);
}
//*** Create the alias ***
if ((d = create_allocation_resource(res, &alias_alloc, 0, a.type, a.reliability, pa->expiration, 1, 0)) != 0) {
log_printf(1, "handle_alias_alloc: create_allocation_resource failed on RID %s! Error=%d\n", res->name, d);
send_cmd_result(task, IBP_E_GENERIC);
unlock_osd_id(a.id);
return(-1);
}
//** Store the creation timestamp **
set_alloc_timestamp(&(alias_alloc.creation_ts), &(task->ipadd));
//*** Specifify the allocation as a alias ***
alias_alloc.alias_id = a.id;
alias_alloc.alias_offset = pa->offset;
alias_alloc.alias_size = pa->len;
//** and store it back in the DB only **
if ((d = modify_allocation_resource(res, alias_alloc.alias_id, &alias_alloc)) != 0) {
log_printf(1, "handle_alias_allocate: modify_allocation_resource failed on RID %s! Error=%d\n", res->name, d);
send_cmd_result(task, IBP_E_GENERIC);
unlock_osd_id(a.id);
return(-1);
}
//** Update the parent timestamp
update_manage_history(res, a.id, a.is_alias, &(task->ipadd), IBP_ALIAS_ALLOCATE, 0, alias_alloc.alias_offset, alias_alloc.expiration, alias_alloc.alias_size, alias_alloc.id);
unlock_osd_id(a.id);
//** Send the result back **
ns_monitor_t *nm = ns_get_monitor(task->ns);
snprintf(token, sizeof(token), "%d ibp://%s:%d/%s#%s/3862277/READ "
"ibp://%s:%d/%s#%s/3862277/WRITE "
"ibp://%s:%d/%s#%s/3862277/MANAGE \n",
IBP_OK,
nm_get_host(nm), nm_get_port(nm), res->name, alias_alloc.caps[READ_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, alias_alloc.caps[WRITE_CAP].v,
nm_get_host(nm), nm_get_port(nm), res->name, alias_alloc.caps[MANAGE_CAP].v);
debug_code(time_t tt=time(NULL);)
debug_printf(1, "handle_alias_allocate: before sending result time: %s\n", ctime(&tt));
err = write_netstream_block(task->ns, task->cmd_timeout, token, strlen(token));
alog_append_osd_id(task->myid, alias_alloc.id);
debug_printf(1, "handle_alias_allocate: Allocation: %s", token);
debug_code(
if (debug_level() > 5) print_allocation_resource(res, log_fd(), &alias_alloc);
)
return(err);
}
//*****************************************************************
// handle_status - Processes a status request.
//
// IBP_ST_INQ
// v1.3
// status nbytes \n
// hard_max_mb hard_used_mb soft_max_mb soft_used_mb max_duration \n
// v1.4
// status nbytes \n
// VS:1.4 DT:dm_type RID:rid RT:rtype CT:ct_bytes ST:st_bytes UT:ut_bytes UH:uh_bytes SH:sh_bytes
// CH:ch_bytes AT:at_bytes AH:ah_bytes DT:max_duration RE \n
//
// IBP_ST_CHANGE
// status \n
//
// IBP_ST_RES
// status RID1 RID2 ... RIDn \n
//
//*****************************************************************
int handle_status(ibp_task_t *task)
{
Cmd_state_t *cmd = &(task->cmd);
Cmd_status_t *status = &(cmd->cargs.status);
debug_printf(1, "handle_status: Starting to process command\n");
if (status->subcmd == IBP_ST_VERSION) {
alog_append_status_version(task->myid);
char buffer[2048];
char result[4096];
uint64_t total, total_used, total_diff, total_free, rbytes, wbytes, cbytes;
uint64_t r_total, r_diff, r_free, r_used, r_alloc, r_alias, total_alloc, total_alias;
double r_total_gb, r_diff_gb, r_free_gb, r_used_gb;
Net_timeout_t dt;
Resource_t *r;
int i;
set_net_timeout(&dt, 1, 0);
char *version = server_version();
result[0] = '\0'; result[sizeof(result)-1] = '\0';
buffer[0] = '\0'; buffer[sizeof(buffer)-1] = '\0';
sprintf(result, "%d\n", IBP_OK);
strncat(result, version, sizeof(result)-1 - strlen(result));
//** Add the uptime data **
print_uptime(buffer, sizeof(buffer));
strncat(result, buffer, sizeof(result)-1 - strlen(result));
//** Add some stats **
snprintf(buffer, sizeof(buffer)-1, "Total Commands: " LU " Connections: %d\n", task->tid,
network_counter(global_network));
strncat(result, buffer, sizeof(result)-1 - strlen(result));
snprintf(buffer, sizeof(buffer)-1, "Active Threads: %d\n", currently_running_tasks());
strncat(result, buffer, sizeof(result)-1 - strlen(result));
get_transfer_stats(&rbytes, &wbytes, &cbytes);
total = rbytes+wbytes;
r_total_gb = total / (1024.0*1024.0*1024.0);
r_used_gb = rbytes / (1024.0*1024.0*1024.0);
r_free_gb = wbytes / (1024.0*1024.0*1024.0);
snprintf(buffer, sizeof(buffer)-1, "Depot Transfer Stats -- Read: " LU " b (%.2lf GB) Write: " LU " b (%.2lf GB) Total: " LU " b (%.2lf GB)\n",
rbytes, r_used_gb, wbytes, r_free_gb, total, r_total_gb);
strncat(result, buffer, sizeof(result)-1 - strlen(result));
r_total_gb = cbytes / (1024.0*1024.0*1024.0);
snprintf(buffer, sizeof(buffer)-1, "Depot-Depot copies: " LU " b (%.2lf GB)\n", cbytes, r_total_gb);
strncat(result, buffer, sizeof(result)-1 - strlen(result));
total = 0; total_used = 0; total_free = 0; total_diff = 0; total_alloc = 0; total_alias = 0;
for (i=0; i< global_config->n_resources; i++) {
r = &(global_config->res[i]);
r_free = resource_allocable(r, 0);
pthread_mutex_lock(&(r->mutex));
r_total = r->max_size[ALLOC_TOTAL];
r_used = r->used_space[ALLOC_HARD] + r->used_space[ALLOC_SOFT];
r_alloc = r->n_allocs;
r_alias = r->n_alias;
pthread_mutex_unlock(&(r->mutex));
if (r_total > r_used) {
r_diff = r_total - r_used;
} else {
r_diff = 0;
}
total = total + r_total;
total_used = total_used + r_used;
total_diff = total_diff + r_diff;
total_free = total_free + r_free;
total_alloc = total_alloc + r_alloc;
total_alias = total_alias + r_alias;
r_total_gb = r_total / (1024.0*1024.0*1024.0);
r_used_gb = r_used / (1024.0*1024.0*1024.0);
r_diff_gb = r_diff / (1024.0*1024.0*1024.0);
r_free_gb = r_free / (1024.0*1024.0*1024.0);
snprintf(buffer, sizeof(buffer)-1, "RID: %s Max: " LU " b (%.2lf GB) Used: " LU " b (%.2lf GB) Diff: " LU " b (%.2lf GB) Free: " LU " b (%.2lf GB) Allocations: " LU " (" LU " alias)\n",
r->name, r_total, r_total_gb, r_used, r_used_gb, r_diff, r_diff_gb, r_free, r_free_gb, r_alloc, r_alias);
strncat(result, buffer, sizeof(result)-1 - strlen(result));
}
r_total_gb = total / (1024.0*1024.0*1024.0);
r_used_gb = total_used / (1024.0*1024.0*1024.0);
r_diff_gb = total_diff / (1024.0*1024.0*1024.0);
r_free_gb = total_free / (1024.0*1024.0*1024.0);
snprintf(buffer, sizeof(buffer)-1, "Total resources: %d Max: " LU " b (%.2lf GB) Used: " LU " b (%.2lf GB) Diff: " LU " b (%.2lf GB) Free: " LU " b (%.2lf GB) Allocations: " LU " (" LU " alias)\n",
global_config->n_resources, total, r_total_gb, total_used, r_used_gb, total_diff, r_diff_gb, total_free, r_free_gb, total_alloc, total_alias);
strncat(result, buffer, sizeof(result)-1 - strlen(result));
snprintf(buffer,sizeof(result)-1 - strlen(result), "\n");
snprintf(buffer, sizeof(result)-1 - strlen(result), "END\n");
strncat(result, buffer, sizeof(result) - 1 - strlen(result));
i = strlen(result);
write_netstream_block(task->ns, task->cmd_timeout, result, i);
alog_append_cmd_result(task->myid, IBP_OK);
return(0);
}
if (strcmp(global_config->server.password, status->password) != 0) {
log_printf(10, "handle_status: Invalid password: %s\n", status->password);
send_cmd_result(task, IBP_E_WRONG_PASSWD);
return(-1);
}
if (status->subcmd == IBP_ST_RES) {
alog_append_status_res(task->myid);
char buffer[2048];
char result[2048];
int i;
Resource_t *r = global_config->res;
snprintf(result, sizeof(result), "%d ", IBP_OK);
for (i=0; i< global_config->n_resources; i++) {
snprintf(buffer, sizeof(buffer), "%s ", r[i].name);
strncat(result, buffer, sizeof(result) -1 - strlen(result));
}
sprintf(buffer, "\n");
strncat(result, buffer, sizeof(result) -1 - strlen(result));
log_printf(10, "handle_status: Sending resource list: %s\n", result);
write_netstream_block(task->ns, task->cmd_timeout, result, strlen(result));
alog_append_cmd_result(task->myid, IBP_OK);
} else if (status->subcmd == IBP_ST_STATS) { //** Send the depot stats
alog_append_status_stats(task->myid, status->start_time);
Net_timeout_t dt;
convert_epoch_time2net(&dt, task->cmd_timeout);
log_printf(10, "handle_status: Sending stats\n");
send_stats(task->ns, status->start_time, dt);
alog_append_cmd_result(task->myid, IBP_OK);
} else if (status->subcmd == IBP_ST_INQ) {
char buffer[2048];
char result[32];
int n, nres;
Resource_t *r = resource_lookup(global_config->rl, status->crid);
if (r == NULL) {
log_printf(10, "handle_status: Invalid RID :%s\n",status->crid);
alog_append_status_inq(task->myid, -1);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
alog_append_status_inq(task->myid, r->rl_index);
uint64_t totalconfigured;
uint64_t totalused;
uint64_t soft_alloc, hard_alloc, total_alloc;
total_alloc = resource_allocable(r, 0);
pthread_mutex_lock(&(r->mutex));
totalconfigured = r->max_size[ALLOC_TOTAL];
totalused = r->used_space[ALLOC_HARD] + r->used_space[ALLOC_SOFT];
if (totalused > totalconfigured) {
soft_alloc = 0;
hard_alloc = 0;
} else {
soft_alloc = r->max_size[ALLOC_SOFT] - r->used_space[ALLOC_SOFT];
if (soft_alloc > total_alloc) soft_alloc = total_alloc;
hard_alloc = r->max_size[ALLOC_HARD] - r->used_space[ALLOC_HARD];
if (hard_alloc > total_alloc) hard_alloc = total_alloc;
}
if (cmd->version == IBPv040) {
//*** 1 2 3 4 5 6 7 8 9 10 11 12 13 13
n = snprintf(buffer, sizeof(buffer), "%s:1.4:1.0 %s:%d %s:%s %s:%d %s:" LU " %s:" LU " %s:" LU " %s:" LU " %s:" LU " %s:" LU " %s:" LU " %s:" LU " %s:%d %s \n",
ST_VERSION, //** 1
ST_DATAMOVERTYPE, DM_TCP, //** 2
ST_RESOURCEID, r->name, //** 3
ST_RESOURCETYPE, RS_DISK, //** 4
ST_CONFIG_TOTAL_SZ, totalconfigured, //** 5
ST_SERVED_TOTAL_SZ, totalconfigured, //** 6
ST_USED_TOTAL_SZ, totalused, //** 7
ST_USED_HARD_SZ, r->used_space[ALLOC_HARD], //** 8
ST_SERVED_HARD_SZ, r->max_size[ALLOC_HARD], //** 9
ST_CONFIG_HARD_SZ, r->max_size[ALLOC_HARD], //** 10
ST_ALLOC_TOTAL_SZ, total_alloc, //** 11
ST_ALLOC_HARD_SZ, hard_alloc, //** 12
ST_DURATION, r->max_duration, //** 13
ST_RS_END); //** 14
} else {
uint64_t soft, soft_used, hard, hard_used;
soft = r->max_size[ALLOC_SOFT] >> 20; soft_used = r->used_space[ALLOC_SOFT] >> 20;
hard = r->max_size[ALLOC_HARD] >> 20; hard_used = r->used_space[ALLOC_HARD] >> 20;
n = snprintf(buffer, sizeof(buffer), "" LU " " LU " " LU " " LU " %d \n",
hard, hard_used, soft, soft_used, r->max_duration);
}
pthread_mutex_unlock(&(r->mutex));
nres = snprintf(result, sizeof(result), "%d %d \n", IBP_OK, n);
if (cmd->version != IBPv031) write_netstream_block(task->ns, task->cmd_timeout, result, nres);
write_netstream_block(task->ns, task->cmd_timeout, buffer, n);
alog_append_cmd_result(task->myid, IBP_OK);
log_printf(10, "handle_status: Succesfully processed IBP_ST_INQ on RID %s\n", r->name);
log_printf(10, "handle_status: depot info: %s\n", buffer);
return(0);
} else if (status->subcmd == IBP_ST_CHANGE) {
//==================IBP_ST_CHANGE NOT ALLOWED==============================
log_printf(10, "handle_status: IBP_ST_CHANGE!!!! rid=%s ns=%d IGNORING!!\n",status->crid, task->ns->id);
alog_append_status_change(task->myid);
send_cmd_result(task, IBP_E_INVALID_CMD);
close_netstream(task->ns);
return(-1);
Resource_t *r = resource_lookup(global_config->rl, status->crid);
if (r == NULL) {
log_printf(10, "handle_status: Invalid RID :%s\n",status->crid);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
log_printf(10, "handle_status: Requested change on RID %s hard:" LU " soft:" LU " expiration:%ld\n",
r->name, status->new_size[ALLOC_HARD], status->new_size[ALLOC_SOFT], status->new_duration);
pthread_mutex_lock(&(r->mutex));
if (r->used_space[ALLOC_HARD] < status->new_size[ALLOC_HARD]) r->max_size[ALLOC_HARD] = status->new_size[ALLOC_HARD];
if (r->used_space[ALLOC_SOFT] < status->new_size[ALLOC_SOFT]) r->max_size[ALLOC_SOFT] = status->new_size[ALLOC_SOFT];
if (status->new_duration < 0) status->new_duration = INT_MAX;
r->max_duration = status->new_duration;
pthread_mutex_unlock(&(r->mutex));
log_printf(10, "handle_status: Succesfully processed IBP_ST_CHANGE on RID %s hard:" LU " soft:" LU " expiration:%d\n",
r->name, r->max_size[ALLOC_HARD], r->max_size[ALLOC_SOFT], r->max_duration);
cmd->state = CMD_STATE_FINISHED;
send_cmd_result(task, IBP_OK);
return(0);
} else {
log_printf(10, "handle_status: Invalid sub command :%d\n",status->subcmd);
send_cmd_result(task, IBP_E_INVALID_CMD);
return(0);
}
debug_printf(1, "handle_status: Successfully processed command\n");
return(0);
}
//*****************************************************************
// handle_manage - Processes the manage allocation command
//
// IBP_INCR | IBP_DECR | IBP_CHNG
// status \n
//
// IBP_PROBE (for an IBP_MANAGE command)
// status read_refcnt write_refcnt curr_size max_size time_remaining \n
// IBP_PROBE (for an IBP_ALIAS_MANAGE command)
// status read_refcnt write_refcnt offset len time_remaining \n
//
// NOTE: From my unsderstanding this command is flawed since any only READ counts
// are used to delete an allocation!! Both READ and WRITE counts
// should be used.
//*****************************************************************
int handle_manage(ibp_task_t *task)
{
Cmd_state_t *cmd = &(task->cmd);
Cmd_manage_t *manage = &(cmd->cargs.manage);
Allocation_t *a = &(manage->a);
Allocation_t ma;
osd_id_t id, pid;
int lock, err, is_alias;
uint64_t alias_offset, alias_len;
if (cmd->command == IBP_MANAGE) {
debug_printf(1, "handle_manage: Starting to process IBP_MANAGE command ns=%d\n", ns_getid(task->ns));
} else {
debug_printf(1, "handle_manage: Starting to process IBP_ALIAS_MANAGE command ns=%d\n", ns_getid(task->ns));
}
Resource_t *r = resource_lookup(global_config->rl, manage->crid);
if (r == NULL) {
log_printf(10, "handle_manage: Invalid RID :%s\n",manage->crid);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_INVALID_RID);
return(-1);
}
if ((err = get_allocation_by_cap_resource(r, MANAGE_CAP, &(manage->cap), a)) != 0) {
log_printf(10, "handle_manage: Invalid cap: %s rid=%s\n", manage->cap.v, r->name);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
pid = a->id;
is_alias = a->is_alias;
alias_offset = 0;
alias_len = a->max_size;
if ((a->is_alias == 1) && (cmd->command == IBP_MANAGE)) { //** This is a alias cap so load the master and invoke restrictions
is_alias = 1;
id = a->alias_id;
alias_offset = a->alias_offset;
alias_len = a->alias_size;
log_printf(10, "handle_manage: ns=%d got a alias cap! loading id " LU "\n", ns_getid(task->ns), id);
if ((err = get_allocation_resource(r, id, a)) != 0) {
log_printf(10, "handle_manage: Invalid alias id: " LU " rid=%s\n", id, r->name);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
if (alias_len == 0) alias_len = a->max_size - alias_offset;
//** Can only isue a probe on a alias allocation through ibp_manage
if (manage->subcmd != IBP_PROBE) {
log_printf(10, "handle_manage: Invalid subcmd for alias id: " LU " rid=%s\n", id, r->name);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_INVALID_CMD);
return(-1);
}
}
//** Verify the master allocation if needed. ***
if ((is_alias == 1) && (cmd->command == IBP_ALIAS_MANAGE) && (manage->subcmd != IBP_PROBE)) {
if ((err = get_allocation_resource(r, a->alias_id, &ma)) != 0) {
log_printf(10, "handle_manage: Invalid alias id: " LU " rid=%s\n", a->alias_id, r->name);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);
}
if (strcmp(ma.caps[MANAGE_CAP].v, manage->master_cap.v) != 0) {
log_printf(10, "handle_manage: Master cap doesn't match with alias read: %s actual: %s rid=%s ns=%d\n",
manage->master_cap.v, ma.caps[MANAGE_CAP].v, r->name, ns_getid(task->ns));
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_INVALID_MANAGE_CAP);
return(-1);
}
}
lock_osd_id(a->id); //** Lock it so we don't get race updates
//** Re-read the data with the lock enabled
id = a->id;
if ((err = get_allocation_resource(r, id, a)) != 0) {
log_printf(10, "handle_manage: Error reading id after lock_osd_id id: " LU " rid=%s\n", id, r->name);
alog_append_manage_bad(task->myid, cmd->command, manage->subcmd);
send_cmd_result(task, IBP_E_CAP_NOT_FOUND);
return(-1);