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
58 #include <forb/config.h>
60 #include <fcb_contact_info.h>
64 #include <ul_gavlcust.h>
67 #include <ul_logreg.h>
68 #include <forb/server_id.h>
69 #include <fres_contract.h>
70 #include "fcb_config.h"
71 #include <fosa_clocks_and_timers.h>
72 #include "contract_log.h"
73 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
74 #include <forb/proto_inet.h>
77 UL_LOG_CUST(ulogd_fcb);
78 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
79 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
81 fosa_abs_time_t start_time;
85 * Resource identification
88 frsh_resource_type_t type;
89 frsh_resource_id_t id;
99 fres_resource_manager mng; /**< Object reference of the resource manager */
100 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
101 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
105 * Resource allocator registered in different nodes/applications
110 fres_resource_allocator ra;
113 struct fcb_contract {
114 fres_contract_id_t id;
115 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
116 gavl_node_t node_fcb;
117 ul_list_node_t node_sc;
118 ul_list_node_t node_reservation;
123 fosa_abs_time_t end_of_stability_period;
124 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
125 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
126 struct fres_contract *to_be_reserved_contract;
127 struct fres_contract *schedulable_contract;
131 * Contract broker data
134 gavl_cust_root_field_t resources; /**< Registered resources */
135 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
138 /** List of contracts to be reserved during spare capacity rebalancing */
139 struct reservation_list {
140 ul_list_head_t fcb_contracts;
144 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
146 struct fcb_contract *fcb_contract;
148 fcb_contract = malloc(sizeof(*fcb_contract));
152 memset(fcb_contract, 0, sizeof(*fcb_contract));
153 fcb_contract->id = *id;
158 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
160 if (fcb_contract->user_contract) {
161 fres_contract_destroy(fcb_contract->user_contract);
163 if (fcb_contract->to_be_reserved_contract) {
164 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
166 if (fcb_contract->requested_contract) {
167 fres_contract_destroy(fcb_contract->requested_contract);
169 if (fcb_contract->schedulable_contract) {
170 fres_contract_destroy(fcb_contract->requested_contract);
175 static inline int res_key_cmp(const struct res_key *a,
176 const struct res_key *b)
178 if (a->type < b->type) {
180 } else if (a->type > b->type) {
182 } else if (a->id < b->id) {
184 } else if (a->id > b->id) {
191 /* Container for registered resources */
192 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
193 struct fcb /* cust_root_t */, \
194 struct resource/* cust_item_t */, \
195 struct res_key /* cust_key_t */, \
196 resources /* cust_root_node */, \
197 node /* cust_item_node */, \
198 key /* cust_item_key */, \
199 res_key_cmp /* cust_cmp_fnc */);
201 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
202 struct fcb /* cust_root_t */, \
203 struct resource/* cust_item_t */, \
204 struct res_key /* cust_key_t */, \
205 resources /* cust_root_node */, \
206 node /* cust_item_node */, \
207 key /* cust_item_key */, \
208 res_key_cmp /* cust_cmp_fnc */);
210 /* Container for allocators registered for a given resource */
211 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
212 struct resource /* cust_root_t */, \
213 struct res_alloc /* cust_item_t */, \
214 forb_server_id /* cust_key_t */, \
215 allocators /* cust_root_node */, \
216 node /* cust_item_node */, \
217 app /* cust_item_key */, \
218 fres_contract_id_cmp /* cust_cmp_fnc */);
220 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
221 struct resource /* cust_root_t */, \
222 struct res_alloc /* cust_item_t */, \
223 forb_server_id /* cust_key_t */, \
224 allocators /* cust_root_node */, \
225 node /* cust_item_node */, \
226 app /* cust_item_key */, \
227 forb_server_id_cmp /* cust_cmp_fnc */);
229 /* Container for contracts with spare capacity negotiated for a given resource */
230 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
231 struct resource /* cust_head_t */, \
232 struct fcb_contract /* cust_item_t */, \
233 sc_contracts /* cust_head_field */, \
234 node_sc /* cust_node_field */);
236 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
237 struct reservation_list /* cust_head_t */, \
238 struct fcb_contract /* cust_item_t */, \
239 fcb_contracts /* cust_head_field */, \
240 node_reservation /* cust_node_field */);
242 /* Container for negotiated contracts */
243 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
244 struct fcb /* cust_root_t */, \
245 struct fcb_contract /* cust_item_t */, \
246 fres_contract_id_t /* cust_key_t */, \
247 contracts /* cust_root_node */, \
248 node_fcb /* cust_item_node */, \
249 id /* cust_item_key */, \
250 fres_contract_id_cmp /* cust_cmp_fnc */);
253 GAVL_CUST_NODE_INT_IMP(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 */);
262 #include "fcb_contract_gavl.inc"
266 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
269 get_res_key(const struct fres_contract *contract, struct res_key *key)
271 fres_block_resource *block_res;
273 block_res = fres_contract_get_resource(contract);
275 ul_logerr("No resource specified\n");
278 key->type = block_res->resource_type;
279 key->id = block_res->resource_id;
285 * Fills in an array of fcb_contracts according to requests submited
286 * to negotiate_contracts(). For every contract in @a contracts, there
287 * is one fcb_contract in @a fcb_contracts array.
290 * @param fcb_contracts Where to store pointers to fcb_contracts
291 * @param resource Resource for which negotiation is being done.
292 * @param app Application which requests negotiation
293 * @param contracts Contracts submited to negotiate_contracts()
294 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
296 * @return Zero on success, non-zero error code on error.
299 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
300 struct resource **resource,
302 struct fres_contract *contracts[], int num)
305 struct fcb_contract *fc;
306 struct res_alloc *ra;
307 struct res_key key, key2 = {-1,-1};
309 for (i=0; i<num; i++) {
310 struct fres_contract *c = contracts[i];
312 log_contract("Received contract", i, c);
314 if (fres_contract_id_is_empty(&c->id)) {
315 /* Normal negotiation request */
316 forb_uuid_generate((forb_uuid_t *)&c->id);
317 fc = fcb_contract_new(&c->id);
320 fcb_contracts[i] = fc;
321 if (!get_res_key(c, &key)) {
322 return FRSH_ERR_RESOURCE_ID_INVALID;
325 fc = fcb_contract_find(fcb, &c->id);
328 fres_contract_id_to_string(str, &c->id, sizeof(str));
329 ul_logerr("Attempt to change unknown contract %s\n", str);
330 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
332 fcb_contracts[i] = fc;
333 if (fres_contract_get_num_blocks(c) == 0) {
335 get_res_key(fc->user_contract, &key);
338 if (!get_res_key(c, &key)) {
339 return FRSH_ERR_RESOURCE_ID_INVALID;
344 fc->requested_contract = c;
346 /* Check that all contracts are for the same resource */
349 *resource = fcb_resource_find(fcb, &key);
351 ul_logerr("No resource manager for %d.%d registered\n",
353 return FRES_ERR_NO_RESOURCE_MANAGER;
356 else if (key.type != key2.type ||
358 ul_logerr("Contract #%d differs in resource!\n", i);
359 return FRSH_ERR_RESOURCE_ID_INVALID;
362 /* Find allocator for this request */
363 ra = fcb_alloc_find(*resource, app);
366 forb_server_id_to_string(str, app, sizeof(str));
367 ul_logerr("No resource allocator found for %d.%d and %s\n",
368 (*resource)->key.type, (*resource)->key.id, str);
369 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
377 * @param resource Resource for which to rebalance capacity and negotiate new contracts
378 * @param fcb_contract New requests to negotiate
379 * @param num The number of elements in @a fcb_contract
380 * @param[out] rl List with changed contracts to be commited
383 prepare_reservation_list(struct resource *resource,
384 struct fcb_contract *fcb_contract[], int num,
385 struct reservation_list *rl)
389 struct fcb_contract *fc;
391 reservation_list_init_head(rl);
393 for (i=0; i<num; i++) {
394 assert(fcb_contract[i]->requested_contract != NULL);
395 reservation_list_insert(rl, fcb_contract[i]);
398 fosa_clock_get_time(CLOCK_REALTIME, &now);
399 ul_list_for_each(sc_contracts, resource, fc) {
400 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
401 fc->requested_contract == NULL) /* Do not insert contract inserted above */
403 reservation_list_insert(rl, fc);
412 * @param resource Resource for which to rebalance capacity and negotiate new contracts
413 * @param rl List with changed contracts to be commited
415 * @return Zero on success, non-zero error code on error
418 rebalance_spare_capacity_and_reserve(struct resource *resource,
419 struct reservation_list *rl)
423 struct fcb_contract *fc;
424 fres_block_spare_capacity *s;
426 fres_contract_ptr_seq contracts;
427 if (!forb_sequence_alloc_buf(contracts, rl->length)) {
430 contracts._length = rl->length;
432 /* Initialize optimization */
433 ul_list_for_each(reservation_list, rl, fc) {
434 fc->to_be_reserved_contract =
435 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
436 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
438 assert(fc->to_be_reserved_contract != NULL);
440 forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
443 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
444 if (s && s->granularity == FRSH_GR_DISCRETE) {
445 fc->sc_variant.initial = s->variants._length - 1;
446 fc->sc_variant.try = fc->sc_variant.initial;
450 bool all_combinations_tried;
451 int criterion, best_criterion = -1;
452 struct fcb_contract *fcb_changed;
453 /* Exhaustive search. Try all combinations of spare capacity
454 * variants and find the one with best creterion. */
456 all_combinations_tried = true;
459 /* Prepare spare capacity variant */
460 ul_list_for_each(reservation_list, rl, fc) {
461 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
462 /* TODO: Simulate continuous granularity by discretization */
463 if (s && s->granularity == FRSH_GR_DISCRETE) {
464 fres_container_copy(fc->to_be_reserved_contract->container,
465 forb_sequence_elem(s->variants, fc->sc_variant.try));
466 criterion += fc->sc_variant.try;
468 if (fcb_changed == NULL) {
469 /* Chnage tried variant for the next round */
470 fc->sc_variant.try = fc->sc_variant.try > 0 ?
471 fc->sc_variant.try - 1 :
472 s->variants._length - 1;
473 /* Do not change other
474 * contracts unless we have
475 * tried all variants */
476 if (fc->sc_variant.try != fc->sc_variant.initial) {
480 if (fc->sc_variant.try != fc->sc_variant.initial)
481 all_combinations_tried = false;
485 if (criterion > best_criterion) {
486 CORBA_Environment ev;
487 /* Reserve contract */
488 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
489 if (forb_exception_occurred(&ev)) {
490 ret = fres_forbex2err(&ev);
491 ul_logerr("FORB exception when reserving contracts\n");
495 ul_logerr("Contract reservation error %d\n", ret);
496 ret = FRES_ERR_ADMISSION_TEST;
499 if (ret == 0) { /* negotiation succeeded */
500 best_criterion = criterion;
503 } while (!all_combinations_tried);
505 if (best_criterion == -1) {
506 ret = FRSH_ERR_CONTRACT_REJECTED;
508 /* At least some variant succeeded */
510 sc_contracts_init_head(resource);
511 ul_list_for_each(reservation_list, rl, fc) {
512 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
513 if (s && s->granularity == FRSH_GR_DISCRETE) {
514 sc_contracts_insert(resource, fc);
519 ul_list_for_each(reservation_list, rl, fc) {
520 fres_contract_destroy(fc->to_be_reserved_contract);
521 fc->to_be_reserved_contract = NULL;
523 forb_sequence_free_buf(contracts, forb_no_destructor);
528 * Create/change VReses according to @a schedulable_contracts.
530 * There might be more allocators for schedulable contracts, so merge
531 * adjacent vreses with the same allocator together and create/change
532 * them in one step. Also the order of schedulable contracts might be
533 * different from the initial order od ids in commit_contracts()
534 * therefore we have to search for every contract.
537 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
539 struct res_alloc *last_ra = NULL;
540 fres_contract_ptr_seq vreses;
542 CORBA_Environment ev;
544 if (!forb_sequence_alloc_buf(vreses, schedulable_contracts->_length)) {
549 for (i=0; i<schedulable_contracts->_length; i++) {
550 struct fcb_contract *fc;
551 struct fres_contract *sc = schedulable_contracts->_buffer[i];
553 log_contract("Changing VRES", i, sc);
555 fc = fcb_contract_find(fcb, &sc->id);
558 if (true /* TODO: if the schedulable contract is changed */ &&
559 fres_contract_get_type(&sc) != FRSH_CT_DUMMY) {
560 if (last_ra != fc->ra) {
561 if (vreses._length) {
562 ret = fres_resource_allocator_change_vreses(last_ra->ra,
564 if (forb_exception_occurred(&ev)) {
565 ret = fres_forbex2err(&ev);
572 vreses._buffer[vreses._length] = sc;
574 fres_contract_destroy(fc->schedulable_contract);
575 fc->schedulable_contract = fres_contract_duplicate(sc);
580 ret = fres_resource_allocator_change_vreses(last_ra->ra,
582 if (forb_exception_occurred(&ev))
583 ret = fres_forbex2err(&ev);
585 forb_sequence_free_buf(vreses, forb_no_destructor);
590 negotiate_contracts(fres_contract_broker obj,
591 const fres_contract_ptr_seq* contracts,
592 fres_contract_id_seq** ids_out,
593 CORBA_Environment *ev)
595 struct fcb *fcb = o2fcb(obj);
596 struct resource *resource = NULL;
599 fres_contract_ptr_seq *schedulable_contracts;
600 struct fcb_contract **fcb_contracts, *fc;
602 fres_contract_id_seq commit_ids;
604 /* Prepare output sequence for the case we return eariler with
606 forb_sequence_alloc(*ids_out, 0);
608 ev->major = FORB_EX_NO_MEMORY;
611 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
613 forb_get_req_source(obj, &app);
615 fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
616 if (!fcb_contracts) {
620 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
622 ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
623 &app, contracts->_buffer, contracts->_length);
625 goto err_free_fcb_contracts;
627 struct reservation_list rl;
628 prepare_reservation_list(resource,
629 fcb_contracts, contracts->_length,
632 /* Allocate all the needed memory before doing reservation. If
633 * there is no enough memory, it has no sense to call resource
635 if (!forb_sequence_alloc_buf(commit_ids, rl.length)) {
637 goto err_free_fcb_contracts;
640 /* Reserve contracts */
641 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
643 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
644 ul_logmsg("Contract(s) was/were rejected\n");
647 fres_strerror(ret, msg, sizeof(msg));
648 ul_logerr("Reservation error: %s\n", msg);
650 goto err_free_fcb_contracts;
653 /* Commit contracts */
654 commit_ids._length = rl.length;
656 ul_list_for_each(reservation_list, &rl, fc) {
657 forb_sequence_elem(commit_ids, i) = fc->id;
661 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
662 &schedulable_contracts, ev);
663 if (forb_exception_occurred(ev)) {
664 ret = fres_forbex2err(ev);
665 goto err_cancel_reservation;
668 /* Add new contracts to our fcb database for later
669 * reference. Canceled contracts are removed below. */
670 for (i=0; i<contracts->_length; i++) {
671 fc = fcb_contracts[i];
673 if (fc->user_contract) {
674 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
676 fres_contract_destroy(fc->user_contract);
677 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
678 /* Note: requested_contract is also
679 * pointed by contracts parameter and
680 * will be freed by FORB. */
681 fc->requested_contract = NULL;
684 /* Insert new contracts */
685 fcb_contract_insert(fcb, fcb_contracts[i]);
686 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
687 fc->requested_contract = NULL;
688 /* See the note above. */
692 ret = change_vreses(fcb, schedulable_contracts);
694 goto err_cancel_reservation;
696 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
698 ul_logerr("VRes was not created\n");
699 goto err_cancel_contracts;
703 /* Return IDs and delete canceled contracts from out database */
704 if (!forb_sequence_alloc_buf(**ids_out, contracts->_length)) {
705 ev->major = FORB_EX_NO_MEMORY;
706 goto err_cancel_contracts;
708 (*ids_out)->_length = contracts->_length;
709 for (i=0; i<contracts->_length; i++) {
710 struct fcb_contract *fc = fcb_contracts[i];
711 forb_sequence_elem(**ids_out, i) = fc->id;
713 if (fc->requested_contract &&
714 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
715 fcb_contract_delete(fcb, fc);
716 /* Note: requested_contract is also pointed by
717 * contracts parameter and will be freed by FORB. */
718 fc->requested_contract = NULL;
719 fcb_contract_destroy(fc);
724 err_cancel_contracts:
726 goto err_free_fcb_contracts;
727 err_cancel_reservation:
728 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
729 err_free_fcb_contracts:
730 for (i=0; i<contracts->_length; i++) {
731 fc = fcb_contracts[i];
732 if (fc && !fc->user_contract) {
733 fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
734 fcb_contract_destroy(fcb_contracts[i]);
742 void redistribute_spare_capacity(fres_contract_broker obj,
743 const frsh_resource_type_t restype,
744 const frsh_resource_id_t resid,
745 CORBA_Environment *ev)
747 struct fcb *fcb = o2fcb(obj);
748 struct res_key key = {restype, resid };
749 struct resource *resource;
750 struct reservation_list rl;
752 resource = fcb_resource_find(fcb, &key);
754 prepare_reservation_list(resource, NULL, 0, &rl);
756 /* forb_sequence_alloc(ids, rl.length); */
757 /* if (!ids || !ids->_buffer) { */
758 /* ev->major = FORB_EX_NO_MEMORY; */
759 /* goto err_free_fcb_contracts; */
761 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
762 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
765 rebalance_spare_capacity_and_reserve(resource, &rl);
769 CORBA_long register_resource(fres_contract_broker obj,
770 const frsh_resource_type_t restype,
771 const frsh_resource_id_t resid,
772 const fres_resource_desc *desc,
773 CORBA_Environment *ev)
775 struct fcb *fcb = o2fcb(obj);
776 struct resource *res, *res2;
778 res = malloc(sizeof(*res));
780 memset(res, 0, sizeof(*res));
781 res->key.type = restype;
783 res2 = fcb_resource_find(fcb, &res->key);
785 if (forb_object_is_stale(res2->mng)) {
786 ul_logmsg("Removing stale manager for resource %d.%d\n",
788 forb_object_release(res2->mng);
789 fcb_resource_delete(fcb, res2);
790 /* TODO: Delete also all allocators associated
791 * with this stale resource manager. */
795 ul_logerr("Resource manager %d.%d already registered\n",
800 res->mng = forb_object_duplicate(desc->manager);
801 res->name = desc->name;
803 fcb_alloc_init_root_field(res);
804 sc_contracts_init_head(res);
805 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
806 res->name, restype, resid);
807 fcb_resource_insert(fcb, res);
816 CORBA_long register_allocator(fres_contract_broker obj,
817 const frsh_resource_type_t restype,
818 const frsh_resource_id_t resid,
819 const fres_resource_allocator ra_obj,
820 CORBA_Environment *ev)
822 struct fcb *fcb = o2fcb(obj);
823 struct resource *res;
824 struct res_alloc *ra;
825 struct res_key resource;
826 forb_server_id server_id;
827 char server_id_str[40];
830 forb_get_server_id(ra_obj, &server_id);
831 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
832 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
833 restype, resid, server_id_str);
835 resource.type = restype;
837 res = fcb_resource_find(fcb, &resource);
839 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
841 ret = FRES_ERR_NO_RESOURCE_MANAGER;
844 ra = fcb_alloc_find(res, &server_id);
846 char *str = forb_object_to_string(ra_obj);
847 ul_logerr("Allocator from already registered (%s)\n",
850 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
853 ra = malloc(sizeof(*ra));
859 ra->ra = forb_object_duplicate(ra_obj);
860 fcb_alloc_insert(res, ra);
866 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
868 struct fcb *fcb = o2fcb(obj);
869 fres_resource_seq *seq;
870 struct resource *res;
873 seq = malloc(sizeof(*seq));
875 ev->major = FORB_EX_NO_MEMORY;
878 memset(seq, 0, sizeof(*seq));
881 gavl_cust_for_each(fcb_resource, fcb, res) {
885 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
890 gavl_cust_for_each(fcb_resource, fcb, res) {
891 seq->_buffer[n].restype = res->key.type;
892 seq->_buffer[n].resid = res->key.id;
893 seq->_buffer[n].desc.manager = res->mng;
894 seq->_buffer[n].desc.name = res->name;
901 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
902 static int register_inet_port(forb_orb orb)
904 forb_port_t *port = forb_malloc(sizeof(*port));
906 struct in_addr listen_on;
910 memset(port, 0, sizeof(*port));
911 listen_on.s_addr = INADDR_ANY;
912 ret = forb_inet_port_init(&port->desc, listen_on, 0);
915 ret = forb_register_port(orb, port);
920 struct forb_fres_contract_broker_impl impl = {
921 .negotiate_contracts = negotiate_contracts,
922 .register_resource = register_resource,
923 .register_allocator = register_allocator,
924 .redistribute_spare_capacity = redistribute_spare_capacity,
925 .get_resources = get_resources,
928 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
930 forb_server_id server_id;
931 char server_id_str[sizeof(forb_server_id)*2+1];
933 forb_get_server_id(peer_orb, &server_id);
934 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
936 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
942 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
944 fosa_rel_time_t delay;
945 fosa_clock_get_time(CLOCK_REALTIME, &now);
946 delay = fosa_abs_time_extract_interval(start_time, now);
947 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
948 fosa_rel_time_to_msec(delay));
952 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
956 static struct option long_opts[] = {
957 { "loglevel", 1, 0, 'l' },
964 printf("usage: fcb [ options ]\n");
965 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
968 int main(int argc, char *argv[])
972 fres_contract_broker fcb;
973 forb_executor_t executor;
975 forb_init_attr_t attr = {
976 .orb_id = "org.frescor.fcb",
977 .peer_discovery_callback = peer_discovery_callback,
978 .peer_dead_callback = peer_dead_callback,
979 .fixed_tcp_port = FCB_TCP_PORT,
980 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
981 .fixed_server_id = FCB_SERVER_ID,
982 .redistribute_hellos = true,
987 while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
990 ul_log_domain_arg2levels(optarg);
995 exit(opt == 'h' ? 0 : 1);
999 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1001 orb = forb_init(&argc, &argv, &attr);
1002 if (!orb) error(1, errno, "FORB initialization failed");
1004 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1005 ret = register_inet_port(orb);
1006 if (ret) error(0, errno, "INET port registration failed");
1009 fcb_resource_init_root_field(&fcb_data);
1010 fcb_contract_init_root_field(&fcb_data);
1012 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1013 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1015 /* Prepare executor before we register the fcb reference,
1016 * so that no reuqests are lost */
1017 ret = forb_executor_init(&executor);
1018 if (ret) error(1, errno, "forb_executor_init failed");
1020 ret = forb_executor_register_object(&executor, fcb);
1021 if (ret) error(1, errno, "forb_executor_register_object failed");
1023 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1024 if (ret) error(1, errno, "forb_register_reference() failed");
1026 ul_logmsg("Waiting for requests\n");
1027 ret = forb_executor_run(&executor);
1028 if (ret) error(1, errno, "forb_executor_run failed");