]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/cbroker/fcb.c
fcb: Work on transaction support continues
[frescor/frsh.git] / fres / cbroker / fcb.c
index 02e99cfefe9cf4ecd220cead20ef83789d176adb..b039ac2b7d3d4bdf9e37ef8e90bc517603bbcc9e 100644 (file)
@@ -53,6 +53,7 @@
  * 
  * 
  */
+#include <semaphore.h>
 #include <getopt.h>
 #include <forb.h>
 #include <forb/config.h>
 #include <fres_contract.h>
 #include "fcb_config.h"
 #include <fosa_clocks_and_timers.h>
+#include "contract_log.h"
 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
 #include <forb/proto_inet.h>
 #endif
 
 UL_LOG_CUST(ulogd_fcb);
-ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
-
+ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
+UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
+       
+bool opt_daemon = false;
+char *opt_pidfile = NULL;
 fosa_abs_time_t start_time;
 
 
@@ -133,7 +138,8 @@ struct fcb {
        gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
 };
 
-/** List of contracts to be reserved during spare capacity rebalancing */
+/** List of contracts to be newly reserved or changed during spare
+ * capacity rebalancing */
 struct reservation_list {
        ul_list_head_t fcb_contracts;
        unsigned length;
@@ -263,7 +269,7 @@ GAVL_CUST_NODE_INT_IMP(fcb_contract         /* cust_prefix */,              \
 
 #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;
@@ -279,30 +285,35 @@ 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
- * to negotiate_contracts(). For every contract in @a contracts, there
- * is one fcb_contract in @a fcb_contracts array.
+ * by an application through negotiate_contracts() or
+ * negotiate_transaction(). For every contract in @a contracts, there
+ * is allocated one fcb_contract in @a fcb_contracts array.
  * 
  * @param fcb
  * @param fcb_contracts Where to store pointers to fcb_contracts
- * @param resource Resource for which negotiation is being done.
- * @param app Application which requests negotiation
- * @param contracts Contracts submited to negotiate_contracts() 
+ * @param contracts Contracts submited for negotiation 
  * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
  * 
  * @return Zero on success, non-zero error code on error.
  */
 static int
 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
-                     struct resource **resource,
-                     forb_server_id *app,
                      struct fres_contract *contracts[], int num)
 {
        unsigned i;
        struct fcb_contract *fc;
-       struct res_alloc *ra;
-       struct res_key key, key2 = {-1,-1};
 
        for (i=0; i<num; i++) {
                struct fres_contract *c = contracts[i];
@@ -314,9 +325,7 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        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);
                        if (!fc) {
@@ -328,16 +337,43 @@ 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);
                                }
                        }
                }
                fc->requested_contract = c;
+       }
+       return 0;
+}
+
+/**
+ * Ensure that all contracts belong to the same resource and find
+ * resource allocators for the contracts.
+ *
+ * @param fcb 
+ * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
+ * @param num Number of contracts in @a fcb_contracts
+ * @param resource Resource for which negotiation is being done.
+ * @param app Application which requests negotiation
+ */
+static int
+check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
+                        struct resource **resource,
+                        forb_server_id *app,
+                        int num)
+{
+       unsigned i;
+       struct res_alloc *ra;
+       struct res_key key, key2 = {-1,-1};
+
+       for (i=0; i<num; i++) {
+               struct fcb_contract *fc = fcb_contracts[i];
+
+               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) {
@@ -346,7 +382,16 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        if (!*resource) {
                                ul_logerr("No resource manager for %d.%d registered\n",
                                          key.type, key.id);
-                               return FRSH_ERR_RESOURCE_ID_INVALID;
+                               return FRES_ERR_NO_RESOURCE_MANAGER;
+                       }
+                       /* Find allocator for this request */
+                       ra = fcb_alloc_find(*resource, app);
+                       if (!ra) {
+                               char str[60];
+                               forb_server_id_to_string(str, app, sizeof(str));
+                               ul_logerr("No resource allocator found for %d.%d and %s\n",
+                                         (*resource)->key.type, (*resource)->key.id, str);
+                               return FRES_ERR_NO_RESOURCE_ALLOCATOR;
                        }
                }
                else if (key.type != key2.type ||
@@ -354,22 +399,16 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        ul_logerr("Contract #%d differs in resource!\n", i);
                        return FRSH_ERR_RESOURCE_ID_INVALID;
                }
-
-               /* Find allocator for this request */
-               ra = fcb_alloc_find(*resource, app);
-               if (!ra) {
-                       char str[60];
-                       forb_server_id_to_string(str, app, sizeof(str));
-                       ul_logerr("No resource allocator found for %d.%d and %s\n",
-                                 (*resource)->key.type, (*resource)->key.id, str);
-                       return FRES_ERR_NO_RESOURCE_ALLOCATOR;
-               }
                fc->ra = ra;
        }       
        return 0;
 }
 
 /**
+ * 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
@@ -420,7 +459,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
        fres_block_spare_capacity *s;
 
        fres_contract_ptr_seq contracts;
-       if (!forb_sequence_alloc_buf(contracts, rl->length)) {
+       if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
                return errno;
        }
        contracts._length = rl->length;
@@ -433,7 +472,7 @@ 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;
+               forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
                i++;
                
                s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
@@ -458,7 +497,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        /* TODO: Simulate continuous granularity by discretization */
                        if (s && s->granularity == FRSH_GR_DISCRETE) {
                                fres_container_copy(fc->to_be_reserved_contract->container,
-                                                   forb_sequence_elem(s->variants, fc->sc_variant.try));
+                                                   forb_sequence_elem(&s->variants, fc->sc_variant.try));
                                criterion += fc->sc_variant.try;
 
                                if (fcb_changed == NULL) {
@@ -516,7 +555,7 @@ err:
                fres_contract_destroy(fc->to_be_reserved_contract);
                fc->to_be_reserved_contract = NULL;
        }
-       forb_sequence_free_buf(contracts, forb_no_destructor);
+       forb_sequence_free_buf(&contracts, forb_no_destructor);
        return ret;
 }
 
@@ -534,10 +573,10 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
 {
        struct res_alloc *last_ra = NULL;
        fres_contract_ptr_seq vreses;
-       int i, ret;
+       int i, ret = 0;
        CORBA_Environment ev;
        
-       if (!forb_sequence_alloc_buf(vreses, schedulable_contracts->_length)) {
+       if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
                return errno;
        }
        vreses._length = 0;
@@ -546,6 +585,8 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                struct fcb_contract *fc;
                struct fres_contract *sc = schedulable_contracts->_buffer[i];
 
+               log_contract("Changing VRES", i, sc);
+
                fc = fcb_contract_find(fcb, &sc->id);
                assert(fc != NULL);
 
@@ -570,15 +611,32 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                
                }
        }
-       ret = fres_resource_allocator_change_vreses(last_ra->ra,
-                                                   &vreses, &ev);
-       if (forb_exception_occurred(&ev))
-               ret = fres_forbex2err(&ev);
+       if (last_ra) {
+               ret = fres_resource_allocator_change_vreses(last_ra->ra,
+                                                           &vreses, &ev);
+               if (forb_exception_occurred(&ev))
+                       ret = fres_forbex2err(&ev);
+       }
 err:
-       forb_sequence_free_buf(vreses, forb_no_destructor);
+       forb_sequence_free_buf(&vreses, forb_no_destructor);
        return ret;
 }
 
+void
+free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
+{
+       int i;
+       struct fcb_contract *fc;
+       for (i=0; i<num; i++) {
+               fc = fcb_contracts[i];
+               if (fc && !fc->user_contract) {
+                       fc->requested_contract = NULL; /* Destroyed by FORB */
+                       fcb_contract_destroy(fc);
+               }
+       }
+       free(fcb_contracts);
+}
+
 CORBA_long
 negotiate_contracts(fres_contract_broker obj,
                    const fres_contract_ptr_seq* contracts,
@@ -593,6 +651,7 @@ negotiate_contracts(fres_contract_broker obj,
        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
         * an error */
@@ -605,27 +664,31 @@ negotiate_contracts(fres_contract_broker obj,
        
        forb_get_req_source(obj, &app);
        
-       fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
+       fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
        if (!fcb_contracts) {
                ret = errno;
                goto err;
        }
-       memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
+       memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
 
-       ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
-                                   &app, contracts->_buffer, contracts->_length);
+       ret = prepare_fcb_contracts(fcb, fcb_contracts,
+                                   contracts->_buffer, num);
+       if (ret)
+               goto err_free_fcb_contracts;
+       ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
+                                      &app, num);
        if (ret)
                goto err_free_fcb_contracts;
 
        struct reservation_list rl;
        prepare_reservation_list(resource,
-                                fcb_contracts, contracts->_length,
+                                fcb_contracts, num,
                                 &rl);
 
        /* Allocate all the needed memory before doing reservation. If
-        * there is no enough memory, it has no sense to call resource
+        * there is not enough memory, it has no sense to call resource
         * manager. */
-       if (!forb_sequence_alloc_buf(commit_ids, rl.length)) {
+       if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
                ret = errno;
                goto err_free_fcb_contracts;
        }
@@ -647,7 +710,7 @@ negotiate_contracts(fres_contract_broker obj,
        commit_ids._length = rl.length;
        i=0;
        ul_list_for_each(reservation_list, &rl, fc) {
-               forb_sequence_elem(commit_ids, i) = fc->id;
+               forb_sequence_elem(&commit_ids, i) = fc->id;
                i++;
        }
        
@@ -660,7 +723,7 @@ negotiate_contracts(fres_contract_broker obj,
 
        /* Add new contracts to our fcb database for later
         * reference. Canceled contracts are removed below. */
-       for (i=0; i<contracts->_length; i++) {
+       for (i=0; i<num; i++) {
                fc =  fcb_contracts[i];
 
                if (fc->user_contract) {
@@ -694,14 +757,14 @@ negotiate_contracts(fres_contract_broker obj,
 
 
        /* Return IDs and delete canceled contracts from out database */
-       if (!forb_sequence_alloc_buf(**ids_out, contracts->_length)) {
+       if (!forb_sequence_alloc_buf(*ids_out, num)) {
                ev->major = FORB_EX_NO_MEMORY;
                goto err_cancel_contracts;
        }
-       (*ids_out)->_length = contracts->_length;
-       for (i=0; i<contracts->_length; i++) {
+       (*ids_out)->_length = num;
+       for (i=0; i<num; i++) {
                struct fcb_contract *fc =  fcb_contracts[i];
-               forb_sequence_elem(**ids_out, i) = fc->id;
+               forb_sequence_elem(*ids_out, i) = fc->id;
 
                if (fc->requested_contract &&
                    fres_contract_get_num_blocks(fc->requested_contract) == 0) {
@@ -720,14 +783,7 @@ err_cancel_contracts:
 err_cancel_reservation:
        fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
 err_free_fcb_contracts:
-       for (i=0; i<contracts->_length; i++) {
-               fc = fcb_contracts[i];
-               if (fc && !fc->user_contract) {
-                       fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
-                       fcb_contract_destroy(fcb_contracts[i]);
-               }
-       }
-       free(fcb_contracts);
+       free_fcb_contracts(fcb_contracts, num);
 err:
        return ret;
 }
@@ -818,6 +874,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        struct res_key resource;
        forb_server_id server_id;
        char server_id_str[40];
+       int ret;
 
        forb_get_server_id(ra_obj, &server_id);
        forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
@@ -830,6 +887,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        if (!res) {
                ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
                          restype, resid);
+               ret = FRES_ERR_NO_RESOURCE_MANAGER;
                goto err;
        }
        ra = fcb_alloc_find(res, &server_id);
@@ -838,10 +896,12 @@ CORBA_long register_allocator(fres_contract_broker obj,
                ul_logerr("Allocator from already registered (%s)\n",
                          str);
                forb_free(str);
+               ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
                goto err;
        }
        ra = malloc(sizeof(*ra));
        if (!ra) {
+               ret = ENOMEM;
                goto err;
        }
        ra->app = server_id;
@@ -849,7 +909,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        fcb_alloc_insert(res, ra);
        return 0;
 err:
-       return FRSH_ERR_RESOURCE_ID_INVALID;
+       return ret;
 }
 
 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
@@ -887,6 +947,96 @@ 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 resource **resources[])
+{
+       int i, j, r, alloc;
+       struct resource **res;
+       struct resource *resource;
+       struct res_key key;
+
+       alloc = 10;
+       res = malloc(alloc*sizeof(*res));
+       if (!res) 
+               return errno;
+       res[0] = NULL;
+       for (r = 0, i = 0; i < num; i++) {
+               get_fc_res_key(fc[i], &key);
+               resource = fcb_resource_find(fcb, &key);
+               /* TODO: Check whether to use GSA for resources. */
+       }
+       res[r] = NULL;
+       return 0;
+}
+                                
+
+
+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;
+
+       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) {
+               ret = errno;
+               goto err;
+       }
+       memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
+
+       ret = prepare_fcb_contracts(fcb, fcb_contracts,
+                                   contracts->_buffer, num);
+       if (ret)
+               goto err_free_fcb_contracts;
+
+       //ret = transaction_get_resources(fcb_contracts, num, &resources);
+       
+       return FRSH_ERR_NOT_IMPLEMENTED;
+err_free_fcb_contracts:
+       free_fcb_contracts(fcb_contracts, num);
+err:
+       return ret;
+}
+
+CORBA_long
+wait_transaction(fres_contract_broker _obj,
+                const CORBA_char * name,
+                fres_transaction_t** transaction,
+                CORBA_Environment *ev)
+{
+       return FRSH_ERR_NOT_IMPLEMENTED;
+}
+
+CORBA_long
+allocate_transaction_vres(fres_contract_broker _obj,
+                         const CORBA_long id,
+                         CORBA_Environment *ev)
+{
+       return FRSH_ERR_NOT_IMPLEMENTED;
+}
+
+
 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
 static int register_inet_port(forb_orb orb)
 {
@@ -912,6 +1062,9 @@ struct forb_fres_contract_broker_impl impl = {
        .register_allocator = register_allocator,
        .redistribute_spare_capacity = redistribute_spare_capacity,
        .get_resources = get_resources,
+       .negotiate_transaction = negotiate_transaction,
+       .wait_transaction = wait_transaction,
+       .allocate_transaction_vres = allocate_transaction_vres,
 };
 
 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
@@ -943,7 +1096,8 @@ void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
 }
 
 static struct option long_opts[] = {
-    { "loglevel", 1, 0, 'l' },
+    { "daemon",   optional_argument, NULL, 'd' },
+    { "loglevel", required_argument, NULL, 'l' },
     { 0, 0, 0, 0}
 };
 
@@ -951,9 +1105,16 @@ static void
 usage(void)
 {
        printf("usage: fcb [ options ]\n");
+       printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
        printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
 }
 
+int print_log_domain(ul_log_domain_t *domain, void *context)
+{
+       printf("%s = %d\n", domain->name, domain->level);
+       return 0;
+}
+
 int main(int argc, char *argv[])
 {
        forb_orb orb;
@@ -968,16 +1129,28 @@ int main(int argc, char *argv[])
                .fixed_tcp_port = FCB_TCP_PORT,
 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT          
                .fixed_server_id = FCB_SERVER_ID,
+               .redistribute_hellos = true,
 #endif
        };
        int  opt;
 
-       ul_logreg_domain(&ulogd_fcb);
-
-       while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
+       while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
                switch (opt) {
                        case 'l':
-                               ul_log_domain_arg2levels(optarg);
+                               if (*optarg == '?') {
+                                       ul_logreg_for_each_domain(print_log_domain, NULL);
+                                       exit(0);
+                               }
+                               {
+                                       int ret;
+                                       ret = ul_log_domain_arg2levels(optarg);
+                                       if (ret) 
+                                               error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
+                               }
+                               break;
+                       case 'd':
+                               opt_daemon = true;
+                               opt_pidfile = optarg;
                                break;
                        case 'h':
                        /*default:*/
@@ -988,6 +1161,9 @@ int main(int argc, char *argv[])
 
        fosa_clock_get_time(CLOCK_REALTIME, &start_time);
 
+       if (opt_daemon)
+               forb_daemon_prepare(opt_pidfile);
+
        orb = forb_init(&argc, &argv, &attr);
        if (!orb) error(1, errno, "FORB initialization failed");
 
@@ -1014,6 +1190,9 @@ int main(int argc, char *argv[])
        if (ret) error(1, errno, "forb_register_reference() failed");
 
        ul_logmsg("Waiting for requests\n");
+       if (opt_daemon)
+               forb_daemon_ready();
+
        ret = forb_executor_run(&executor);
        if (ret) error(1, errno, "forb_executor_run failed");