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 #define COMPILE_TIME_ASSERT(cond, msg) \
81 typedef char msg[(cond) ? 1 : -1]
84 UL_LOG_CUST(ulogd_fcb);
85 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
86 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
88 bool opt_daemon = false;
89 char *opt_pidfile = NULL;
90 fosa_abs_time_t start_time;
92 /** List of contracts to be newly reserved or changed by resource manager */
93 struct reservation_list {
94 ul_list_head_t fcb_contracts;
99 * Resource identification
102 frsh_resource_type_t type;
103 frsh_resource_id_t id;
107 * Registered resource
113 fres_resource_manager mng; /**< Object reference of the resource manager */
114 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
115 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
116 struct reservation_list rl; /**< Temporary list of contracts to be reserved on this resource */
120 * Resource allocator registered in different nodes/applications
125 fres_resource_allocator ra;
128 struct fcb_contract {
129 fres_contract_id_t id;
130 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
131 gavl_node_t node_fcb;
132 ul_list_node_t node_sc;
133 ul_list_node_t node_reservation;
138 fosa_abs_time_t end_of_stability_period;
139 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
140 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
141 struct fres_contract *to_be_reserved_contract;
142 struct fres_contract *schedulable_contract;
146 * Contract broker data
149 fres_contract_id_t contract_counter;
150 gavl_cust_root_field_t resources; /**< Registered resources */
151 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
154 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
156 struct fcb_contract *fcb_contract;
158 fcb_contract = malloc(sizeof(*fcb_contract));
162 memset(fcb_contract, 0, sizeof(*fcb_contract));
163 fcb_contract->id = *id;
168 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
170 if (fcb_contract->user_contract) {
171 fres_contract_destroy(fcb_contract->user_contract);
173 if (fcb_contract->to_be_reserved_contract) {
174 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
176 if (fcb_contract->requested_contract) {
177 fres_contract_destroy(fcb_contract->requested_contract);
179 if (fcb_contract->schedulable_contract) {
180 fres_contract_destroy(fcb_contract->requested_contract);
185 static inline int res_key_cmp(const struct res_key *a,
186 const struct res_key *b)
188 if (a->type < b->type) {
190 } else if (a->type > b->type) {
192 } else if (a->id < b->id) {
194 } else if (a->id > b->id) {
201 /* Container for registered resources */
202 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
203 struct fcb /* cust_root_t */, \
204 struct resource/* cust_item_t */, \
205 struct res_key /* cust_key_t */, \
206 resources /* cust_root_node */, \
207 node /* cust_item_node */, \
208 key /* cust_item_key */, \
209 res_key_cmp /* cust_cmp_fnc */);
211 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
212 struct fcb /* cust_root_t */, \
213 struct resource/* cust_item_t */, \
214 struct res_key /* cust_key_t */, \
215 resources /* cust_root_node */, \
216 node /* cust_item_node */, \
217 key /* cust_item_key */, \
218 res_key_cmp /* cust_cmp_fnc */);
220 /* Container for allocators registered for a given resource */
221 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
222 struct resource /* cust_root_t */, \
223 struct res_alloc /* cust_item_t */, \
224 forb_server_id /* cust_key_t */, \
225 allocators /* cust_root_node */, \
226 node /* cust_item_node */, \
227 app /* cust_item_key */, \
228 fres_contract_id_cmp /* cust_cmp_fnc */);
230 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
231 struct resource /* cust_root_t */, \
232 struct res_alloc /* cust_item_t */, \
233 forb_server_id /* cust_key_t */, \
234 allocators /* cust_root_node */, \
235 node /* cust_item_node */, \
236 app /* cust_item_key */, \
237 forb_server_id_cmp /* cust_cmp_fnc */);
239 /* Container for contracts with spare capacity negotiated for a given resource */
240 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
241 struct resource /* cust_head_t */, \
242 struct fcb_contract /* cust_item_t */, \
243 sc_contracts /* cust_head_field */, \
244 node_sc /* cust_node_field */);
246 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
247 struct reservation_list /* cust_head_t */, \
248 struct fcb_contract /* cust_item_t */, \
249 fcb_contracts /* cust_head_field */, \
250 node_reservation /* cust_node_field */);
252 /* Container for negotiated contracts */
253 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
254 struct fcb /* cust_root_t */, \
255 struct fcb_contract /* cust_item_t */, \
256 fres_contract_id_t /* cust_key_t */, \
257 contracts /* cust_root_node */, \
258 node_fcb /* cust_item_node */, \
259 id /* cust_item_key */, \
260 fres_contract_id_cmp /* cust_cmp_fnc */);
263 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
264 struct fcb /* cust_root_t */, \
265 struct fcb_contract /* cust_item_t */, \
266 fres_contract_id_t /* cust_key_t */, \
267 contracts /* cust_root_node */, \
268 node_fcb /* cust_item_node */, \
269 id /* cust_item_key */, \
270 fres_contract_id_cmp /* cust_cmp_fnc */);
272 #include "fcb_contract_gavl.inc"
276 gsa_array_field_t array;
279 GSA_CUST_DEC(res_array /* cust_prefix */,
280 struct res_array /* cust_array_t */,
281 struct resource /* cust_item_t */,
282 struct res_key /* cust_key_t */,
283 array /* cust_array_field */,
284 key /* cust_item_key */,
285 res_key_cmp /* cust_cmp_fnc */);
287 GSA_CUST_IMP(res_array /* cust_prefix */,
288 struct res_array /* cust_array_t */,
289 struct resource /* cust_item_t */,
290 struct res_key /* cust_key_t */,
291 array /* cust_array_field */,
292 key /* cust_item_key */,
293 res_key_cmp /* cust_cmp_fnc */,
294 true /* cust_ins_fl */);
298 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
300 static struct res_key*
301 get_res_key(const struct fres_contract *contract, struct res_key *key)
303 fres_block_resource *block_res;
305 block_res = fres_contract_get_resource(contract);
307 ul_logerr("No resource specified\n");
310 key->type = block_res->resource_type;
311 key->id = block_res->resource_id;
316 static struct res_key*
317 get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
319 if (fc->user_contract)
320 get_res_key(fc->user_contract, key);
322 get_res_key(fc->requested_contract, key);
327 * Fills in an array of fcb_contracts according to requests submited
328 * by an application through negotiate_contracts() or
329 * negotiate_transaction(). For every contract in @a contracts, there
330 * is allocated one fcb_contract in @a fcb_contracts array.
333 * @param fcb_contracts Where to store pointers to fcb_contracts
334 * @param contracts Contracts submited for negotiation
335 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
337 * @return Zero on success, non-zero error code on error.
340 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
341 struct fres_contract *contracts[], int num)
344 struct fcb_contract *fc;
346 for (i=0; i<num; i++) {
347 struct fres_contract *c = contracts[i];
349 if (fres_contract_id_is_empty(&c->id)) {
350 /* Normal negotiation request */
351 COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
352 wrong_size_of_contract_id);
353 c->id = ++fcb->contract_counter;
354 fc = fcb_contract_new(&c->id);
357 fcb_contracts[i] = fc;
358 log_contract("Negotiation request", i, c);
360 fc = fcb_contract_find(fcb, &c->id);
363 fres_contract_id_to_string(str, &c->id, sizeof(str));
364 ul_logerr("Attempt to change unknown contract %s\n", str);
365 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
367 fcb_contracts[i] = fc;
368 if (fres_contract_get_num_blocks(c) == 0) {
370 log_contract("Cancelation request", i, fc->user_contract);
373 log_contract("Renegotiation request", i, fc->user_contract);
377 fc->requested_contract = c;
383 * Ensure that all contracts belong to the same resource and find
384 * resource allocators for the contracts.
387 * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
388 * @param num Number of contracts in @a fcb_contracts
389 * @param resource Resource for which negotiation is being done.
390 * @param app Application which requests negotiation
393 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
394 struct resource **resource,
399 struct res_alloc *ra;
400 struct res_key key, key2 = {-1,-1};
402 for (i=0; i<num; i++) {
403 struct fcb_contract *fc = fcb_contracts[i];
405 if (!get_fc_res_key(fc, &key))
406 return FRSH_ERR_RESOURCE_ID_INVALID;
408 /* Check that all contracts are for the same resource */
411 *resource = fcb_resource_find(fcb, &key);
413 ul_logerr("No resource manager for %d.%d registered\n",
415 return FRES_ERR_NO_RESOURCE_MANAGER;
417 /* Find allocator for this request */
418 ra = fcb_alloc_find(*resource, app);
421 forb_server_id_to_string(str, app, sizeof(str));
422 ul_logerr("No resource allocator found for %d.%d and %s\n",
423 (*resource)->key.type, (*resource)->key.id, str);
424 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
427 else if (key.type != key2.type ||
429 ul_logerr("Contract #%d differs in resource!\n", i);
430 return FRSH_ERR_RESOURCE_ID_INVALID;
438 * Prepares a list of contracts to pass to resource manager for (re)reserving.
440 * @todo Needs to be changed for compatibility with transactions.
442 * @param resource Resource for which to rebalance capacity and negotiate new contracts
443 * @param fcb_contract New requests to negotiate
444 * @param num The number of elements in @a fcb_contract
445 * @param[out] rl List with changed contracts to be commited
448 prepare_reservation_list(struct resource *resource,
449 struct fcb_contract *fcb_contract[], int num)
453 struct fcb_contract *fc;
455 reservation_list_init_head(&resource->rl);
456 resource->rl.length = 0;
457 for (i=0; i<num; i++) {
458 assert(fcb_contract[i]->requested_contract != NULL);
459 reservation_list_insert(&resource->rl, fcb_contract[i]);
460 resource->rl.length++;
462 fosa_clock_get_time(CLOCK_REALTIME, &now);
463 ul_list_for_each(sc_contracts, resource, fc) {
464 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
465 fc->requested_contract == NULL) /* Do not insert contract inserted above */
467 reservation_list_insert(&resource->rl, fc);
468 resource->rl.length++;
474 reserve_resource(struct resource *resource)
477 fres_contract_ptr_seq contracts;
478 struct fres_contract *c;
479 struct fcb_contract *fc;
480 CORBA_Environment ev;
482 if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
486 forb_sequence_length(&contracts) = resource->rl.length;
489 /* Prepare FORB sequence */
490 ul_list_for_each(reservation_list, &resource->rl, fc) {
492 if (fc->to_be_reserved_contract)
493 /* Contract without spare capacity */
494 c = fc->to_be_reserved_contract;
495 else if (fc->requested_contract)
496 c = fc->requested_contract;
498 forb_sequence_elem(&contracts, i++) = c;
501 /* Reserve contract */
502 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
503 if (forb_exception_occurred(&ev)) {
504 ret = fres_forbex2err(&ev);
505 ul_logerr("FORB exception when reserving contracts\n");
509 ul_logerr("Contract reservation error %d\n", ret);
510 ret = FRES_ERR_ADMISSION_TEST;
515 ret = FRSH_ERR_CONTRACT_REJECTED;
519 forb_sequence_free_buf(&contracts, forb_no_destructor);
527 * @param resource Resource for which to rebalance capacity and negotiate new contracts
528 * @param rl List with changed contracts to be commited
530 * @return Zero on success, non-zero error code on error
533 rebalance_spare_capacity_and_reserve(struct resource *resource)
536 struct reservation_list *rl = &resource->rl;
537 struct fcb_contract *fc;
538 fres_block_spare_capacity *s;
540 /* Initialize optimization */
541 ul_list_for_each(reservation_list, rl, fc) {
542 fc->to_be_reserved_contract =
543 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
544 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
546 assert(fc->to_be_reserved_contract != NULL);
548 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
549 if (s && s->granularity == FRSH_GR_DISCRETE) {
550 fc->sc_variant.initial = s->variants._length - 1;
551 fc->sc_variant.try = fc->sc_variant.initial;
555 bool all_combinations_tried;
556 int criterion, best_criterion = -1;
557 struct fcb_contract *fcb_changed;
558 /* Exhaustive search. Try all combinations of spare capacity
559 * variants and find the one with best creterion. */
561 all_combinations_tried = true;
564 /* Prepare spare capacity variant */
565 ul_list_for_each(reservation_list, rl, fc) {
566 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
567 /* TODO: Simulate continuous granularity by discretization */
568 if (s && s->granularity == FRSH_GR_DISCRETE) {
569 fres_container_copy(fc->to_be_reserved_contract->container,
570 forb_sequence_elem(&s->variants, fc->sc_variant.try));
571 criterion += fc->sc_variant.try;
573 if (fcb_changed == NULL) {
574 /* Chnage tried variant for the next round */
575 fc->sc_variant.try = fc->sc_variant.try > 0 ?
576 fc->sc_variant.try - 1 :
577 s->variants._length - 1;
578 /* Do not change other
579 * contracts unless we have
580 * tried all variants */
581 if (fc->sc_variant.try != fc->sc_variant.initial) {
585 if (fc->sc_variant.try != fc->sc_variant.initial)
586 all_combinations_tried = false;
590 if (criterion > best_criterion) {
591 ret = reserve_resource(resource);
594 best_criterion = criterion;
596 case FRSH_ERR_CONTRACT_REJECTED:
602 } while (!all_combinations_tried);
604 if (best_criterion == -1) {
605 ret = FRSH_ERR_CONTRACT_REJECTED;
607 /* At least some variant succeeded */
609 sc_contracts_init_head(resource);
610 ul_list_for_each(reservation_list, rl, fc) {
611 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
612 if (s && s->granularity == FRSH_GR_DISCRETE) {
613 sc_contracts_insert(resource, fc);
618 ul_list_for_each(reservation_list, rl, fc) {
619 fres_contract_destroy(fc->to_be_reserved_contract);
620 fc->to_be_reserved_contract = NULL;
626 * Create/change VReses according to @a schedulable_contracts.
628 * There might be more allocators for schedulable contracts, so merge
629 * adjacent vreses with the same allocator together and create/change
630 * them in one step. Also the order of schedulable contracts might be
631 * different from the initial order od ids in commit_contracts()
632 * therefore we have to search for every contract.
635 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
637 struct res_alloc *last_ra = NULL;
638 fres_contract_ptr_seq vreses;
640 CORBA_Environment ev;
642 if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
647 for (i=0; i<schedulable_contracts->_length; i++) {
648 struct fcb_contract *fc;
649 struct fres_contract *sc = schedulable_contracts->_buffer[i];
651 log_contract("Changing VRES", i, sc);
653 fc = fcb_contract_find(fcb, &sc->id);
656 if (true /* TODO: if the schedulable contract is changed */) {
657 if (last_ra != fc->ra) {
658 if (vreses._length) {
659 ret = fres_resource_allocator_change_vreses(last_ra->ra,
661 if (forb_exception_occurred(&ev)) {
662 ret = fres_forbex2err(&ev);
669 vreses._buffer[vreses._length] = sc;
671 fres_contract_destroy(fc->schedulable_contract);
672 fc->schedulable_contract = fres_contract_duplicate(sc);
678 ret = fres_resource_allocator_change_vreses(last_ra->ra,
680 if (forb_exception_occurred(&ev))
681 ret = fres_forbex2err(&ev);
684 forb_sequence_free_buf(&vreses, forb_no_destructor);
689 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
692 struct fcb_contract *fc;
693 for (i=0; i<num; i++) {
694 fc = fcb_contracts[i];
695 if (fc && !fc->user_contract) {
696 fc->requested_contract = NULL; /* Destroyed by FORB */
697 fcb_contract_destroy(fc);
704 commit_resource(struct resource *resource,
705 fres_contract_ptr_seq **schedulable_contracts)
708 fres_contract_id_seq commit_ids;
710 struct fcb_contract *fc;
711 CORBA_Environment ev;
713 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
718 commit_ids._length = resource->rl.length;
720 ul_list_for_each(reservation_list, &resource->rl, fc) {
721 forb_sequence_elem(&commit_ids, i) = fc->id;
725 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
726 schedulable_contracts, &ev);
727 if (forb_exception_occurred(&ev)) {
728 ret = fres_forbex2err(&ev);
733 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
738 int cancel_reservations(struct resource *resource)
741 fres_contract_id_seq commit_ids;
743 struct fcb_contract *fc;
744 CORBA_Environment ev;
746 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
751 commit_ids._length = resource->rl.length;
753 ul_list_for_each(reservation_list, &resource->rl, fc) {
754 forb_sequence_elem(&commit_ids, i) = fc->id;
758 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
759 if (forb_exception_occurred(&ev)) {
760 ret = fres_forbex2err(&ev);
766 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
771 /* Add new contracts to our fcb database for later
772 * reference. Canceled contracts are removed below. */
774 fcb_remember_contracts(struct fcb *fcb,
775 struct fcb_contract **fcb_contracts,
778 struct fcb_contract *fc;
781 for (i=0; i<num; i++) {
782 fc = fcb_contracts[i];
784 if (fc->user_contract) {
785 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
787 fres_contract_destroy(fc->user_contract);
788 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
789 /* Note: requested_contract is also
790 * pointed by contracts parameter and
791 * will be freed by FORB. */
792 fc->requested_contract = NULL;
795 /* Insert new contracts */
796 fcb_contract_insert(fcb, fcb_contracts[i]);
797 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
798 fc->requested_contract = NULL;
799 /* See the note above. */
806 negotiate_contracts(fres_contract_broker obj,
807 const fres_contract_ptr_seq* contracts,
808 fres_contract_id_seq** ids_out,
809 CORBA_Environment *ev)
811 struct fcb *fcb = o2fcb(obj);
812 struct resource *resource = NULL;
815 fres_contract_ptr_seq *schedulable_contracts;
816 struct fcb_contract **fcb_contracts;
818 int num = contracts->_length;
820 /* Prepare output sequence for the case we return eariler with
822 forb_sequence_alloc(*ids_out, 0);
824 ev->major = FORB_EX_NO_MEMORY;
827 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
829 forb_get_req_source(obj, &app);
831 fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
832 if (!fcb_contracts) {
836 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
838 ret = prepare_fcb_contracts(fcb, fcb_contracts,
839 contracts->_buffer, num);
841 goto err_free_fcb_contracts;
842 ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
845 goto err_free_fcb_contracts;
847 prepare_reservation_list(resource, fcb_contracts, num);
849 /* Reserve contracts */
850 ret = rebalance_spare_capacity_and_reserve(resource);
852 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
853 ul_logmsg("Contract(s) was/were rejected\n");
856 fres_strerror(ret, msg, sizeof(msg));
857 ul_logerr("Reservation error: %s\n", msg);
859 goto err_free_fcb_contracts;
862 /* Commit contracts */
863 ret = commit_resource(resource, &schedulable_contracts);
865 goto err_cancel_reservation;
867 fcb_remember_contracts(fcb, fcb_contracts, num);
869 ret = change_vreses(fcb, schedulable_contracts);
871 goto err_cancel_reservation;
873 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
875 ul_logerr("VRes was not created\n");
876 goto err_cancel_contracts;
880 /* Return IDs and delete canceled contracts from out database */
881 if (!forb_sequence_alloc_buf(*ids_out, num)) {
882 ev->major = FORB_EX_NO_MEMORY;
883 goto err_cancel_contracts;
885 (*ids_out)->_length = num;
886 for (i=0; i<num; i++) {
887 struct fcb_contract *fc = fcb_contracts[i];
888 forb_sequence_elem(*ids_out, i) = fc->id;
890 if (fc->requested_contract &&
891 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
892 fcb_contract_delete(fcb, fc);
893 /* Note: requested_contract is also pointed by
894 * contracts parameter and will be freed by FORB. */
895 fc->requested_contract = NULL;
896 fcb_contract_destroy(fc);
901 err_cancel_contracts:
903 goto err_free_fcb_contracts;
904 err_cancel_reservation:
905 cancel_reservations(resource);
906 err_free_fcb_contracts:
907 free_fcb_contracts(fcb_contracts, num);
912 void redistribute_spare_capacity(fres_contract_broker obj,
913 const frsh_resource_type_t restype,
914 const frsh_resource_id_t resid,
915 CORBA_Environment *ev)
917 struct fcb *fcb = o2fcb(obj);
918 struct res_key key = {restype, resid };
919 struct resource *resource;
921 resource = fcb_resource_find(fcb, &key);
923 prepare_reservation_list(resource, NULL, 0);
925 /* forb_sequence_alloc(ids, rl.length); */
926 /* if (!ids || !ids->_buffer) { */
927 /* ev->major = FORB_EX_NO_MEMORY; */
928 /* goto err_free_fcb_contracts; */
930 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
931 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
934 rebalance_spare_capacity_and_reserve(resource);
938 CORBA_long register_resource(fres_contract_broker obj,
939 const frsh_resource_type_t restype,
940 const frsh_resource_id_t resid,
941 const fres_resource_desc *desc,
942 CORBA_Environment *ev)
944 struct fcb *fcb = o2fcb(obj);
945 struct resource *res, *res2;
947 res = malloc(sizeof(*res));
949 memset(res, 0, sizeof(*res));
950 res->key.type = restype;
952 res2 = fcb_resource_find(fcb, &res->key);
954 if (forb_object_is_stale(res2->mng)) {
955 ul_logmsg("Removing stale manager for resource %d.%d\n",
957 forb_object_release(res2->mng);
958 fcb_resource_delete(fcb, res2);
959 /* TODO: Delete also all allocators associated
960 * with this stale resource manager. */
964 ul_logerr("Resource manager %d.%d already registered\n",
969 res->mng = forb_object_duplicate(desc->manager);
970 res->name = desc->name;
972 fcb_alloc_init_root_field(res);
973 sc_contracts_init_head(res);
974 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
975 res->name, restype, resid);
976 fcb_resource_insert(fcb, res);
985 CORBA_long register_allocator(fres_contract_broker obj,
986 const frsh_resource_type_t restype,
987 const frsh_resource_id_t resid,
988 const fres_resource_allocator ra_obj,
989 CORBA_Environment *ev)
991 struct fcb *fcb = o2fcb(obj);
992 struct resource *res;
993 struct res_alloc *ra;
994 struct res_key resource;
995 forb_server_id server_id;
996 char server_id_str[40];
999 forb_get_server_id(ra_obj, &server_id);
1000 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1001 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
1002 restype, resid, server_id_str);
1004 resource.type = restype;
1005 resource.id = resid;
1006 res = fcb_resource_find(fcb, &resource);
1008 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
1010 ret = FRES_ERR_NO_RESOURCE_MANAGER;
1013 ra = fcb_alloc_find(res, &server_id);
1015 char *str = forb_object_to_string(ra_obj);
1016 ul_logerr("Allocator from already registered (%s)\n",
1019 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1022 ra = malloc(sizeof(*ra));
1027 ra->app = server_id;
1028 ra->ra = forb_object_duplicate(ra_obj);
1029 fcb_alloc_insert(res, ra);
1035 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1037 struct fcb *fcb = o2fcb(obj);
1038 fres_resource_seq *seq;
1039 struct resource *res;
1042 seq = malloc(sizeof(*seq));
1044 ev->major = FORB_EX_NO_MEMORY;
1047 memset(seq, 0, sizeof(*seq));
1050 gavl_cust_for_each(fcb_resource, fcb, res) {
1054 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1059 gavl_cust_for_each(fcb_resource, fcb, res) {
1060 seq->_buffer[n].restype = res->key.type;
1061 seq->_buffer[n].resid = res->key.id;
1062 seq->_buffer[n].desc.manager = res->mng;
1063 seq->_buffer[n].desc.name = res->name;
1071 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1073 struct fres_contract **c;
1074 forb_sequence_foreach(&transaction->contracts, c)
1075 if (fres_contract_get_spare_capacity(*c))
1081 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1082 int num, struct res_array *res_array)
1085 struct resource *resource;
1089 for (i = 0; i < num; i++) {
1090 get_fc_res_key(fc[i], &key);
1091 resource = fcb_resource_find(fcb, &key);
1092 ret = res_array_insert(res_array, resource);
1094 ret = FRSH_ERR_INTERNAL_ERROR;
1104 negotiate_transaction(fres_contract_broker _obj,
1105 const fres_transaction_t* transaction,
1106 CORBA_Environment *ev)
1108 struct fcb *fcb = o2fcb(_obj);
1109 struct fcb_contract **fcb_contracts, *fc;
1110 const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1111 int i, ret, num = user_contracts->_length;
1112 struct res_array res_array;
1114 struct resource *resource;
1115 fres_contract_ptr_seq *schedulable_contracts;
1117 ul_logmsg("Negotiating transaction with %d contracts\n", num);
1118 if (transaction_has_spare_capacity(transaction)) {
1119 ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1123 fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1124 if (!fcb_contracts) {
1128 memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1130 ret = prepare_fcb_contracts(fcb, fcb_contracts,
1131 user_contracts->_buffer, num);
1133 goto err_free_fcb_contracts;
1135 res_array_init_array_field(&res_array);
1136 ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1138 gsa_cust_for_each(res_array, &res_array, resource) {
1139 reservation_list_init_head(&resource->rl);
1140 resource->rl.length = 0;
1141 for (i = 0; i < num; i++) {
1142 fc = fcb_contracts[i];
1143 get_fc_res_key(fc, &key);
1144 if (res_key_cmp(&key, &resource->key) == 0) {
1145 reservation_list_insert(&resource->rl, fc);
1146 resource->rl.length++;
1150 gsa_cust_for_each(res_array, &res_array, resource) {
1151 ret = reserve_resource(resource);
1153 ul_logerr("Reservation failed\n");
1154 goto err_cancel_reservation;
1157 gsa_cust_for_each(res_array, &res_array, resource) {
1158 ret = commit_resource(resource, &schedulable_contracts);
1160 ul_logerr("Commit failed\n");
1161 goto err_cancel_reservation;
1164 fcb_remember_contracts(fcb, fcb_contracts, num);
1165 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
1167 res_array_delete_all(&res_array);
1169 err_cancel_reservation:
1170 gsa_cust_for_each(res_array, &res_array, resource) {
1171 cancel_reservations(resource);
1173 res_array_delete_all(&res_array);
1174 err_free_fcb_contracts:
1175 free_fcb_contracts(fcb_contracts, num);
1181 wait_transaction(fres_contract_broker _obj,
1182 const CORBA_char * name,
1183 fres_transaction_t** transaction,
1184 CORBA_Environment *ev)
1186 return FRSH_ERR_NOT_IMPLEMENTED;
1190 allocate_transaction_vres(fres_contract_broker _obj,
1191 const CORBA_long id,
1192 CORBA_Environment *ev)
1194 return FRSH_ERR_NOT_IMPLEMENTED;
1198 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1199 static int register_inet_port(forb_orb orb)
1201 forb_port_t *port = forb_malloc(sizeof(*port));
1203 struct in_addr listen_on;
1207 memset(port, 0, sizeof(*port));
1208 listen_on.s_addr = INADDR_ANY;
1209 ret = forb_inet_port_init(&port->desc, listen_on, 0);
1212 ret = forb_register_port(orb, port);
1217 struct forb_fres_contract_broker_impl impl = {
1218 .negotiate_contracts = negotiate_contracts,
1219 .register_resource = register_resource,
1220 .register_allocator = register_allocator,
1221 .redistribute_spare_capacity = redistribute_spare_capacity,
1222 .get_resources = get_resources,
1223 .negotiate_transaction = negotiate_transaction,
1224 .wait_transaction = wait_transaction,
1225 .allocate_transaction_vres = allocate_transaction_vres,
1228 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1230 forb_server_id server_id;
1231 char server_id_str[sizeof(forb_server_id)*2+1];
1233 forb_get_server_id(peer_orb, &server_id);
1234 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1236 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1242 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1243 fosa_abs_time_t now;
1244 fosa_rel_time_t delay;
1245 fosa_clock_get_time(CLOCK_REALTIME, &now);
1246 delay = fosa_abs_time_extract_interval(start_time, now);
1247 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1248 fosa_rel_time_to_msec(delay));
1252 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1256 static struct option long_opts[] = {
1257 { "daemon", optional_argument, NULL, 'd' },
1258 { "loglevel", required_argument, NULL, 'l' },
1265 printf("usage: fcb [ options ]\n");
1266 printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
1267 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
1270 int print_log_domain(ul_log_domain_t *domain, void *context)
1272 printf("%s = %d\n", domain->name, domain->level);
1276 int main(int argc, char *argv[])
1279 struct fcb fcb_data;
1280 fres_contract_broker fcb;
1281 forb_executor_t executor;
1283 forb_init_attr_t attr = {
1284 .orb_id = "org.frescor.fcb",
1285 .peer_discovery_callback = peer_discovery_callback,
1286 .peer_dead_callback = peer_dead_callback,
1287 .fixed_tcp_port = FCB_TCP_PORT,
1288 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
1289 .fixed_server_id = FCB_SERVER_ID,
1290 .redistribute_hellos = true,
1295 while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1298 if (*optarg == '?') {
1299 ul_logreg_for_each_domain(print_log_domain, NULL);
1304 ret = ul_log_domain_arg2levels(optarg);
1306 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1311 opt_pidfile = optarg;
1316 exit(opt == 'h' ? 0 : 1);
1320 memset(&fcb_data, 0, sizeof(fcb_data));
1321 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1324 forb_daemon_prepare(opt_pidfile);
1326 orb = forb_init(&argc, &argv, &attr);
1327 if (!orb) error(1, errno, "FORB initialization failed");
1329 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1330 ret = register_inet_port(orb);
1331 if (ret) error(0, errno, "INET port registration failed");
1334 fcb_resource_init_root_field(&fcb_data);
1335 fcb_contract_init_root_field(&fcb_data);
1337 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1338 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1340 /* Prepare executor before we register the fcb reference,
1341 * so that no reuqests are lost */
1342 ret = forb_executor_init(&executor);
1343 if (ret) error(1, errno, "forb_executor_init failed");
1345 ret = forb_executor_register_object(&executor, fcb);
1346 if (ret) error(1, errno, "forb_executor_register_object failed");
1348 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1349 if (ret) error(1, errno, "forb_register_reference() failed");
1351 ul_logmsg("Waiting for requests\n");
1353 forb_daemon_ready();
1355 ret = forb_executor_run(&executor);
1356 if (ret) error(1, errno, "forb_executor_run failed");