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 #include <semaphore.h>
59 #include <forb/config.h>
61 #include <fcb_contact_info.h>
65 #include <ul_gavlcust.h>
68 #include <ul_logreg.h>
69 #include <forb/server_id.h>
70 #include <fres_contract.h>
71 #include "fcb_config.h"
72 #include <fosa_clocks_and_timers.h>
73 #include "contract_log.h"
74 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
75 #include <forb/proto_inet.h>
78 UL_LOG_CUST(ulogd_fcb);
79 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
80 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
82 bool opt_daemon = false;
83 char *opt_pidfile = NULL;
84 fosa_abs_time_t start_time;
88 * Resource identification
91 frsh_resource_type_t type;
92 frsh_resource_id_t id;
102 fres_resource_manager mng; /**< Object reference of the resource manager */
103 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
104 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
108 * Resource allocator registered in different nodes/applications
113 fres_resource_allocator ra;
116 struct fcb_contract {
117 fres_contract_id_t id;
118 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
119 gavl_node_t node_fcb;
120 ul_list_node_t node_sc;
121 ul_list_node_t node_reservation;
126 fosa_abs_time_t end_of_stability_period;
127 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
128 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
129 struct fres_contract *to_be_reserved_contract;
130 struct fres_contract *schedulable_contract;
134 * Contract broker data
137 gavl_cust_root_field_t resources; /**< Registered resources */
138 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
141 /** List of contracts to be reserved during spare capacity rebalancing */
142 struct reservation_list {
143 ul_list_head_t fcb_contracts;
147 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
149 struct fcb_contract *fcb_contract;
151 fcb_contract = malloc(sizeof(*fcb_contract));
155 memset(fcb_contract, 0, sizeof(*fcb_contract));
156 fcb_contract->id = *id;
161 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
163 if (fcb_contract->user_contract) {
164 fres_contract_destroy(fcb_contract->user_contract);
166 if (fcb_contract->to_be_reserved_contract) {
167 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
169 if (fcb_contract->requested_contract) {
170 fres_contract_destroy(fcb_contract->requested_contract);
172 if (fcb_contract->schedulable_contract) {
173 fres_contract_destroy(fcb_contract->requested_contract);
178 static inline int res_key_cmp(const struct res_key *a,
179 const struct res_key *b)
181 if (a->type < b->type) {
183 } else if (a->type > b->type) {
185 } else if (a->id < b->id) {
187 } else if (a->id > b->id) {
194 /* Container for registered resources */
195 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
196 struct fcb /* cust_root_t */, \
197 struct resource/* cust_item_t */, \
198 struct res_key /* cust_key_t */, \
199 resources /* cust_root_node */, \
200 node /* cust_item_node */, \
201 key /* cust_item_key */, \
202 res_key_cmp /* cust_cmp_fnc */);
204 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
205 struct fcb /* cust_root_t */, \
206 struct resource/* cust_item_t */, \
207 struct res_key /* cust_key_t */, \
208 resources /* cust_root_node */, \
209 node /* cust_item_node */, \
210 key /* cust_item_key */, \
211 res_key_cmp /* cust_cmp_fnc */);
213 /* Container for allocators registered for a given resource */
214 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
215 struct resource /* cust_root_t */, \
216 struct res_alloc /* cust_item_t */, \
217 forb_server_id /* cust_key_t */, \
218 allocators /* cust_root_node */, \
219 node /* cust_item_node */, \
220 app /* cust_item_key */, \
221 fres_contract_id_cmp /* cust_cmp_fnc */);
223 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
224 struct resource /* cust_root_t */, \
225 struct res_alloc /* cust_item_t */, \
226 forb_server_id /* cust_key_t */, \
227 allocators /* cust_root_node */, \
228 node /* cust_item_node */, \
229 app /* cust_item_key */, \
230 forb_server_id_cmp /* cust_cmp_fnc */);
232 /* Container for contracts with spare capacity negotiated for a given resource */
233 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
234 struct resource /* cust_head_t */, \
235 struct fcb_contract /* cust_item_t */, \
236 sc_contracts /* cust_head_field */, \
237 node_sc /* cust_node_field */);
239 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
240 struct reservation_list /* cust_head_t */, \
241 struct fcb_contract /* cust_item_t */, \
242 fcb_contracts /* cust_head_field */, \
243 node_reservation /* cust_node_field */);
245 /* Container for negotiated contracts */
246 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
247 struct fcb /* cust_root_t */, \
248 struct fcb_contract /* cust_item_t */, \
249 fres_contract_id_t /* cust_key_t */, \
250 contracts /* cust_root_node */, \
251 node_fcb /* cust_item_node */, \
252 id /* cust_item_key */, \
253 fres_contract_id_cmp /* cust_cmp_fnc */);
256 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
257 struct fcb /* cust_root_t */, \
258 struct fcb_contract /* cust_item_t */, \
259 fres_contract_id_t /* cust_key_t */, \
260 contracts /* cust_root_node */, \
261 node_fcb /* cust_item_node */, \
262 id /* cust_item_key */, \
263 fres_contract_id_cmp /* cust_cmp_fnc */);
265 #include "fcb_contract_gavl.inc"
269 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
272 get_res_key(const struct fres_contract *contract, struct res_key *key)
274 fres_block_resource *block_res;
276 block_res = fres_contract_get_resource(contract);
278 ul_logerr("No resource specified\n");
281 key->type = block_res->resource_type;
282 key->id = block_res->resource_id;
288 * Fills in an array of fcb_contracts according to requests submited
289 * by an application through negotiate_contracts() or
290 * negotiate_transaction(). For every contract in @a contracts, there
291 * is allocated one fcb_contract in @a fcb_contracts array.
294 * @param fcb_contracts Where to store pointers to fcb_contracts
295 * @param contracts Contracts submited for negotiation
296 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
298 * @return Zero on success, non-zero error code on error.
301 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
302 struct fres_contract *contracts[], int num)
305 struct fcb_contract *fc;
308 for (i=0; i<num; i++) {
309 struct fres_contract *c = contracts[i];
311 if (fres_contract_id_is_empty(&c->id)) {
312 /* Normal negotiation request */
313 forb_uuid_generate((forb_uuid_t *)&c->id);
314 fc = fcb_contract_new(&c->id);
317 fcb_contracts[i] = fc;
318 if (!get_res_key(c, &key)) {
319 return FRSH_ERR_RESOURCE_ID_INVALID;
321 log_contract("Negotiation request", i, c);
323 fc = fcb_contract_find(fcb, &c->id);
326 fres_contract_id_to_string(str, &c->id, sizeof(str));
327 ul_logerr("Attempt to change unknown contract %s\n", str);
328 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
330 fcb_contracts[i] = fc;
331 if (fres_contract_get_num_blocks(c) == 0) {
333 get_res_key(fc->user_contract, &key);
334 log_contract("Cancelation request", i, fc->user_contract);
337 if (!get_res_key(c, &key)) {
338 return FRSH_ERR_RESOURCE_ID_INVALID;
340 log_contract("Renegotiation request", i, fc->user_contract);
344 fc->requested_contract = c;
350 * Ensure that all contracts belong to the same resource and find
351 * resource allocators for the contracts.
354 * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
355 * @param num Number of contracts in @a fcb_contracts
356 * @param resource Resource for which negotiation is being done.
357 * @param app Application which requests negotiation
360 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
361 struct resource **resource,
366 struct res_alloc *ra;
367 struct res_key key, key2 = {-1,-1};
369 for (i=0; i<num; i++) {
370 struct fcb_contract *fc = fcb_contracts[i];
371 struct fres_contract *c = fc->requested_contract;
373 if (fres_contract_get_num_blocks(c) == 0) {
375 get_res_key(fc->user_contract, &key);
376 log_contract("Cancelation request", i, fc->user_contract);
378 /* (Re)Negotiation */
379 if (!get_res_key(c, &key)) {
380 return FRSH_ERR_RESOURCE_ID_INVALID;
383 /* Check that all contracts are for the same resource */
386 *resource = fcb_resource_find(fcb, &key);
388 ul_logerr("No resource manager for %d.%d registered\n",
390 return FRES_ERR_NO_RESOURCE_MANAGER;
392 /* Find allocator for this request */
393 ra = fcb_alloc_find(*resource, app);
396 forb_server_id_to_string(str, app, sizeof(str));
397 ul_logerr("No resource allocator found for %d.%d and %s\n",
398 (*resource)->key.type, (*resource)->key.id, str);
399 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
402 else if (key.type != key2.type ||
404 ul_logerr("Contract #%d differs in resource!\n", i);
405 return FRSH_ERR_RESOURCE_ID_INVALID;
413 * @param resource Resource for which to rebalance capacity and negotiate new contracts
414 * @param fcb_contract New requests to negotiate
415 * @param num The number of elements in @a fcb_contract
416 * @param[out] rl List with changed contracts to be commited
419 prepare_reservation_list(struct resource *resource,
420 struct fcb_contract *fcb_contract[], int num,
421 struct reservation_list *rl)
425 struct fcb_contract *fc;
427 reservation_list_init_head(rl);
429 for (i=0; i<num; i++) {
430 assert(fcb_contract[i]->requested_contract != NULL);
431 reservation_list_insert(rl, fcb_contract[i]);
434 fosa_clock_get_time(CLOCK_REALTIME, &now);
435 ul_list_for_each(sc_contracts, resource, fc) {
436 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
437 fc->requested_contract == NULL) /* Do not insert contract inserted above */
439 reservation_list_insert(rl, fc);
448 * @param resource Resource for which to rebalance capacity and negotiate new contracts
449 * @param rl List with changed contracts to be commited
451 * @return Zero on success, non-zero error code on error
454 rebalance_spare_capacity_and_reserve(struct resource *resource,
455 struct reservation_list *rl)
459 struct fcb_contract *fc;
460 fres_block_spare_capacity *s;
462 fres_contract_ptr_seq contracts;
463 if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
466 contracts._length = rl->length;
468 /* Initialize optimization */
469 ul_list_for_each(reservation_list, rl, fc) {
470 fc->to_be_reserved_contract =
471 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
472 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
474 assert(fc->to_be_reserved_contract != NULL);
476 forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
479 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
480 if (s && s->granularity == FRSH_GR_DISCRETE) {
481 fc->sc_variant.initial = s->variants._length - 1;
482 fc->sc_variant.try = fc->sc_variant.initial;
486 bool all_combinations_tried;
487 int criterion, best_criterion = -1;
488 struct fcb_contract *fcb_changed;
489 /* Exhaustive search. Try all combinations of spare capacity
490 * variants and find the one with best creterion. */
492 all_combinations_tried = true;
495 /* Prepare spare capacity variant */
496 ul_list_for_each(reservation_list, rl, fc) {
497 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
498 /* TODO: Simulate continuous granularity by discretization */
499 if (s && s->granularity == FRSH_GR_DISCRETE) {
500 fres_container_copy(fc->to_be_reserved_contract->container,
501 forb_sequence_elem(&s->variants, fc->sc_variant.try));
502 criterion += fc->sc_variant.try;
504 if (fcb_changed == NULL) {
505 /* Chnage tried variant for the next round */
506 fc->sc_variant.try = fc->sc_variant.try > 0 ?
507 fc->sc_variant.try - 1 :
508 s->variants._length - 1;
509 /* Do not change other
510 * contracts unless we have
511 * tried all variants */
512 if (fc->sc_variant.try != fc->sc_variant.initial) {
516 if (fc->sc_variant.try != fc->sc_variant.initial)
517 all_combinations_tried = false;
521 if (criterion > best_criterion) {
522 CORBA_Environment ev;
523 /* Reserve contract */
524 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
525 if (forb_exception_occurred(&ev)) {
526 ret = fres_forbex2err(&ev);
527 ul_logerr("FORB exception when reserving contracts\n");
531 ul_logerr("Contract reservation error %d\n", ret);
532 ret = FRES_ERR_ADMISSION_TEST;
535 if (ret == 0) { /* negotiation succeeded */
536 best_criterion = criterion;
539 } while (!all_combinations_tried);
541 if (best_criterion == -1) {
542 ret = FRSH_ERR_CONTRACT_REJECTED;
544 /* At least some variant succeeded */
546 sc_contracts_init_head(resource);
547 ul_list_for_each(reservation_list, rl, fc) {
548 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
549 if (s && s->granularity == FRSH_GR_DISCRETE) {
550 sc_contracts_insert(resource, fc);
555 ul_list_for_each(reservation_list, rl, fc) {
556 fres_contract_destroy(fc->to_be_reserved_contract);
557 fc->to_be_reserved_contract = NULL;
559 forb_sequence_free_buf(&contracts, forb_no_destructor);
564 * Create/change VReses according to @a schedulable_contracts.
566 * There might be more allocators for schedulable contracts, so merge
567 * adjacent vreses with the same allocator together and create/change
568 * them in one step. Also the order of schedulable contracts might be
569 * different from the initial order od ids in commit_contracts()
570 * therefore we have to search for every contract.
573 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
575 struct res_alloc *last_ra = NULL;
576 fres_contract_ptr_seq vreses;
578 CORBA_Environment ev;
580 if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
585 for (i=0; i<schedulable_contracts->_length; i++) {
586 struct fcb_contract *fc;
587 struct fres_contract *sc = schedulable_contracts->_buffer[i];
589 log_contract("Changing VRES", i, sc);
591 fc = fcb_contract_find(fcb, &sc->id);
594 if (true /* TODO: if the schedulable contract is changed */) {
595 if (last_ra != fc->ra) {
596 if (vreses._length) {
597 ret = fres_resource_allocator_change_vreses(last_ra->ra,
599 if (forb_exception_occurred(&ev)) {
600 ret = fres_forbex2err(&ev);
607 vreses._buffer[vreses._length] = sc;
609 fres_contract_destroy(fc->schedulable_contract);
610 fc->schedulable_contract = fres_contract_duplicate(sc);
616 ret = fres_resource_allocator_change_vreses(last_ra->ra,
618 if (forb_exception_occurred(&ev))
619 ret = fres_forbex2err(&ev);
622 forb_sequence_free_buf(&vreses, forb_no_destructor);
627 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
630 struct fcb_contract *fc;
631 for (i=0; i<num; i++) {
632 fc = fcb_contracts[i];
633 if (fc && !fc->user_contract) {
634 fc->requested_contract = NULL; /* Destroyed by FORB */
635 fcb_contract_destroy(fc);
642 negotiate_contracts(fres_contract_broker obj,
643 const fres_contract_ptr_seq* contracts,
644 fres_contract_id_seq** ids_out,
645 CORBA_Environment *ev)
647 struct fcb *fcb = o2fcb(obj);
648 struct resource *resource = NULL;
651 fres_contract_ptr_seq *schedulable_contracts;
652 struct fcb_contract **fcb_contracts, *fc;
654 fres_contract_id_seq commit_ids;
655 int num = contracts->_length;
657 /* Prepare output sequence for the case we return eariler with
659 forb_sequence_alloc(*ids_out, 0);
661 ev->major = FORB_EX_NO_MEMORY;
664 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
666 forb_get_req_source(obj, &app);
668 fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
669 if (!fcb_contracts) {
673 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
675 ret = prepare_fcb_contracts(fcb, fcb_contracts,
676 contracts->_buffer, num);
678 goto err_free_fcb_contracts;
679 ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
682 goto err_free_fcb_contracts;
684 struct reservation_list rl;
685 prepare_reservation_list(resource,
689 /* Allocate all the needed memory before doing reservation. If
690 * there is not enough memory, it has no sense to call resource
692 if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
694 goto err_free_fcb_contracts;
697 /* Reserve contracts */
698 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
700 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
701 ul_logmsg("Contract(s) was/were rejected\n");
704 fres_strerror(ret, msg, sizeof(msg));
705 ul_logerr("Reservation error: %s\n", msg);
707 goto err_free_fcb_contracts;
710 /* Commit contracts */
711 commit_ids._length = rl.length;
713 ul_list_for_each(reservation_list, &rl, fc) {
714 forb_sequence_elem(&commit_ids, i) = fc->id;
718 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
719 &schedulable_contracts, ev);
720 if (forb_exception_occurred(ev)) {
721 ret = fres_forbex2err(ev);
722 goto err_cancel_reservation;
725 /* Add new contracts to our fcb database for later
726 * reference. Canceled contracts are removed below. */
727 for (i=0; i<num; i++) {
728 fc = fcb_contracts[i];
730 if (fc->user_contract) {
731 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
733 fres_contract_destroy(fc->user_contract);
734 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
735 /* Note: requested_contract is also
736 * pointed by contracts parameter and
737 * will be freed by FORB. */
738 fc->requested_contract = NULL;
741 /* Insert new contracts */
742 fcb_contract_insert(fcb, fcb_contracts[i]);
743 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
744 fc->requested_contract = NULL;
745 /* See the note above. */
749 ret = change_vreses(fcb, schedulable_contracts);
751 goto err_cancel_reservation;
753 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
755 ul_logerr("VRes was not created\n");
756 goto err_cancel_contracts;
760 /* Return IDs and delete canceled contracts from out database */
761 if (!forb_sequence_alloc_buf(*ids_out, num)) {
762 ev->major = FORB_EX_NO_MEMORY;
763 goto err_cancel_contracts;
765 (*ids_out)->_length = num;
766 for (i=0; i<num; i++) {
767 struct fcb_contract *fc = fcb_contracts[i];
768 forb_sequence_elem(*ids_out, i) = fc->id;
770 if (fc->requested_contract &&
771 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
772 fcb_contract_delete(fcb, fc);
773 /* Note: requested_contract is also pointed by
774 * contracts parameter and will be freed by FORB. */
775 fc->requested_contract = NULL;
776 fcb_contract_destroy(fc);
781 err_cancel_contracts:
783 goto err_free_fcb_contracts;
784 err_cancel_reservation:
785 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
786 err_free_fcb_contracts:
787 free_fcb_contracts(fcb_contracts, num);
792 void redistribute_spare_capacity(fres_contract_broker obj,
793 const frsh_resource_type_t restype,
794 const frsh_resource_id_t resid,
795 CORBA_Environment *ev)
797 struct fcb *fcb = o2fcb(obj);
798 struct res_key key = {restype, resid };
799 struct resource *resource;
800 struct reservation_list rl;
802 resource = fcb_resource_find(fcb, &key);
804 prepare_reservation_list(resource, NULL, 0, &rl);
806 /* forb_sequence_alloc(ids, rl.length); */
807 /* if (!ids || !ids->_buffer) { */
808 /* ev->major = FORB_EX_NO_MEMORY; */
809 /* goto err_free_fcb_contracts; */
811 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
812 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
815 rebalance_spare_capacity_and_reserve(resource, &rl);
819 CORBA_long register_resource(fres_contract_broker obj,
820 const frsh_resource_type_t restype,
821 const frsh_resource_id_t resid,
822 const fres_resource_desc *desc,
823 CORBA_Environment *ev)
825 struct fcb *fcb = o2fcb(obj);
826 struct resource *res, *res2;
828 res = malloc(sizeof(*res));
830 memset(res, 0, sizeof(*res));
831 res->key.type = restype;
833 res2 = fcb_resource_find(fcb, &res->key);
835 if (forb_object_is_stale(res2->mng)) {
836 ul_logmsg("Removing stale manager for resource %d.%d\n",
838 forb_object_release(res2->mng);
839 fcb_resource_delete(fcb, res2);
840 /* TODO: Delete also all allocators associated
841 * with this stale resource manager. */
845 ul_logerr("Resource manager %d.%d already registered\n",
850 res->mng = forb_object_duplicate(desc->manager);
851 res->name = desc->name;
853 fcb_alloc_init_root_field(res);
854 sc_contracts_init_head(res);
855 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
856 res->name, restype, resid);
857 fcb_resource_insert(fcb, res);
866 CORBA_long register_allocator(fres_contract_broker obj,
867 const frsh_resource_type_t restype,
868 const frsh_resource_id_t resid,
869 const fres_resource_allocator ra_obj,
870 CORBA_Environment *ev)
872 struct fcb *fcb = o2fcb(obj);
873 struct resource *res;
874 struct res_alloc *ra;
875 struct res_key resource;
876 forb_server_id server_id;
877 char server_id_str[40];
880 forb_get_server_id(ra_obj, &server_id);
881 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
882 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
883 restype, resid, server_id_str);
885 resource.type = restype;
887 res = fcb_resource_find(fcb, &resource);
889 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
891 ret = FRES_ERR_NO_RESOURCE_MANAGER;
894 ra = fcb_alloc_find(res, &server_id);
896 char *str = forb_object_to_string(ra_obj);
897 ul_logerr("Allocator from already registered (%s)\n",
900 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
903 ra = malloc(sizeof(*ra));
909 ra->ra = forb_object_duplicate(ra_obj);
910 fcb_alloc_insert(res, ra);
916 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
918 struct fcb *fcb = o2fcb(obj);
919 fres_resource_seq *seq;
920 struct resource *res;
923 seq = malloc(sizeof(*seq));
925 ev->major = FORB_EX_NO_MEMORY;
928 memset(seq, 0, sizeof(*seq));
931 gavl_cust_for_each(fcb_resource, fcb, res) {
935 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
940 gavl_cust_for_each(fcb_resource, fcb, res) {
941 seq->_buffer[n].restype = res->key.type;
942 seq->_buffer[n].resid = res->key.id;
943 seq->_buffer[n].desc.manager = res->mng;
944 seq->_buffer[n].desc.name = res->name;
952 negotiate_transaction(fres_contract_broker _obj,
953 const fres_transaction_t* transaction,
954 CORBA_Environment *ev)
956 struct fcb *fcb = o2fcb(_obj);
957 struct fcb_contract **fcb_contracts;
958 const fres_contract_ptr_seq* contracts = &transaction->contracts;
959 int num = contracts->_length;
962 fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
963 if (!fcb_contracts) {
967 memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
969 ret = prepare_fcb_contracts(fcb, fcb_contracts,
970 contracts->_buffer, num);
972 goto err_free_fcb_contracts;
974 return FRSH_ERR_NOT_IMPLEMENTED;
975 err_free_fcb_contracts:
976 free_fcb_contracts(fcb_contracts, num);
982 wait_transaction(fres_contract_broker _obj,
983 const CORBA_char * name,
984 fres_transaction_t** transaction,
985 CORBA_Environment *ev)
987 return FRSH_ERR_NOT_IMPLEMENTED;
991 allocate_transaction_vres(fres_contract_broker _obj,
993 CORBA_Environment *ev)
995 return FRSH_ERR_NOT_IMPLEMENTED;
999 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1000 static int register_inet_port(forb_orb orb)
1002 forb_port_t *port = forb_malloc(sizeof(*port));
1004 struct in_addr listen_on;
1008 memset(port, 0, sizeof(*port));
1009 listen_on.s_addr = INADDR_ANY;
1010 ret = forb_inet_port_init(&port->desc, listen_on, 0);
1013 ret = forb_register_port(orb, port);
1018 struct forb_fres_contract_broker_impl impl = {
1019 .negotiate_contracts = negotiate_contracts,
1020 .register_resource = register_resource,
1021 .register_allocator = register_allocator,
1022 .redistribute_spare_capacity = redistribute_spare_capacity,
1023 .get_resources = get_resources,
1024 .negotiate_transaction = negotiate_transaction,
1025 .wait_transaction = wait_transaction,
1026 .allocate_transaction_vres = allocate_transaction_vres,
1029 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1031 forb_server_id server_id;
1032 char server_id_str[sizeof(forb_server_id)*2+1];
1034 forb_get_server_id(peer_orb, &server_id);
1035 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1037 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1043 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1044 fosa_abs_time_t now;
1045 fosa_rel_time_t delay;
1046 fosa_clock_get_time(CLOCK_REALTIME, &now);
1047 delay = fosa_abs_time_extract_interval(start_time, now);
1048 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1049 fosa_rel_time_to_msec(delay));
1053 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1057 static struct option long_opts[] = {
1058 { "daemon", optional_argument, NULL, 'd' },
1059 { "loglevel", required_argument, NULL, 'l' },
1066 printf("usage: fcb [ options ]\n");
1067 printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
1068 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
1071 int print_log_domain(ul_log_domain_t *domain, void *context)
1073 printf("%s = %d\n", domain->name, domain->level);
1077 int main(int argc, char *argv[])
1080 struct fcb fcb_data;
1081 fres_contract_broker fcb;
1082 forb_executor_t executor;
1084 forb_init_attr_t attr = {
1085 .orb_id = "org.frescor.fcb",
1086 .peer_discovery_callback = peer_discovery_callback,
1087 .peer_dead_callback = peer_dead_callback,
1088 .fixed_tcp_port = FCB_TCP_PORT,
1089 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
1090 .fixed_server_id = FCB_SERVER_ID,
1091 .redistribute_hellos = true,
1096 while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1099 if (*optarg == '?') {
1100 ul_logreg_for_each_domain(print_log_domain, NULL);
1105 ret = ul_log_domain_arg2levels(optarg);
1107 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1112 opt_pidfile = optarg;
1117 exit(opt == 'h' ? 0 : 1);
1121 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1124 forb_daemon_prepare(opt_pidfile);
1126 orb = forb_init(&argc, &argv, &attr);
1127 if (!orb) error(1, errno, "FORB initialization failed");
1129 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1130 ret = register_inet_port(orb);
1131 if (ret) error(0, errno, "INET port registration failed");
1134 fcb_resource_init_root_field(&fcb_data);
1135 fcb_contract_init_root_field(&fcb_data);
1137 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1138 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1140 /* Prepare executor before we register the fcb reference,
1141 * so that no reuqests are lost */
1142 ret = forb_executor_init(&executor);
1143 if (ret) error(1, errno, "forb_executor_init failed");
1145 ret = forb_executor_register_object(&executor, fcb);
1146 if (ret) error(1, errno, "forb_executor_register_object failed");
1148 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1149 if (ret) error(1, errno, "forb_register_reference() failed");
1151 ul_logmsg("Waiting for requests\n");
1153 forb_daemon_ready();
1155 ret = forb_executor_run(&executor);
1156 if (ret) error(1, errno, "forb_executor_run failed");