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_transaction;
130 struct fcb_contract {
131 fres_contract_id_t id;
132 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
133 gavl_node_t node_fcb;
134 ul_list_node_t node_sc;
135 ul_list_node_t node_reservation;
136 struct fcb_transaction *transaction;
141 fosa_abs_time_t end_of_stability_period;
142 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
143 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
144 struct fres_contract *to_be_reserved_contract;
145 struct fres_contract *schedulable_contract;
148 struct fcb_transaction {
151 gsa_array_field_t contracts;
155 * Contract broker data
158 fres_contract_id_t contract_counter;
159 gavl_cust_root_field_t resources; /**< Registered resources */
160 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
161 gavl_cust_root_field_t transactions; /**< Transactions negotiated by this FCB */
164 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
166 struct fcb_contract *fcb_contract;
168 fcb_contract = malloc(sizeof(*fcb_contract));
172 memset(fcb_contract, 0, sizeof(*fcb_contract));
173 fcb_contract->id = *id;
178 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
180 if (fcb_contract->user_contract) {
181 fres_contract_destroy(fcb_contract->user_contract);
183 if (fcb_contract->to_be_reserved_contract) {
184 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
186 if (fcb_contract->requested_contract) {
187 fres_contract_destroy(fcb_contract->requested_contract);
189 if (fcb_contract->schedulable_contract) {
190 fres_contract_destroy(fcb_contract->requested_contract);
196 unsorted(const void *a, const void *b)
199 /* Unordered dynamic array of contracts in transaction */
200 GSA_CUST_DEC(tran_contract /* cust_prefix */,
201 struct fcb_transaction /* cust_array_t */,
202 struct fcb_contract/* cust_item_t */,
203 fres_contract_id_t /* cust_key_t */,
204 contracts /* cust_array_field */,
205 id /* cust_item_key */,
206 unsorted /* cust_cmp_fnc */);
208 GSA_CUST_IMP(tran_contract /* cust_prefix */,
209 struct fcb_transaction /* cust_array_t */,
210 struct fcb_contract/* cust_item_t */,
211 fres_contract_id_t /* cust_key_t */,
212 contracts /* cust_array_field */,
213 id /* cust_item_key */,
214 unsorted /* cust_cmp_fnc */,
215 true /* cust_ins_fl */);
217 struct fcb_transaction *fcb_transaction_new(char *name)
219 struct fcb_transaction *fcb_transaction;
221 fcb_transaction = malloc(sizeof(*fcb_transaction));
222 if (!fcb_transaction) {
225 memset(fcb_transaction, 0, sizeof(*fcb_transaction));
226 fcb_transaction->name = strdup(name);
228 tran_contract_init_array_field(fcb_transaction);
230 return fcb_transaction;
233 void fcb_transaction_destroy(struct fcb_transaction *fcb_transaction)
235 free(fcb_transaction->name);
236 free(fcb_transaction);
239 static inline int res_key_cmp(const struct res_key *a,
240 const struct res_key *b)
242 if (a->type < b->type) {
244 } else if (a->type > b->type) {
246 } else if (a->id < b->id) {
248 } else if (a->id > b->id) {
255 /* Container for registered resources */
256 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
257 struct fcb /* cust_root_t */, \
258 struct resource/* cust_item_t */, \
259 struct res_key /* cust_key_t */, \
260 resources /* cust_root_node */, \
261 node /* cust_item_node */, \
262 key /* cust_item_key */, \
263 res_key_cmp /* cust_cmp_fnc */);
265 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
266 struct fcb /* cust_root_t */, \
267 struct resource/* cust_item_t */, \
268 struct res_key /* cust_key_t */, \
269 resources /* cust_root_node */, \
270 node /* cust_item_node */, \
271 key /* cust_item_key */, \
272 res_key_cmp /* cust_cmp_fnc */);
274 /* Container for allocators registered for a given resource */
275 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
276 struct resource /* cust_root_t */, \
277 struct res_alloc /* cust_item_t */, \
278 forb_server_id /* cust_key_t */, \
279 allocators /* cust_root_node */, \
280 node /* cust_item_node */, \
281 app /* cust_item_key */, \
282 fres_contract_id_cmp /* cust_cmp_fnc */);
284 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
285 struct resource /* cust_root_t */, \
286 struct res_alloc /* cust_item_t */, \
287 forb_server_id /* cust_key_t */, \
288 allocators /* cust_root_node */, \
289 node /* cust_item_node */, \
290 app /* cust_item_key */, \
291 forb_server_id_cmp /* cust_cmp_fnc */);
293 /* Container for contracts with spare capacity negotiated for a given resource */
294 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
295 struct resource /* cust_head_t */, \
296 struct fcb_contract /* cust_item_t */, \
297 sc_contracts /* cust_head_field */, \
298 node_sc /* cust_node_field */);
300 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
301 struct reservation_list /* cust_head_t */, \
302 struct fcb_contract /* cust_item_t */, \
303 fcb_contracts /* cust_head_field */, \
304 node_reservation /* cust_node_field */);
306 /* Container for negotiated contracts */
307 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
308 struct fcb /* cust_root_t */, \
309 struct fcb_contract /* cust_item_t */, \
310 fres_contract_id_t /* cust_key_t */, \
311 contracts /* cust_root_node */, \
312 node_fcb /* cust_item_node */, \
313 id /* cust_item_key */, \
314 fres_contract_id_cmp /* cust_cmp_fnc */);
317 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
318 struct fcb /* cust_root_t */, \
319 struct fcb_contract /* cust_item_t */, \
320 fres_contract_id_t /* cust_key_t */, \
321 contracts /* cust_root_node */, \
322 node_fcb /* cust_item_node */, \
323 id /* cust_item_key */, \
324 fres_contract_id_cmp /* cust_cmp_fnc */);
326 #include "fcb_contract_gavl.inc"
327 #include <forb/forb.h>
331 fcb_transaction_cmp(char * const *a, char * const *b)
332 { return strcmp(*a, *b); }
334 GAVL_CUST_NODE_INT_DEC(fcb_transaction /* cust_prefix */, \
335 struct fcb /* cust_root_t */, \
336 struct fcb_transaction /* cust_item_t */, \
337 char* /* cust_key_t */, \
338 transactions /* cust_root_node */, \
339 node /* cust_item_node */, \
340 name /* cust_item_key */, \
341 fcb_transaction_cmp /* cust_cmp_fnc */);
343 GAVL_CUST_NODE_INT_IMP(fcb_transaction /* cust_prefix */, \
344 struct fcb /* cust_root_t */, \
345 struct fcb_transaction /* cust_item_t */, \
346 char* /* cust_key_t */, \
347 transactions /* cust_root_node */, \
348 node /* cust_item_node */, \
349 name /* cust_item_key */, \
350 fcb_transaction_cmp /* cust_cmp_fnc */);
353 gsa_array_field_t array;
356 GSA_CUST_DEC(res_array /* cust_prefix */,
357 struct res_array /* cust_array_t */,
358 struct resource /* cust_item_t */,
359 struct res_key /* cust_key_t */,
360 array /* cust_array_field */,
361 key /* cust_item_key */,
362 res_key_cmp /* cust_cmp_fnc */);
364 GSA_CUST_IMP(res_array /* cust_prefix */,
365 struct res_array /* cust_array_t */,
366 struct resource /* cust_item_t */,
367 struct res_key /* cust_key_t */,
368 array /* cust_array_field */,
369 key /* cust_item_key */,
370 res_key_cmp /* cust_cmp_fnc */,
371 true /* cust_ins_fl */);
375 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
377 static struct res_key*
378 get_res_key(const struct fres_contract *contract, struct res_key *key)
380 fres_block_resource *block_res;
382 block_res = fres_contract_get_resource(contract);
384 ul_logerr("No resource specified\n");
387 key->type = block_res->resource_type;
388 key->id = block_res->resource_id;
393 static struct res_key*
394 get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
396 if (fc->user_contract)
397 get_res_key(fc->user_contract, key);
399 get_res_key(fc->requested_contract, key);
404 * Fills in an array of fcb_contracts according to requests submited
405 * by an application through negotiate_contracts() or
406 * negotiate_transaction(). For every contract in @a contracts, there
407 * is allocated one fcb_contract in @a fcb_contracts array.
410 * @param fcb_contracts Where to store pointers to fcb_contracts
411 * @param contracts Contracts submited for negotiation
412 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
414 * @return Zero on success, non-zero error code on error.
417 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
418 struct fres_contract *contracts[], int num)
421 struct fcb_contract *fc;
423 for (i=0; i<num; i++) {
424 struct fres_contract *c = contracts[i];
426 if (fres_contract_id_is_empty(&c->id)) {
427 /* Normal negotiation request */
428 COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
429 wrong_size_of_contract_id);
430 c->id = ++fcb->contract_counter;
431 fc = fcb_contract_new(&c->id);
434 fcb_contracts[i] = fc;
435 log_contract("Negotiation request", i, c);
437 fc = fcb_contract_find(fcb, &c->id);
440 fres_contract_id_to_string(str, &c->id, sizeof(str));
441 ul_logerr("Attempt to change unknown contract %s\n", str);
442 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
444 fcb_contracts[i] = fc;
445 if (fres_contract_get_num_blocks(c) == 0) {
447 log_contract("Cancelation request", i, fc->user_contract);
450 log_contract("Renegotiation request", i, fc->user_contract);
454 fc->requested_contract = c;
460 * Ensure that all contracts belong to the same resource and find
461 * resource allocators for the contracts.
464 * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
465 * @param num Number of contracts in @a fcb_contracts
466 * @param resource Resource for which negotiation is being done.
467 * @param app Application which requests negotiation
470 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
471 struct resource **resource,
476 struct res_alloc *ra;
477 struct res_key key, key2 = {-1,-1};
479 for (i=0; i<num; i++) {
480 struct fcb_contract *fc = fcb_contracts[i];
482 if (!get_fc_res_key(fc, &key))
483 return FRSH_ERR_RESOURCE_ID_INVALID;
485 /* Check that all contracts are for the same resource */
488 *resource = fcb_resource_find(fcb, &key);
490 ul_logerr("No resource manager for %d.%d registered\n",
492 return FRES_ERR_NO_RESOURCE_MANAGER;
494 /* Find allocator for this request */
495 ra = fcb_alloc_find(*resource, app);
498 forb_server_id_to_string(str, app, sizeof(str));
499 ul_logerr("No resource allocator found for %d.%d and %s\n",
500 (*resource)->key.type, (*resource)->key.id, str);
501 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
504 else if (key.type != key2.type ||
506 ul_logerr("Contract #%d differs in resource!\n", i);
507 return FRSH_ERR_RESOURCE_ID_INVALID;
515 * Prepares a list of contracts to pass to resource manager for (re)reserving.
517 * @todo Needs to be changed for compatibility with transactions.
519 * @param resource Resource for which to rebalance capacity and negotiate new contracts
520 * @param fcb_contract New requests to negotiate
521 * @param num The number of elements in @a fcb_contract
522 * @param[out] rl List with changed contracts to be commited
525 prepare_reservation_list(struct resource *resource,
526 struct fcb_contract *fcb_contract[], int num)
530 struct fcb_contract *fc;
532 reservation_list_init_head(&resource->rl);
533 resource->rl.length = 0;
534 for (i=0; i<num; i++) {
535 assert(fcb_contract[i]->requested_contract != NULL);
536 reservation_list_insert(&resource->rl, fcb_contract[i]);
537 resource->rl.length++;
539 fosa_clock_get_time(CLOCK_REALTIME, &now);
540 ul_list_for_each(sc_contracts, resource, fc) {
541 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
542 fc->requested_contract == NULL) /* Do not insert contract inserted above */
544 reservation_list_insert(&resource->rl, fc);
545 resource->rl.length++;
551 reserve_resource(struct resource *resource)
554 fres_contract_ptr_seq contracts;
555 struct fres_contract *c;
556 struct fcb_contract *fc;
557 CORBA_Environment ev;
559 if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
563 forb_sequence_length(&contracts) = resource->rl.length;
566 /* Prepare FORB sequence */
567 ul_list_for_each(reservation_list, &resource->rl, fc) {
569 if (fc->to_be_reserved_contract)
570 /* Contract without spare capacity */
571 c = fc->to_be_reserved_contract;
572 else if (fc->requested_contract)
573 c = fc->requested_contract;
575 forb_sequence_elem(&contracts, i++) = c;
578 /* Reserve contract */
579 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
580 if (forb_exception_occurred(&ev)) {
581 ret = fres_forbex2err(&ev);
582 ul_logerr("FORB exception when reserving contracts\n");
586 ul_logerr("Contract reservation error %d\n", ret);
587 ret = FRES_ERR_ADMISSION_TEST;
592 ret = FRSH_ERR_CONTRACT_REJECTED;
596 forb_sequence_free_buf(&contracts, forb_no_destructor);
604 * @param resource Resource for which to rebalance capacity and negotiate new contracts
605 * @param rl List with changed contracts to be commited
607 * @return Zero on success, non-zero error code on error
610 rebalance_spare_capacity_and_reserve(struct resource *resource)
613 struct reservation_list *rl = &resource->rl;
614 struct fcb_contract *fc;
615 fres_block_spare_capacity *s;
617 /* Initialize optimization */
618 ul_list_for_each(reservation_list, rl, fc) {
619 fc->to_be_reserved_contract =
620 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
621 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
623 assert(fc->to_be_reserved_contract != NULL);
625 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
626 if (s && s->granularity == FRSH_GR_DISCRETE) {
627 fc->sc_variant.initial = s->variants._length - 1;
628 fc->sc_variant.try = fc->sc_variant.initial;
632 bool all_combinations_tried;
633 int criterion, best_criterion = -1;
634 struct fcb_contract *fcb_changed;
635 /* Exhaustive search. Try all combinations of spare capacity
636 * variants and find the one with best creterion. */
638 all_combinations_tried = true;
641 /* Prepare spare capacity variant */
642 ul_list_for_each(reservation_list, rl, fc) {
643 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
644 /* TODO: Simulate continuous granularity by discretization */
645 if (s && s->granularity == FRSH_GR_DISCRETE) {
646 fres_container_copy(fc->to_be_reserved_contract->container,
647 forb_sequence_elem(&s->variants, fc->sc_variant.try));
648 criterion += fc->sc_variant.try;
650 if (fcb_changed == NULL) {
651 /* Chnage tried variant for the next round */
652 fc->sc_variant.try = fc->sc_variant.try > 0 ?
653 fc->sc_variant.try - 1 :
654 s->variants._length - 1;
655 /* Do not change other
656 * contracts unless we have
657 * tried all variants */
658 if (fc->sc_variant.try != fc->sc_variant.initial) {
662 if (fc->sc_variant.try != fc->sc_variant.initial)
663 all_combinations_tried = false;
667 if (criterion > best_criterion) {
668 ret = reserve_resource(resource);
671 best_criterion = criterion;
673 case FRSH_ERR_CONTRACT_REJECTED:
679 } while (!all_combinations_tried);
681 if (best_criterion == -1) {
682 ret = FRSH_ERR_CONTRACT_REJECTED;
684 /* At least some variant succeeded */
686 sc_contracts_init_head(resource);
687 ul_list_for_each(reservation_list, rl, fc) {
688 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
689 if (s && s->granularity == FRSH_GR_DISCRETE) {
690 sc_contracts_insert(resource, fc);
695 ul_list_for_each(reservation_list, rl, fc) {
696 fres_contract_destroy(fc->to_be_reserved_contract);
697 fc->to_be_reserved_contract = NULL;
703 * Create/change VReses according to @a schedulable_contracts.
705 * There might be more allocators for schedulable contracts, so merge
706 * adjacent vreses with the same allocator together and create/change
707 * them in one step. Also the order of schedulable contracts might be
708 * different from the initial order od ids in commit_contracts()
709 * therefore we have to search for every contract.
712 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
714 struct res_alloc *last_ra = NULL;
715 fres_contract_ptr_seq vreses;
717 CORBA_Environment ev;
719 if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
724 for (i=0; i<schedulable_contracts->_length; i++) {
725 struct fcb_contract *fc;
726 struct fres_contract *sc = schedulable_contracts->_buffer[i];
728 log_contract("Changing VRES", i, sc);
730 fc = fcb_contract_find(fcb, &sc->id);
733 if (true /* TODO: if the schedulable contract is changed */) {
734 if (last_ra != fc->ra) {
735 if (vreses._length) {
736 ret = fres_resource_allocator_change_vreses(last_ra->ra,
738 if (forb_exception_occurred(&ev)) {
739 ret = fres_forbex2err(&ev);
746 vreses._buffer[vreses._length] = sc;
753 ret = fres_resource_allocator_change_vreses(last_ra->ra,
755 if (forb_exception_occurred(&ev))
756 ret = fres_forbex2err(&ev);
759 forb_sequence_free_buf(&vreses, forb_no_destructor);
764 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
767 struct fcb_contract *fc;
768 for (i=0; i<num; i++) {
769 fc = fcb_contracts[i];
770 if (fc && !fc->user_contract) {
771 fc->requested_contract = NULL; /* Destroyed by FORB */
772 fcb_contract_destroy(fc);
779 commit_resource(struct resource *resource,
780 fres_contract_ptr_seq **schedulable_contracts)
783 fres_contract_id_seq commit_ids;
785 struct fcb_contract *fc;
786 CORBA_Environment ev;
788 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
793 commit_ids._length = resource->rl.length;
795 ul_list_for_each(reservation_list, &resource->rl, fc) {
796 forb_sequence_elem(&commit_ids, i) = fc->id;
800 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
801 schedulable_contracts, &ev);
802 if (forb_exception_occurred(&ev)) {
803 ret = fres_forbex2err(&ev);
808 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
813 int cancel_reservations(struct resource *resource)
816 fres_contract_id_seq commit_ids;
818 struct fcb_contract *fc;
819 CORBA_Environment ev;
821 if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
826 commit_ids._length = resource->rl.length;
828 ul_list_for_each(reservation_list, &resource->rl, fc) {
829 forb_sequence_elem(&commit_ids, i) = fc->id;
833 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
834 if (forb_exception_occurred(&ev)) {
835 ret = fres_forbex2err(&ev);
841 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
846 /* Add new contracts to our fcb database for later
847 * reference. Canceled contracts are removed below. */
849 fcb_remember_contracts(struct fcb *fcb,
850 struct fcb_contract **fcb_contracts, int num)
852 struct fcb_contract *fc;
855 for (i=0; i<num; i++) {
856 fc = fcb_contracts[i];
858 if (fc->user_contract) {
859 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
861 fres_contract_destroy(fc->user_contract);
862 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
863 /* Note: requested_contract is also
864 * pointed by contracts parameter and
865 * will be freed by FORB. */
866 fc->requested_contract = NULL;
869 /* Insert new contracts */
870 fcb_contract_insert(fcb, fcb_contracts[i]);
871 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
872 fc->requested_contract = NULL;
873 /* See the note above. */
880 fcb_remember_schedulable_contracts(struct fcb *fcb,
881 fres_contract_ptr_seq *schedulable_contracts)
883 struct fcb_contract *fc;
885 for (i=0; i<schedulable_contracts->_length; i++) {
886 struct fres_contract *sc = schedulable_contracts->_buffer[i];
887 fc = fcb_contract_find(fcb, &sc->id);
889 fres_contract_destroy(fc->schedulable_contract);
890 fc->schedulable_contract = fres_contract_duplicate(sc);
891 assert(fc->schedulable_contract);
897 reject_transactions(struct fcb_contract *fcb_contracts[], int num)
900 for (i = 0; i < num; i++) {
901 if (fcb_contracts[i]->transaction)
902 return FRES_ERR_VRES_PART_OF_TRANSACTION;
908 negotiate_contracts(fres_contract_broker obj,
909 const fres_contract_ptr_seq* contracts,
910 fres_contract_id_seq** ids_out,
911 CORBA_Environment *ev)
913 struct fcb *fcb = o2fcb(obj);
914 struct resource *resource = NULL;
917 fres_contract_ptr_seq *schedulable_contracts;
918 struct fcb_contract **fcb_contracts;
920 int num = contracts->_length;
922 /* Prepare output sequence for the case we return eariler with
924 forb_sequence_alloc(*ids_out, 0);
926 ev->major = FORB_EX_NO_MEMORY;
929 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
931 forb_get_req_source(obj, &app);
933 fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
934 if (!fcb_contracts) {
938 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
940 ret = prepare_fcb_contracts(fcb, fcb_contracts,
941 contracts->_buffer, num);
943 goto err_free_fcb_contracts;
945 ret = reject_transactions(fcb_contracts, num);
947 goto err_free_fcb_contracts;
948 ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
951 goto err_free_fcb_contracts;
953 prepare_reservation_list(resource, fcb_contracts, num);
955 /* Reserve contracts */
956 ret = rebalance_spare_capacity_and_reserve(resource);
958 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
959 ul_logmsg("Contract(s) was/were rejected\n");
962 fres_strerror(ret, msg, sizeof(msg));
963 ul_logerr("Reservation error: %s\n", msg);
965 goto err_free_fcb_contracts;
968 /* Commit contracts */
969 ret = commit_resource(resource, &schedulable_contracts);
971 goto err_cancel_reservation;
973 fcb_remember_contracts(fcb, fcb_contracts, num);
974 fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
976 ret = change_vreses(fcb, schedulable_contracts);
978 goto err_cancel_reservation;
980 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
982 ul_logerr("VRes was not created\n");
983 goto err_cancel_contracts;
987 /* Return IDs and delete canceled contracts from out database */
988 if (!forb_sequence_alloc_buf(*ids_out, num)) {
989 ev->major = FORB_EX_NO_MEMORY;
990 goto err_cancel_contracts;
992 (*ids_out)->_length = num;
993 for (i=0; i<num; i++) {
994 struct fcb_contract *fc = fcb_contracts[i];
995 forb_sequence_elem(*ids_out, i) = fc->id;
997 if (fc->requested_contract &&
998 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
999 fcb_contract_delete(fcb, fc);
1000 /* Note: requested_contract is also pointed by
1001 * contracts parameter and will be freed by FORB. */
1002 fc->requested_contract = NULL;
1003 fcb_contract_destroy(fc);
1008 err_cancel_contracts:
1010 goto err_free_fcb_contracts;
1011 err_cancel_reservation:
1012 cancel_reservations(resource);
1013 err_free_fcb_contracts:
1014 free_fcb_contracts(fcb_contracts, num);
1019 void redistribute_spare_capacity(fres_contract_broker obj,
1020 const frsh_resource_type_t restype,
1021 const frsh_resource_id_t resid,
1022 CORBA_Environment *ev)
1024 struct fcb *fcb = o2fcb(obj);
1025 struct res_key key = {restype, resid };
1026 struct resource *resource;
1028 resource = fcb_resource_find(fcb, &key);
1030 prepare_reservation_list(resource, NULL, 0);
1032 /* forb_sequence_alloc(ids, rl.length); */
1033 /* if (!ids || !ids->_buffer) { */
1034 /* ev->major = FORB_EX_NO_MEMORY; */
1035 /* goto err_free_fcb_contracts; */
1037 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
1038 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
1041 rebalance_spare_capacity_and_reserve(resource);
1045 CORBA_long register_resource(fres_contract_broker obj,
1046 const frsh_resource_type_t restype,
1047 const frsh_resource_id_t resid,
1048 const fres_resource_desc *desc,
1049 CORBA_Environment *ev)
1051 struct fcb *fcb = o2fcb(obj);
1052 struct resource *res, *res2;
1054 res = malloc(sizeof(*res));
1056 memset(res, 0, sizeof(*res));
1057 res->key.type = restype;
1058 res->key.id = resid;
1059 res2 = fcb_resource_find(fcb, &res->key);
1061 if (forb_object_is_stale(res2->mng)) {
1062 ul_logmsg("Removing stale manager for resource %d.%d\n",
1064 forb_object_release(res2->mng);
1065 fcb_resource_delete(fcb, res2);
1066 /* TODO: Delete also all allocators associated
1067 * with this stale resource manager. */
1071 ul_logerr("Resource manager %d.%d already registered\n",
1076 res->mng = forb_object_duplicate(desc->manager);
1077 res->name = desc->name;
1079 fcb_alloc_init_root_field(res);
1080 sc_contracts_init_head(res);
1081 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
1082 res->name, restype, resid);
1083 fcb_resource_insert(fcb, res);
1092 CORBA_long register_allocator(fres_contract_broker obj,
1093 const frsh_resource_type_t restype,
1094 const frsh_resource_id_t resid,
1095 const fres_resource_allocator ra_obj,
1096 CORBA_Environment *ev)
1098 struct fcb *fcb = o2fcb(obj);
1099 struct resource *res;
1100 struct res_alloc *ra;
1101 struct res_key resource;
1102 forb_server_id server_id;
1103 char server_id_str[40];
1106 forb_get_server_id(ra_obj, &server_id);
1107 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1108 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
1109 restype, resid, server_id_str);
1111 resource.type = restype;
1112 resource.id = resid;
1113 res = fcb_resource_find(fcb, &resource);
1115 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
1117 ret = FRES_ERR_NO_RESOURCE_MANAGER;
1120 ra = fcb_alloc_find(res, &server_id);
1122 char *str = forb_object_to_string(ra_obj);
1123 ul_logerr("Allocator from already registered (%s)\n",
1126 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1129 ra = malloc(sizeof(*ra));
1134 ra->app = server_id;
1135 ra->ra = forb_object_duplicate(ra_obj);
1136 fcb_alloc_insert(res, ra);
1142 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1144 struct fcb *fcb = o2fcb(obj);
1145 fres_resource_seq *seq;
1146 struct resource *res;
1149 seq = malloc(sizeof(*seq));
1151 ev->major = FORB_EX_NO_MEMORY;
1154 memset(seq, 0, sizeof(*seq));
1157 gavl_cust_for_each(fcb_resource, fcb, res) {
1161 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1166 gavl_cust_for_each(fcb_resource, fcb, res) {
1167 seq->_buffer[n].restype = res->key.type;
1168 seq->_buffer[n].resid = res->key.id;
1169 seq->_buffer[n].desc.manager = res->mng;
1170 seq->_buffer[n].desc.name = res->name;
1178 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1180 struct fres_contract **c;
1181 forb_sequence_foreach(&transaction->contracts, c)
1182 if (fres_contract_get_spare_capacity(*c))
1188 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1189 int num, struct res_array *res_array)
1192 struct resource *resource;
1196 for (i = 0; i < num; i++) {
1197 get_fc_res_key(fc[i], &key);
1198 resource = fcb_resource_find(fcb, &key);
1199 ret = res_array_insert(res_array, resource);
1201 ret = FRSH_ERR_INTERNAL_ERROR;
1211 negotiate_transaction(fres_contract_broker _obj,
1212 const fres_transaction_t* transaction,
1213 CORBA_Environment *ev)
1215 struct fcb *fcb = o2fcb(_obj);
1216 struct fcb_contract **fcb_contracts, *fc;
1217 const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1218 int i, ret, num = user_contracts->_length;
1219 struct res_array res_array;
1221 struct resource *resource;
1222 fres_contract_ptr_seq *schedulable_contracts;
1223 struct fcb_transaction *ft;
1225 ul_logmsg("Negotiating transaction '%s' with %d contracts\n", transaction->name, num);
1226 if (transaction_has_spare_capacity(transaction)) {
1227 ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1231 fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1232 if (!fcb_contracts) {
1236 memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1238 ret = prepare_fcb_contracts(fcb, fcb_contracts,
1239 user_contracts->_buffer, num);
1241 goto err_free_fcb_contracts;
1243 res_array_init_array_field(&res_array);
1244 ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1246 gsa_cust_for_each(res_array, &res_array, resource) {
1247 reservation_list_init_head(&resource->rl);
1248 resource->rl.length = 0;
1249 for (i = 0; i < num; i++) {
1250 fc = fcb_contracts[i];
1251 get_fc_res_key(fc, &key);
1252 if (res_key_cmp(&key, &resource->key) == 0) {
1253 reservation_list_insert(&resource->rl, fc);
1254 resource->rl.length++;
1258 gsa_cust_for_each(res_array, &res_array, resource) {
1259 ret = reserve_resource(resource);
1261 ul_logerr("Reservation failed\n");
1262 goto err_cancel_reservation;
1265 fcb_remember_contracts(fcb, fcb_contracts, num);
1266 gsa_cust_for_each(res_array, &res_array, resource) {
1267 ret = commit_resource(resource, &schedulable_contracts);
1269 ul_logerr("Commit failed\n");
1270 goto err_cancel_reservation;
1272 fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
1273 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
1275 /* TODO: After we distinguish between APP and a real
1276 * allocator, allocate VRESes (and perform mode change) here.
1277 * Only if the resource allocator is not known at this point,
1278 * wait with allocation the same way as it is done now. For
1279 * this we will need more information about allocators i.e.
1280 * its type (centralized, distributed, in app, ...). Too
1281 * compilcated ;-( */
1283 ft = fcb_transaction_new(transaction->name);
1286 goto err_cancel_reservation;
1288 enum { NONE, CANCELATION, NEGOTIATION } op = NONE;
1289 for (i = 0; i < num; i++) {
1290 fc = fcb_contracts[i];
1291 if (fc->requested_contract &&
1292 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
1293 assert(i == 0 || op == CANCELATION);
1295 /* TODO: Review and test transaction cancelation!!! */
1296 fcb_contract_delete(fcb, fc);
1297 /* Note: requested_contract is also pointed by
1298 * contracts parameter and will be freed by FORB. */
1299 fc->requested_contract = NULL;
1300 fcb_contract_destroy(fc);
1302 assert(i == 0 || op == NEGOTIATION);
1304 tran_contract_insert_at(ft, fc, i);
1305 fc->transaction = ft;
1310 fcb_transaction_insert(fcb, ft);
1313 fcb_transaction_delete(fcb, ft);
1314 fcb_transaction_destroy(ft);
1319 res_array_delete_all(&res_array);
1321 err_cancel_reservation:
1322 gsa_cust_for_each(res_array, &res_array, resource) {
1323 cancel_reservations(resource);
1325 res_array_delete_all(&res_array);
1326 err_free_fcb_contracts:
1327 free_fcb_contracts(fcb_contracts, num);
1333 wait_transaction(fres_contract_broker _obj,
1334 const CORBA_char * name,
1335 fres_transaction_t** transaction,
1336 CORBA_Environment *ev)
1338 return FRSH_ERR_NOT_IMPLEMENTED;
1342 allocate_transaction_vres(fres_contract_broker _obj,
1343 const CORBA_char * name,
1344 const CORBA_long index,
1345 fres_contract_id_t *id,
1346 CORBA_Environment *ev)
1348 struct fcb *fcb = o2fcb(_obj);
1349 struct fcb_transaction *ft;
1350 struct fcb_contract *fc;
1352 struct res_alloc *ra;
1353 char * const *n = (char * const *)&name;
1355 struct resource *resource;
1356 fres_contract_ptr_seq vreses;
1357 CORBA_Environment ev2;
1359 #define GOTO(label, error) do { ret = (error); goto label; } while(0)
1361 ul_logmsg("Allocate transaction VRES '%s'[%d]\n", name, index);
1363 ft = fcb_transaction_find(fcb, n);
1364 if (!ft) GOTO(err, FRES_ERR_TRANSACTION_NOT_FOUND);
1366 fc = tran_contract_indx2item(ft, index);
1367 if (!fc) GOTO(err, FRSH_ERR_TOO_LARGE);
1369 /* TODO: If we allow migration of task between resources (due
1370 * to spare capacity reallocation), the following check will
1371 * need reworking/enhancing. */
1372 if (fc->ra) GOTO(err, FRES_ERR_VRES_ALREADY_ALLOCATED);
1374 if (!get_fc_res_key(fc, &key))
1375 GOTO(err, FRSH_ERR_RESOURCE_ID_INVALID);
1376 resource = fcb_resource_find(fcb, &key);
1378 GOTO(err, FRES_ERR_NO_RESOURCE_MANAGER);
1380 forb_get_req_source(_obj, &app);
1381 /* Find allocator for the requested VRES */
1382 ra = fcb_alloc_find(resource, &app);
1385 forb_server_id_to_string(str, &app, sizeof(str));
1386 ul_logerr("No resource allocator found for %d.%d and %s\n",
1387 resource->key.type, resource->key.id, str);
1388 GOTO(err, FRES_ERR_NO_RESOURCE_ALLOCATOR);
1392 if (!forb_sequence_alloc_buf(&vreses, 1))
1395 assert(fc->schedulable_contract);
1396 forb_sequence_elem(&vreses, 0) = fc->schedulable_contract;
1397 forb_sequence_length(&vreses) = 1;
1398 ret = fres_resource_allocator_change_vreses(ra->ra, &vreses, &ev2);
1399 ul_logerr("Err = %d\n", ret);
1400 if (forb_exception_occurred(&ev2))
1401 GOTO(err_free, fres_forbex2err(&ev2));
1408 forb_sequence_free_buf(&vreses, forb_no_destructor);
1414 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1415 static int register_inet_port(forb_orb orb)
1417 forb_port_t *port = forb_malloc(sizeof(*port));
1419 struct in_addr listen_on;
1423 memset(port, 0, sizeof(*port));
1424 listen_on.s_addr = INADDR_ANY;
1425 ret = forb_inet_port_init(&port->desc, listen_on, 0);
1428 ret = forb_register_port(orb, port);
1433 struct forb_fres_contract_broker_impl impl = {
1434 .negotiate_contracts = negotiate_contracts,
1435 .register_resource = register_resource,
1436 .register_allocator = register_allocator,
1437 .redistribute_spare_capacity = redistribute_spare_capacity,
1438 .get_resources = get_resources,
1439 .negotiate_transaction = negotiate_transaction,
1440 .wait_transaction = wait_transaction,
1441 .allocate_transaction_vres = allocate_transaction_vres,
1444 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1446 forb_server_id server_id;
1447 char server_id_str[sizeof(forb_server_id)*2+1];
1449 forb_get_server_id(peer_orb, &server_id);
1450 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1452 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1458 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1459 fosa_abs_time_t now;
1460 fosa_rel_time_t delay;
1461 fosa_clock_get_time(CLOCK_REALTIME, &now);
1462 delay = fosa_abs_time_extract_interval(start_time, now);
1463 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1464 fosa_rel_time_to_msec(delay));
1468 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1472 static struct option long_opts[] = {
1473 { "loglevel", required_argument, NULL, 'l' },
1480 printf("usage: fcb [ options ]\n");
1481 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
1484 int print_log_domain(ul_log_domain_t *domain, void *context)
1486 printf("%s = %d\n", domain->name, domain->level);
1490 int forb_main(forb_orb orb, int argc, char *argv[])
1492 struct fcb fcb_data;
1493 fres_contract_broker fcb;
1494 forb_executor_t executor;
1498 while ((opt = getopt_long(argc, argv, "hl:", &long_opts[0], NULL)) != EOF) {
1501 if (*optarg == '?') {
1502 ul_logreg_for_each_domain(print_log_domain, NULL);
1507 ret = ul_log_domain_arg2levels(optarg);
1509 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1515 exit(opt == 'h' ? 0 : 1);
1519 memset(&fcb_data, 0, sizeof(fcb_data));
1520 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1524 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1525 ret = register_inet_port(orb);
1526 if (ret) error(0, errno, "INET port registration failed");
1529 fcb_resource_init_root_field(&fcb_data);
1530 fcb_contract_init_root_field(&fcb_data);
1531 fcb_transaction_init_root_field(&fcb_data);
1533 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1534 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1536 /* Prepare executor before we register the fcb reference,
1537 * so that no reuqests are lost */
1538 ret = forb_executor_init(&executor);
1539 if (ret) error(1, errno, "forb_executor_init failed");
1541 ret = forb_executor_register_object(&executor, fcb);
1542 if (ret) error(1, errno, "forb_executor_register_object failed");
1544 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1545 if (ret) error(1, errno, "forb_register_reference() failed");
1547 ul_logmsg("Waiting for requests\n");
1549 forb_signal_server_ready(orb);
1551 ret = forb_executor_run(&executor);
1552 if (ret) error(1, errno, "forb_executor_run failed");