]> 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 4aa3ec0df05afe0dccb14a8f10efa8396c03ec7a..7fc217aa4bc07e867713931634c803876804f851 100644 (file)
@@ -53,6 +53,7 @@
  * 
  * 
  */
+#define WITH_C99               /* For ul_gsa.h iterators */
 #include <semaphore.h>
 #include <getopt.h>
 #include <forb.h>
@@ -63,6 +64,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <ul_gavlcust.h>
+#include <ul_gsacust.h>
 #include <string.h>
 #include <ul_log.h>
 #include <ul_logreg.h>
 #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);
@@ -83,6 +89,11 @@ bool opt_daemon = false;
 char *opt_pidfile = NULL;
 fosa_abs_time_t start_time;
 
+/** List of contracts to be newly reserved or changed by resource manager */
+struct reservation_list {
+       ul_list_head_t fcb_contracts;
+       unsigned length;
+};
 
 /**
  * Resource identification 
@@ -102,6 +113,7 @@ struct resource {
        fres_resource_manager mng; /**< Object reference of the resource manager */
        gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
        ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
+       struct reservation_list rl; /**< Temporary list of contracts to be reserved on this resource */
 };
 
 /**
@@ -134,16 +146,11 @@ 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 */
 };
 
-/** List of contracts to be reserved during spare capacity rebalancing */
-struct reservation_list {
-       ul_list_head_t fcb_contracts;
-       unsigned length;
-};
-
 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
 {
        struct fcb_contract *fcb_contract;
@@ -265,10 +272,32 @@ GAVL_CUST_NODE_INT_IMP(fcb_contract         /* cust_prefix */,            \
 #include "fcb_contract_gavl.inc"
 #endif
 
+struct res_array {
+       gsa_array_field_t array;
+};
+
+GSA_CUST_DEC(res_array         /* cust_prefix */,
+            struct res_array   /* cust_array_t */,
+            struct resource    /* cust_item_t */,
+            struct res_key     /* cust_key_t */,
+            array              /* cust_array_field */,
+            key                /* cust_item_key */,
+            res_key_cmp        /* cust_cmp_fnc */);
+
+GSA_CUST_IMP(res_array         /* cust_prefix */,
+            struct res_array   /* cust_array_t */,
+            struct resource    /* cust_item_t */,
+            struct res_key     /* cust_key_t */,
+            array              /* cust_array_field */,
+            key                /* cust_item_key */,
+            res_key_cmp        /* cust_cmp_fnc */,
+            true               /* cust_ins_fl */);
+
+
 
 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
 
-struct res_key*
+static struct res_key*
 get_res_key(const struct fres_contract *contract, struct res_key *key)
 {
        fres_block_resource *block_res;
@@ -284,6 +313,16 @@ get_res_key(const struct fres_contract *contract, struct res_key *key)
        return key;
 }
 
+static struct res_key*
+get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
+{
+       if (fc->user_contract)
+               get_res_key(fc->user_contract, key);
+       else
+               get_res_key(fc->requested_contract, key);
+       return key;
+}
+
 /** 
  * Fills in an array of fcb_contracts according to requests submited
  * by an application through negotiate_contracts() or
@@ -303,21 +342,19 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
 {
        unsigned i;
        struct fcb_contract *fc;
-       struct res_key key;
 
        for (i=0; i<num; i++) {
                struct fres_contract *c = contracts[i];
 
                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;
                        fcb_contracts[i] = fc;
-                       if (!get_res_key(c, &key)) {
-                               return FRSH_ERR_RESOURCE_ID_INVALID;
-                       }
                        log_contract("Negotiation request", i, c);
                } else {
                        fc = fcb_contract_find(fcb, &c->id);
@@ -330,13 +367,9 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                                fcb_contracts[i] = fc;
                                if (fres_contract_get_num_blocks(c) == 0) {
                                        /* Cancelation */
-                                       get_res_key(fc->user_contract, &key);
                                        log_contract("Cancelation request", i, fc->user_contract);
                                } else {
                                        /* Renegotiation */
-                                       if (!get_res_key(c, &key)) {
-                                               return FRSH_ERR_RESOURCE_ID_INVALID;
-                                       }
                                        log_contract("Renegotiation request", i, fc->user_contract);
                                }
                        }
@@ -368,18 +401,10 @@ check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
 
        for (i=0; i<num; i++) {
                struct fcb_contract *fc = fcb_contracts[i];
-               struct fres_contract *c = fc->requested_contract;
 
-               if (fres_contract_get_num_blocks(c) == 0) {
-                       /* Cancelation */
-                       get_res_key(fc->user_contract, &key);
-                       log_contract("Cancelation request", i, fc->user_contract);
-               } else {
-                       /* (Re)Negotiation */
-                       if (!get_res_key(c, &key)) {
-                               return FRSH_ERR_RESOURCE_ID_INVALID;
-                       }
-               }
+               if (!get_fc_res_key(fc, &key))
+                       return FRSH_ERR_RESOURCE_ID_INVALID;
+
                /* Check that all contracts are for the same resource */
                if (i==0) {
                        key2 = key;
@@ -410,6 +435,10 @@ check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
 }
 
 /**
+ * Prepares a list of contracts to pass to resource manager for (re)reserving.
+ *
+ * @todo Needs to be changed for compatibility with transactions.
+ * 
  * @param resource Resource for which to rebalance capacity and negotiate new contracts
  * @param fcb_contract New requests to negotiate
  * @param num The number of elements in @a fcb_contract
@@ -417,31 +446,81 @@ check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
  */
 static void
 prepare_reservation_list(struct resource *resource,
-                        struct fcb_contract *fcb_contract[], int num,
-                        struct reservation_list *rl)
+                        struct fcb_contract *fcb_contract[], int num)
 {
        int i;
        fosa_abs_time_t now;
        struct fcb_contract *fc;
 
-       reservation_list_init_head(rl);
-       rl->length = 0;
+       reservation_list_init_head(&resource->rl);
+       resource->rl.length = 0;
        for (i=0; i<num; i++) {
                assert(fcb_contract[i]->requested_contract != NULL);
-               reservation_list_insert(rl, fcb_contract[i]);
-               rl->length++;
+               reservation_list_insert(&resource->rl, fcb_contract[i]);
+               resource->rl.length++;
        }
        fosa_clock_get_time(CLOCK_REALTIME, &now);
        ul_list_for_each(sc_contracts, resource, fc) {
                if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
                    fc->requested_contract == NULL) /* Do not insert contract inserted above */
                {
-                       reservation_list_insert(rl, fc);
-                       rl->length++;
+                       reservation_list_insert(&resource->rl, fc);
+                       resource->rl.length++;
                }
        }
 }
 
+static int
+reserve_resource(struct resource *resource)
+{
+       int ret, i;
+       fres_contract_ptr_seq contracts;
+       struct fres_contract *c;
+       struct fcb_contract *fc;
+       CORBA_Environment ev;
+       
+       if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
+               ret = errno;
+               goto err;
+       }
+       forb_sequence_length(&contracts) = resource->rl.length;
+
+       i=0;
+       /* Prepare FORB sequence  */
+       ul_list_for_each(reservation_list, &resource->rl, fc) {
+               c = NULL;
+               if (fc->to_be_reserved_contract)
+                       /* Contract without spare capacity */
+                       c = fc->to_be_reserved_contract;
+               else if (fc->requested_contract)
+                       c = fc->requested_contract;
+               assert(c);
+               forb_sequence_elem(&contracts, i++) = c;
+       }
+       
+       /* Reserve contract */
+       ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
+       if (forb_exception_occurred(&ev)) {
+               ret = fres_forbex2err(&ev);
+               ul_logerr("FORB exception when reserving contracts\n");
+               goto err_free;
+       }
+       if (ret < 0) {
+               ul_logerr("Contract reservation error %d\n", ret);
+               ret = FRES_ERR_ADMISSION_TEST;
+               goto err_free;
+       } 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:
+       return ret;
+}
+
 /** 
  * 
  * 
@@ -451,20 +530,13 @@ prepare_reservation_list(struct resource *resource,
  * @return Zero on success, non-zero error code on error
  */
 static int
-rebalance_spare_capacity_and_reserve(struct resource *resource,
-                                    struct reservation_list *rl)
+rebalance_spare_capacity_and_reserve(struct resource *resource)
 {
        int ret;
-       unsigned i;
+       struct reservation_list *rl = &resource->rl;
        struct fcb_contract *fc;
        fres_block_spare_capacity *s;
 
-       fres_contract_ptr_seq contracts;
-       if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
-               return errno;
-       }
-       contracts._length = rl->length;
-       i=0;
        /* Initialize optimization */
        ul_list_for_each(reservation_list, rl, fc) {
                fc->to_be_reserved_contract =
@@ -473,9 +545,6 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        NULL;
                assert(fc->to_be_reserved_contract != NULL);
 
-               forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
-               i++;
-               
                s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
                if (s && s->granularity == FRSH_GR_DISCRETE) {
                        fc->sc_variant.initial = s->variants._length - 1;
@@ -519,21 +588,15 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                }
 
                if (criterion > best_criterion) {
-                       CORBA_Environment ev;
-                       /* Reserve contract */
-                       ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
-                       if (forb_exception_occurred(&ev)) {
-                               ret = fres_forbex2err(&ev);
-                               ul_logerr("FORB exception when reserving contracts\n");
-                               goto err;
-                       }
-                       if (ret < 0) {
-                               ul_logerr("Contract reservation error %d\n", ret);
-                               ret = FRES_ERR_ADMISSION_TEST;
-                               goto err;
-                       }
-                       if (ret == 0) { /* negotiation succeeded */
+                       ret = reserve_resource(resource);
+                       switch (ret) {
+                       case FRSH_NO_ERROR:
                                best_criterion = criterion;
+                               break;
+                       case FRSH_ERR_CONTRACT_REJECTED:
+                               break;
+                       default:
+                               goto err;
                        }
                }
        } while (!all_combinations_tried);
@@ -556,7 +619,6 @@ err:
                fres_contract_destroy(fc->to_be_reserved_contract);
                fc->to_be_reserved_contract = NULL;
        }
-       forb_sequence_free_buf(&contracts, forb_no_destructor);
        return ret;
 }
 
@@ -638,6 +700,74 @@ free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
        free(fcb_contracts);
 }
 
+int
+commit_resource(struct resource *resource,
+               fres_contract_ptr_seq **schedulable_contracts)
+{
+       int ret;
+       fres_contract_id_seq commit_ids;
+       int i;
+       struct fcb_contract *fc;
+       CORBA_Environment ev;
+       
+       if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
+               ret = errno;
+               goto err;
+       }
+
+       commit_ids._length = resource->rl.length;
+       i=0;
+       ul_list_for_each(reservation_list, &resource->rl, fc) {
+               forb_sequence_elem(&commit_ids, i) = fc->id;
+               i++;
+       }
+       
+       fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
+                                              schedulable_contracts, &ev);
+       if (forb_exception_occurred(&ev)) {
+               ret = fres_forbex2err(&ev);
+               goto err_free;
+       }
+       return 0;
+err_free:
+       forb_sequence_free_buf(&commit_ids, forb_no_destructor);
+err:
+       return ret;
+}
+
+int cancel_reservations(struct resource *resource)
+{
+       int ret;
+       fres_contract_id_seq commit_ids;
+       int i;
+       struct fcb_contract *fc;
+       CORBA_Environment ev;
+       
+       if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
+               ret = errno;
+               goto err;
+       }
+
+       commit_ids._length = resource->rl.length;
+       i=0;
+       ul_list_for_each(reservation_list, &resource->rl, fc) {
+               forb_sequence_elem(&commit_ids, i) = fc->id;
+               i++;
+       }
+       
+       fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
+       if (forb_exception_occurred(&ev)) {
+               ret = fres_forbex2err(&ev);
+               goto err_free;
+       }
+       return 0;
+err_free:
+       forb_sequence_free_buf(&commit_ids, forb_no_destructor);
+err:
+       return ret;
+}
+
+
 CORBA_long
 negotiate_contracts(fres_contract_broker obj,
                    const fres_contract_ptr_seq* contracts,
@@ -651,7 +781,6 @@ negotiate_contracts(fres_contract_broker obj,
        fres_contract_ptr_seq *schedulable_contracts;
        struct fcb_contract **fcb_contracts, *fc;
        unsigned i;
-       fres_contract_id_seq commit_ids;
        int num = contracts->_length;
 
        /* Prepare output sequence for the case we return eariler with
@@ -681,21 +810,10 @@ negotiate_contracts(fres_contract_broker obj,
        if (ret)
                goto err_free_fcb_contracts;
 
-       struct reservation_list rl;
-       prepare_reservation_list(resource,
-                                fcb_contracts, num,
-                                &rl);
-
-       /* Allocate all the needed memory before doing reservation. If
-        * there is not enough memory, it has no sense to call resource
-        * manager. */
-       if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
-               ret = errno;
-               goto err_free_fcb_contracts;
-       }
+       prepare_reservation_list(resource, fcb_contracts, num);
 
        /* Reserve contracts */
-       ret = rebalance_spare_capacity_and_reserve(resource, &rl);
+       ret = rebalance_spare_capacity_and_reserve(resource);
        if (ret) {
                if (ret == FRSH_ERR_CONTRACT_REJECTED) {
                        ul_logmsg("Contract(s) was/were rejected\n");
@@ -708,19 +826,9 @@ negotiate_contracts(fres_contract_broker obj,
        }
 
        /* Commit contracts */
-       commit_ids._length = rl.length;
-       i=0;
-       ul_list_for_each(reservation_list, &rl, fc) {
-               forb_sequence_elem(&commit_ids, i) = fc->id;
-               i++;
-       }
-       
-       fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
-                                              &schedulable_contracts, ev);
-       if (forb_exception_occurred(ev)) {
-               ret = fres_forbex2err(ev);
+       ret = commit_resource(resource, &schedulable_contracts);
+       if (ret)
                goto err_cancel_reservation;
-       }
 
        /* Add new contracts to our fcb database for later
         * reference. Canceled contracts are removed below. */
@@ -782,7 +890,7 @@ err_cancel_contracts:
        /* TODO */
        goto err_free_fcb_contracts;
 err_cancel_reservation:
-       fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
+       cancel_reservations(resource);
 err_free_fcb_contracts:
        free_fcb_contracts(fcb_contracts, num);
 err:
@@ -797,11 +905,10 @@ void redistribute_spare_capacity(fres_contract_broker obj,
        struct fcb *fcb = o2fcb(obj);
        struct res_key key = {restype, resid };
        struct resource *resource;
-       struct reservation_list rl;
        
        resource = fcb_resource_find(fcb, &key);
 
-       prepare_reservation_list(resource, NULL, 0, &rl);
+       prepare_reservation_list(resource, NULL, 0);
 
 /*     forb_sequence_alloc(ids, rl.length); */
 /*     if (!ids || !ids->_buffer) { */
@@ -812,7 +919,7 @@ void redistribute_spare_capacity(fres_contract_broker obj,
 /*     *ids_out = ids;         /\* ids is freed by FORB *\/ */
        
        
-       rebalance_spare_capacity_and_reserve(resource, &rl);
+       rebalance_spare_capacity_and_reserve(resource);
        /* Commit */
 }
 
@@ -948,16 +1055,58 @@ void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORB
        *resources = seq;
 }
 
+static bool
+transaction_has_spare_capacity(const fres_transaction_t* transaction)
+{
+       struct fres_contract **c;
+       forb_sequence_foreach(&transaction->contracts, c)
+               if (fres_contract_get_spare_capacity(*c))
+                       return true;
+       return false;
+}
+
+static int
+transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
+                         int num, struct res_array *res_array)
+{
+       int i;
+       struct resource *resource;
+       struct res_key key;
+       int ret;
+
+       res_array_init_array_field(res_array);
+       for (i = 0; i < num; i++) {
+               get_fc_res_key(fc[i], &key);
+               resource = fcb_resource_find(fcb, &key);
+               ret = res_array_insert(res_array, resource);
+               if (ret == -1) {
+                       ret = FRSH_ERR_INTERNAL_ERROR;
+                       goto err;
+               }
+       }
+       ret = 0;
+err:
+       return ret;
+}
+                                
 CORBA_long
 negotiate_transaction(fres_contract_broker _obj,
                      const fres_transaction_t* transaction,
                      CORBA_Environment *ev)
 {
        struct fcb *fcb = o2fcb(_obj);
-       struct fcb_contract **fcb_contracts;
-       const fres_contract_ptr_seq* contracts = &transaction->contracts;
-       int num = contracts->_length;
-       int ret;
+       struct fcb_contract **fcb_contracts, *fc;
+       const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
+       int i, ret, num = user_contracts->_length;
+       struct res_array res_array;
+       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;
+       }
 
        fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
        if (!fcb_contracts) {
@@ -967,11 +1116,48 @@ negotiate_transaction(fres_contract_broker _obj,
        memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
 
        ret = prepare_fcb_contracts(fcb, fcb_contracts,
-                                   contracts->_buffer, num);
+                                   user_contracts->_buffer, num);
        if (ret)
                goto err_free_fcb_contracts;
+
+       ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
+
+       gsa_cust_for_each(res_array, &res_array, resource) {
+               reservation_list_init_head(&resource->rl);
+               resource->rl.length = 0;
+               for (i = 0; i < num; i++) {
+                       fc = fcb_contracts[i];
+                       get_fc_res_key(fc, &key);
+                       if (res_key_cmp(&key, &resource->key) == 0) {
+                               reservation_list_insert(&resource->rl, fc);
+                               resource->rl.length++;
+                       }
+               }
+       }
+       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 FRSH_ERR_NOT_IMPLEMENTED;
+       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:
+       res_array_delete_all(&res_array);
 err_free_fcb_contracts:
        free_fcb_contracts(fcb_contracts, num);
 err:
@@ -1118,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)