]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/frsh/fres/cbroker/fcb.c
Added debugging outputs
[frescor/frsh-forb.git] / src / frsh / fres / cbroker / fcb.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
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                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
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.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
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.                                              */
37 /*                                                                        */
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 /**************************************************************************/
46
47 /**
48  * @file   fcb.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Mon Oct 20 18:00:18 2008
51  * 
52  * @brief  FRESCOR Contract Broker
53  * 
54  * 
55  */
56 #define WITH_C99                /* For ul_gsa.h iterators */
57 #include <semaphore.h>
58 #include <getopt.h>
59 #include <forb.h>
60 #include <forb/config.h>
61 #include <fcb.h>
62 #include <fcb_contact_info.h>
63 #include <error.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <ul_gavlcust.h>
67 #include <ul_gsacust.h>
68 #include <string.h>
69 #include <ul_log.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>
78 #endif
79
80 #define COMPILE_TIME_ASSERT(cond, msg) \
81         typedef char msg[(cond) ? 1 : -1]
82
83
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);
87         
88 bool opt_daemon = false;
89 char *opt_pidfile = NULL;
90 fosa_abs_time_t start_time;
91
92 /** List of contracts to be newly reserved or changed by resource manager */
93 struct reservation_list {
94         ul_list_head_t fcb_contracts;
95         unsigned length;
96 };
97
98 /**
99  * Resource identification 
100  */
101 struct res_key {
102         frsh_resource_type_t type;
103         frsh_resource_id_t id;
104 };
105
106 /**
107  * Registered resource
108  */
109 struct resource {
110         gavl_node_t node;
111         struct res_key key;
112         char *name;
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 */
117 };
118
119 /**
120  * Resource allocator registered in different nodes/applications 
121  */
122 struct res_alloc {
123         gavl_node_t node;
124         forb_server_id app;
125         fres_resource_allocator ra;
126 };
127
128 struct fcb_transaction;
129
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;
137         struct {
138                 int initial;
139                 int try;
140         } sc_variant;
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;
146 };
147
148 struct fcb_transaction {
149         char *name;
150         gavl_node_t node;
151         gsa_array_field_t contracts;
152 };
153
154 /**
155  * Contract broker data
156  */
157 struct fcb {
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 */
162 };
163
164 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
165 {
166         struct fcb_contract *fcb_contract;
167         
168         fcb_contract = malloc(sizeof(*fcb_contract));
169         if (!fcb_contract) {
170                 return NULL;
171         }
172         memset(fcb_contract, 0, sizeof(*fcb_contract));
173         fcb_contract->id = *id;
174
175         return fcb_contract;
176 }
177
178 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
179 {
180         if (fcb_contract->user_contract) {
181                 fres_contract_destroy(fcb_contract->user_contract);
182         }
183         if (fcb_contract->to_be_reserved_contract) {
184                 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
185         }
186         if (fcb_contract->requested_contract) {
187                 fres_contract_destroy(fcb_contract->requested_contract);
188         }
189         if (fcb_contract->schedulable_contract) {
190                 fres_contract_destroy(fcb_contract->requested_contract);
191         }
192         free(fcb_contract);
193 }
194
195 static inline int
196 unsorted(const void *a, const void *b)
197 { abort(); }
198
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 */);
207
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 */);
216
217 struct fcb_transaction *fcb_transaction_new(char *name)
218 {
219         struct fcb_transaction *fcb_transaction;
220         
221         fcb_transaction = malloc(sizeof(*fcb_transaction));
222         if (!fcb_transaction) {
223                 return NULL;
224         }
225         memset(fcb_transaction, 0, sizeof(*fcb_transaction));
226         fcb_transaction->name = strdup(name);
227
228         tran_contract_init_array_field(fcb_transaction);
229
230         return fcb_transaction;
231 }
232
233 void fcb_transaction_destroy(struct fcb_transaction *fcb_transaction)
234 {
235         free(fcb_transaction->name);
236         free(fcb_transaction);
237 }
238
239 static inline int res_key_cmp(const struct res_key *a,
240                               const struct res_key *b)
241 {
242         if (a->type < b->type) {
243                 return -1;
244         } else if (a->type > b->type) {
245                 return +1;
246         } else if (a->id < b->id) {
247                 return -1;
248         } else if (a->id > b->id) {
249                 return +1;
250         } else {
251                 return 0;
252         }
253 }
254
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 */);
264
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 */);
273
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 */);
283
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 */);
292
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 */);
299
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 */);
305
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 */);
315
316 #if 1
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 */);
325 #else
326 #include "fcb_contract_gavl.inc"
327 #include <forb/forb.h>
328 #endif
329
330 static inline int
331 fcb_transaction_cmp(char * const *a, char * const *b)
332 { return strcmp(*a, *b); }
333
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 */);
342
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 */);
351
352 struct res_array {
353         gsa_array_field_t array;
354 };
355
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 */);
363
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 */);
372
373
374
375 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
376
377 static struct res_key*
378 get_res_key(const struct fres_contract *contract, struct res_key *key)
379 {
380         fres_block_resource *block_res;
381         
382         block_res = fres_contract_get_resource(contract);
383         if (!block_res) {
384                 ul_logerr("No resource specified\n");
385                 return NULL;
386         } else {
387                 key->type = block_res->resource_type;
388                 key->id = block_res->resource_id;
389         }
390         return key;
391 }
392
393 static struct res_key*
394 get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
395 {
396         if (fc->user_contract)
397                 get_res_key(fc->user_contract, key);
398         else
399                 get_res_key(fc->requested_contract, key);
400         return key;
401 }
402
403 /** 
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.
408  * 
409  * @param fcb
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).
413  * 
414  * @return Zero on success, non-zero error code on error.
415  */
416 static int
417 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
418                       struct fres_contract *contracts[], int num)
419 {
420         unsigned i;
421         struct fcb_contract *fc;
422
423         for (i=0; i<num; i++) {
424                 struct fres_contract *c = contracts[i];
425
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);
432                         if (!fc)
433                                 return errno;
434                         fcb_contracts[i] = fc;
435                         log_contract("Negotiation request", i, c);
436                 } else {
437                         fc = fcb_contract_find(fcb, &c->id);
438                         if (!fc) {
439                                 char str[60];
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;
443                         } else {
444                                 fcb_contracts[i] = fc;
445                                 if (fres_contract_get_num_blocks(c) == 0) {
446                                         /* Cancelation */
447                                         log_contract("Cancelation request", i, fc->user_contract);
448                                 } else {
449                                         /* Renegotiation */
450                                         log_contract("Renegotiation request", i, fc->user_contract);
451                                 }
452                         }
453                 }
454                 fc->requested_contract = c;
455         }
456         return 0;
457 }
458
459 /**
460  * Ensure that all contracts belong to the same resource and find
461  * resource allocators for the contracts.
462  *
463  * @param fcb 
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
468  */
469 static int
470 check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
471                          struct resource **resource,
472                          forb_server_id *app,
473                          int num)
474 {
475         unsigned i;
476         struct res_alloc *ra = NULL;
477         struct res_key key, key2 = {-1,-1};
478
479         for (i=0; i<num; i++) {
480                 struct fcb_contract *fc = fcb_contracts[i];
481
482                 if (!get_fc_res_key(fc, &key))
483                         return FRSH_ERR_RESOURCE_ID_INVALID;
484
485                 /* Check that all contracts are for the same resource */
486                 if (i==0) {
487                         key2 = key;
488                         *resource = fcb_resource_find(fcb, &key);
489                         if (!*resource) {
490                                 ul_logerr("No resource manager for %d.%d registered\n",
491                                           key.type, key.id);
492                                 return FRES_ERR_NO_RESOURCE_MANAGER;
493                         }
494                         /* Find allocator for this request */
495                         ra = fcb_alloc_find(*resource, app);
496                         if (!ra) {
497                                 char str[60];
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;
502                         }
503                 }
504                 else if (key.type != key2.type ||
505                          key.id   != key2.id) {
506                         ul_logerr("Contract #%d differs in resource!\n", i);
507                         return FRSH_ERR_RESOURCE_ID_INVALID;
508                 }
509                 fc->ra = ra;
510         }       
511         return 0;
512 }
513
514 /**
515  * Prepares a list of contracts to pass to resource manager for (re)reserving.
516  *
517  * @todo Needs to be changed for compatibility with transactions.
518  * 
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
523  */
524 static void
525 prepare_reservation_list(struct resource *resource,
526                          struct fcb_contract *fcb_contract[], int num)
527 {
528         int i;
529         fosa_abs_time_t now;
530         struct fcb_contract *fc;
531
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++;
538         }
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 */
543                 {
544                         reservation_list_insert(&resource->rl, fc);
545                         resource->rl.length++;
546                 }
547         }
548 }
549
550 static int
551 reserve_resource(struct resource *resource)
552 {
553         int ret, i;
554         fres_contract_ptr_seq contracts;
555         struct fres_contract *c;
556         struct fcb_contract *fc;
557         CORBA_Environment ev;
558         
559         if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
560                 ret = errno;
561                 goto err;
562         }
563         forb_sequence_length(&contracts) = resource->rl.length;
564
565         i=0;
566         /* Prepare FORB sequence  */
567         ul_list_for_each(reservation_list, &resource->rl, fc) {
568                 c = NULL;
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;
574                 assert(c);
575                 forb_sequence_elem(&contracts, i++) = c;
576         }
577         
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");
583                 goto err_free;
584         }
585         if (ret < 0) {
586                 ul_logerr("Contract reservation error %d\n", ret);
587                 ret = FRES_ERR_ADMISSION_TEST;
588                 goto err_free;
589         } else if (ret == 0)
590                 ret = FRSH_NO_ERROR;
591         else if (ret == 1)
592                 ret = FRSH_ERR_CONTRACT_REJECTED;
593         else
594                 assert(false);
595 err_free:
596         forb_sequence_free_buf(&contracts, forb_no_destructor);
597 err:
598         return ret;
599 }
600
601 /** 
602  * 
603  * 
604  * @param resource Resource for which to rebalance capacity and negotiate new contracts
605  * @param rl List with changed contracts to be commited
606  * 
607  * @return Zero on success, non-zero error code on error
608  */
609 static int
610 rebalance_spare_capacity_and_reserve(struct resource *resource)
611 {
612         int ret;
613         struct reservation_list *rl = &resource->rl;
614         struct fcb_contract *fc;
615         fres_block_spare_capacity *s;
616
617         ul_logmsg("rebalance_spare_capacity_and_reserve - started\n");
618
619         /* Initialize optimization */
620         ul_list_for_each(reservation_list, rl, fc) {
621                 fc->to_be_reserved_contract =
622                         fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
623                         fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
624                         NULL;
625                 assert(fc->to_be_reserved_contract != NULL);
626
627                 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
628                 if (s && s->granularity == FRSH_GR_DISCRETE) {
629                         fc->sc_variant.initial = s->variants._length - 1;
630                         fc->sc_variant.try = fc->sc_variant.initial;
631                 }
632         }
633
634         bool all_combinations_tried;
635         int criterion, best_criterion = -1;
636         struct fcb_contract *fcb_changed;
637         /* Exhaustive search. Try all combinations of spare capacity
638          * variants and find the one with best creterion. */
639         do {
640                 all_combinations_tried = true;
641                 fcb_changed = NULL;
642                 criterion = 0;
643                 /* Prepare spare capacity variant */
644                 ul_list_for_each(reservation_list, rl, fc) {
645                         s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
646                         /* TODO: Simulate continuous granularity by discretization */
647                         if (s && s->granularity == FRSH_GR_DISCRETE) {
648                                 fres_container_copy(fc->to_be_reserved_contract->container,
649                                                     forb_sequence_elem(&s->variants, fc->sc_variant.try));
650                                 criterion += fc->sc_variant.try;
651
652                                 if (fcb_changed == NULL) {
653                                         /* Chnage tried variant for the next round */
654                                         fc->sc_variant.try = fc->sc_variant.try > 0 ?
655                                                 fc->sc_variant.try - 1 :
656                                                 s->variants._length - 1;
657                                         /* Do not change other
658                                          * contracts unless we have
659                                          * tried all variants */
660                                         if (fc->sc_variant.try != fc->sc_variant.initial) {
661                                                 fcb_changed = fc;
662                                         }
663                                 }
664                                 if (fc->sc_variant.try != fc->sc_variant.initial)
665                                         all_combinations_tried = false;
666                         }
667                 }
668
669                 if (criterion > best_criterion) {
670                         ret = reserve_resource(resource);
671                         switch (ret) {
672                         case FRSH_NO_ERROR:
673                                 best_criterion = criterion;
674                                 break;
675                         case FRSH_ERR_CONTRACT_REJECTED:
676                                 break;
677                         default:
678                                 goto err;
679                         }
680                 }
681         } while (!all_combinations_tried);
682
683         if (best_criterion == -1) {
684                 ret = FRSH_ERR_CONTRACT_REJECTED;
685         } else {
686                 /* At least some variant succeeded */
687                 ret = 0;
688                 sc_contracts_init_head(resource);
689                 ul_list_for_each(reservation_list, rl, fc) {
690                         s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
691                         if (s && s->granularity == FRSH_GR_DISCRETE) {
692                                 sc_contracts_insert(resource, fc);
693                         }
694                 }
695         }
696 err:
697         ul_list_for_each(reservation_list, rl, fc) {
698                 fres_contract_destroy(fc->to_be_reserved_contract);
699                 fc->to_be_reserved_contract = NULL;
700         }
701
702         ul_logmsg("rebalance_spare_capacity_and_reserve - finished\n");
703
704         return ret;
705 }
706
707 /**
708  * Create/change VReses according to @a schedulable_contracts.
709  *
710  * There might be more allocators for schedulable contracts, so merge
711  * adjacent vreses with the same allocator together and create/change
712  * them in one step. Also the order of schedulable contracts might be
713  * different from the initial order od ids in commit_contracts()
714  * therefore we have to search for every contract.
715  */
716 static int
717 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
718 {
719         struct res_alloc *last_ra = NULL;
720         fres_contract_ptr_seq vreses;
721         int i, ret = 0;
722         CORBA_Environment ev;
723         
724         if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
725                 return errno;
726         }
727         vreses._length = 0;
728         
729         for (i=0; i<schedulable_contracts->_length; i++) {
730                 struct fcb_contract *fc;
731                 struct fres_contract *sc = schedulable_contracts->_buffer[i];
732
733                 log_contract("Changing VRES", i, sc);
734
735                 fc = fcb_contract_find(fcb, &sc->id);
736                 assert(fc != NULL);
737
738                 if (true /* TODO: if the schedulable contract is changed */) {
739                         if (last_ra != fc->ra) {
740                                 if (vreses._length) {
741                                         ret = fres_resource_allocator_change_vreses(last_ra->ra,
742                                                                                     &vreses, &ev);
743                                         if (forb_exception_occurred(&ev)) {
744                                                 ret = fres_forbex2err(&ev);
745                                                 goto err;
746                                         }
747                                         if (ret) goto err;
748                                 }
749                                 vreses._length = 0;
750                         }
751                         vreses._buffer[vreses._length] = sc;
752                         vreses._length++;
753                         last_ra = fc->ra;
754                 
755                 }
756         }
757         if (last_ra) {
758                 ret = fres_resource_allocator_change_vreses(last_ra->ra,
759                                                             &vreses, &ev);
760                 if (forb_exception_occurred(&ev))
761                         ret = fres_forbex2err(&ev);
762         }
763 err:
764         forb_sequence_free_buf(&vreses, forb_no_destructor);
765         return ret;
766 }
767
768 void
769 free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
770 {
771         int i;
772         struct fcb_contract *fc;
773         for (i=0; i<num; i++) {
774                 fc = fcb_contracts[i];
775                 if (fc && !fc->user_contract) {
776                         fc->requested_contract = NULL; /* Destroyed by FORB */
777                         fcb_contract_destroy(fc);
778                 }
779         }
780         free(fcb_contracts);
781 }
782
783 int
784 commit_resource(struct resource *resource,
785                 fres_contract_ptr_seq **schedulable_contracts)
786 {
787         int ret;
788         fres_contract_id_seq commit_ids;
789         int i;
790         struct fcb_contract *fc;
791         CORBA_Environment ev;
792         
793         if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
794                 ret = errno;
795                 goto err;
796         }
797
798         commit_ids._length = resource->rl.length;
799         i=0;
800         ul_list_for_each(reservation_list, &resource->rl, fc) {
801                 forb_sequence_elem(&commit_ids, i) = fc->id;
802                 i++;
803         }
804         
805         fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
806                                                schedulable_contracts, &ev);
807         if (forb_exception_occurred(&ev)) {
808                 ret = fres_forbex2err(&ev);
809                 goto err_free;
810         }
811         return 0;
812 err_free:
813         forb_sequence_free_buf(&commit_ids, forb_no_destructor);
814 err:
815         return ret;
816 }
817
818 int cancel_reservations(struct resource *resource)
819 {
820         int ret;
821         fres_contract_id_seq commit_ids;
822         int i;
823         struct fcb_contract *fc;
824         CORBA_Environment ev;
825         
826         if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
827                 ret = errno;
828                 goto err;
829         }
830
831         commit_ids._length = resource->rl.length;
832         i=0;
833         ul_list_for_each(reservation_list, &resource->rl, fc) {
834                 forb_sequence_elem(&commit_ids, i) = fc->id;
835                 i++;
836         }
837         
838         fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
839         if (forb_exception_occurred(&ev)) {
840                 ret = fres_forbex2err(&ev);
841                 goto err_free;
842         }
843         
844         return 0;
845 err_free:
846         forb_sequence_free_buf(&commit_ids, forb_no_destructor);
847 err:
848         return ret;
849 }
850
851 /* Add new contracts to our fcb database for later
852  * reference. Canceled contracts are removed below. */
853 int
854 fcb_remember_contracts(struct fcb *fcb,
855                        struct fcb_contract **fcb_contracts, int num)
856 {
857         struct fcb_contract *fc;
858         int i;
859         
860         for (i=0; i<num; i++) {
861                 fc =  fcb_contracts[i];
862
863                 if (fc->user_contract) {
864                         if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
865                                 /* Renegotiation */
866                                 fres_contract_destroy(fc->user_contract);
867                                 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
868                                 /* Note: requested_contract is also
869                                  * pointed by contracts parameter and
870                                  * will be freed by FORB. */
871                                 fc->requested_contract = NULL;
872                         }
873                 } else {
874                         /* Insert new contracts */
875                         fcb_contract_insert(fcb, fcb_contracts[i]);
876                         fc->user_contract = fres_contract_duplicate(fc->requested_contract);
877                         fc->requested_contract = NULL;
878                         /* See the note above. */
879                 }
880         }
881         return 0;
882 }
883
884 int
885 fcb_remember_schedulable_contracts(struct fcb *fcb,
886                                    fres_contract_ptr_seq *schedulable_contracts)
887 {
888         struct fcb_contract *fc;
889         int i;
890         for (i=0; i<schedulable_contracts->_length; i++) {
891                 struct fres_contract *sc = schedulable_contracts->_buffer[i];
892                 fc = fcb_contract_find(fcb, &sc->id);
893                 assert(fc != NULL);
894                 fres_contract_destroy(fc->schedulable_contract);
895                 fc->schedulable_contract = fres_contract_duplicate(sc);
896                 assert(fc->schedulable_contract);
897         }
898         return 0;
899 }
900
901 static int
902 reject_transactions(struct fcb_contract *fcb_contracts[], int num)
903 {
904         int i;
905         for (i = 0; i < num; i++) {
906                 if (fcb_contracts[i]->transaction)
907                         return FRES_ERR_VRES_PART_OF_TRANSACTION;
908         }
909         return 0;
910 }
911
912 CORBA_long
913 negotiate_contracts(fres_contract_broker obj,
914                     const fres_contract_ptr_seq* contracts,
915                     fres_contract_id_seq** ids_out,
916                     CORBA_Environment *ev)
917 {
918         struct fcb *fcb = o2fcb(obj);
919         struct resource *resource = NULL;
920         int ret = 0;
921         forb_server_id app;
922         fres_contract_ptr_seq *schedulable_contracts;
923         struct fcb_contract **fcb_contracts;
924         unsigned i;
925         int num = contracts->_length;
926
927         /* Prepare output sequence for the case we return eariler with
928          * an error */
929         forb_sequence_alloc(*ids_out, 0);
930         if (!*ids_out) {
931                 ev->major = FORB_EX_NO_MEMORY;
932                 goto err;
933         }
934         CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
935         
936         forb_get_req_source(obj, &app);
937         
938         fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
939         if (!fcb_contracts) {
940                 ret = errno;
941                 goto err;
942         }
943         memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
944
945         ret = prepare_fcb_contracts(fcb, fcb_contracts,
946                                     contracts->_buffer, num);
947         if (ret)
948                 goto err_free_fcb_contracts;
949
950         ret = reject_transactions(fcb_contracts, num);
951         if (ret)
952                 goto err_free_fcb_contracts;
953         ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
954                                        &app, num);
955         if (ret)
956                 goto err_free_fcb_contracts;
957
958         prepare_reservation_list(resource, fcb_contracts, num);
959
960         /* Reserve contracts */
961         ret = rebalance_spare_capacity_and_reserve(resource);
962         if (ret) {
963                 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
964                         ul_logmsg("Contract(s) was/were rejected\n");
965                 } else {
966                         char msg[100];
967                         fres_strerror(ret, msg, sizeof(msg));
968                         ul_logerr("Reservation error: %s\n", msg);
969                 }
970                 goto err_free_fcb_contracts;
971         }
972
973         /* Commit contracts */
974         ret = commit_resource(resource, &schedulable_contracts);
975         if (ret)
976                 goto err_cancel_reservation;
977
978         fcb_remember_contracts(fcb, fcb_contracts, num);
979         fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
980
981         ret = change_vreses(fcb, schedulable_contracts);
982         if (ret)
983                 goto err_cancel_reservation;
984         
985         forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
986         if (ret != 0) {
987                 ul_logerr("VRes was not created\n");
988                 goto err_cancel_contracts;
989         }
990
991
992         /* Return IDs and delete canceled contracts from out database */
993         if (!forb_sequence_alloc_buf(*ids_out, num)) {
994                 ev->major = FORB_EX_NO_MEMORY;
995                 goto err_cancel_contracts;
996         }
997         (*ids_out)->_length = num;
998         for (i=0; i<num; i++) {
999                 struct fcb_contract *fc =  fcb_contracts[i];
1000                 forb_sequence_elem(*ids_out, i) = fc->id;
1001
1002                 if (fc->requested_contract &&
1003                     fres_contract_get_num_blocks(fc->requested_contract) == 0) {
1004                         fcb_contract_delete(fcb, fc);
1005                         /* Note: requested_contract is also pointed by
1006                          * contracts parameter and will be freed by FORB. */
1007                         fc->requested_contract = NULL;
1008                         fcb_contract_destroy(fc);
1009                 }
1010         }
1011         return 0;
1012
1013 err_cancel_contracts:
1014         /* TODO */
1015         goto err_free_fcb_contracts;
1016 err_cancel_reservation:
1017         cancel_reservations(resource);
1018 err_free_fcb_contracts:
1019         free_fcb_contracts(fcb_contracts, num);
1020 err:
1021         return ret;
1022 }
1023
1024 void redistribute_spare_capacity(fres_contract_broker obj,
1025                                  const frsh_resource_type_t restype,
1026                                  const frsh_resource_id_t resid,
1027                                  CORBA_Environment *ev)
1028 {
1029         struct fcb *fcb = o2fcb(obj);
1030         struct res_key key = {restype, resid };
1031         struct resource *resource;
1032         const frsh_resource_type_t restype_to_find = restype;
1033         
1034         resource = fcb_resource_find(fcb, &key);
1035
1036         //TODO: delete debug output
1037         printf("\ncalled function 'redistribute_spare_capacity'\n");
1038
1039         struct fcb_contract * fc;
1040         struct res_key * rk;
1041
1042         gavl_cust_for_each(fcb_contract, fcb, fc) {
1043                         rk = get_fc_res_key(fc, &key);
1044                         if(rk->type == restype_to_find) {
1045                                 reservation_list_insert(&resource->rl, fc);
1046                                 resource->rl.length++;
1047                         }
1048         }
1049
1050 /*      forb_sequence_alloc(ids, rl.length); */
1051 /*      if (!ids || !ids->_buffer) { */
1052 /*              ev->major = FORB_EX_NO_MEMORY; */
1053 /*              goto err_free_fcb_contracts; */
1054 /*      } */
1055 /*      CORBA_sequence_set_release(ids, CORBA_TRUE); */
1056 /*      *ids_out = ids;         /\* ids is freed by FORB *\/ */
1057         
1058         rebalance_spare_capacity_and_reserve(resource);
1059 }
1060
1061 CORBA_long register_resource(fres_contract_broker obj,
1062                             const frsh_resource_type_t restype,
1063                             const frsh_resource_id_t resid,
1064                             const fres_resource_desc *desc,
1065                             CORBA_Environment *ev)
1066 {
1067         struct fcb *fcb = o2fcb(obj);
1068         struct resource *res, *res2;
1069
1070         res = malloc(sizeof(*res));
1071         if (!res) goto err;
1072         memset(res, 0, sizeof(*res));
1073         res->key.type = restype;
1074         res->key.id = resid;
1075         res2 = fcb_resource_find(fcb, &res->key);
1076         if (res2) {
1077                 if (forb_object_is_stale(res2->mng)) {
1078                         ul_logmsg("Removing stale manager for resource %d.%d\n",
1079                                   restype, resid);
1080                         forb_object_release(res2->mng);
1081                         fcb_resource_delete(fcb, res2);
1082                         /* TODO: Delete also all allocators associated
1083                          * with this stale resource manager. */
1084                         free(res);
1085                         res = res2;
1086                 } else {
1087                         ul_logerr("Resource manager %d.%d already registered\n",
1088                                   restype, resid);
1089                         goto free_err;
1090                 }
1091         }
1092         res->mng = forb_object_duplicate(desc->manager);
1093         res->name = desc->name;
1094
1095         fcb_alloc_init_root_field(res);
1096         sc_contracts_init_head(res);
1097         ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
1098                   res->name, restype, resid);
1099         fcb_resource_insert(fcb, res);
1100         return 0;
1101 free_err:
1102         free(res);
1103 err:
1104         return -1;
1105 }
1106
1107
1108 CORBA_long register_allocator(fres_contract_broker obj,
1109                               const frsh_resource_type_t restype,
1110                               const frsh_resource_id_t resid,
1111                               const fres_resource_allocator ra_obj,
1112                               CORBA_Environment *ev)
1113 {
1114         struct fcb *fcb = o2fcb(obj);
1115         struct resource *res;
1116         struct res_alloc *ra;
1117         struct res_key resource;
1118         forb_server_id server_id;
1119         char server_id_str[40];
1120         int ret;
1121
1122         forb_get_server_id(ra_obj, &server_id);
1123         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1124         ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
1125                   restype, resid, server_id_str);
1126         
1127         resource.type = restype;
1128         resource.id = resid;
1129         res = fcb_resource_find(fcb, &resource);
1130         if (!res) {
1131                 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
1132                           restype, resid);
1133                 ret = FRES_ERR_NO_RESOURCE_MANAGER;
1134                 goto err;
1135         }
1136         ra = fcb_alloc_find(res, &server_id);
1137         if (ra) {
1138                 char *str = forb_object_to_string(ra_obj);
1139                 ul_logerr("Allocator from already registered (%s)\n",
1140                           str);
1141                 forb_free(str);
1142                 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
1143                 goto err;
1144         }
1145         ra = malloc(sizeof(*ra));
1146         if (!ra) {
1147                 ret = ENOMEM;
1148                 goto err;
1149         }
1150         ra->app = server_id;
1151         ra->ra = forb_object_duplicate(ra_obj);
1152         fcb_alloc_insert(res, ra);
1153         return 0;
1154 err:
1155         return ret;
1156 }
1157
1158 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
1159 {
1160         struct fcb *fcb = o2fcb(obj);
1161         fres_resource_seq *seq;
1162         struct resource *res;
1163         int n;
1164
1165         seq = malloc(sizeof(*seq));
1166         if (!seq) {
1167                 ev->major = FORB_EX_NO_MEMORY;
1168                 return;
1169         }
1170         memset(seq, 0, sizeof(*seq));
1171
1172         n=0;
1173         gavl_cust_for_each(fcb_resource, fcb, res) {
1174                 n++;
1175         }
1176
1177         seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
1178         seq->_maximum = n;
1179         seq->_length = n;
1180
1181         n = 0;
1182         gavl_cust_for_each(fcb_resource, fcb, res) {
1183                 seq->_buffer[n].restype = res->key.type;
1184                 seq->_buffer[n].resid = res->key.id;
1185                 seq->_buffer[n].desc.manager = res->mng;
1186                 seq->_buffer[n].desc.name = res->name;
1187                 n++;
1188         }
1189
1190         *resources = seq;
1191 }
1192
1193 static bool
1194 transaction_has_spare_capacity(const fres_transaction_t* transaction)
1195 {
1196         struct fres_contract **c;
1197         forb_sequence_foreach(&transaction->contracts, c)
1198                 if (fres_contract_get_spare_capacity(*c))
1199                         return true;
1200         return false;
1201 }
1202
1203 static int
1204 transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
1205                           int num, struct res_array *res_array)
1206 {
1207         int i;
1208         struct resource *resource;
1209         struct res_key key;
1210         int ret;
1211
1212         for (i = 0; i < num; i++) {
1213                 get_fc_res_key(fc[i], &key);
1214                 resource = fcb_resource_find(fcb, &key);
1215                 ret = res_array_insert(res_array, resource);
1216                 if (ret == -1) {
1217                         ret = FRSH_ERR_INTERNAL_ERROR;
1218                         goto err;
1219                 }
1220         }
1221         ret = 0;
1222 err:
1223         return ret;
1224 }
1225                                  
1226 CORBA_long
1227 negotiate_transaction(fres_contract_broker _obj,
1228                       const fres_transaction_t* transaction,
1229                       CORBA_Environment *ev)
1230 {
1231         struct fcb *fcb = o2fcb(_obj);
1232         struct fcb_contract **fcb_contracts, *fc;
1233         const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
1234         int i, ret, num = user_contracts->_length;
1235         struct res_array res_array;
1236         struct res_key key;
1237         struct resource *resource;
1238         fres_contract_ptr_seq *schedulable_contracts;
1239         struct fcb_transaction *ft;
1240
1241         ul_logmsg("Negotiating transaction '%s' with %d contracts\n", transaction->name, num);
1242         if (transaction_has_spare_capacity(transaction)) {
1243                 ret =  FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
1244                 goto err;
1245         }
1246
1247         fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
1248         if (!fcb_contracts) {
1249                 ret = errno;
1250                 goto err;
1251         }
1252         memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
1253
1254         ret = prepare_fcb_contracts(fcb, fcb_contracts,
1255                                     user_contracts->_buffer, num);
1256         if (ret)
1257                 goto err_free_fcb_contracts;
1258
1259         res_array_init_array_field(&res_array);
1260         ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
1261
1262         gsa_cust_for_each(res_array, &res_array, resource) {
1263                 reservation_list_init_head(&resource->rl);
1264                 resource->rl.length = 0;
1265                 for (i = 0; i < num; i++) {
1266                         fc = fcb_contracts[i];
1267                         get_fc_res_key(fc, &key);
1268                         if (res_key_cmp(&key, &resource->key) == 0) {
1269                                 reservation_list_insert(&resource->rl, fc);
1270                                 resource->rl.length++;
1271                         }
1272                 }
1273         }
1274         gsa_cust_for_each(res_array, &res_array, resource) {
1275                 ret = reserve_resource(resource);
1276                 if (ret) {
1277                         ul_logerr("Reservation failed\n");
1278                         goto err_cancel_reservation;
1279                 }
1280         }
1281         fcb_remember_contracts(fcb, fcb_contracts, num);
1282         gsa_cust_for_each(res_array, &res_array, resource) {
1283                 ret = commit_resource(resource, &schedulable_contracts);
1284                 if (ret) {
1285                         ul_logerr("Commit failed\n");
1286                         goto err_cancel_reservation;
1287                 }
1288                 fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
1289                 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
1290         }
1291         /* TODO: After we distinguish between APP and a real
1292          * allocator, allocate VRESes (and perform mode change) here.
1293          * Only if the resource allocator is not known at this point,
1294          * wait with allocation the same way as it is done now. For
1295          * this we will need more information about allocators i.e.
1296          * its type (centralized, distributed, in app, ...). Too
1297          * compilcated ;-( */
1298
1299         ft = fcb_transaction_new(transaction->name);
1300         if (!ft) {
1301                 ret = errno;
1302                 goto err_cancel_reservation;
1303         }
1304         enum { NONE, CANCELATION, NEGOTIATION } op = NONE;
1305         for (i = 0; i < num; i++) {
1306                 fc =  fcb_contracts[i];
1307                 if (fc->requested_contract &&
1308                     fres_contract_get_num_blocks(fc->requested_contract) == 0) {
1309                         assert(i == 0 || op == CANCELATION);
1310                         op = CANCELATION;
1311                         /* TODO: Review and test transaction cancelation!!! */
1312                         fcb_contract_delete(fcb, fc);
1313                         /* Note: requested_contract is also pointed by
1314                          * contracts parameter and will be freed by FORB. */
1315                         fc->requested_contract = NULL;
1316                         fcb_contract_destroy(fc);
1317                 } else {
1318                         assert(i == 0 || op == NEGOTIATION);
1319                         op = NEGOTIATION;
1320                         tran_contract_insert_at(ft, fc, i);
1321                         fc->transaction = ft;
1322                 }
1323         }
1324         switch (op) {
1325         case NEGOTIATION:
1326                 fcb_transaction_insert(fcb, ft);
1327                 break;
1328         case CANCELATION:
1329                 fcb_transaction_delete(fcb, ft);
1330                 fcb_transaction_destroy(ft);
1331                 break;
1332         default:
1333                 assert(false);
1334         }
1335         res_array_delete_all(&res_array);
1336         return 0;
1337 err_cancel_reservation:
1338         gsa_cust_for_each(res_array, &res_array, resource) {
1339                 cancel_reservations(resource);
1340         }
1341         res_array_delete_all(&res_array);
1342 err_free_fcb_contracts:
1343         free_fcb_contracts(fcb_contracts, num);
1344 err:
1345         return ret;
1346 }
1347
1348 CORBA_long
1349 wait_transaction(fres_contract_broker _obj,
1350                  const CORBA_char * name,
1351                  fres_transaction_t** transaction,
1352                  CORBA_Environment *ev)
1353 {
1354         return FRSH_ERR_NOT_IMPLEMENTED;
1355 }
1356
1357 CORBA_long
1358 allocate_transaction_vres(fres_contract_broker _obj,
1359                           const CORBA_char * name,
1360                           const CORBA_long index,
1361                           fres_contract_id_t *id,
1362                           CORBA_Environment *ev)
1363 {
1364         struct fcb *fcb = o2fcb(_obj);
1365         struct fcb_transaction *ft;
1366         struct fcb_contract *fc;
1367         int ret;
1368         struct res_alloc *ra;
1369         char * const *n = (char * const *)&name;
1370         struct res_key key;
1371         struct resource *resource;
1372         fres_contract_ptr_seq vreses;
1373         CORBA_Environment ev2;
1374         forb_server_id app;
1375 #define GOTO(label, error) do { ret = (error); goto label; } while(0)
1376
1377         ul_logmsg("Allocate transaction VRES '%s'[%d]\n", name, index);
1378         
1379         ft = fcb_transaction_find(fcb, n);
1380         if (!ft) GOTO(err, FRES_ERR_TRANSACTION_NOT_FOUND);
1381
1382         fc = tran_contract_indx2item(ft, index);
1383         if (!fc) GOTO(err, FRSH_ERR_TOO_LARGE);
1384
1385         /* TODO: If we allow migration of task between resources (due
1386          * to spare capacity reallocation), the following check will
1387          * need reworking/enhancing. */
1388         if (fc->ra) GOTO(err, FRES_ERR_VRES_ALREADY_ALLOCATED);
1389
1390         if (!get_fc_res_key(fc, &key))
1391                 GOTO(err, FRSH_ERR_RESOURCE_ID_INVALID);
1392         resource = fcb_resource_find(fcb, &key);
1393         if (!resource)
1394                 GOTO(err, FRES_ERR_NO_RESOURCE_MANAGER);
1395
1396         forb_get_req_source(_obj, &app);
1397         /* Find allocator for the requested VRES */
1398         ra = fcb_alloc_find(resource, &app);
1399         if (!ra) {
1400                 char str[60];
1401                 forb_server_id_to_string(str, &app, sizeof(str));
1402                 ul_logerr("No resource allocator found for %d.%d and %s\n",
1403                           resource->key.type, resource->key.id, str);
1404                 GOTO(err, FRES_ERR_NO_RESOURCE_ALLOCATOR);
1405         }
1406         fc->ra = ra;
1407
1408         if (!forb_sequence_alloc_buf(&vreses, 1))
1409                 GOTO(err, errno);
1410
1411         assert(fc->schedulable_contract);
1412         forb_sequence_elem(&vreses, 0) = fc->schedulable_contract;
1413         forb_sequence_length(&vreses) = 1;
1414         ret = fres_resource_allocator_change_vreses(ra->ra, &vreses, &ev2);
1415         ul_logerr("Err = %d\n", ret);
1416         if (forb_exception_occurred(&ev2))
1417                 GOTO(err_free, fres_forbex2err(&ev2));
1418         if (ret)
1419                 goto err_free;
1420         *id = fc->id;
1421         
1422         ret = 0;
1423 err_free:
1424         forb_sequence_free_buf(&vreses, forb_no_destructor);
1425 err:
1426         return ret;
1427 }
1428
1429
1430 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1431 static int register_inet_port(forb_orb orb)
1432 {
1433         forb_port_t *port = forb_malloc(sizeof(*port));
1434         int ret;
1435         struct in_addr listen_on;
1436         
1437         if (!port)
1438                 return -1;
1439         memset(port, 0, sizeof(*port));
1440         listen_on.s_addr = INADDR_ANY;
1441         ret = forb_inet_port_init(&port->desc, listen_on, 0);
1442         if (ret)
1443                 return ret;
1444         ret = forb_register_port(orb, port);
1445         return ret;
1446 }
1447 #endif
1448
1449 struct forb_fres_contract_broker_impl impl = {
1450         .negotiate_contracts = negotiate_contracts,
1451         .register_resource = register_resource,
1452         .register_allocator = register_allocator,
1453         .redistribute_spare_capacity = redistribute_spare_capacity,
1454         .get_resources = get_resources,
1455         .negotiate_transaction = negotiate_transaction,
1456         .wait_transaction = wait_transaction,
1457         .allocate_transaction_vres = allocate_transaction_vres,
1458 };
1459
1460 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
1461 {
1462         forb_server_id server_id;
1463         char server_id_str[sizeof(forb_server_id)*2+1];
1464
1465         forb_get_server_id(peer_orb, &server_id);
1466         forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
1467 #if 0
1468         ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
1469 #endif
1470
1471         if (orb_id == NULL)
1472                 return;
1473
1474         if (strcmp(orb_id, "org.frescor.fcb") == 0) {
1475                 fosa_abs_time_t now;
1476                 fosa_rel_time_t delay;
1477                 fosa_clock_get_time(CLOCK_REALTIME, &now);
1478                 delay = fosa_abs_time_extract_interval(start_time, now);
1479                 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
1480                           fosa_rel_time_to_msec(delay));
1481         }
1482 }
1483
1484 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
1485 {
1486 }
1487
1488 static struct option long_opts[] = {
1489     { "loglevel", required_argument, NULL, 'l' },
1490     { 0, 0, 0, 0}
1491 };
1492
1493 static void
1494 usage(void)
1495 {
1496         printf("usage: fcb [ options ]\n");
1497         printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
1498 }
1499
1500 int print_log_domain(ul_log_domain_t *domain, void *context)
1501 {
1502         printf("%s = %d\n", domain->name, domain->level);
1503         return 0;
1504 }
1505
1506 int forb_main(forb_orb orb, int argc, char *argv[])
1507 {
1508         struct fcb fcb_data;
1509         fres_contract_broker fcb;
1510         forb_executor_t executor;
1511         int ret;
1512         int  opt;
1513
1514         while ((opt = getopt_long(argc, argv, "hl:", &long_opts[0], NULL)) != EOF) {
1515                 switch (opt) {
1516                         case 'l':
1517                                 if (*optarg == '?') {
1518                                         ul_logreg_for_each_domain(print_log_domain, NULL);
1519                                         exit(0);
1520                                 }
1521                                 {
1522                                         int ret;
1523                                         ret = ul_log_domain_arg2levels(optarg);
1524                                         if (ret) 
1525                                                 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1526                                 }
1527                                 break;
1528                         case 'h':
1529                         /*default:*/
1530                                 usage();
1531                                 exit(opt == 'h' ? 0 : 1);
1532                 }
1533         }
1534
1535         memset(&fcb_data, 0, sizeof(fcb_data));
1536         fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1537
1538
1539
1540 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1541         ret = register_inet_port(orb);
1542         if (ret) error(0, errno, "INET port registration failed");
1543 #endif
1544
1545         fcb_resource_init_root_field(&fcb_data);
1546         fcb_contract_init_root_field(&fcb_data);
1547         fcb_transaction_init_root_field(&fcb_data);
1548
1549         fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1550         if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1551
1552         /* Prepare executor before we register the fcb reference,
1553          * so that no reuqests are lost */
1554         ret = forb_executor_init(&executor);
1555         if (ret) error(1, errno, "forb_executor_init failed");
1556         
1557         ret = forb_executor_register_object(&executor, fcb);
1558         if (ret) error(1, errno, "forb_executor_register_object failed");
1559
1560         ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1561         if (ret) error(1, errno, "forb_register_reference() failed");
1562
1563         ul_logmsg("Waiting for requests\n");
1564         
1565         forb_signal_server_ready(orb);
1566
1567         ret = forb_executor_run(&executor);
1568         if (ret) error(1, errno, "forb_executor_run failed");
1569         
1570         return 0;
1571 }