]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/cbroker/fcb.c
Progress on transaction negotiation
[frescor/frsh.git] / fres / cbroker / fcb.c
index 83d314df83ea7016f5cafa22a5c824fda9002862..7fc217aa4bc07e867713931634c803876804f851 100644 (file)
 #include <forb/proto_inet.h>
 #endif
 
+#define COMPILE_TIME_ASSERT(cond, msg) \
+       typedef char msg[(cond) ? 1 : -1]
+
+
 UL_LOG_CUST(ulogd_fcb);
 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
@@ -142,6 +146,7 @@ struct fcb_contract {
  * Contract broker data
  */
 struct fcb {
+       fres_contract_id_t contract_counter;
        gavl_cust_root_field_t resources; /**< Registered resources */
        gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
 };
@@ -343,7 +348,9 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
 
                if (fres_contract_id_is_empty(&c->id)) {
                        /* Normal negotiation request */
-                       forb_uuid_generate((forb_uuid_t *)&c->id);
+                       COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
+                                           wrong_size_of_contract_id);
+                       c->id = ++fcb->contract_counter;
                        fc = fcb_contract_new(&c->id);
                        if (!fc)
                                return errno;
@@ -502,8 +509,12 @@ reserve_resource(struct resource *resource)
                ul_logerr("Contract reservation error %d\n", ret);
                ret = FRES_ERR_ADMISSION_TEST;
                goto err_free;
-       }
-       ret = 0;
+       } else if (ret == 0)
+               ret = FRSH_NO_ERROR;
+       else if (ret == 1)
+               ret = FRSH_ERR_CONTRACT_REJECTED;
+       else
+               assert(false);
 err_free:
        forb_sequence_free_buf(&contracts, forb_no_destructor);
 err:
@@ -578,11 +589,15 @@ rebalance_spare_capacity_and_reserve(struct resource *resource)
 
                if (criterion > best_criterion) {
                        ret = reserve_resource(resource);
-                       if (ret)
+                       switch (ret) {
+                       case FRSH_NO_ERROR:
+                               best_criterion = criterion;
+                               break;
+                       case FRSH_ERR_CONTRACT_REJECTED:
+                               break;
+                       default:
                                goto err;
-
-                       /* negotiation succeeded */
-                       best_criterion = criterion;
+                       }
                }
        } while (!all_combinations_tried);
 
@@ -1082,14 +1097,12 @@ negotiate_transaction(fres_contract_broker _obj,
        struct fcb *fcb = o2fcb(_obj);
        struct fcb_contract **fcb_contracts, *fc;
        const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
-       int num = user_contracts->_length;
-       int ret;
+       int i, ret, num = user_contracts->_length;
        struct res_array res_array;
-       fres_contract_ptr_seq res_contracts;
-       int i, j;
        struct res_key key;
        struct resource *resource;
 
+       ul_logmsg("Negotiating transaction of %d contracts\n", num);
        if (transaction_has_spare_capacity(transaction)) {
                ret =  FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
                goto err;
@@ -1109,11 +1122,6 @@ negotiate_transaction(fres_contract_broker _obj,
 
        ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
 
-       if (!forb_sequence_alloc_buf(&res_contracts, num)) {
-               ret = errno;
-               goto err_free_resources;
-       }
-
        gsa_cust_for_each(res_array, &res_array, resource) {
                reservation_list_init_head(&resource->rl);
                resource->rl.length = 0;
@@ -1126,26 +1134,26 @@ negotiate_transaction(fres_contract_broker _obj,
                        }
                }
        }
-/*     gsa_cust_for_each(res_array, &res_array, resource) { */
-/*             CORBA_Environment ev; */
-/*             /\* Reserve contract *\/ */
-/*             reservation_list2s */
-/*             ret = fres_resource_manager_reserve_contracts(resource->mng, &res_contracts, &ev); */
-/*             if (forb_exception_occurred(&ev)) { */
-/*                             ret = fres_forbex2err(&ev); */
-/*                             ul_logerr("FORB exception when reserving contracts\n"); */
-/*                             goto err_free_res_contracts; */
-/*                     } */
-/*                     if (ret < 0) { */
-/*                             ul_logerr("Contract reservation error %d\n", ret); */
-/*                             ret = FRES_ERR_ADMISSION_TEST; */
-/*                             goto err; */
-/*                     } */
-/*                     if (ret == 0) { /\* negotiation succeeded *\/ */
-/*                     } */
-/*     } */
+       gsa_cust_for_each(res_array, &res_array, resource) {
+               ret = reserve_resource(resource);
+               if (ret) {
+                       ul_logerr("Reservation failed\n");
+                       goto err_cancel_reservation;
+               }
+       }
+       gsa_cust_for_each(res_array, &res_array, resource) {
+               ret = commit_resource(resource);
+               if (ret) {
+                       ul_logerr("Commit failed\n");
+                       goto err_cancel_reservation;
+               }
+       }
        
        return 0;
+err_cancel_reservation:
+       gsa_cust_for_each(res_array, &res_array, resource) {
+               cancel_reservations(resource);
+       }
 err_free_res_contracts:
        forb_sequence_free_buf(&res_contracts, forb_no_destructor);
 err_free_resources:
@@ -1296,6 +1304,7 @@ int main(int argc, char *argv[])
                }
        }
 
+       memset(&fcb_data, 0, sizeof(fcb_data));
        fosa_clock_get_time(CLOCK_REALTIME, &start_time);
 
        if (opt_daemon)