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);
765 forb_sequence_free_buf(&commit_ids, forb_no_destructor);
772 negotiate_contracts(fres_contract_broker obj,
773 const fres_contract_ptr_seq* contracts,
774 fres_contract_id_seq** ids_out,
775 CORBA_Environment *ev)
777 struct fcb *fcb = o2fcb(obj);
778 struct resource *resource = NULL;
781 fres_contract_ptr_seq *schedulable_contracts;
782 struct fcb_contract **fcb_contracts, *fc;
784 int num = contracts->_length;
786 /* Prepare output sequence for the case we return eariler with
788 forb_sequence_alloc(*ids_out, 0);
790 ev->major = FORB_EX_NO_MEMORY;
793 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
795 forb_get_req_source(obj, &app);
797 fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
798 if (!fcb_contracts) {
802 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
804 ret = prepare_fcb_contracts(fcb, fcb_contracts,
805 contracts->_buffer, num);
807 goto err_free_fcb_contracts;
808 ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
811 goto err_free_fcb_contracts;
813 prepare_reservation_list(resource, fcb_contracts, num);
815 /* Reserve contracts */
816 ret = rebalance_spare_capacity_and_reserve(resource);
818 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
819 ul_logmsg("Contract(s) was/were rejected\n");
822 fres_strerror(ret, msg, sizeof(msg));
823 ul_logerr("Reservation error: %s\n", msg);
825 goto err_free_fcb_contracts;
828 /* Commit contracts */
829 ret = commit_resource(resource, &schedulable_contracts);
831 goto err_cancel_reservation;
833 /* Add new contracts to our fcb database for later
834 * reference. Canceled contracts are removed below. */
835 for (i=0; i<num; i++) {
836 fc = fcb_contracts[i];
838 if (fc->user_contract) {
839 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
841 fres_contract_destroy(fc->user_contract);
842 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
843 /* Note: requested_contract is also
844 * pointed by contracts parameter and
845 * will be freed by FORB. */
846 fc->requested_contract = NULL;
849 /* Insert new contracts */
850 fcb_contract_insert(fcb, fcb_contracts[i]);
851 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
852 fc->requested_contract = NULL;
853 /* See the note above. */
857 ret = change_vreses(fcb, schedulable_contracts);
859 goto err_cancel_reservation;
861 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
863 ul_logerr("VRes was not created\n");
864 goto err_cancel_contracts;
868 /* Return IDs and delete canceled contracts from out database */
869 if (!forb_sequence_alloc_buf(*ids_out, num)) {
870 ev->major = FORB_EX_NO_MEMORY;
871 goto err_cancel_contracts;
873 (*ids_out)->_length = num;
874 for (i=0; i<num; i++) {
875 struct fcb_contract *fc = fcb_contracts[i];
876 forb_sequence_elem(*ids_out, i) = fc->id;
878 if (fc->requested_contract &&
879 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
880 fcb_contract_delete(fcb, fc);
881 /* Note: requested_contract is also pointed by
882 * contracts parameter and will be freed by FORB. */
883 fc->requested_contract = NULL;
884 fcb_contract_destroy(fc);
889 err_cancel_contracts:
891 goto err_free_fcb_contracts;
892 err_cancel_reservation:
893 cancel_reservations(resource);
894 err_free_fcb_contracts:
895 free_fcb_contracts(fcb_contracts, num);
900 void redistribute_spare_capacity(fres_contract_broker obj,
901 const frsh_resource_type_t restype,
902 const frsh_resource_id_t resid,
903 CORBA_Environment *ev)
905 struct fcb *fcb = o2fcb(obj);
906 struct res_key key = {restype, resid };
907 struct resource *resource;
909 resource = fcb_resource_find(fcb, &key);
911 prepare_reservation_list(resource, NULL, 0);
913 /* forb_sequence_alloc(ids, rl.length); */
914 /* if (!ids || !ids->_buffer) { */
915 /* ev->major = FORB_EX_NO_MEMORY; */
916 /* goto err_free_fcb_contracts; */
918 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
919 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
922 rebalance_spare_capacity_and_reserve(resource);
926 CORBA_long register_resource(fres_contract_broker obj,
927 const frsh_resource_type_t restype,
928 const frsh_resource_id_t resid,
929 const fres_resource_desc *desc,
930 CORBA_Environment *ev)
932 struct fcb *fcb = o2fcb(obj);
933 struct resource *res, *res2;
935 res = malloc(sizeof(*res));
937 memset(res, 0, sizeof(*res));
938 res->key.type = restype;
940 res2 = fcb_resource_find(fcb, &res->key);
942 if (forb_object_is_stale(res2->mng)) {
943 ul_logmsg("Removing stale manager for resource %d.%d\n",
945 forb_object_release(res2->mng);
946 fcb_resource_delete(fcb, res2);
947 /* TODO: Delete also all allocators associated
948 * with this stale resource manager. */
952 ul_logerr("Resource manager %d.%d already registered\n",
957 res->mng = forb_object_duplicate(desc->manager);
958 res->name = desc->name;
960 fcb_alloc_init_root_field(res);
961 sc_contracts_init_head(res);
962 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
963 res->name, restype, resid);
964 fcb_resource_insert(fcb, res);
973 CORBA_long register_allocator(fres_contract_broker obj,
974 const frsh_resource_type_t restype,
975 const frsh_resource_id_t resid,
976 const fres_resource_allocator ra_obj,
977 CORBA_Environment *ev)
979 struct fcb *fcb = o2fcb(obj);
980 struct resource *res;
981 struct res_alloc *ra;
982 struct res_key resource;
983 forb_server_id server_id;
984 char server_id_str[40];
987 forb_get_server_id(ra_obj, &server_id);
988 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
989 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
990 restype, resid, server_id_str);
992 resource.type = restype;
994 res = fcb_resource_find(fcb, &resource);
996 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
998 ret = FRES_ERR_NO_RESOURCE_MANAGER;
1001 ra = fcb_alloc_find(res, &server_id);
1003 char *str = forb_object_to_string(ra_obj);
1004 ul_logerr("Allocator from already registered (%s)\n",
1007 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1010 ra = malloc(sizeof(*ra));
1015 ra->app = server_id;
1016 ra->ra = forb_object_duplicate(ra_obj);
1017 fcb_alloc_insert(res, ra);
1023 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1025 struct fcb *fcb = o2fcb(obj);
1026 fres_resource_seq *seq;
1027 struct resource *res;
1030 seq = malloc(sizeof(*seq));
1032 ev->major = FORB_EX_NO_MEMORY;
1035 memset(seq, 0, sizeof(*seq));
1038 gavl_cust_for_each(fcb_resource, fcb, res) {
1042 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1047 gavl_cust_for_each(fcb_resource, fcb, res) {
1048 seq->_buffer[n].restype = res->key.type;
1049 seq->_buffer[n].resid = res->key.id;
1050 seq->_buffer[n].desc.manager = res->mng;
1051 seq->_buffer[n].desc.name = res->name;
1059 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1061 struct fres_contract **c;
1062 forb_sequence_foreach(&transaction->contracts, c)
1063 if (fres_contract_get_spare_capacity(*c))
1069 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1070 int num, struct res_array *res_array)
1073 struct resource *resource;
1077 for (i = 0; i < num; i++) {
1078 get_fc_res_key(fc[i], &key);
1079 resource = fcb_resource_find(fcb, &key);
1080 ret = res_array_insert(res_array, resource);
1082 ret = FRSH_ERR_INTERNAL_ERROR;
1092 negotiate_transaction(fres_contract_broker _obj,
1093 const fres_transaction_t* transaction,
1094 CORBA_Environment *ev)
1096 struct fcb *fcb = o2fcb(_obj);
1097 struct fcb_contract **fcb_contracts, *fc;
1098 const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1099 int i, ret, num = user_contracts->_length;
1100 struct res_array res_array;
1102 struct resource *resource;
1104 ul_logmsg("Negotiating transaction of %d contracts\n", num);
1105 if (transaction_has_spare_capacity(transaction)) {
1106 ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1110 fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1111 if (!fcb_contracts) {
1115 memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1117 ret = prepare_fcb_contracts(fcb, fcb_contracts,
1118 user_contracts->_buffer, num);
1120 goto err_free_fcb_contracts;
1122 res_array_init_array_field(&res_array);
1123 ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1125 gsa_cust_for_each(res_array, &res_array, resource) {
1126 reservation_list_init_head(&resource->rl);
1127 resource->rl.length = 0;
1128 for (i = 0; i < num; i++) {
1129 fc = fcb_contracts[i];
1130 get_fc_res_key(fc, &key);
1131 if (res_key_cmp(&key, &resource->key) == 0) {
1132 reservation_list_insert(&resource->rl, fc);
1133 resource->rl.length++;
1137 gsa_cust_for_each(res_array, &res_array, resource) {
1138 ret = reserve_resource(resource);
1140 ul_logerr("Reservation failed\n");
1141 goto err_cancel_reservation;
1144 gsa_cust_for_each(res_array, &res_array, resource) {
1145 ret = commit_resource(resource);
1147 ul_logerr("Commit failed\n");
1148 goto err_cancel_reservation;
1152 res_array_delete_all(&res_array);
1154 err_cancel_reservation:
1155 gsa_cust_for_each(res_array, &res_array, resource) {
1156 cancel_reservations(resource);
1158 res_array_delete_all(&res_array);
1159 err_free_fcb_contracts:
1160 free_fcb_contracts(fcb_contracts, num);
1166 wait_transaction(fres_contract_broker _obj,
1167 const CORBA_char * name,
1168 fres_transaction_t** transaction,
1169 CORBA_Environment *ev)
1171 return FRSH_ERR_NOT_IMPLEMENTED;
1175 allocate_transaction_vres(fres_contract_broker _obj,
1176 const CORBA_long id,
1177 CORBA_Environment *ev)
1179 return FRSH_ERR_NOT_IMPLEMENTED;
1183 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1184 static int register_inet_port(forb_orb orb)
1186 forb_port_t *port = forb_malloc(sizeof(*port));
1188 struct in_addr listen_on;
1192 memset(port, 0, sizeof(*port));
1193 listen_on.s_addr = INADDR_ANY;
1194 ret = forb_inet_port_init(&port->desc, listen_on, 0);
1197 ret = forb_register_port(orb, port);
1202 struct forb_fres_contract_broker_impl impl = {
1203 .negotiate_contracts = negotiate_contracts,
1204 .register_resource = register_resource,
1205 .register_allocator = register_allocator,
1206 .redistribute_spare_capacity = redistribute_spare_capacity,
1207 .get_resources = get_resources,
1208 .negotiate_transaction = negotiate_transaction,
1209 .wait_transaction = wait_transaction,
1210 .allocate_transaction_vres = allocate_transaction_vres,
1213 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1215 forb_server_id server_id;
1216 char server_id_str[sizeof(forb_server_id)*2+1];
1218 forb_get_server_id(peer_orb, &server_id);
1219 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1221 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1227 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1228 fosa_abs_time_t now;
1229 fosa_rel_time_t delay;
1230 fosa_clock_get_time(CLOCK_REALTIME, &now);
1231 delay = fosa_abs_time_extract_interval(start_time, now);
1232 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1233 fosa_rel_time_to_msec(delay));
1237 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1241 static struct option long_opts[] = {
1242 { "daemon", optional_argument, NULL, 'd' },
1243 { "loglevel", required_argument, NULL, 'l' },
1250 printf("usage: fcb [ options ]\n");
1251 printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
1252 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
1255 int print_log_domain(ul_log_domain_t *domain, void *context)
1257 printf("%s = %d\n", domain->name, domain->level);
1261 int main(int argc, char *argv[])
1264 struct fcb fcb_data;
1265 fres_contract_broker fcb;
1266 forb_executor_t executor;
1268 forb_init_attr_t attr = {
1269 .orb_id = "org.frescor.fcb",
1270 .peer_discovery_callback = peer_discovery_callback,
1271 .peer_dead_callback = peer_dead_callback,
1272 .fixed_tcp_port = FCB_TCP_PORT,
1273 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
1274 .fixed_server_id = FCB_SERVER_ID,
1275 .redistribute_hellos = true,
1280 while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1283 if (*optarg == '?') {
1284 ul_logreg_for_each_domain(print_log_domain, NULL);
1289 ret = ul_log_domain_arg2levels(optarg);
1291 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1296 opt_pidfile = optarg;
1301 exit(opt == 'h' ? 0 : 1);
1305 memset(&fcb_data, 0, sizeof(fcb_data));
1306 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1309 forb_daemon_prepare(opt_pidfile);
1311 orb = forb_init(&argc, &argv, &attr);
1312 if (!orb) error(1, errno, "FORB initialization failed");
1314 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1315 ret = register_inet_port(orb);
1316 if (ret) error(0, errno, "INET port registration failed");
1319 fcb_resource_init_root_field(&fcb_data);
1320 fcb_contract_init_root_field(&fcb_data);
1322 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1323 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1325 /* Prepare executor before we register the fcb reference,
1326 * so that no reuqests are lost */
1327 ret = forb_executor_init(&executor);
1328 if (ret) error(1, errno, "forb_executor_init failed");
1330 ret = forb_executor_register_object(&executor, fcb);
1331 if (ret) error(1, errno, "forb_executor_register_object failed");
1333 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1334 if (ret) error(1, errno, "forb_register_reference() failed");
1336 ul_logmsg("Waiting for requests\n");
1338 forb_daemon_ready();
1340 ret = forb_executor_run(&executor);
1341 if (ret) error(1, errno, "forb_executor_run failed");