1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FRSH (FRescor ScHeduler) */
27 /* FRSH is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FRSH is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FRSH; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FRSH header files in a file, */
39 /* instantiating FRSH generics or templates, or linking other files */
40 /* with FRSH objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Mon Oct 20 18:00:18 2008
52 * @brief FRESCOR Contract Broker
56 #define WITH_C99 /* For ul_gsa.h iterators */
57 #include <semaphore.h>
60 #include <forb/config.h>
62 #include <fcb_contact_info.h>
66 #include <ul_gavlcust.h>
67 #include <ul_gsacust.h>
70 #include <ul_logreg.h>
71 #include <forb/server_id.h>
72 #include <fres_contract.h>
73 #include "fcb_config.h"
74 #include <fosa_clocks_and_timers.h>
75 #include "contract_log.h"
76 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
77 #include <forb/proto_inet.h>
80 UL_LOG_CUST(ulogd_fcb);
81 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
82 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
84 bool opt_daemon = false;
85 char *opt_pidfile = NULL;
86 fosa_abs_time_t start_time;
88 /** List of contracts to be newly reserved or changed by resource manager */
89 struct reservation_list {
90 ul_list_head_t fcb_contracts;
95 * Resource identification
98 frsh_resource_type_t type;
99 frsh_resource_id_t id;
103 * Registered resource
109 fres_resource_manager mng; /**< Object reference of the resource manager */
110 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
111 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
112 struct reservation_list rl; /**< Temporary list of contracts to be reserved on this resource */
116 * Resource allocator registered in different nodes/applications
121 fres_resource_allocator ra;
124 struct fcb_contract {
125 fres_contract_id_t id;
126 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
127 gavl_node_t node_fcb;
128 ul_list_node_t node_sc;
129 ul_list_node_t node_reservation;
134 fosa_abs_time_t end_of_stability_period;
135 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
136 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
137 struct fres_contract *to_be_reserved_contract;
138 struct fres_contract *schedulable_contract;
142 * Contract broker data
145 gavl_cust_root_field_t resources; /**< Registered resources */
146 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
149 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
151 struct fcb_contract *fcb_contract;
153 fcb_contract = malloc(sizeof(*fcb_contract));
157 memset(fcb_contract, 0, sizeof(*fcb_contract));
158 fcb_contract->id = *id;
163 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
165 if (fcb_contract->user_contract) {
166 fres_contract_destroy(fcb_contract->user_contract);
168 if (fcb_contract->to_be_reserved_contract) {
169 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
171 if (fcb_contract->requested_contract) {
172 fres_contract_destroy(fcb_contract->requested_contract);
174 if (fcb_contract->schedulable_contract) {
175 fres_contract_destroy(fcb_contract->requested_contract);
180 static inline int res_key_cmp(const struct res_key *a,
181 const struct res_key *b)
183 if (a->type < b->type) {
185 } else if (a->type > b->type) {
187 } else if (a->id < b->id) {
189 } else if (a->id > b->id) {
196 /* Container for registered resources */
197 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
198 struct fcb /* cust_root_t */, \
199 struct resource/* cust_item_t */, \
200 struct res_key /* cust_key_t */, \
201 resources /* cust_root_node */, \
202 node /* cust_item_node */, \
203 key /* cust_item_key */, \
204 res_key_cmp /* cust_cmp_fnc */);
206 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
207 struct fcb /* cust_root_t */, \
208 struct resource/* cust_item_t */, \
209 struct res_key /* cust_key_t */, \
210 resources /* cust_root_node */, \
211 node /* cust_item_node */, \
212 key /* cust_item_key */, \
213 res_key_cmp /* cust_cmp_fnc */);
215 /* Container for allocators registered for a given resource */
216 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
217 struct resource /* cust_root_t */, \
218 struct res_alloc /* cust_item_t */, \
219 forb_server_id /* cust_key_t */, \
220 allocators /* cust_root_node */, \
221 node /* cust_item_node */, \
222 app /* cust_item_key */, \
223 fres_contract_id_cmp /* cust_cmp_fnc */);
225 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
226 struct resource /* cust_root_t */, \
227 struct res_alloc /* cust_item_t */, \
228 forb_server_id /* cust_key_t */, \
229 allocators /* cust_root_node */, \
230 node /* cust_item_node */, \
231 app /* cust_item_key */, \
232 forb_server_id_cmp /* cust_cmp_fnc */);
234 /* Container for contracts with spare capacity negotiated for a given resource */
235 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
236 struct resource /* cust_head_t */, \
237 struct fcb_contract /* cust_item_t */, \
238 sc_contracts /* cust_head_field */, \
239 node_sc /* cust_node_field */);
241 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
242 struct reservation_list /* cust_head_t */, \
243 struct fcb_contract /* cust_item_t */, \
244 fcb_contracts /* cust_head_field */, \
245 node_reservation /* cust_node_field */);
247 /* Container for negotiated contracts */
248 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
249 struct fcb /* cust_root_t */, \
250 struct fcb_contract /* cust_item_t */, \
251 fres_contract_id_t /* cust_key_t */, \
252 contracts /* cust_root_node */, \
253 node_fcb /* cust_item_node */, \
254 id /* cust_item_key */, \
255 fres_contract_id_cmp /* cust_cmp_fnc */);
258 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
259 struct fcb /* cust_root_t */, \
260 struct fcb_contract /* cust_item_t */, \
261 fres_contract_id_t /* cust_key_t */, \
262 contracts /* cust_root_node */, \
263 node_fcb /* cust_item_node */, \
264 id /* cust_item_key */, \
265 fres_contract_id_cmp /* cust_cmp_fnc */);
267 #include "fcb_contract_gavl.inc"
271 gsa_array_field_t array;
274 GSA_CUST_DEC(res_array /* cust_prefix */,
275 struct res_array /* cust_array_t */,
276 struct resource /* cust_item_t */,
277 struct res_key /* cust_key_t */,
278 array /* cust_array_field */,
279 key /* cust_item_key */,
280 res_key_cmp /* cust_cmp_fnc */);
282 GSA_CUST_IMP(res_array /* cust_prefix */,
283 struct res_array /* cust_array_t */,
284 struct resource /* cust_item_t */,
285 struct res_key /* cust_key_t */,
286 array /* cust_array_field */,
287 key /* cust_item_key */,
288 res_key_cmp /* cust_cmp_fnc */,
289 true /* cust_ins_fl */);
293 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
295 static struct res_key*
296 get_res_key(const struct fres_contract *contract, struct res_key *key)
298 fres_block_resource *block_res;
300 block_res = fres_contract_get_resource(contract);
302 ul_logerr("No resource specified\n");
305 key->type = block_res->resource_type;
306 key->id = block_res->resource_id;
311 static struct res_key*
312 get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
314 if (fc->user_contract)
315 get_res_key(fc->user_contract, key);
317 get_res_key(fc->requested_contract, key);
322 * Fills in an array of fcb_contracts according to requests submited
323 * by an application through negotiate_contracts() or
324 * negotiate_transaction(). For every contract in @a contracts, there
325 * is allocated one fcb_contract in @a fcb_contracts array.
328 * @param fcb_contracts Where to store pointers to fcb_contracts
329 * @param contracts Contracts submited for negotiation
330 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
332 * @return Zero on success, non-zero error code on error.
335 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
336 struct fres_contract *contracts[], int num)
339 struct fcb_contract *fc;
341 for (i=0; i<num; i++) {
342 struct fres_contract *c = contracts[i];
344 if (fres_contract_id_is_empty(&c->id)) {
345 /* Normal negotiation request */
346 forb_uuid_generate((forb_uuid_t *)&c->id);
347 fc = fcb_contract_new(&c->id);
350 fcb_contracts[i] = fc;
351 log_contract("Negotiation request", i, c);
353 fc = fcb_contract_find(fcb, &c->id);
356 fres_contract_id_to_string(str, &c->id, sizeof(str));
357 ul_logerr("Attempt to change unknown contract %s\n", str);
358 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
360 fcb_contracts[i] = fc;
361 if (fres_contract_get_num_blocks(c) == 0) {
363 log_contract("Cancelation request", i, fc->user_contract);
366 log_contract("Renegotiation request", i, fc->user_contract);
370 fc->requested_contract = c;
376 * Ensure that all contracts belong to the same resource and find
377 * resource allocators for the contracts.
380 * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
381 * @param num Number of contracts in @a fcb_contracts
382 * @param resource Resource for which negotiation is being done.
383 * @param app Application which requests negotiation
386 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
387 struct resource **resource,
392 struct res_alloc *ra;
393 struct res_key key, key2 = {-1,-1};
395 for (i=0; i<num; i++) {
396 struct fcb_contract *fc = fcb_contracts[i];
398 if (!get_fc_res_key(fc, &key))
399 return FRSH_ERR_RESOURCE_ID_INVALID;
401 /* Check that all contracts are for the same resource */
404 *resource = fcb_resource_find(fcb, &key);
406 ul_logerr("No resource manager for %d.%d registered\n",
408 return FRES_ERR_NO_RESOURCE_MANAGER;
410 /* Find allocator for this request */
411 ra = fcb_alloc_find(*resource, app);
414 forb_server_id_to_string(str, app, sizeof(str));
415 ul_logerr("No resource allocator found for %d.%d and %s\n",
416 (*resource)->key.type, (*resource)->key.id, str);
417 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
420 else if (key.type != key2.type ||
422 ul_logerr("Contract #%d differs in resource!\n", i);
423 return FRSH_ERR_RESOURCE_ID_INVALID;
431 * Prepares a list of contracts to pass to resource manager for (re)reserving.
433 * @todo Needs to be changed for compatibility with transactions.
435 * @param resource Resource for which to rebalance capacity and negotiate new contracts
436 * @param fcb_contract New requests to negotiate
437 * @param num The number of elements in @a fcb_contract
438 * @param[out] rl List with changed contracts to be commited
441 prepare_reservation_list(struct resource *resource,
442 struct fcb_contract *fcb_contract[], int num)
446 struct fcb_contract *fc;
448 reservation_list_init_head(&resource->rl);
449 resource->rl.length = 0;
450 for (i=0; i<num; i++) {
451 assert(fcb_contract[i]->requested_contract != NULL);
452 reservation_list_insert(&resource->rl, fcb_contract[i]);
453 resource->rl.length++;
455 fosa_clock_get_time(CLOCK_REALTIME, &now);
456 ul_list_for_each(sc_contracts, resource, fc) {
457 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
458 fc->requested_contract == NULL) /* Do not insert contract inserted above */
460 reservation_list_insert(&resource->rl, fc);
461 resource->rl.length++;
467 reserve_resource(struct resource *resource)
470 fres_contract_ptr_seq contracts;
471 struct fres_contract *c;
472 struct fcb_contract *fc;
473 CORBA_Environment ev;
475 if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
479 forb_sequence_length(&contracts) = resource->rl.length;
482 /* Prepare FORB sequence */
483 ul_list_for_each(reservation_list, &resource->rl, fc) {
485 if (fc->to_be_reserved_contract)
486 /* Contract without spare capacity */
487 c = fc->to_be_reserved_contract;
488 else if (fc->requested_contract)
489 c = fc->requested_contract;
491 forb_sequence_elem(&contracts, i++) = c;
494 /* Reserve contract */
495 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
496 if (forb_exception_occurred(&ev)) {
497 ret = fres_forbex2err(&ev);
498 ul_logerr("FORB exception when reserving contracts\n");
502 ul_logerr("Contract reservation error %d\n", ret);
503 ret = FRES_ERR_ADMISSION_TEST;
508 forb_sequence_free_buf(&contracts, forb_no_destructor);
516 * @param resource Resource for which to rebalance capacity and negotiate new contracts
517 * @param rl List with changed contracts to be commited
519 * @return Zero on success, non-zero error code on error
522 rebalance_spare_capacity_and_reserve(struct resource *resource)
525 struct reservation_list *rl = &resource->rl;
526 struct fcb_contract *fc;
527 fres_block_spare_capacity *s;
529 /* Initialize optimization */
530 ul_list_for_each(reservation_list, rl, fc) {
531 fc->to_be_reserved_contract =
532 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
533 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
535 assert(fc->to_be_reserved_contract != NULL);
537 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
538 if (s && s->granularity == FRSH_GR_DISCRETE) {
539 fc->sc_variant.initial = s->variants._length - 1;
540 fc->sc_variant.try = fc->sc_variant.initial;
544 bool all_combinations_tried;
545 int criterion, best_criterion = -1;
546 struct fcb_contract *fcb_changed;
547 /* Exhaustive search. Try all combinations of spare capacity
548 * variants and find the one with best creterion. */
550 all_combinations_tried = true;
553 /* Prepare spare capacity variant */
554 ul_list_for_each(reservation_list, rl, fc) {
555 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
556 /* TODO: Simulate continuous granularity by discretization */
557 if (s && s->granularity == FRSH_GR_DISCRETE) {
558 fres_container_copy(fc->to_be_reserved_contract->container,
559 forb_sequence_elem(&s->variants, fc->sc_variant.try));
560 criterion += fc->sc_variant.try;
562 if (fcb_changed == NULL) {
563 /* Chnage tried variant for the next round */
564 fc->sc_variant.try = fc->sc_variant.try > 0 ?
565 fc->sc_variant.try - 1 :
566 s->variants._length - 1;
567 /* Do not change other
568 * contracts unless we have
569 * tried all variants */
570 if (fc->sc_variant.try != fc->sc_variant.initial) {
574 if (fc->sc_variant.try != fc->sc_variant.initial)
575 all_combinations_tried = false;
579 if (criterion > best_criterion) {
580 ret = reserve_resource(resource);
584 /* negotiation succeeded */
585 best_criterion = criterion;
587 } while (!all_combinations_tried);
589 if (best_criterion == -1) {
590 ret = FRSH_ERR_CONTRACT_REJECTED;
592 /* At least some variant succeeded */
594 sc_contracts_init_head(resource);
595 ul_list_for_each(reservation_list, rl, fc) {
596 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
597 if (s && s->granularity == FRSH_GR_DISCRETE) {
598 sc_contracts_insert(resource, fc);
603 ul_list_for_each(reservation_list, rl, fc) {
604 fres_contract_destroy(fc->to_be_reserved_contract);
605 fc->to_be_reserved_contract = NULL;
611 * Create/change VReses according to @a schedulable_contracts.
613 * There might be more allocators for schedulable contracts, so merge
614 * adjacent vreses with the same allocator together and create/change
615 * them in one step. Also the order of schedulable contracts might be
616 * different from the initial order od ids in commit_contracts()
617 * therefore we have to search for every contract.
620 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
622 struct res_alloc *last_ra = NULL;
623 fres_contract_ptr_seq vreses;
625 CORBA_Environment ev;
627 if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
632 for (i=0; i<schedulable_contracts->_length; i++) {
633 struct fcb_contract *fc;
634 struct fres_contract *sc = schedulable_contracts->_buffer[i];
636 log_contract("Changing VRES", i, sc);
638 fc = fcb_contract_find(fcb, &sc->id);
641 if (true /* TODO: if the schedulable contract is changed */) {
642 if (last_ra != fc->ra) {
643 if (vreses._length) {
644 ret = fres_resource_allocator_change_vreses(last_ra->ra,
646 if (forb_exception_occurred(&ev)) {
647 ret = fres_forbex2err(&ev);
654 vreses._buffer[vreses._length] = sc;
656 fres_contract_destroy(fc->schedulable_contract);
657 fc->schedulable_contract = fres_contract_duplicate(sc);
663 ret = fres_resource_allocator_change_vreses(last_ra->ra,
665 if (forb_exception_occurred(&ev))
666 ret = fres_forbex2err(&ev);
669 forb_sequence_free_buf(&vreses, forb_no_destructor);
674 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
677 struct fcb_contract *fc;
678 for (i=0; i<num; i++) {
679 fc = fcb_contracts[i];
680 if (fc && !fc->user_contract) {
681 fc->requested_contract = NULL; /* Destroyed by FORB */
682 fcb_contract_destroy(fc);
689 commit_resource(struct resource *resource,
690 fres_contract_ptr_seq **schedulable_contracts)
693 fres_contract_id_seq commit_ids;
695 struct fcb_contract *fc;
696 CORBA_Environment ev;
698 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
703 commit_ids._length = resource->rl.length;
705 ul_list_for_each(reservation_list, &resource->rl, fc) {
706 forb_sequence_elem(&commit_ids, i) = fc->id;
710 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
711 schedulable_contracts, &ev);
712 if (forb_exception_occurred(&ev)) {
713 ret = fres_forbex2err(&ev);
718 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
723 int cancel_reservations(struct resource *resource)
726 fres_contract_id_seq commit_ids;
728 struct fcb_contract *fc;
729 CORBA_Environment ev;
731 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
736 commit_ids._length = resource->rl.length;
738 ul_list_for_each(reservation_list, &resource->rl, fc) {
739 forb_sequence_elem(&commit_ids, i) = fc->id;
743 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
744 if (forb_exception_occurred(&ev)) {
745 ret = fres_forbex2err(&ev);
750 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
757 negotiate_contracts(fres_contract_broker obj,
758 const fres_contract_ptr_seq* contracts,
759 fres_contract_id_seq** ids_out,
760 CORBA_Environment *ev)
762 struct fcb *fcb = o2fcb(obj);
763 struct resource *resource = NULL;
766 fres_contract_ptr_seq *schedulable_contracts;
767 struct fcb_contract **fcb_contracts, *fc;
769 int num = contracts->_length;
771 /* Prepare output sequence for the case we return eariler with
773 forb_sequence_alloc(*ids_out, 0);
775 ev->major = FORB_EX_NO_MEMORY;
778 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
780 forb_get_req_source(obj, &app);
782 fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
783 if (!fcb_contracts) {
787 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
789 ret = prepare_fcb_contracts(fcb, fcb_contracts,
790 contracts->_buffer, num);
792 goto err_free_fcb_contracts;
793 ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
796 goto err_free_fcb_contracts;
798 prepare_reservation_list(resource, fcb_contracts, num);
800 /* Reserve contracts */
801 ret = rebalance_spare_capacity_and_reserve(resource);
803 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
804 ul_logmsg("Contract(s) was/were rejected\n");
807 fres_strerror(ret, msg, sizeof(msg));
808 ul_logerr("Reservation error: %s\n", msg);
810 goto err_free_fcb_contracts;
813 /* Commit contracts */
814 ret = commit_resource(resource, &schedulable_contracts);
816 goto err_cancel_reservation;
818 /* Add new contracts to our fcb database for later
819 * reference. Canceled contracts are removed below. */
820 for (i=0; i<num; i++) {
821 fc = fcb_contracts[i];
823 if (fc->user_contract) {
824 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
826 fres_contract_destroy(fc->user_contract);
827 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
828 /* Note: requested_contract is also
829 * pointed by contracts parameter and
830 * will be freed by FORB. */
831 fc->requested_contract = NULL;
834 /* Insert new contracts */
835 fcb_contract_insert(fcb, fcb_contracts[i]);
836 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
837 fc->requested_contract = NULL;
838 /* See the note above. */
842 ret = change_vreses(fcb, schedulable_contracts);
844 goto err_cancel_reservation;
846 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
848 ul_logerr("VRes was not created\n");
849 goto err_cancel_contracts;
853 /* Return IDs and delete canceled contracts from out database */
854 if (!forb_sequence_alloc_buf(*ids_out, num)) {
855 ev->major = FORB_EX_NO_MEMORY;
856 goto err_cancel_contracts;
858 (*ids_out)->_length = num;
859 for (i=0; i<num; i++) {
860 struct fcb_contract *fc = fcb_contracts[i];
861 forb_sequence_elem(*ids_out, i) = fc->id;
863 if (fc->requested_contract &&
864 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
865 fcb_contract_delete(fcb, fc);
866 /* Note: requested_contract is also pointed by
867 * contracts parameter and will be freed by FORB. */
868 fc->requested_contract = NULL;
869 fcb_contract_destroy(fc);
874 err_cancel_contracts:
876 goto err_free_fcb_contracts;
877 err_cancel_reservation:
878 cancel_reservations(resource);
879 err_free_fcb_contracts:
880 free_fcb_contracts(fcb_contracts, num);
885 void redistribute_spare_capacity(fres_contract_broker obj,
886 const frsh_resource_type_t restype,
887 const frsh_resource_id_t resid,
888 CORBA_Environment *ev)
890 struct fcb *fcb = o2fcb(obj);
891 struct res_key key = {restype, resid };
892 struct resource *resource;
894 resource = fcb_resource_find(fcb, &key);
896 prepare_reservation_list(resource, NULL, 0);
898 /* forb_sequence_alloc(ids, rl.length); */
899 /* if (!ids || !ids->_buffer) { */
900 /* ev->major = FORB_EX_NO_MEMORY; */
901 /* goto err_free_fcb_contracts; */
903 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
904 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
907 rebalance_spare_capacity_and_reserve(resource);
911 CORBA_long register_resource(fres_contract_broker obj,
912 const frsh_resource_type_t restype,
913 const frsh_resource_id_t resid,
914 const fres_resource_desc *desc,
915 CORBA_Environment *ev)
917 struct fcb *fcb = o2fcb(obj);
918 struct resource *res, *res2;
920 res = malloc(sizeof(*res));
922 memset(res, 0, sizeof(*res));
923 res->key.type = restype;
925 res2 = fcb_resource_find(fcb, &res->key);
927 if (forb_object_is_stale(res2->mng)) {
928 ul_logmsg("Removing stale manager for resource %d.%d\n",
930 forb_object_release(res2->mng);
931 fcb_resource_delete(fcb, res2);
932 /* TODO: Delete also all allocators associated
933 * with this stale resource manager. */
937 ul_logerr("Resource manager %d.%d already registered\n",
942 res->mng = forb_object_duplicate(desc->manager);
943 res->name = desc->name;
945 fcb_alloc_init_root_field(res);
946 sc_contracts_init_head(res);
947 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
948 res->name, restype, resid);
949 fcb_resource_insert(fcb, res);
958 CORBA_long register_allocator(fres_contract_broker obj,
959 const frsh_resource_type_t restype,
960 const frsh_resource_id_t resid,
961 const fres_resource_allocator ra_obj,
962 CORBA_Environment *ev)
964 struct fcb *fcb = o2fcb(obj);
965 struct resource *res;
966 struct res_alloc *ra;
967 struct res_key resource;
968 forb_server_id server_id;
969 char server_id_str[40];
972 forb_get_server_id(ra_obj, &server_id);
973 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
974 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
975 restype, resid, server_id_str);
977 resource.type = restype;
979 res = fcb_resource_find(fcb, &resource);
981 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
983 ret = FRES_ERR_NO_RESOURCE_MANAGER;
986 ra = fcb_alloc_find(res, &server_id);
988 char *str = forb_object_to_string(ra_obj);
989 ul_logerr("Allocator from already registered (%s)\n",
992 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
995 ra = malloc(sizeof(*ra));
1000 ra->app = server_id;
1001 ra->ra = forb_object_duplicate(ra_obj);
1002 fcb_alloc_insert(res, ra);
1008 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1010 struct fcb *fcb = o2fcb(obj);
1011 fres_resource_seq *seq;
1012 struct resource *res;
1015 seq = malloc(sizeof(*seq));
1017 ev->major = FORB_EX_NO_MEMORY;
1020 memset(seq, 0, sizeof(*seq));
1023 gavl_cust_for_each(fcb_resource, fcb, res) {
1027 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1032 gavl_cust_for_each(fcb_resource, fcb, res) {
1033 seq->_buffer[n].restype = res->key.type;
1034 seq->_buffer[n].resid = res->key.id;
1035 seq->_buffer[n].desc.manager = res->mng;
1036 seq->_buffer[n].desc.name = res->name;
1044 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1046 struct fres_contract **c;
1047 forb_sequence_foreach(&transaction->contracts, c)
1048 if (fres_contract_get_spare_capacity(*c))
1054 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1055 int num, struct res_array *res_array)
1058 struct resource *resource;
1062 res_array_init_array_field(res_array);
1063 for (i = 0; i < num; i++) {
1064 get_fc_res_key(fc[i], &key);
1065 resource = fcb_resource_find(fcb, &key);
1066 ret = res_array_insert(res_array, resource);
1068 ret = FRSH_ERR_INTERNAL_ERROR;
1078 negotiate_transaction(fres_contract_broker _obj,
1079 const fres_transaction_t* transaction,
1080 CORBA_Environment *ev)
1082 struct fcb *fcb = o2fcb(_obj);
1083 struct fcb_contract **fcb_contracts, *fc;
1084 const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1085 int num = user_contracts->_length;
1087 struct res_array res_array;
1088 fres_contract_ptr_seq res_contracts;
1091 struct resource *resource;
1093 if (transaction_has_spare_capacity(transaction)) {
1094 ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1098 fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1099 if (!fcb_contracts) {
1103 memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1105 ret = prepare_fcb_contracts(fcb, fcb_contracts,
1106 user_contracts->_buffer, num);
1108 goto err_free_fcb_contracts;
1110 ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1112 if (!forb_sequence_alloc_buf(&res_contracts, num)) {
1114 goto err_free_resources;
1117 gsa_cust_for_each(res_array, &res_array, resource) {
1118 reservation_list_init_head(&resource->rl);
1119 resource->rl.length = 0;
1120 for (i = 0; i < num; i++) {
1121 fc = fcb_contracts[i];
1122 get_fc_res_key(fc, &key);
1123 if (res_key_cmp(&key, &resource->key) == 0) {
1124 reservation_list_insert(&resource->rl, fc);
1125 resource->rl.length++;
1129 /* gsa_cust_for_each(res_array, &res_array, resource) { */
1130 /* CORBA_Environment ev; */
1131 /* /\* Reserve contract *\/ */
1132 /* reservation_list2s */
1133 /* ret = fres_resource_manager_reserve_contracts(resource->mng, &res_contracts, &ev); */
1134 /* if (forb_exception_occurred(&ev)) { */
1135 /* ret = fres_forbex2err(&ev); */
1136 /* ul_logerr("FORB exception when reserving contracts\n"); */
1137 /* goto err_free_res_contracts; */
1139 /* if (ret < 0) { */
1140 /* ul_logerr("Contract reservation error %d\n", ret); */
1141 /* ret = FRES_ERR_ADMISSION_TEST; */
1144 /* if (ret == 0) { /\* negotiation succeeded *\/ */
1149 err_free_res_contracts:
1150 forb_sequence_free_buf(&res_contracts, forb_no_destructor);
1152 res_array_delete_all(&res_array);
1153 err_free_fcb_contracts:
1154 free_fcb_contracts(fcb_contracts, num);
1160 wait_transaction(fres_contract_broker _obj,
1161 const CORBA_char * name,
1162 fres_transaction_t** transaction,
1163 CORBA_Environment *ev)
1165 return FRSH_ERR_NOT_IMPLEMENTED;
1169 allocate_transaction_vres(fres_contract_broker _obj,
1170 const CORBA_long id,
1171 CORBA_Environment *ev)
1173 return FRSH_ERR_NOT_IMPLEMENTED;
1177 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1178 static int register_inet_port(forb_orb orb)
1180 forb_port_t *port = forb_malloc(sizeof(*port));
1182 struct in_addr listen_on;
1186 memset(port, 0, sizeof(*port));
1187 listen_on.s_addr = INADDR_ANY;
1188 ret = forb_inet_port_init(&port->desc, listen_on, 0);
1191 ret = forb_register_port(orb, port);
1196 struct forb_fres_contract_broker_impl impl = {
1197 .negotiate_contracts = negotiate_contracts,
1198 .register_resource = register_resource,
1199 .register_allocator = register_allocator,
1200 .redistribute_spare_capacity = redistribute_spare_capacity,
1201 .get_resources = get_resources,
1202 .negotiate_transaction = negotiate_transaction,
1203 .wait_transaction = wait_transaction,
1204 .allocate_transaction_vres = allocate_transaction_vres,
1207 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1209 forb_server_id server_id;
1210 char server_id_str[sizeof(forb_server_id)*2+1];
1212 forb_get_server_id(peer_orb, &server_id);
1213 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1215 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1221 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1222 fosa_abs_time_t now;
1223 fosa_rel_time_t delay;
1224 fosa_clock_get_time(CLOCK_REALTIME, &now);
1225 delay = fosa_abs_time_extract_interval(start_time, now);
1226 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1227 fosa_rel_time_to_msec(delay));
1231 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1235 static struct option long_opts[] = {
1236 { "daemon", optional_argument, NULL, 'd' },
1237 { "loglevel", required_argument, NULL, 'l' },
1244 printf("usage: fcb [ options ]\n");
1245 printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
1246 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
1249 int print_log_domain(ul_log_domain_t *domain, void *context)
1251 printf("%s = %d\n", domain->name, domain->level);
1255 int main(int argc, char *argv[])
1258 struct fcb fcb_data;
1259 fres_contract_broker fcb;
1260 forb_executor_t executor;
1262 forb_init_attr_t attr = {
1263 .orb_id = "org.frescor.fcb",
1264 .peer_discovery_callback = peer_discovery_callback,
1265 .peer_dead_callback = peer_dead_callback,
1266 .fixed_tcp_port = FCB_TCP_PORT,
1267 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
1268 .fixed_server_id = FCB_SERVER_ID,
1269 .redistribute_hellos = true,
1274 while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1277 if (*optarg == '?') {
1278 ul_logreg_for_each_domain(print_log_domain, NULL);
1283 ret = ul_log_domain_arg2levels(optarg);
1285 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1290 opt_pidfile = optarg;
1295 exit(opt == 'h' ? 0 : 1);
1299 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1302 forb_daemon_prepare(opt_pidfile);
1304 orb = forb_init(&argc, &argv, &attr);
1305 if (!orb) error(1, errno, "FORB initialization failed");
1307 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1308 ret = register_inet_port(orb);
1309 if (ret) error(0, errno, "INET port registration failed");
1312 fcb_resource_init_root_field(&fcb_data);
1313 fcb_contract_init_root_field(&fcb_data);
1315 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1316 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1318 /* Prepare executor before we register the fcb reference,
1319 * so that no reuqests are lost */
1320 ret = forb_executor_init(&executor);
1321 if (ret) error(1, errno, "forb_executor_init failed");
1323 ret = forb_executor_register_object(&executor, fcb);
1324 if (ret) error(1, errno, "forb_executor_register_object failed");
1326 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1327 if (ret) error(1, errno, "forb_register_reference() failed");
1329 ul_logmsg("Waiting for requests\n");
1331 forb_daemon_ready();
1333 ret = forb_executor_run(&executor);
1334 if (ret) error(1, errno, "forb_executor_run failed");